Skip to content

Commit 37d6390

Browse files
committed
discovery: use a no-op hash accumluator for local networks
If LND is running on a local network, then use deterministic sampling so that we can have deterministic peer bootstrapping.
1 parent 339dd0c commit 37d6390

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

discovery/bootstrapper.go

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,26 @@ var _ NetworkPeerBootstrapper = (*ChannelGraphBootstrapper)(nil)
138138
// backed by an active autopilot.ChannelGraph instance. This type of network
139139
// peer bootstrapper will use the authenticated nodes within the known channel
140140
// graph to bootstrap connections.
141-
func NewGraphBootstrapper(cg autopilot.ChannelGraph) (NetworkPeerBootstrapper,
142-
error) {
141+
func NewGraphBootstrapper(cg autopilot.ChannelGraph,
142+
deterministicSampling bool) (NetworkPeerBootstrapper, error) {
143143

144-
hashAccumulator, err := newRandomHashAccumulator()
145-
if err != nil {
146-
return nil, fmt.Errorf("unable to create hash accumulator: %w",
147-
err)
144+
var (
145+
hashAccumulator hashAccumulator
146+
err error
147+
)
148+
if deterministicSampling {
149+
// If we're using deterministic sampling, then we'll use a
150+
// no-op hash accumulator that will always return false for
151+
// skipNode.
152+
hashAccumulator = newNoOpHashAccumulator()
153+
} else {
154+
// Otherwise, we'll use a random hash accumulator to sample
155+
// nodes from the channel graph.
156+
hashAccumulator, err = newRandomHashAccumulator()
157+
if err != nil {
158+
return nil, fmt.Errorf("unable to create hash "+
159+
"accumulator: %w", err)
160+
}
148161
}
149162

150163
return &ChannelGraphBootstrapper{
@@ -602,3 +615,29 @@ func (r *randomHashAccumulator) rotate() {
602615
func (r *randomHashAccumulator) skipNode(pub route.Vertex) bool {
603616
return bytes.Compare(r.hash[:], pub[1:]) > 0
604617
}
618+
619+
// noOpHashAccumulator is a no-op implementation of the hashAccumulator
620+
// interface. This is used when we want deterministic behavior and don't
621+
// want to sample nodes randomly from the channel graph.
622+
type noOpHashAccumulator struct{}
623+
624+
// newNoOpHashAccumulator returns a new instance of a noOpHashAccumulator.
625+
func newNoOpHashAccumulator() *noOpHashAccumulator {
626+
return &noOpHashAccumulator{}
627+
}
628+
629+
// rotate is a no-op for the noOpHashAccumulator.
630+
//
631+
// NOTE: this is part of the hashAccumulator interface.
632+
func (*noOpHashAccumulator) rotate() {}
633+
634+
// skipNode always returns false, meaning that no nodes will be skipped.
635+
//
636+
// NOTE: this is part of the hashAccumulator interface.
637+
func (*noOpHashAccumulator) skipNode(route.Vertex) bool {
638+
return false
639+
}
640+
641+
// A compile-time assertion to ensure that noOpHashAccumulator meets the
642+
// hashAccumulator interface.
643+
var _ hashAccumulator = (*noOpHashAccumulator)(nil)

docs/release-notes/release-notes-0.20.0.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ reader of a payment request.
144144
disabling has now been
145145
[removed](https://github.com/lightningnetwork/lnd/pull/9967) meaning that any
146146
test network scripts that rely on bootstrapping being disabled will need to
147-
explicitly define the `--nobootstrap` flag.
147+
explicitly define the `--nobootstrap` flag. Bootstrapping will now also be
148+
[deterministic](https://github.com/lightningnetwork/lnd/pull/10003) on local
149+
test networks so that bootstrapping behaviour can be tested for.
148150

149151
## Database
150152

server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3064,7 +3064,9 @@ func initNetworkBootstrappers(s *server) ([]discovery.NetworkPeerBootstrapper, e
30643064
// this can be used by default if we've already partially seeded the
30653065
// network.
30663066
chanGraph := autopilot.ChannelGraphFromDatabase(s.graphDB)
3067-
graphBootstrapper, err := discovery.NewGraphBootstrapper(chanGraph)
3067+
graphBootstrapper, err := discovery.NewGraphBootstrapper(
3068+
chanGraph, s.cfg.Bitcoin.IsLocalNetwork(),
3069+
)
30683070
if err != nil {
30693071
return nil, err
30703072
}

0 commit comments

Comments
 (0)