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

lightningnetwork / lnd / 13305418364

13 Feb 2025 10:17AM UTC coverage: 49.364% (-0.002%) from 49.366%
13305418364

push

github

web-flow
Merge pull request #9501 from yyforyongyu/getinfo-blockheight

rpcserver: check `blockbeatDispatcher` when deciding `isSynced`

60 of 72 new or added lines in 2 files covered. (83.33%)

68 existing lines in 16 files now uncovered.

100780 of 204155 relevant lines covered (49.36%)

1.54 hits per line

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

60.02
/graph/builder.go
1
package graph
2

3
import (
4
        "fmt"
5
        "sync"
6
        "sync/atomic"
7
        "time"
8

9
        "github.com/btcsuite/btcd/btcec/v2"
10
        "github.com/btcsuite/btcd/wire"
11
        "github.com/go-errors/errors"
12
        "github.com/lightningnetwork/lnd/batch"
13
        "github.com/lightningnetwork/lnd/chainntnfs"
14
        graphdb "github.com/lightningnetwork/lnd/graph/db"
15
        "github.com/lightningnetwork/lnd/graph/db/models"
16
        "github.com/lightningnetwork/lnd/kvdb"
17
        "github.com/lightningnetwork/lnd/lnutils"
18
        "github.com/lightningnetwork/lnd/lnwallet"
19
        "github.com/lightningnetwork/lnd/lnwire"
20
        "github.com/lightningnetwork/lnd/multimutex"
21
        "github.com/lightningnetwork/lnd/netann"
22
        "github.com/lightningnetwork/lnd/routing/chainview"
23
        "github.com/lightningnetwork/lnd/routing/route"
24
        "github.com/lightningnetwork/lnd/ticker"
25
)
26

27
const (
28
        // DefaultChannelPruneExpiry is the default duration used to determine
29
        // if a channel should be pruned or not.
30
        DefaultChannelPruneExpiry = time.Hour * 24 * 14
31

32
        // DefaultFirstTimePruneDelay is the time we'll wait after startup
33
        // before attempting to prune the graph for zombie channels. We don't
34
        // do it immediately after startup to allow lnd to start up without
35
        // getting blocked by this job.
36
        DefaultFirstTimePruneDelay = 30 * time.Second
37

38
        // defaultStatInterval governs how often the router will log non-empty
39
        // stats related to processing new channels, updates, or node
40
        // announcements.
41
        defaultStatInterval = time.Minute
42
)
43

44
var (
45
        // ErrGraphBuilderShuttingDown is returned if the graph builder is in
46
        // the process of shutting down.
47
        ErrGraphBuilderShuttingDown = fmt.Errorf("graph builder shutting down")
48
)
49

50
// Config holds the configuration required by the Builder.
51
type Config struct {
52
        // SelfNode is the public key of the node that this channel router
53
        // belongs to.
54
        SelfNode route.Vertex
55

56
        // Graph is the channel graph that the ChannelRouter will use to gather
57
        // metrics from and also to carry out path finding queries.
58
        Graph DB
59

60
        // Chain is the router's source to the most up-to-date blockchain data.
61
        // All incoming advertised channels will be checked against the chain
62
        // to ensure that the channels advertised are still open.
63
        Chain lnwallet.BlockChainIO
64

65
        // ChainView is an instance of a FilteredChainView which is used to
66
        // watch the sub-set of the UTXO set (the set of active channels) that
67
        // we need in order to properly maintain the channel graph.
68
        ChainView chainview.FilteredChainView
69

70
        // Notifier is a reference to the ChainNotifier, used to grab
71
        // the latest blocks if the router is missing any.
72
        Notifier chainntnfs.ChainNotifier
73

74
        // ChannelPruneExpiry is the duration used to determine if a channel
75
        // should be pruned or not. If the delta between now and when the
76
        // channel was last updated is greater than ChannelPruneExpiry, then
77
        // the channel is marked as a zombie channel eligible for pruning.
78
        ChannelPruneExpiry time.Duration
79

80
        // GraphPruneInterval is used as an interval to determine how often we
81
        // should examine the channel graph to garbage collect zombie channels.
82
        GraphPruneInterval time.Duration
83

84
        // FirstTimePruneDelay is the time we'll wait after startup before
85
        // attempting to prune the graph for zombie channels. We don't do it
86
        // immediately after startup to allow lnd to start up without getting
87
        // blocked by this job.
88
        FirstTimePruneDelay time.Duration
89

90
        // AssumeChannelValid toggles whether the builder will prune channels
91
        // based on their spentness vs using the fact that they are considered
92
        // zombies.
93
        AssumeChannelValid bool
94

95
        // StrictZombiePruning determines if we attempt to prune zombie
96
        // channels according to a stricter criteria. If true, then we'll prune
97
        // a channel if only *one* of the edges is considered a zombie.
98
        // Otherwise, we'll only prune the channel when both edges have a very
99
        // dated last update.
100
        StrictZombiePruning bool
101

102
        // IsAlias returns whether a passed ShortChannelID is an alias. This is
103
        // only used for our local channels.
104
        IsAlias func(scid lnwire.ShortChannelID) bool
105
}
106

107
// Builder builds and maintains a view of the Lightning Network graph.
108
type Builder struct {
109
        started atomic.Bool
110
        stopped atomic.Bool
111

112
        ntfnClientCounter atomic.Uint64
113
        bestHeight        atomic.Uint32
114

115
        cfg *Config
116

117
        // newBlocks is a channel in which new blocks connected to the end of
118
        // the main chain are sent over, and blocks updated after a call to
119
        // UpdateFilter.
120
        newBlocks <-chan *chainview.FilteredBlock
121

122
        // staleBlocks is a channel in which blocks disconnected from the end
123
        // of our currently known best chain are sent over.
124
        staleBlocks <-chan *chainview.FilteredBlock
125

126
        // topologyClients maps a client's unique notification ID to a
127
        // topologyClient client that contains its notification dispatch
128
        // channel.
129
        topologyClients *lnutils.SyncMap[uint64, *topologyClient]
130

131
        // ntfnClientUpdates is a channel that's used to send new updates to
132
        // topology notification clients to the Builder. Updates either
133
        // add a new notification client, or cancel notifications for an
134
        // existing client.
135
        ntfnClientUpdates chan *topologyClientUpdate
136

137
        // channelEdgeMtx is a mutex we use to make sure we process only one
138
        // ChannelEdgePolicy at a time for a given channelID, to ensure
139
        // consistency between the various database accesses.
140
        channelEdgeMtx *multimutex.Mutex[uint64]
141

142
        // statTicker is a resumable ticker that logs the router's progress as
143
        // it discovers channels or receives updates.
144
        statTicker ticker.Ticker
145

146
        // stats tracks newly processed channels, updates, and node
147
        // announcements over a window of defaultStatInterval.
148
        stats *builderStats
149

150
        quit chan struct{}
151
        wg   sync.WaitGroup
152
}
153

154
// A compile time check to ensure Builder implements the
155
// ChannelGraphSource interface.
156
var _ ChannelGraphSource = (*Builder)(nil)
157

158
// NewBuilder constructs a new Builder.
159
func NewBuilder(cfg *Config) (*Builder, error) {
3✔
160
        return &Builder{
3✔
161
                cfg:               cfg,
3✔
162
                topologyClients:   &lnutils.SyncMap[uint64, *topologyClient]{},
3✔
163
                ntfnClientUpdates: make(chan *topologyClientUpdate),
3✔
164
                channelEdgeMtx:    multimutex.NewMutex[uint64](),
3✔
165
                statTicker:        ticker.New(defaultStatInterval),
3✔
166
                stats:             new(builderStats),
3✔
167
                quit:              make(chan struct{}),
3✔
168
        }, nil
3✔
169
}
3✔
170

