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

lightningnetwork / lnd / 15561477203

10 Jun 2025 01:54PM UTC coverage: 58.351% (-10.1%) from 68.487%
15561477203

Pull #9356

github

web-flow
Merge 6440b25db into c6d6d4c0b
Pull Request #9356: lnrpc: add incoming/outgoing channel ids filter to forwarding history request

33 of 36 new or added lines in 2 files covered. (91.67%)

28366 existing lines in 455 files now uncovered.

97715 of 167461 relevant lines covered (58.35%)

1.81 hits per line

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

55.77
/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/lnutils"
17
        "github.com/lightningnetwork/lnd/lnwallet"
18
        "github.com/lightningnetwork/lnd/lnwire"
19
        "github.com/lightningnetwork/lnd/multimutex"
20
        "github.com/lightningnetwork/lnd/netann"
21
        "github.com/lightningnetwork/lnd/routing/chainview"
22
        "github.com/lightningnetwork/lnd/routing/route"
23
        "github.com/lightningnetwork/lnd/ticker"
24
)
25

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

111
        bestHeight atomic.Uint32
112

113
        cfg *Config
114

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

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

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

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

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

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

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

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

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

163
        log.Info("Builder starting")
3✔
164

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

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

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

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

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

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

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

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

×
232
                        return err
×
233
                }
×
234

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

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

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

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

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

×
269
                        return err
×
270
                }
×
271
        }
272

273
        b.wg.Add(1)
3✔
274
        go b.networkHandler()
3✔
275

3✔
276
        log.Debug("Builder started")
3✔
277

3✔
278
        return nil
3✔
279
}
280

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

289
        log.Info("Builder shutting down...")
3✔
290

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

299
        close(b.quit)
3✔
300
        b.wg.Wait()
3✔
301

3✔
302
        log.Debug("Builder shutdown complete")
3✔
303

3✔
304
        return nil
3✔
305
}
306

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

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

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

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

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

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

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

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

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

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

UNCOV
382
                default:
×
383
                }
384

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

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

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

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

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

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

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

3✔
449
        return nil
3✔
450
}
451

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

×
UNCOV
460
        chanExpiry := b.cfg.ChannelPruneExpiry
×
UNCOV
461

×
UNCOV
462
        e1Zombie := e1 == nil || time.Since(e1.LastUpdate) >= chanExpiry
×
UNCOV
463
        e2Zombie := e2 == nil || time.Since(e2.LastUpdate) >= chanExpiry
×
UNCOV
464

×
UNCOV
465
        var e1Time, e2Time time.Time
×
UNCOV
466
        if e1 != nil {
×
UNCOV
467
                e1Time = e1.LastUpdate
×
UNCOV
468
        }
×
UNCOV
469
        if e2 != nil {
×
UNCOV
470
                e2Time = e2.LastUpdate
×
UNCOV
471
        }
×
472

UNCOV
473
        return e1Zombie, e2Zombie, b.IsZombieChannel(e1Time, e2Time)
×
474
}
475

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

×
UNCOV
482
        chanExpiry := b.cfg.ChannelPruneExpiry
×
UNCOV
483

×
UNCOV
484
        e1Zombie := updateTime1.IsZero() ||
×
UNCOV
485
                time.Since(updateTime1) >= chanExpiry
×
UNCOV
486

×
UNCOV
487
        e2Zombie := updateTime2.IsZero() ||
×
UNCOV
488
                time.Since(updateTime2) >= chanExpiry
×
UNCOV
489

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

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

