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

lightningnetwork / lnd / 19080946931

04 Nov 2025 07:47PM UTC coverage: 66.65% (+0.009%) from 66.641%
19080946931

Pull #10340

github

web-flow
Merge c364eae9c into 4131a8e58
Pull Request #10340: graph: fix zombie chan pruning

9 of 25 new or added lines in 1 file covered. (36.0%)

291 existing lines in 18 files now uncovered.

137293 of 205990 relevant lines covered (66.65%)

21217.79 hits per line

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

77.15
/graph/builder.go
1
package graph
2

3
import (
4
        "context"
5
        "errors"
6
        "fmt"
7
        "sync"
8
        "sync/atomic"
9
        "time"
10

11
        "github.com/btcsuite/btcd/btcec/v2"
12
        "github.com/btcsuite/btcd/wire"
13
        "github.com/lightningnetwork/lnd/batch"
14
        "github.com/lightningnetwork/lnd/chainntnfs"
15
        graphdb "github.com/lightningnetwork/lnd/graph/db"
16
        "github.com/lightningnetwork/lnd/graph/db/models"
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
        bestHeight atomic.Uint32
113

114
        cfg *Config
115

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

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

125
        // channelEdgeMtx is a mutex we use to make sure we process only one
126
        // ChannelEdgePolicy at a time for a given channelID, to ensure
127
        // consistency between the various database accesses.
128
        channelEdgeMtx *multimutex.Mutex[uint64]
129

130
        // statTicker is a resumable ticker that logs the router's progress as
131
        // it discovers channels or receives updates.
132
        statTicker ticker.Ticker
133

134
        // stats tracks newly processed channels, updates, and node
135
        // announcements over a window of defaultStatInterval.
136
        stats *builderStats
137

138
        quit chan struct{}
139
        wg   sync.WaitGroup
140
}
141

142
// A compile time check to ensure Builder implements the
143
// ChannelGraphSource interface.
144
var _ ChannelGraphSource = (*Builder)(nil)
145

146
// NewBuilder constructs a new Builder.
147
func NewBuilder(cfg *Config) (*Builder, error) {
23✔
148
        return &Builder{
23✔
149
                cfg:            cfg,
23✔
150
                channelEdgeMtx: multimutex.NewMutex[uint64](),
23✔
151
                statTicker:     ticker.New(defaultStatInterval),
23✔
152
                stats:          new(builderStats),
23✔
153
                quit:           make(chan struct{}),
23✔
154
        }, nil
23✔
155
}
23✔
156

157
// Start launches all the goroutines the Builder requires to carry out its
158
// duties. If the builder has already been started, then this method is a noop.
159
func (b *Builder) Start() error {
23✔
160
        if !b.started.CompareAndSwap(false, true) {
23✔
161
                return nil
×
162
        }
×
163

164
        log.Info("Builder starting")
23✔
165

23✔
166
        bestHash, bestHeight, err := b.cfg.Chain.GetBestBlock()
23✔
167
        if err != nil {
23✔
168
                return err
×
169
        }
×
170

171
        // If the graph has never been pruned, or hasn't fully been created yet,
172
        // then we don't treat this as an explicit error.
173
        if _, _, err := b.cfg.Graph.PruneTip(); err != nil {
44✔
174
                switch {
21✔
175
                case errors.Is(err, graphdb.ErrGraphNeverPruned):
21✔
176
                        fallthrough
21✔
177

178
                case errors.Is(err, graphdb.ErrGraphNotFound):
21✔
179
                        // If the graph has never been pruned, then we'll set
21✔
180
                        // the prune height to the current best height of the
21✔
181
                        // chain backend.
21✔
182
                        _, err = b.cfg.Graph.PruneGraph(
21✔
183
                                nil, bestHash, uint32(bestHeight),
21✔
184
                        )
21✔
185
                        if err != nil {
21✔
186
                                return err
×
187
                        }
×
188

189
                default:
×
190
                        return err
×
191
                }
192
        }
193

194
        // If AssumeChannelValid is present, then we won't rely on pruning
195
        // channels from the graph based on their spentness, but whether they
196
        // are considered zombies or not. We will start zombie pruning after a
197
        // small delay, to avoid slowing down startup of lnd.
198
        if b.cfg.AssumeChannelValid { //nolint:nestif
24✔
199
                time.AfterFunc(b.cfg.FirstTimePruneDelay, func() {
2✔
200
                        select {
1✔
201
                        case <-b.quit:
×
202
                                return
×
203
                        default:
1✔
204
                        }
205

206
                        log.Info("Initial zombie prune starting")
1✔
207
                        if err := b.pruneZombieChans(); err != nil {
1✔
208
                                log.Errorf("Unable to prune zombies: %v", err)
×
209
                        }
×
210
                })
211
        } else {
22✔
212
                // Otherwise, we'll use our filtered chain view to prune
22✔
213
                // channels as soon as they are detected as spent on-chain.
22✔
214
                if err := b.cfg.ChainView.Start(); err != nil {
22✔
215
                        return err
×
216
                }
×
217

218
                // Once the instance is active, we'll fetch the channel we'll
219
                // receive notifications over.
220
                b.newBlocks = b.cfg.ChainView.FilteredBlocks()
22✔
221
                b.staleBlocks = b.cfg.ChainView.DisconnectedBlocks()
22✔
222

22✔
223
                // Before we perform our manual block pruning, we'll construct
22✔
224
                // and apply a fresh chain filter to the active
22✔
225
                // FilteredChainView instance.  We do this before, as otherwise
22✔
226
                // we may miss on-chain events as the filter hasn't properly
22✔
227
                // been applied.
22✔
228
                channelView, err := b.cfg.Graph.ChannelView()
22✔
229
                if err != nil && !errors.Is(
22✔
230
                        err, graphdb.ErrGraphNoEdgesFound,
22✔
231
                ) {
22✔
232

×
233
                        return err
×
234
                }
×
235

236
                log.Infof("Filtering chain using %v channels active",
22✔
237
                        len(channelView))
22✔
238

22✔
239
                if len(channelView) != 0 {
32✔
240
                        err = b.cfg.ChainView.UpdateFilter(
10✔
241
                                channelView, uint32(bestHeight),
10✔
242
                        )
10✔
243
                        if err != nil {
10✔
244
                                return err
×
245
                        }
×
246
                }
247

248
                // The graph pruning might have taken a while and there could be
249
                // new blocks available.
250
                _, bestHeight, err = b.cfg.Chain.GetBestBlock()
22✔
251
                if err != nil {
22✔
252
                        return err
×
253
                }
×
254
                b.bestHeight.Store(uint32(bestHeight))
22✔
255

22✔
256
                // Before we begin normal operation of the router, we first need
22✔
257
                // to synchronize the channel graph to the latest state of the
22✔
258
                // UTXO set.
22✔
259
                if err := b.syncGraphWithChain(); err != nil {
22✔
260
                        return err
×
261
                }
×
262

263
                // Finally, before we proceed, we'll prune any unconnected nodes
264
                // from the graph in order to ensure we maintain a tight graph
265
                // of "useful" nodes.
266
                err = b.cfg.Graph.PruneGraphNodes()
22✔
267
                if err != nil &&
22✔
268
                        !errors.Is(err, graphdb.ErrGraphNodesNotFound) {
22✔
269

×
270
                        return err
×
271
                }
×
272
        }
273

274
        b.wg.Add(1)
23✔
275
        go b.networkHandler()
23✔
276

23✔
277
        log.Debug("Builder started")
23✔
278

23✔
279
        return nil
23✔
280
}
281