171
// Start launches all the goroutines the Builder requires to carry out its
172
// duties. If the builder has already been started, then this method is a noop.
173
func (b *Builder) Start() error {
3✔
174
        if !b.started.CompareAndSwap(false, true) {
3✔
175
                return nil
×
176
        }
×
177

178
        log.Info("Builder starting")
3✔
179

3✔
180
        bestHash, bestHeight, err := b.cfg.Chain.GetBestBlock()
3✔
181
        if err != nil {
3✔
182
                return err
×
183
        }
×
184

185
        // If the graph has never been pruned, or hasn't fully been created yet,
186
        // then we don't treat this as an explicit error.
187
        if _, _, err := b.cfg.Graph.PruneTip(); err != nil {
6✔
188
                switch {
3✔
189
                case errors.Is(err, graphdb.ErrGraphNeverPruned):
3✔
190
                        fallthrough
3✔
191

192
                case errors.Is(err, graphdb.ErrGraphNotFound):
3✔
193
                        // If the graph has never been pruned, then we'll set
3✔
194
                        // the prune height to the current best height of the
3✔
195
                        // chain backend.
3✔
196
                        _, err = b.cfg.Graph.PruneGraph(
3✔
197
                                nil, bestHash, uint32(bestHeight),
3✔
198
                        )
3✔
199
                        if err != nil {
3✔
200
                                return err
×
201
                        }
×
202

203
                default:
×
204
                        return err
×
205
                }
206
        }
207

208
        // If AssumeChannelValid is present, then we won't rely on pruning
209
        // channels from the graph based on their spentness, but whether they
210
        // are considered zombies or not. We will start zombie pruning after a
211
        // small delay, to avoid slowing down startup of lnd.
212
        if b.cfg.AssumeChannelValid { //nolint:nestif
3✔
213
                time.AfterFunc(b.cfg.FirstTimePruneDelay, func() {
×
214
                        select {
×
215
                        case <-b.quit:
×
216
                                return
×
217
                        default:
×
218
                        }
219

220
                        log.Info("Initial zombie prune starting")
×
221
                        if err := b.pruneZombieChans(); err != nil {
×
222
                                log.Errorf("Unable to prune zombies: %v", err)
×
223
                        }
×
224
                })
225
        } else {
3✔
226
                // Otherwise, we'll use our filtered chain view to prune
3✔
227
                // channels as soon as they are detected as spent on-chain.
3✔
228
                if err := b.cfg.ChainView.Start(); err != nil {
3✔
229
                        return err
×
230
                }
×
231

232
                // Once the instance is active, we'll fetch the channel we'll
233
                // receive notifications over.
234
                b.newBlocks = b.cfg.ChainView.FilteredBlocks()
3✔
235
                b.staleBlocks = b.cfg.ChainView.DisconnectedBlocks()
3✔
236

3✔
237
                // Before we perform our manual block pruning, we'll construct
3✔
238
                // and apply a fresh chain filter to the active
3✔
239
                // FilteredChainView instance.  We do this before, as otherwise
3✔
240
                // we may miss on-chain events as the filter hasn't properly
3✔
241
                // been applied.
3✔
242
                channelView, err := b.cfg.Graph.ChannelView()
3✔
243
                if err != nil && !errors.Is(
3✔
244
                        err, graphdb.ErrGraphNoEdgesFound,
3✔
245
                ) {
3✔
246

×
247
                        return err
×
248
                }
×
249

250
                log.Infof("Filtering chain using %v channels active",
3✔
251
                        len(channelView))
3✔
252

3✔
253
                if len(channelView) != 0 {
6✔
254
                        err = b.cfg.ChainView.UpdateFilter(
3✔
255
                                channelView, uint32(bestHeight),
3✔
256
                        )
3✔
257
                        if err != nil {
3✔
258
                                return err
×
259
                        }
×
260
                }
261

262
                // The graph pruning might have taken a while and there could be
263
                // new blocks available.
264
                _, bestHeight, err = b.cfg.Chain.GetBestBlock()
3✔
265
                if err != nil {
3✔
266
                        return err
×
267
                }
×
268
                b.bestHeight.Store(uint32(bestHeight))
3✔
269

3✔
270
                // Before we begin normal operation of the router, we first need
3✔
271
                // to synchronize the channel graph to the latest state of the
3✔
272
                // UTXO set.
3✔
273
                if err := b.syncGraphWithChain(); err != nil {
3✔
274
                        return err
×
275
                }
×
276

277
                // Finally, before we proceed, we'll prune any unconnected nodes
278
                // from the graph in order to ensure we maintain a tight graph
279
                // of "useful" nodes.
280
                err = b.cfg.Graph.PruneGraphNodes()
3✔
281
                if err != nil &&
3✔
282
                        !errors.Is(err, graphdb.ErrGraphNodesNotFound) {
3✔
283

×
284
                        return err
×
285
                }
×
286
        }
287

288
        b.wg.Add(1)
3✔
289
        go b.networkHandler()
3✔
290

3✔
291
        log.Debug("Builder started")
3✔
292

3✔
293
        return nil
3✔
294
}
295

296
// Stop signals to the Builder that it should halt all routines. This method
297
// will *block* until all goroutines have excited. If the builder has already
298
// stopped then this method will return immediately.
299
func (b *Builder) Stop() error {
3✔
300
        if !b.stopped.CompareAndSwap(false, true) {
3✔
301
                return nil
×
302
        }
×
303

304
        log.Info("Builder shutting down...")
3✔
305

3✔
306
        // Our filtered chain view could've only been started if
3✔
307
        // AssumeChannelValid isn't present.
3✔
308
        if !b.cfg.AssumeChannelValid {
6✔
309
                if err := b.cfg.ChainView.Stop(); err != nil {
3✔
310
                        return err
×
311
                }
×
312
        }
313

314
        close(b.quit)
3✔
315
        b.wg.Wait()
3✔
316

3✔
317
        log.Debug("Builder shutdown complete")
3✔
318

3✔
319
        return nil
3✔
320
}
321

322
// syncGraphWithChain attempts to synchronize the current channel graph with
323
// the latest UTXO set state. This process involves pruning from the channel
324
// graph any channels which have been closed by spending their funding output
325
// since we've been down.
326
func (b *Builder) syncGraphWithChain() error {
3✔
327
        // First, we'll need to check to see if we're already in sync with the
3✔
328
        // latest state of the UTXO set.
3✔
329
        bestHash, bestHeight, err := b.cfg.Chain.GetBestBlock()
3✔
330
        if err != nil {
3✔
331
                return err
×
332
        }
×
333
        b.bestHeight.Store(uint32(bestHeight))
3✔
334

3✔
335
        pruneHash, pruneHeight, err := b.cfg.Graph.PruneTip()
3✔
336
        if err != nil {
3✔
337
                switch {
×
338
                // If the graph has never been pruned, or hasn't fully been
339
                // created yet, then we don't treat this as an explicit error.
340
                case errors.Is(err, graphdb.ErrGraphNeverPruned):
×
341
                case errors.Is(err, graphdb.ErrGraphNotFound):
×
342
                default:
×
343
                        return err
×
344
                }
345
        }
346

347
        log.Infof("Prune tip for Channel Graph: height=%v, hash=%v",
3✔
348
                pruneHeight, pruneHash)
3✔
349

3✔
350
        switch {
3✔
351
        // If the graph has never been pruned, then we can exit early as this
352
        // entails it's being created for the first time and hasn't seen any
353
        // block or created channels.
354
        case pruneHeight == 0 || pruneHash == nil:
×
355
                return nil
×
356

357
        // If the block hashes and heights match exactly, then we don't need to
358
        // prune the channel graph as we're already fully in sync.
359
        case bestHash.IsEqual(pruneHash) && uint32(bestHeight) == pruneHeight:
3✔
360
                return nil
3✔
361
        }
362

363
        // If the main chain blockhash at prune height is different from the
364
        // prune hash, this might indicate the database is on a stale branch.
365
        mainBlockHash, err := b.cfg.Chain.GetBlockHash(int64(pruneHeight))
3✔
366
        if err != nil {
3✔
367
                return err
×
368
        }
×
369

370
        // While we are on a stale branch of the chain, walk backwards to find
371
        // first common block.
372
        for !pruneHash.IsEqual(mainBlockHash) {
3✔
373
                log.Infof("channel graph is stale. Disconnecting block %v "+
×
374
                        "(hash=%v)", pruneHeight, pruneHash)
×
375
                // Prune the graph for every channel that was opened at height
×
376
                // >= pruneHeight.
×
377
                _, err := b.cfg.Graph.DisconnectBlockAtHeight(pruneHeight)
×
378
                if err != nil {
×
379
                        return err
×
380
                }
×
381

382
                pruneHash, pruneHeight, err = b.cfg.Graph.PruneTip()
×
383
                switch {
×
384
                // If at this point the graph has never been pruned, we can exit
385
                // as this entails we are back to the point where it hasn't seen
386
                // any block or created channels, alas there's nothing left to
387
                // prune.
388
                case errors.Is(err, graphdb.ErrGraphNeverPruned):
×
389
                        return nil
×
390

391
                case errors.Is(err, graphdb.ErrGraphNotFound):
×
392
                        return nil
×
393

394
                case err != nil:
×
395
                        return err
×
396

397
                default:
×
398
                }
399

400
                mainBlockHash, err = b.cfg.Chain.GetBlockHash(
×
401
                        int64(pruneHeight),
×
402
                )
×
403
                if err != nil {
×
404
                        return err
×
405
                }
×
406
        }
407

408
        log.Infof("Syncing channel graph from height=%v (hash=%v) to "+
3✔
409
                "height=%v (hash=%v)", pruneHeight, pruneHash, bestHeight,
3✔
410
                bestHash)
3✔
411

3✔
412
        // If we're not yet caught up, then we'll walk forward in the chain
3✔
413
        // pruning the channel graph with each new block that hasn't yet been
3✔
414
        // consumed by the channel graph.
3✔
415
        var spentOutputs []*wire.OutPoint
3✔
416
        for nextHeight := pruneHeight + 1; nextHeight <= uint32(bestHeight); nextHeight++ { //nolint:ll
6✔
417
                // Break out of the rescan early if a shutdown has been
3✔
418
                // requested, otherwise long rescans will block the daemon from
3✔
419
                // shutting down promptly.
3✔
420
                select {
3✔
421
                case <-b.quit:
×
422
                        return ErrGraphBuilderShuttingDown
×
423
                default:
3✔
424
                }
425

426
                // Using the next height, request a manual block pruning from
427
                // the chainview for the particular block hash.
428
                log.Infof("Filtering block for closed channels, at height: %v",
3✔
429
                        int64(nextHeight))
3✔
430
                nextHash, err := b.cfg.Chain.GetBlockHash(int64(nextHeight))
3✔
431
                if err != nil {
3✔
432
                        return err
×
433
                }
×
434
                log.Tracef("Running block filter on block with hash: %v",
3✔
435
                        nextHash)
3✔
436
                filterBlock, err := b.cfg.ChainView.FilterBlock(nextHash)
3✔
437
                if err != nil {
3✔
438
                        return err
×
439
                }
×
440

441
                // We're only interested in all prior outputs that have been
442
                // spent in the block, so collate all the referenced previous
443
                // outpoints within each tx and input.
444
                for _, tx := range filterBlock.Transactions {
6✔
445
                        for _, txIn := range tx.TxIn {
6✔
446
                                spentOutputs = append(spentOutputs,
3✔
447
                                        &txIn.PreviousOutPoint)
3✔
448
                        }
3✔
449
                }
450
        }
451

452
        // With the spent outputs gathered, attempt to prune the channel graph,
453
        // also passing in the best hash+height so the prune tip can be updated.
454
        closedChans, err := b.cfg.Graph.PruneGraph(
3✔
455
                spentOutputs, bestHash, uint32(bestHeight),
3✔
456
        )
3✔
457
        if err != nil {
3✔
458
                return err
×
459
        }
×
460

461
        log.Infof("Graph pruning complete: %v channels were closed since "+
3✔
462
                "height %v", len(closedChans), pruneHeight)
3✔
463

3✔
464
        return nil
3✔
465
}
466