501
// pruneZombieChans is a method that will be called periodically to prune out
502
// any "zombie" channels. We consider channels zombies if *both* edges haven't
503
// been updated since our zombie horizon. If AssumeChannelValid is present,
504
// we'll also consider channels zombies if *both* edges are disabled. This
505
// usually signals that a channel has been closed on-chain. We do this
506
// periodically to keep a healthy, lively routing table.
UNCOV
507
func (b *Builder) pruneZombieChans() error {
×
UNCOV
508
        chansToPrune := make(map[uint64]struct{})
×
UNCOV
509
        chanExpiry := b.cfg.ChannelPruneExpiry
×
UNCOV
510

×
UNCOV
511
        log.Infof("Examining channel graph for zombie channels")
×
UNCOV
512

×
UNCOV
513
        // A helper method to detect if the channel belongs to this node
×
UNCOV
514
        isSelfChannelEdge := func(info *models.ChannelEdgeInfo) bool {
×
UNCOV
515
                return info.NodeKey1Bytes == b.cfg.SelfNode ||
×
UNCOV
516
                        info.NodeKey2Bytes == b.cfg.SelfNode
×
UNCOV
517
        }
×
518

519
        // First, we'll collect all the channels which are eligible for garbage
520
        // collection due to being zombies.
UNCOV
521
        filterPruneChans := func(info *models.ChannelEdgeInfo,
×
UNCOV
522
                e1, e2 *models.ChannelEdgePolicy) error {
×
UNCOV
523

×
UNCOV
524
                // Exit early in case this channel is already marked to be
×
UNCOV
525
                // pruned
×
UNCOV
526
                _, markedToPrune := chansToPrune[info.ChannelID]
×
UNCOV
527
                if markedToPrune {
×
528
                        return nil
×
529
                }
×
530

531
                // We'll ensure that we don't attempt to prune our *own*
532
                // channels from the graph, as in any case this should be
533
                // re-advertised by the sub-system above us.
UNCOV
534
                if isSelfChannelEdge(info) {
×
UNCOV
535
                        return nil
×
UNCOV
536
                }
×
537

UNCOV
538
                e1Zombie, e2Zombie, isZombieChan := b.isZombieChannel(e1, e2)
×
UNCOV
539

×
UNCOV
540
                if e1Zombie {
×
UNCOV
541
                        log.Tracef("Node1 pubkey=%x of chan_id=%v is zombie",
×
UNCOV
542
                                info.NodeKey1Bytes, info.ChannelID)
×
UNCOV
543
                }
×
544

UNCOV
545
                if e2Zombie {
×
UNCOV
546
                        log.Tracef("Node2 pubkey=%x of chan_id=%v is zombie",
×
UNCOV
547
                                info.NodeKey2Bytes, info.ChannelID)
×
UNCOV
548
                }
×
549

550
                // If either edge hasn't been updated for a period of
551
                // chanExpiry, then we'll mark the channel itself as eligible
552
                // for graph pruning.
UNCOV
553
                if !isZombieChan {
×
UNCOV
554
                        return nil
×
UNCOV
555
                }
×
556

UNCOV
557
                log.Debugf("ChannelID(%v) is a zombie, collecting to prune",
×
UNCOV
558
                        info.ChannelID)
×
UNCOV
559

×
UNCOV
560
                // TODO(roasbeef): add ability to delete single directional edge
×
UNCOV
561
                chansToPrune[info.ChannelID] = struct{}{}
×
UNCOV
562

×
UNCOV
563
                return nil
×
564
        }
565

566
        // If AssumeChannelValid is present we'll look at the disabled bit for
567
        // both edges. If they're both disabled, then we can interpret this as
568
        // the channel being closed and can prune it from our graph.
UNCOV
569
        if b.cfg.AssumeChannelValid {
×
UNCOV
570
                disabledChanIDs, err := b.cfg.Graph.DisabledChannelIDs()
×
UNCOV
571
                if err != nil {
×
572
                        return fmt.Errorf("unable to get disabled channels "+
×
573
                                "ids chans: %v", err)
×
574
                }
×
575

UNCOV
576
                disabledEdges, err := b.cfg.Graph.FetchChanInfos(
×
UNCOV
577
                        disabledChanIDs,
×
UNCOV
578
                )
×
UNCOV
579
                if err != nil {
×
580
                        return fmt.Errorf("unable to fetch disabled channels "+
×
581
                                "edges chans: %v", err)
×
582
                }
×
583

584
                // Ensuring we won't prune our own channel from the graph.
UNCOV
585
                for _, disabledEdge := range disabledEdges {
×
UNCOV
586
                        if !isSelfChannelEdge(disabledEdge.Info) {
×
UNCOV
587
                                chansToPrune[disabledEdge.Info.ChannelID] =
×
UNCOV
588
                                        struct{}{}
×
UNCOV
589
                        }
×
590
                }
591
        }
592

UNCOV
593
        startTime := time.Unix(0, 0)
×
UNCOV
594
        endTime := time.Now().Add(-1 * chanExpiry)
×
UNCOV
595
        oldEdges, err := b.cfg.Graph.ChanUpdatesInHorizon(startTime, endTime)
×
UNCOV
596
        if err != nil {
×
597
                return fmt.Errorf("unable to fetch expired channel updates "+
×
598
                        "chans: %v", err)
×
599
        }
×
600

UNCOV
601
        for _, u := range oldEdges {
×
UNCOV
602
                err = filterPruneChans(u.Info, u.Policy1, u.Policy2)
×
UNCOV
603
                if err != nil {
×
604
                        return fmt.Errorf("error filtering channels to "+
×
605
                                "prune: %w", err)
×
606
                }
×
607
        }
608

UNCOV
609
        log.Infof("Pruning %v zombie channels", len(chansToPrune))
×
UNCOV
610
        if len(chansToPrune) == 0 {
×
UNCOV
611
                return nil
×
UNCOV
612
        }
×
613

614
        // With the set of zombie-like channels obtained, we'll do another pass
615
        // to delete them from the channel graph.
UNCOV
616
        toPrune := make([]uint64, 0, len(chansToPrune))
×
UNCOV
617
        for chanID := range chansToPrune {
×
UNCOV
618
                toPrune = append(toPrune, chanID)
×
UNCOV
619
                log.Tracef("Pruning zombie channel with ChannelID(%v)", chanID)
×
UNCOV
620
        }
×
UNCOV
621
        err = b.cfg.Graph.DeleteChannelEdges(
×
UNCOV
622
                b.cfg.StrictZombiePruning, true, toPrune...,
×
UNCOV
623
        )
×
UNCOV
624
        if err != nil {
×
625
                return fmt.Errorf("unable to delete zombie channels: %w", err)
×
626
        }
×
627

628
        // With the channels pruned, we'll also attempt to prune any nodes that
629
        // were a part of them.
UNCOV
630
        err = b.cfg.Graph.PruneGraphNodes()
×
UNCOV
631
        if err != nil && !errors.Is(err, graphdb.ErrGraphNodesNotFound) {
×
632
                return fmt.Errorf("unable to prune graph nodes: %w", err)
×
633
        }
×
634

UNCOV
635
        return nil
×
636
}
637

