• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 15959600311

29 Jun 2025 09:33PM UTC coverage: 67.577% (-0.03%) from 67.606%
15959600311

Pull #8825

github

web-flow
Merge b3542eca4 into 6290edf14
Pull Request #8825: lnd: use persisted node announcement settings across restarts

44 of 49 new or added lines in 1 file covered. (89.8%)

92 existing lines in 17 files now uncovered.

135081 of 199891 relevant lines covered (67.58%)

21854.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

40.82
/discovery/bootstrapper.go
1
package discovery
2

3
import (
4
        "bytes"
5
        "context"
6
        "crypto/rand"
7
        "crypto/sha256"
8
        "errors"
9
        "fmt"
10
        prand "math/rand"
11
        "net"
12
        "strconv"
13
        "strings"
14
        "time"
15

16
        "github.com/btcsuite/btcd/btcec/v2"
17
        "github.com/btcsuite/btcd/btcutil/bech32"
18
        "github.com/lightningnetwork/lnd/autopilot"
19
        "github.com/lightningnetwork/lnd/lnutils"
20
        "github.com/lightningnetwork/lnd/lnwire"
21
        "github.com/lightningnetwork/lnd/tor"
22
        "github.com/miekg/dns"
23
)
24

25
func init() {
13✔
26
        prand.Seed(time.Now().Unix())
13✔
27
}
13✔
28

29
// NetworkPeerBootstrapper is an interface that represents an initial peer
30
// bootstrap mechanism. This interface is to be used to bootstrap a new peer to
31
// the connection by providing it with the pubkey+address of a set of existing
32
// peers on the network. Several bootstrap mechanisms can be implemented such
33
// as DNS, in channel graph, DHT's, etc.
34
type NetworkPeerBootstrapper interface {
35
        // SampleNodeAddrs uniformly samples a set of specified address from
36
        // the network peer bootstrapper source. The num addrs field passed in
37
        // denotes how many valid peer addresses to return. The passed set of
38
        // node nodes allows the caller to ignore a set of nodes perhaps
39
        // because they already have connections established.
40
        SampleNodeAddrs(ctx context.Context, numAddrs uint32,
41
                ignore map[autopilot.NodeID]struct{}) ([]*lnwire.NetAddress,
42
                error)
43

44
        // Name returns a human readable string which names the concrete
45
        // implementation of the NetworkPeerBootstrapper.
46
        Name() string
47
}
48

49
// MultiSourceBootstrap attempts to utilize a set of NetworkPeerBootstrapper
50
// passed in to return the target (numAddrs) number of peer addresses that can
51
// be used to bootstrap a peer just joining the Lightning Network. Each
52
// bootstrapper will be queried successively until the target amount is met. If
53
// the ignore map is populated, then the bootstrappers will be instructed to
54
// skip those nodes.
55
func MultiSourceBootstrap(ctx context.Context,
56
        ignore map[autopilot.NodeID]struct{}, numAddrs uint32,
57
        bootstrappers ...NetworkPeerBootstrapper) ([]*lnwire.NetAddress, error) {
3✔
58

3✔
59
        // We'll randomly shuffle our bootstrappers before querying them in
3✔
60
        // order to avoid from querying the same bootstrapper method over and
3✔
61
        // over, as some of these might tend to provide better/worse results
3✔
62
        // than others.
3✔
63
        bootstrappers = shuffleBootstrappers(bootstrappers)
3✔
64

3✔
65
        var addrs []*lnwire.NetAddress
3✔
66
        for _, bootstrapper := range bootstrappers {
6✔
67
                // If we already have enough addresses, then we can exit early
3✔
68
                // w/o querying the additional bootstrappers.
3✔
69
                if uint32(len(addrs)) >= numAddrs {
3✔
70
                        break
×
71
                }
72

73
                log.Infof("Attempting to bootstrap with: %v", bootstrapper.Name())
3✔
74

3✔
75
                // If we still need additional addresses, then we'll compute
3✔
76
                // the number of address remaining that we need to fetch.
3✔
77
                numAddrsLeft := numAddrs - uint32(len(addrs))
3✔
78
                log.Tracef("Querying for %v addresses", numAddrsLeft)
3✔
79
                netAddrs, err := bootstrapper.SampleNodeAddrs(
3✔
80
                        ctx, numAddrsLeft, ignore,
3✔
81
                )
3✔
82
                if err != nil {
3✔
83
                        // If we encounter an error with a bootstrapper, then
×
84
                        // we'll continue on to the next available
×
85
                        // bootstrapper.
×
86
                        log.Errorf("Unable to query bootstrapper %v: %v",
×
87
                                bootstrapper.Name(), err)
×
88
                        continue
×
89
                }
90

91
                addrs = append(addrs, netAddrs...)
3✔
92
        }
93

94
        if len(addrs) == 0 {
3✔
UNCOV
95
                return nil, errors.New("no addresses found")
×
UNCOV
96
        }
×
97

98
        log.Infof("Obtained %v addrs to bootstrap network with", len(addrs))
3✔
99

3✔
100
        return addrs, nil
3✔
101
}
102