467
// isZombieChannel takes two edge policy updates and determines if the
468
// corresponding channel should be considered a zombie. The first boolean is
469
// true if the policy update from node 1 is considered a zombie, the second
470
// boolean is that of node 2, and the final boolean is true if the channel
471
// is considered a zombie.
472
func (b *Builder) isZombieChannel(e1,
473
        e2 *models.ChannelEdgePolicy) (bool, bool, bool) {
×
474

×
475
        chanExpiry := b.cfg.ChannelPruneExpiry
×
476

×
477
        e1Zombie := e1 == nil || time.Since(e1.LastUpdate) >= chanExpiry
×
478
        e2Zombie := e2 == nil || time.Since(e2.LastUpdate) >= chanExpiry
×
479

×
480
        var e1Time, e2Time time.Time
×
481
        if e1 != nil {
×
482
                e1Time = e1.LastUpdate
×
483
        }
×
484
        if e2 != nil {
×
485
                e2Time = e2.LastUpdate
×
486
        }
×
487

488
        return e1Zombie, e2Zombie, b.IsZombieChannel(e1Time, e2Time)
×
489
}
490

491
// IsZombieChannel takes the timestamps of the latest channel updates for a
492
// channel and returns true if the channel should be considered a zombie based
493
// on these timestamps.
494
func (b *Builder) IsZombieChannel(updateTime1,
495
        updateTime2 time.Time) bool {
3✔
496

3✔
497
        chanExpiry := b.cfg.ChannelPruneExpiry
3✔
498

3✔
499
        e1Zombie := updateTime1.IsZero() ||
3✔
500
                time.Since(updateTime1) >= chanExpiry
3✔
501

3✔
502
        e2Zombie := updateTime2.IsZero() ||
3✔
503
                time.Since(updateTime2) >= chanExpiry
3✔
504

3✔
505
        // If we're using strict zombie pruning, then a channel is only
3✔
506
        // considered live if both edges have a recent update we know of.
3✔
507
        if b.cfg.StrictZombiePruning {
4✔
508
                return e1Zombie || e2Zombie
1✔
509
        }
1✔
510

511
        // Otherwise, if we're using the less strict variant, then a channel is
512
        // considered live if either of the edges have a recent update.
513
        return e1Zombie && e2Zombie
2✔
514
}
515

516
// pruneZombieChans is a method that will be called periodically to prune out
517
// any "zombie" channels. We consider channels zombies if *both* edges haven't
518
// been updated since our zombie horizon. If AssumeChannelValid is present,
519
// we'll also consider channels zombies if *both* edges are disabled. This
520
// usually signals that a channel has been closed on-chain. We do this
521
// periodically to keep a healthy, lively routing table.
522
func (b *Builder) pruneZombieChans() error {
×
523
        chansToPrune := make(map[uint64]struct{})
×
524
        chanExpiry := b.cfg.ChannelPruneExpiry
×
525

×
526
        log.Infof("Examining channel graph for zombie channels")
×
527

×
528
        // A helper method to detect if the channel belongs to this node
×
529
        isSelfChannelEdge := func(info *models.ChannelEdgeInfo) bool {
×
530
                return info.NodeKey1Bytes == b.cfg.SelfNode ||
×
531
                        info.NodeKey2Bytes == b.cfg.SelfNode
×
532
        }
×
533

534
        // First, we'll collect all the channels which are eligible for garbage
535
        // collection due to being zombies.
536
        filterPruneChans := func(info *models.ChannelEdgeInfo,
×
537
                e1, e2 *models.ChannelEdgePolicy) error {
×
538

×
539
                // Exit early in case this channel is already marked to be
×
540
                // pruned
×
541
                _, markedToPrune := chansToPrune[info.ChannelID]
×
542
                if markedToPrune {
×
543
                        return nil
×
544
                }
×
545

546
                // We'll ensure that we don't attempt to prune our *own*
547
                // channels from the graph, as in any case this should be
548
                // re-advertised by the sub-system above us.
549
                if isSelfChannelEdge(info) {
×
550
                        return nil
×
551
                }
×
552

553
                e1Zombie, e2Zombie, isZombieChan := b.isZombieChannel(e1, e2)
×
554

×
555
                if e1Zombie {
×
556
                        log.Tracef("Node1 pubkey=%x of chan_id=%v is zombie",
×
557
                                info.NodeKey1Bytes, info.ChannelID)
×
558
                }
×
559

560
                if e2Zombie {
×
561
                        log.Tracef("Node2 pubkey=%x of chan_id=%v is zombie",
×
562
                                info.NodeKey2Bytes, info.ChannelID)
×
563
                }
×
564

565
                // If either edge hasn't been updated for a period of
566
                // chanExpiry, then we'll mark the channel itself as eligible
567
                // for graph pruning.
568
                if !isZombieChan {
×
569
                        return nil
×
570
                }
×
571

572
                log.Debugf("ChannelID(%v) is a zombie, collecting to prune",
×
573
                        info.ChannelID)
×
574

×
575
                // TODO(roasbeef): add ability to delete single directional edge
×
576
                chansToPrune[info.ChannelID] = struct{}{}
×
577

×
578
                return nil
×
579
        }
580

581
        // If AssumeChannelValid is present we'll look at the disabled bit for
582
        // both edges. If they're both disabled, then we can interpret this as
583
        // the channel being closed and can prune it from our graph.
584
        if b.cfg.AssumeChannelValid {
×
585
                disabledChanIDs, err := b.cfg.Graph.DisabledChannelIDs()
×
586
                if err != nil {
×
587
                        return fmt.Errorf("unable to get disabled channels "+
×
588
                                "ids chans: %v", err)
×
589
                }
×
590

591
                disabledEdges, err := b.cfg.Graph.FetchChanInfos(
×
592
                        disabledChanIDs,
×
593
                )
×
594
                if err != nil {
×
595
                        return fmt.Errorf("unable to fetch disabled channels "+
×
596
                                "edges chans: %v", err)
×
597
                }
×
598

599
                // Ensuring we won't prune our own channel from the graph.
600
                for _, disabledEdge := range disabledEdges {
×
601
                        if !isSelfChannelEdge(disabledEdge.Info) {
×
602
                                chansToPrune[disabledEdge.Info.ChannelID] =
×
603
                                        struct{}{}
×
604
                        }
×
605
                }
606
        }
607

608
        startTime := time.Unix(0, 0)
×
609
        endTime := time.Now().Add(-1 * chanExpiry)
×
610
        oldEdges, err := b.cfg.Graph.ChanUpdatesInHorizon(startTime, endTime)
×
611
        if err != nil {
×
612
                return fmt.Errorf("unable to fetch expired channel updates "+
×
613
                        "chans: %v", err)
×
614
        }
×
615

616
        for _, u := range oldEdges {
×
617
                err = filterPruneChans(u.Info, u.Policy1, u.Policy2)
×
618
                if err != nil {
×
619
                        return fmt.Errorf("error filtering channels to "+
×
620
                                "prune: %w", err)
×
621
                }
×
622
        }
623

624
        log.Infof("Pruning %v zombie channels", len(chansToPrune))
×
625
        if len(chansToPrune) == 0 {
×
626
                return nil
×
627
        }
×
628

629
        // With the set of zombie-like channels obtained, we'll do another pass
630
        // to delete them from the channel graph.
631
        toPrune := make([]uint64, 0, len(chansToPrune))
×
632
        for chanID := range chansToPrune {
×
633
                toPrune = append(toPrune, chanID)
×
634
                log.Tracef("Pruning zombie channel with ChannelID(%v)", chanID)
×
635
        }
×
636
        err = b.cfg.Graph.DeleteChannelEdges(
×
637
                b.cfg.StrictZombiePruning, true, toPrune...,
×
638
        )
×
639
        if err != nil {
×
640
                return fmt.Errorf("unable to delete zombie channels: %w", err)
×
641
        }
×
642

643
        // With the channels pruned, we'll also attempt to prune any nodes that
644
        // were a part of them.
645
        err = b.cfg.Graph.PruneGraphNodes()
×
646
        if err != nil && !errors.Is(err, graphdb.ErrGraphNodesNotFound) {
×
647
                return fmt.Errorf("unable to prune graph nodes: %w", err)
×
648
        }
×
649

650
        return nil
×
651
}
652

