discovery: pass context through to bootstrapper SampleNodeAddrs

Since the ChannelGraphBootstrapper implementation makes a call to the
graph DB.
This commit is contained in:
Elle Mouton
2025-04-08 07:05:00 +02:00
parent f4b7cc4f4b
commit 291381ef86
2 changed files with 22 additions and 13 deletions

View File

@@ -2,6 +2,7 @@ package discovery
import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"errors"
@@ -36,8 +37,9 @@ type NetworkPeerBootstrapper interface {
// denotes how many valid peer addresses to return. The passed set of
// node nodes allows the caller to ignore a set of nodes perhaps
// because they already have connections established.
SampleNodeAddrs(numAddrs uint32,
ignore map[autopilot.NodeID]struct{}) ([]*lnwire.NetAddress, error)
SampleNodeAddrs(ctx context.Context, numAddrs uint32,
ignore map[autopilot.NodeID]struct{}) ([]*lnwire.NetAddress,
error)
// Name returns a human readable string which names the concrete
// implementation of the NetworkPeerBootstrapper.
@@ -50,7 +52,8 @@ type NetworkPeerBootstrapper interface {
// bootstrapper will be queried successively until the target amount is met. If
// the ignore map is populated, then the bootstrappers will be instructed to
// skip those nodes.
func MultiSourceBootstrap(ignore map[autopilot.NodeID]struct{}, numAddrs uint32,
func MultiSourceBootstrap(ctx context.Context,
ignore map[autopilot.NodeID]struct{}, numAddrs uint32,
bootstrappers ...NetworkPeerBootstrapper) ([]*lnwire.NetAddress, error) {
// We'll randomly shuffle our bootstrappers before querying them in
@@ -73,7 +76,9 @@ func MultiSourceBootstrap(ignore map[autopilot.NodeID]struct{}, numAddrs uint32,
// the number of address remaining that we need to fetch.
numAddrsLeft := numAddrs - uint32(len(addrs))
log.Tracef("Querying for %v addresses", numAddrsLeft)
netAddrs, err := bootstrapper.SampleNodeAddrs(numAddrsLeft, ignore)
netAddrs, err := bootstrapper.SampleNodeAddrs(
ctx, numAddrsLeft, ignore,
)
if err != nil {
// If we encounter an error with a bootstrapper, then
// we'll continue on to the next available
@@ -152,7 +157,8 @@ func NewGraphBootstrapper(cg autopilot.ChannelGraph) (NetworkPeerBootstrapper, e
// many valid peer addresses to return.
//
// NOTE: Part of the NetworkPeerBootstrapper interface.
func (c *ChannelGraphBootstrapper) SampleNodeAddrs(numAddrs uint32,
func (c *ChannelGraphBootstrapper) SampleNodeAddrs(_ context.Context,
numAddrs uint32,
ignore map[autopilot.NodeID]struct{}) ([]*lnwire.NetAddress, error) {
// We'll merge the ignore map with our currently selected map in order
@@ -382,7 +388,8 @@ func (d *DNSSeedBootstrapper) fallBackSRVLookup(soaShim string,
// network peer bootstrapper source. The num addrs field passed in denotes how
// many valid peer addresses to return. The set of DNS seeds are used
// successively to retrieve eligible target nodes.
func (d *DNSSeedBootstrapper) SampleNodeAddrs(numAddrs uint32,
func (d *DNSSeedBootstrapper) SampleNodeAddrs(_ context.Context,
numAddrs uint32,
ignore map[autopilot.NodeID]struct{}) ([]*lnwire.NetAddress, error) {
var netAddrs []*lnwire.NetAddress

View File

@@ -2628,7 +2628,9 @@ func (s *server) Start(ctx context.Context) error {
}
s.wg.Add(1)
go s.peerBootstrapper(defaultMinPeers, bootstrappers)
go s.peerBootstrapper(
ctx, defaultMinPeers, bootstrappers,
)
} else {
srvrLog.Infof("Auto peer bootstrapping is disabled")
}
@@ -3075,7 +3077,7 @@ func (s *server) createBootstrapIgnorePeers() map[autopilot.NodeID]struct{} {
// invariant, we ensure that our node is connected to a diverse set of peers
// and that nodes newly joining the network receive an up to date network view
// as soon as possible.
func (s *server) peerBootstrapper(numTargetPeers uint32,
func (s *server) peerBootstrapper(ctx context.Context, numTargetPeers uint32,
bootstrappers []discovery.NetworkPeerBootstrapper) {
defer s.wg.Done()
@@ -3085,7 +3087,7 @@ func (s *server) peerBootstrapper(numTargetPeers uint32,
// We'll start off by aggressively attempting connections to peers in
// order to be a part of the network as soon as possible.
s.initialPeerBootstrap(ignoreList, numTargetPeers, bootstrappers)
s.initialPeerBootstrap(ctx, ignoreList, numTargetPeers, bootstrappers)
// Once done, we'll attempt to maintain our target minimum number of
// peers.
@@ -3163,7 +3165,7 @@ func (s *server) peerBootstrapper(numTargetPeers uint32,
ignoreList = s.createBootstrapIgnorePeers()
peerAddrs, err := discovery.MultiSourceBootstrap(
ignoreList, numNeeded*2, bootstrappers...,
ctx, ignoreList, numNeeded*2, bootstrappers...,
)
if err != nil {
srvrLog.Errorf("Unable to retrieve bootstrap "+
@@ -3212,8 +3214,8 @@ const bootstrapBackOffCeiling = time.Minute * 5
// initialPeerBootstrap attempts to continuously connect to peers on startup
// until the target number of peers has been reached. This ensures that nodes
// receive an up to date network view as soon as possible.
func (s *server) initialPeerBootstrap(ignore map[autopilot.NodeID]struct{},
numTargetPeers uint32,
func (s *server) initialPeerBootstrap(ctx context.Context,
ignore map[autopilot.NodeID]struct{}, numTargetPeers uint32,
bootstrappers []discovery.NetworkPeerBootstrapper) {
srvrLog.Debugf("Init bootstrap with targetPeers=%v, bootstrappers=%v, "+
@@ -3272,7 +3274,7 @@ func (s *server) initialPeerBootstrap(ignore map[autopilot.NodeID]struct{},
// in order to reach our target.
peersNeeded := numTargetPeers - numActivePeers
bootstrapAddrs, err := discovery.MultiSourceBootstrap(
ignore, peersNeeded, bootstrappers...,
ctx, ignore, peersNeeded, bootstrappers...,
)
if err != nil {
srvrLog.Errorf("Unable to retrieve initial bootstrap "+