Skip to content

Commit ade638e

Browse files
committed
inline functions in p2p_handler.go
1 parent c60a425 commit ade638e

1 file changed

Lines changed: 29 additions & 26 deletions

File tree

block/internal/syncing/p2p_handler.go

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,41 @@ func (h *P2PHandler) OnHeightProcessed(height uint64) {
6363
// ProcessHeaderRange scans the provided heights and emits events when both the
6464
// header and data are available.
6565
func (h *P2PHandler) ProcessHeaderRange(ctx context.Context, startHeight, endHeight uint64, heightInCh chan<- common.DAHeightEvent) {
66-
h.processRange(ctx, startHeight, endHeight, heightInCh, "header_range")
66+
if startHeight > endHeight {
67+
return
68+
}
69+
70+
for height := startHeight; height <= endHeight; height++ {
71+
h.mu.Lock()
72+
shouldProcess := height > h.processedHeight
73+
h.mu.Unlock()
74+
75+
if !shouldProcess {
76+
continue
77+
}
78+
h.processHeight(ctx, height, heightInCh, "header_range")
79+
}
6780
}
6881

6982
// ProcessDataRange scans the provided heights and emits events when both the
7083
// header and data are available.
7184
func (h *P2PHandler) ProcessDataRange(ctx context.Context, startHeight, endHeight uint64, heightInCh chan<- common.DAHeightEvent) {
72-
h.processRange(ctx, startHeight, endHeight, heightInCh, "data_range")
73-
}
74-
75-
func (h *P2PHandler) processRange(ctx context.Context, startHeight, endHeight uint64, heightInCh chan<- common.DAHeightEvent, source string) {
7685
if startHeight > endHeight {
7786
return
7887
}
7988

8089
for height := startHeight; height <= endHeight; height++ {
81-
if !h.shouldProcessHeight(height) {
90+
h.mu.Lock()
91+
shouldProcess := height > h.processedHeight
92+
h.mu.Unlock()
93+
94+
if !shouldProcess {
8295
continue
8396
}
84-
h.processHeight(ctx, height, heightInCh, source)
97+
h.processHeight(ctx, height, heightInCh, "data_range")
8598
}
8699
}
87100

88-
func (h *P2PHandler) shouldProcessHeight(height uint64) bool {
89-
h.mu.Lock()
90-
defer h.mu.Unlock()
91-
return height > h.processedHeight
92-
}
93-
94101
func (h *P2PHandler) processHeight(ctx context.Context, height uint64, heightInCh chan<- common.DAHeightEvent, source string) {
95102
header, err := h.headerStore.GetByHeight(ctx, height)
96103
if err != nil {
@@ -123,19 +130,6 @@ func (h *P2PHandler) processHeight(ctx context.Context, height uint64, heightInC
123130
return
124131
}
125132

126-
h.emitEvent(height, header, data, heightInCh, source)
127-
}
128-
129-
// assertExpectedProposer validates the proposer address.
130-
func (h *P2PHandler) assertExpectedProposer(proposerAddr []byte) error {
131-
if !bytes.Equal(h.genesis.ProposerAddress, proposerAddr) {
132-
return fmt.Errorf("proposer address mismatch: got %x, expected %x",
133-
proposerAddr, h.genesis.ProposerAddress)
134-
}
135-
return nil
136-
}
137-
138-
func (h *P2PHandler) emitEvent(height uint64, header *types.SignedHeader, data *types.Data, heightInCh chan<- common.DAHeightEvent, source string) {
139133
event := common.DAHeightEvent{
140134
Header: header,
141135
Data: data,
@@ -151,3 +145,12 @@ func (h *P2PHandler) emitEvent(height uint64, header *types.SignedHeader, data *
151145

152146
h.logger.Debug().Uint64("height", height).Str("source", source).Msg("processed event from P2P")
153147
}
148+
149+
// assertExpectedProposer validates the proposer address.
150+
func (h *P2PHandler) assertExpectedProposer(proposerAddr []byte) error {
151+
if !bytes.Equal(h.genesis.ProposerAddress, proposerAddr) {
152+
return fmt.Errorf("proposer address mismatch: got %x, expected %x",
153+
proposerAddr, h.genesis.ProposerAddress)
154+
}
155+
return nil
156+
}

0 commit comments

Comments
 (0)