103
// shuffleBootstrappers shuffles the set of bootstrappers in order to avoid
104
// querying the same bootstrapper over and over. To shuffle the set of
105
// candidates, we use a version of the Fisher–Yates shuffle algorithm.
106
func shuffleBootstrappers(candidates []NetworkPeerBootstrapper) []NetworkPeerBootstrapper {
3✔
107
        shuffled := make([]NetworkPeerBootstrapper, len(candidates))
3✔
108
        perm := prand.Perm(len(candidates))
3✔
109

3✔
110
        for i, v := range perm {
6✔
111
                shuffled[v] = candidates[i]
3✔
112
        }
3✔
113

114
        return shuffled
3✔
115
}
116

117
// ChannelGraphBootstrapper is an implementation of the NetworkPeerBootstrapper
118
// which attempts to retrieve advertised peers directly from the active channel
119
// graph. This instance requires a backing autopilot.ChannelGraph instance in
120
// order to operate properly.
121
type ChannelGraphBootstrapper struct {
122
        chanGraph autopilot.ChannelGraph
123

124
        // hashAccumulator is a set of 32 random bytes that are read upon the
125
        // creation of the channel graph bootstrapper. We use this value to
126
        // randomly select nodes within the known graph to connect to. After
127
        // each selection, we rotate the accumulator by hashing it with itself.
128
        hashAccumulator [32]byte
129

130
        tried map[autopilot.NodeID]struct{}
131
}
132

133
// A compile time assertion to ensure that ChannelGraphBootstrapper meets the
134
// NetworkPeerBootstrapper interface.
135
var _ NetworkPeerBootstrapper = (*ChannelGraphBootstrapper)(nil)
136

137
// NewGraphBootstrapper returns a new instance of a ChannelGraphBootstrapper
138
// backed by an active autopilot.ChannelGraph instance. This type of network
139
// peer bootstrapper will use the authenticated nodes within the known channel
140
// graph to bootstrap connections.
141
func NewGraphBootstrapper(cg autopilot.ChannelGraph) (NetworkPeerBootstrapper, error) {
3✔
142

3✔
143
        c := &ChannelGraphBootstrapper{
3✔
144
                chanGraph: cg,
3✔
145
                tried:     make(map[autopilot.NodeID]struct{}),
3✔
146
        }
3✔
147

3✔
148
        if _, err := rand.Read(c.hashAccumulator[:]); err != nil {
3✔
149
                return nil, err
×
150
        }
×
151

152
        return c, nil
3✔
153
}
154