282
// Stop signals to the Builder that it should halt all routines. This method
283
// will *block* until all goroutines have excited. If the builder has already
284
// stopped then this method will return immediately.
285
func (b *Builder) Stop() error {
23✔
286
        if !b.stopped.CompareAndSwap(false, true) {
25✔
287
                return nil
2✔
288
        }
2✔
289

290
        log.Info("Builder shutting down...")
21✔
291

21✔
292
        // Our filtered chain view could've only been started if
21✔
293
        // AssumeChannelValid isn't present.
21✔
294
        if !b.cfg.AssumeChannelValid {
41✔
295
                if err := b.cfg.ChainView.Stop(); err != nil {
20✔
296
                        return err
×
297
                }
×
298
        }
299

300
        close(b.quit)
21✔
301
        b.wg.Wait()
21✔
302

21✔
303
        log.Debug("Builder shutdown complete")
21✔
304

21✔
305
        return nil
21✔
306
}
307

308
// syncGraphWithChain attempts to synchronize the current channel graph with
309
// the latest UTXO set state. This process involves pruning from the channel
310
// graph any channels which have been closed by spending their funding output
311
// since we've been down.
312
func (b *Builder) syncGraphWithChain() error {
22✔
313
        // First, we'll need to check to see if we're already in sync with the
22✔
314
        // latest state of the UTXO set.
22✔
315
        bestHash, bestHeight, err := b.cfg.Chain.GetBestBlock()
22✔
316
        if err != nil {
22✔
317
                return err
×
318
        }
×
319
        b.bestHeight.Store(uint32(bestHeight))
22✔
320

22✔
321
        pruneHash, pruneHeight, err := b.cfg.Graph.PruneTip()
22✔
322
        if err != nil {
22✔
323
                switch {
×
324
                // If the graph has never been pruned, or hasn't fully been
325
                // created yet, then we don't treat this as an explicit error.
326
                case errors.Is(err, graphdb.ErrGraphNeverPruned):
×
327
                case errors.Is(err, graphdb.ErrGraphNotFound):
×
328
                default:
×
329
                        return err
×
330
                }
331
        }
332

333
        log.Infof("Prune tip for Channel Graph: height=%v, hash=%v",
22✔
334
                pruneHeight, pruneHash)
22✔
335

22✔
336
        switch {
22✔
337
        // If the graph has never been pruned, then we can exit early as this
338
        // entails it's being created for the first time and hasn't seen any
339
        // block or created channels.
340
        case pruneHeight == 0 || pruneHash == nil:
3✔
341
                return nil
3✔
342

343
        // If the block hashes and heights match exactly, then we don't need to
344
        // prune the channel graph as we're already fully in sync.
345
        case bestHash.IsEqual(pruneHash) && uint32(bestHeight) == pruneHeight:
17✔
346
                return nil
17✔
347
        }
348

349
        // If the main chain blockhash at prune height is different from the
350
        // prune hash, this might indicate the database is on a stale branch.
351
        mainBlockHash, err := b.cfg.Chain.GetBlockHash(int64(pruneHeight))
5✔
352
        if err != nil {
5✔
353
                return err
×
354
        }
×
355

356
        // While we are on a stale branch of the chain, walk backwards to find
357
        // first common block.
358
        for !pruneHash.IsEqual(mainBlockHash) {
15✔
359
                log.Infof("channel graph is stale. Disconnecting block %v "+
10✔
360
                        "(hash=%v)", pruneHeight, pruneHash)
10✔
361
                // Prune the graph for every channel that was opened at height
10✔
362
                // >= pruneHeight.
10✔
363
                _, err := b.cfg.Graph.DisconnectBlockAtHeight(pruneHeight)
10✔
364
                if err != nil {
10✔
365
                        return err
×
366
                }
×
367

368
                pruneHash, pruneHeight, err = b.cfg.Graph.PruneTip()
10✔
369
                switch {
10✔
370
                // If at this point the graph has never been pruned, we can exit
371
                // as this entails we are back to the point where it hasn't seen
372
                // any block or created channels, alas there's nothing left to
373
                // prune.
374
                case errors.Is(err, graphdb.ErrGraphNeverPruned):
×
375
                        return nil
×
376

377
                case errors.Is(err, graphdb.ErrGraphNotFound):
×
378
                        return nil
×
379

380
                case err != nil:
×
381
                        return err
×
382

383
                default:
10✔
384
                }
385

386
                mainBlockHash, err = b.cfg.Chain.GetBlockHash(
10✔
387
                        int64(pruneHeight),
10✔
388
                )
10✔
389
                if err != nil {
10✔
390
                        return err
×
391
                }
×
392
        }
393

394
        log.Infof("Syncing channel graph from height=%v (hash=%v) to "+
5✔
395
                "height=%v (hash=%v)", pruneHeight, pruneHash, bestHeight,
5✔
396
                bestHash)
5✔
397

5✔
398
        // If we're not yet caught up, then we'll walk forward in the chain
5✔
399
        // pruning the channel graph with each new block that hasn't yet been
5✔
400
        // consumed by the channel graph.
5✔
401
        var spentOutputs []*wire.OutPoint
5✔
402
        for nextHeight := pruneHeight + 1; nextHeight <= uint32(bestHeight); nextHeight++ { //nolint:ll
32✔
403
                // Break out of the rescan early if a shutdown has been
27✔
404
                // requested, otherwise long rescans will block the daemon from
27✔
405
                // shutting down promptly.
27✔
406
                select {
27✔
407
                case <-b.quit:
×
408
                        return ErrGraphBuilderShuttingDown
×
409
                default:
27✔
410
                }
411

412
                // Using the next height, request a manual block pruning from
413
                // the chainview for the particular block hash.
414
                log.Infof("Filtering block for closed channels, at height: %v",
27✔
415
                        int64(nextHeight))
27✔
416
                nextHash, err := b.cfg.Chain.GetBlockHash(int64(nextHeight))
27✔
417
                if err != nil {
27✔
418
                        return err
×
419
                }
×
420
                log.Tracef("Running block filter on block with hash: %v",
27✔
421
                        nextHash)
27✔
422
                filterBlock, err := b.cfg.ChainView.FilterBlock(nextHash)
27✔
423
                if err != nil {
27✔
424
                        return err
×
425
                }
×
426

427
                // We're only interested in all prior outputs that have been
428
                // spent in the block, so collate all the referenced previous
429
                // outpoints within each tx and input.
430
                for _, tx := range filterBlock.Transactions {
31✔
431
                        for _, txIn := range tx.TxIn {
8✔
432
                                spentOutputs = append(spentOutputs,
4✔
433
                                        &txIn.PreviousOutPoint)
4✔
434
                        }
4✔
435
                }
436
        }
437

438
        // With the spent outputs gathered, attempt to prune the channel graph,
439
        // also passing in the best hash+height so the prune tip can be updated.
440
        closedChans, err := b.cfg.Graph.PruneGraph(
5✔
441
                spentOutputs, bestHash, uint32(bestHeight),
5✔
442
        )
5✔
443
        if err != nil {
5✔
444
                return err
×
445
        }
×
446

447
        log.Infof("Graph pruning complete: %v channels were closed since "+
5✔
448
                "height %v", len(closedChans), pruneHeight)
5✔
449

5✔
450
        return nil
5✔
451
}
452

