From c1c0cd040c37f247a2d294ba59e3810ad5043cf5 Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Thu, 5 Oct 2023 23:18:53 +0800 Subject: [PATCH] Fix Initial Sync PreProcessing (#13007) Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> --- beacon-chain/sync/initial-sync/round_robin.go | 6 ++-- .../sync/initial-sync/round_robin_test.go | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/beacon-chain/sync/initial-sync/round_robin.go b/beacon-chain/sync/initial-sync/round_robin.go index 8a48e6c0adcd..b77c105f2c6a 100644 --- a/beacon-chain/sync/initial-sync/round_robin.go +++ b/beacon-chain/sync/initial-sync/round_robin.go @@ -276,7 +276,8 @@ func validUnprocessed(ctx context.Context, bwb []blocks.BlockWithVerifiedBlobs, for i := range bwb { b := bwb[i].Block if headSlot >= b.Block().Slot() && isProc(ctx, b) { - processed = &i + val := i + processed = &val continue } if i > 0 { @@ -295,7 +296,8 @@ func validUnprocessed(ctx context.Context, bwb []blocks.BlockWithVerifiedBlobs, maxRoot := maxIncoming.Root() return nil, fmt.Errorf("headSlot:%d, blockSlot:%d , root %#x:%w", headSlot, maxIncoming.Block().Slot(), maxRoot, errBlockAlreadyProcessed) } - return bwb[*processed:], nil + nonProcessedIdx := *processed + 1 + return bwb[nonProcessedIdx:], nil } func (s *Service) processBatchedBlocks(ctx context.Context, genesis time.Time, diff --git a/beacon-chain/sync/initial-sync/round_robin_test.go b/beacon-chain/sync/initial-sync/round_robin_test.go index f5c7a04548a7..dab0f3a42a4a 100644 --- a/beacon-chain/sync/initial-sync/round_robin_test.go +++ b/beacon-chain/sync/initial-sync/round_robin_test.go @@ -667,3 +667,38 @@ func TestService_syncToFinalizedEpoch(t *testing.T) { assert.NoError(t, s.syncToFinalizedEpoch(context.Background(), genesis)) assert.LogsContain(t, hook, "Already synced to finalized epoch") } + +func TestService_ValidUnprocessed(t *testing.T) { + beaconDB := dbtest.SetupDB(t) + genesisBlk := util.NewBeaconBlock() + genesisBlkRoot, err := genesisBlk.Block.HashTreeRoot() + require.NoError(t, err) + util.SaveBlock(t, context.Background(), beaconDB, genesisBlk) + + var batch []blocks.BlockWithVerifiedBlobs + currBlockRoot := genesisBlkRoot + for i := primitives.Slot(1); i < 10; i++ { + parentRoot := currBlockRoot + blk1 := util.NewBeaconBlock() + blk1.Block.Slot = i + blk1.Block.ParentRoot = parentRoot[:] + blk1Root, err := blk1.Block.HashTreeRoot() + require.NoError(t, err) + util.SaveBlock(t, context.Background(), beaconDB, blk1) + wsb, err := blocks.NewSignedBeaconBlock(blk1) + require.NoError(t, err) + rowsb, err := blocks.NewROBlock(wsb) + require.NoError(t, err) + batch = append(batch, blocks.BlockWithVerifiedBlobs{Block: rowsb}) + currBlockRoot = blk1Root + } + + retBlocks, err := validUnprocessed(context.Background(), batch, 2, func(ctx context.Context, block blocks.ROBlock) bool { + // Ignore first 2 blocks in the batch. + return block.Block().Slot() <= 2 + }) + require.NoError(t, err) + + // Ensure that the unprocessed batch is returned correctly. + assert.Equal(t, len(retBlocks), len(batch)-2) +}