diff --git a/beacon-chain/sync/pending_blocks_queue.go b/beacon-chain/sync/pending_blocks_queue.go index 5bebd8c3ce..10c6f6aa3d 100644 --- a/beacon-chain/sync/pending_blocks_queue.go +++ b/beacon-chain/sync/pending_blocks_queue.go @@ -8,6 +8,7 @@ import ( "sync" "time" + "github.com/libp2p/go-libp2p/core" "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/v4/async" "github.com/prysmaticlabs/prysm/v4/beacon-chain/blockchain" @@ -184,6 +185,7 @@ func (s *Service) hasPeer() bool { } // processAndBroadcastBlock validates, processes, and broadcasts a block. +// part of the function is to request missing blobs from peers if the block contains kzg commitments. func (s *Service) processAndBroadcastBlock(ctx context.Context, b interfaces.ReadOnlySignedBeaconBlock, blkRoot [32]byte) error { if err := s.validateBeaconBlock(ctx, b, blkRoot); err != nil { if !errors.Is(ErrOptimisticParent, err) { @@ -192,6 +194,14 @@ func (s *Service) processAndBroadcastBlock(ctx context.Context, b interfaces.Rea } } + peers := s.getBestPeers() + peerCount := len(peers) + if peerCount > 0 { + if err := s.requestPendingBlobs(ctx, b.Block(), blkRoot, peers[rand.NewGenerator().Int()%peerCount]); err != nil { + return err + } + } + if err := s.cfg.chain.ReceiveBlock(ctx, b, blkRoot); err != nil { return err } @@ -219,6 +229,12 @@ func (s *Service) handleBlockProcessingError(ctx context.Context, err error, b i log.WithError(err).WithField("slot", b.Block().Slot()).Debug("Could not process block") } +// getBestPeers returns the list of best peers based on finalized checkpoint epoch. +func (s *Service) getBestPeers() []core.PeerID { + _, bestPeers := s.cfg.p2p.Peers().BestFinalized(maxPeerRequest, s.cfg.chain.FinalizedCheckpt().Epoch) + return bestPeers +} + func (s *Service) checkIfBlockIsBad( ctx context.Context, span *trace.Span, @@ -265,8 +281,7 @@ func (s *Service) sendBatchRootRequest(ctx context.Context, roots [][32]byte, ra if len(roots) == 0 { return nil } - cp := s.cfg.chain.FinalizedCheckpt() - _, bestPeers := s.cfg.p2p.Peers().BestFinalized(maxPeerRequest, cp.Epoch) + bestPeers := s.getBestPeers() if len(bestPeers) == 0 { return nil }