453
// isZombieChannel takes two edge policy updates and determines if the
454
// corresponding channel should be considered a zombie. The first boolean is
455
// true if the policy update from node 1 is considered a zombie, the second
456
// boolean is that of node 2, and the final boolean is true if the channel
457
// is considered a zombie.
458
func (b *Builder) isZombieChannel(e1,
459
        e2 *models.ChannelEdgePolicy) (bool, bool, bool) {
6✔
460

6✔
461
        chanExpiry := b.cfg.ChannelPruneExpiry
6✔
462

6✔
463
        e1Zombie := e1 == nil || time.Since(e1.LastUpdate) >= chanExpiry
6✔
464
        e2Zombie := e2 == nil || time.Since(e2.LastUpdate) >= chanExpiry
6✔
465

6✔
466
        var e1Time, e2Time time.Time
6✔
467
        if e1 != nil {
10✔
468
                e1Time = e1.LastUpdate
4✔
469
        }
4✔
470
        if e2 != nil {
12✔
471
                e2Time = e2.LastUpdate
6✔
472
        }
6✔
473

474
        return e1Zombie, e2Zombie, b.IsZombieChannel(e1Time, e2Time)
6✔
475
}
476

477
// IsZombieChannel takes the timestamps of the latest channel updates for a
478
// channel and returns true if the channel should be considered a zombie based
479
// on these timestamps.
480
func (b *Builder) IsZombieChannel(updateTime1,
481
        updateTime2 time.Time) bool {
6✔
482

6✔
483
        chanExpiry := b.cfg.ChannelPruneExpiry
6✔
484

6✔
485
        e1Zombie := updateTime1.IsZero() ||
6✔
486
                time.Since(updateTime1) >= chanExpiry
6✔
487

6✔
488
        e2Zombie := updateTime2.IsZero() ||
6✔
489
                time.Since(updateTime2) >= chanExpiry
6✔
490

6✔
491
        // If we're using strict zombie pruning, then a channel is only
6✔
492
        // considered live if both edges have a recent update we know of.
6✔
493
        if b.cfg.StrictZombiePruning {
9✔
494
                return e1Zombie || e2Zombie
3✔
495
        }
3✔
496

497
        // Otherwise, if we're using the less strict variant, then a channel is
498
        // considered live if either of the edges have a recent update.
499
        return e1Zombie && e2Zombie
3✔
500
}
501

502
// IsZombieByAge checks if a channel is a zombie by its age. It uses the
503
// timestamp of the block of the transaction that opened the channel. We use
504
// this only for channels that have no edge policies, as we can't use the last
505
// update timestamp to determine if the channel is a zombie.
506
func (b *Builder) IsZombieByAge(scid uint64) (bool, error) {
3✔
507
        blockHeight := lnwire.NewShortChanIDFromInt(scid).BlockHeight
3✔
508

3✔
509
        blockhash, err := b.cfg.Chain.GetBlockHash(int64(blockHeight))
3✔
510
        if err != nil {
3✔
NEW
511
                return false, err
×
NEW
512
        }
×
513

514
        header, err := b.cfg.Chain.GetBlockHeader(blockhash)
3✔
515
        if err != nil {
3✔
NEW
516
                return false, err
×
NEW
517
        }
×
518

519
        return time.Since(header.Timestamp) >= b.cfg.ChannelPruneExpiry, nil
3✔
520
}
521

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

5✔
532
        log.Infof("Examining channel graph for zombie channels")
5✔
533

5✔
534
        // A helper method to detect if the channel belongs to this node
5✔
535
        isSelfChannelEdge := func(info *models.ChannelEdgeInfo) bool {
16✔
536
                return info.NodeKey1Bytes == b.cfg.SelfNode ||
11✔
537
                        info.NodeKey2Bytes == b.cfg.SelfNode
11✔
538
        }
11✔
539

540
        // First, we'll collect all the channels which are eligible for garbage
541
        // collection due to being zombies.
542
        filterPruneChans := func(info *models.ChannelEdgeInfo,
5✔
543
                e1, e2 *models.ChannelEdgePolicy) error {
13✔
544

8✔
545
                // Exit early in case this channel is already marked to be
8✔
546
                // pruned
8✔
547
                _, markedToPrune := chansToPrune[info.ChannelID]
8✔
548
                if markedToPrune {
8✔
549
                        return nil
×
550
                }
×
551

552
                // We'll ensure that we don't attempt to prune our *own*
553
                // channels from the graph, as in any case this should be
554
                // re-advertised by the sub-system above us.
555
                if isSelfChannelEdge(info) {
10✔
556
                        return nil
2✔
557
                }
2✔
558

559
                // If both edges are nil, then we'll check if the channel is a
560
                // zombie that has been opened for long and never received a
561
                // policy update.
562
                if e1 == nil && e2 == nil {
6✔
NEW
563
                        isZombie, err := b.IsZombieByAge(info.ChannelID)
×
NEW
564
                        if err != nil {
×
NEW
565
                                return fmt.Errorf("unable to check if "+
×
NEW
566
                                        "channel is a zombie: %w", err)
×
NEW
567
                        }
×
568

NEW
569
                        if isZombie {
×
NEW
570
                                log.Trace("Channel with chan_id=%v is zombie",
×
NEW
571
                                        info.ChannelID)
×
NEW
572

×
NEW
573
                                chansToPrune[info.ChannelID] = struct{}{}
×
NEW
574
                        }
×
575

576
                        // We've handled channels with no policies, so we can
577
                        // exit early to process the next channel.
NEW
578
                        return nil
×
579
                }
580

581
                e1Zombie, e2Zombie, isZombieChan := b.isZombieChannel(e1, e2)
6✔
582

6✔
583
                if e1Zombie {
10✔
584
                        log.Tracef("Node1 pubkey=%x of chan_id=%v is zombie",
4✔
585
                                info.NodeKey1Bytes, info.ChannelID)
4✔
586
                }
4✔
587

588
                if e2Zombie {
12✔
589
                        log.Tracef("Node2 pubkey=%x of chan_id=%v is zombie",
6✔
590
                                info.NodeKey2Bytes, info.ChannelID)
6✔
591
                }
6✔
592

593
                // If either edge hasn't been updated for a period of
594
                // chanExpiry, then we'll mark the channel itself as eligible
595
                // for graph pruning.
596
                if !isZombieChan {
7✔
597
                        return nil
1✔
598
                }
1✔
599

600
                log.Debugf("ChannelID(%v) is a zombie, collecting to prune",
5✔
601
                        info.ChannelID)
5✔
602

5✔
603
                // TODO(roasbeef): add ability to delete single directional edge
5✔
604
                chansToPrune[info.ChannelID] = struct{}{}
5✔
605

5✔
606
                return nil
5✔
607
        }
608

609
        // If AssumeChannelValid is present we'll look at the disabled bit for
610
        // both edges. If they're both disabled, then we can interpret this as
611
        // the channel being closed and can prune it from our graph.
612
        if b.cfg.AssumeChannelValid {
7✔
613
                disabledChanIDs, err := b.cfg.Graph.DisabledChannelIDs()
2✔
614
                if err != nil {
2✔
615
                        return fmt.Errorf("unable to get disabled channels "+
×
616
                                "ids chans: %v", err)
×
617
                }
×
618

619
                disabledEdges, err := b.cfg.Graph.FetchChanInfos(
2✔
620
                        disabledChanIDs,
2✔
621
                )
2✔
622
                if err != nil {
2✔
623
                        return fmt.Errorf("unable to fetch disabled channels "+
×
624
                                "edges chans: %v", err)
×
625
                }
×
626

627
                // Ensuring we won't prune our own channel from the graph.
628
                for _, disabledEdge := range disabledEdges {
5✔
629
                        if !isSelfChannelEdge(disabledEdge.Info) {
4✔
630
                                chansToPrune[disabledEdge.Info.ChannelID] =
1✔
631
                                        struct{}{}
1✔
632
                        }
1✔
633
                }
634
        }