638
// networkHandler is the primary goroutine for the Builder. The roles of
639
// this goroutine include answering queries related to the state of the
640
// network, pruning the graph on new block notification, applying network
641
// updates, and registering new topology clients.
642
//
643
// NOTE: This MUST be run as a goroutine.
644
func (b *Builder) networkHandler() {
3✔
645
        defer b.wg.Done()
3✔
646

3✔
647
        graphPruneTicker := time.NewTicker(b.cfg.GraphPruneInterval)
3✔
648
        defer graphPruneTicker.Stop()
3✔
649

3✔
650
        defer b.statTicker.Stop()
3✔
651

3✔
652
        b.stats.Reset()
3✔
653

3✔
654
        for {
6✔
655
                // If there are stats, resume the statTicker.
3✔
656
                if !b.stats.Empty() {
6✔
657
                        b.statTicker.Resume()
3✔
658
                }
3✔
659

660
                select {
3✔
661
                case chainUpdate, ok := <-b.staleBlocks:
2✔
662
                        // If the channel has been closed, then this indicates
2✔
663
                        // the daemon is shutting down, so we exit ourselves.
2✔
664
                        if !ok {
2✔
665
                                return
×
666
                        }
×
667

668
                        // Since this block is stale, we update our best height
669
                        // to the previous block.
670
                        blockHeight := chainUpdate.Height
2✔
671
                        b.bestHeight.Store(blockHeight - 1)
2✔
672

2✔
673
                        // Update the channel graph to reflect that this block
2✔
674
                        // was disconnected.
2✔
675
                        _, err := b.cfg.Graph.DisconnectBlockAtHeight(
2✔
676
                                blockHeight,
2✔
677
                        )
2✔
678
                        if err != nil {
2✔
679
                                log.Errorf("unable to prune graph with stale "+
×
680
                                        "block: %v", err)
×
681
                                continue
×
682
                        }
683

684
                        // TODO(halseth): notify client about the reorg?
685

686
                // A new block has arrived, so we can prune the channel graph
687
                // of any channels which were closed in the block.
688
                case chainUpdate, ok := <-b.newBlocks:
3✔
689
                        // If the channel has been closed, then this indicates
3✔
690
                        // the daemon is shutting down, so we exit ourselves.
3✔
691
                        if !ok {
3✔
692
                                return
×
693
                        }
×
694

695
                        // We'll ensure that any new blocks received attach
696
                        // directly to the end of our main chain. If not, then
697
                        // we've somehow missed some blocks. Here we'll catch
698
                        // up the chain with the latest blocks.
699
                        currentHeight := b.bestHeight.Load()
3✔
700
                        switch {
3✔
701
                        case chainUpdate.Height == currentHeight+1:
3✔
702
                                err := b.updateGraphWithClosedChannels(
3✔
703
                                        chainUpdate,
3✔
704
                                )
3✔
705
                                if err != nil {
3✔
706
                                        log.Errorf("unable to prune graph "+
×
707
                                                "with closed channels: %v", err)
×
708
                                }
×
709

UNCOV
710
                        case chainUpdate.Height > currentHeight+1:
×
UNCOV
711
                                log.Errorf("out of order block: expecting "+
×
UNCOV
712
                                        "height=%v, got height=%v",
×
UNCOV
713
                                        currentHeight+1, chainUpdate.Height)
×
UNCOV
714

×
UNCOV
715
                                err := b.getMissingBlocks(
×
UNCOV
716
                                        currentHeight, chainUpdate,
×
UNCOV
717
                                )
×
UNCOV
718
                                if err != nil {
×
719
                                        log.Errorf("unable to retrieve missing"+
×
720
                                                "blocks: %v", err)
×
721
                                }
×
722

UNCOV
723
                        case chainUpdate.Height < currentHeight+1:
×
UNCOV
724
                                log.Errorf("out of order block: expecting "+
×
UNCOV
725
                                        "height=%v, got height=%v",
×
UNCOV
726
                                        currentHeight+1, chainUpdate.Height)
×
UNCOV
727

×
UNCOV
728
                                log.Infof("Skipping channel pruning since "+
×
UNCOV
729
                                        "received block height %v was already"+
×
UNCOV
730
                                        " processed.", chainUpdate.Height)
×
731
                        }
732

733
                // The graph prune ticker has ticked, so we'll examine the
734
                // state of the known graph to filter out any zombie channels
735
                // for pruning.
736
                case <-graphPruneTicker.C:
×
737
                        if err := b.pruneZombieChans(); err != nil {
×
738
                                log.Errorf("Unable to prune zombies: %v", err)
×
739
                        }
×
740

741
                // Log any stats if we've processed a non-empty number of
742
                // channels, updates, or nodes. We'll only pause the ticker if
743
                // the last window contained no updates to avoid resuming and
744
                // pausing while consecutive windows contain new info.
745
                case <-b.statTicker.Ticks():
2✔
746
                        if !b.stats.Empty() {
4✔
747
                                log.Infof(b.stats.String())
2✔
748
                        } else {
2✔
749
                                b.statTicker.Pause()
×
750
                        }
×
751
                        b.stats.Reset()
2✔
752

753
                // The router has been signalled to exit, to we exit our main
754
                // loop so the wait group can be decremented.
755
                case <-b.quit:
3✔
756
                        return
3✔
757
                }
758
        }
759
}
760

761
// getMissingBlocks walks through all missing blocks and updates the graph
762
// closed channels accordingly.
763
func (b *Builder) getMissingBlocks(currentHeight uint32,
UNCOV
764
        chainUpdate *chainview.FilteredBlock) error {
×
UNCOV
765

×
UNCOV
766
        outdatedHash, err := b.cfg.Chain.GetBlockHash(int64(currentHeight))
×
UNCOV
767
        if err != nil {
×
768
                return err
×
769
        }
×
770

UNCOV
771
        outdatedBlock := &chainntnfs.BlockEpoch{
×
UNCOV
772
                Height: int32(currentHeight),
×
UNCOV
773
                Hash:   outdatedHash,
×
UNCOV
774
        }
×
UNCOV
775

×
UNCOV
776
        epochClient, err := b.cfg.Notifier.RegisterBlockEpochNtfn(
×
UNCOV
777
                outdatedBlock,
×
UNCOV
778
        )
×
UNCOV
779
        if err != nil {
×
780
                return err
×
781
        }
×
UNCOV
782
        defer epochClient.Cancel()
×
UNCOV
783

×
UNCOV
784
        blockDifference := int(chainUpdate.Height - currentHeight)
×
UNCOV
785

×
UNCOV
786
        // We'll walk through all the outdated blocks and make sure we're able
×
UNCOV
787
        // to update the graph with any closed channels from them.
×
UNCOV
788
        for i := 0; i < blockDifference; i++ {
×
UNCOV
789
                var (
×
UNCOV
790
                        missingBlock *chainntnfs.BlockEpoch
×
UNCOV
791
                        ok           bool
×
UNCOV
792
                )
×
UNCOV
793

×
UNCOV
794
                select {
×
UNCOV
795
                case missingBlock, ok = <-epochClient.Epochs:
×
UNCOV
796
                        if !ok {
×
797
                                return nil
×
798
                        }
×
799

800
                case <-b.quit:
×
801
                        return nil
×
802
                }
803

UNCOV
804
                filteredBlock, err := b.cfg.ChainView.FilterBlock(
×
UNCOV
805
                        missingBlock.Hash,
×
UNCOV
806
                )
×
UNCOV
807
                if err != nil {
×
808
                        return err
×
809
                }
×
810

UNCOV
811
                err = b.updateGraphWithClosedChannels(
×
UNCOV
812
                        filteredBlock,
×
UNCOV
813
                )
×
UNCOV
814
                if err != nil {
×
815
                        return err
×
816
                }
×
817
        }
818

UNCOV
819
        return nil
×
820
}
821

