Skip to content

Commit

Permalink
netsync: change isSyncCandidate behavior to include pruned nodes
Browse files Browse the repository at this point in the history
isSyncCandidate is now changed to return true even if the peer is a
pruned node if and only if our chaintip is within 288 blocks of the
peer.

Rationale:
Pruned nodes that signal NODE_NETWORK_LIMITED MUST serve 288 blocks from
their chaintip.  If our chaintip is within that range, this peer can be
a sync candidate even if they aren't an archival node.
  • Loading branch information
kcalvinalvin committed Sep 25, 2023
1 parent 1012f1e commit 8cecc41
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions netsync/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,30 @@ func (sm *SyncManager) isSyncCandidate(peer *peerpkg.Peer) bool {
log.Errorf("Unable to query for segwit "+
"soft-fork state: %v", err)
}
if segwitActive && !peer.IsWitnessEnabled() {
return false
}

nodeServices := peer.Services()
if nodeServices&wire.SFNodeNetwork != wire.SFNodeNetwork ||
(segwitActive && !peer.IsWitnessEnabled()) {

switch {
case nodeServices&wire.SFNodeNetwork == wire.SFNodeNetwork:
// Node is a sync candidate if it has all the blocks.
case nodeServices&wire.SFNodeNetworkLimited == wire.SFNodeNetworkLimited:
// Even if the peer is pruned, if they have the node network limited
// flag, they are able to serve 2 days worth of blocks from the
// current tip. Therefore, check if our chaintip is within that range.
bestHeight := sm.chain.BestSnapshot().Height
peerLastBlock := peer.LastBlock()

// bestHeight+1 as we need the peer to serve us the next block,
// not the one we already have.
if bestHeight+1 < peerLastBlock-wire.NodeNetworkLimitedBlockThreshold {
return false
}
default:
// If the peer isn't an archival node and it's not signaling
// NODE_NETWORK_LIMITED, we can't sync off of this node.
return false
}
}
Expand All @@ -428,7 +449,7 @@ func (sm *SyncManager) handleNewPeerMsg(peer *peerpkg.Peer) {

log.Infof("New valid peer %s (%s)", peer, peer.UserAgent())

// Initialize the peer state
// Initialize the peer state.
isSyncCandidate := sm.isSyncCandidate(peer)
sm.peerStates[peer] = &peerSyncState{
syncCandidate: isSyncCandidate,
Expand Down

0 comments on commit 8cecc41

Please sign in to comment.