diff --git a/beacon-chain/sync/initial-sync/round_robin.go b/beacon-chain/sync/initial-sync/round_robin.go index d700459744..928d51ebe5 100644 --- a/beacon-chain/sync/initial-sync/round_robin.go +++ b/beacon-chain/sync/initial-sync/round_robin.go @@ -9,6 +9,7 @@ import ( "sync/atomic" "time" + "github.com/prysmaticlabs/prysm/beacon-chain/flags" "github.com/libp2p/go-libp2p-core/peer" "github.com/paulbellamy/ratecounter" "github.com/pkg/errors" @@ -44,8 +45,9 @@ func (s *Service) roundRobinSync(genesis time.Time) error { counter := ratecounter.NewRateCounter(counterSeconds * time.Second) randGenerator := rand.New(rand.NewSource(time.Now().Unix())) var lastEmptyRequests int + highestFinalizedSlot := helpers.StartSlot(s.highestFinalizedEpoch() + 1) // Step 1 - Sync to end of finalized epoch. - for s.chain.HeadSlot() < helpers.StartSlot(s.highestFinalizedEpoch()+1) { + for s.chain.HeadSlot() < highestFinalizedSlot { root, finalizedEpoch, peers := s.p2p.Peers().BestFinalized(params.BeaconConfig().MaxPeersToSync, helpers.SlotToEpoch(s.chain.HeadSlot())) if len(peers) == 0 { log.Warn("No peers; waiting for reconnect") @@ -53,6 +55,10 @@ func (s *Service) roundRobinSync(genesis time.Time) error { continue } + if len(peers) >= flags.Get().MinimumSyncPeers { + highestFinalizedSlot = helpers.StartSlot(finalizedEpoch + 1) + } + // shuffle peers to prevent a bad peer from // stalling sync with invalid blocks randGenerator.Shuffle(len(peers), func(i, j int) { @@ -82,8 +88,8 @@ func (s *Service) roundRobinSync(genesis time.Time) error { } // Short circuit start far exceeding the highest finalized epoch in some infinite loop. - if start > helpers.StartSlot(s.highestFinalizedEpoch()+1) { - return nil, errors.Errorf("attempted to ask for a start slot of %d which is greater than the next highest epoch of %d", start, s.highestFinalizedEpoch()+1) + if start > highestFinalizedSlot { + return nil, errors.Errorf("attempted to ask for a start slot of %d which is greater than the next highest slot of %d", start, highestFinalizedSlot) } atomic.AddInt32(&p2pRequestCount, int32(len(peers))) @@ -177,7 +183,8 @@ func (s *Service) roundRobinSync(genesis time.Time) error { 0, // remainder ) if err != nil { - return err + log.WithError(err).Error("Round robing sync request failed") + continue } // Since the block responses were appended to the list, we must sort them in order to