155
// SampleNodeAddrs uniformly samples a set of specified address from the
156
// network peer bootstrapper source. The num addrs field passed in denotes how
157
// many valid peer addresses to return.
158
//
159
// NOTE: Part of the NetworkPeerBootstrapper interface.
160
func (c *ChannelGraphBootstrapper) SampleNodeAddrs(_ context.Context,
161
        numAddrs uint32,
162
        ignore map[autopilot.NodeID]struct{}) ([]*lnwire.NetAddress, error) {
3✔
163

3✔
164
        ctx := context.TODO()
3✔
165

3✔
166
        // We'll merge the ignore map with our currently selected map in order
3✔
167
        // to ensure we don't return any duplicate nodes.
3✔
168
        for n := range ignore {
6✔
169
                log.Tracef("Ignored node %x for bootstrapping", n)
3✔
170
                c.tried[n] = struct{}{}
3✔
171
        }
3✔
172

173
        // In order to bootstrap, we'll iterate all the nodes in the channel
174
        // graph, accumulating nodes until either we go through all active
175
        // nodes, or we reach our limit. We ensure that we meet the randomly
176
        // sample constraint as we maintain an xor accumulator to ensure we
177
        // randomly sample nodes independent of the iteration of the channel
178
        // graph.
179
        sampleAddrs := func() ([]*lnwire.NetAddress, error) {
6✔
180
                var (
3✔
181
                        a []*lnwire.NetAddress
3✔
182

3✔
183
                        // We'll create a special error so we can return early
3✔
184
                        // and abort the transaction once we find a match.
3✔
185
                        errFound = fmt.Errorf("found node")
3✔
186
                )
3✔
187

3✔
188
                err := c.chanGraph.ForEachNode(ctx, func(_ context.Context,
3✔
189
                        node autopilot.Node) error {
6✔
190

3✔
191
                        nID := autopilot.NodeID(node.PubKey())
3✔
192
                        if _, ok := c.tried[nID]; ok {
6✔
193
                                return nil
3✔
194
                        }
3✔
195

196
                        // We'll select the first node we come across who's
197
                        // public key is less than our current accumulator
198
                        // value. When comparing, we skip the first byte as
199
                        // it's 50/50. If it isn't less, than then we'll
200
                        // continue forward.
201
                        nodePubKeyBytes := node.PubKey()
3✔
202
                        if bytes.Compare(c.hashAccumulator[:], nodePubKeyBytes[1:]) > 0 {
5✔
203
                                return nil
2✔
204
                        }
2✔
205

206
                        for _, nodeAddr := range node.Addrs() {
6✔
207
                                // If we haven't yet reached our limit, then
3✔
208
                                // we'll copy over the details of this node
3✔
209
                                // into the set of addresses to be returned.
3✔
210
                                switch nodeAddr.(type) {
3✔
211
                                case *net.TCPAddr, *tor.OnionAddr:
3✔
212
                                default:
×
213
                                        // If this isn't a valid address
×
214
                                        // supported by the protocol, then we'll
×
215
                                        // skip this node.
×
216
                                        return nil
×
217
                                }
218

219
                                nodePub, err := btcec.ParsePubKey(
3✔
220
                                        nodePubKeyBytes[:],
3✔
221
                                )
3✔
222
                                if err != nil {
3✔
223
                                        return err
×
224
                                }
×
225

226
                                // At this point, we've found an eligible node,
227
                                // so we'll return early with our shibboleth
228
                                // error.
229
                                a = append(a, &lnwire.NetAddress{
3✔
230
                                        IdentityKey: nodePub,
3✔
231
                                        Address:     nodeAddr,
3✔
232
                                })
3✔
233
                        }
234

235
                        c.tried[nID] = struct{}{}
3✔
236

3✔
237
                        return errFound
3✔
238
                })
239
                if err != nil && !errors.Is(err, errFound) {
3✔
240
                        return nil, err
×
241
                }
×
242

243
                return a, nil
3✔
244
        }
245

246
        // We'll loop and sample new addresses from the graph source until
247
        // we've reached our target number of outbound connections or we hit 50
248
        // attempts, which ever comes first.
249
        var (
3✔
250
                addrs []*lnwire.NetAddress
3✔
251
                tries uint32
3✔
252
        )
3✔
253
        for tries < 30 && uint32(len(addrs)) < numAddrs {
6✔
254
                sampleAddrs, err := sampleAddrs()
3✔
255
                if err != nil {
3✔
256
                        return nil, err
×
257
                }
×
258

259
                tries++
3✔
260

3✔
261
                // We'll now rotate our hash accumulator one value forwards.
3✔
262
                c.hashAccumulator = sha256.Sum256(c.hashAccumulator[:])
3✔
263

3✔
264
                // If this attempt didn't yield any addresses, then we'll exit
3✔
265
                // early.
3✔
266
                if len(sampleAddrs) == 0 {
5✔
267
                        continue
2✔
268
                }
269

270
                addrs = append(addrs, sampleAddrs...)
3✔
271
        }
272

273
        log.Tracef("Ending hash accumulator state: %x", c.hashAccumulator)
3✔
274

3✔
275
        return addrs, nil
3✔
276
}
277

