Skip to content

Commit

Permalink
Don't Panic if 0 Peers are Left (prysmaticlabs#4594)
Browse files Browse the repository at this point in the history
* log error
* Merge branch 'master' into dontReturnError
* return blocks
* change back
* Merge branch 'dontReturnError' of https://github.com/prysmaticlabs/geth-sharding into dontReturnError
* use a more static finalized epoch
* Update beacon-chain/sync/initial-sync/round_robin.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>
* Merge refs/heads/master into dontReturnError
* jim's review
* Merge branch 'dontReturnError' of https://github.com/prysmaticlabs/geth-sharding into dontReturnError
* Update beacon-chain/sync/initial-sync/round_robin.go
  • Loading branch information
nisdas authored and cryptomental committed Feb 24, 2020
1 parent 774ad2b commit 86f9b13
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions beacon-chain/sync/initial-sync/round_robin.go
Expand Up @@ -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"
Expand Down Expand Up @@ -44,15 +45,20 @@ 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")
time.Sleep(refreshTime)
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) {
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 86f9b13

Please sign in to comment.