Skip to content

Commit

Permalink
chain: refine peer selection logic for pruned nodes.
Browse files Browse the repository at this point in the history
We discard inbound peers because we don't know their listen port
and sort the peers by pingtime to select the fastest first.
  • Loading branch information
ziggie1984 committed Jan 25, 2024
1 parent b104af5 commit f4361d8
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions chain/pruned_block_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"math/rand"
"net"
"sort"
"sync"
"time"

Expand Down Expand Up @@ -395,7 +396,20 @@ func (d *PrunedBlockDispatcher) connectToPeer(addr string) (bool, error) {
// "full-node".
func filterPeers(peers []btcjson.GetPeerInfoResult) ([]string, error) {
var eligible []string // nolint:prealloc

// First we sort the peers by the measured ping time, to choose the best
// peers to fetch blocks from.
sort.Slice(peers, func(i, j int) bool {
return peers[i].PingTime < peers[j].PingTime
})

for _, peer := range peers {
// We cannot use the inbound peers here because the referenced
// port in the `addr` field is not the listen port for the p2p
// connection but a random outgoing port of the peer.
if peer.Inbound {
continue
}
rawServices, err := hex.DecodeString(peer.Services)
if err != nil {
return nil, err
Expand Down

0 comments on commit f4361d8

Please sign in to comment.