822
// updateGraphWithClosedChannels prunes the channel graph of closed channels
823
// that are no longer needed.
824
func (b *Builder) updateGraphWithClosedChannels(
825
        chainUpdate *chainview.FilteredBlock) error {
3✔
826

3✔
827
        // Once a new block arrives, we update our running track of the height
3✔
828
        // of the chain tip.
3✔
829
        blockHeight := chainUpdate.Height
3✔
830

3✔
831
        b.bestHeight.Store(blockHeight)
3✔
832
        log.Infof("Pruning channel graph using block %v (height=%v)",
3✔
833
                chainUpdate.Hash, blockHeight)
3✔
834

3✔
835
        // We're only interested in all prior outputs that have been spent in
3✔
836
        // the block, so collate all the referenced previous outpoints within
3✔
837
        // each tx and input.
3✔
838
        var spentOutputs []*wire.OutPoint
3✔
839
        for _, tx := range chainUpdate.Transactions {
6✔
840
                for _, txIn := range tx.TxIn {
6✔
841
                        spentOutputs = append(spentOutputs,
3✔
842
                                &txIn.PreviousOutPoint)
3✔
843
                }
3✔
844
        }
845

846
        // With the spent outputs gathered, attempt to prune the channel graph,
847
        // also passing in the hash+height of the block being pruned so the
848
        // prune tip can be updated.
849
        chansClosed, err := b.cfg.Graph.PruneGraph(spentOutputs,
3✔
850
                &chainUpdate.Hash, chainUpdate.Height)
3✔
851
        if err != nil {
3✔
852
                log.Errorf("unable to prune routing table: %v", err)
×
853
                return err
×
854
        }
×
855

856
        log.Infof("Block %v (height=%v) closed %v channels", chainUpdate.Hash,
3✔
857
                blockHeight, len(chansClosed))
3✔
858

3✔
859
        return nil
3✔
860
}
861

862
// assertNodeAnnFreshness returns a non-nil error if we have an announcement in
863
// the database for the passed node with a timestamp newer than the passed
864
// timestamp. ErrIgnored will be returned if we already have the node, and
865
// ErrOutdated will be returned if we have a timestamp that's after the new
866
// timestamp.
867
func (b *Builder) assertNodeAnnFreshness(node route.Vertex,
868
        msgTimestamp time.Time) error {
3✔
869

3✔
870
        // If we are not already aware of this node, it means that we don't
3✔
871
        // know about any channel using this node. To avoid a DoS attack by
3✔
872
        // node announcements, we will ignore such nodes. If we do know about
3✔
873
        // this node, check that this update brings info newer than what we
3✔
874
        // already have.
3✔
875
        lastUpdate, exists, err := b.cfg.Graph.HasLightningNode(node)
3✔
876
        if err != nil {
3✔
877
                return errors.Errorf("unable to query for the "+
×
878
                        "existence of node: %v", err)
×
879
        }
×
880
        if !exists {
6✔
881
                return NewErrf(ErrIgnored, "Ignoring node announcement"+
3✔
882
                        " for node not found in channel graph (%x)",
3✔
883
                        node[:])
3✔
884
        }
3✔
885

886
        // If we've reached this point then we're aware of the vertex being
887
        // advertised. So we now check if the new message has a new time stamp,
888
        // if not then we won't accept the new data as it would override newer
889
        // data.
890
        if !lastUpdate.Before(msgTimestamp) {
6✔
891
                return NewErrf(ErrOutdated, "Ignoring outdated "+
3✔
892
                        "announcement for %x", node[:])
3✔
893
        }
3✔
894

895
        return nil
3✔
896
}
897

898
// MarkZombieEdge adds a channel that failed complete validation into the zombie
899
// index so we can avoid having to re-validate it in the future.
900
func (b *Builder) MarkZombieEdge(chanID uint64) error {
×
901
        // If the edge fails validation we'll mark the edge itself as a zombie
×
902
        // so we don't continue to request it. We use the "zero key" for both
×
903
        // node pubkeys so this edge can't be resurrected.
×
904
        var zeroKey [33]byte
×
905
        err := b.cfg.Graph.MarkEdgeZombie(chanID, zeroKey, zeroKey)
×
906
        if err != nil {
×
907
                return fmt.Errorf("unable to mark spent chan(id=%v) as a "+
×
908
                        "zombie: %w", chanID, err)
×
909
        }
×
910

911
        return nil
×
912
}
913

