Merge pull request #1416 from cfromknecht/prioritize-higher-sync-peer

netsync/manager: prioritize higher height peers for sync peer
This commit is contained in:
Olaoluwa Osuntokun 2019-04-24 16:21:41 -07:00 committed by GitHub
commit 8bb9c0b392
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,6 +6,7 @@ package netsync
import ( import (
"container/list" "container/list"
"math/rand"
"net" "net"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -236,7 +237,7 @@ func (sm *SyncManager) startSync() {
} }
best := sm.chain.BestSnapshot() best := sm.chain.BestSnapshot()
var bestPeer *peerpkg.Peer var higherPeers, equalPeers []*peerpkg.Peer
for peer, state := range sm.peerStates { for peer, state := range sm.peerStates {
if !state.syncCandidate { if !state.syncCandidate {
continue continue
@ -258,9 +259,33 @@ func (sm *SyncManager) startSync() {
continue continue
} }
// TODO(davec): Use a better algorithm to choose the best peer. // If the peer is at the same height as us, we'll add it a set
// For now, just pick the first available candidate. // of backup peers in case we do not find one with a higher
bestPeer = peer // height. If we are synced up with all of our peers, all of
// them will be in this set.
if peer.LastBlock() == best.Height {
equalPeers = append(equalPeers, peer)
continue
}
// This peer has a height greater than our own, we'll consider
// it in the set of better peers from which we'll randomly
// select.
higherPeers = append(higherPeers, peer)
}
// Pick randomly from the set of peers greater than our block height,
// falling back to a random peer of the same height if none are greater.
//
// TODO(conner): Use a better algorithm to ranking peers based on
// observed metrics and/or sync in parallel.
var bestPeer *peerpkg.Peer
switch {
case len(higherPeers) > 0:
bestPeer = higherPeers[rand.Intn(len(higherPeers))]
case len(equalPeers) > 0:
bestPeer = equalPeers[rand.Intn(len(equalPeers))]
} }
// Start syncing from the best peer if one was selected. // Start syncing from the best peer if one was selected.