635

636
        startTime := time.Unix(0, 0)
5✔
637
        endTime := time.Now().Add(-1 * chanExpiry)
5✔
638
        oldEdgesIter := b.cfg.Graph.ChanUpdatesInHorizon(startTime, endTime)
5✔
639

5✔
640
        for u, err := range oldEdgesIter {
13✔
641
                if err != nil {
8✔
642
                        return fmt.Errorf("unable to fetch expired "+
×
643
                                "channel updates chans: %v", err)
×
644
                }
×
645

646
                err = filterPruneChans(u.Info, u.Policy1, u.Policy2)
8✔
647
                if err != nil {
8✔
648
                        return fmt.Errorf("error filtering channels to "+
×
649
                                "prune: %w", err)
×
650
                }
×
651
        }
652

653
        log.Infof("Pruning %v zombie channels", len(chansToPrune))
5✔
654
        if len(chansToPrune) == 0 {
7✔
655
                return nil
2✔
656
        }
2✔
657

658
        // With the set of zombie-like channels obtained, we'll do another pass
659
        // to delete them from the channel graph.
660
        toPrune := make([]uint64, 0, len(chansToPrune))
3✔
661
        for chanID := range chansToPrune {
9✔
662
                toPrune = append(toPrune, chanID)
6✔
663
                log.Tracef("Pruning zombie channel with ChannelID(%v)", chanID)
6✔
664
        }
6✔
665
        err := b.cfg.Graph.DeleteChannelEdges(
3✔
666
                b.cfg.StrictZombiePruning, true, toPrune...,
3✔
667
        )
3✔
668
        if err != nil {
3✔
669
                return fmt.Errorf("unable to delete zombie channels: %w", err)
×
670
        }
×
671

672
        // With the channels pruned, we'll also attempt to prune any nodes that
673
        // were a part of them.
674
        err = b.cfg.Graph.PruneGraphNodes()
3✔
675
        if err != nil && !errors.Is(err, graphdb.ErrGraphNodesNotFound) {
3✔
676
                return fmt.Errorf("unable to prune graph nodes: %w", err)
×
677
        }
×
678

679
        return nil
3✔
680
}
681

682
// networkHandler is the primary goroutine for the Builder. The roles of
683
// this goroutine include answering queries related to the state of the
684
// network, pruning the graph on new block notification, applying network
685
// updates, and registering new topology clients.
686
//
687
// NOTE: This MUST be run as a goroutine.
688
func (b *Builder) networkHandler() {
23✔
689
        defer b.wg.Done()
23✔
690

23✔
691
        graphPruneTicker := time.NewTicker(b.cfg.GraphPruneInterval)
23✔
692
        defer graphPruneTicker.Stop()
23✔
693

23✔
694
        defer b.statTicker.Stop()
23✔
695

23✔
696
        b.stats.Reset()
23✔
697

23✔
698
        for {
120✔
699
                // If there are stats, resume the statTicker.
97✔
700
                if !b.stats.Empty() {
125✔
701
                        b.statTicker.Resume()
28✔
702
                }
28✔
703

704
                select {
97✔
705
                case chainUpdate, ok := <-b.staleBlocks:
13✔
706
                        // If the channel has been closed, then this indicates
13✔
707
                        // the daemon is shutting down, so we exit ourselves.
13✔
708
                        if !ok {
13✔
709
                                return
×
710
                        }
×
711

712
                        // Since this block is stale, we update our best height
713
                        // to the previous block.
714
                        blockHeight := chainUpdate.Height
13✔
715
                        b.bestHeight.Store(blockHeight - 1)
13✔
716

13✔
717
                        // Update the channel graph to reflect that this block
13✔
718
                        // was disconnected.
13✔
719
                        _, err := b.cfg.Graph.DisconnectBlockAtHeight(
13✔
720
                                blockHeight,
13✔
721
                        )
13✔
722
                        if err != nil {
13✔
723
                                log.Errorf("unable to prune graph with stale "+
×
724
                                        "block: %v", err)
×
725
                                continue
×
726
                        }
727

728
                        // TODO(halseth): notify client about the reorg?
729

730
                // A new block has arrived, so we can prune the channel graph
731
                // of any channels which were closed in the block.
732
                case chainUpdate, ok := <-b.newBlocks:
67✔
733
                        // If the channel has been closed, then this indicates
67✔
734
                        // the daemon is shutting down, so we exit ourselves.
67✔
735
                        if !ok {
67✔
736
                                return
×
737
                        }
×
738

739
                        // We'll ensure that any new blocks received attach
740
                        // directly to the end of our main chain. If not, then
741
                        // we've somehow missed some blocks. Here we'll catch
742
                        // up the chain with the latest blocks.
743
                        currentHeight := b.bestHeight.Load()
67✔
744
                        switch {
67✔
745
                        case chainUpdate.Height == currentHeight+1:
61✔
746
                                err := b.updateGraphWithClosedChannels(
61✔
747
                                        chainUpdate,
61✔
748
                                )
61✔
749
                                if err != nil {
61✔
750
                                        log.Errorf("unable to prune graph "+
×
751
                                                "with closed channels: %v", err)
×
752
                                }
×
753

754
                        case chainUpdate.Height > currentHeight+1:
1✔
755
                                log.Errorf("out of order block: expecting "+
1✔
756
                                        "height=%v, got height=%v",
1✔
757
                                        currentHeight+1, chainUpdate.Height)
1✔
758

1✔
759
                                err := b.getMissingBlocks(
1✔
760
                                        currentHeight, chainUpdate,
1✔
761
                                )
1✔
762
                                if err != nil {
1✔
763
                                        log.Errorf("unable to retrieve missing"+
×
764
                                                "blocks: %v", err)
×
765
                                }
×
766

767
                        case chainUpdate.Height < currentHeight+1:
5✔
768
                                log.Errorf("out of order block: expecting "+
5✔
769
                                        "height=%v, got height=%v",
5✔
770
                                        currentHeight+1, chainUpdate.Height)
5✔
771

5✔
772
                                log.Infof("Skipping channel pruning since "+
5✔
773
                                        "received block height %v was already"+
5✔
774
                                        " processed.", chainUpdate.Height)
5✔
775
                        }
776

777
                // The graph prune ticker has ticked, so we'll examine the
778
                // state of the known graph to filter out any zombie channels
779
                // for pruning.
780
                case <-graphPruneTicker.C:
×
781
                        if err := b.pruneZombieChans(); err != nil {
×
782
                                log.Errorf("Unable to prune zombies: %v", err)
×
783
                        }
×
784

785
                // Log any stats if we've processed a non-empty number of
786
                // channels, updates, or nodes. We'll only pause the ticker if
787
                // the last window contained no updates to avoid resuming and
788
                // pausing while consecutive windows contain new info.
789
                case <-b.statTicker.Ticks():
2✔
790
                        if !b.stats.Empty() {
4✔
791
                                log.Infof(b.stats.String())
2✔
792
                        } else {
2✔
793
                                b.statTicker.Pause()
×
794
                        }
×
795
                        b.stats.Reset()
2✔
796

797
                // The router has been signalled to exit, to we exit our main
798
                // loop so the wait group can be decremented.
799
                case <-b.quit:
21✔
800
                        return
21✔
801
                }
802
        }
803
}
804