653
// handleNetworkUpdate is responsible for processing the update message and
654
// notifies topology changes, if any.
655
//
656
// NOTE: must be run inside goroutine.
657
func (b *Builder) handleNetworkUpdate(update *routingMsg) {
3✔
658
        defer b.wg.Done()
3✔
659

3✔
660
        // Process the routing update to determine if this is either a new
3✔
661
        // update from our PoV or an update to a prior vertex/edge we
3✔
662
        // previously accepted.
3✔
663
        var err error
3✔
664
        switch msg := update.msg.(type) {
3✔
665
        case *models.LightningNode:
3✔
666
                err = b.addNode(msg, update.op...)
3✔
667

668
        case *models.ChannelEdgeInfo:
3✔
669
                err = b.addEdge(msg, update.op...)
3✔
670

671
        case *models.ChannelEdgePolicy:
3✔
672
                err = b.updateEdge(msg, update.op...)
3✔
673

674
        default:
×
675
                err = errors.Errorf("wrong routing update message type")
×
676
        }
677
        update.err <- err
3✔
678

3✔
679
        // If the error is not nil here, there's no need to send topology
3✔
680
        // change.
3✔
681
        if err != nil {
6✔
682
                // Log as a debug message if this is not an error we need to be
3✔
683
                // concerned about.
3✔
684
                if IsError(err, ErrIgnored, ErrOutdated) {
6✔
685
                        log.Debugf("process network updates got: %v", err)
3✔
686
                } else {
3✔
687
                        log.Errorf("process network updates got: %v", err)
×
688
                }
×
689

690
                return
3✔
691
        }
692

693
        // Otherwise, we'll send off a new notification for the newly accepted
694
        // update, if any.
695
        topChange := &TopologyChange{}
3✔
696
        err = addToTopologyChange(b.cfg.Graph, topChange, update.msg)
3✔
697
        if err != nil {
3✔
698
                log.Errorf("unable to update topology change notification: %v",
×
699
                        err)
×
700
                return
×
701
        }
×
702

703
        if !topChange.isEmpty() {
6✔
704
                b.notifyTopologyChange(topChange)
3✔
705
        }
3✔
706
}
707

708
// networkHandler is the primary goroutine for the Builder. The roles of
709
// this goroutine include answering queries related to the state of the
710
// network, pruning the graph on new block notification and registering new
711
// topology clients.
712
//
713
// NOTE: This MUST be run as a goroutine.
714
func (b *Builder) networkHandler() {
3✔
715
        defer b.wg.Done()
3✔
716

3✔
717
        graphPruneTicker := time.NewTicker(b.cfg.GraphPruneInterval)
3✔
718
        defer graphPruneTicker.Stop()
3✔
719

3✔
720
        defer b.statTicker.Stop()
3✔
721

3✔
722
        b.stats.Reset()
3✔
723

3✔
724
        for {
6✔
725
                // If there are stats, resume the statTicker.
3✔
726
                if !b.stats.Empty() {
6✔
727
                        b.statTicker.Resume()
3✔
728
                }
3✔
729

730
                select {
3✔
731
                case chainUpdate, ok := <-b.staleBlocks:
2✔
732
                        // If the channel has been closed, then this indicates
2✔
733
                        // the daemon is shutting down, so we exit ourselves.
2✔
734
                        if !ok {
2✔
735
                                return
×
736
                        }
×
737

738
                        // Since this block is stale, we update our best height
739
                        // to the previous block.
740
                        blockHeight := chainUpdate.Height
2✔
741
                        b.bestHeight.Store(blockHeight - 1)
2✔
742

2✔
743
                        // Update the channel graph to reflect that this block
2✔
744
                        // was disconnected.
2✔
745
                        _, err := b.cfg.Graph.DisconnectBlockAtHeight(
2✔
746
                                blockHeight,
2✔
747
                        )
2✔
748
                        if err != nil {
2✔
749
                                log.Errorf("unable to prune graph with stale "+
×
750
                                        "block: %v", err)
×
751
                                continue
×
752
                        }
753

754
                        // TODO(halseth): notify client about the reorg?
755

756
                // A new block has arrived, so we can prune the channel graph
757
                // of any channels which were closed in the block.
758
                case chainUpdate, ok := <-b.newBlocks:
3✔
759
                        // If the channel has been closed, then this indicates
3✔
760
                        // the daemon is shutting down, so we exit ourselves.
3✔
761
                        if !ok {
3✔
762
                                return
×
763
                        }
×
764

765
                        // We'll ensure that any new blocks received attach
766
                        // directly to the end of our main chain. If not, then
767
                        // we've somehow missed some blocks. Here we'll catch
768
                        // up the chain with the latest blocks.
769
                        currentHeight := b.bestHeight.Load()
3✔
770
                        switch {
3✔
771
                        case chainUpdate.Height == currentHeight+1:
3✔
772
                                err := b.updateGraphWithClosedChannels(
3✔
773
                                        chainUpdate,
3✔
774
                                )
3✔
775
                                if err != nil {
3✔
776
                                        log.Errorf("unable to prune graph "+
×
777
                                                "with closed channels: %v", err)
×
778
                                }
×
779

780
                        case chainUpdate.Height > currentHeight+1:
×
781
                                log.Errorf("out of order block: expecting "+
×
782
                                        "height=%v, got height=%v",
×
783
                                        currentHeight+1, chainUpdate.Height)
×
784

×
785
                                err := b.getMissingBlocks(
×
786
                                        currentHeight, chainUpdate,
×
787
                                )
×
788
                                if err != nil {
×
789
                                        log.Errorf("unable to retrieve missing"+
×
790
                                                "blocks: %v", err)
×
791
                                }
×
792

UNCOV
793
                        case chainUpdate.Height < currentHeight+1:
×
UNCOV
794
                                log.Errorf("out of order block: expecting "+
×
UNCOV
795
                                        "height=%v, got height=%v",
×
UNCOV
796
                                        currentHeight+1, chainUpdate.Height)
×
UNCOV
797

×
UNCOV
798
                                log.Infof("Skipping channel pruning since "+
×
UNCOV
799
                                        "received block height %v was already"+
×
UNCOV
800
                                        " processed.", chainUpdate.Height)
×
801
                        }
802

803
                // A new notification client update has arrived. We're either
804
                // gaining a new client, or cancelling notifications for an
805
                // existing client.
806
                case ntfnUpdate := <-b.ntfnClientUpdates:
3✔
807
                        clientID := ntfnUpdate.clientID
3✔
808

3✔
809
                        if ntfnUpdate.cancel {
6✔
810
                                client, ok := b.topologyClients.LoadAndDelete(
3✔
811
                                        clientID,
3✔
812
                                )
3✔
813
                                if ok {
6✔
814
                                        close(client.exit)
3✔
815
                                        client.wg.Wait()
3✔
816

3✔
817
                                        close(client.ntfnChan)
3✔
818
                                }
3✔
819

820
                                continue
3✔
821
                        }
822

823
                        b.topologyClients.Store(clientID, &topologyClient{
3✔
824
                                ntfnChan: ntfnUpdate.ntfnChan,
3✔
825
                                exit:     make(chan struct{}),
3✔
826
                        })
3✔
827

828
                // The graph prune ticker has ticked, so we'll examine the
829
                // state of the known graph to filter out any zombie channels
830
                // for pruning.
831
                case <-graphPruneTicker.C:
×
832
                        if err := b.pruneZombieChans(); err != nil {
×
833
                                log.Errorf("Unable to prune zombies: %v", err)
×
834
                        }
×
835

836
                // Log any stats if we've processed a non-empty number of
837
                // channels, updates, or nodes. We'll only pause the ticker if
838
                // the last window contained no updates to avoid resuming and
839
                // pausing while consecutive windows contain new info.
840
                case <-b.statTicker.Ticks():
2✔
841
                        if !b.stats.Empty() {
4✔
842
                                log.Infof(b.stats.String())
2✔
843
                        } else {
2✔
844
                                b.statTicker.Pause()
×
845
                        }
×
846
                        b.stats.Reset()
2✔
847

848
                // The router has been signalled to exit, to we exit our main
849
                // loop so the wait group can be decremented.
850
                case <-b.quit:
3✔
851
                        return
3✔
852
                }
853
        }
854
}
855