914
// ApplyChannelUpdate validates a channel update and if valid, applies it to the
915
// database. It returns a bool indicating whether the updates were successful.
916
func (b *Builder) ApplyChannelUpdate(msg *lnwire.ChannelUpdate1) bool {
3✔
917
        ch, _, _, err := b.GetChannelByID(msg.ShortChannelID)
3✔
918
        if err != nil {
6✔
919
                log.Errorf("Unable to retrieve channel by id: %v", err)
3✔
920
                return false
3✔
921
        }
3✔
922

923
        var pubKey *btcec.PublicKey
3✔
924

3✔
925
        switch msg.ChannelFlags & lnwire.ChanUpdateDirection {
3✔
926
        case 0:
3✔
927
                pubKey, _ = ch.NodeKey1()
3✔
928

929
        case 1:
2✔
930
                pubKey, _ = ch.NodeKey2()
2✔
931
        }
932

933
        // Exit early if the pubkey cannot be decided.
934
        if pubKey == nil {
3✔
935
                log.Errorf("Unable to decide pubkey with ChannelFlags=%v",
×
936
                        msg.ChannelFlags)
×
937
                return false
×
938
        }
×
939

940
        err = netann.ValidateChannelUpdateAnn(pubKey, ch.Capacity, msg)
3✔
941
        if err != nil {
3✔
942
                log.Errorf("Unable to validate channel update: %v", err)
×
943
                return false
×
944
        }
×
945

946
        err = b.UpdateEdge(&models.ChannelEdgePolicy{
3✔
947
                SigBytes:                  msg.Signature.ToSignatureBytes(),
3✔
948
                ChannelID:                 msg.ShortChannelID.ToUint64(),
3✔
949
                LastUpdate:                time.Unix(int64(msg.Timestamp), 0),
3✔
950
                MessageFlags:              msg.MessageFlags,
3✔
951
                ChannelFlags:              msg.ChannelFlags,
3✔
952
                TimeLockDelta:             msg.TimeLockDelta,
3✔
953
                MinHTLC:                   msg.HtlcMinimumMsat,
3✔
954
                MaxHTLC:                   msg.HtlcMaximumMsat,
3✔
955
                FeeBaseMSat:               lnwire.MilliSatoshi(msg.BaseFee),
3✔
956
                FeeProportionalMillionths: lnwire.MilliSatoshi(msg.FeeRate),
3✔
957
                ExtraOpaqueData:           msg.ExtraOpaqueData,
3✔
958
        })
3✔
959
        if err != nil && !IsError(err, ErrIgnored, ErrOutdated) {
3✔
960
                log.Errorf("Unable to apply channel update: %v", err)
×
961
                return false
×
962
        }
×
963

964
        return true
3✔
965
}
966

967
// AddNode is used to add information about a node to the router database. If
968
// the node with this pubkey is not present in an existing channel, it will
969
// be ignored.
970
//
971
// NOTE: This method is part of the ChannelGraphSource interface.
972
func (b *Builder) AddNode(node *models.LightningNode,
973
        op ...batch.SchedulerOption) error {
3✔
974

3✔
975
        err := b.addNode(node, op...)
3✔
976
        if err != nil {
6✔
977
                logNetworkMsgProcessError(err)
3✔
978

3✔
979
                return err
3✔
980
        }
3✔
981

982
        return nil
3✔
983
}
984

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

3✔
992
        // Before we add the node to the database, we'll check to see if the
3✔
993
        // announcement is "fresh" or not. If it isn't, then we'll return an
3✔
994
        // error.
3✔
995
        err := b.assertNodeAnnFreshness(node.PubKeyBytes, node.LastUpdate)
3✔
996
        if err != nil {
6✔
997
                return err
3✔
998
        }
3✔
999

1000
        if err := b.cfg.Graph.AddLightningNode(node, op...); err != nil {
3✔
1001
                return errors.Errorf("unable to add node %x to the "+
×
1002
                        "graph: %v", node.PubKeyBytes, err)
×
1003
        }
×
1004

1005
        log.Tracef("Updated vertex data for node=%x", node.PubKeyBytes)
3✔
1006
        b.stats.incNumNodeUpdates()
3✔
1007

3✔
1008
        return nil
3✔
1009
}
1010

1011
// AddEdge is used to add edge/channel to the topology of the router, after all
1012
// information about channel will be gathered this edge/channel might be used
1013
// in construction of payment path.
1014
//
1015
// NOTE: This method is part of the ChannelGraphSource interface.
1016
func (b *Builder) AddEdge(edge *models.ChannelEdgeInfo,
1017
        op ...batch.SchedulerOption) error {
3✔
1018

3✔
1019
        err := b.addEdge(edge, op...)
3✔
1020
        if err != nil {
6✔
1021
                logNetworkMsgProcessError(err)
3✔
1022

3✔
1023
                return err
3✔
1024
        }
3✔
1025

1026
        return nil
3✔
1027
}
1028