278
// Name returns a human readable string which names the concrete implementation
279
// of the NetworkPeerBootstrapper.
280
//
281
// NOTE: Part of the NetworkPeerBootstrapper interface.
282
func (c *ChannelGraphBootstrapper) Name() string {
3✔
283
        return "Authenticated Channel Graph"
3✔
284
}
3✔
285

286
// DNSSeedBootstrapper as an implementation of the NetworkPeerBootstrapper
287
// interface which implements peer bootstrapping via a special DNS seed as
288
// defined in BOLT-0010. For further details concerning Lightning's current DNS
289
// boot strapping protocol, see this link:
290
//   - https://github.com/lightningnetwork/lightning-rfc/blob/master/10-dns-bootstrap.md
291
type DNSSeedBootstrapper struct {
292
        // dnsSeeds is an array of two tuples we'll use for bootstrapping. The
293
        // first item in the tuple is the primary host we'll use to attempt the
294
        // SRV lookup we require. If we're unable to receive a response over
295
        // UDP, then we'll fall back to manual TCP resolution. The second item
296
        // in the tuple is a special A record that we'll query in order to
297
        // receive the IP address of the current authoritative DNS server for
298
        // the network seed.
299
        dnsSeeds [][2]string
300
        net      tor.Net
301

302
        // timeout is the maximum amount of time a dial will wait for a connect to
303
        // complete.
304
        timeout time.Duration
305
}
306

307
// A compile time assertion to ensure that DNSSeedBootstrapper meets the
308
// NetworkPeerjBootstrapper interface.
309
var _ NetworkPeerBootstrapper = (*ChannelGraphBootstrapper)(nil)
310

311
// NewDNSSeedBootstrapper returns a new instance of the DNSSeedBootstrapper.
312
// The set of passed seeds should point to DNS servers that properly implement
313
// Lightning's DNS peer bootstrapping protocol as defined in BOLT-0010. The set
314
// of passed DNS seeds should come in pairs, with the second host name to be
315
// used as a fallback for manual TCP resolution in the case of an error
316
// receiving the UDP response. The second host should return a single A record
317
// with the IP address of the authoritative name server.
318
func NewDNSSeedBootstrapper(
319
        seeds [][2]string, net tor.Net,
320
        timeout time.Duration) NetworkPeerBootstrapper {
×
321

×
322
        return &DNSSeedBootstrapper{dnsSeeds: seeds, net: net, timeout: timeout}
×
323
}
×
324

