Skip to content

Commit 50c17d4

Browse files
committed
align p2p handler with p2p broadcast
1 parent 5c30004 commit 50c17d4

4 files changed

Lines changed: 15 additions & 92 deletions

File tree

block/internal/syncing/p2p_handler.go

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -77,29 +77,23 @@ func (h *P2PHandler) ProcessHeaderRange(ctx context.Context, startHeight, endHei
7777
continue
7878
}
7979

80-
// Get corresponding data
80+
// Get corresponding data (empty data are still broadcasted by peers)
8181
var data *types.Data
82-
if bytes.Equal(header.DataHash, common.DataHashForEmptyTxs) {
83-
// Create empty data for headers with empty data hash
84-
data = h.createEmptyDataForHeader(ctx, header)
85-
} else {
86-
// Try to get data from data store with timeout
87-
timeoutCtx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
88-
retrievedData, err := h.dataStore.GetByHeight(timeoutCtx, height)
89-
cancel()
90-
91-
if err != nil {
92-
if errors.Is(err, context.DeadlineExceeded) {
93-
h.logger.Debug().Uint64("height", height).Msg("timeout waiting for data from store, will retry later")
94-
// Don't continue processing if data is not available
95-
// Store event with header only for later processing
96-
continue
97-
}
98-
h.logger.Debug().Uint64("height", height).Err(err).Msg("could not retrieve data for header from data store")
82+
timeoutCtx, cancel = context.WithTimeout(ctx, 500*time.Millisecond)
83+
retrievedData, err := h.dataStore.GetByHeight(timeoutCtx, height)
84+
cancel()
85+
86+
if err != nil {
87+
if errors.Is(err, context.DeadlineExceeded) {
88+
h.logger.Debug().Uint64("height", height).Msg("timeout waiting for data from store, will retry later")
89+
// Don't continue processing if data is not available
90+
// Store event with header only for later processing
9991
continue
10092
}
101-
data = retrievedData
93+
h.logger.Debug().Uint64("height", height).Err(err).Msg("could not retrieve data for header from data store")
94+
continue
10295
}
96+
data = retrievedData
10397

10498
// further header validation (signature) is done in validateBlock.
10599
// we need to be sure that the previous block n-1 was executed before validating block n
@@ -201,34 +195,3 @@ func (h *P2PHandler) assertExpectedProposer(proposerAddr []byte) error {
201195
}
202196
return nil
203197
}
204-
205-
// createEmptyDataForHeader creates empty data for headers with empty data hash
206-
func (h *P2PHandler) createEmptyDataForHeader(ctx context.Context, header *types.SignedHeader) *types.Data {
207-
headerHeight := header.Height()
208-
var lastDataHash types.Hash
209-
210-
if headerHeight > 1 {
211-
// Try to get previous data hash with a short timeout, but don't fail if not available
212-
timeoutCtx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
213-
prevData, err := h.dataStore.GetByHeight(timeoutCtx, headerHeight-1)
214-
cancel()
215-
216-
if err == nil && prevData != nil {
217-
lastDataHash = prevData.Hash()
218-
} else {
219-
h.logger.Debug().Uint64("current_height", headerHeight).Uint64("previous_height", headerHeight-1).
220-
Msg("previous block not available, using empty last data hash")
221-
}
222-
}
223-
224-
metadata := &types.Metadata{
225-
ChainID: header.ChainID(),
226-
Height: headerHeight,
227-
Time: header.BaseHeader.Time,
228-
LastDataHash: lastDataHash,
229-
}
230-
231-
return &types.Data{
232-
Metadata: metadata,
233-
}
234-
}

block/internal/syncing/p2p_handler_test.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -222,46 +222,6 @@ func TestP2PHandler_ProposerMismatch_Rejected(t *testing.T) {
222222
require.Len(t, events, 0)
223223
}
224224

225-
func TestP2PHandler_CreateEmptyDataForHeader_UsesPreviousDataHash(t *testing.T) {
226-
p2pData := setupP2P(t)
227-
ctx := context.Background()
228-
229-
// Prepare a header at height 10
230-
signedHeader := p2pMakeSignedHeader(t, p2pData.Genesis.ChainID, 10, p2pData.ProposerAddr, p2pData.ProposerPub, p2pData.Signer)
231-
signedHeader.DataHash = common.DataHashForEmptyTxs
232-
233-
// Mock previous data at height 9 so handler can propagate its hash
234-
previousData := makeData(p2pData.Genesis.ChainID, 9, 1)
235-
p2pData.DataStore.EXPECT().GetByHeight(mock.Anything, uint64(9)).Return(previousData, nil).Once()
236-
237-
emptyData := p2pData.Handler.createEmptyDataForHeader(ctx, signedHeader)
238-
require.NotNil(t, emptyData, "handler should synthesize empty data when header declares empty data hash")
239-
require.Equal(t, p2pData.Genesis.ChainID, emptyData.ChainID(), "synthesized data should carry header chain ID")
240-
require.Equal(t, uint64(10), emptyData.Height(), "synthesized data should carry header height")
241-
require.Equal(t, signedHeader.BaseHeader.Time, emptyData.Metadata.Time, "synthesized data should carry header time")
242-
require.Equal(t, previousData.Hash(), emptyData.LastDataHash, "synthesized data should propagate previous data hash")
243-
}
244-
245-
func TestP2PHandler_CreateEmptyDataForHeader_NoPreviousData(t *testing.T) {
246-
p2pData := setupP2P(t)
247-
ctx := context.Background()
248-
249-
// Prepare a header at height 2 (previous height exists but will return error)
250-
signedHeader := p2pMakeSignedHeader(t, p2pData.Genesis.ChainID, 2, p2pData.ProposerAddr, p2pData.ProposerPub, p2pData.Signer)
251-
signedHeader.DataHash = common.DataHashForEmptyTxs
252-
253-
// Mock previous data fetch failure
254-
p2pData.DataStore.EXPECT().GetByHeight(mock.Anything, uint64(1)).Return(nil, errors.New("not available")).Once()
255-
256-
emptyData := p2pData.Handler.createEmptyDataForHeader(ctx, signedHeader)
257-
require.NotNil(t, emptyData, "handler should synthesize empty data even when previous data is unavailable")
258-
require.Equal(t, p2pData.Genesis.ChainID, emptyData.ChainID(), "synthesized data should carry header chain ID")
259-
require.Equal(t, uint64(2), emptyData.Height(), "synthesized data should carry header height")
260-
require.Equal(t, signedHeader.BaseHeader.Time, emptyData.Metadata.Time, "synthesized data should carry header time")
261-
// When no previous data is available, LastDataHash should be zero value
262-
require.Equal(t, (types.Hash)(nil), emptyData.LastDataHash, "last data hash should be empty when previous data is not available")
263-
}
264-
265225
func TestP2PHandler_ProcessHeaderRange_MultipleHeightsHappyPath(t *testing.T) {
266226
p2pData := setupP2P(t)
267227
ctx := context.Background()

block/internal/syncing/syncer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func (s *Syncer) syncLoop() {
273273
select {
274274
case <-s.ctx.Done():
275275
return
276-
case <-time.After(min(100*time.Millisecond, s.config.Node.BlockTime.Duration)):
276+
case <-time.After(min(25*time.Millisecond, s.config.Node.BlockTime.Duration)):
277277
}
278278
}
279279
}

block/internal/syncing/syncer_backoff_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func TestSyncer_BackoffOnDAError(t *testing.T) {
154154
assert.GreaterOrEqual(t, callCount, 2, "should continue without significant delay")
155155
if len(callTimes) >= 2 {
156156
timeBetweenCalls := callTimes[1].Sub(callTimes[0])
157-
assert.Less(t, timeBetweenCalls, 100*time.Millisecond,
157+
assert.Less(t, timeBetweenCalls, 120*time.Millisecond,
158158
"should not have backoff delay for ErrBlobNotFound")
159159
}
160160
}

0 commit comments

Comments
 (0)