805
// getMissingBlocks walks through all missing blocks and updates the graph
806
// closed channels accordingly.
807
func (b *Builder) getMissingBlocks(currentHeight uint32,
808
        chainUpdate *chainview.FilteredBlock) error {
1✔
809

1✔
810
        outdatedHash, err := b.cfg.Chain.GetBlockHash(int64(currentHeight))
1✔
811
        if err != nil {
1✔
812
                return err
×
813
        }
×
814

815
        outdatedBlock := &chainntnfs.BlockEpoch{
1✔
816
                Height: int32(currentHeight),
1✔
817
                Hash:   outdatedHash,
1✔
818
        }
1✔
819

1✔
820
        epochClient, err := b.cfg.Notifier.RegisterBlockEpochNtfn(
1✔
821
                outdatedBlock,
1✔
822
        )
1✔
823
        if err != nil {
1✔
824
                return err
×
825
        }
×
826
        defer epochClient.Cancel()
1✔
827

1✔
828
        blockDifference := int(chainUpdate.Height - currentHeight)
1✔
829

1✔
830
        // We'll walk through all the outdated blocks and make sure we're able
1✔
831
        // to update the graph with any closed channels from them.
1✔
832
        for i := 0; i < blockDifference; i++ {
6✔
833
                var (
5✔
834
                        missingBlock *chainntnfs.BlockEpoch
5✔
835
                        ok           bool
5✔
836
                )
5✔
837

5✔
838
                select {
5✔
839
                case missingBlock, ok = <-epochClient.Epochs:
5✔
840
                        if !ok {
5✔
841
                                return nil
×
842
                        }
×
843

844
                case <-b.quit:
×
845
                        return nil
×
846
                }
847

848
                filteredBlock, err := b.cfg.ChainView.FilterBlock(
5✔
849
                        missingBlock.Hash,
5✔
850
                )
5✔
851
                if err != nil {
5✔
852
                        return err
×
853
                }
×
854

855
                err = b.updateGraphWithClosedChannels(
5✔
856
                        filteredBlock,
5✔
857
                )
5✔
858
                if err != nil {
5✔
859
                        return err
×
860
                }
×
861
        }
862

863
        return nil
1✔
864
}
865

866
// updateGraphWithClosedChannels prunes the channel graph of closed channels
867
// that are no longer needed.
868
func (b *Builder) updateGraphWithClosedChannels(
869
        chainUpdate *chainview.FilteredBlock) error {
66✔
870

66✔
871
        // Once a new block arrives, we update our running track of the height
66✔
872
        // of the chain tip.
66✔
873
        blockHeight := chainUpdate.Height
66✔
874

66✔
875
        b.bestHeight.Store(blockHeight)
66✔
876
        log.Infof("Pruning channel graph using block %v (height=%v)",
66✔
877
                chainUpdate.Hash, blockHeight)
66✔
878

66✔
879
        // We're only interested in all prior outputs that have been spent in
66✔
880
        // the block, so collate all the referenced previous outpoints within
66✔
881
        // each tx and input.
66✔
882
        var spentOutputs []*wire.OutPoint
66✔
883
        for _, tx := range chainUpdate.Transactions {
70✔
884
                for _, txIn := range tx.TxIn {
8✔
885
                        spentOutputs = append(spentOutputs,
4✔
886
                                &txIn.PreviousOutPoint)
4✔
887
                }
4✔
888
        }
889

890
        // With the spent outputs gathered, attempt to prune the channel graph,
891
        // also passing in the hash+height of the block being pruned so the
892
        // prune tip can be updated.
893
        chansClosed, err := b.cfg.Graph.PruneGraph(spentOutputs,
66✔
894
                &chainUpdate.Hash, chainUpdate.Height)
66✔
895
        if err != nil {
66✔
896
                log.Errorf("unable to prune routing table: %v", err)
×
897
                return err
×
898
        }
×
899

900
        log.Infof("Block %v (height=%v) closed %v channels", chainUpdate.Hash,
66✔
901
                blockHeight, len(chansClosed))
66✔
902

66✔
903
        return nil
66✔
904
}
905

906
// assertNodeAnnFreshness returns a non-nil error if we have an announcement in
907
// the database for the passed node with a timestamp newer than the passed
908
// timestamp. ErrIgnored will be returned if we already have the node, and
909
// ErrOutdated will be returned if we have a timestamp that's after the new
910
// timestamp.
911
func (b *Builder) assertNodeAnnFreshness(ctx context.Context, node route.Vertex,
912
        msgTimestamp time.Time) error {
13✔
913

13✔
914
        // If we are not already aware of this node, it means that we don't
13✔
915
        // know about any channel using this node. To avoid a DoS attack by
13✔
916
        // node announcements, we will ignore such nodes. If we do know about
13✔
917
        // this node, check that this update brings info newer than what we
13✔
918
        // already have.
13✔
919
        lastUpdate, exists, err := b.cfg.Graph.HasNode(ctx, node)
13✔
920
        if err != nil {
13✔
921
                return fmt.Errorf("unable to query for the "+
×
922
                        "existence of node: %w", err)
×
923
        }
×
924
        if !exists {
17✔
925
                return NewErrf(ErrIgnored, "Ignoring node announcement"+
4✔
926
                        " for node not found in channel graph (%x)",
4✔
927
                        node[:])
4✔
928
        }
4✔
929

930
        // If we've reached this point then we're aware of the vertex being
931
        // advertised. So we now check if the new message has a new time stamp,
932
        // if not then we won't accept the new data as it would override newer
933
        // data.
934
        if !lastUpdate.Before(msgTimestamp) {
16✔
935
                return NewErrf(ErrOutdated, "Ignoring outdated "+
4✔
936
                        "announcement for %x", node[:])
4✔
937
        }
4✔
938

939
        return nil
11✔
940
}
941

942
// MarkZombieEdge adds a channel that failed complete validation into the zombie
943
// index so we can avoid having to re-validate it in the future.
944
func (b *Builder) MarkZombieEdge(chanID uint64) error {
×
945
        // If the edge fails validation we'll mark the edge itself as a zombie
×
946
        // so we don't continue to request it. We use the "zero key" for both
×
947
        // node pubkeys so this edge can't be resurrected.
×
948
        var zeroKey [33]byte
×
949
        err := b.cfg.Graph.MarkEdgeZombie(chanID, zeroKey, zeroKey)
×
950
        if err != nil {
×
951
                return fmt.Errorf("unable to mark spent chan(id=%v) as a "+
×
952
                        "zombie: %w", chanID, err)
×
953
        }
×
954

955
        return nil
×
956
}
957

