-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathp2p_handler.go
More file actions
126 lines (109 loc) · 3.44 KB
/
p2p_handler.go
File metadata and controls
126 lines (109 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package syncing
import (
"bytes"
"context"
"fmt"
"sync"
goheader "github.com/celestiaorg/go-header"
"github.com/rs/zerolog"
"github.com/evstack/ev-node/block/internal/cache"
"github.com/evstack/ev-node/block/internal/common"
"github.com/evstack/ev-node/pkg/genesis"
"github.com/evstack/ev-node/types"
)
// P2PHandler fan-outs store updates from the go-header stores into the syncer.
// It ensures both the header and data for a given height are present and
// consistent before emitting an event to the syncer.
type P2PHandler struct {
headerStore goheader.Store[*types.SignedHeader]
dataStore goheader.Store[*types.Data]
cache cache.Manager
genesis genesis.Genesis
logger zerolog.Logger
mu sync.Mutex
processedHeight uint64 // highest block fully applied by the syncer
}
// NewP2PHandler creates a new P2P handler.
func NewP2PHandler(
headerStore goheader.Store[*types.SignedHeader],
dataStore goheader.Store[*types.Data],
cache cache.Manager,
genesis genesis.Genesis,
logger zerolog.Logger,
) *P2PHandler {
return &P2PHandler{
headerStore: headerStore,
dataStore: dataStore,
cache: cache,
genesis: genesis,
logger: logger.With().Str("component", "p2p_handler").Logger(),
}
}
// SetProcessedHeight updates the highest processed block height.
func (h *P2PHandler) SetProcessedHeight(height uint64) {
h.mu.Lock()
if height > h.processedHeight {
h.processedHeight = height
}
h.mu.Unlock()
}
// ProcessHeight waits until both header and data for the given height are available.
// Once available, it validates and emits the event to the provided channel or stores it as pending.
func (h *P2PHandler) ProcessHeight(ctx context.Context, height uint64, heightInCh chan<- common.DAHeightEvent) error {
h.mu.Lock()
shouldProcess := height > h.processedHeight
h.mu.Unlock()
if !shouldProcess {
return nil
}
header, err := h.headerStore.GetByHeight(ctx, height)
if err != nil {
if ctx.Err() == nil {
h.logger.Debug().Uint64("height", height).Err(err).Msg("header unavailable in store")
}
return err
}
if err := h.assertExpectedProposer(header.ProposerAddress); err != nil {
h.logger.Debug().Uint64("height", height).Err(err).Msg("invalid header from P2P")
return err
}
data, err := h.dataStore.GetByHeight(ctx, height)
if err != nil {
if ctx.Err() == nil {
h.logger.Debug().Uint64("height", height).Err(err).Msg("data unavailable in store")
}
return err
}
dataCommitment := data.DACommitment()
if !bytes.Equal(header.DataHash[:], dataCommitment[:]) {
err := fmt.Errorf("data hash mismatch: header %x, data %x", header.DataHash, dataCommitment)
h.logger.Warn().Uint64("height", height).Err(err).Msg("discarding inconsistent block from P2P")
return err
}
event := common.DAHeightEvent{
Header: header,
Data: data,
DaHeight: 0,
Source: common.SourceP2P,
}
select {
case heightInCh <- event:
default:
h.cache.SetPendingEvent(event.Header.Height(), &event)
}
h.mu.Lock()
if height > h.processedHeight {
h.processedHeight = height
}
h.mu.Unlock()
h.logger.Debug().Uint64("height", height).Msg("processed event from P2P")
return nil
}
// assertExpectedProposer validates the proposer address.
func (h *P2PHandler) assertExpectedProposer(proposerAddr []byte) error {
if !bytes.Equal(h.genesis.ProposerAddress, proposerAddr) {
return fmt.Errorf("proposer address mismatch: got %x, expected %x",
proposerAddr, h.genesis.ProposerAddress)
}
return nil
}