Skip to content

Commit

Permalink
Improves block deduplication on batch processing (#7075)
Browse files Browse the repository at this point in the history
* fixes block deduplication on batch processing
* Nishant's suggestions
  • Loading branch information
farazdagi authored Aug 21, 2020
1 parent ec1dd85 commit 05d6dc7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
24 changes: 18 additions & 6 deletions beacon-chain/sync/initial-sync/round_robin.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (s *Service) processBlock(
if err != nil {
return err
}
if blk.Block.Slot <= s.lastProcessedSlot && (s.db.HasBlock(ctx, blkRoot) || s.chain.HasInitSyncBlock(blkRoot)) {
if s.isProcessedBlock(ctx, blk, blkRoot) {
return errors.Wrapf(errBlockAlreadyProcessed, "slot: %d , root %#x", blk.Block.Slot, blkRoot)
}

Expand All @@ -233,16 +233,20 @@ func (s *Service) processBatchedBlocks(ctx context.Context, genesis time.Time,
return errors.New("0 blocks provided into method")
}
firstBlock := blks[0]
for s.lastProcessedSlot >= firstBlock.Block.Slot {
blkRoot, err := stateutil.BlockRoot(firstBlock.Block)
if err != nil {
return err
}
for s.lastProcessedSlot >= firstBlock.Block.Slot && s.isProcessedBlock(ctx, firstBlock, blkRoot) {
if len(blks) == 1 {
return errors.New("no good blocks in batch")
}
blks = blks[1:]
firstBlock = blks[0]
}
blkRoot, err := stateutil.BlockRoot(firstBlock.Block)
if err != nil {
return err
blkRoot, err = stateutil.BlockRoot(firstBlock.Block)
if err != nil {
return err
}
}
s.logBatchSyncStatus(genesis, blks, blkRoot)
parentRoot := bytesutil.ToBytes32(firstBlock.Block.ParentRoot)
Expand Down Expand Up @@ -285,3 +289,11 @@ func (s *Service) updatePeerScorerStats(pid peer.ID, startSlot uint64) {
scorer.IncrementProcessedBlocks(pid, diff)
}
}

// isProcessedBlock checks DB and local cache for presence of a given block, to avoid duplicates.
func (s *Service) isProcessedBlock(ctx context.Context, blk *eth.SignedBeaconBlock, blkRoot [32]byte) bool {
if blk.Block.Slot <= s.lastProcessedSlot && (s.db.HasBlock(ctx, blkRoot) || s.chain.HasInitSyncBlock(blkRoot)) {
return true
}
return false
}
12 changes: 6 additions & 6 deletions beacon-chain/sync/initial-sync/round_robin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,24 @@ func TestService_roundRobinSync(t *testing.T) {
{
blocks: makeSequence(1, 384),
finalizedEpoch: 9,
headSlot: 320,
headSlot: 384,
},
{
blocks: makeSequence(1, 320),
finalizedEpoch: 9,
headSlot: 320,
headSlot: 384,
failureSlots: makeSequence(1, 320),
},
{
blocks: makeSequence(1, 320),
finalizedEpoch: 9,
headSlot: 320,
headSlot: 384,
failureSlots: makeSequence(1, 320),
},
{
blocks: makeSequence(1, 320),
finalizedEpoch: 9,
headSlot: 320,
headSlot: 384,
failureSlots: makeSequence(1, 320),
},
},
Expand All @@ -202,12 +202,12 @@ func TestService_roundRobinSync(t *testing.T) {
{
blocks: makeSequence(1, 384),
finalizedEpoch: 10,
headSlot: 320,
headSlot: 384,
},
{
blocks: makeSequence(1, 384),
finalizedEpoch: 10,
headSlot: 320,
headSlot: 384,
},
{
blocks: makeSequence(1, 256),
Expand Down

0 comments on commit 05d6dc7

Please sign in to comment.