What changed, and why it matters
This commit adds a new internal helper function called fetchHigherPeers to the Bitcoin network synchronization code, along with unit tests. It simply returns a list of peer connections that advertise a higher blockchain height than a given value and are eligible for syncing. There is no security issue visible in this change.
No security action required. This is a routine code addition with tests.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces fetchHigherPeers in netsync/manager.go, which iterates over sm.peerStates and returns peers where state.syncCandidate is true and peer.LastBlock() > height. It also adds TestFetchHigherPeers in netsync/manager_test.go covering sync-candidate filtering and height comparison. No callers of fetchHigherPeers are added in this commit, and no existing behavior is modified.
Changed components
netsync/manager.gonetsync/manager_test.goInspect captured patch +78 / −0
diff --git a/netsync/manager.go b/netsync/manager.go
index 480eead..626cf49 100644
--- a/netsync/manager.go
+++ b/netsync/manager.go
@@ -252,6 +252,26 @@ func (sm *SyncManager) findNextHeaderCheckpoint(height int32) *chaincfg.Checkpoi
return nextCheckpoint
}
+// fetchHigherPeers returns all the peers that are at a higher block than the
+// given height. The peers that are not sync candidates are omitted from the
+// returned list.
+func (sm *SyncManager) fetchHigherPeers(height int32) []*peerpkg.Peer {
+ higherPeers := make([]*peerpkg.Peer, 0, len(sm.peerStates))
+ for peer, state := range sm.peerStates {
+ if !state.syncCandidate {
+ continue
+ }
+
+ if peer.LastBlock() <= height {
+ continue
+ }
+
+ higherPeers = append(higherPeers, peer)
+ }
+
+ return higherPeers
+}
+
// 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
diff --git a/netsync/manager_test.go b/netsync/manager_test.go
index ee84cea..bc84bc7 100644
--- a/netsync/manager_test.go
+++ b/netsync/manager_test.go
@@ -226,3 +226,61 @@ func TestCheckHeadersList(t *testing.T) {
require.Equal(t, test.behaviorFlags, gotFlags)
}
}
+
+func TestFetchHigherPeers(t *testing.T) {
+ // Create mock SyncManager.
+ sm, tearDown := makeMockSyncManager(t, &chaincfg.MainNetParams)
+ defer tearDown()
+
+ tests := []struct {
+ peerHeights []int32
+ peerSyncCandidate []bool
+ height int32
+ expectedCnt int
+ }{
+ {
+ peerHeights: []int32{9, 10, 10, 10},
+ peerSyncCandidate: []bool{true, true, true, true},
+ height: 5,
+ expectedCnt: 4,
+ },
+
+ {
+ peerHeights: []int32{9, 10, 10, 10},
+ peerSyncCandidate: []bool{false, false, true, true},
+ height: 5,
+ expectedCnt: 2,
+ },
+
+ {
+ peerHeights: []int32{1, 100, 100, 100, 100},
+ peerSyncCandidate: []bool{true, false, true, true, false},
+ height: 100,
+ expectedCnt: 0,
+ },
+ }
+
+ for _, test := range tests {
+ // Setup peers.
+ sm.peerStates = make(map[*peer.Peer]*peerSyncState)
+ for i, height := range test.peerHeights {
+ peer := peer.NewInboundPeer(&peer.Config{})
+ peer.UpdateLastBlockHeight(height)
+ sm.peerStates[peer] = &peerSyncState{
+ syncCandidate: test.peerSyncCandidate[i],
+ requestedTxns: make(map[chainhash.Hash]struct{}),
+ requestedBlocks: make(map[chainhash.Hash]struct{}),
+ }
+ }
+
+ // Fetch higher peers and assert.
+ peers := sm.fetchHigherPeers(test.height)
+ require.Equal(t, test.expectedCnt, len(peers))
+
+ for _, peer := range peers {
+ state, found := sm.peerStates[peer]
+ require.True(t, found)
+ require.True(t, state.syncCandidate)
+ }
+ }
+}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.