856
// getMissingBlocks walks through all missing blocks and updates the graph
857
// closed channels accordingly.
858
func (b *Builder) getMissingBlocks(currentHeight uint32,
859
        chainUpdate *chainview.FilteredBlock) error {
×
860

×
861
        outdatedHash, err := b.cfg.Chain.GetBlockHash(int64(currentHeight))
×
862
        if err != nil {
×
863
                return err
×
864
        }
×
865

866
        outdatedBlock := &chainntnfs.BlockEpoch{
×
867
                Height: int32(currentHeight),
×
868
                Hash:   outdatedHash,
×
869
        }
×
870

×
871
        epochClient, err := b.cfg.Notifier.RegisterBlockEpochNtfn(
×
872
                outdatedBlock,
×
873
        )
×
874
        if err != nil {
×
875
                return err
×
876
        }
×
877
        defer epochClient.Cancel()
×
878

×
879
        blockDifference := int(chainUpdate.Height - currentHeight)
×
880

×
881
        // We'll walk through all the outdated blocks and make sure we're able
×
882
        // to update the graph with any closed channels from them.
×
883
        for i := 0; i < blockDifference; i++ {
×
884
                var (
×
885
                        missingBlock *chainntnfs.BlockEpoch
×
886
                        ok           bool
×
887
                )
×
888

×
889
                select {
×
890
                case missingBlock, ok = <-epochClient.Epochs:
×
891
                        if !ok {
×
892
                                return nil
×
893
                        }
×
894

895
                case <-b.quit:
×
896
                        return nil
×
897
                }
898

899
                filteredBlock, err := b.cfg.ChainView.FilterBlock(
×
900
                        missingBlock.Hash,
×
901
                )
×
902
                if err != nil {
×
903
                        return err
×
904
                }
×
905

906
                err = b.updateGraphWithClosedChannels(
×
907
                        filteredBlock,
×
908
                )
×
909
                if err != nil {
×
910
                        return err
×
911
                }
×
912
        }
913

914
        return nil
×
915
}
916

917
// updateGraphWithClosedChannels prunes the channel graph of closed channels
918
// that are no longer needed.
919
func (b *Builder) updateGraphWithClosedChannels(
920
        chainUpdate *chainview.FilteredBlock) error {
3✔
921

3✔
922
        // Once a new block arrives, we update our running track of the height
3✔
923
        // of the chain tip.
3✔
924
        blockHeight := chainUpdate.Height
3✔
925

3✔
926
        b.bestHeight.Store(blockHeight)
3✔
927
        log.Infof("Pruning channel graph using block %v (height=%v)",
3✔
928
                chainUpdate.Hash, blockHeight)
3✔
929

3✔
930
        // We're only interested in all prior outputs that have been spent in
3✔
931
        // the block, so collate all the referenced previous outpoints within
3✔
932
        // each tx and input.
3✔
933
        var spentOutputs []*wire.OutPoint
3✔
934
        for _, tx := range chainUpdate.Transactions {
6✔
935
                for _, txIn := range tx.TxIn {
6✔
936
                        spentOutputs = append(spentOutputs,
3✔
937
                                &txIn.PreviousOutPoint)
3✔
938
                }
3✔
939
        }
940

941
        // With the spent outputs gathered, attempt to prune the channel graph,
942
        // also passing in the hash+height of the block being pruned so the
943
        // prune tip can be updated.
944
        chansClosed, err := b.cfg.Graph.PruneGraph(spentOutputs,
3✔
945
                &chainUpdate.Hash, chainUpdate.Height)
3✔
946
        if err != nil {
3✔
947
                log.Errorf("unable to prune routing table: %v", err)
×
948
                return err
×
949
        }
×
950

951
        log.Infof("Block %v (height=%v) closed %v channels", chainUpdate.Hash,
3✔
952
                blockHeight, len(chansClosed))
3✔
953

3✔
954
        if len(chansClosed) == 0 {
6✔
955
                return err
3✔
956
        }
3✔
957

958
        // Notify all currently registered clients of the newly closed channels.
959
        closeSummaries := createCloseSummaries(blockHeight, chansClosed...)
3✔
960
        b.notifyTopologyChange(&TopologyChange{
3✔
961
                ClosedChannels: closeSummaries,
3✔
962
        })
3✔
963

3✔
964
        return nil
3✔
965
}
966

967
// assertNodeAnnFreshness returns a non-nil error if we have an announcement in
968
// the database for the passed node with a timestamp newer than the passed
969
// timestamp. ErrIgnored will be returned if we already have the node, and
970
// ErrOutdated will be returned if we have a timestamp that's after the new
971
// timestamp.
972
func (b *Builder) assertNodeAnnFreshness(node route.Vertex,
973
        msgTimestamp time.Time) error {
3✔
974

3✔
975
        // If we are not already aware of this node, it means that we don't
3✔
976
        // know about any channel using this node. To avoid a DoS attack by
3✔
977
        // node announcements, we will ignore such nodes. If we do know about
3✔
978
        // this node, check that this update brings info newer than what we
3✔
979
        // already have.
3✔
980
        lastUpdate, exists, err := b.cfg.Graph.HasLightningNode(node)
3✔
981
        if err != nil {
3✔
982
                return errors.Errorf("unable to query for the "+
×
983
                        "existence of node: %v", err)
×
984
        }
×
985
        if !exists {
6✔
986
                return NewErrf(ErrIgnored, "Ignoring node announcement"+
3✔
987
                        " for node not found in channel graph (%x)",
3✔
988
                        node[:])
3✔
989
        }
3✔
990

991
        // If we've reached this point then we're aware of the vertex being
992
        // advertised. So we now check if the new message has a new time stamp,
993
        // if not then we won't accept the new data as it would override newer
994
        // data.
995
        if !lastUpdate.Before(msgTimestamp) {
6✔
996
                return NewErrf(ErrOutdated, "Ignoring outdated "+
3✔
997
                        "announcement for %x", node[:])
3✔
998
        }
3✔
999

1000
        return nil
3✔
1001
}
1002