958
// ApplyChannelUpdate validates a channel update and if valid, applies it to the
959
// database. It returns a bool indicating whether the updates were successful.
960
func (b *Builder) ApplyChannelUpdate(msg *lnwire.ChannelUpdate1) bool {
3✔
961
        ctx := context.TODO()
3✔
962

3✔
963
        ch, _, _, err := b.GetChannelByID(msg.ShortChannelID)
3✔
964
        if err != nil {
6✔
965
                log.Errorf("Unable to retrieve channel by id: %v", err)
3✔
966
                return false
3✔
967
        }
3✔
968

969
        var pubKey *btcec.PublicKey
3✔
970

3✔
971
        switch msg.ChannelFlags & lnwire.ChanUpdateDirection {
3✔
972
        case 0:
3✔
973
                pubKey, _ = ch.NodeKey1()
3✔
974

975
        case 1:
3✔
976
                pubKey, _ = ch.NodeKey2()
3✔
977
        }
978

979
        // Exit early if the pubkey cannot be decided.
980
        if pubKey == nil {
3✔
981
                log.Errorf("Unable to decide pubkey with ChannelFlags=%v",
×
982
                        msg.ChannelFlags)
×
983
                return false
×
984
        }
×
985

986
        err = netann.ValidateChannelUpdateAnn(pubKey, ch.Capacity, msg)
3✔
987
        if err != nil {
3✔
988
                log.Errorf("Unable to validate channel update: %v", err)
×
989
                return false
×
990
        }
×
991

992
        update := &models.ChannelEdgePolicy{
3✔
993
                SigBytes:                  msg.Signature.ToSignatureBytes(),
3✔
994
                ChannelID:                 msg.ShortChannelID.ToUint64(),
3✔
995
                LastUpdate:                time.Unix(int64(msg.Timestamp), 0),
3✔
996
                MessageFlags:              msg.MessageFlags,
3✔
997
                ChannelFlags:              msg.ChannelFlags,
3✔
998
                TimeLockDelta:             msg.TimeLockDelta,
3✔
999
                MinHTLC:                   msg.HtlcMinimumMsat,
3✔
1000
                MaxHTLC:                   msg.HtlcMaximumMsat,
3✔
1001
                FeeBaseMSat:               lnwire.MilliSatoshi(msg.BaseFee),
3✔
1002
                FeeProportionalMillionths: lnwire.MilliSatoshi(msg.FeeRate),
3✔
1003
                InboundFee:                msg.InboundFee.ValOpt(),
3✔
1004
                ExtraOpaqueData:           msg.ExtraOpaqueData,
3✔
1005
        }
3✔
1006

3✔
1007
        err = b.UpdateEdge(ctx, update)
3✔
1008
        if err != nil && !IsError(err, ErrIgnored, ErrOutdated) {
3✔
1009
                log.Errorf("Unable to apply channel update: %v", err)
×
1010
                return false
×
1011
        }
×
1012

1013
        return true
3✔
1014
}
1015

1016
// AddNode is used to add information about a node to the router database. If
1017
// the node with this pubkey is not present in an existing channel, it will
1018
// be ignored.
1019
//
1020
// NOTE: This method is part of the ChannelGraphSource interface.
1021
func (b *Builder) AddNode(ctx context.Context, node *models.Node,
1022
        op ...batch.SchedulerOption) error {
10✔
1023

10✔
1024
        err := b.addNode(ctx, node, op...)
10✔
1025
        if err != nil {
14✔
1026
                logNetworkMsgProcessError(err)
4✔
1027

4✔
1028
                return err
4✔
1029
        }
4✔
1030

1031
        return nil
9✔
1032
}
1033

1034
// addNode does some basic checks on the given Node against what we
1035
// currently have persisted in the graph, and then adds it to the graph. If we
1036
// already know about the node, then we only update our DB if the new update
1037
// has a newer timestamp than the last one we received.
1038
func (b *Builder) addNode(ctx context.Context, node *models.Node,
1039
        op ...batch.SchedulerOption) error {
10✔
1040

10✔
1041
        // Before we add the node to the database, we'll check to see if the
10✔
1042
        // announcement is "fresh" or not. If it isn't, then we'll return an
10✔
1043
        // error.
10✔
1044
        err := b.assertNodeAnnFreshness(ctx, node.PubKeyBytes, node.LastUpdate)
10✔
1045
        if err != nil {
14✔
1046
                return err
4✔
1047
        }
4✔
1048

1049
        if err := b.cfg.Graph.AddNode(ctx, node, op...); err != nil {
9✔
1050
                return fmt.Errorf("unable to add node %x to the "+
×
1051
                        "graph: %w", node.PubKeyBytes, err)
×
1052
        }
×
1053

1054
        log.Tracef("Updated vertex data for node=%x", node.PubKeyBytes)
9✔
1055
        b.stats.incNumNodeUpdates()
9✔
1056

9✔
1057
        return nil
9✔
1058
}
1059

1060
// AddEdge is used to add edge/channel to the topology of the router, after all
1061
// information about channel will be gathered this edge/channel might be used
1062
// in construction of payment path.
1063
//
1064
// NOTE: This method is part of the ChannelGraphSource interface.
1065
func (b *Builder) AddEdge(ctx context.Context, edge *models.ChannelEdgeInfo,
1066
        op ...batch.SchedulerOption) error {
17✔
1067

17✔
1068
        err := b.addEdge(ctx, edge, op...)
17✔
1069
        if err != nil {
20✔
1070
                logNetworkMsgProcessError(err)
3✔
1071

3✔
1072
                return err
3✔
1073
        }
3✔
1074

1075
        return nil
17✔
1076
}
1077

1078
// addEdge does some validation on the new channel edge against what we
1079
// currently have persisted in the graph, and then adds it to the graph. The
1080
// Chain View is updated with the new edge if it is successfully added to the
1081
// graph. We only persist the channel if we currently dont have it at all in
1082
// our graph.
1083
func (b *Builder) addEdge(ctx context.Context, edge *models.ChannelEdgeInfo,
1084
        op ...batch.SchedulerOption) error {
17✔
1085

17✔
1086
        log.Debugf("Received ChannelEdgeInfo for channel %v", edge.ChannelID)
17✔
1087

17✔
1088
        // Prior to processing the announcement we first check if we
17✔
1089
        // already know of this channel, if so, then we can exit early.
17✔
1090
        _, _, exists, isZombie, err := b.cfg.Graph.HasChannelEdge(
17✔
1091
                edge.ChannelID,
17✔
1092
        )
17✔
1093
        if err != nil && !errors.Is(err, graphdb.ErrGraphNoEdgesFound) {
17✔
1094
                return fmt.Errorf("unable to check for edge existence: %w",
×
1095
                        err)
×
1096
        }
×
1097
        if isZombie {
17✔
1098
                return NewErrf(ErrIgnored, "ignoring msg for zombie chan_id=%v",
×
1099
                        edge.ChannelID)
×
1100
        }
×
1101
        if exists {
20✔
1102
                return NewErrf(ErrIgnored, "ignoring msg for known chan_id=%v",
3✔
1103
                        edge.ChannelID)
3✔
1104
        }
3✔
1105

1106
        if err := b.cfg.Graph.AddChannelEdge(ctx, edge, op...); err != nil {
17✔
1107
                return fmt.Errorf("unable to add edge: %w", err)
×
1108
        }
×
1109

1110
        b.stats.incNumEdgesDiscovered()
17✔
1111

17✔
1112
        // If AssumeChannelValid is present, of if the SCID is an alias, then
17✔
1113
        // the gossiper would not have done the expensive work of fetching
17✔
1114
        // a funding transaction and validating it. So we won't have the channel
17✔
1115
        // capacity nor the funding script. So we just log and return here.
17✔
1116
        scid := lnwire.NewShortChanIDFromInt(edge.ChannelID)
17✔
1117
        if b.cfg.AssumeChannelValid || b.cfg.IsAlias(scid) {
20✔
1118
                log.Tracef("New channel discovered! Link connects %x and %x "+
3✔
1119
                        "with ChannelID(%v)", edge.NodeKey1Bytes,
3✔
1120
                        edge.NodeKey2Bytes, edge.ChannelID)
3✔
1121

3✔
1122
                return nil
3✔
1123
        }
3✔
1124

1125
        log.Debugf("New channel discovered! Link connects %x and %x with "+
17✔
1126
                "ChannelPoint(%v): chan_id=%v, capacity=%v", edge.NodeKey1Bytes,
17✔
1127
                edge.NodeKey2Bytes, edge.ChannelPoint, edge.ChannelID,
17✔
1128
                edge.Capacity)
17✔
1129

17✔
1130
        // Otherwise, then we expect the funding script to be present on the
17✔
1131
        // edge since it would have been fetched when the gossiper validated the
17✔
1132
        // announcement.
17✔
1133
        fundingPkScript, err := edge.FundingScript.UnwrapOrErr(fmt.Errorf(
17✔
1134
                "expected the funding transaction script to be set",
17✔
1135
        ))
17✔
1136
        if err != nil {
17✔
1137
                return err
×
1138
        }
×
1139

1140
        // As a new edge has been added to the channel graph, we'll update the
1141
        // current UTXO filter within our active FilteredChainView so we are
1142
        // notified if/when this channel is closed.
1143
        filterUpdate := []graphdb.EdgePoint{
17✔
1144
                {
17✔
1145
                        FundingPkScript: fundingPkScript,
17✔
1146
                        OutPoint:        edge.ChannelPoint,
17✔
1147
                },
17✔
1148
        }
17✔
1149

17✔
1150
        err = b.cfg.ChainView.UpdateFilter(filterUpdate, b.bestHeight.Load())
17✔
1151
        if err != nil {
17✔
1152
                return fmt.Errorf("unable to update chain view: %w", err)
×
1153
        }
×
1154

1155
        return nil
17✔
1156
}
1157