1029
// addEdge does some validation on the new channel edge against what we
1030
// currently have persisted in the graph, and then adds it to the graph. The
1031
// Chain View is updated with the new edge if it is successfully added to the
1032
// graph. We only persist the channel if we currently dont have it at all in
1033
// our graph.
1034
//
1035
// TODO(elle): this currently also does funding-transaction validation. But this
1036
// should be moved to the gossiper instead.
1037
func (b *Builder) addEdge(edge *models.ChannelEdgeInfo,
1038
        op ...batch.SchedulerOption) error {
3✔
1039

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

3✔
1042
        // Prior to processing the announcement we first check if we
3✔
1043
        // already know of this channel, if so, then we can exit early.
3✔
1044
        _, _, exists, isZombie, err := b.cfg.Graph.HasChannelEdge(
3✔
1045
                edge.ChannelID,
3✔
1046
        )
3✔
1047
        if err != nil && !errors.Is(err, graphdb.ErrGraphNoEdgesFound) {
3✔
1048
                return errors.Errorf("unable to check for edge existence: %v",
×
1049
                        err)
×
1050
        }
×
1051
        if isZombie {
3✔
1052
                return NewErrf(ErrIgnored, "ignoring msg for zombie chan_id=%v",
×
1053
                        edge.ChannelID)
×
1054
        }
×
1055
        if exists {
6✔
1056
                return NewErrf(ErrIgnored, "ignoring msg for known chan_id=%v",
3✔
1057
                        edge.ChannelID)
3✔
1058
        }
3✔
1059

1060
        if err := b.cfg.Graph.AddChannelEdge(edge, op...); err != nil {
3✔
1061
                return fmt.Errorf("unable to add edge: %w", err)
×
1062
        }
×
1063

1064
        b.stats.incNumEdgesDiscovered()
3✔
1065

3✔
1066
        // If AssumeChannelValid is present, of if the SCID is an alias, then
3✔
1067
        // the gossiper would not have done the expensive work of fetching
3✔
1068
        // a funding transaction and validating it. So we won't have the channel
3✔
1069
        // capacity nor the funding script. So we just log and return here.
3✔
1070
        scid := lnwire.NewShortChanIDFromInt(edge.ChannelID)
3✔
1071
        if b.cfg.AssumeChannelValid || b.cfg.IsAlias(scid) {
6✔
1072
                log.Tracef("New channel discovered! Link connects %x and %x "+
3✔
1073
                        "with ChannelID(%v)", edge.NodeKey1Bytes,
3✔
1074
                        edge.NodeKey2Bytes, edge.ChannelID)
3✔
1075

3✔
1076
                return nil
3✔
1077
        }
3✔
1078

1079
        log.Debugf("New channel discovered! Link connects %x and %x with "+
3✔
1080
                "ChannelPoint(%v): chan_id=%v, capacity=%v", edge.NodeKey1Bytes,
3✔
1081
                edge.NodeKey2Bytes, edge.ChannelPoint, edge.ChannelID,
3✔
1082
                edge.Capacity)
3✔
1083

3✔
1084
        // Otherwise, then we expect the funding script to be present on the
3✔
1085
        // edge since it would have been fetched when the gossiper validated the
3✔
1086
        // announcement.
3✔
1087
        fundingPkScript, err := edge.FundingScript.UnwrapOrErr(fmt.Errorf(
3✔
1088
                "expected the funding transaction script to be set",
3✔
1089
        ))
3✔
1090
        if err != nil {
3✔
1091
                return err
×
1092
        }
×
1093

1094
        // As a new edge has been added to the channel graph, we'll update the
1095
        // current UTXO filter within our active FilteredChainView so we are
1096
        // notified if/when this channel is closed.
1097
        filterUpdate := []graphdb.EdgePoint{
3✔
1098
                {
3✔
1099
                        FundingPkScript: fundingPkScript,
3✔
1100
                        OutPoint:        edge.ChannelPoint,
3✔
1101
                },
3✔
1102
        }
3✔
1103

3✔
1104
        err = b.cfg.ChainView.UpdateFilter(filterUpdate, b.bestHeight.Load())
3✔
1105
        if err != nil {
3✔
1106
                return errors.Errorf("unable to update chain "+
×
1107
                        "view: %v", err)
×
1108
        }
×
1109

1110
        return nil
3✔
1111
}
1112

1113
// UpdateEdge is used to update edge information, without this message edge
1114
// considered as not fully constructed.
1115
//
1116
// NOTE: This method is part of the ChannelGraphSource interface.
1117
func (b *Builder) UpdateEdge(update *models.ChannelEdgePolicy,
1118
        op ...batch.SchedulerOption) error {
3✔
1119

3✔
1120
        err := b.updateEdge(update, op...)
3✔
1121
        if err != nil {
6✔
1122
                logNetworkMsgProcessError(err)
3✔
1123

3✔
1124
                return err
3✔
1125
        }
3✔
1126

1127
        return nil
3✔
1128
}
1129

