Improves block deduplication on batch processing (#7075)

* fixes block deduplication on batch processing
* Nishant's suggestions
This commit is contained in:
Victor Farazdagi
2020-08-21 13:51:27 +03:00
committed by GitHub
parent ec1dd85c44
commit 05d6dc7a10
2 changed files with 24 additions and 12 deletions

View File

@@ -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)
}
@@ -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)
@@ -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
}

View File

@@ -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),
},
},
@@ -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),