1158
// UpdateEdge is used to update edge information, without this message edge
1159
// considered as not fully constructed.
1160
//
1161
// NOTE: This method is part of the ChannelGraphSource interface.
1162
func (b *Builder) UpdateEdge(ctx context.Context,
1163
        update *models.ChannelEdgePolicy, op ...batch.SchedulerOption) error {
9✔
1164

9✔
1165
        err := b.updateEdge(ctx, update, op...)
9✔
1166
        if err != nil {
13✔
1167
                logNetworkMsgProcessError(err)
4✔
1168

4✔
1169
                return err
4✔
1170
        }
4✔
1171

1172
        return nil
8✔
1173
}
1174

1175
// updateEdge validates the new edge policy against what we currently have
1176
// persisted in the graph, and then applies it to the graph if the update is
1177
// considered fresh enough and if we actually have a channel persisted for the
1178
// given update.
1179
func (b *Builder) updateEdge(ctx context.Context,
1180
        policy *models.ChannelEdgePolicy, op ...batch.SchedulerOption) error {
9✔
1181

9✔
1182
        log.Debugf("Received ChannelEdgePolicy for channel %v",
9✔
1183
                policy.ChannelID)
9✔
1184

9✔
1185
        // We make sure to hold the mutex for this channel ID, such that no
9✔
1186
        // other goroutine is concurrently doing database accesses for the same
9✔
1187
        // channel ID.
9✔
1188
        b.channelEdgeMtx.Lock(policy.ChannelID)
9✔
1189
        defer b.channelEdgeMtx.Unlock(policy.ChannelID)
9✔
1190

9✔
1191
        edge1Timestamp, edge2Timestamp, exists, isZombie, err :=
9✔
1192
                b.cfg.Graph.HasChannelEdge(policy.ChannelID)
9✔
1193
        if err != nil && !errors.Is(err, graphdb.ErrGraphNoEdgesFound) {
9✔
1194
                return fmt.Errorf("unable to check for edge existence: %w", err)
×
1195
        }
×
1196

1197
        // If the channel is marked as a zombie in our database, and
1198
        // we consider this a stale update, then we should not apply the
1199
        // policy.
1200
        isStaleUpdate := time.Since(policy.LastUpdate) >
9✔
1201
                b.cfg.ChannelPruneExpiry
9✔
1202

9✔
1203
        if isZombie && isStaleUpdate {
9✔
1204
                return NewErrf(ErrIgnored, "ignoring stale update "+
×
1205
                        "(flags=%v|%v) for zombie chan_id=%v",
×
1206
                        policy.MessageFlags, policy.ChannelFlags,
×
1207
                        policy.ChannelID)
×
1208
        }
×
1209

1210
        // If the channel doesn't exist in our database, we cannot apply the
1211
        // updated policy.
1212
        if !exists {
10✔
1213
                return NewErrf(ErrIgnored, "ignoring update (flags=%v|%v) for "+
1✔
1214
                        "unknown chan_id=%v", policy.MessageFlags,
1✔
1215
                        policy.ChannelFlags, policy.ChannelID)
1✔
1216
        }
1✔
1217

1218
        log.Debugf("Found edge1Timestamp=%v, edge2Timestamp=%v",
8✔
1219
                edge1Timestamp, edge2Timestamp)
8✔
1220

8✔
1221
        // As edges are directional edge node has a unique policy for the
8✔
1222
        // direction of the edge they control. Therefore, we first check if we
8✔
1223
        // already have the most up-to-date information for that edge. If this
8✔
1224
        // message has a timestamp not strictly newer than what we already know
8✔
1225
        // of we can exit early.
8✔
1226
        switch policy.ChannelFlags & lnwire.ChanUpdateDirection {
8✔
1227
        // A flag set of 0 indicates this is an announcement for the "first"
1228
        // node in the channel.
1229
        case 0:
6✔
1230
                // Ignore outdated message.
6✔
1231
                if !edge1Timestamp.Before(policy.LastUpdate) {
9✔
1232
                        return NewErrf(ErrOutdated, "Ignoring "+
3✔
1233
                                "outdated update (flags=%v|%v) for "+
3✔
1234
                                "known chan_id=%v", policy.MessageFlags,
3✔
1235
                                policy.ChannelFlags, policy.ChannelID)
3✔
1236
                }
3✔
1237

1238
        // Similarly, a flag set of 1 indicates this is an announcement
1239
        // for the "second" node in the channel.
1240
        case 1:
5✔
1241
                // Ignore outdated message.
5✔
1242
                if !edge2Timestamp.Before(policy.LastUpdate) {
8✔
1243
                        return NewErrf(ErrOutdated, "Ignoring "+
3✔
1244
                                "outdated update (flags=%v|%v) for "+
3✔
1245
                                "known chan_id=%v", policy.MessageFlags,
3✔
1246
                                policy.ChannelFlags, policy.ChannelID)
3✔
1247
                }
3✔
1248
        }
1249

1250
        // Now that we know this isn't a stale update, we'll apply the new edge
1251
        // policy to the proper directional edge within the channel graph.
1252
        if err = b.cfg.Graph.UpdateEdgePolicy(ctx, policy, op...); err != nil {
8✔
1253
                err := fmt.Errorf("unable to add channel: %w", err)
×
1254
                log.Error(err)
×
1255
                return err
×
1256
        }
×
1257

1258
        log.Tracef("New channel update applied: %v",
8✔
1259
                lnutils.SpewLogClosure(policy))
8✔
1260
        b.stats.incNumChannelUpdates()
8✔
1261

8✔
1262
        return nil
8✔
1263
}
1264

1265
// logNetworkMsgProcessError logs the error received from processing a network
1266
// message. It logs as a debug message if the error is not critical.
1267
func logNetworkMsgProcessError(err error) {
5✔
1268
        if IsError(err, ErrIgnored, ErrOutdated) {
10✔
1269
                log.Debugf("process network updates got: %v", err)
5✔
1270

5✔
1271
                return
5✔
1272
        }
5✔
1273

1274
        log.Errorf("process network updates got: %v", err)
×
1275
}
1276

1277
// CurrentBlockHeight returns the block height from POV of the router subsystem.
1278
//
1279
// NOTE: This method is part of the ChannelGraphSource interface.
1280
func (b *Builder) CurrentBlockHeight() (uint32, error) {
3✔
1281
        _, height, err := b.cfg.Chain.GetBestBlock()
3✔
1282
        return uint32(height), err
3✔
1283
}
3✔
1284

1285
// SyncedHeight returns the block height to which the router subsystem currently
1286
// is synced to. This can differ from the above chain height if the goroutine
1287
// responsible for processing the blocks isn't yet up to speed.
1288
func (b *Builder) SyncedHeight() uint32 {
3✔
1289
        return b.bestHeight.Load()
3✔
1290
}
3✔
1291