1130
// updateEdge validates the new edge policy against what we currently have
1131
// persisted in the graph, and then applies it to the graph if the update is
1132
// considered fresh enough and if we actually have a channel persisted for the
1133
// given update.
1134
func (b *Builder) updateEdge(policy *models.ChannelEdgePolicy,
1135
        op ...batch.SchedulerOption) error {
3✔
1136

3✔
1137
        log.Debugf("Received ChannelEdgePolicy for channel %v",
3✔
1138
                policy.ChannelID)
3✔
1139

3✔
1140
        // We make sure to hold the mutex for this channel ID, such that no
3✔
1141
        // other goroutine is concurrently doing database accesses for the same
3✔
1142
        // channel ID.
3✔
1143
        b.channelEdgeMtx.Lock(policy.ChannelID)
3✔
1144
        defer b.channelEdgeMtx.Unlock(policy.ChannelID)
3✔
1145

3✔
1146
        edge1Timestamp, edge2Timestamp, exists, isZombie, err :=
3✔
1147
                b.cfg.Graph.HasChannelEdge(policy.ChannelID)
3✔
1148
        if err != nil && !errors.Is(err, graphdb.ErrGraphNoEdgesFound) {
3✔
1149
                return errors.Errorf("unable to check for edge existence: %v",
×
1150
                        err)
×
1151
        }
×
1152

1153
        // If the channel is marked as a zombie in our database, and
1154
        // we consider this a stale update, then we should not apply the
1155
        // policy.
1156
        isStaleUpdate := time.Since(policy.LastUpdate) >
3✔
1157
                b.cfg.ChannelPruneExpiry
3✔
1158

3✔
1159
        if isZombie && isStaleUpdate {
3✔
1160
                return NewErrf(ErrIgnored, "ignoring stale update "+
×
1161
                        "(flags=%v|%v) for zombie chan_id=%v",
×
1162
                        policy.MessageFlags, policy.ChannelFlags,
×
1163
                        policy.ChannelID)
×
1164
        }
×
1165

1166
        // If the channel doesn't exist in our database, we cannot apply the
1167
        // updated policy.
1168
        if !exists {
3✔
UNCOV
1169
                return NewErrf(ErrIgnored, "ignoring update (flags=%v|%v) for "+
×
UNCOV
1170
                        "unknown chan_id=%v", policy.MessageFlags,
×
UNCOV
1171
                        policy.ChannelFlags, policy.ChannelID)
×
UNCOV
1172
        }
×
1173

1174
        log.Debugf("Found edge1Timestamp=%v, edge2Timestamp=%v",
3✔
1175
                edge1Timestamp, edge2Timestamp)
3✔
1176

3✔
1177
        // As edges are directional edge node has a unique policy for the
3✔
1178
        // direction of the edge they control. Therefore, we first check if we
3✔
1179
        // already have the most up-to-date information for that edge. If this
3✔
1180
        // message has a timestamp not strictly newer than what we already know
3✔
1181
        // of we can exit early.
3✔
1182
        switch policy.ChannelFlags & lnwire.ChanUpdateDirection {
3✔
1183
        // A flag set of 0 indicates this is an announcement for the "first"
1184
        // node in the channel.
1185
        case 0:
3✔
1186
                // Ignore outdated message.
3✔
1187
                if !edge1Timestamp.Before(policy.LastUpdate) {
6✔
1188
                        return NewErrf(ErrOutdated, "Ignoring "+
3✔
1189
                                "outdated update (flags=%v|%v) for "+
3✔
1190
                                "known chan_id=%v", policy.MessageFlags,
3✔
1191
                                policy.ChannelFlags, policy.ChannelID)
3✔
1192
                }
3✔
1193

1194
        // Similarly, a flag set of 1 indicates this is an announcement
1195
        // for the "second" node in the channel.
1196
        case 1:
3✔
1197
                // Ignore outdated message.
3✔
1198
                if !edge2Timestamp.Before(policy.LastUpdate) {
5✔
1199
                        return NewErrf(ErrOutdated, "Ignoring "+
2✔
1200
                                "outdated update (flags=%v|%v) for "+
2✔
1201
                                "known chan_id=%v", policy.MessageFlags,
2✔
1202
                                policy.ChannelFlags, policy.ChannelID)
2✔
1203
                }
2✔
1204
        }
1205

1206
        // Now that we know this isn't a stale update, we'll apply the new edge
1207
        // policy to the proper directional edge within the channel graph.
1208
        if err = b.cfg.Graph.UpdateEdgePolicy(policy, op...); err != nil {
3✔
1209
                err := errors.Errorf("unable to add channel: %v", err)
×
1210
                log.Error(err)
×
1211
                return err
×
1212
        }
×
1213

1214
        log.Tracef("New channel update applied: %v",
3✔
1215
                lnutils.SpewLogClosure(policy))
3✔
1216
        b.stats.incNumChannelUpdates()
3✔
1217

3✔
1218
        return nil
3✔
1219
}
1220

1221
// logNetworkMsgProcessError logs the error received from processing a network
1222
// message. It logs as a debug message if the error is not critical.
1223
func logNetworkMsgProcessError(err error) {
3✔
1224
        if IsError(err, ErrIgnored, ErrOutdated) {
6✔
1225
                log.Debugf("process network updates got: %v", err)
3✔
1226

3✔
1227
                return
3✔
1228
        }
3✔
1229

1230
        log.Errorf("process network updates got: %v", err)
×
1231
}
1232

1233
// CurrentBlockHeight returns the block height from POV of the router subsystem.
1234
//
1235
// NOTE: This method is part of the ChannelGraphSource interface.
1236
func (b *Builder) CurrentBlockHeight() (uint32, error) {
3✔
1237
        _, height, err := b.cfg.Chain.GetBestBlock()
3✔
1238
        return uint32(height), err
3✔
1239
}
3✔
1240

1241
// SyncedHeight returns the block height to which the router subsystem currently
1242
// is synced to. This can differ from the above chain height if the goroutine
1243
// responsible for processing the blocks isn't yet up to speed.
1244
func (b *Builder) SyncedHeight() uint32 {
3✔
1245
        return b.bestHeight.Load()
3✔
1246
}
3✔
1247

1248
// GetChannelByID return the channel by the channel id.
1249
//
1250
// NOTE: This method is part of the ChannelGraphSource interface.
1251
func (b *Builder) GetChannelByID(chanID lnwire.ShortChannelID) (
1252
        *models.ChannelEdgeInfo,
1253
        *models.ChannelEdgePolicy,
1254
        *models.ChannelEdgePolicy, error) {
3✔
1255

3✔
1256
        return b.cfg.Graph.FetchChannelEdgesByID(chanID.ToUint64())
3✔
1257
}
3✔
1258

1259
// FetchLightningNode attempts to look up a target node by its identity public
1260
// key. graphdb.ErrGraphNodeNotFound is returned if the node doesn't exist
1261
// within the graph.
1262
//
1263
// NOTE: This method is part of the ChannelGraphSource interface.
1264
func (b *Builder) FetchLightningNode(
1265
        node route.Vertex) (*models.LightningNode, error) {
3✔
1266

3✔
1267
        return b.cfg.Graph.FetchLightningNode(node)
3✔
1268
}
3✔
1269

1270
// ForAllOutgoingChannels is used to iterate over all outgoing channels owned by
1271
// the router.
1272
//
1273
// NOTE: This method is part of the ChannelGraphSource interface.
1274
func (b *Builder) ForAllOutgoingChannels(cb func(*models.ChannelEdgeInfo,
1275
        *models.ChannelEdgePolicy) error) error {
3✔
1276

3✔
1277
        return b.cfg.Graph.ForEachNodeChannel(b.cfg.SelfNode,
3✔
1278
                func(c *models.ChannelEdgeInfo, e *models.ChannelEdgePolicy,
3✔
1279
                        _ *models.ChannelEdgePolicy) error {
6✔
1280

3✔
1281
                        if e == nil {
3✔
1282
                                return fmt.Errorf("channel from self node " +
×
1283
                                        "has no policy")
×
1284
                        }
×
1285

1286
                        return cb(c, e)
3✔
1287
                },
1288
        )
1289
}
1290

