Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions netsync/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,39 @@ func (sm *SyncManager) isInIBDMode() bool {
return true
}

// fetchHeaders randomly picks a peer that has a higher advertised header
// and pushes a get headers message to it.
func (sm *SyncManager) fetchHeaders() {
_, height := sm.chain.BestHeader()
higherPeers := sm.fetchHigherPeers(height)
var bestPeer *peerpkg.Peer
switch {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we expect other cases in the future else we can just use if?

case len(higherPeers) > 0:
bestPeer = higherPeers[rand.Intn(len(higherPeers))]
}

if bestPeer == nil {
log.Warnf("No sync peer candidates available")
return
}

locator, err := sm.chain.LatestBlockLocatorByHeader()
if err != nil {
log.Errorf("Failed to get block locator for the "+
"latest block header: %v", err)
return
}

log.Infof("Downloading headers for blocks %d to "+
"%d from peer %s", height+1,
bestPeer.LastBlock(), bestPeer.Addr())

bestPeer.PushGetHeadersMsg(locator, &zeroHash)

sm.headersFirstMode = true
sm.syncPeer = bestPeer
}

// startSync will choose the best peer among the available candidate peers to
// download/sync the blockchain from. When syncing is already running, it
// simply returns. It also examines the candidates for any which are no longer
Expand Down