mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-05-13 21:30:06 +02:00
discovery: pass context through to bootstrapper SampleNodeAddrs
Since the ChannelGraphBootstrapper implementation makes a call to the graph DB.
This commit is contained in:
parent
1bc66db228
commit
3f9c554c4d
@ -2,6 +2,7 @@ package discovery
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"errors"
|
"errors"
|
||||||
@ -36,8 +37,9 @@ type NetworkPeerBootstrapper interface {
|
|||||||
// denotes how many valid peer addresses to return. The passed set of
|
// denotes how many valid peer addresses to return. The passed set of
|
||||||
// node nodes allows the caller to ignore a set of nodes perhaps
|
// node nodes allows the caller to ignore a set of nodes perhaps
|
||||||
// because they already have connections established.
|
// because they already have connections established.
|
||||||
SampleNodeAddrs(numAddrs uint32,
|
SampleNodeAddrs(ctx context.Context, numAddrs uint32,
|
||||||
ignore map[autopilot.NodeID]struct{}) ([]*lnwire.NetAddress, error)
|
ignore map[autopilot.NodeID]struct{}) ([]*lnwire.NetAddress,
|
||||||
|
error)
|
||||||
|
|
||||||
// Name returns a human readable string which names the concrete
|
// Name returns a human readable string which names the concrete
|
||||||
// implementation of the NetworkPeerBootstrapper.
|
// implementation of the NetworkPeerBootstrapper.
|
||||||
@ -50,7 +52,8 @@ type NetworkPeerBootstrapper interface {
|
|||||||
// bootstrapper will be queried successively until the target amount is met. If
|
// bootstrapper will be queried successively until the target amount is met. If
|
||||||
// the ignore map is populated, then the bootstrappers will be instructed to
|
// the ignore map is populated, then the bootstrappers will be instructed to
|
||||||
// skip those nodes.
|
// 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) {
|
bootstrappers ...NetworkPeerBootstrapper) ([]*lnwire.NetAddress, error) {
|
||||||
|
|
||||||
// We'll randomly shuffle our bootstrappers before querying them in
|
// 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.
|
// the number of address remaining that we need to fetch.
|
||||||
numAddrsLeft := numAddrs - uint32(len(addrs))
|
numAddrsLeft := numAddrs - uint32(len(addrs))
|
||||||
log.Tracef("Querying for %v addresses", numAddrsLeft)
|
log.Tracef("Querying for %v addresses", numAddrsLeft)
|
||||||
netAddrs, err := bootstrapper.SampleNodeAddrs(numAddrsLeft, ignore)
|
netAddrs, err := bootstrapper.SampleNodeAddrs(
|
||||||
|
ctx, numAddrsLeft, ignore,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If we encounter an error with a bootstrapper, then
|
// If we encounter an error with a bootstrapper, then
|
||||||
// we'll continue on to the next available
|
// 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.
|
// many valid peer addresses to return.
|
||||||
//
|
//
|
||||||
// NOTE: Part of the NetworkPeerBootstrapper interface.
|
// 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) {
|
ignore map[autopilot.NodeID]struct{}) ([]*lnwire.NetAddress, error) {
|
||||||
|
|
||||||
// We'll merge the ignore map with our currently selected map in order
|
// 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
|
// 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
|
// many valid peer addresses to return. The set of DNS seeds are used
|
||||||
// successively to retrieve eligible target nodes.
|
// 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) {
|
ignore map[autopilot.NodeID]struct{}) ([]*lnwire.NetAddress, error) {
|
||||||
|
|
||||||
var netAddrs []*lnwire.NetAddress
|
var netAddrs []*lnwire.NetAddress
|
||||||
|
16
server.go
16
server.go
@ -2628,7 +2628,9 @@ func (s *server) Start(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
s.wg.Add(1)
|
s.wg.Add(1)
|
||||||
go s.peerBootstrapper(defaultMinPeers, bootstrappers)
|
go s.peerBootstrapper(
|
||||||
|
ctx, defaultMinPeers, bootstrappers,
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
srvrLog.Infof("Auto peer bootstrapping is disabled")
|
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
|
// 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
|
// and that nodes newly joining the network receive an up to date network view
|
||||||
// as soon as possible.
|
// as soon as possible.
|
||||||
func (s *server) peerBootstrapper(numTargetPeers uint32,
|
func (s *server) peerBootstrapper(ctx context.Context, numTargetPeers uint32,
|
||||||
bootstrappers []discovery.NetworkPeerBootstrapper) {
|
bootstrappers []discovery.NetworkPeerBootstrapper) {
|
||||||
|
|
||||||
defer s.wg.Done()
|
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
|
// We'll start off by aggressively attempting connections to peers in
|
||||||
// order to be a part of the network as soon as possible.
|
// 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
|
// Once done, we'll attempt to maintain our target minimum number of
|
||||||
// peers.
|
// peers.
|
||||||
@ -3163,7 +3165,7 @@ func (s *server) peerBootstrapper(numTargetPeers uint32,
|
|||||||
ignoreList = s.createBootstrapIgnorePeers()
|
ignoreList = s.createBootstrapIgnorePeers()
|
||||||
|
|
||||||
peerAddrs, err := discovery.MultiSourceBootstrap(
|
peerAddrs, err := discovery.MultiSourceBootstrap(
|
||||||
ignoreList, numNeeded*2, bootstrappers...,
|
ctx, ignoreList, numNeeded*2, bootstrappers...,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
srvrLog.Errorf("Unable to retrieve bootstrap "+
|
srvrLog.Errorf("Unable to retrieve bootstrap "+
|
||||||
@ -3212,8 +3214,8 @@ const bootstrapBackOffCeiling = time.Minute * 5
|
|||||||
// initialPeerBootstrap attempts to continuously connect to peers on startup
|
// initialPeerBootstrap attempts to continuously connect to peers on startup
|
||||||
// until the target number of peers has been reached. This ensures that nodes
|
// until the target number of peers has been reached. This ensures that nodes
|
||||||
// receive an up to date network view as soon as possible.
|
// receive an up to date network view as soon as possible.
|
||||||
func (s *server) initialPeerBootstrap(ignore map[autopilot.NodeID]struct{},
|
func (s *server) initialPeerBootstrap(ctx context.Context,
|
||||||
numTargetPeers uint32,
|
ignore map[autopilot.NodeID]struct{}, numTargetPeers uint32,
|
||||||
bootstrappers []discovery.NetworkPeerBootstrapper) {
|
bootstrappers []discovery.NetworkPeerBootstrapper) {
|
||||||
|
|
||||||
srvrLog.Debugf("Init bootstrap with targetPeers=%v, bootstrappers=%v, "+
|
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.
|
// in order to reach our target.
|
||||||
peersNeeded := numTargetPeers - numActivePeers
|
peersNeeded := numTargetPeers - numActivePeers
|
||||||
bootstrapAddrs, err := discovery.MultiSourceBootstrap(
|
bootstrapAddrs, err := discovery.MultiSourceBootstrap(
|
||||||
ignore, peersNeeded, bootstrappers...,
|
ctx, ignore, peersNeeded, bootstrappers...,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
srvrLog.Errorf("Unable to retrieve initial bootstrap "+
|
srvrLog.Errorf("Unable to retrieve initial bootstrap "+
|
||||||
|
Loading…
x
Reference in New Issue
Block a user