From 39072e1b7424dba2a837dd393eec36407de5e1a0 Mon Sep 17 00:00:00 2001 From: terencechain Date: Mon, 9 Oct 2023 10:35:15 -0700 Subject: [PATCH] Check block exists in pending queue before requesting from peer (#13013) --- .../sync/pending_attestations_queue.go | 21 ++++++++++++------- beacon-chain/sync/pending_blocks_queue.go | 6 +++++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/beacon-chain/sync/pending_attestations_queue.go b/beacon-chain/sync/pending_attestations_queue.go index 828a2c3ae011..9151f699e8be 100644 --- a/beacon-chain/sync/pending_attestations_queue.go +++ b/beacon-chain/sync/pending_attestations_queue.go @@ -75,14 +75,19 @@ func (s *Service) processPendingAtts(ctx context.Context) error { delete(s.blkRootToPendingAtts, bRoot) s.pendingAttsLock.Unlock() } else { - // Pending attestation's missing block has not arrived yet. - log.WithFields(logrus.Fields{ - "currentSlot": s.cfg.clock.CurrentSlot(), - "attSlot": attestations[0].Message.Aggregate.Data.Slot, - "attCount": len(attestations), - "blockRoot": hex.EncodeToString(bytesutil.Trunc(bRoot[:])), - }).Debug("Requesting block for pending attestation") - pendingRoots = append(pendingRoots, bRoot) + s.pendingQueueLock.RLock() + seen := s.seenPendingBlocks[bRoot] + s.pendingQueueLock.RUnlock() + if !seen { + // Pending attestation's missing block has not arrived yet. + log.WithFields(logrus.Fields{ + "currentSlot": s.cfg.clock.CurrentSlot(), + "attSlot": attestations[0].Message.Aggregate.Data.Slot, + "attCount": len(attestations), + "blockRoot": hex.EncodeToString(bytesutil.Trunc(bRoot[:])), + }).Debug("Requesting block for pending attestation") + pendingRoots = append(pendingRoots, bRoot) + } } } return s.sendBatchRootRequest(ctx, pendingRoots, randGen) diff --git a/beacon-chain/sync/pending_blocks_queue.go b/beacon-chain/sync/pending_blocks_queue.go index 259cb5044d22..2daf49b752e5 100644 --- a/beacon-chain/sync/pending_blocks_queue.go +++ b/beacon-chain/sync/pending_blocks_queue.go @@ -247,11 +247,15 @@ func (s *Service) sendBatchRootRequest(ctx context.Context, roots [][32]byte, ra defer span.End() roots = dedupRoots(roots) + s.pendingQueueLock.RLock() for i := len(roots) - 1; i >= 0; i-- { - if s.cfg.chain.BlockBeingSynced(roots[i]) { + r := roots[i] + if s.seenPendingBlocks[r] || s.cfg.chain.BlockBeingSynced(r) { roots = append(roots[:i], roots[i+1:]...) } } + s.pendingQueueLock.RUnlock() + if len(roots) == 0 { return nil }