mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 15:37:56 -05:00
Improves block deduplication on batch processing (#7075)
* fixes block deduplication on batch processing * Nishant's suggestions
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user