1003
// MarkZombieEdge adds a channel that failed complete validation into the zombie
1004
// index so we can avoid having to re-validate it in the future.
1005
func (b *Builder) MarkZombieEdge(chanID uint64) error {
×
1006
        // If the edge fails validation we'll mark the edge itself as a zombie
×
1007
        // so we don't continue to request it. We use the "zero key" for both
×
1008
        // node pubkeys so this edge can't be resurrected.
×
1009
        var zeroKey [33]byte
×
1010
        err := b.cfg.Graph.MarkEdgeZombie(chanID, zeroKey, zeroKey)
×
1011
        if err != nil {
×
1012
                return fmt.Errorf("unable to mark spent chan(id=%v) as a "+
×
1013
                        "zombie: %w", chanID, err)
×
1014
        }
×
1015

1016
        return nil
×
1017
}
1018

1019
// routingMsg couples a routing related routing topology update to the
1020
// error channel.
1021
type routingMsg struct {
1022
        msg interface{}
1023
        op  []batch.SchedulerOption
1024
        err chan error
1025
}
1026

1027
// ApplyChannelUpdate validates a channel update and if valid, applies it to the
1028
// database. It returns a bool indicating whether the updates were successful.
1029
func (b *Builder) ApplyChannelUpdate(msg *lnwire.ChannelUpdate1) bool {
3✔
1030
        ch, _, _, err := b.GetChannelByID(msg.ShortChannelID)
3✔
1031
        if err != nil {
6✔
1032
                log.Errorf("Unable to retrieve channel by id: %v", err)
3✔
1033
                return false
3✔
1034
        }
3✔
1035

1036
        var pubKey *btcec.PublicKey
3✔
1037

3✔
1038
        switch msg.ChannelFlags & lnwire.ChanUpdateDirection {
3✔
1039
        case 0:
3✔
1040
                pubKey, _ = ch.NodeKey1()
3✔
1041

1042
        case 1:
3✔
1043
                pubKey, _ = ch.NodeKey2()
3✔
1044
        }
1045

1046
        // Exit early if the pubkey cannot be decided.
1047
        if pubKey == nil {
3✔
1048
                log.Errorf("Unable to decide pubkey with ChannelFlags=%v",
×
1049
                        msg.ChannelFlags)
×
1050
                return false
×
1051
        }
×
1052

1053
        err = netann.ValidateChannelUpdateAnn(pubKey, ch.Capacity, msg)
3✔
1054
        if err != nil {
3✔
1055
                log.Errorf("Unable to validate channel update: %v", err)
×
1056
                return false
×
1057
        }
×
1058

1059
        err = b.UpdateEdge(&models.ChannelEdgePolicy{
3✔
1060
                SigBytes:                  msg.Signature.ToSignatureBytes(),
3✔
1061
                ChannelID:                 msg.ShortChannelID.ToUint64(),
3✔
1062
                LastUpdate:                time.Unix(int64(msg.Timestamp), 0),
3✔
1063
                MessageFlags:              msg.MessageFlags,
3✔
1064
                ChannelFlags:              msg.ChannelFlags,
3✔
1065
                TimeLockDelta:             msg.TimeLockDelta,
3✔
1066
                MinHTLC:                   msg.HtlcMinimumMsat,
3✔
1067
                MaxHTLC:                   msg.HtlcMaximumMsat,
3✔
1068
                FeeBaseMSat:               lnwire.MilliSatoshi(msg.BaseFee),
3✔
1069
                FeeProportionalMillionths: lnwire.MilliSatoshi(msg.FeeRate),
3✔
1070
                ExtraOpaqueData:           msg.ExtraOpaqueData,
3✔
1071
        })
3✔
1072
        if err != nil && !IsError(err, ErrIgnored, ErrOutdated) {
3✔
1073
                log.Errorf("Unable to apply channel update: %v", err)
×
1074
                return false
×
1075
        }
×
1076

1077
        return true
3✔
1078
}
1079

1080
// AddNode is used to add information about a node to the router database. If
1081
// the node with this pubkey is not present in an existing channel, it will
1082
// be ignored.
1083
//
1084
// NOTE: This method is part of the ChannelGraphSource interface.
1085
func (b *Builder) AddNode(node *models.LightningNode,
1086
        op ...batch.SchedulerOption) error {
3✔
1087

3✔
1088
        rMsg := &routingMsg{
3✔
1089
                msg: node,
3✔
1090
                op:  op,
3✔
1091
                err: make(chan error, 1),
3✔
1092
        }
3✔
1093

3✔
1094
        b.wg.Add(1)
3✔
1095
        go b.handleNetworkUpdate(rMsg)
3✔
1096

3✔
1097
        select {
3✔
1098
        case err := <-rMsg.err:
3✔
1099
                return err
3✔
1100
        case <-b.quit:
×
1101
                return ErrGraphBuilderShuttingDown
×
1102
        }
1103
}
1104

1105
// addNode does some basic checks on the given LightningNode against what we
1106
// currently have persisted in the graph, and then adds it to the graph. If we
1107
// already know about the node, then we only update our DB if the new update
1108
// has a newer timestamp than the last one we received.
1109
func (b *Builder) addNode(node *models.LightningNode,
1110
        op ...batch.SchedulerOption) error {
3✔
1111

3✔
1112
        // Before we add the node to the database, we'll check to see if the
3✔
1113
        // announcement is "fresh" or not. If it isn't, then we'll return an
3✔
1114
        // error.
3✔
1115
        err := b.assertNodeAnnFreshness(node.PubKeyBytes, node.LastUpdate)
3✔
1116
        if err != nil {
6✔
1117
                return err
3✔
1118
        }
3✔
1119

1120
        if err := b.cfg.Graph.AddLightningNode(node, op...); err != nil {
3✔
1121
                return errors.Errorf("unable to add node %x to the "+
×
1122
                        "graph: %v", node.PubKeyBytes, err)
×
1123
        }
×
1124

1125
        log.Tracef("Updated vertex data for node=%x", node.PubKeyBytes)
3✔
1126
        b.stats.incNumNodeUpdates()
3✔
1127

3✔
1128
        return nil
3✔
1129
}
1130

1131
// AddEdge is used to add edge/channel to the topology of the router, after all
1132
// information about channel will be gathered this edge/channel might be used
1133
// in construction of payment path.
1134
//
1135
// NOTE: This method is part of the ChannelGraphSource interface.
1136
func (b *Builder) AddEdge(edge *models.ChannelEdgeInfo,
1137
        op ...batch.SchedulerOption) error {
3✔
1138

3✔
1139
        rMsg := &routingMsg{
3✔
1140
                msg: edge,
3✔
1141
                op:  op,
3✔
1142
                err: make(chan error, 1),
3✔
1143
        }
3✔
1144

3✔
1145
        b.wg.Add(1)
3✔
1146
        go b.handleNetworkUpdate(rMsg)
3✔
1147

3✔
1148
        select {
3✔
1149
        case err := <-rMsg.err:
3✔
1150
                return err
3✔
1151
        case <-b.quit:
×
1152
                return ErrGraphBuilderShuttingDown
×
1153
        }
1154
}
1155