325
// fallBackSRVLookup attempts to manually query for SRV records we need to
326
// properly bootstrap. We do this by querying the special record at the "soa."
327
// sub-domain of supporting DNS servers. The returned IP address will be the IP
328
// address of the authoritative DNS server. Once we have this IP address, we'll
329
// connect manually over TCP to request the SRV record. This is necessary as
330
// the records we return are currently too large for a class of resolvers,
331
// causing them to be filtered out. The targetEndPoint is the original end
332
// point that was meant to be hit.
333
func (d *DNSSeedBootstrapper) fallBackSRVLookup(soaShim string,
334
        targetEndPoint string) ([]*net.SRV, error) {
×
335

×
336
        log.Tracef("Attempting to query fallback DNS seed")
×
337

×
338
        // First, we'll lookup the IP address of the server that will act as
×
339
        // our shim.
×
340
        addrs, err := d.net.LookupHost(soaShim)
×
341
        if err != nil {
×
342
                return nil, err
×
343
        }
×
344

345
        // Once we have the IP address, we'll establish a TCP connection using
346
        // port 53.
347
        dnsServer := net.JoinHostPort(addrs[0], "53")
×
348
        conn, err := d.net.Dial("tcp", dnsServer, d.timeout)
×
349
        if err != nil {
×
350
                return nil, err
×
351
        }
×
352

353
        dnsHost := fmt.Sprintf("_nodes._tcp.%v.", targetEndPoint)
×
354
        dnsConn := &dns.Conn{Conn: conn}
×
355
        defer dnsConn.Close()
×
356

×
357
        // With the connection established, we'll craft our SRV query, write
×
358
        // toe request, then wait for the server to give our response.
×
359
        msg := new(dns.Msg)
×
360
        msg.SetQuestion(dnsHost, dns.TypeSRV)
×
361
        if err := dnsConn.WriteMsg(msg); err != nil {
×
362
                return nil, err
×
363
        }
×
364
        resp, err := dnsConn.ReadMsg()
×
365
        if err != nil {
×
366
                return nil, err
×
367
        }
×
368

369
        // If the message response code was not the success code, fail.
370
        if resp.Rcode != dns.RcodeSuccess {
×
371
                return nil, fmt.Errorf("unsuccessful SRV request, "+
×
372
                        "received: %v", resp.Rcode)
×
373
        }
×
374

375
        // Retrieve the RR(s) of the Answer section, and convert to the format
376
        // that net.LookupSRV would normally return.
377
        var rrs []*net.SRV
×
378
        for _, rr := range resp.Answer {
×
379
                srv := rr.(*dns.SRV)
×
380
                rrs = append(rrs, &net.SRV{
×
381
                        Target:   srv.Target,
×
382
                        Port:     srv.Port,
×
383
                        Priority: srv.Priority,
×
384
                        Weight:   srv.Weight,
×
385
                })
×
386
        }
×
387

388
        return rrs, nil
×
389
}
390