1291
// AddProof updates the channel edge info with proof which is needed to
1292
// properly announce the edge to the rest of the network.
1293
//
1294
// NOTE: This method is part of the ChannelGraphSource interface.
1295
func (b *Builder) AddProof(chanID lnwire.ShortChannelID,
1296
        proof *models.ChannelAuthProof) error {
3✔
1297

3✔
1298
        return b.cfg.Graph.AddEdgeProof(chanID, proof)
3✔
1299
}
3✔
1300

1301
// IsStaleNode returns true if the graph source has a node announcement for the
1302
// target node with a more recent timestamp.
1303
//
1304
// NOTE: This method is part of the ChannelGraphSource interface.
1305
func (b *Builder) IsStaleNode(node route.Vertex,
1306
        timestamp time.Time) bool {
3✔
1307

3✔
1308
        // If our attempt to assert that the node announcement is fresh fails,
3✔
1309
        // then we know that this is actually a stale announcement.
3✔
1310
        err := b.assertNodeAnnFreshness(node, timestamp)
3✔
1311
        if err != nil {
6✔
1312
                log.Debugf("Checking stale node %x got %v", node, err)
3✔
1313
                return true
3✔
1314
        }
3✔
1315

1316
        return false
3✔
1317
}
1318

1319
// IsPublicNode determines whether the given vertex is seen as a public node in
1320
// the graph from the graph's source node's point of view.
1321
//
1322
// NOTE: This method is part of the ChannelGraphSource interface.
1323
func (b *Builder) IsPublicNode(node route.Vertex) (bool, error) {
3✔
1324
        return b.cfg.Graph.IsPublicNode(node)
3✔
1325
}
3✔
1326

1327
// IsKnownEdge returns true if the graph source already knows of the passed
1328
// channel ID either as a live or zombie edge.
1329
//
1330
// NOTE: This method is part of the ChannelGraphSource interface.
1331
func (b *Builder) IsKnownEdge(chanID lnwire.ShortChannelID) bool {
3✔
1332
        _, _, exists, isZombie, _ := b.cfg.Graph.HasChannelEdge(
3✔
1333
                chanID.ToUint64(),
3✔
1334
        )
3✔
1335

3✔
1336
        return exists || isZombie
3✔
1337
}
3✔
1338

1339
// IsZombieEdge returns true if the graph source has marked the given channel ID
1340
// as a zombie edge.
1341
//
1342
// NOTE: This method is part of the ChannelGraphSource interface.
1343
func (b *Builder) IsZombieEdge(chanID lnwire.ShortChannelID) (bool, error) {
×
1344
        _, _, _, isZombie, err := b.cfg.Graph.HasChannelEdge(chanID.ToUint64())
×
1345

×
1346
        return isZombie, err
×
1347
}
×
1348

1349
// IsStaleEdgePolicy returns true if the graph source has a channel edge for
1350
// the passed channel ID (and flags) that have a more recent timestamp.
1351
//
1352
// NOTE: This method is part of the ChannelGraphSource interface.
1353
func (b *Builder) IsStaleEdgePolicy(chanID lnwire.ShortChannelID,
1354
        timestamp time.Time, flags lnwire.ChanUpdateChanFlags) bool {
3✔
1355

3✔
1356
        edge1Timestamp, edge2Timestamp, exists, isZombie, err :=
3✔
1357
                b.cfg.Graph.HasChannelEdge(chanID.ToUint64())
3✔
1358
        if err != nil {
3✔
1359
                log.Debugf("Check stale edge policy got error: %v", err)
×
1360
                return false
×
1361
        }
×
1362

1363
        // If we know of the edge as a zombie, then we'll make some additional
1364
        // checks to determine if the new policy is fresh.
1365
        if isZombie {
3✔
1366
                // When running with AssumeChannelValid, we also prune channels
×
1367
                // if both of their edges are disabled. We'll mark the new
×
1368
                // policy as stale if it remains disabled.
×
1369
                if b.cfg.AssumeChannelValid {
×
1370
                        isDisabled := flags&lnwire.ChanUpdateDisabled ==
×
1371
                                lnwire.ChanUpdateDisabled
×
1372
                        if isDisabled {
×
1373
                                return true
×
1374
                        }
×
1375
                }
1376

1377
                // Otherwise, we'll fall back to our usual ChannelPruneExpiry.
1378
                return time.Since(timestamp) > b.cfg.ChannelPruneExpiry
×
1379
        }
1380

1381
        // If we don't know of the edge, then it means it's fresh (thus not
1382
        // stale).
1383
        if !exists {
6✔
1384
                return false
3✔
1385
        }
3✔
1386

1387
        // As edges are directional edge node has a unique policy for the
1388
        // direction of the edge they control. Therefore, we first check if we
1389
        // already have the most up-to-date information for that edge. If so,
1390
        // then we can exit early.
1391
        switch {
3✔
1392
        // A flag set of 0 indicates this is an announcement for the "first"
1393
        // node in the channel.
1394
        case flags&lnwire.ChanUpdateDirection == 0:
3✔
1395
                return !edge1Timestamp.Before(timestamp)
3✔
1396

1397
        // Similarly, a flag set of 1 indicates this is an announcement for the
1398
        // "second" node in the channel.
1399
        case flags&lnwire.ChanUpdateDirection == 1:
3✔
1400
                return !edge2Timestamp.Before(timestamp)
3✔
1401
        }
1402

1403
        return false
×
1404
}
1405

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