1156
// addEdge does some validation on the new channel edge against what we
1157
// currently have persisted in the graph, and then adds it to the graph. The
1158
// Chain View is updated with the new edge if it is successfully added to the
1159
// graph. We only persist the channel if we currently dont have it at all in
1160
// our graph.
1161
//
1162
// TODO(elle): this currently also does funding-transaction validation. But this
1163
// should be moved to the gossiper instead.
1164
func (b *Builder) addEdge(edge *models.ChannelEdgeInfo,
1165
        op ...batch.SchedulerOption) error {
3✔
1166

3✔
1167
        log.Debugf("Received ChannelEdgeInfo for channel %v", edge.ChannelID)
3✔
1168

3✔
1169
        // Prior to processing the announcement we first check if we
3✔
1170
        // already know of this channel, if so, then we can exit early.
3✔
1171
        _, _, exists, isZombie, err := b.cfg.Graph.HasChannelEdge(
3✔
1172
                edge.ChannelID,
3✔
1173
        )
3✔
1174
        if err != nil && !errors.Is(err, graphdb.ErrGraphNoEdgesFound) {
3✔
1175
                return errors.Errorf("unable to check for edge existence: %v",
×
1176
                        err)
×
1177
        }
×
1178
        if isZombie {
3✔
1179
                return NewErrf(ErrIgnored, "ignoring msg for zombie chan_id=%v",
×
1180
                        edge.ChannelID)
×
1181
        }
×
1182
        if exists {
6✔
1183
                return NewErrf(ErrIgnored, "ignoring msg for known chan_id=%v",
3✔
1184
                        edge.ChannelID)
3✔
1185
        }
3✔
1186

1187
        if err := b.cfg.Graph.AddChannelEdge(edge, op...); err != nil {
3✔
1188
                return fmt.Errorf("unable to add edge: %w", err)
×
1189
        }
×
1190

1191
        b.stats.incNumEdgesDiscovered()
3✔
1192

3✔
1193
        // If AssumeChannelValid is present, of if the SCID is an alias, then
3✔
1194
        // the gossiper would not have done the expensive work of fetching
3✔
1195
        // a funding transaction and validating it. So we won't have the channel
3✔
1196
        // capacity nor the funding script. So we just log and return here.
3✔
1197
        scid := lnwire.NewShortChanIDFromInt(edge.ChannelID)
3✔
1198
        if b.cfg.AssumeChannelValid || b.cfg.IsAlias(scid) {
6✔
1199
                log.Tracef("New channel discovered! Link connects %x and %x "+
3✔
1200
                        "with ChannelID(%v)", edge.NodeKey1Bytes,
3✔
1201
                        edge.NodeKey2Bytes, edge.ChannelID)
3✔
1202

3✔
1203
                return nil
3✔
1204
        }
3✔
1205

1206
        log.Debugf("New channel discovered! Link connects %x and %x with "+
3✔
1207
                "ChannelPoint(%v): chan_id=%v, capacity=%v", edge.NodeKey1Bytes,
3✔
1208
                edge.NodeKey2Bytes, edge.ChannelPoint, edge.ChannelID,
3✔
1209
                edge.Capacity)
3✔
1210

3✔
1211
        // Otherwise, then we expect the funding script to be present on the
3✔
1212
        // edge since it would have been fetched when the gossiper validated the
3✔
1213
        // announcement.
3✔
1214
        fundingPkScript, err := edge.FundingScript.UnwrapOrErr(fmt.Errorf(
3✔
1215
                "expected the funding transaction script to be set",
3✔
1216
        ))
3✔
1217
        if err != nil {
3✔
1218
                return err
×
1219
        }
×
1220

1221
        // As a new edge has been added to the channel graph, we'll update the
1222
        // current UTXO filter within our active FilteredChainView so we are
1223
        // notified if/when this channel is closed.
1224
        filterUpdate := []graphdb.EdgePoint{
3✔
1225
                {
3✔
1226
                        FundingPkScript: fundingPkScript,
3✔
1227
                        OutPoint:        edge.ChannelPoint,
3✔
1228
                },
3✔
1229
        }
3✔
1230

3✔
1231
        err = b.cfg.ChainView.UpdateFilter(filterUpdate, b.bestHeight.Load())
3✔
1232
        if err != nil {
3✔
1233
                return errors.Errorf("unable to update chain "+
×
1234
                        "view: %v", err)
×
1235
        }
×
1236

1237
        return nil
3✔
1238
}
1239

1240
// UpdateEdge is used to update edge information, without this message edge
1241
// considered as not fully constructed.
1242
//
1243
// NOTE: This method is part of the ChannelGraphSource interface.
1244
func (b *Builder) UpdateEdge(update *models.ChannelEdgePolicy,
1245
        op ...batch.SchedulerOption) error {
3✔
1246

3✔
1247
        rMsg := &routingMsg{
3✔
1248
                msg: update,
3✔
1249
                op:  op,
3✔
1250
                err: make(chan error, 1),
3✔
1251
        }
3✔
1252

3✔
1253
        b.wg.Add(1)
3✔
1254
        go b.handleNetworkUpdate(rMsg)
3✔
1255

3✔
1256
        select {
3✔
1257
        case err := <-rMsg.err:
3✔
1258
                return err
3✔
1259
        case <-b.quit:
×
1260
                return ErrGraphBuilderShuttingDown
×
1261
        }
1262
}
1263

1264
// updateEdge validates the new edge policy against what we currently have
1265
// persisted in the graph, and then applies it to the graph if the update is
1266
// considered fresh enough and if we actually have a channel persisted for the
1267
// given update.
1268
func (b *Builder) updateEdge(policy *models.ChannelEdgePolicy,
1269
        op ...batch.SchedulerOption) error {
3✔
1270

3✔
1271
        log.Debugf("Received ChannelEdgePolicy for channel %v",
3✔
1272
                policy.ChannelID)
3✔
1273

3✔
1274
        // We make sure to hold the mutex for this channel ID, such that no
3✔
1275
        // other goroutine is concurrently doing database accesses for the same
3✔
1276
        // channel ID.
3✔
1277
        b.channelEdgeMtx.Lock(policy.ChannelID)
3✔
1278
        defer b.channelEdgeMtx.Unlock(policy.ChannelID)
3✔
1279

3✔
1280
        edge1Timestamp, edge2Timestamp, exists, isZombie, err :=
3✔
1281
                b.cfg.Graph.HasChannelEdge(policy.ChannelID)
3✔
1282
        if err != nil && !errors.Is(err, graphdb.ErrGraphNoEdgesFound) {
3✔
1283
                return errors.Errorf("unable to check for edge existence: %v",
×
1284
                        err)
×
1285
        }
×
1286

1287
        // If the channel is marked as a zombie in our database, and
1288
        // we consider this a stale update, then we should not apply the
1289
        // policy.
1290
        isStaleUpdate := time.Since(policy.LastUpdate) >
3✔
1291
                b.cfg.ChannelPruneExpiry
3✔
1292

3✔
1293
        if isZombie && isStaleUpdate {
3✔
1294
                return NewErrf(ErrIgnored, "ignoring stale update "+
×
1295
                        "(flags=%v|%v) for zombie chan_id=%v",
×
1296
                        policy.MessageFlags, policy.ChannelFlags,
×
1297
                        policy.ChannelID)
×
1298
        }
×
1299

1300
        // If the channel doesn't exist in our database, we cannot apply the
1301
        // updated policy.
1302
        if !exists {
3✔
1303
                return NewErrf(ErrIgnored, "ignoring update (flags=%v|%v) for "+
×
1304
                        "unknown chan_id=%v", policy.MessageFlags,
×
1305
                        policy.ChannelFlags, policy.ChannelID)
×
1306
        }
×
1307

1308
        log.Debugf("Found edge1Timestamp=%v, edge2Timestamp=%v",
3✔
1309
                edge1Timestamp, edge2Timestamp)
3✔
1310

3✔
1311
        // As edges are directional edge node has a unique policy for the
3✔
1312
        // direction of the edge they control. Therefore, we first check if we
3✔
1313
        // already have the most up-to-date information for that edge. If this
3✔
1314
        // message has a timestamp not strictly newer than what we already know
3✔
1315
        // of we can exit early.
3✔
1316
        switch policy.ChannelFlags & lnwire.ChanUpdateDirection {
3✔
1317
        // A flag set of 0 indicates this is an announcement for the "first"
1318
        // node in the channel.
1319
        case 0:
3✔
1320
                // Ignore outdated message.
3✔
1321
                if !edge1Timestamp.Before(policy.LastUpdate) {
6✔
1322
                        return NewErrf(ErrOutdated, "Ignoring "+
3✔
1323
                                "outdated update (flags=%v|%v) for "+
3✔
1324
                                "known chan_id=%v", policy.MessageFlags,
3✔
1325
                                policy.ChannelFlags, policy.ChannelID)
3✔
1326
                }
3✔
1327

1328
        // Similarly, a flag set of 1 indicates this is an announcement
1329
        // for the "second" node in the channel.
1330
        case 1:
3✔
1331
                // Ignore outdated message.
3✔
1332
                if !edge2Timestamp.Before(policy.LastUpdate) {
6✔
1333
                        return NewErrf(ErrOutdated, "Ignoring "+
3✔
1334
                                "outdated update (flags=%v|%v) for "+
3✔
1335
                                "known chan_id=%v", policy.MessageFlags,
3✔
1336
                                policy.ChannelFlags, policy.ChannelID)
3✔
1337
                }
3✔
1338
        }
1339

1340
        // Now that we know this isn't a stale update, we'll apply the new edge
1341
        // policy to the proper directional edge within the channel graph.
1342
        if err = b.cfg.Graph.UpdateEdgePolicy(policy, op...); err != nil {
3✔
1343
                err := errors.Errorf("unable to add channel: %v", err)
×
1344
                log.Error(err)
×
1345
                return err
×
1346
        }
×
1347

1348
        log.Tracef("New channel update applied: %v",
3✔
1349
                lnutils.SpewLogClosure(policy))
3✔
1350
        b.stats.incNumChannelUpdates()
3✔
1351

3✔
1352
        return nil
3✔
1353
}
1354

1355
// CurrentBlockHeight returns the block height from POV of the router subsystem.
1356
//
1357
// NOTE: This method is part of the ChannelGraphSource interface.
1358
func (b *Builder) CurrentBlockHeight() (uint32, error) {
3✔
1359
        _, height, err := b.cfg.Chain.GetBestBlock()
3✔
1360
        return uint32(height), err
3✔
1361
}
3✔
1362