391
// SampleNodeAddrs uniformly samples a set of specified address from the
392
// network peer bootstrapper source. The num addrs field passed in denotes how
393
// many valid peer addresses to return. The set of DNS seeds are used
394
// successively to retrieve eligible target nodes.
395
func (d *DNSSeedBootstrapper) SampleNodeAddrs(_ context.Context,
396
        numAddrs uint32,
397
        ignore map[autopilot.NodeID]struct{}) ([]*lnwire.NetAddress, error) {
×
398

×
399
        var netAddrs []*lnwire.NetAddress
×
400

×
401
        // We'll try all the registered DNS seeds, exiting early if one of them
×
402
        // gives us all the peers we need.
×
403
        //
×
404
        // TODO(roasbeef): should combine results from both
×
405
search:
×
406
        for _, dnsSeedTuple := range d.dnsSeeds {
×
407
                // We'll first query the seed with an SRV record so we can
×
408
                // obtain a random sample of the encoded public keys of nodes.
×
409
                // We use the lndLookupSRV function for this task.
×
410
                primarySeed := dnsSeedTuple[0]
×
411
                _, addrs, err := d.net.LookupSRV(
×
412
                        "nodes", "tcp", primarySeed, d.timeout,
×
413
                )
×
414
                if err != nil {
×
415
                        log.Tracef("Unable to lookup SRV records via "+
×
416
                                "primary seed (%v): %v", primarySeed, err)
×
417

×
418
                        log.Trace("Falling back to secondary")
×
419

×
420
                        // If the host of the secondary seed is blank, then
×
421
                        // we'll bail here as we can't proceed.
×
422
                        if dnsSeedTuple[1] == "" {
×
423
                                log.Tracef("DNS seed %v has no secondary, "+
×
424
                                        "skipping fallback", primarySeed)
×
425
                                continue
×
426
                        }
427

428
                        // If we get an error when trying to query via the
429
                        // primary seed, we'll fallback to the secondary seed
430
                        // before concluding failure.
431
                        soaShim := dnsSeedTuple[1]
×
432
                        addrs, err = d.fallBackSRVLookup(
×
433
                                soaShim, primarySeed,
×
434
                        )
×
435
                        if err != nil {
×
436
                                log.Tracef("Unable to query fall "+
×
437
                                        "back dns seed (%v): %v", soaShim, err)
×
438
                                continue
×
439
                        }
440

441
                        log.Tracef("Successfully queried fallback DNS seed")
×
442
                }
443

444
                log.Tracef("Retrieved SRV records from dns seed: %v",
×
445
                        lnutils.SpewLogClosure(addrs))
×
446

×
447
                // Next, we'll need to issue an A record request for each of
×
448
                // the nodes, skipping it if nothing comes back.
×
449
                for _, nodeSrv := range addrs {
×
450
                        if uint32(len(netAddrs)) >= numAddrs {
×
451
                                break search
×
452
                        }
453

454
                        // With the SRV target obtained, we'll now perform
455
                        // another query to obtain the IP address for the
456
                        // matching bech32 encoded node key. We use the
457
                        // lndLookup function for this task.
458
                        bechNodeHost := nodeSrv.Target
×
459
                        addrs, err := d.net.LookupHost(bechNodeHost)
×
460
                        if err != nil {
×
461
                                return nil, err
×
462
                        }
×
463

464
                        if len(addrs) == 0 {
×
465
                                log.Tracef("No addresses for %v, skipping",
×
466
                                        bechNodeHost)
×
467
                                continue
×
468
                        }
469

470
                        log.Tracef("Attempting to convert: %v", bechNodeHost)
×
471

×
472
                        // If the host isn't correctly formatted, then we'll
×
473
                        // skip it.
×
474
                        if len(bechNodeHost) == 0 ||
×
475
                                !strings.Contains(bechNodeHost, ".") {
×
476

×
477
                                continue
×
478
                        }
479

480
                        // If we have a set of valid addresses, then we'll need
481
                        // to parse the public key from the original bech32
482
                        // encoded string.
483
                        bechNode := strings.Split(bechNodeHost, ".")
×
484
                        _, nodeBytes5Bits, err := bech32.Decode(bechNode[0])
×
485
                        if err != nil {
×
486
                                return nil, err
×
487
                        }
×
488

489
                        // Once we have the bech32 decoded pubkey, we'll need
490
                        // to convert the 5-bit word grouping into our regular
491
                        // 8-bit word grouping so we can convert it into a
492
                        // public key.
493
                        nodeBytes, err := bech32.ConvertBits(
×
494
                                nodeBytes5Bits, 5, 8, false,
×
495
                        )
×
496
                        if err != nil {
×
497
                                return nil, err
×
498
                        }
×
499
                        nodeKey, err := btcec.ParsePubKey(nodeBytes)
×
500
                        if err != nil {
×
501
                                return nil, err
×
502
                        }
×
503

504
                        // If we have an ignore list, and this node is in the
505
                        // ignore list, then we'll go to the next candidate.
506
                        if ignore != nil {
×
507
                                nID := autopilot.NewNodeID(nodeKey)
×
508
                                if _, ok := ignore[nID]; ok {
×
509
                                        continue
×
510
                                }
511
                        }
512

513
                        // Finally we'll convert the host:port peer to a proper
514
                        // TCP address to use within the lnwire.NetAddress. We
515
                        // don't need to use the lndResolveTCP function here
516
                        // because we already have the host:port peer.
517
                        addr := net.JoinHostPort(
×
518
                                addrs[0],
×
519
                                strconv.FormatUint(uint64(nodeSrv.Port), 10),
×
520
                        )
×
521
                        tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
×
522
                        if err != nil {
×
523
                                return nil, err
×
524
                        }
×
525

526
                        // Finally, with all the information parsed, we'll
527
                        // return this fully valid address as a connection
528
                        // attempt.
529
                        lnAddr := &lnwire.NetAddress{
×
530
                                IdentityKey: nodeKey,
×
531
                                Address:     tcpAddr,
×
532
                        }
×
533

×
534
                        log.Tracef("Obtained %v as valid reachable "+
×
535
                                "node", lnAddr)
×
536

×
537
                        netAddrs = append(netAddrs, lnAddr)
×
538
                }
539
        }
540

541
        return netAddrs, nil
×
542
}
543

544
// Name returns a human readable string which names the concrete
545
// implementation of the NetworkPeerBootstrapper.
546
func (d *DNSSeedBootstrapper) Name() string {
×
547
        return fmt.Sprintf("BOLT-0010 DNS Seed: %v", d.dnsSeeds)
×
548
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc