From f979e9669ec17e1e83feb682736099e75edcb5ae Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Fri, 17 Jan 2020 14:43:32 -0800 Subject: [PATCH] Better parent block request (#4572) * Use a good peer instead of a random one, if we know about it * Exit init sync if there is an issue * Merge refs/heads/master into better-parent-block-processing * Merge refs/heads/master into better-parent-block-processing * Merge refs/heads/master into better-parent-block-processing * Merge refs/heads/master into better-parent-block-processing * Update pending_blocks_queue.go --- beacon-chain/sync/initial-sync/round_robin.go | 3 ++- beacon-chain/sync/pending_blocks_queue.go | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/beacon-chain/sync/initial-sync/round_robin.go b/beacon-chain/sync/initial-sync/round_robin.go index 9588162236ca..d700459744af 100644 --- a/beacon-chain/sync/initial-sync/round_robin.go +++ b/beacon-chain/sync/initial-sync/round_robin.go @@ -253,7 +253,8 @@ func (s *Service) roundRobinSync(genesis time.Time) error { for _, blk := range resp { s.logSyncStatus(genesis, blk.Block, []peer.ID{best}, counter) if err := s.chain.ReceiveBlockNoPubsubForkchoice(ctx, blk); err != nil { - return err + log.WithError(err).Error("Failed to process block, exiting init sync") + return nil } } if len(resp) == 0 { diff --git a/beacon-chain/sync/pending_blocks_queue.go b/beacon-chain/sync/pending_blocks_queue.go index 6c1774db6f37..a6c45abf14ac 100644 --- a/beacon-chain/sync/pending_blocks_queue.go +++ b/beacon-chain/sync/pending_blocks_queue.go @@ -69,7 +69,18 @@ func (r *Service) processPendingBlocks(ctx context.Context) error { "parentRoot": hex.EncodeToString(b.Block.ParentRoot), }).Info("Requesting parent block") req := [][32]byte{bytesutil.ToBytes32(b.Block.ParentRoot)} - if err := r.sendRecentBeaconBlocksRequest(ctx, req, pids[rand.Int()%len(pids)]); err != nil { + + // Start with a random peer to query, but choose the first peer in our unsorted list that claims to + // have a head slot newer than the block slot we are requesting. + pid := pids[rand.Int()%len(pids)] + for _, p := range pids { + if cs, _ := r.p2p.Peers().ChainState(p); cs != nil && cs.HeadSlot >= uint64(s) { + pid = p + break + } + } + + if err := r.sendRecentBeaconBlocksRequest(ctx, req, pid); err != nil { traceutil.AnnotateError(span, err) log.Errorf("Could not send recent block request: %v", err) }