1363
// SyncedHeight returns the block height to which the router subsystem currently
1364
// is synced to. This can differ from the above chain height if the goroutine
1365
// responsible for processing the blocks isn't yet up to speed.
1366
func (b *Builder) SyncedHeight() uint32 {
3✔
1367
        return b.bestHeight.Load()
3✔
1368
}
3✔
1369

1370
// GetChannelByID return the channel by the channel id.
1371
//
1372
// NOTE: This method is part of the ChannelGraphSource interface.
1373
func (b *Builder) GetChannelByID(chanID lnwire.ShortChannelID) (
1374
        *models.ChannelEdgeInfo,
1375
        *models.ChannelEdgePolicy,
1376
        *models.ChannelEdgePolicy, error) {
3✔
1377

3✔
1378
        return b.cfg.Graph.FetchChannelEdgesByID(chanID.ToUint64())
3✔
1379
}
3✔
1380

1381
// FetchLightningNode attempts to look up a target node by its identity public
1382
// key. graphdb.ErrGraphNodeNotFound is returned if the node doesn't exist
1383
// within the graph.
1384
//
1385
// NOTE: This method is part of the ChannelGraphSource interface.
1386
func (b *Builder) FetchLightningNode(
1387
        node route.Vertex) (*models.LightningNode, error) {
3✔
1388

3✔
1389
        return b.cfg.Graph.FetchLightningNode(node)
3✔
1390
}
3✔
1391

1392
// ForAllOutgoingChannels is used to iterate over all outgoing channels owned by
1393
// the router.
1394
//
1395
// NOTE: This method is part of the ChannelGraphSource interface.
1396
func (b *Builder) ForAllOutgoingChannels(cb func(*models.ChannelEdgeInfo,
1397
        *models.ChannelEdgePolicy) error) error {
3✔
1398

3✔
1399
        return b.cfg.Graph.ForEachNodeChannel(b.cfg.SelfNode,
3✔
1400
                func(_ kvdb.RTx, c *models.ChannelEdgeInfo,
3✔
1401
                        e *models.ChannelEdgePolicy,
3✔
1402
                        _ *models.ChannelEdgePolicy) error {
6✔
1403

3✔
1404
                        if e == nil {
3✔
1405
                                return fmt.Errorf("channel from self node " +
×
1406
                                        "has no policy")
×
1407
                        }
×
1408

1409
                        return cb(c, e)
3✔
1410
                },
1411
        )
1412
}
1413

1414
// AddProof updates the channel edge info with proof which is needed to
1415
// properly announce the edge to the rest of the network.
1416
//
1417
// NOTE: This method is part of the ChannelGraphSource interface.
1418
func (b *Builder) AddProof(chanID lnwire.ShortChannelID,
1419
        proof *models.ChannelAuthProof) error {
3✔
1420

3✔
1421
        info, _, _, err := b.cfg.Graph.FetchChannelEdgesByID(chanID.ToUint64())
3✔
1422
        if err != nil {
3✔
1423
                return err
×
1424
        }
×
1425

1426
        info.AuthProof = proof
3✔
1427

3✔
1428
        return b.cfg.Graph.UpdateChannelEdge(info)
3✔
1429
}
1430

1431
// IsStaleNode returns true if the graph source has a node announcement for the
1432
// target node with a more recent timestamp.
1433
//
1434
// NOTE: This method is part of the ChannelGraphSource interface.
1435
func (b *Builder) IsStaleNode(node route.Vertex,
1436
        timestamp time.Time) bool {
3✔
1437

3✔
1438
        // If our attempt to assert that the node announcement is fresh fails,
3✔
1439
        // then we know that this is actually a stale announcement.
3✔
1440
        err := b.assertNodeAnnFreshness(node, timestamp)
3✔
1441
        if err != nil {
6✔
1442
                log.Debugf("Checking stale node %x got %v", node, err)
3✔
1443
                return true
3✔
1444
        }
3✔
1445

1446
        return false
3✔
1447
}
1448

1449
// IsPublicNode determines whether the given vertex is seen as a public node in
1450
// the graph from the graph's source node's point of view.
1451
//
1452
// NOTE: This method is part of the ChannelGraphSource interface.
1453
func (b *Builder) IsPublicNode(node route.Vertex) (bool, error) {
3✔
1454
        return b.cfg.Graph.IsPublicNode(node)
3✔
1455
}
3✔
1456

1457
// IsKnownEdge returns true if the graph source already knows of the passed
1458
// channel ID either as a live or zombie edge.
1459
//
1460
// NOTE: This method is part of the ChannelGraphSource interface.
1461
func (b *Builder) IsKnownEdge(chanID lnwire.ShortChannelID) bool {
3✔
1462
        _, _, exists, isZombie, _ := b.cfg.Graph.HasChannelEdge(
3✔
1463
                chanID.ToUint64(),
3✔
1464
        )
3✔
1465

3✔
1466
        return exists || isZombie
3✔
1467
}
3✔
1468

1469
// IsZombieEdge returns true if the graph source has marked the given channel ID
1470
// as a zombie edge.
1471
//
1472
// NOTE: This method is part of the ChannelGraphSource interface.
1473
func (b *Builder) IsZombieEdge(chanID lnwire.ShortChannelID) (bool, error) {
×
1474
        _, _, _, isZombie, err := b.cfg.Graph.HasChannelEdge(chanID.ToUint64())
×
1475

×
1476
        return isZombie, err
×
1477
}
×
1478

1479
// IsStaleEdgePolicy returns true if the graph source has a channel edge for
1480
// the passed channel ID (and flags) that have a more recent timestamp.
1481
//
1482
// NOTE: This method is part of the ChannelGraphSource interface.
1483
func (b *Builder) IsStaleEdgePolicy(chanID lnwire.ShortChannelID,
1484
        timestamp time.Time, flags lnwire.ChanUpdateChanFlags) bool {
3✔
1485

3✔
1486
        edge1Timestamp, edge2Timestamp, exists, isZombie, err :=
3✔
1487
                b.cfg.Graph.HasChannelEdge(chanID.ToUint64())
3✔
1488
        if err != nil {
3✔
1489
                log.Debugf("Check stale edge policy got error: %v", err)
×
1490
                return false
×
1491
        }
×
1492

1493
        // If we know of the edge as a zombie, then we'll make some additional
1494
        // checks to determine if the new policy is fresh.
1495
        if isZombie {
3✔
1496
                // When running with AssumeChannelValid, we also prune channels
×
1497
                // if both of their edges are disabled. We'll mark the new
×
1498
                // policy as stale if it remains disabled.
×
1499
                if b.cfg.AssumeChannelValid {
×
1500
                        isDisabled := flags&lnwire.ChanUpdateDisabled ==
×
1501
                                lnwire.ChanUpdateDisabled
×
1502
                        if isDisabled {
×
1503
                                return true
×
1504
                        }
×
1505
                }
1506

1507
                // Otherwise, we'll fall back to our usual ChannelPruneExpiry.
1508
                return time.Since(timestamp) > b.cfg.ChannelPruneExpiry
×
1509
        }
1510

1511
        // If we don't know of the edge, then it means it's fresh (thus not
1512
        // stale).
1513
        if !exists {
6✔
1514
                return false
3✔
1515
        }
3✔
1516

1517
        // As edges are directional edge node has a unique policy for the
1518
        // direction of the edge they control. Therefore, we first check if we
1519
        // already have the most up-to-date information for that edge. If so,
1520
        // then we can exit early.
1521
        switch {
3✔
1522
        // A flag set of 0 indicates this is an announcement for the "first"
1523
        // node in the channel.
1524
        case flags&lnwire.ChanUpdateDirection == 0:
3✔
1525
                return !edge1Timestamp.Before(timestamp)
3✔
1526

1527
        // Similarly, a flag set of 1 indicates this is an announcement for the
1528
        // "second" node in the channel.
1529
        case flags&lnwire.ChanUpdateDirection == 1:
3✔
1530
                return !edge2Timestamp.Before(timestamp)
3✔
1531
        }
1532

1533
        return false
×
1534
}
1535

1536
// MarkEdgeLive clears an edge from our zombie index, deeming it as live.
1537
//
1538
// NOTE: This method is part of the ChannelGraphSource interface.
1539
func (b *Builder) MarkEdgeLive(chanID lnwire.ShortChannelID) error {
×
1540
        return b.cfg.Graph.MarkEdgeLive(chanID.ToUint64())
×
1541
}
×
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