1292
// GetChannelByID return the channel by the channel id.
1293
//
1294
// NOTE: This method is part of the ChannelGraphSource interface.
1295
func (b *Builder) GetChannelByID(chanID lnwire.ShortChannelID) (
1296
        *models.ChannelEdgeInfo,
1297
        *models.ChannelEdgePolicy,
1298
        *models.ChannelEdgePolicy, error) {
4✔
1299

4✔
1300
        return b.cfg.Graph.FetchChannelEdgesByID(chanID.ToUint64())
4✔
1301
}
4✔
1302

1303
// FetchNode attempts to look up a target node by its identity public
1304
// key. graphdb.ErrGraphNodeNotFound is returned if the node doesn't exist
1305
// within the graph.
1306
//
1307
// NOTE: This method is part of the ChannelGraphSource interface.
1308
func (b *Builder) FetchNode(ctx context.Context,
1309
        node route.Vertex) (*models.Node, error) {
3✔
1310

3✔
1311
        return b.cfg.Graph.FetchNode(ctx, node)
3✔
1312
}
3✔
1313

1314
// ForAllOutgoingChannels is used to iterate over all outgoing channels owned by
1315
// the router.
1316
//
1317
// NOTE: This method is part of the ChannelGraphSource interface.
1318
func (b *Builder) ForAllOutgoingChannels(ctx context.Context,
1319
        cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy) error,
1320
        reset func()) error {
3✔
1321

3✔
1322
        return b.cfg.Graph.ForEachNodeChannel(
3✔
1323
                ctx, b.cfg.SelfNode,
3✔
1324
                func(c *models.ChannelEdgeInfo, e *models.ChannelEdgePolicy,
3✔
1325
                        _ *models.ChannelEdgePolicy) error {
6✔
1326

3✔
1327
                        if e == nil {
3✔
1328
                                return fmt.Errorf("channel from self node " +
×
1329
                                        "has no policy")
×
1330
                        }
×
1331

1332
                        return cb(c, e)
3✔
1333
                }, reset,
1334
        )
1335
}
1336

1337
// AddProof updates the channel edge info with proof which is needed to
1338
// properly announce the edge to the rest of the network.
1339
//
1340
// NOTE: This method is part of the ChannelGraphSource interface.
1341
func (b *Builder) AddProof(chanID lnwire.ShortChannelID,
1342
        proof *models.ChannelAuthProof) error {
4✔
1343

4✔
1344
        return b.cfg.Graph.AddEdgeProof(chanID, proof)
4✔
1345
}
4✔
1346

1347
// IsStaleNode returns true if the graph source has a node announcement for the
1348
// target node with a more recent timestamp.
1349
//
1350
// NOTE: This method is part of the ChannelGraphSource interface.
1351
func (b *Builder) IsStaleNode(ctx context.Context, node route.Vertex,
1352
        timestamp time.Time) bool {
6✔
1353

6✔
1354
        // If our attempt to assert that the node announcement is fresh fails,
6✔
1355
        // then we know that this is actually a stale announcement.
6✔
1356
        err := b.assertNodeAnnFreshness(ctx, node, timestamp)
6✔
1357
        if err != nil {
10✔
1358
                log.Debugf("Checking stale node %s got %v", node, err)
4✔
1359
                return true
4✔
1360
        }
4✔
1361

1362
        return false
5✔
1363
}
1364

1365
// IsPublicNode determines whether the given vertex is seen as a public node in
1366
// the graph from the graph's source node's point of view.
1367
//
1368
// NOTE: This method is part of the ChannelGraphSource interface.
1369
func (b *Builder) IsPublicNode(node route.Vertex) (bool, error) {
3✔
1370
        return b.cfg.Graph.IsPublicNode(node)
3✔
1371
}
3✔
1372

1373
// IsKnownEdge returns true if the graph source already knows of the passed
1374
// channel ID either as a live or zombie edge.
1375
//
1376
// NOTE: This method is part of the ChannelGraphSource interface.
1377
func (b *Builder) IsKnownEdge(chanID lnwire.ShortChannelID) bool {
4✔
1378
        _, _, exists, isZombie, _ := b.cfg.Graph.HasChannelEdge(
4✔
1379
                chanID.ToUint64(),
4✔
1380
        )
4✔
1381

4✔
1382
        return exists || isZombie
4✔
1383
}
4✔
1384

1385
// IsZombieEdge returns true if the graph source has marked the given channel ID
1386
// as a zombie edge.
1387
//
1388
// NOTE: This method is part of the ChannelGraphSource interface.
1389
func (b *Builder) IsZombieEdge(chanID lnwire.ShortChannelID) (bool, error) {
×
1390
        _, _, _, isZombie, err := b.cfg.Graph.HasChannelEdge(chanID.ToUint64())
×
1391

×
1392
        return isZombie, err
×
1393
}
×
1394

1395
// IsStaleEdgePolicy returns true if the graph source has a channel edge for
1396
// the passed channel ID (and flags) that have a more recent timestamp.
1397
//
1398
// NOTE: This method is part of the ChannelGraphSource interface.
1399
func (b *Builder) IsStaleEdgePolicy(chanID lnwire.ShortChannelID,
1400
        timestamp time.Time, flags lnwire.ChanUpdateChanFlags) bool {
9✔
1401

9✔
1402
        edge1Timestamp, edge2Timestamp, exists, isZombie, err :=
9✔
1403
                b.cfg.Graph.HasChannelEdge(chanID.ToUint64())
9✔
1404
        if err != nil {
9✔
1405
                log.Debugf("Check stale edge policy got error: %v", err)
×
1406
                return false
×
1407
        }
×
1408

1409
        // If we know of the edge as a zombie, then we'll make some additional
1410
        // checks to determine if the new policy is fresh.
1411
        if isZombie {
9✔
1412
                // When running with AssumeChannelValid, we also prune channels
×
1413
                // if both of their edges are disabled. We'll mark the new
×
1414
                // policy as stale if it remains disabled.
×
1415
                if b.cfg.AssumeChannelValid {
×
1416
                        isDisabled := flags&lnwire.ChanUpdateDisabled ==
×
1417
                                lnwire.ChanUpdateDisabled
×
1418
                        if isDisabled {
×
1419
                                return true
×
1420
                        }
×
1421
                }
1422

1423
                // Otherwise, we'll fall back to our usual ChannelPruneExpiry.
1424
                return time.Since(timestamp) > b.cfg.ChannelPruneExpiry
×
1425
        }
1426

1427
        // If we don't know of the edge, then it means it's fresh (thus not
1428
        // stale).
1429
        if !exists {
14✔
1430
                return false
5✔
1431
        }
5✔
1432

1433
        // As edges are directional edge node has a unique policy for the
1434
        // direction of the edge they control. Therefore, we first check if we
1435
        // already have the most up-to-date information for that edge. If so,
1436
        // then we can exit early.
1437
        switch {
7✔
1438
        // A flag set of 0 indicates this is an announcement for the "first"
1439
        // node in the channel.
1440
        case flags&lnwire.ChanUpdateDirection == 0:
5✔
1441
                return !edge1Timestamp.Before(timestamp)
5✔
1442

1443
        // Similarly, a flag set of 1 indicates this is an announcement for the
1444
        // "second" node in the channel.
1445
        case flags&lnwire.ChanUpdateDirection == 1:
5✔
1446
                return !edge2Timestamp.Before(timestamp)
5✔
1447
        }
1448

1449
        return false
×
1450
}
1451

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