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

lightningnetwork / lnd / 13980275562

20 Mar 2025 10:06PM UTC coverage: 58.6% (-10.2%) from 68.789%
13980275562

Pull #9623

github

web-flow
Merge b9b960345 into 09b674508
Pull Request #9623: Size msg test msg

0 of 1518 new or added lines in 42 files covered. (0.0%)

26603 existing lines in 443 files now uncovered.

96807 of 165200 relevant lines covered (58.6%)

1.82 hits per line

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

68.05
/graph/db/graph.go
1
package graphdb
2

3
import (
4
        "bytes"
5
        "crypto/sha256"
6
        "encoding/binary"
7
        "errors"
8
        "fmt"
9
        "io"
10
        "math"
11
        "net"
12
        "sort"
13
        "sync"
14
        "testing"
15
        "time"
16

17
        "github.com/btcsuite/btcd/btcec/v2"
18
        "github.com/btcsuite/btcd/chaincfg/chainhash"
19
        "github.com/btcsuite/btcd/txscript"
20
        "github.com/btcsuite/btcd/wire"
21
        "github.com/btcsuite/btcwallet/walletdb"
22
        "github.com/lightningnetwork/lnd/aliasmgr"
23
        "github.com/lightningnetwork/lnd/batch"
24
        "github.com/lightningnetwork/lnd/graph/db/models"
25
        "github.com/lightningnetwork/lnd/input"
26
        "github.com/lightningnetwork/lnd/kvdb"
27
        "github.com/lightningnetwork/lnd/lnwire"
28
        "github.com/lightningnetwork/lnd/routing/route"
29
)
30

31
var (
32
        // nodeBucket is a bucket which houses all the vertices or nodes within
33
        // the channel graph. This bucket has a single-sub bucket which adds an
34
        // additional index from pubkey -> alias. Within the top-level of this
35
        // bucket, the key space maps a node's compressed public key to the
36
        // serialized information for that node. Additionally, there's a
37
        // special key "source" which stores the pubkey of the source node. The
38
        // source node is used as the starting point for all graph/queries and
39
        // traversals. The graph is formed as a star-graph with the source node
40
        // at the center.
41
        //
42
        // maps: pubKey -> nodeInfo
43
        // maps: source -> selfPubKey
44
        nodeBucket = []byte("graph-node")
45

46
        // nodeUpdateIndexBucket is a sub-bucket of the nodeBucket. This bucket
47
        // will be used to quickly look up the "freshness" of a node's last
48
        // update to the network. The bucket only contains keys, and no values,
49
        // it's mapping:
50
        //
51
        // maps: updateTime || nodeID -> nil
52
        nodeUpdateIndexBucket = []byte("graph-node-update-index")
53

54
        // sourceKey is a special key that resides within the nodeBucket. The
55
        // sourceKey maps a key to the public key of the "self node".
56
        sourceKey = []byte("source")
57

58
        // aliasIndexBucket is a sub-bucket that's nested within the main
59
        // nodeBucket. This bucket maps the public key of a node to its
60
        // current alias. This bucket is provided as it can be used within a
61
        // future UI layer to add an additional degree of confirmation.
62
        aliasIndexBucket = []byte("alias")
63

64
        // edgeBucket is a bucket which houses all of the edge or channel
65
        // information within the channel graph. This bucket essentially acts
66
        // as an adjacency list, which in conjunction with a range scan, can be
67
        // used to iterate over all the incoming and outgoing edges for a
68
        // particular node. Key in the bucket use a prefix scheme which leads
69
        // with the node's public key and sends with the compact edge ID.
70
        // For each chanID, there will be two entries within the bucket, as the
71
        // graph is directed: nodes may have different policies w.r.t to fees
72
        // for their respective directions.
73
        //
74
        // maps: pubKey || chanID -> channel edge policy for node
75
        edgeBucket = []byte("graph-edge")
76

77
        // unknownPolicy is represented as an empty slice. It is
78
        // used as the value in edgeBucket for unknown channel edge policies.
79
        // Unknown policies are still stored in the database to enable efficient
80
        // lookup of incoming channel edges.
81
        unknownPolicy = []byte{}
82

83
        // chanStart is an array of all zero bytes which is used to perform
84
        // range scans within the edgeBucket to obtain all of the outgoing
85
        // edges for a particular node.
86
        chanStart [8]byte
87

88
        // edgeIndexBucket is an index which can be used to iterate all edges
89
        // in the bucket, grouping them according to their in/out nodes.
90
        // Additionally, the items in this bucket also contain the complete
91
        // edge information for a channel. The edge information includes the
92
        // capacity of the channel, the nodes that made the channel, etc. This
93
        // bucket resides within the edgeBucket above. Creation of an edge
94
        // proceeds in two phases: first the edge is added to the edge index,
95
        // afterwards the edgeBucket can be updated with the latest details of
96
        // the edge as they are announced on the network.
97
        //
98
        // maps: chanID -> pubKey1 || pubKey2 || restofEdgeInfo
99
        edgeIndexBucket = []byte("edge-index")
100

101
        // edgeUpdateIndexBucket is a sub-bucket of the main edgeBucket. This
102
        // bucket contains an index which allows us to gauge the "freshness" of
103
        // a channel's last updates.
104
        //
105
        // maps: updateTime || chanID -> nil
106
        edgeUpdateIndexBucket = []byte("edge-update-index")
107

108
        // channelPointBucket maps a channel's full outpoint (txid:index) to
109
        // its short 8-byte channel ID. This bucket resides within the
110
        // edgeBucket above, and can be used to quickly remove an edge due to
111
        // the outpoint being spent, or to query for existence of a channel.
112
        //
113
        // maps: outPoint -> chanID
114
        channelPointBucket = []byte("chan-index")
115

116
        // zombieBucket is a sub-bucket of the main edgeBucket bucket
117
        // responsible for maintaining an index of zombie channels. Each entry
118
        // exists within the bucket as follows:
119
        //
120
        // maps: chanID -> pubKey1 || pubKey2
121
        //
122
        // The chanID represents the channel ID of the edge that is marked as a
123
        // zombie and is used as the key, which maps to the public keys of the
124
        // edge's participants.
125
        zombieBucket = []byte("zombie-index")
126

127
        // disabledEdgePolicyBucket is a sub-bucket of the main edgeBucket
128
        // bucket responsible for maintaining an index of disabled edge
129
        // policies. Each entry exists within the bucket as follows:
130
        //
131
        // maps: <chanID><direction> -> []byte{}
132
        //
133
        // The chanID represents the channel ID of the edge and the direction is
134
        // one byte representing the direction of the edge. The main purpose of
135
        // this index is to allow pruning disabled channels in a fast way without
136
        // the need to iterate all over the graph.
137
        disabledEdgePolicyBucket = []byte("disabled-edge-policy-index")
138

139
        // graphMetaBucket is a top-level bucket which stores various meta-deta
140
        // related to the on-disk channel graph. Data stored in this bucket
141
        // includes the block to which the graph has been synced to, the total
142
        // number of channels, etc.
143
        graphMetaBucket = []byte("graph-meta")
144

145
        // pruneLogBucket is a bucket within the graphMetaBucket that stores
146
        // a mapping from the block height to the hash for the blocks used to
147
        // prune the graph.
148
        // Once a new block is discovered, any channels that have been closed
149
        // (by spending the outpoint) can safely be removed from the graph, and
150
        // the block is added to the prune log. We need to keep such a log for
151
        // the case where a reorg happens, and we must "rewind" the state of the
152
        // graph by removing channels that were previously confirmed. In such a
153
        // case we'll remove all entries from the prune log with a block height
154
        // that no longer exists.
155
        pruneLogBucket = []byte("prune-log")
156

157
        // closedScidBucket is a top-level bucket that stores scids for
158
        // channels that we know to be closed. This is used so that we don't
159
        // need to perform expensive validation checks if we receive a channel
160
        // announcement for the channel again.
161
        //
162
        // maps: scid -> []byte{}
163
        closedScidBucket = []byte("closed-scid")
164
)
165

166
const (
167
        // MaxAllowedExtraOpaqueBytes is the largest amount of opaque bytes that
168
        // we'll permit to be written to disk. We limit this as otherwise, it
169
        // would be possible for a node to create a ton of updates and slowly
170
        // fill our disk, and also waste bandwidth due to relaying.
171
        MaxAllowedExtraOpaqueBytes = 10000
172
)
173

174
// ChannelGraph is a persistent, on-disk graph representation of the Lightning
175
// Network. This struct can be used to implement path finding algorithms on top
176
// of, and also to update a node's view based on information received from the
177
// p2p network. Internally, the graph is stored using a modified adjacency list
178
// representation with some added object interaction possible with each
179
// serialized edge/node. The graph is stored is directed, meaning that are two
180
// edges stored for each channel: an inbound/outbound edge for each node pair.
181
// Nodes, edges, and edge information can all be added to the graph
182
// independently. Edge removal results in the deletion of all edge information
183
// for that edge.
184
type ChannelGraph struct {
185
        db kvdb.Backend
186

187
        // cacheMu guards all caches (rejectCache, chanCache, graphCache). If
188
        // this mutex will be acquired at the same time as the DB mutex then
189
        // the cacheMu MUST be acquired first to prevent deadlock.
190
        cacheMu     sync.RWMutex
191
        rejectCache *rejectCache
192
        chanCache   *channelCache
193
        graphCache  *GraphCache
194

195
        chanScheduler batch.Scheduler
196
        nodeScheduler batch.Scheduler
197
}
198

199
// NewChannelGraph allocates a new ChannelGraph backed by a DB instance. The
200
// returned instance has its own unique reject cache and channel cache.
201
func NewChannelGraph(db kvdb.Backend, options ...OptionModifier) (*ChannelGraph,
202
        error) {
3✔
203

3✔
204
        opts := DefaultOptions()
3✔
205
        for _, o := range options {
6✔
206
                o(opts)
3✔
207
        }
3✔
208

209
        if !opts.NoMigration {
6✔
210
                if err := initChannelGraph(db); err != nil {
3✔
211
                        return nil, err
×
212
                }
×
213
        }
214

215
        g := &ChannelGraph{
3✔
216
                db:          db,
3✔
217
                rejectCache: newRejectCache(opts.RejectCacheSize),
3✔
218
                chanCache:   newChannelCache(opts.ChannelCacheSize),
3✔
219
        }
3✔
220
        g.chanScheduler = batch.NewTimeScheduler(
3✔
221
                db, &g.cacheMu, opts.BatchCommitInterval,
3✔
222
        )
3✔
223
        g.nodeScheduler = batch.NewTimeScheduler(
3✔
224
                db, nil, opts.BatchCommitInterval,
3✔
225
        )
3✔
226

3✔
227
        // The graph cache can be turned off (e.g. for mobile users) for a
3✔
228
        // speed/memory usage tradeoff.
3✔
229
        if opts.UseGraphCache {
6✔
230
                g.graphCache = NewGraphCache(opts.PreAllocCacheNumNodes)
3✔
231
                startTime := time.Now()
3✔
232
                log.Debugf("Populating in-memory channel graph, this might " +
3✔
233
                        "take a while...")
3✔
234

3✔
235
                err := g.ForEachNodeCacheable(func(node route.Vertex,
3✔
236
                        features *lnwire.FeatureVector) error {
6✔
237

3✔
238
                        g.graphCache.AddNodeFeatures(node, features)
3✔
239

3✔
240
                        return nil
3✔
241
                })
3✔
242
                if err != nil {
3✔
243
                        return nil, err
×
244
                }
×
245

246
                err = g.ForEachChannel(func(info *models.ChannelEdgeInfo,
3✔
247
                        policy1, policy2 *models.ChannelEdgePolicy) error {
6✔
248

3✔
249
                        g.graphCache.AddChannel(info, policy1, policy2)
3✔
250

3✔
251
                        return nil
3✔
252
                })
3✔
253
                if err != nil {
3✔
254
                        return nil, err
×
255
                }
×
256

257
                log.Debugf("Finished populating in-memory channel graph (took "+
3✔
258
                        "%v, %s)", time.Since(startTime), g.graphCache.Stats())
3✔
259
        }
260

261
        return g, nil
3✔
262
}
263

264
// channelMapKey is the key structure used for storing channel edge policies.
265
type channelMapKey struct {
266
        nodeKey route.Vertex
267
        chanID  [8]byte
268
}
269

270
// getChannelMap loads all channel edge policies from the database and stores
271
// them in a map.
272
func (c *ChannelGraph) getChannelMap(edges kvdb.RBucket) (
273
        map[channelMapKey]*models.ChannelEdgePolicy, error) {
3✔
274

3✔
275
        // Create a map to store all channel edge policies.
3✔
276
        channelMap := make(map[channelMapKey]*models.ChannelEdgePolicy)
3✔
277

3✔
278
        err := kvdb.ForAll(edges, func(k, edgeBytes []byte) error {
6✔
279
                // Skip embedded buckets.
3✔
280
                if bytes.Equal(k, edgeIndexBucket) ||
3✔
281
                        bytes.Equal(k, edgeUpdateIndexBucket) ||
3✔
282
                        bytes.Equal(k, zombieBucket) ||
3✔
283
                        bytes.Equal(k, disabledEdgePolicyBucket) ||
3✔
284
                        bytes.Equal(k, channelPointBucket) {
6✔
285

3✔
286
                        return nil
3✔
287
                }
3✔
288

289
                // Validate key length.
290
                if len(k) != 33+8 {
3✔
291
                        return fmt.Errorf("invalid edge key %x encountered", k)
×
292
                }
×
293

294
                var key channelMapKey
3✔
295
                copy(key.nodeKey[:], k[:33])
3✔
296
                copy(key.chanID[:], k[33:])
3✔
297

3✔
298
                // No need to deserialize unknown policy.
3✔
299
                if bytes.Equal(edgeBytes, unknownPolicy) {
3✔
300
                        return nil
×
301
                }
×
302

303
                edgeReader := bytes.NewReader(edgeBytes)
3✔
304
                edge, err := deserializeChanEdgePolicyRaw(
3✔
305
                        edgeReader,
3✔
306
                )
3✔
307

3✔
308
                switch {
3✔
309
                // If the db policy was missing an expected optional field, we
310
                // return nil as if the policy was unknown.
311
                case err == ErrEdgePolicyOptionalFieldNotFound:
×
312
                        return nil
×
313

314
                case err != nil:
×
315
                        return err
×
316
                }
317

318
                channelMap[key] = edge
3✔
319

3✔
320
                return nil
3✔
321
        })
322
        if err != nil {
3✔
323
                return nil, err
×
324
        }
×
325

326
        return channelMap, nil
3✔
327
}
328

329
var graphTopLevelBuckets = [][]byte{
330
        nodeBucket,
331
        edgeBucket,
332
        graphMetaBucket,
333
        closedScidBucket,
334
}
335

336
// Wipe completely deletes all saved state within all used buckets within the
337
// database. The deletion is done in a single transaction, therefore this
338
// operation is fully atomic.
339
func (c *ChannelGraph) Wipe() error {
×
340
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
×
341
                for _, tlb := range graphTopLevelBuckets {
×
342
                        err := tx.DeleteTopLevelBucket(tlb)
×
343
                        if err != nil && err != kvdb.ErrBucketNotFound {
×
344
                                return err
×
345
                        }
×
346
                }
347
                return nil
×
348
        }, func() {})
×
349
        if err != nil {
×
350
                return err
×
351
        }
×
352

353
        return initChannelGraph(c.db)
×
354
}
355

356
// createChannelDB creates and initializes a fresh version of  In
357
// the case that the target path has not yet been created or doesn't yet exist,
358
// then the path is created. Additionally, all required top-level buckets used
359
// within the database are created.
360
func initChannelGraph(db kvdb.Backend) error {
3✔
361
        err := kvdb.Update(db, func(tx kvdb.RwTx) error {
6✔
362
                for _, tlb := range graphTopLevelBuckets {
6✔
363
                        if _, err := tx.CreateTopLevelBucket(tlb); err != nil {
3✔
364
                                return err
×
365
                        }
×
366
                }
367

368
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
369
                _, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
3✔
370
                if err != nil {
3✔
371
                        return err
×
372
                }
×
373
                _, err = nodes.CreateBucketIfNotExists(nodeUpdateIndexBucket)
3✔
374
                if err != nil {
3✔
375
                        return err
×
376
                }
×
377

378
                edges := tx.ReadWriteBucket(edgeBucket)
3✔
379
                _, err = edges.CreateBucketIfNotExists(edgeIndexBucket)
3✔
380
                if err != nil {
3✔
381
                        return err
×
382
                }
×
383
                _, err = edges.CreateBucketIfNotExists(edgeUpdateIndexBucket)
3✔
384
                if err != nil {
3✔
385
                        return err
×
386
                }
×
387
                _, err = edges.CreateBucketIfNotExists(channelPointBucket)
3✔
388
                if err != nil {
3✔
389
                        return err
×
390
                }
×
391
                _, err = edges.CreateBucketIfNotExists(zombieBucket)
3✔
392
                if err != nil {
3✔
393
                        return err
×
394
                }
×
395

396
                graphMeta := tx.ReadWriteBucket(graphMetaBucket)
3✔
397
                _, err = graphMeta.CreateBucketIfNotExists(pruneLogBucket)
3✔
398
                return err
3✔
399
        }, func() {})
3✔
400
        if err != nil {
3✔
401
                return fmt.Errorf("unable to create new channel graph: %w", err)
×
402
        }
×
403

404
        return nil
3✔
405
}
406

407
// AddrsForNode returns all known addresses for the target node public key that
408
// the graph DB is aware of. The returned boolean indicates if the given node is
409
// unknown to the graph DB or not.
410
//
411
// NOTE: this is part of the channeldb.AddrSource interface.
412
func (c *ChannelGraph) AddrsForNode(nodePub *btcec.PublicKey) (bool, []net.Addr,
413
        error) {
3✔
414

3✔
415
        pubKey, err := route.NewVertexFromBytes(nodePub.SerializeCompressed())
3✔
416
        if err != nil {
3✔
417
                return false, nil, err
×
418
        }
×
419

420
        node, err := c.FetchLightningNode(pubKey)
3✔
421
        // We don't consider it an error if the graph is unaware of the node.
3✔
422
        switch {
3✔
423
        case err != nil && !errors.Is(err, ErrGraphNodeNotFound):
×
424
                return false, nil, err
×
425

426
        case errors.Is(err, ErrGraphNodeNotFound):
3✔
427
                return false, nil, nil
3✔
428
        }
429

430
        return true, node.Addresses, nil
3✔
431
}
432

433
// ForEachChannel iterates through all the channel edges stored within the
434
// graph and invokes the passed callback for each edge. The callback takes two
435
// edges as since this is a directed graph, both the in/out edges are visited.
436
// If the callback returns an error, then the transaction is aborted and the
437
// iteration stops early.
438
//
439
// NOTE: If an edge can't be found, or wasn't advertised, then a nil pointer
440
// for that particular channel edge routing policy will be passed into the
441
// callback.
442
func (c *ChannelGraph) ForEachChannel(cb func(*models.ChannelEdgeInfo,
443
        *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
3✔
444

3✔
445
        return c.db.View(func(tx kvdb.RTx) error {
6✔
446
                edges := tx.ReadBucket(edgeBucket)
3✔
447
                if edges == nil {
3✔
448
                        return ErrGraphNoEdgesFound
×
449
                }
×
450

451
                // First, load all edges in memory indexed by node and channel
452
                // id.
453
                channelMap, err := c.getChannelMap(edges)
3✔
454
                if err != nil {
3✔
455
                        return err
×
456
                }
×
457

458
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
459
                if edgeIndex == nil {
3✔
460
                        return ErrGraphNoEdgesFound
×
461
                }
×
462

463
                // Load edge index, recombine each channel with the policies
464
                // loaded above and invoke the callback.
465
                return kvdb.ForAll(
3✔
466
                        edgeIndex, func(k, edgeInfoBytes []byte) error {
6✔
467
                                var chanID [8]byte
3✔
468
                                copy(chanID[:], k)
3✔
469

3✔
470
                                edgeInfoReader := bytes.NewReader(edgeInfoBytes)
3✔
471
                                info, err := deserializeChanEdgeInfo(
3✔
472
                                        edgeInfoReader,
3✔
473
                                )
3✔
474
                                if err != nil {
3✔
475
                                        return err
×
476
                                }
×
477

478
                                policy1 := channelMap[channelMapKey{
3✔
479
                                        nodeKey: info.NodeKey1Bytes,
3✔
480
                                        chanID:  chanID,
3✔
481
                                }]
3✔
482

3✔
483
                                policy2 := channelMap[channelMapKey{
3✔
484
                                        nodeKey: info.NodeKey2Bytes,
3✔
485
                                        chanID:  chanID,
3✔
486
                                }]
3✔
487

3✔
488
                                return cb(&info, policy1, policy2)
3✔
489
                        },
490
                )
491
        }, func() {})
3✔
492
}
493

494
// forEachNodeDirectedChannel iterates through all channels of a given node,
495
// executing the passed callback on the directed edge representing the channel
496
// and its incoming policy. If the callback returns an error, then the iteration
497
// is halted with the error propagated back up to the caller. An optional read
498
// transaction may be provided. If none is provided, a new one will be created.
499
//
500
// Unknown policies are passed into the callback as nil values.
501
func (c *ChannelGraph) forEachNodeDirectedChannel(tx kvdb.RTx,
502
        node route.Vertex, cb func(channel *DirectedChannel) error) error {
3✔
503

3✔
504
        if c.graphCache != nil {
6✔
505
                return c.graphCache.ForEachChannel(node, cb)
3✔
506
        }
3✔
507

508
        // Fallback that uses the database.
509
        toNodeCallback := func() route.Vertex {
6✔
510
                return node
3✔
511
        }
3✔
512
        toNodeFeatures, err := c.fetchNodeFeatures(tx, node)
3✔
513
        if err != nil {
3✔
514
                return err
×
515
        }
×
516

517
        dbCallback := func(tx kvdb.RTx, e *models.ChannelEdgeInfo, p1,
3✔
518
                p2 *models.ChannelEdgePolicy) error {
6✔
519

3✔
520
                var cachedInPolicy *models.CachedEdgePolicy
3✔
521
                if p2 != nil {
6✔
522
                        cachedInPolicy = models.NewCachedPolicy(p2)
3✔
523
                        cachedInPolicy.ToNodePubKey = toNodeCallback
3✔
524
                        cachedInPolicy.ToNodeFeatures = toNodeFeatures
3✔
525
                }
3✔
526

527
                var inboundFee lnwire.Fee
3✔
528
                if p1 != nil {
6✔
529
                        // Extract inbound fee. If there is a decoding error,
3✔
530
                        // skip this edge.
3✔
531
                        _, err := p1.ExtraOpaqueData.ExtractRecords(&inboundFee)
3✔
532
                        if err != nil {
3✔
UNCOV
533
                                return nil
×
UNCOV
534
                        }
×
535
                }
536

537
                directedChannel := &DirectedChannel{
3✔
538
                        ChannelID:    e.ChannelID,
3✔
539
                        IsNode1:      node == e.NodeKey1Bytes,
3✔
540
                        OtherNode:    e.NodeKey2Bytes,
3✔
541
                        Capacity:     e.Capacity,
3✔
542
                        OutPolicySet: p1 != nil,
3✔
543
                        InPolicy:     cachedInPolicy,
3✔
544
                        InboundFee:   inboundFee,
3✔
545
                }
3✔
546

3✔
547
                if node == e.NodeKey2Bytes {
6✔
548
                        directedChannel.OtherNode = e.NodeKey1Bytes
3✔
549
                }
3✔
550

551
                return cb(directedChannel)
3✔
552
        }
553
        return nodeTraversal(tx, node[:], c.db, dbCallback)
3✔
554
}
555

556
// fetchNodeFeatures returns the features of a given node. If no features are
557
// known for the node, an empty feature vector is returned. An optional read
558
// transaction may be provided. If none is provided, a new one will be created.
559
func (c *ChannelGraph) fetchNodeFeatures(tx kvdb.RTx,
560
        node route.Vertex) (*lnwire.FeatureVector, error) {
3✔
561

3✔
562
        if c.graphCache != nil {
6✔
563
                return c.graphCache.GetFeatures(node), nil
3✔
564
        }
3✔
565

566
        // Fallback that uses the database.
567
        targetNode, err := c.FetchLightningNodeTx(tx, node)
3✔
568
        switch err {
3✔
569
        // If the node exists and has features, return them directly.
570
        case nil:
3✔
571
                return targetNode.Features, nil
3✔
572

573
        // If we couldn't find a node announcement, populate a blank feature
574
        // vector.
UNCOV
575
        case ErrGraphNodeNotFound:
×
UNCOV
576
                return lnwire.EmptyFeatureVector(), nil
×
577

578
        // Otherwise, bubble the error up.
579
        default:
×
580
                return nil, err
×
581
        }
582
}
583

584
// ForEachNodeDirectedChannel iterates through all channels of a given node,
585
// executing the passed callback on the directed edge representing the channel
586
// and its incoming policy. If the callback returns an error, then the iteration
587
// is halted with the error propagated back up to the caller. If the graphCache
588
// is available, then it will be used to retrieve the node's channels instead
589
// of the database.
590
//
591
// Unknown policies are passed into the callback as nil values.
592
//
593
// NOTE: this is part of the graphdb.NodeTraverser interface.
594
func (c *ChannelGraph) ForEachNodeDirectedChannel(nodePub route.Vertex,
595
        cb func(channel *DirectedChannel) error) error {
3✔
596

3✔
597
        return c.forEachNodeDirectedChannel(nil, nodePub, cb)
3✔
598
}
3✔
599

600
// FetchNodeFeatures returns the features of the given node. If no features are
601
// known for the node, an empty feature vector is returned.
602
// If the graphCache is available, then it will be used to retrieve the node's
603
// features instead of the database.
604
//
605
// NOTE: this is part of the graphdb.NodeTraverser interface.
606
func (c *ChannelGraph) FetchNodeFeatures(nodePub route.Vertex) (
607
        *lnwire.FeatureVector, error) {
3✔
608

3✔
609
        return c.fetchNodeFeatures(nil, nodePub)
3✔
610
}
3✔
611

612
// ForEachNodeCached is similar to forEachNode, but it utilizes the channel
613
// graph cache instead. Note that this doesn't return all the information the
614
// regular forEachNode method does.
615
//
616
// NOTE: The callback contents MUST not be modified.
617
func (c *ChannelGraph) ForEachNodeCached(cb func(node route.Vertex,
UNCOV
618
        chans map[uint64]*DirectedChannel) error) error {
×
UNCOV
619

×
UNCOV
620
        if c.graphCache != nil {
×
621
                return c.graphCache.ForEachNode(cb)
×
622
        }
×
623

624
        // Otherwise call back to a version that uses the database directly.
625
        // We'll iterate over each node, then the set of channels for each
626
        // node, and construct a similar callback functiopn signature as the
627
        // main funcotin expects.
UNCOV
628
        return c.forEachNode(func(tx kvdb.RTx,
×
UNCOV
629
                node *models.LightningNode) error {
×
UNCOV
630

×
UNCOV
631
                channels := make(map[uint64]*DirectedChannel)
×
UNCOV
632

×
UNCOV
633
                err := c.ForEachNodeChannelTx(tx, node.PubKeyBytes,
×
UNCOV
634
                        func(tx kvdb.RTx, e *models.ChannelEdgeInfo,
×
UNCOV
635
                                p1 *models.ChannelEdgePolicy,
×
UNCOV
636
                                p2 *models.ChannelEdgePolicy) error {
×
UNCOV
637

×
UNCOV
638
                                toNodeCallback := func() route.Vertex {
×
639
                                        return node.PubKeyBytes
×
640
                                }
×
UNCOV
641
                                toNodeFeatures, err := c.fetchNodeFeatures(
×
UNCOV
642
                                        tx, node.PubKeyBytes,
×
UNCOV
643
                                )
×
UNCOV
644
                                if err != nil {
×
645
                                        return err
×
646
                                }
×
647

UNCOV
648
                                var cachedInPolicy *models.CachedEdgePolicy
×
UNCOV
649
                                if p2 != nil {
×
UNCOV
650
                                        cachedInPolicy =
×
UNCOV
651
                                                models.NewCachedPolicy(p2)
×
UNCOV
652
                                        cachedInPolicy.ToNodePubKey =
×
UNCOV
653
                                                toNodeCallback
×
UNCOV
654
                                        cachedInPolicy.ToNodeFeatures =
×
UNCOV
655
                                                toNodeFeatures
×
UNCOV
656
                                }
×
657

UNCOV
658
                                directedChannel := &DirectedChannel{
×
UNCOV
659
                                        ChannelID: e.ChannelID,
×
UNCOV
660
                                        IsNode1: node.PubKeyBytes ==
×
UNCOV
661
                                                e.NodeKey1Bytes,
×
UNCOV
662
                                        OtherNode:    e.NodeKey2Bytes,
×
UNCOV
663
                                        Capacity:     e.Capacity,
×
UNCOV
664
                                        OutPolicySet: p1 != nil,
×
UNCOV
665
                                        InPolicy:     cachedInPolicy,
×
UNCOV
666
                                }
×
UNCOV
667

×
UNCOV
668
                                if node.PubKeyBytes == e.NodeKey2Bytes {
×
UNCOV
669
                                        directedChannel.OtherNode =
×
UNCOV
670
                                                e.NodeKey1Bytes
×
UNCOV
671
                                }
×
672

UNCOV
673
                                channels[e.ChannelID] = directedChannel
×
UNCOV
674

×
UNCOV
675
                                return nil
×
676
                        })
UNCOV
677
                if err != nil {
×
678
                        return err
×
679
                }
×
680

UNCOV
681
                return cb(node.PubKeyBytes, channels)
×
682
        })
683
}
684

685
// DisabledChannelIDs returns the channel ids of disabled channels.
686
// A channel is disabled when two of the associated ChanelEdgePolicies
687
// have their disabled bit on.
UNCOV
688
func (c *ChannelGraph) DisabledChannelIDs() ([]uint64, error) {
×
UNCOV
689
        var disabledChanIDs []uint64
×
UNCOV
690
        var chanEdgeFound map[uint64]struct{}
×
UNCOV
691

×
UNCOV
692
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
×
UNCOV
693
                edges := tx.ReadBucket(edgeBucket)
×
UNCOV
694
                if edges == nil {
×
695
                        return ErrGraphNoEdgesFound
×
696
                }
×
697

UNCOV
698
                disabledEdgePolicyIndex := edges.NestedReadBucket(
×
UNCOV
699
                        disabledEdgePolicyBucket,
×
UNCOV
700
                )
×
UNCOV
701
                if disabledEdgePolicyIndex == nil {
×
UNCOV
702
                        return nil
×
UNCOV
703
                }
×
704

705
                // We iterate over all disabled policies and we add each channel
706
                // that has more than one disabled policy to disabledChanIDs
707
                // array.
UNCOV
708
                return disabledEdgePolicyIndex.ForEach(
×
UNCOV
709
                        func(k, v []byte) error {
×
UNCOV
710
                                chanID := byteOrder.Uint64(k[:8])
×
UNCOV
711
                                _, edgeFound := chanEdgeFound[chanID]
×
UNCOV
712
                                if edgeFound {
×
UNCOV
713
                                        delete(chanEdgeFound, chanID)
×
UNCOV
714
                                        disabledChanIDs = append(
×
UNCOV
715
                                                disabledChanIDs, chanID,
×
UNCOV
716
                                        )
×
UNCOV
717

×
UNCOV
718
                                        return nil
×
UNCOV
719
                                }
×
720

UNCOV
721
                                chanEdgeFound[chanID] = struct{}{}
×
UNCOV
722

×
UNCOV
723
                                return nil
×
724
                        },
725
                )
UNCOV
726
        }, func() {
×
UNCOV
727
                disabledChanIDs = nil
×
UNCOV
728
                chanEdgeFound = make(map[uint64]struct{})
×
UNCOV
729
        })
×
UNCOV
730
        if err != nil {
×
731
                return nil, err
×
732
        }
×
733

UNCOV
734
        return disabledChanIDs, nil
×
735
}
736

737
// ForEachNode iterates through all the stored vertices/nodes in the graph,
738
// executing the passed callback with each node encountered. If the callback
739
// returns an error, then the transaction is aborted and the iteration stops
740
// early. Any operations performed on the NodeTx passed to the call-back are
741
// executed under the same read transaction and so, methods on the NodeTx object
742
// _MUST_ only be called from within the call-back.
743
func (c *ChannelGraph) ForEachNode(cb func(tx NodeRTx) error) error {
3✔
744
        return c.forEachNode(func(tx kvdb.RTx,
3✔
745
                node *models.LightningNode) error {
6✔
746

3✔
747
                return cb(newChanGraphNodeTx(tx, c, node))
3✔
748
        })
3✔
749
}
750

751
// forEachNode iterates through all the stored vertices/nodes in the graph,
752
// executing the passed callback with each node encountered. If the callback
753
// returns an error, then the transaction is aborted and the iteration stops
754
// early.
755
//
756
// TODO(roasbeef): add iterator interface to allow for memory efficient graph
757
// traversal when graph gets mega.
758
func (c *ChannelGraph) forEachNode(
759
        cb func(kvdb.RTx, *models.LightningNode) error) error {
3✔
760

3✔
761
        traversal := func(tx kvdb.RTx) error {
6✔
762
                // First grab the nodes bucket which stores the mapping from
3✔
763
                // pubKey to node information.
3✔
764
                nodes := tx.ReadBucket(nodeBucket)
3✔
765
                if nodes == nil {
3✔
766
                        return ErrGraphNotFound
×
767
                }
×
768

769
                return nodes.ForEach(func(pubKey, nodeBytes []byte) error {
6✔
770
                        // If this is the source key, then we skip this
3✔
771
                        // iteration as the value for this key is a pubKey
3✔
772
                        // rather than raw node information.
3✔
773
                        if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
6✔
774
                                return nil
3✔
775
                        }
3✔
776

777
                        nodeReader := bytes.NewReader(nodeBytes)
3✔
778
                        node, err := deserializeLightningNode(nodeReader)
3✔
779
                        if err != nil {
3✔
780
                                return err
×
781
                        }
×
782

783
                        // Execute the callback, the transaction will abort if
784
                        // this returns an error.
785
                        return cb(tx, &node)
3✔
786
                })
787
        }
788

789
        return kvdb.View(c.db, traversal, func() {})
6✔
790
}
791

792
// ForEachNodeCacheable iterates through all the stored vertices/nodes in the
793
// graph, executing the passed callback with each node encountered. If the
794
// callback returns an error, then the transaction is aborted and the iteration
795
// stops early.
796
func (c *ChannelGraph) ForEachNodeCacheable(cb func(route.Vertex,
797
        *lnwire.FeatureVector) error) error {
3✔
798

3✔
799
        traversal := func(tx kvdb.RTx) error {
6✔
800
                // First grab the nodes bucket which stores the mapping from
3✔
801
                // pubKey to node information.
3✔
802
                nodes := tx.ReadBucket(nodeBucket)
3✔
803
                if nodes == nil {
3✔
804
                        return ErrGraphNotFound
×
805
                }
×
806

807
                return nodes.ForEach(func(pubKey, nodeBytes []byte) error {
6✔
808
                        // If this is the source key, then we skip this
3✔
809
                        // iteration as the value for this key is a pubKey
3✔
810
                        // rather than raw node information.
3✔
811
                        if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
6✔
812
                                return nil
3✔
813
                        }
3✔
814

815
                        nodeReader := bytes.NewReader(nodeBytes)
3✔
816
                        node, features, err := deserializeLightningNodeCacheable( //nolint:ll
3✔
817
                                nodeReader,
3✔
818
                        )
3✔
819
                        if err != nil {
3✔
820
                                return err
×
821
                        }
×
822

823
                        // Execute the callback, the transaction will abort if
824
                        // this returns an error.
825
                        return cb(node, features)
3✔
826
                })
827
        }
828

829
        return kvdb.View(c.db, traversal, func() {})
6✔
830
}
831

832
// SourceNode returns the source node of the graph. The source node is treated
833
// as the center node within a star-graph. This method may be used to kick off
834
// a path finding algorithm in order to explore the reachability of another
835
// node based off the source node.
836
func (c *ChannelGraph) SourceNode() (*models.LightningNode, error) {
3✔
837
        var source *models.LightningNode
3✔
838
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
839
                // First grab the nodes bucket which stores the mapping from
3✔
840
                // pubKey to node information.
3✔
841
                nodes := tx.ReadBucket(nodeBucket)
3✔
842
                if nodes == nil {
3✔
843
                        return ErrGraphNotFound
×
844
                }
×
845

846
                node, err := c.sourceNode(nodes)
3✔
847
                if err != nil {
3✔
UNCOV
848
                        return err
×
UNCOV
849
                }
×
850
                source = node
3✔
851

3✔
852
                return nil
3✔
853
        }, func() {
3✔
854
                source = nil
3✔
855
        })
3✔
856
        if err != nil {
3✔
UNCOV
857
                return nil, err
×
UNCOV
858
        }
×
859

860
        return source, nil
3✔
861
}
862

863
// sourceNode uses an existing database transaction and returns the source node
864
// of the graph. The source node is treated as the center node within a
865
// star-graph. This method may be used to kick off a path finding algorithm in
866
// order to explore the reachability of another node based off the source node.
867
func (c *ChannelGraph) sourceNode(nodes kvdb.RBucket) (*models.LightningNode,
868
        error) {
3✔
869

3✔
870
        selfPub := nodes.Get(sourceKey)
3✔
871
        if selfPub == nil {
3✔
UNCOV
872
                return nil, ErrSourceNodeNotSet
×
UNCOV
873
        }
×
874

875
        // With the pubKey of the source node retrieved, we're able to
876
        // fetch the full node information.
877
        node, err := fetchLightningNode(nodes, selfPub)
3✔
878
        if err != nil {
3✔
879
                return nil, err
×
880
        }
×
881

882
        return &node, nil
3✔
883
}
884

885
// SetSourceNode sets the source node within the graph database. The source
886
// node is to be used as the center of a star-graph within path finding
887
// algorithms.
888
func (c *ChannelGraph) SetSourceNode(node *models.LightningNode) error {
3✔
889
        nodePubBytes := node.PubKeyBytes[:]
3✔
890

3✔
891
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
892
                // First grab the nodes bucket which stores the mapping from
3✔
893
                // pubKey to node information.
3✔
894
                nodes, err := tx.CreateTopLevelBucket(nodeBucket)
3✔
895
                if err != nil {
3✔
896
                        return err
×
897
                }
×
898

899
                // Next we create the mapping from source to the targeted
900
                // public key.
901
                if err := nodes.Put(sourceKey, nodePubBytes); err != nil {
3✔
902
                        return err
×
903
                }
×
904

905
                // Finally, we commit the information of the lightning node
906
                // itself.
907
                return addLightningNode(tx, node)
3✔
908
        }, func() {})
3✔
909
}
910

911
// AddLightningNode adds a vertex/node to the graph database. If the node is not
912
// in the database from before, this will add a new, unconnected one to the
913
// graph. If it is present from before, this will update that node's
914
// information. Note that this method is expected to only be called to update an
915
// already present node from a node announcement, or to insert a node found in a
916
// channel update.
917
//
918
// TODO(roasbeef): also need sig of announcement
919
func (c *ChannelGraph) AddLightningNode(node *models.LightningNode,
920
        op ...batch.SchedulerOption) error {
3✔
921

3✔
922
        r := &batch.Request{
3✔
923
                Update: func(tx kvdb.RwTx) error {
6✔
924
                        if c.graphCache != nil {
6✔
925
                                c.graphCache.AddNodeFeatures(
3✔
926
                                        node.PubKeyBytes, node.Features,
3✔
927
                                )
3✔
928
                        }
3✔
929

930
                        return addLightningNode(tx, node)
3✔
931
                },
932
        }
933

934
        for _, f := range op {
6✔
935
                f(r)
3✔
936
        }
3✔
937

938
        return c.nodeScheduler.Execute(r)
3✔
939
}
940

941
func addLightningNode(tx kvdb.RwTx, node *models.LightningNode) error {
3✔
942
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
3✔
943
        if err != nil {
3✔
944
                return err
×
945
        }
×
946

947
        aliases, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
3✔
948
        if err != nil {
3✔
949
                return err
×
950
        }
×
951

952
        updateIndex, err := nodes.CreateBucketIfNotExists(
3✔
953
                nodeUpdateIndexBucket,
3✔
954
        )
3✔
955
        if err != nil {
3✔
956
                return err
×
957
        }
×
958

959
        return putLightningNode(nodes, aliases, updateIndex, node)
3✔
960
}
961

962
// LookupAlias attempts to return the alias as advertised by the target node.
963
// TODO(roasbeef): currently assumes that aliases are unique...
964
func (c *ChannelGraph) LookupAlias(pub *btcec.PublicKey) (string, error) {
3✔
965
        var alias string
3✔
966

3✔
967
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
968
                nodes := tx.ReadBucket(nodeBucket)
3✔
969
                if nodes == nil {
3✔
970
                        return ErrGraphNodesNotFound
×
971
                }
×
972

973
                aliases := nodes.NestedReadBucket(aliasIndexBucket)
3✔
974
                if aliases == nil {
3✔
975
                        return ErrGraphNodesNotFound
×
976
                }
×
977

978
                nodePub := pub.SerializeCompressed()
3✔
979
                a := aliases.Get(nodePub)
3✔
980
                if a == nil {
3✔
UNCOV
981
                        return ErrNodeAliasNotFound
×
UNCOV
982
                }
×
983

984
                // TODO(roasbeef): should actually be using the utf-8
985
                // package...
986
                alias = string(a)
3✔
987
                return nil
3✔
988
        }, func() {
3✔
989
                alias = ""
3✔
990
        })
3✔
991
        if err != nil {
3✔
UNCOV
992
                return "", err
×
UNCOV
993
        }
×
994

995
        return alias, nil
3✔
996
}
997

998
// DeleteLightningNode starts a new database transaction to remove a vertex/node
999
// from the database according to the node's public key.
UNCOV
1000
func (c *ChannelGraph) DeleteLightningNode(nodePub route.Vertex) error {
×
UNCOV
1001
        // TODO(roasbeef): ensure dangling edges are removed...
×
UNCOV
1002
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
×
UNCOV
1003
                nodes := tx.ReadWriteBucket(nodeBucket)
×
UNCOV
1004
                if nodes == nil {
×
1005
                        return ErrGraphNodeNotFound
×
1006
                }
×
1007

UNCOV
1008
                if c.graphCache != nil {
×
UNCOV
1009
                        c.graphCache.RemoveNode(nodePub)
×
UNCOV
1010
                }
×
1011

UNCOV
1012
                return c.deleteLightningNode(nodes, nodePub[:])
×
UNCOV
1013
        }, func() {})
×
1014
}
1015

1016
// deleteLightningNode uses an existing database transaction to remove a
1017
// vertex/node from the database according to the node's public key.
1018
func (c *ChannelGraph) deleteLightningNode(nodes kvdb.RwBucket,
1019
        compressedPubKey []byte) error {
3✔
1020

3✔
1021
        aliases := nodes.NestedReadWriteBucket(aliasIndexBucket)
3✔
1022
        if aliases == nil {
3✔
1023
                return ErrGraphNodesNotFound
×
1024
        }
×
1025

1026
        if err := aliases.Delete(compressedPubKey); err != nil {
3✔
1027
                return err
×
1028
        }
×
1029

1030
        // Before we delete the node, we'll fetch its current state so we can
1031
        // determine when its last update was to clear out the node update
1032
        // index.
1033
        node, err := fetchLightningNode(nodes, compressedPubKey)
3✔
1034
        if err != nil {
3✔
1035
                return err
×
1036
        }
×
1037

1038
        if err := nodes.Delete(compressedPubKey); err != nil {
3✔
1039
                return err
×
1040
        }
×
1041

1042
        // Finally, we'll delete the index entry for the node within the
1043
        // nodeUpdateIndexBucket as this node is no longer active, so we don't
1044
        // need to track its last update.
1045
        nodeUpdateIndex := nodes.NestedReadWriteBucket(nodeUpdateIndexBucket)
3✔
1046
        if nodeUpdateIndex == nil {
3✔
1047
                return ErrGraphNodesNotFound
×
1048
        }
×
1049

1050
        // In order to delete the entry, we'll need to reconstruct the key for
1051
        // its last update.
1052
        updateUnix := uint64(node.LastUpdate.Unix())
3✔
1053
        var indexKey [8 + 33]byte
3✔
1054
        byteOrder.PutUint64(indexKey[:8], updateUnix)
3✔
1055
        copy(indexKey[8:], compressedPubKey)
3✔
1056

3✔
1057
        return nodeUpdateIndex.Delete(indexKey[:])
3✔
1058
}
1059

1060
// AddChannelEdge adds a new (undirected, blank) edge to the graph database. An
1061
// undirected edge from the two target nodes are created. The information stored
1062
// denotes the static attributes of the channel, such as the channelID, the keys
1063
// involved in creation of the channel, and the set of features that the channel
1064
// supports. The chanPoint and chanID are used to uniquely identify the edge
1065
// globally within the database.
1066
func (c *ChannelGraph) AddChannelEdge(edge *models.ChannelEdgeInfo,
1067
        op ...batch.SchedulerOption) error {
3✔
1068

3✔
1069
        var alreadyExists bool
3✔
1070
        r := &batch.Request{
3✔
1071
                Reset: func() {
6✔
1072
                        alreadyExists = false
3✔
1073
                },
3✔
1074
                Update: func(tx kvdb.RwTx) error {
3✔
1075
                        err := c.addChannelEdge(tx, edge)
3✔
1076

3✔
1077
                        // Silence ErrEdgeAlreadyExist so that the batch can
3✔
1078
                        // succeed, but propagate the error via local state.
3✔
1079
                        if err == ErrEdgeAlreadyExist {
3✔
UNCOV
1080
                                alreadyExists = true
×
UNCOV
1081
                                return nil
×
UNCOV
1082
                        }
×
1083

1084
                        return err
3✔
1085
                },
1086
                OnCommit: func(err error) error {
3✔
1087
                        switch {
3✔
1088
                        case err != nil:
×
1089
                                return err
×
UNCOV
1090
                        case alreadyExists:
×
UNCOV
1091
                                return ErrEdgeAlreadyExist
×
1092
                        default:
3✔
1093
                                c.rejectCache.remove(edge.ChannelID)
3✔
1094
                                c.chanCache.remove(edge.ChannelID)
3✔
1095
                                return nil
3✔
1096
                        }
1097
                },
1098
        }
1099

1100
        for _, f := range op {
6✔
1101
                if f == nil {
3✔
1102
                        return fmt.Errorf("nil scheduler option was used")
×
1103
                }
×
1104

1105
                f(r)
3✔
1106
        }
1107

1108
        return c.chanScheduler.Execute(r)
3✔
1109
}
1110

1111
// addChannelEdge is the private form of AddChannelEdge that allows callers to
1112
// utilize an existing db transaction.
1113
func (c *ChannelGraph) addChannelEdge(tx kvdb.RwTx,
1114
        edge *models.ChannelEdgeInfo) error {
3✔
1115

3✔
1116
        // Construct the channel's primary key which is the 8-byte channel ID.
3✔
1117
        var chanKey [8]byte
3✔
1118
        binary.BigEndian.PutUint64(chanKey[:], edge.ChannelID)
3✔
1119

3✔
1120
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
3✔
1121
        if err != nil {
3✔
1122
                return err
×
1123
        }
×
1124
        edges, err := tx.CreateTopLevelBucket(edgeBucket)
3✔
1125
        if err != nil {
3✔
1126
                return err
×
1127
        }
×
1128
        edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
3✔
1129
        if err != nil {
3✔
1130
                return err
×
1131
        }
×
1132
        chanIndex, err := edges.CreateBucketIfNotExists(channelPointBucket)
3✔
1133
        if err != nil {
3✔
1134
                return err
×
1135
        }
×
1136

1137
        // First, attempt to check if this edge has already been created. If
1138
        // so, then we can exit early as this method is meant to be idempotent.
1139
        if edgeInfo := edgeIndex.Get(chanKey[:]); edgeInfo != nil {
3✔
UNCOV
1140
                return ErrEdgeAlreadyExist
×
UNCOV
1141
        }
×
1142

1143
        if c.graphCache != nil {
6✔
1144
                c.graphCache.AddChannel(edge, nil, nil)
3✔
1145
        }
3✔
1146

1147
        // Before we insert the channel into the database, we'll ensure that
1148
        // both nodes already exist in the channel graph. If either node
1149
        // doesn't, then we'll insert a "shell" node that just includes its
1150
        // public key, so subsequent validation and queries can work properly.
1151
        _, node1Err := fetchLightningNode(nodes, edge.NodeKey1Bytes[:])
3✔
1152
        switch {
3✔
1153
        case node1Err == ErrGraphNodeNotFound:
3✔
1154
                node1Shell := models.LightningNode{
3✔
1155
                        PubKeyBytes:          edge.NodeKey1Bytes,
3✔
1156
                        HaveNodeAnnouncement: false,
3✔
1157
                }
3✔
1158
                err := addLightningNode(tx, &node1Shell)
3✔
1159
                if err != nil {
3✔
1160
                        return fmt.Errorf("unable to create shell node "+
×
1161
                                "for: %x: %w", edge.NodeKey1Bytes, err)
×
1162
                }
×
1163
        case node1Err != nil:
×
1164
                return node1Err
×
1165
        }
1166

1167
        _, node2Err := fetchLightningNode(nodes, edge.NodeKey2Bytes[:])
3✔
1168
        switch {
3✔
1169
        case node2Err == ErrGraphNodeNotFound:
3✔
1170
                node2Shell := models.LightningNode{
3✔
1171
                        PubKeyBytes:          edge.NodeKey2Bytes,
3✔
1172
                        HaveNodeAnnouncement: false,
3✔
1173
                }
3✔
1174
                err := addLightningNode(tx, &node2Shell)
3✔
1175
                if err != nil {
3✔
1176
                        return fmt.Errorf("unable to create shell node "+
×
1177
                                "for: %x: %w", edge.NodeKey2Bytes, err)
×
1178
                }
×
1179
        case node2Err != nil:
×
1180
                return node2Err
×
1181
        }
1182

1183
        // If the edge hasn't been created yet, then we'll first add it to the
1184
        // edge index in order to associate the edge between two nodes and also
1185
        // store the static components of the channel.
1186
        if err := putChanEdgeInfo(edgeIndex, edge, chanKey); err != nil {
3✔
1187
                return err
×
1188
        }
×
1189

1190
        // Mark edge policies for both sides as unknown. This is to enable
1191
        // efficient incoming channel lookup for a node.
1192
        keys := []*[33]byte{
3✔
1193
                &edge.NodeKey1Bytes,
3✔
1194
                &edge.NodeKey2Bytes,
3✔
1195
        }
3✔
1196
        for _, key := range keys {
6✔
1197
                err := putChanEdgePolicyUnknown(edges, edge.ChannelID, key[:])
3✔
1198
                if err != nil {
3✔
1199
                        return err
×
1200
                }
×
1201
        }
1202

1203
        // Finally we add it to the channel index which maps channel points
1204
        // (outpoints) to the shorter channel ID's.
1205
        var b bytes.Buffer
3✔
1206
        if err := WriteOutpoint(&b, &edge.ChannelPoint); err != nil {
3✔
1207
                return err
×
1208
        }
×
1209
        return chanIndex.Put(b.Bytes(), chanKey[:])
3✔
1210
}
1211

1212
// HasChannelEdge returns true if the database knows of a channel edge with the
1213
// passed channel ID, and false otherwise. If an edge with that ID is found
1214
// within the graph, then two time stamps representing the last time the edge
1215
// was updated for both directed edges are returned along with the boolean. If
1216
// it is not found, then the zombie index is checked and its result is returned
1217
// as the second boolean.
1218
func (c *ChannelGraph) HasChannelEdge(
1219
        chanID uint64) (time.Time, time.Time, bool, bool, error) {
3✔
1220

3✔
1221
        var (
3✔
1222
                upd1Time time.Time
3✔
1223
                upd2Time time.Time
3✔
1224
                exists   bool
3✔
1225
                isZombie bool
3✔
1226
        )
3✔
1227

3✔
1228
        // We'll query the cache with the shared lock held to allow multiple
3✔
1229
        // readers to access values in the cache concurrently if they exist.
3✔
1230
        c.cacheMu.RLock()
3✔
1231
        if entry, ok := c.rejectCache.get(chanID); ok {
6✔
1232
                c.cacheMu.RUnlock()
3✔
1233
                upd1Time = time.Unix(entry.upd1Time, 0)
3✔
1234
                upd2Time = time.Unix(entry.upd2Time, 0)
3✔
1235
                exists, isZombie = entry.flags.unpack()
3✔
1236
                return upd1Time, upd2Time, exists, isZombie, nil
3✔
1237
        }
3✔
1238
        c.cacheMu.RUnlock()
3✔
1239

3✔
1240
        c.cacheMu.Lock()
3✔
1241
        defer c.cacheMu.Unlock()
3✔
1242

3✔
1243
        // The item was not found with the shared lock, so we'll acquire the
3✔
1244
        // exclusive lock and check the cache again in case another method added
3✔
1245
        // the entry to the cache while no lock was held.
3✔
1246
        if entry, ok := c.rejectCache.get(chanID); ok {
5✔
1247
                upd1Time = time.Unix(entry.upd1Time, 0)
2✔
1248
                upd2Time = time.Unix(entry.upd2Time, 0)
2✔
1249
                exists, isZombie = entry.flags.unpack()
2✔
1250
                return upd1Time, upd2Time, exists, isZombie, nil
2✔
1251
        }
2✔
1252

1253
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1254
                edges := tx.ReadBucket(edgeBucket)
3✔
1255
                if edges == nil {
3✔
1256
                        return ErrGraphNoEdgesFound
×
1257
                }
×
1258
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
1259
                if edgeIndex == nil {
3✔
1260
                        return ErrGraphNoEdgesFound
×
1261
                }
×
1262

1263
                var channelID [8]byte
3✔
1264
                byteOrder.PutUint64(channelID[:], chanID)
3✔
1265

3✔
1266
                // If the edge doesn't exist, then we'll also check our zombie
3✔
1267
                // index.
3✔
1268
                if edgeIndex.Get(channelID[:]) == nil {
6✔
1269
                        exists = false
3✔
1270
                        zombieIndex := edges.NestedReadBucket(zombieBucket)
3✔
1271
                        if zombieIndex != nil {
6✔
1272
                                isZombie, _, _ = isZombieEdge(
3✔
1273
                                        zombieIndex, chanID,
3✔
1274
                                )
3✔
1275
                        }
3✔
1276

1277
                        return nil
3✔
1278
                }
1279

1280
                exists = true
3✔
1281
                isZombie = false
3✔
1282

3✔
1283
                // If the channel has been found in the graph, then retrieve
3✔
1284
                // the edges itself so we can return the last updated
3✔
1285
                // timestamps.
3✔
1286
                nodes := tx.ReadBucket(nodeBucket)
3✔
1287
                if nodes == nil {
3✔
1288
                        return ErrGraphNodeNotFound
×
1289
                }
×
1290

1291
                e1, e2, err := fetchChanEdgePolicies(
3✔
1292
                        edgeIndex, edges, channelID[:],
3✔
1293
                )
3✔
1294
                if err != nil {
3✔
1295
                        return err
×
1296
                }
×
1297

1298
                // As we may have only one of the edges populated, only set the
1299
                // update time if the edge was found in the database.
1300
                if e1 != nil {
6✔
1301
                        upd1Time = e1.LastUpdate
3✔
1302
                }
3✔
1303
                if e2 != nil {
6✔
1304
                        upd2Time = e2.LastUpdate
3✔
1305
                }
3✔
1306

1307
                return nil
3✔
1308
        }, func() {}); err != nil {
3✔
1309
                return time.Time{}, time.Time{}, exists, isZombie, err
×
1310
        }
×
1311

1312
        c.rejectCache.insert(chanID, rejectCacheEntry{
3✔
1313
                upd1Time: upd1Time.Unix(),
3✔
1314
                upd2Time: upd2Time.Unix(),
3✔
1315
                flags:    packRejectFlags(exists, isZombie),
3✔
1316
        })
3✔
1317

3✔
1318
        return upd1Time, upd2Time, exists, isZombie, nil
3✔
1319
}
1320

1321
// AddEdgeProof sets the proof of an existing edge in the graph database.
1322
func (c *ChannelGraph) AddEdgeProof(chanID lnwire.ShortChannelID,
1323
        proof *models.ChannelAuthProof) error {
3✔
1324

3✔
1325
        // Construct the channel's primary key which is the 8-byte channel ID.
3✔
1326
        var chanKey [8]byte
3✔
1327
        binary.BigEndian.PutUint64(chanKey[:], chanID.ToUint64())
3✔
1328

3✔
1329
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1330
                edges := tx.ReadWriteBucket(edgeBucket)
3✔
1331
                if edges == nil {
3✔
1332
                        return ErrEdgeNotFound
×
1333
                }
×
1334

1335
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
1336
                if edgeIndex == nil {
3✔
1337
                        return ErrEdgeNotFound
×
1338
                }
×
1339

1340
                edge, err := fetchChanEdgeInfo(edgeIndex, chanKey[:])
3✔
1341
                if err != nil {
3✔
1342
                        return err
×
1343
                }
×
1344

1345
                edge.AuthProof = proof
3✔
1346

3✔
1347
                return putChanEdgeInfo(edgeIndex, &edge, chanKey)
3✔
1348
        }, func() {})
3✔
1349
}
1350

1351
const (
1352
        // pruneTipBytes is the total size of the value which stores a prune
1353
        // entry of the graph in the prune log. The "prune tip" is the last
1354
        // entry in the prune log, and indicates if the channel graph is in
1355
        // sync with the current UTXO state. The structure of the value
1356
        // is: blockHash, taking 32 bytes total.
1357
        pruneTipBytes = 32
1358
)
1359

1360
// PruneGraph prunes newly closed channels from the channel graph in response
1361
// to a new block being solved on the network. Any transactions which spend the
1362
// funding output of any known channels within he graph will be deleted.
1363
// Additionally, the "prune tip", or the last block which has been used to
1364
// prune the graph is stored so callers can ensure the graph is fully in sync
1365
// with the current UTXO state. A slice of channels that have been closed by
1366
// the target block are returned if the function succeeds without error.
1367
func (c *ChannelGraph) PruneGraph(spentOutputs []*wire.OutPoint,
1368
        blockHash *chainhash.Hash, blockHeight uint32) (
1369
        []*models.ChannelEdgeInfo, error) {
3✔
1370

3✔
1371
        c.cacheMu.Lock()
3✔
1372
        defer c.cacheMu.Unlock()
3✔
1373

3✔
1374
        var chansClosed []*models.ChannelEdgeInfo
3✔
1375

3✔
1376
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1377
                // First grab the edges bucket which houses the information
3✔
1378
                // we'd like to delete
3✔
1379
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
3✔
1380
                if err != nil {
3✔
1381
                        return err
×
1382
                }
×
1383

1384
                // Next grab the two edge indexes which will also need to be
1385
                // updated.
1386
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
3✔
1387
                if err != nil {
3✔
1388
                        return err
×
1389
                }
×
1390
                chanIndex, err := edges.CreateBucketIfNotExists(
3✔
1391
                        channelPointBucket,
3✔
1392
                )
3✔
1393
                if err != nil {
3✔
1394
                        return err
×
1395
                }
×
1396
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
1397
                if nodes == nil {
3✔
1398
                        return ErrSourceNodeNotSet
×
1399
                }
×
1400
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
3✔
1401
                if err != nil {
3✔
1402
                        return err
×
1403
                }
×
1404

1405
                // For each of the outpoints that have been spent within the
1406
                // block, we attempt to delete them from the graph as if that
1407
                // outpoint was a channel, then it has now been closed.
1408
                for _, chanPoint := range spentOutputs {
6✔
1409
                        // TODO(roasbeef): load channel bloom filter, continue
3✔
1410
                        // if NOT if filter
3✔
1411

3✔
1412
                        var opBytes bytes.Buffer
3✔
1413
                        err := WriteOutpoint(&opBytes, chanPoint)
3✔
1414
                        if err != nil {
3✔
1415
                                return err
×
1416
                        }
×
1417

1418
                        // First attempt to see if the channel exists within
1419
                        // the database, if not, then we can exit early.
1420
                        chanID := chanIndex.Get(opBytes.Bytes())
3✔
1421
                        if chanID == nil {
3✔
UNCOV
1422
                                continue
×
1423
                        }
1424

1425
                        // However, if it does, then we'll read out the full
1426
                        // version so we can add it to the set of deleted
1427
                        // channels.
1428
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
1429
                        if err != nil {
3✔
1430
                                return err
×
1431
                        }
×
1432

1433
                        // Attempt to delete the channel, an ErrEdgeNotFound
1434
                        // will be returned if that outpoint isn't known to be
1435
                        // a channel. If no error is returned, then a channel
1436
                        // was successfully pruned.
1437
                        err = c.delChannelEdgeUnsafe(
3✔
1438
                                edges, edgeIndex, chanIndex, zombieIndex,
3✔
1439
                                chanID, false, false,
3✔
1440
                        )
3✔
1441
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
3✔
1442
                                return err
×
1443
                        }
×
1444

1445
                        chansClosed = append(chansClosed, &edgeInfo)
3✔
1446
                }
1447

1448
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
3✔
1449
                if err != nil {
3✔
1450
                        return err
×
1451
                }
×
1452

1453
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
3✔
1454
                        pruneLogBucket,
3✔
1455
                )
3✔
1456
                if err != nil {
3✔
1457
                        return err
×
1458
                }
×
1459

1460
                // With the graph pruned, add a new entry to the prune log,
1461
                // which can be used to check if the graph is fully synced with
1462
                // the current UTXO state.
1463
                var blockHeightBytes [4]byte
3✔
1464
                byteOrder.PutUint32(blockHeightBytes[:], blockHeight)
3✔
1465

3✔
1466
                var newTip [pruneTipBytes]byte
3✔
1467
                copy(newTip[:], blockHash[:])
3✔
1468

3✔
1469
                err = pruneBucket.Put(blockHeightBytes[:], newTip[:])
3✔
1470
                if err != nil {
3✔
1471
                        return err
×
1472
                }
×
1473

1474
                // Now that the graph has been pruned, we'll also attempt to
1475
                // prune any nodes that have had a channel closed within the
1476
                // latest block.
1477
                return c.pruneGraphNodes(nodes, edgeIndex)
3✔
1478
        }, func() {
3✔
1479
                chansClosed = nil
3✔
1480
        })
3✔
1481
        if err != nil {
3✔
1482
                return nil, err
×
1483
        }
×
1484

1485
        for _, channel := range chansClosed {
6✔
1486
                c.rejectCache.remove(channel.ChannelID)
3✔
1487
                c.chanCache.remove(channel.ChannelID)
3✔
1488
        }
3✔
1489

1490
        if c.graphCache != nil {
6✔
1491
                log.Debugf("Pruned graph, cache now has %s",
3✔
1492
                        c.graphCache.Stats())
3✔
1493
        }
3✔
1494

1495
        return chansClosed, nil
3✔
1496
}
1497

1498
// PruneGraphNodes is a garbage collection method which attempts to prune out
1499
// any nodes from the channel graph that are currently unconnected. This ensure
1500
// that we only maintain a graph of reachable nodes. In the event that a pruned
1501
// node gains more channels, it will be re-added back to the graph.
1502
func (c *ChannelGraph) PruneGraphNodes() error {
3✔
1503
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1504
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
1505
                if nodes == nil {
3✔
1506
                        return ErrGraphNodesNotFound
×
1507
                }
×
1508
                edges := tx.ReadWriteBucket(edgeBucket)
3✔
1509
                if edges == nil {
3✔
1510
                        return ErrGraphNotFound
×
1511
                }
×
1512
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
1513
                if edgeIndex == nil {
3✔
1514
                        return ErrGraphNoEdgesFound
×
1515
                }
×
1516

1517
                return c.pruneGraphNodes(nodes, edgeIndex)
3✔
1518
        }, func() {})
3✔
1519
}
1520

1521
// pruneGraphNodes attempts to remove any nodes from the graph who have had a
1522
// channel closed within the current block. If the node still has existing
1523
// channels in the graph, this will act as a no-op.
1524
func (c *ChannelGraph) pruneGraphNodes(nodes kvdb.RwBucket,
1525
        edgeIndex kvdb.RwBucket) error {
3✔
1526

3✔
1527
        log.Trace("Pruning nodes from graph with no open channels")
3✔
1528

3✔
1529
        // We'll retrieve the graph's source node to ensure we don't remove it
3✔
1530
        // even if it no longer has any open channels.
3✔
1531
        sourceNode, err := c.sourceNode(nodes)
3✔
1532
        if err != nil {
3✔
1533
                return err
×
1534
        }
×
1535

1536
        // We'll use this map to keep count the number of references to a node
1537
        // in the graph. A node should only be removed once it has no more
1538
        // references in the graph.
1539
        nodeRefCounts := make(map[[33]byte]int)
3✔
1540
        err = nodes.ForEach(func(pubKey, nodeBytes []byte) error {
6✔
1541
                // If this is the source key, then we skip this
3✔
1542
                // iteration as the value for this key is a pubKey
3✔
1543
                // rather than raw node information.
3✔
1544
                if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
6✔
1545
                        return nil
3✔
1546
                }
3✔
1547

1548
                var nodePub [33]byte
3✔
1549
                copy(nodePub[:], pubKey)
3✔
1550
                nodeRefCounts[nodePub] = 0
3✔
1551

3✔
1552
                return nil
3✔
1553
        })
1554
        if err != nil {
3✔
1555
                return err
×
1556
        }
×
1557

1558
        // To ensure we never delete the source node, we'll start off by
1559
        // bumping its ref count to 1.
1560
        nodeRefCounts[sourceNode.PubKeyBytes] = 1
3✔
1561

3✔
1562
        // Next, we'll run through the edgeIndex which maps a channel ID to the
3✔
1563
        // edge info. We'll use this scan to populate our reference count map
3✔
1564
        // above.
3✔
1565
        err = edgeIndex.ForEach(func(chanID, edgeInfoBytes []byte) error {
6✔
1566
                // The first 66 bytes of the edge info contain the pubkeys of
3✔
1567
                // the nodes that this edge attaches. We'll extract them, and
3✔
1568
                // add them to the ref count map.
3✔
1569
                var node1, node2 [33]byte
3✔
1570
                copy(node1[:], edgeInfoBytes[:33])
3✔
1571
                copy(node2[:], edgeInfoBytes[33:])
3✔
1572

3✔
1573
                // With the nodes extracted, we'll increase the ref count of
3✔
1574
                // each of the nodes.
3✔
1575
                nodeRefCounts[node1]++
3✔
1576
                nodeRefCounts[node2]++
3✔
1577

3✔
1578
                return nil
3✔
1579
        })
3✔
1580
        if err != nil {
3✔
1581
                return err
×
1582
        }
×
1583

1584
        // Finally, we'll make a second pass over the set of nodes, and delete
1585
        // any nodes that have a ref count of zero.
1586
        var numNodesPruned int
3✔
1587
        for nodePubKey, refCount := range nodeRefCounts {
6✔
1588
                // If the ref count of the node isn't zero, then we can safely
3✔
1589
                // skip it as it still has edges to or from it within the
3✔
1590
                // graph.
3✔
1591
                if refCount != 0 {
6✔
1592
                        continue
3✔
1593
                }
1594

1595
                if c.graphCache != nil {
6✔
1596
                        c.graphCache.RemoveNode(nodePubKey)
3✔
1597
                }
3✔
1598

1599
                // If we reach this point, then there are no longer any edges
1600
                // that connect this node, so we can delete it.
1601
                err := c.deleteLightningNode(nodes, nodePubKey[:])
3✔
1602
                if err != nil {
3✔
1603
                        if errors.Is(err, ErrGraphNodeNotFound) ||
×
1604
                                errors.Is(err, ErrGraphNodesNotFound) {
×
1605

×
1606
                                log.Warnf("Unable to prune node %x from the "+
×
1607
                                        "graph: %v", nodePubKey, err)
×
1608
                                continue
×
1609
                        }
1610

1611
                        return err
×
1612
                }
1613

1614
                log.Infof("Pruned unconnected node %x from channel graph",
3✔
1615
                        nodePubKey[:])
3✔
1616

3✔
1617
                numNodesPruned++
3✔
1618
        }
1619

1620
        if numNodesPruned > 0 {
6✔
1621
                log.Infof("Pruned %v unconnected nodes from the channel graph",
3✔
1622
                        numNodesPruned)
3✔
1623
        }
3✔
1624

1625
        return nil
3✔
1626
}
1627

1628
// DisconnectBlockAtHeight is used to indicate that the block specified
1629
// by the passed height has been disconnected from the main chain. This
1630
// will "rewind" the graph back to the height below, deleting channels
1631
// that are no longer confirmed from the graph. The prune log will be
1632
// set to the last prune height valid for the remaining chain.
1633
// Channels that were removed from the graph resulting from the
1634
// disconnected block are returned.
1635
func (c *ChannelGraph) DisconnectBlockAtHeight(height uint32) (
1636
        []*models.ChannelEdgeInfo, error) {
2✔
1637

2✔
1638
        // Every channel having a ShortChannelID starting at 'height'
2✔
1639
        // will no longer be confirmed.
2✔
1640
        startShortChanID := lnwire.ShortChannelID{
2✔
1641
                BlockHeight: height,
2✔
1642
        }
2✔
1643

2✔
1644
        // Delete everything after this height from the db up until the
2✔
1645
        // SCID alias range.
2✔
1646
        endShortChanID := aliasmgr.StartingAlias
2✔
1647

2✔
1648
        // The block height will be the 3 first bytes of the channel IDs.
2✔
1649
        var chanIDStart [8]byte
2✔
1650
        byteOrder.PutUint64(chanIDStart[:], startShortChanID.ToUint64())
2✔
1651
        var chanIDEnd [8]byte
2✔
1652
        byteOrder.PutUint64(chanIDEnd[:], endShortChanID.ToUint64())
2✔
1653

2✔
1654
        c.cacheMu.Lock()
2✔
1655
        defer c.cacheMu.Unlock()
2✔
1656

2✔
1657
        // Keep track of the channels that are removed from the graph.
2✔
1658
        var removedChans []*models.ChannelEdgeInfo
2✔
1659

2✔
1660
        if err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
4✔
1661
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
2✔
1662
                if err != nil {
2✔
1663
                        return err
×
1664
                }
×
1665
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
2✔
1666
                if err != nil {
2✔
1667
                        return err
×
1668
                }
×
1669
                chanIndex, err := edges.CreateBucketIfNotExists(
2✔
1670
                        channelPointBucket,
2✔
1671
                )
2✔
1672
                if err != nil {
2✔
1673
                        return err
×
1674
                }
×
1675
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
2✔
1676
                if err != nil {
2✔
1677
                        return err
×
1678
                }
×
1679

1680
                // Scan from chanIDStart to chanIDEnd, deleting every
1681
                // found edge.
1682
                // NOTE: we must delete the edges after the cursor loop, since
1683
                // modifying the bucket while traversing is not safe.
1684
                // NOTE: We use a < comparison in bytes.Compare instead of <=
1685
                // so that the StartingAlias itself isn't deleted.
1686
                var keys [][]byte
2✔
1687
                cursor := edgeIndex.ReadWriteCursor()
2✔
1688

2✔
1689
                //nolint:ll
2✔
1690
                for k, v := cursor.Seek(chanIDStart[:]); k != nil &&
2✔
1691
                        bytes.Compare(k, chanIDEnd[:]) < 0; k, v = cursor.Next() {
4✔
1692
                        edgeInfoReader := bytes.NewReader(v)
2✔
1693
                        edgeInfo, err := deserializeChanEdgeInfo(edgeInfoReader)
2✔
1694
                        if err != nil {
2✔
1695
                                return err
×
1696
                        }
×
1697

1698
                        keys = append(keys, k)
2✔
1699
                        removedChans = append(removedChans, &edgeInfo)
2✔
1700
                }
1701

1702
                for _, k := range keys {
4✔
1703
                        err = c.delChannelEdgeUnsafe(
2✔
1704
                                edges, edgeIndex, chanIndex, zombieIndex,
2✔
1705
                                k, false, false,
2✔
1706
                        )
2✔
1707
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
2✔
1708
                                return err
×
1709
                        }
×
1710
                }
1711

1712
                // Delete all the entries in the prune log having a height
1713
                // greater or equal to the block disconnected.
1714
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
2✔
1715
                if err != nil {
2✔
1716
                        return err
×
1717
                }
×
1718

1719
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
2✔
1720
                        pruneLogBucket,
2✔
1721
                )
2✔
1722
                if err != nil {
2✔
1723
                        return err
×
1724
                }
×
1725

1726
                var pruneKeyStart [4]byte
2✔
1727
                byteOrder.PutUint32(pruneKeyStart[:], height)
2✔
1728

2✔
1729
                var pruneKeyEnd [4]byte
2✔
1730
                byteOrder.PutUint32(pruneKeyEnd[:], math.MaxUint32)
2✔
1731

2✔
1732
                // To avoid modifying the bucket while traversing, we delete
2✔
1733
                // the keys in a second loop.
2✔
1734
                var pruneKeys [][]byte
2✔
1735
                pruneCursor := pruneBucket.ReadWriteCursor()
2✔
1736
                //nolint:ll
2✔
1737
                for k, _ := pruneCursor.Seek(pruneKeyStart[:]); k != nil &&
2✔
1738
                        bytes.Compare(k, pruneKeyEnd[:]) <= 0; k, _ = pruneCursor.Next() {
4✔
1739
                        pruneKeys = append(pruneKeys, k)
2✔
1740
                }
2✔
1741

1742
                for _, k := range pruneKeys {
4✔
1743
                        if err := pruneBucket.Delete(k); err != nil {
2✔
1744
                                return err
×
1745
                        }
×
1746
                }
1747

1748
                return nil
2✔
1749
        }, func() {
2✔
1750
                removedChans = nil
2✔
1751
        }); err != nil {
2✔
1752
                return nil, err
×
1753
        }
×
1754

1755
        for _, channel := range removedChans {
4✔
1756
                c.rejectCache.remove(channel.ChannelID)
2✔
1757
                c.chanCache.remove(channel.ChannelID)
2✔
1758
        }
2✔
1759

1760
        return removedChans, nil
2✔
1761
}
1762

1763
// PruneTip returns the block height and hash of the latest block that has been
1764
// used to prune channels in the graph. Knowing the "prune tip" allows callers
1765
// to tell if the graph is currently in sync with the current best known UTXO
1766
// state.
1767
func (c *ChannelGraph) PruneTip() (*chainhash.Hash, uint32, error) {
3✔
1768
        var (
3✔
1769
                tipHash   chainhash.Hash
3✔
1770
                tipHeight uint32
3✔
1771
        )
3✔
1772

3✔
1773
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1774
                graphMeta := tx.ReadBucket(graphMetaBucket)
3✔
1775
                if graphMeta == nil {
3✔
1776
                        return ErrGraphNotFound
×
1777
                }
×
1778
                pruneBucket := graphMeta.NestedReadBucket(pruneLogBucket)
3✔
1779
                if pruneBucket == nil {
3✔
1780
                        return ErrGraphNeverPruned
×
1781
                }
×
1782

1783
                pruneCursor := pruneBucket.ReadCursor()
3✔
1784

3✔
1785
                // The prune key with the largest block height will be our
3✔
1786
                // prune tip.
3✔
1787
                k, v := pruneCursor.Last()
3✔
1788
                if k == nil {
6✔
1789
                        return ErrGraphNeverPruned
3✔
1790
                }
3✔
1791

1792
                // Once we have the prune tip, the value will be the block hash,
1793
                // and the key the block height.
1794
                copy(tipHash[:], v[:])
3✔
1795
                tipHeight = byteOrder.Uint32(k[:])
3✔
1796

3✔
1797
                return nil
3✔
1798
        }, func() {})
3✔
1799
        if err != nil {
6✔
1800
                return nil, 0, err
3✔
1801
        }
3✔
1802

1803
        return &tipHash, tipHeight, nil
3✔
1804
}
1805

1806
// DeleteChannelEdges removes edges with the given channel IDs from the
1807
// database and marks them as zombies. This ensures that we're unable to re-add
1808
// it to our database once again. If an edge does not exist within the
1809
// database, then ErrEdgeNotFound will be returned. If strictZombiePruning is
1810
// true, then when we mark these edges as zombies, we'll set up the keys such
1811
// that we require the node that failed to send the fresh update to be the one
1812
// that resurrects the channel from its zombie state. The markZombie bool
1813
// denotes whether or not to mark the channel as a zombie.
1814
func (c *ChannelGraph) DeleteChannelEdges(strictZombiePruning, markZombie bool,
1815
        chanIDs ...uint64) error {
3✔
1816

3✔
1817
        // TODO(roasbeef): possibly delete from node bucket if node has no more
3✔
1818
        // channels
3✔
1819
        // TODO(roasbeef): don't delete both edges?
3✔
1820

3✔
1821
        c.cacheMu.Lock()
3✔
1822
        defer c.cacheMu.Unlock()
3✔
1823

3✔
1824
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1825
                edges := tx.ReadWriteBucket(edgeBucket)
3✔
1826
                if edges == nil {
3✔
1827
                        return ErrEdgeNotFound
×
1828
                }
×
1829
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
1830
                if edgeIndex == nil {
3✔
1831
                        return ErrEdgeNotFound
×
1832
                }
×
1833
                chanIndex := edges.NestedReadWriteBucket(channelPointBucket)
3✔
1834
                if chanIndex == nil {
3✔
1835
                        return ErrEdgeNotFound
×
1836
                }
×
1837
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
1838
                if nodes == nil {
3✔
1839
                        return ErrGraphNodeNotFound
×
1840
                }
×
1841
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
3✔
1842
                if err != nil {
3✔
1843
                        return err
×
1844
                }
×
1845

1846
                var rawChanID [8]byte
3✔
1847
                for _, chanID := range chanIDs {
6✔
1848
                        byteOrder.PutUint64(rawChanID[:], chanID)
3✔
1849
                        err := c.delChannelEdgeUnsafe(
3✔
1850
                                edges, edgeIndex, chanIndex, zombieIndex,
3✔
1851
                                rawChanID[:], markZombie, strictZombiePruning,
3✔
1852
                        )
3✔
1853
                        if err != nil {
3✔
UNCOV
1854
                                return err
×
UNCOV
1855
                        }
×
1856
                }
1857

1858
                return nil
3✔
1859
        }, func() {})
3✔
1860
        if err != nil {
3✔
UNCOV
1861
                return err
×
UNCOV
1862
        }
×
1863

1864
        for _, chanID := range chanIDs {
6✔
1865
                c.rejectCache.remove(chanID)
3✔
1866
                c.chanCache.remove(chanID)
3✔
1867
        }
3✔
1868

1869
        return nil
3✔
1870
}
1871

1872
// ChannelID attempt to lookup the 8-byte compact channel ID which maps to the
1873
// passed channel point (outpoint). If the passed channel doesn't exist within
1874
// the database, then ErrEdgeNotFound is returned.
1875
func (c *ChannelGraph) ChannelID(chanPoint *wire.OutPoint) (uint64, error) {
3✔
1876
        var chanID uint64
3✔
1877
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1878
                var err error
3✔
1879
                chanID, err = getChanID(tx, chanPoint)
3✔
1880
                return err
3✔
1881
        }, func() {
6✔
1882
                chanID = 0
3✔
1883
        }); err != nil {
6✔
1884
                return 0, err
3✔
1885
        }
3✔
1886

1887
        return chanID, nil
3✔
1888
}
1889

1890
// getChanID returns the assigned channel ID for a given channel point.
1891
func getChanID(tx kvdb.RTx, chanPoint *wire.OutPoint) (uint64, error) {
3✔
1892
        var b bytes.Buffer
3✔
1893
        if err := WriteOutpoint(&b, chanPoint); err != nil {
3✔
1894
                return 0, err
×
1895
        }
×
1896

1897
        edges := tx.ReadBucket(edgeBucket)
3✔
1898
        if edges == nil {
3✔
1899
                return 0, ErrGraphNoEdgesFound
×
1900
        }
×
1901
        chanIndex := edges.NestedReadBucket(channelPointBucket)
3✔
1902
        if chanIndex == nil {
3✔
1903
                return 0, ErrGraphNoEdgesFound
×
1904
        }
×
1905

1906
        chanIDBytes := chanIndex.Get(b.Bytes())
3✔
1907
        if chanIDBytes == nil {
6✔
1908
                return 0, ErrEdgeNotFound
3✔
1909
        }
3✔
1910

1911
        chanID := byteOrder.Uint64(chanIDBytes)
3✔
1912

3✔
1913
        return chanID, nil
3✔
1914
}
1915

1916
// TODO(roasbeef): allow updates to use Batch?
1917

1918
// HighestChanID returns the "highest" known channel ID in the channel graph.
1919
// This represents the "newest" channel from the PoV of the chain. This method
1920
// can be used by peers to quickly determine if they're graphs are in sync.
1921
func (c *ChannelGraph) HighestChanID() (uint64, error) {
3✔
1922
        var cid uint64
3✔
1923

3✔
1924
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1925
                edges := tx.ReadBucket(edgeBucket)
3✔
1926
                if edges == nil {
3✔
1927
                        return ErrGraphNoEdgesFound
×
1928
                }
×
1929
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
1930
                if edgeIndex == nil {
3✔
1931
                        return ErrGraphNoEdgesFound
×
1932
                }
×
1933

1934
                // In order to find the highest chan ID, we'll fetch a cursor
1935
                // and use that to seek to the "end" of our known rage.
1936
                cidCursor := edgeIndex.ReadCursor()
3✔
1937

3✔
1938
                lastChanID, _ := cidCursor.Last()
3✔
1939

3✔
1940
                // If there's no key, then this means that we don't actually
3✔
1941
                // know of any channels, so we'll return a predicable error.
3✔
1942
                if lastChanID == nil {
6✔
1943
                        return ErrGraphNoEdgesFound
3✔
1944
                }
3✔
1945

1946
                // Otherwise, we'll de serialize the channel ID and return it
1947
                // to the caller.
1948
                cid = byteOrder.Uint64(lastChanID)
3✔
1949
                return nil
3✔
1950
        }, func() {
3✔
1951
                cid = 0
3✔
1952
        })
3✔
1953
        if err != nil && err != ErrGraphNoEdgesFound {
3✔
1954
                return 0, err
×
1955
        }
×
1956

1957
        return cid, nil
3✔
1958
}
1959

1960
// ChannelEdge represents the complete set of information for a channel edge in
1961
// the known channel graph. This struct couples the core information of the
1962
// edge as well as each of the known advertised edge policies.
1963
type ChannelEdge struct {
1964
        // Info contains all the static information describing the channel.
1965
        Info *models.ChannelEdgeInfo
1966

1967
        // Policy1 points to the "first" edge policy of the channel containing
1968
        // the dynamic information required to properly route through the edge.
1969
        Policy1 *models.ChannelEdgePolicy
1970

1971
        // Policy2 points to the "second" edge policy of the channel containing
1972
        // the dynamic information required to properly route through the edge.
1973
        Policy2 *models.ChannelEdgePolicy
1974

1975
        // Node1 is "node 1" in the channel. This is the node that would have
1976
        // produced Policy1 if it exists.
1977
        Node1 *models.LightningNode
1978

1979
        // Node2 is "node 2" in the channel. This is the node that would have
1980
        // produced Policy2 if it exists.
1981
        Node2 *models.LightningNode
1982
}
1983

1984
// ChanUpdatesInHorizon returns all the known channel edges which have at least
1985
// one edge that has an update timestamp within the specified horizon.
1986
func (c *ChannelGraph) ChanUpdatesInHorizon(startTime,
1987
        endTime time.Time) ([]ChannelEdge, error) {
3✔
1988

3✔
1989
        // To ensure we don't return duplicate ChannelEdges, we'll use an
3✔
1990
        // additional map to keep track of the edges already seen to prevent
3✔
1991
        // re-adding it.
3✔
1992
        var edgesSeen map[uint64]struct{}
3✔
1993
        var edgesToCache map[uint64]ChannelEdge
3✔
1994
        var edgesInHorizon []ChannelEdge
3✔
1995

3✔
1996
        c.cacheMu.Lock()
3✔
1997
        defer c.cacheMu.Unlock()
3✔
1998

3✔
1999
        var hits int
3✔
2000
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
2001
                edges := tx.ReadBucket(edgeBucket)
3✔
2002
                if edges == nil {
3✔
2003
                        return ErrGraphNoEdgesFound
×
2004
                }
×
2005
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
2006
                if edgeIndex == nil {
3✔
2007
                        return ErrGraphNoEdgesFound
×
2008
                }
×
2009
                edgeUpdateIndex := edges.NestedReadBucket(edgeUpdateIndexBucket)
3✔
2010
                if edgeUpdateIndex == nil {
3✔
2011
                        return ErrGraphNoEdgesFound
×
2012
                }
×
2013

2014
                nodes := tx.ReadBucket(nodeBucket)
3✔
2015
                if nodes == nil {
3✔
2016
                        return ErrGraphNodesNotFound
×
2017
                }
×
2018

2019
                // We'll now obtain a cursor to perform a range query within
2020
                // the index to find all channels within the horizon.
2021
                updateCursor := edgeUpdateIndex.ReadCursor()
3✔
2022

3✔
2023
                var startTimeBytes, endTimeBytes [8 + 8]byte
3✔
2024
                byteOrder.PutUint64(
3✔
2025
                        startTimeBytes[:8], uint64(startTime.Unix()),
3✔
2026
                )
3✔
2027
                byteOrder.PutUint64(
3✔
2028
                        endTimeBytes[:8], uint64(endTime.Unix()),
3✔
2029
                )
3✔
2030

3✔
2031
                // With our start and end times constructed, we'll step through
3✔
2032
                // the index collecting the info and policy of each update of
3✔
2033
                // each channel that has a last update within the time range.
3✔
2034
                //
3✔
2035
                //nolint:ll
3✔
2036
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
3✔
2037
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
6✔
2038

3✔
2039
                        // We have a new eligible entry, so we'll slice of the
3✔
2040
                        // chan ID so we can query it in the DB.
3✔
2041
                        chanID := indexKey[8:]
3✔
2042

3✔
2043
                        // If we've already retrieved the info and policies for
3✔
2044
                        // this edge, then we can skip it as we don't need to do
3✔
2045
                        // so again.
3✔
2046
                        chanIDInt := byteOrder.Uint64(chanID)
3✔
2047
                        if _, ok := edgesSeen[chanIDInt]; ok {
3✔
UNCOV
2048
                                continue
×
2049
                        }
2050

2051
                        if channel, ok := c.chanCache.get(chanIDInt); ok {
6✔
2052
                                hits++
3✔
2053
                                edgesSeen[chanIDInt] = struct{}{}
3✔
2054
                                edgesInHorizon = append(edgesInHorizon, channel)
3✔
2055
                                continue
3✔
2056
                        }
2057

2058
                        // First, we'll fetch the static edge information.
2059
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
2060
                        if err != nil {
3✔
2061
                                chanID := byteOrder.Uint64(chanID)
×
2062
                                return fmt.Errorf("unable to fetch info for "+
×
2063
                                        "edge with chan_id=%v: %v", chanID, err)
×
2064
                        }
×
2065

2066
                        // With the static information obtained, we'll now
2067
                        // fetch the dynamic policy info.
2068
                        edge1, edge2, err := fetchChanEdgePolicies(
3✔
2069
                                edgeIndex, edges, chanID,
3✔
2070
                        )
3✔
2071
                        if err != nil {
3✔
2072
                                chanID := byteOrder.Uint64(chanID)
×
2073
                                return fmt.Errorf("unable to fetch policies "+
×
2074
                                        "for edge with chan_id=%v: %v", chanID,
×
2075
                                        err)
×
2076
                        }
×
2077

2078
                        node1, err := fetchLightningNode(
3✔
2079
                                nodes, edgeInfo.NodeKey1Bytes[:],
3✔
2080
                        )
3✔
2081
                        if err != nil {
3✔
2082
                                return err
×
2083
                        }
×
2084

2085
                        node2, err := fetchLightningNode(
3✔
2086
                                nodes, edgeInfo.NodeKey2Bytes[:],
3✔
2087
                        )
3✔
2088
                        if err != nil {
3✔
2089
                                return err
×
2090
                        }
×
2091

2092
                        // Finally, we'll collate this edge with the rest of
2093
                        // edges to be returned.
2094
                        edgesSeen[chanIDInt] = struct{}{}
3✔
2095
                        channel := ChannelEdge{
3✔
2096
                                Info:    &edgeInfo,
3✔
2097
                                Policy1: edge1,
3✔
2098
                                Policy2: edge2,
3✔
2099
                                Node1:   &node1,
3✔
2100
                                Node2:   &node2,
3✔
2101
                        }
3✔
2102
                        edgesInHorizon = append(edgesInHorizon, channel)
3✔
2103
                        edgesToCache[chanIDInt] = channel
3✔
2104
                }
2105

2106
                return nil
3✔
2107
        }, func() {
3✔
2108
                edgesSeen = make(map[uint64]struct{})
3✔
2109
                edgesToCache = make(map[uint64]ChannelEdge)
3✔
2110
                edgesInHorizon = nil
3✔
2111
        })
3✔
2112
        switch {
3✔
2113
        case err == ErrGraphNoEdgesFound:
×
2114
                fallthrough
×
2115
        case err == ErrGraphNodesNotFound:
×
2116
                break
×
2117

2118
        case err != nil:
×
2119
                return nil, err
×
2120
        }
2121

2122
        // Insert any edges loaded from disk into the cache.
2123
        for chanid, channel := range edgesToCache {
6✔
2124
                c.chanCache.insert(chanid, channel)
3✔
2125
        }
3✔
2126

2127
        log.Debugf("ChanUpdatesInHorizon hit percentage: %f (%d/%d)",
3✔
2128
                float64(hits)/float64(len(edgesInHorizon)), hits,
3✔
2129
                len(edgesInHorizon))
3✔
2130

3✔
2131
        return edgesInHorizon, nil
3✔
2132
}
2133

2134
// NodeUpdatesInHorizon returns all the known lightning node which have an
2135
// update timestamp within the passed range. This method can be used by two
2136
// nodes to quickly determine if they have the same set of up to date node
2137
// announcements.
2138
func (c *ChannelGraph) NodeUpdatesInHorizon(startTime,
2139
        endTime time.Time) ([]models.LightningNode, error) {
3✔
2140

3✔
2141
        var nodesInHorizon []models.LightningNode
3✔
2142

3✔
2143
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
2144
                nodes := tx.ReadBucket(nodeBucket)
3✔
2145
                if nodes == nil {
3✔
2146
                        return ErrGraphNodesNotFound
×
2147
                }
×
2148

2149
                nodeUpdateIndex := nodes.NestedReadBucket(nodeUpdateIndexBucket)
3✔
2150
                if nodeUpdateIndex == nil {
3✔
2151
                        return ErrGraphNodesNotFound
×
2152
                }
×
2153

2154
                // We'll now obtain a cursor to perform a range query within
2155
                // the index to find all node announcements within the horizon.
2156
                updateCursor := nodeUpdateIndex.ReadCursor()
3✔
2157

3✔
2158
                var startTimeBytes, endTimeBytes [8 + 33]byte
3✔
2159
                byteOrder.PutUint64(
3✔
2160
                        startTimeBytes[:8], uint64(startTime.Unix()),
3✔
2161
                )
3✔
2162
                byteOrder.PutUint64(
3✔
2163
                        endTimeBytes[:8], uint64(endTime.Unix()),
3✔
2164
                )
3✔
2165

3✔
2166
                // With our start and end times constructed, we'll step through
3✔
2167
                // the index collecting info for each node within the time
3✔
2168
                // range.
3✔
2169
                //
3✔
2170
                //nolint:ll
3✔
2171
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
3✔
2172
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
6✔
2173

3✔
2174
                        nodePub := indexKey[8:]
3✔
2175
                        node, err := fetchLightningNode(nodes, nodePub)
3✔
2176
                        if err != nil {
3✔
2177
                                return err
×
2178
                        }
×
2179

2180
                        nodesInHorizon = append(nodesInHorizon, node)
3✔
2181
                }
2182

2183
                return nil
3✔
2184
        }, func() {
3✔
2185
                nodesInHorizon = nil
3✔
2186
        })
3✔
2187
        switch {
3✔
2188
        case err == ErrGraphNoEdgesFound:
×
2189
                fallthrough
×
2190
        case err == ErrGraphNodesNotFound:
×
2191
                break
×
2192

2193
        case err != nil:
×
2194
                return nil, err
×
2195
        }
2196

2197
        return nodesInHorizon, nil
3✔
2198
}
2199

2200
// FilterKnownChanIDs takes a set of channel IDs and return the subset of chan
2201
// ID's that we don't know and are not known zombies of the passed set. In other
2202
// words, we perform a set difference of our set of chan ID's and the ones
2203
// passed in. This method can be used by callers to determine the set of
2204
// channels another peer knows of that we don't.
2205
func (c *ChannelGraph) FilterKnownChanIDs(chansInfo []ChannelUpdateInfo,
2206
        isZombieChan func(time.Time, time.Time) bool) ([]uint64, error) {
3✔
2207

3✔
2208
        var newChanIDs []uint64
3✔
2209

3✔
2210
        c.cacheMu.Lock()
3✔
2211
        defer c.cacheMu.Unlock()
3✔
2212

3✔
2213
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
2214
                edges := tx.ReadBucket(edgeBucket)
3✔
2215
                if edges == nil {
3✔
2216
                        return ErrGraphNoEdgesFound
×
2217
                }
×
2218
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
2219
                if edgeIndex == nil {
3✔
2220
                        return ErrGraphNoEdgesFound
×
2221
                }
×
2222

2223
                // Fetch the zombie index, it may not exist if no edges have
2224
                // ever been marked as zombies. If the index has been
2225
                // initialized, we will use it later to skip known zombie edges.
2226
                zombieIndex := edges.NestedReadBucket(zombieBucket)
3✔
2227

3✔
2228
                // We'll run through the set of chanIDs and collate only the
3✔
2229
                // set of channel that are unable to be found within our db.
3✔
2230
                var cidBytes [8]byte
3✔
2231
                for _, info := range chansInfo {
6✔
2232
                        scid := info.ShortChannelID.ToUint64()
3✔
2233
                        byteOrder.PutUint64(cidBytes[:], scid)
3✔
2234

3✔
2235
                        // If the edge is already known, skip it.
3✔
2236
                        if v := edgeIndex.Get(cidBytes[:]); v != nil {
6✔
2237
                                continue
3✔
2238
                        }
2239

2240
                        // If the edge is a known zombie, skip it.
2241
                        if zombieIndex != nil {
6✔
2242
                                isZombie, _, _ := isZombieEdge(
3✔
2243
                                        zombieIndex, scid,
3✔
2244
                                )
3✔
2245

3✔
2246
                                // TODO(ziggie): Make sure that for the strict
3✔
2247
                                // pruning case we compare the pubkeys and
3✔
2248
                                // whether the right timestamp is not older than
3✔
2249
                                // the `ChannelPruneExpiry`.
3✔
2250
                                //
3✔
2251
                                // NOTE: The timestamp data has no verification
3✔
2252
                                // attached to it in the `ReplyChannelRange` msg
3✔
2253
                                // so we are trusting this data at this point.
3✔
2254
                                // However it is not critical because we are
3✔
2255
                                // just removing the channel from the db when
3✔
2256
                                // the timestamps are more recent. During the
3✔
2257
                                // querying of the gossip msg verification
3✔
2258
                                // happens as usual.
3✔
2259
                                // However we should start punishing peers when
3✔
2260
                                // they don't provide us honest data ?
3✔
2261
                                isStillZombie := isZombieChan(
3✔
2262
                                        info.Node1UpdateTimestamp,
3✔
2263
                                        info.Node2UpdateTimestamp,
3✔
2264
                                )
3✔
2265

3✔
2266
                                switch {
3✔
2267
                                // If the edge is a known zombie and if we
2268
                                // would still consider it a zombie given the
2269
                                // latest update timestamps, then we skip this
2270
                                // channel.
UNCOV
2271
                                case isZombie && isStillZombie:
×
UNCOV
2272
                                        continue
×
2273

2274
                                // Otherwise, if we have marked it as a zombie
2275
                                // but the latest update timestamps could bring
2276
                                // it back from the dead, then we mark it alive,
2277
                                // and we let it be added to the set of IDs to
2278
                                // query our peer for.
UNCOV
2279
                                case isZombie && !isStillZombie:
×
UNCOV
2280
                                        err := c.markEdgeLiveUnsafe(tx, scid)
×
UNCOV
2281
                                        if err != nil {
×
2282
                                                return err
×
2283
                                        }
×
2284
                                }
2285
                        }
2286

2287
                        newChanIDs = append(newChanIDs, scid)
3✔
2288
                }
2289

2290
                return nil
3✔
2291
        }, func() {
3✔
2292
                newChanIDs = nil
3✔
2293
        })
3✔
2294
        switch {
3✔
2295
        // If we don't know of any edges yet, then we'll return the entire set
2296
        // of chan IDs specified.
2297
        case err == ErrGraphNoEdgesFound:
×
2298
                ogChanIDs := make([]uint64, len(chansInfo))
×
2299
                for i, info := range chansInfo {
×
2300
                        ogChanIDs[i] = info.ShortChannelID.ToUint64()
×
2301
                }
×
2302

2303
                return ogChanIDs, nil
×
2304

2305
        case err != nil:
×
2306
                return nil, err
×
2307
        }
2308

2309
        return newChanIDs, nil
3✔
2310
}
2311

2312
// ChannelUpdateInfo couples the SCID of a channel with the timestamps of the
2313
// latest received channel updates for the channel.
2314
type ChannelUpdateInfo struct {
2315
        // ShortChannelID is the SCID identifier of the channel.
2316
        ShortChannelID lnwire.ShortChannelID
2317

2318
        // Node1UpdateTimestamp is the timestamp of the latest received update
2319
        // from the node 1 channel peer. This will be set to zero time if no
2320
        // update has yet been received from this node.
2321
        Node1UpdateTimestamp time.Time
2322

2323
        // Node2UpdateTimestamp is the timestamp of the latest received update
2324
        // from the node 2 channel peer. This will be set to zero time if no
2325
        // update has yet been received from this node.
2326
        Node2UpdateTimestamp time.Time
2327
}
2328

2329
// NewChannelUpdateInfo is a constructor which makes sure we initialize the
2330
// timestamps with zero seconds unix timestamp which equals
2331
// `January 1, 1970, 00:00:00 UTC` in case the value is `time.Time{}`.
2332
func NewChannelUpdateInfo(scid lnwire.ShortChannelID, node1Timestamp,
2333
        node2Timestamp time.Time) ChannelUpdateInfo {
3✔
2334

3✔
2335
        chanInfo := ChannelUpdateInfo{
3✔
2336
                ShortChannelID:       scid,
3✔
2337
                Node1UpdateTimestamp: node1Timestamp,
3✔
2338
                Node2UpdateTimestamp: node2Timestamp,
3✔
2339
        }
3✔
2340

3✔
2341
        if node1Timestamp.IsZero() {
6✔
2342
                chanInfo.Node1UpdateTimestamp = time.Unix(0, 0)
3✔
2343
        }
3✔
2344

2345
        if node2Timestamp.IsZero() {
6✔
2346
                chanInfo.Node2UpdateTimestamp = time.Unix(0, 0)
3✔
2347
        }
3✔
2348

2349
        return chanInfo
3✔
2350
}
2351

2352
// BlockChannelRange represents a range of channels for a given block height.
2353
type BlockChannelRange struct {
2354
        // Height is the height of the block all of the channels below were
2355
        // included in.
2356
        Height uint32
2357

2358
        // Channels is the list of channels identified by their short ID
2359
        // representation known to us that were included in the block height
2360
        // above. The list may include channel update timestamp information if
2361
        // requested.
2362
        Channels []ChannelUpdateInfo
2363
}
2364

2365
// FilterChannelRange returns the channel ID's of all known channels which were
2366
// mined in a block height within the passed range. The channel IDs are grouped
2367
// by their common block height. This method can be used to quickly share with a
2368
// peer the set of channels we know of within a particular range to catch them
2369
// up after a period of time offline. If withTimestamps is true then the
2370
// timestamp info of the latest received channel update messages of the channel
2371
// will be included in the response.
2372
func (c *ChannelGraph) FilterChannelRange(startHeight,
2373
        endHeight uint32, withTimestamps bool) ([]BlockChannelRange, error) {
3✔
2374

3✔
2375
        startChanID := &lnwire.ShortChannelID{
3✔
2376
                BlockHeight: startHeight,
3✔
2377
        }
3✔
2378

3✔
2379
        endChanID := lnwire.ShortChannelID{
3✔
2380
                BlockHeight: endHeight,
3✔
2381
                TxIndex:     math.MaxUint32 & 0x00ffffff,
3✔
2382
                TxPosition:  math.MaxUint16,
3✔
2383
        }
3✔
2384

3✔
2385
        // As we need to perform a range scan, we'll convert the starting and
3✔
2386
        // ending height to their corresponding values when encoded using short
3✔
2387
        // channel ID's.
3✔
2388
        var chanIDStart, chanIDEnd [8]byte
3✔
2389
        byteOrder.PutUint64(chanIDStart[:], startChanID.ToUint64())
3✔
2390
        byteOrder.PutUint64(chanIDEnd[:], endChanID.ToUint64())
3✔
2391

3✔
2392
        var channelsPerBlock map[uint32][]ChannelUpdateInfo
3✔
2393
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
2394
                edges := tx.ReadBucket(edgeBucket)
3✔
2395
                if edges == nil {
3✔
2396
                        return ErrGraphNoEdgesFound
×
2397
                }
×
2398
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
2399
                if edgeIndex == nil {
3✔
2400
                        return ErrGraphNoEdgesFound
×
2401
                }
×
2402

2403
                cursor := edgeIndex.ReadCursor()
3✔
2404

3✔
2405
                // We'll now iterate through the database, and find each
3✔
2406
                // channel ID that resides within the specified range.
3✔
2407
                //
3✔
2408
                //nolint:ll
3✔
2409
                for k, v := cursor.Seek(chanIDStart[:]); k != nil &&
3✔
2410
                        bytes.Compare(k, chanIDEnd[:]) <= 0; k, v = cursor.Next() {
6✔
2411
                        // Don't send alias SCIDs during gossip sync.
3✔
2412
                        edgeReader := bytes.NewReader(v)
3✔
2413
                        edgeInfo, err := deserializeChanEdgeInfo(edgeReader)
3✔
2414
                        if err != nil {
3✔
2415
                                return err
×
2416
                        }
×
2417

2418
                        if edgeInfo.AuthProof == nil {
6✔
2419
                                continue
3✔
2420
                        }
2421

2422
                        // This channel ID rests within the target range, so
2423
                        // we'll add it to our returned set.
2424
                        rawCid := byteOrder.Uint64(k)
3✔
2425
                        cid := lnwire.NewShortChanIDFromInt(rawCid)
3✔
2426

3✔
2427
                        chanInfo := NewChannelUpdateInfo(
3✔
2428
                                cid, time.Time{}, time.Time{},
3✔
2429
                        )
3✔
2430

3✔
2431
                        if !withTimestamps {
3✔
UNCOV
2432
                                channelsPerBlock[cid.BlockHeight] = append(
×
UNCOV
2433
                                        channelsPerBlock[cid.BlockHeight],
×
UNCOV
2434
                                        chanInfo,
×
UNCOV
2435
                                )
×
UNCOV
2436

×
UNCOV
2437
                                continue
×
2438
                        }
2439

2440
                        node1Key, node2Key := computeEdgePolicyKeys(&edgeInfo)
3✔
2441

3✔
2442
                        rawPolicy := edges.Get(node1Key)
3✔
2443
                        if len(rawPolicy) != 0 {
6✔
2444
                                r := bytes.NewReader(rawPolicy)
3✔
2445

3✔
2446
                                edge, err := deserializeChanEdgePolicyRaw(r)
3✔
2447
                                if err != nil && !errors.Is(
3✔
2448
                                        err, ErrEdgePolicyOptionalFieldNotFound,
3✔
2449
                                ) {
3✔
2450

×
2451
                                        return err
×
2452
                                }
×
2453

2454
                                chanInfo.Node1UpdateTimestamp = edge.LastUpdate
3✔
2455
                        }
2456

2457
                        rawPolicy = edges.Get(node2Key)
3✔
2458
                        if len(rawPolicy) != 0 {
6✔
2459
                                r := bytes.NewReader(rawPolicy)
3✔
2460

3✔
2461
                                edge, err := deserializeChanEdgePolicyRaw(r)
3✔
2462
                                if err != nil && !errors.Is(
3✔
2463
                                        err, ErrEdgePolicyOptionalFieldNotFound,
3✔
2464
                                ) {
3✔
2465

×
2466
                                        return err
×
2467
                                }
×
2468

2469
                                chanInfo.Node2UpdateTimestamp = edge.LastUpdate
3✔
2470
                        }
2471

2472
                        channelsPerBlock[cid.BlockHeight] = append(
3✔
2473
                                channelsPerBlock[cid.BlockHeight], chanInfo,
3✔
2474
                        )
3✔
2475
                }
2476

2477
                return nil
3✔
2478
        }, func() {
3✔
2479
                channelsPerBlock = make(map[uint32][]ChannelUpdateInfo)
3✔
2480
        })
3✔
2481

2482
        switch {
3✔
2483
        // If we don't know of any channels yet, then there's nothing to
2484
        // filter, so we'll return an empty slice.
2485
        case err == ErrGraphNoEdgesFound || len(channelsPerBlock) == 0:
3✔
2486
                return nil, nil
3✔
2487

2488
        case err != nil:
×
2489
                return nil, err
×
2490
        }
2491

2492
        // Return the channel ranges in ascending block height order.
2493
        blocks := make([]uint32, 0, len(channelsPerBlock))
3✔
2494
        for block := range channelsPerBlock {
6✔
2495
                blocks = append(blocks, block)
3✔
2496
        }
3✔
2497
        sort.Slice(blocks, func(i, j int) bool {
6✔
2498
                return blocks[i] < blocks[j]
3✔
2499
        })
3✔
2500

2501
        channelRanges := make([]BlockChannelRange, 0, len(channelsPerBlock))
3✔
2502
        for _, block := range blocks {
6✔
2503
                channelRanges = append(channelRanges, BlockChannelRange{
3✔
2504
                        Height:   block,
3✔
2505
                        Channels: channelsPerBlock[block],
3✔
2506
                })
3✔
2507
        }
3✔
2508

2509
        return channelRanges, nil
3✔
2510
}
2511

2512
// FetchChanInfos returns the set of channel edges that correspond to the passed
2513
// channel ID's. If an edge is the query is unknown to the database, it will
2514
// skipped and the result will contain only those edges that exist at the time
2515
// of the query. This can be used to respond to peer queries that are seeking to
2516
// fill in gaps in their view of the channel graph.
2517
func (c *ChannelGraph) FetchChanInfos(chanIDs []uint64) ([]ChannelEdge, error) {
3✔
2518
        return c.fetchChanInfos(nil, chanIDs)
3✔
2519
}
3✔
2520

2521
// fetchChanInfos returns the set of channel edges that correspond to the passed
2522
// channel ID's. If an edge is the query is unknown to the database, it will
2523
// skipped and the result will contain only those edges that exist at the time
2524
// of the query. This can be used to respond to peer queries that are seeking to
2525
// fill in gaps in their view of the channel graph.
2526
//
2527
// NOTE: An optional transaction may be provided. If none is provided, then a
2528
// new one will be created.
2529
func (c *ChannelGraph) fetchChanInfos(tx kvdb.RTx, chanIDs []uint64) (
2530
        []ChannelEdge, error) {
3✔
2531
        // TODO(roasbeef): sort cids?
3✔
2532

3✔
2533
        var (
3✔
2534
                chanEdges []ChannelEdge
3✔
2535
                cidBytes  [8]byte
3✔
2536
        )
3✔
2537

3✔
2538
        fetchChanInfos := func(tx kvdb.RTx) error {
6✔
2539
                edges := tx.ReadBucket(edgeBucket)
3✔
2540
                if edges == nil {
3✔
2541
                        return ErrGraphNoEdgesFound
×
2542
                }
×
2543
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
2544
                if edgeIndex == nil {
3✔
2545
                        return ErrGraphNoEdgesFound
×
2546
                }
×
2547
                nodes := tx.ReadBucket(nodeBucket)
3✔
2548
                if nodes == nil {
3✔
2549
                        return ErrGraphNotFound
×
2550
                }
×
2551

2552
                for _, cid := range chanIDs {
6✔
2553
                        byteOrder.PutUint64(cidBytes[:], cid)
3✔
2554

3✔
2555
                        // First, we'll fetch the static edge information. If
3✔
2556
                        // the edge is unknown, we will skip the edge and
3✔
2557
                        // continue gathering all known edges.
3✔
2558
                        edgeInfo, err := fetchChanEdgeInfo(
3✔
2559
                                edgeIndex, cidBytes[:],
3✔
2560
                        )
3✔
2561
                        switch {
3✔
UNCOV
2562
                        case errors.Is(err, ErrEdgeNotFound):
×
UNCOV
2563
                                continue
×
2564
                        case err != nil:
×
2565
                                return err
×
2566
                        }
2567

2568
                        // With the static information obtained, we'll now
2569
                        // fetch the dynamic policy info.
2570
                        edge1, edge2, err := fetchChanEdgePolicies(
3✔
2571
                                edgeIndex, edges, cidBytes[:],
3✔
2572
                        )
3✔
2573
                        if err != nil {
3✔
2574
                                return err
×
2575
                        }
×
2576

2577
                        node1, err := fetchLightningNode(
3✔
2578
                                nodes, edgeInfo.NodeKey1Bytes[:],
3✔
2579
                        )
3✔
2580
                        if err != nil {
3✔
2581
                                return err
×
2582
                        }
×
2583

2584
                        node2, err := fetchLightningNode(
3✔
2585
                                nodes, edgeInfo.NodeKey2Bytes[:],
3✔
2586
                        )
3✔
2587
                        if err != nil {
3✔
2588
                                return err
×
2589
                        }
×
2590

2591
                        chanEdges = append(chanEdges, ChannelEdge{
3✔
2592
                                Info:    &edgeInfo,
3✔
2593
                                Policy1: edge1,
3✔
2594
                                Policy2: edge2,
3✔
2595
                                Node1:   &node1,
3✔
2596
                                Node2:   &node2,
3✔
2597
                        })
3✔
2598
                }
2599
                return nil
3✔
2600
        }
2601

2602
        if tx == nil {
6✔
2603
                err := kvdb.View(c.db, fetchChanInfos, func() {
6✔
2604
                        chanEdges = nil
3✔
2605
                })
3✔
2606
                if err != nil {
3✔
2607
                        return nil, err
×
2608
                }
×
2609

2610
                return chanEdges, nil
3✔
2611
        }
2612

UNCOV
2613
        err := fetchChanInfos(tx)
×
UNCOV
2614
        if err != nil {
×
2615
                return nil, err
×
2616
        }
×
2617

UNCOV
2618
        return chanEdges, nil
×
2619
}
2620

2621
func delEdgeUpdateIndexEntry(edgesBucket kvdb.RwBucket, chanID uint64,
2622
        edge1, edge2 *models.ChannelEdgePolicy) error {
3✔
2623

3✔
2624
        // First, we'll fetch the edge update index bucket which currently
3✔
2625
        // stores an entry for the channel we're about to delete.
3✔
2626
        updateIndex := edgesBucket.NestedReadWriteBucket(edgeUpdateIndexBucket)
3✔
2627
        if updateIndex == nil {
3✔
2628
                // No edges in bucket, return early.
×
2629
                return nil
×
2630
        }
×
2631

2632
        // Now that we have the bucket, we'll attempt to construct a template
2633
        // for the index key: updateTime || chanid.
2634
        var indexKey [8 + 8]byte
3✔
2635
        byteOrder.PutUint64(indexKey[8:], chanID)
3✔
2636

3✔
2637
        // With the template constructed, we'll attempt to delete an entry that
3✔
2638
        // would have been created by both edges: we'll alternate the update
3✔
2639
        // times, as one may had overridden the other.
3✔
2640
        if edge1 != nil {
6✔
2641
                byteOrder.PutUint64(
3✔
2642
                        indexKey[:8], uint64(edge1.LastUpdate.Unix()),
3✔
2643
                )
3✔
2644
                if err := updateIndex.Delete(indexKey[:]); err != nil {
3✔
2645
                        return err
×
2646
                }
×
2647
        }
2648

2649
        // We'll also attempt to delete the entry that may have been created by
2650
        // the second edge.
2651
        if edge2 != nil {
6✔
2652
                byteOrder.PutUint64(
3✔
2653
                        indexKey[:8], uint64(edge2.LastUpdate.Unix()),
3✔
2654
                )
3✔
2655
                if err := updateIndex.Delete(indexKey[:]); err != nil {
3✔
2656
                        return err
×
2657
                }
×
2658
        }
2659

2660
        return nil
3✔
2661
}
2662

2663
// delChannelEdgeUnsafe deletes the edge with the given chanID from the graph
2664
// cache. It then goes on to delete any policy info and edge info for this
2665
// channel from the DB and finally, if isZombie is true, it will add an entry
2666
// for this channel in the zombie index.
2667
//
2668
// NOTE: this method MUST only be called if the cacheMu has already been
2669
// acquired.
2670
func (c *ChannelGraph) delChannelEdgeUnsafe(edges, edgeIndex, chanIndex,
2671
        zombieIndex kvdb.RwBucket, chanID []byte, isZombie,
2672
        strictZombie bool) error {
3✔
2673

3✔
2674
        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
2675
        if err != nil {
3✔
UNCOV
2676
                return err
×
UNCOV
2677
        }
×
2678

2679
        if c.graphCache != nil {
6✔
2680
                c.graphCache.RemoveChannel(
3✔
2681
                        edgeInfo.NodeKey1Bytes, edgeInfo.NodeKey2Bytes,
3✔
2682
                        edgeInfo.ChannelID,
3✔
2683
                )
3✔
2684
        }
3✔
2685

2686
        // We'll also remove the entry in the edge update index bucket before
2687
        // we delete the edges themselves so we can access their last update
2688
        // times.
2689
        cid := byteOrder.Uint64(chanID)
3✔
2690
        edge1, edge2, err := fetchChanEdgePolicies(edgeIndex, edges, chanID)
3✔
2691
        if err != nil {
3✔
2692
                return err
×
2693
        }
×
2694
        err = delEdgeUpdateIndexEntry(edges, cid, edge1, edge2)
3✔
2695
        if err != nil {
3✔
2696
                return err
×
2697
        }
×
2698

2699
        // The edge key is of the format pubKey || chanID. First we construct
2700
        // the latter half, populating the channel ID.
2701
        var edgeKey [33 + 8]byte
3✔
2702
        copy(edgeKey[33:], chanID)
3✔
2703

3✔
2704
        // With the latter half constructed, copy over the first public key to
3✔
2705
        // delete the edge in this direction, then the second to delete the
3✔
2706
        // edge in the opposite direction.
3✔
2707
        copy(edgeKey[:33], edgeInfo.NodeKey1Bytes[:])
3✔
2708
        if edges.Get(edgeKey[:]) != nil {
6✔
2709
                if err := edges.Delete(edgeKey[:]); err != nil {
3✔
2710
                        return err
×
2711
                }
×
2712
        }
2713
        copy(edgeKey[:33], edgeInfo.NodeKey2Bytes[:])
3✔
2714
        if edges.Get(edgeKey[:]) != nil {
6✔
2715
                if err := edges.Delete(edgeKey[:]); err != nil {
3✔
2716
                        return err
×
2717
                }
×
2718
        }
2719

2720
        // As part of deleting the edge we also remove all disabled entries
2721
        // from the edgePolicyDisabledIndex bucket. We do that for both
2722
        // directions.
2723
        err = updateEdgePolicyDisabledIndex(edges, cid, false, false)
3✔
2724
        if err != nil {
3✔
2725
                return err
×
2726
        }
×
2727
        err = updateEdgePolicyDisabledIndex(edges, cid, true, false)
3✔
2728
        if err != nil {
3✔
2729
                return err
×
2730
        }
×
2731

2732
        // With the edge data deleted, we can purge the information from the two
2733
        // edge indexes.
2734
        if err := edgeIndex.Delete(chanID); err != nil {
3✔
2735
                return err
×
2736
        }
×
2737
        var b bytes.Buffer
3✔
2738
        if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
3✔
2739
                return err
×
2740
        }
×
2741
        if err := chanIndex.Delete(b.Bytes()); err != nil {
3✔
2742
                return err
×
2743
        }
×
2744

2745
        // Finally, we'll mark the edge as a zombie within our index if it's
2746
        // being removed due to the channel becoming a zombie. We do this to
2747
        // ensure we don't store unnecessary data for spent channels.
2748
        if !isZombie {
6✔
2749
                return nil
3✔
2750
        }
3✔
2751

2752
        nodeKey1, nodeKey2 := edgeInfo.NodeKey1Bytes, edgeInfo.NodeKey2Bytes
3✔
2753
        if strictZombie {
3✔
UNCOV
2754
                nodeKey1, nodeKey2 = makeZombiePubkeys(&edgeInfo, edge1, edge2)
×
UNCOV
2755
        }
×
2756

2757
        return markEdgeZombie(
3✔
2758
                zombieIndex, byteOrder.Uint64(chanID), nodeKey1, nodeKey2,
3✔
2759
        )
3✔
2760
}
2761

2762
// makeZombiePubkeys derives the node pubkeys to store in the zombie index for a
2763
// particular pair of channel policies. The return values are one of:
2764
//  1. (pubkey1, pubkey2)
2765
//  2. (pubkey1, blank)
2766
//  3. (blank, pubkey2)
2767
//
2768
// A blank pubkey means that corresponding node will be unable to resurrect a
2769
// channel on its own. For example, node1 may continue to publish recent
2770
// updates, but node2 has fallen way behind. After marking an edge as a zombie,
2771
// we don't want another fresh update from node1 to resurrect, as the edge can
2772
// only become live once node2 finally sends something recent.
2773
//
2774
// In the case where we have neither update, we allow either party to resurrect
2775
// the channel. If the channel were to be marked zombie again, it would be
2776
// marked with the correct lagging channel since we received an update from only
2777
// one side.
2778
func makeZombiePubkeys(info *models.ChannelEdgeInfo,
UNCOV
2779
        e1, e2 *models.ChannelEdgePolicy) ([33]byte, [33]byte) {
×
UNCOV
2780

×
UNCOV
2781
        switch {
×
2782
        // If we don't have either edge policy, we'll return both pubkeys so
2783
        // that the channel can be resurrected by either party.
2784
        case e1 == nil && e2 == nil:
×
2785
                return info.NodeKey1Bytes, info.NodeKey2Bytes
×
2786

2787
        // If we're missing edge1, or if both edges are present but edge1 is
2788
        // older, we'll return edge1's pubkey and a blank pubkey for edge2. This
2789
        // means that only an update from edge1 will be able to resurrect the
2790
        // channel.
UNCOV
2791
        case e1 == nil || (e2 != nil && e1.LastUpdate.Before(e2.LastUpdate)):
×
UNCOV
2792
                return info.NodeKey1Bytes, [33]byte{}
×
2793

2794
        // Otherwise, we're missing edge2 or edge2 is the older side, so we
2795
        // return a blank pubkey for edge1. In this case, only an update from
2796
        // edge2 can resurect the channel.
UNCOV
2797
        default:
×
UNCOV
2798
                return [33]byte{}, info.NodeKey2Bytes
×
2799
        }
2800
}
2801

2802
// UpdateEdgePolicy updates the edge routing policy for a single directed edge
2803
// within the database for the referenced channel. The `flags` attribute within
2804
// the ChannelEdgePolicy determines which of the directed edges are being
2805
// updated. If the flag is 1, then the first node's information is being
2806
// updated, otherwise it's the second node's information. The node ordering is
2807
// determined by the lexicographical ordering of the identity public keys of the
2808
// nodes on either side of the channel.
2809
func (c *ChannelGraph) UpdateEdgePolicy(edge *models.ChannelEdgePolicy,
2810
        op ...batch.SchedulerOption) error {
3✔
2811

3✔
2812
        var (
3✔
2813
                isUpdate1    bool
3✔
2814
                edgeNotFound bool
3✔
2815
        )
3✔
2816

3✔
2817
        r := &batch.Request{
3✔
2818
                Reset: func() {
6✔
2819
                        isUpdate1 = false
3✔
2820
                        edgeNotFound = false
3✔
2821
                },
3✔
2822
                Update: func(tx kvdb.RwTx) error {
3✔
2823
                        var err error
3✔
2824
                        isUpdate1, err = updateEdgePolicy(
3✔
2825
                                tx, edge, c.graphCache,
3✔
2826
                        )
3✔
2827

3✔
2828
                        if err != nil {
3✔
UNCOV
2829
                                log.Errorf("UpdateEdgePolicy faild: %v", err)
×
UNCOV
2830
                        }
×
2831

2832
                        // Silence ErrEdgeNotFound so that the batch can
2833
                        // succeed, but propagate the error via local state.
2834
                        if errors.Is(err, ErrEdgeNotFound) {
3✔
UNCOV
2835
                                edgeNotFound = true
×
UNCOV
2836
                                return nil
×
UNCOV
2837
                        }
×
2838

2839
                        return err
3✔
2840
                },
2841
                OnCommit: func(err error) error {
3✔
2842
                        switch {
3✔
2843
                        case err != nil:
×
2844
                                return err
×
UNCOV
2845
                        case edgeNotFound:
×
UNCOV
2846
                                return ErrEdgeNotFound
×
2847
                        default:
3✔
2848
                                c.updateEdgeCache(edge, isUpdate1)
3✔
2849
                                return nil
3✔
2850
                        }
2851
                },
2852
        }
2853

2854
        for _, f := range op {
6✔
2855
                f(r)
3✔
2856
        }
3✔
2857

2858
        return c.chanScheduler.Execute(r)
3✔
2859
}
2860

2861
func (c *ChannelGraph) updateEdgeCache(e *models.ChannelEdgePolicy,
2862
        isUpdate1 bool) {
3✔
2863

3✔
2864
        // If an entry for this channel is found in reject cache, we'll modify
3✔
2865
        // the entry with the updated timestamp for the direction that was just
3✔
2866
        // written. If the edge doesn't exist, we'll load the cache entry lazily
3✔
2867
        // during the next query for this edge.
3✔
2868
        if entry, ok := c.rejectCache.get(e.ChannelID); ok {
6✔
2869
                if isUpdate1 {
6✔
2870
                        entry.upd1Time = e.LastUpdate.Unix()
3✔
2871
                } else {
6✔
2872
                        entry.upd2Time = e.LastUpdate.Unix()
3✔
2873
                }
3✔
2874
                c.rejectCache.insert(e.ChannelID, entry)
3✔
2875
        }
2876

2877
        // If an entry for this channel is found in channel cache, we'll modify
2878
        // the entry with the updated policy for the direction that was just
2879
        // written. If the edge doesn't exist, we'll defer loading the info and
2880
        // policies and lazily read from disk during the next query.
2881
        if channel, ok := c.chanCache.get(e.ChannelID); ok {
6✔
2882
                if isUpdate1 {
6✔
2883
                        channel.Policy1 = e
3✔
2884
                } else {
6✔
2885
                        channel.Policy2 = e
3✔
2886
                }
3✔
2887
                c.chanCache.insert(e.ChannelID, channel)
3✔
2888
        }
2889
}
2890

2891
// updateEdgePolicy attempts to update an edge's policy within the relevant
2892
// buckets using an existing database transaction. The returned boolean will be
2893
// true if the updated policy belongs to node1, and false if the policy belonged
2894
// to node2.
2895
func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy,
2896
        graphCache *GraphCache) (bool, error) {
3✔
2897

3✔
2898
        edges := tx.ReadWriteBucket(edgeBucket)
3✔
2899
        if edges == nil {
3✔
2900
                return false, ErrEdgeNotFound
×
2901
        }
×
2902
        edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
2903
        if edgeIndex == nil {
3✔
2904
                return false, ErrEdgeNotFound
×
2905
        }
×
2906

2907
        // Create the channelID key be converting the channel ID
2908
        // integer into a byte slice.
2909
        var chanID [8]byte
3✔
2910
        byteOrder.PutUint64(chanID[:], edge.ChannelID)
3✔
2911

3✔
2912
        // With the channel ID, we then fetch the value storing the two
3✔
2913
        // nodes which connect this channel edge.
3✔
2914
        nodeInfo := edgeIndex.Get(chanID[:])
3✔
2915
        if nodeInfo == nil {
3✔
UNCOV
2916
                return false, ErrEdgeNotFound
×
UNCOV
2917
        }
×
2918

2919
        // Depending on the flags value passed above, either the first
2920
        // or second edge policy is being updated.
2921
        var fromNode, toNode []byte
3✔
2922
        var isUpdate1 bool
3✔
2923
        if edge.ChannelFlags&lnwire.ChanUpdateDirection == 0 {
6✔
2924
                fromNode = nodeInfo[:33]
3✔
2925
                toNode = nodeInfo[33:66]
3✔
2926
                isUpdate1 = true
3✔
2927
        } else {
6✔
2928
                fromNode = nodeInfo[33:66]
3✔
2929
                toNode = nodeInfo[:33]
3✔
2930
                isUpdate1 = false
3✔
2931
        }
3✔
2932

2933
        // Finally, with the direction of the edge being updated
2934
        // identified, we update the on-disk edge representation.
2935
        err := putChanEdgePolicy(edges, edge, fromNode, toNode)
3✔
2936
        if err != nil {
3✔
2937
                return false, err
×
2938
        }
×
2939

2940
        var (
3✔
2941
                fromNodePubKey route.Vertex
3✔
2942
                toNodePubKey   route.Vertex
3✔
2943
        )
3✔
2944
        copy(fromNodePubKey[:], fromNode)
3✔
2945
        copy(toNodePubKey[:], toNode)
3✔
2946

3✔
2947
        if graphCache != nil {
6✔
2948
                graphCache.UpdatePolicy(
3✔
2949
                        edge, fromNodePubKey, toNodePubKey, isUpdate1,
3✔
2950
                )
3✔
2951
        }
3✔
2952

2953
        return isUpdate1, nil
3✔
2954
}
2955

2956
// isPublic determines whether the node is seen as public within the graph from
2957
// the source node's point of view. An existing database transaction can also be
2958
// specified.
2959
func (c *ChannelGraph) isPublic(tx kvdb.RTx, nodePub route.Vertex,
2960
        sourcePubKey []byte) (bool, error) {
3✔
2961

3✔
2962
        // In order to determine whether this node is publicly advertised within
3✔
2963
        // the graph, we'll need to look at all of its edges and check whether
3✔
2964
        // they extend to any other node than the source node. errDone will be
3✔
2965
        // used to terminate the check early.
3✔
2966
        nodeIsPublic := false
3✔
2967
        errDone := errors.New("done")
3✔
2968
        err := c.ForEachNodeChannelTx(tx, nodePub, func(tx kvdb.RTx,
3✔
2969
                info *models.ChannelEdgeInfo, _ *models.ChannelEdgePolicy,
3✔
2970
                _ *models.ChannelEdgePolicy) error {
6✔
2971

3✔
2972
                // If this edge doesn't extend to the source node, we'll
3✔
2973
                // terminate our search as we can now conclude that the node is
3✔
2974
                // publicly advertised within the graph due to the local node
3✔
2975
                // knowing of the current edge.
3✔
2976
                if !bytes.Equal(info.NodeKey1Bytes[:], sourcePubKey) &&
3✔
2977
                        !bytes.Equal(info.NodeKey2Bytes[:], sourcePubKey) {
6✔
2978

3✔
2979
                        nodeIsPublic = true
3✔
2980
                        return errDone
3✔
2981
                }
3✔
2982

2983
                // Since the edge _does_ extend to the source node, we'll also
2984
                // need to ensure that this is a public edge.
2985
                if info.AuthProof != nil {
6✔
2986
                        nodeIsPublic = true
3✔
2987
                        return errDone
3✔
2988
                }
3✔
2989

2990
                // Otherwise, we'll continue our search.
2991
                return nil
3✔
2992
        })
2993
        if err != nil && err != errDone {
3✔
2994
                return false, err
×
2995
        }
×
2996

2997
        return nodeIsPublic, nil
3✔
2998
}
2999

3000
// FetchLightningNodeTx attempts to look up a target node by its identity
3001
// public key. If the node isn't found in the database, then
3002
// ErrGraphNodeNotFound is returned. An optional transaction may be provided.
3003
// If none is provided, then a new one will be created.
3004
func (c *ChannelGraph) FetchLightningNodeTx(tx kvdb.RTx, nodePub route.Vertex) (
3005
        *models.LightningNode, error) {
3✔
3006

3✔
3007
        return c.fetchLightningNode(tx, nodePub)
3✔
3008
}
3✔
3009

3010
// FetchLightningNode attempts to look up a target node by its identity public
3011
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
3012
// returned.
3013
func (c *ChannelGraph) FetchLightningNode(nodePub route.Vertex) (
3014
        *models.LightningNode, error) {
3✔
3015

3✔
3016
        return c.fetchLightningNode(nil, nodePub)
3✔
3017
}
3✔
3018

3019
// fetchLightningNode attempts to look up a target node by its identity public
3020
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
3021
// returned. An optional transaction may be provided. If none is provided, then
3022
// a new one will be created.
3023
func (c *ChannelGraph) fetchLightningNode(tx kvdb.RTx,
3024
        nodePub route.Vertex) (*models.LightningNode, error) {
3✔
3025

3✔
3026
        var node *models.LightningNode
3✔
3027
        fetch := func(tx kvdb.RTx) error {
6✔
3028
                // First grab the nodes bucket which stores the mapping from
3✔
3029
                // pubKey to node information.
3✔
3030
                nodes := tx.ReadBucket(nodeBucket)
3✔
3031
                if nodes == nil {
3✔
3032
                        return ErrGraphNotFound
×
3033
                }
×
3034

3035
                // If a key for this serialized public key isn't found, then
3036
                // the target node doesn't exist within the database.
3037
                nodeBytes := nodes.Get(nodePub[:])
3✔
3038
                if nodeBytes == nil {
6✔
3039
                        return ErrGraphNodeNotFound
3✔
3040
                }
3✔
3041

3042
                // If the node is found, then we can de deserialize the node
3043
                // information to return to the user.
3044
                nodeReader := bytes.NewReader(nodeBytes)
3✔
3045
                n, err := deserializeLightningNode(nodeReader)
3✔
3046
                if err != nil {
3✔
3047
                        return err
×
3048
                }
×
3049

3050
                node = &n
3✔
3051

3✔
3052
                return nil
3✔
3053
        }
3054

3055
        if tx == nil {
6✔
3056
                err := kvdb.View(
3✔
3057
                        c.db, fetch, func() {
6✔
3058
                                node = nil
3✔
3059
                        },
3✔
3060
                )
3061
                if err != nil {
6✔
3062
                        return nil, err
3✔
3063
                }
3✔
3064

3065
                return node, nil
3✔
3066
        }
3067

UNCOV
3068
        err := fetch(tx)
×
UNCOV
3069
        if err != nil {
×
UNCOV
3070
                return nil, err
×
UNCOV
3071
        }
×
3072

UNCOV
3073
        return node, nil
×
3074
}
3075

3076
// HasLightningNode determines if the graph has a vertex identified by the
3077
// target node identity public key. If the node exists in the database, a
3078
// timestamp of when the data for the node was lasted updated is returned along
3079
// with a true boolean. Otherwise, an empty time.Time is returned with a false
3080
// boolean.
3081
func (c *ChannelGraph) HasLightningNode(nodePub [33]byte) (time.Time, bool,
3082
        error) {
3✔
3083

3✔
3084
        var (
3✔
3085
                updateTime time.Time
3✔
3086
                exists     bool
3✔
3087
        )
3✔
3088

3✔
3089
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
3090
                // First grab the nodes bucket which stores the mapping from
3✔
3091
                // pubKey to node information.
3✔
3092
                nodes := tx.ReadBucket(nodeBucket)
3✔
3093
                if nodes == nil {
3✔
3094
                        return ErrGraphNotFound
×
3095
                }
×
3096

3097
                // If a key for this serialized public key isn't found, we can
3098
                // exit early.
3099
                nodeBytes := nodes.Get(nodePub[:])
3✔
3100
                if nodeBytes == nil {
6✔
3101
                        exists = false
3✔
3102
                        return nil
3✔
3103
                }
3✔
3104

3105
                // Otherwise we continue on to obtain the time stamp
3106
                // representing the last time the data for this node was
3107
                // updated.
3108
                nodeReader := bytes.NewReader(nodeBytes)
3✔
3109
                node, err := deserializeLightningNode(nodeReader)
3✔
3110
                if err != nil {
3✔
3111
                        return err
×
3112
                }
×
3113

3114
                exists = true
3✔
3115
                updateTime = node.LastUpdate
3✔
3116
                return nil
3✔
3117
        }, func() {
3✔
3118
                updateTime = time.Time{}
3✔
3119
                exists = false
3✔
3120
        })
3✔
3121
        if err != nil {
3✔
3122
                return time.Time{}, exists, err
×
3123
        }
×
3124

3125
        return updateTime, exists, nil
3✔
3126
}
3127

3128
// nodeTraversal is used to traverse all channels of a node given by its
3129
// public key and passes channel information into the specified callback.
3130
func nodeTraversal(tx kvdb.RTx, nodePub []byte, db kvdb.Backend,
3131
        cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3132
                *models.ChannelEdgePolicy) error) error {
3✔
3133

3✔
3134
        traversal := func(tx kvdb.RTx) error {
6✔
3135
                edges := tx.ReadBucket(edgeBucket)
3✔
3136
                if edges == nil {
3✔
3137
                        return ErrGraphNotFound
×
3138
                }
×
3139
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
3140
                if edgeIndex == nil {
3✔
3141
                        return ErrGraphNoEdgesFound
×
3142
                }
×
3143

3144
                // In order to reach all the edges for this node, we take
3145
                // advantage of the construction of the key-space within the
3146
                // edge bucket. The keys are stored in the form: pubKey ||
3147
                // chanID. Therefore, starting from a chanID of zero, we can
3148
                // scan forward in the bucket, grabbing all the edges for the
3149
                // node. Once the prefix no longer matches, then we know we're
3150
                // done.
3151
                var nodeStart [33 + 8]byte
3✔
3152
                copy(nodeStart[:], nodePub)
3✔
3153
                copy(nodeStart[33:], chanStart[:])
3✔
3154

3✔
3155
                // Starting from the key pubKey || 0, we seek forward in the
3✔
3156
                // bucket until the retrieved key no longer has the public key
3✔
3157
                // as its prefix. This indicates that we've stepped over into
3✔
3158
                // another node's edges, so we can terminate our scan.
3✔
3159
                edgeCursor := edges.ReadCursor()
3✔
3160
                for nodeEdge, _ := edgeCursor.Seek(nodeStart[:]); bytes.HasPrefix(nodeEdge, nodePub); nodeEdge, _ = edgeCursor.Next() { //nolint:ll
6✔
3161
                        // If the prefix still matches, the channel id is
3✔
3162
                        // returned in nodeEdge. Channel id is used to lookup
3✔
3163
                        // the node at the other end of the channel and both
3✔
3164
                        // edge policies.
3✔
3165
                        chanID := nodeEdge[33:]
3✔
3166
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
3167
                        if err != nil {
3✔
3168
                                return err
×
3169
                        }
×
3170

3171
                        outgoingPolicy, err := fetchChanEdgePolicy(
3✔
3172
                                edges, chanID, nodePub,
3✔
3173
                        )
3✔
3174
                        if err != nil {
3✔
3175
                                return err
×
3176
                        }
×
3177

3178
                        otherNode, err := edgeInfo.OtherNodeKeyBytes(nodePub)
3✔
3179
                        if err != nil {
3✔
3180
                                return err
×
3181
                        }
×
3182

3183
                        incomingPolicy, err := fetchChanEdgePolicy(
3✔
3184
                                edges, chanID, otherNode[:],
3✔
3185
                        )
3✔
3186
                        if err != nil {
3✔
3187
                                return err
×
3188
                        }
×
3189

3190
                        // Finally, we execute the callback.
3191
                        err = cb(tx, &edgeInfo, outgoingPolicy, incomingPolicy)
3✔
3192
                        if err != nil {
6✔
3193
                                return err
3✔
3194
                        }
3✔
3195
                }
3196

3197
                return nil
3✔
3198
        }
3199

3200
        // If no transaction was provided, then we'll create a new transaction
3201
        // to execute the transaction within.
3202
        if tx == nil {
6✔
3203
                return kvdb.View(db, traversal, func() {})
6✔
3204
        }
3205

3206
        // Otherwise, we re-use the existing transaction to execute the graph
3207
        // traversal.
3208
        return traversal(tx)
3✔
3209
}
3210

3211
// ForEachNodeChannel iterates through all channels of the given node,
3212
// executing the passed callback with an edge info structure and the policies
3213
// of each end of the channel. The first edge policy is the outgoing edge *to*
3214
// the connecting node, while the second is the incoming edge *from* the
3215
// connecting node. If the callback returns an error, then the iteration is
3216
// halted with the error propagated back up to the caller.
3217
//
3218
// Unknown policies are passed into the callback as nil values.
3219
func (c *ChannelGraph) ForEachNodeChannel(nodePub route.Vertex,
3220
        cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3221
                *models.ChannelEdgePolicy) error) error {
3✔
3222

3✔
3223
        return nodeTraversal(nil, nodePub[:], c.db, cb)
3✔
3224
}
3✔
3225

3226
// ForEachNodeChannelTx iterates through all channels of the given node,
3227
// executing the passed callback with an edge info structure and the policies
3228
// of each end of the channel. The first edge policy is the outgoing edge *to*
3229
// the connecting node, while the second is the incoming edge *from* the
3230
// connecting node. If the callback returns an error, then the iteration is
3231
// halted with the error propagated back up to the caller.
3232
//
3233
// Unknown policies are passed into the callback as nil values.
3234
//
3235
// If the caller wishes to re-use an existing boltdb transaction, then it
3236
// should be passed as the first argument.  Otherwise, the first argument should
3237
// be nil and a fresh transaction will be created to execute the graph
3238
// traversal.
3239
func (c *ChannelGraph) ForEachNodeChannelTx(tx kvdb.RTx,
3240
        nodePub route.Vertex, cb func(kvdb.RTx, *models.ChannelEdgeInfo,
3241
                *models.ChannelEdgePolicy,
3242
                *models.ChannelEdgePolicy) error) error {
3✔
3243

3✔
3244
        return nodeTraversal(tx, nodePub[:], c.db, cb)
3✔
3245
}
3✔
3246

3247
// FetchOtherNode attempts to fetch the full LightningNode that's opposite of
3248
// the target node in the channel. This is useful when one knows the pubkey of
3249
// one of the nodes, and wishes to obtain the full LightningNode for the other
3250
// end of the channel.
3251
func (c *ChannelGraph) FetchOtherNode(tx kvdb.RTx,
3252
        channel *models.ChannelEdgeInfo, thisNodeKey []byte) (
3253
        *models.LightningNode, error) {
3✔
3254

3✔
3255
        // Ensure that the node passed in is actually a member of the channel.
3✔
3256
        var targetNodeBytes [33]byte
3✔
3257
        switch {
3✔
3258
        case bytes.Equal(channel.NodeKey1Bytes[:], thisNodeKey):
3✔
3259
                targetNodeBytes = channel.NodeKey2Bytes
3✔
3260
        case bytes.Equal(channel.NodeKey2Bytes[:], thisNodeKey):
3✔
3261
                targetNodeBytes = channel.NodeKey1Bytes
3✔
3262
        default:
×
3263
                return nil, fmt.Errorf("node not participating in this channel")
×
3264
        }
3265

3266
        var targetNode *models.LightningNode
3✔
3267
        fetchNodeFunc := func(tx kvdb.RTx) error {
6✔
3268
                // First grab the nodes bucket which stores the mapping from
3✔
3269
                // pubKey to node information.
3✔
3270
                nodes := tx.ReadBucket(nodeBucket)
3✔
3271
                if nodes == nil {
3✔
3272
                        return ErrGraphNotFound
×
3273
                }
×
3274

3275
                node, err := fetchLightningNode(nodes, targetNodeBytes[:])
3✔
3276
                if err != nil {
3✔
3277
                        return err
×
3278
                }
×
3279

3280
                targetNode = &node
3✔
3281

3✔
3282
                return nil
3✔
3283
        }
3284

3285
        // If the transaction is nil, then we'll need to create a new one,
3286
        // otherwise we can use the existing db transaction.
3287
        var err error
3✔
3288
        if tx == nil {
3✔
3289
                err = kvdb.View(c.db, fetchNodeFunc, func() {
×
3290
                        targetNode = nil
×
3291
                })
×
3292
        } else {
3✔
3293
                err = fetchNodeFunc(tx)
3✔
3294
        }
3✔
3295

3296
        return targetNode, err
3✔
3297
}
3298

3299
// computeEdgePolicyKeys is a helper function that can be used to compute the
3300
// keys used to index the channel edge policy info for the two nodes of the
3301
// edge. The keys for node 1 and node 2 are returned respectively.
3302
func computeEdgePolicyKeys(info *models.ChannelEdgeInfo) ([]byte, []byte) {
3✔
3303
        var (
3✔
3304
                node1Key [33 + 8]byte
3✔
3305
                node2Key [33 + 8]byte
3✔
3306
        )
3✔
3307

3✔
3308
        copy(node1Key[:], info.NodeKey1Bytes[:])
3✔
3309
        copy(node2Key[:], info.NodeKey2Bytes[:])
3✔
3310

3✔
3311
        byteOrder.PutUint64(node1Key[33:], info.ChannelID)
3✔
3312
        byteOrder.PutUint64(node2Key[33:], info.ChannelID)
3✔
3313

3✔
3314
        return node1Key[:], node2Key[:]
3✔
3315
}
3✔
3316

3317
// FetchChannelEdgesByOutpoint attempts to lookup the two directed edges for
3318
// the channel identified by the funding outpoint. If the channel can't be
3319
// found, then ErrEdgeNotFound is returned. A struct which houses the general
3320
// information for the channel itself is returned as well as two structs that
3321
// contain the routing policies for the channel in either direction.
3322
func (c *ChannelGraph) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
3323
        *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3324
        *models.ChannelEdgePolicy, error) {
3✔
3325

3✔
3326
        var (
3✔
3327
                edgeInfo *models.ChannelEdgeInfo
3✔
3328
                policy1  *models.ChannelEdgePolicy
3✔
3329
                policy2  *models.ChannelEdgePolicy
3✔
3330
        )
3✔
3331

3✔
3332
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
3333
                // First, grab the node bucket. This will be used to populate
3✔
3334
                // the Node pointers in each edge read from disk.
3✔
3335
                nodes := tx.ReadBucket(nodeBucket)
3✔
3336
                if nodes == nil {
3✔
3337
                        return ErrGraphNotFound
×
3338
                }
×
3339

3340
                // Next, grab the edge bucket which stores the edges, and also
3341
                // the index itself so we can group the directed edges together
3342
                // logically.
3343
                edges := tx.ReadBucket(edgeBucket)
3✔
3344
                if edges == nil {
3✔
3345
                        return ErrGraphNoEdgesFound
×
3346
                }
×
3347
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
3348
                if edgeIndex == nil {
3✔
3349
                        return ErrGraphNoEdgesFound
×
3350
                }
×
3351

3352
                // If the channel's outpoint doesn't exist within the outpoint
3353
                // index, then the edge does not exist.
3354
                chanIndex := edges.NestedReadBucket(channelPointBucket)
3✔
3355
                if chanIndex == nil {
3✔
3356
                        return ErrGraphNoEdgesFound
×
3357
                }
×
3358
                var b bytes.Buffer
3✔
3359
                if err := WriteOutpoint(&b, op); err != nil {
3✔
3360
                        return err
×
3361
                }
×
3362
                chanID := chanIndex.Get(b.Bytes())
3✔
3363
                if chanID == nil {
6✔
3364
                        return fmt.Errorf("%w: op=%v", ErrEdgeNotFound, op)
3✔
3365
                }
3✔
3366

3367
                // If the channel is found to exists, then we'll first retrieve
3368
                // the general information for the channel.
3369
                edge, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
3370
                if err != nil {
3✔
3371
                        return fmt.Errorf("%w: chanID=%x", err, chanID)
×
3372
                }
×
3373
                edgeInfo = &edge
3✔
3374

3✔
3375
                // Once we have the information about the channels' parameters,
3✔
3376
                // we'll fetch the routing policies for each for the directed
3✔
3377
                // edges.
3✔
3378
                e1, e2, err := fetchChanEdgePolicies(edgeIndex, edges, chanID)
3✔
3379
                if err != nil {
3✔
3380
                        return fmt.Errorf("failed to find policy: %w", err)
×
3381
                }
×
3382

3383
                policy1 = e1
3✔
3384
                policy2 = e2
3✔
3385
                return nil
3✔
3386
        }, func() {
3✔
3387
                edgeInfo = nil
3✔
3388
                policy1 = nil
3✔
3389
                policy2 = nil
3✔
3390
        })
3✔
3391
        if err != nil {
6✔
3392
                return nil, nil, nil, err
3✔
3393
        }
3✔
3394

3395
        return edgeInfo, policy1, policy2, nil
3✔
3396
}
3397

3398
// FetchChannelEdgesByID attempts to lookup the two directed edges for the
3399
// channel identified by the channel ID. If the channel can't be found, then
3400
// ErrEdgeNotFound is returned. A struct which houses the general information
3401
// for the channel itself is returned as well as two structs that contain the
3402
// routing policies for the channel in either direction.
3403
//
3404
// ErrZombieEdge an be returned if the edge is currently marked as a zombie
3405
// within the database. In this case, the ChannelEdgePolicy's will be nil, and
3406
// the ChannelEdgeInfo will only include the public keys of each node.
3407
func (c *ChannelGraph) FetchChannelEdgesByID(chanID uint64) (
3408
        *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3409
        *models.ChannelEdgePolicy, error) {
3✔
3410

3✔
3411
        var (
3✔
3412
                edgeInfo  *models.ChannelEdgeInfo
3✔
3413
                policy1   *models.ChannelEdgePolicy
3✔
3414
                policy2   *models.ChannelEdgePolicy
3✔
3415
                channelID [8]byte
3✔
3416
        )
3✔
3417

3✔
3418
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
3419
                // First, grab the node bucket. This will be used to populate
3✔
3420
                // the Node pointers in each edge read from disk.
3✔
3421
                nodes := tx.ReadBucket(nodeBucket)
3✔
3422
                if nodes == nil {
3✔
3423
                        return ErrGraphNotFound
×
3424
                }
×
3425

3426
                // Next, grab the edge bucket which stores the edges, and also
3427
                // the index itself so we can group the directed edges together
3428
                // logically.
3429
                edges := tx.ReadBucket(edgeBucket)
3✔
3430
                if edges == nil {
3✔
3431
                        return ErrGraphNoEdgesFound
×
3432
                }
×
3433
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
3434
                if edgeIndex == nil {
3✔
3435
                        return ErrGraphNoEdgesFound
×
3436
                }
×
3437

3438
                byteOrder.PutUint64(channelID[:], chanID)
3✔
3439

3✔
3440
                // Now, attempt to fetch edge.
3✔
3441
                edge, err := fetchChanEdgeInfo(edgeIndex, channelID[:])
3✔
3442

3✔
3443
                // If it doesn't exist, we'll quickly check our zombie index to
3✔
3444
                // see if we've previously marked it as so.
3✔
3445
                if errors.Is(err, ErrEdgeNotFound) {
6✔
3446
                        // If the zombie index doesn't exist, or the edge is not
3✔
3447
                        // marked as a zombie within it, then we'll return the
3✔
3448
                        // original ErrEdgeNotFound error.
3✔
3449
                        zombieIndex := edges.NestedReadBucket(zombieBucket)
3✔
3450
                        if zombieIndex == nil {
3✔
3451
                                return ErrEdgeNotFound
×
3452
                        }
×
3453

3454
                        isZombie, pubKey1, pubKey2 := isZombieEdge(
3✔
3455
                                zombieIndex, chanID,
3✔
3456
                        )
3✔
3457
                        if !isZombie {
6✔
3458
                                return ErrEdgeNotFound
3✔
3459
                        }
3✔
3460

3461
                        // Otherwise, the edge is marked as a zombie, so we'll
3462
                        // populate the edge info with the public keys of each
3463
                        // party as this is the only information we have about
3464
                        // it and return an error signaling so.
3465
                        edgeInfo = &models.ChannelEdgeInfo{
3✔
3466
                                NodeKey1Bytes: pubKey1,
3✔
3467
                                NodeKey2Bytes: pubKey2,
3✔
3468
                        }
3✔
3469
                        return ErrZombieEdge
3✔
3470
                }
3471

3472
                // Otherwise, we'll just return the error if any.
3473
                if err != nil {
3✔
3474
                        return err
×
3475
                }
×
3476

3477
                edgeInfo = &edge
3✔
3478

3✔
3479
                // Then we'll attempt to fetch the accompanying policies of this
3✔
3480
                // edge.
3✔
3481
                e1, e2, err := fetchChanEdgePolicies(
3✔
3482
                        edgeIndex, edges, channelID[:],
3✔
3483
                )
3✔
3484
                if err != nil {
3✔
3485
                        return err
×
3486
                }
×
3487

3488
                policy1 = e1
3✔
3489
                policy2 = e2
3✔
3490
                return nil
3✔
3491
        }, func() {
3✔
3492
                edgeInfo = nil
3✔
3493
                policy1 = nil
3✔
3494
                policy2 = nil
3✔
3495
        })
3✔
3496
        if err == ErrZombieEdge {
6✔
3497
                return edgeInfo, nil, nil, err
3✔
3498
        }
3✔
3499
        if err != nil {
6✔
3500
                return nil, nil, nil, err
3✔
3501
        }
3✔
3502

3503
        return edgeInfo, policy1, policy2, nil
3✔
3504
}
3505

3506
// IsPublicNode is a helper method that determines whether the node with the
3507
// given public key is seen as a public node in the graph from the graph's
3508
// source node's point of view.
3509
func (c *ChannelGraph) IsPublicNode(pubKey [33]byte) (bool, error) {
3✔
3510
        var nodeIsPublic bool
3✔
3511
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
3512
                nodes := tx.ReadBucket(nodeBucket)
3✔
3513
                if nodes == nil {
3✔
3514
                        return ErrGraphNodesNotFound
×
3515
                }
×
3516
                ourPubKey := nodes.Get(sourceKey)
3✔
3517
                if ourPubKey == nil {
3✔
3518
                        return ErrSourceNodeNotSet
×
3519
                }
×
3520
                node, err := fetchLightningNode(nodes, pubKey[:])
3✔
3521
                if err != nil {
3✔
3522
                        return err
×
3523
                }
×
3524

3525
                nodeIsPublic, err = c.isPublic(tx, node.PubKeyBytes, ourPubKey)
3✔
3526
                return err
3✔
3527
        }, func() {
3✔
3528
                nodeIsPublic = false
3✔
3529
        })
3✔
3530
        if err != nil {
3✔
3531
                return false, err
×
3532
        }
×
3533

3534
        return nodeIsPublic, nil
3✔
3535
}
3536

3537
// genMultiSigP2WSH generates the p2wsh'd multisig script for 2 of 2 pubkeys.
3538
func genMultiSigP2WSH(aPub, bPub []byte) ([]byte, error) {
3✔
3539
        witnessScript, err := input.GenMultiSigScript(aPub, bPub)
3✔
3540
        if err != nil {
3✔
3541
                return nil, err
×
3542
        }
×
3543

3544
        // With the witness script generated, we'll now turn it into a p2wsh
3545
        // script:
3546
        //  * OP_0 <sha256(script)>
3547
        bldr := txscript.NewScriptBuilder(
3✔
3548
                txscript.WithScriptAllocSize(input.P2WSHSize),
3✔
3549
        )
3✔
3550
        bldr.AddOp(txscript.OP_0)
3✔
3551
        scriptHash := sha256.Sum256(witnessScript)
3✔
3552
        bldr.AddData(scriptHash[:])
3✔
3553

3✔
3554
        return bldr.Script()
3✔
3555
}
3556

3557
// EdgePoint couples the outpoint of a channel with the funding script that it
3558
// creates. The FilteredChainView will use this to watch for spends of this
3559
// edge point on chain. We require both of these values as depending on the
3560
// concrete implementation, either the pkScript, or the out point will be used.
3561
type EdgePoint struct {
3562
        // FundingPkScript is the p2wsh multi-sig script of the target channel.
3563
        FundingPkScript []byte
3564

3565
        // OutPoint is the outpoint of the target channel.
3566
        OutPoint wire.OutPoint
3567
}
3568

3569
// String returns a human readable version of the target EdgePoint. We return
3570
// the outpoint directly as it is enough to uniquely identify the edge point.
3571
func (e *EdgePoint) String() string {
×
3572
        return e.OutPoint.String()
×
3573
}
×
3574

3575
// ChannelView returns the verifiable edge information for each active channel
3576
// within the known channel graph. The set of UTXO's (along with their scripts)
3577
// returned are the ones that need to be watched on chain to detect channel
3578
// closes on the resident blockchain.
3579
func (c *ChannelGraph) ChannelView() ([]EdgePoint, error) {
3✔
3580
        var edgePoints []EdgePoint
3✔
3581
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
3582
                // We're going to iterate over the entire channel index, so
3✔
3583
                // we'll need to fetch the edgeBucket to get to the index as
3✔
3584
                // it's a sub-bucket.
3✔
3585
                edges := tx.ReadBucket(edgeBucket)
3✔
3586
                if edges == nil {
3✔
3587
                        return ErrGraphNoEdgesFound
×
3588
                }
×
3589
                chanIndex := edges.NestedReadBucket(channelPointBucket)
3✔
3590
                if chanIndex == nil {
3✔
3591
                        return ErrGraphNoEdgesFound
×
3592
                }
×
3593
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
3594
                if edgeIndex == nil {
3✔
3595
                        return ErrGraphNoEdgesFound
×
3596
                }
×
3597

3598
                // Once we have the proper bucket, we'll range over each key
3599
                // (which is the channel point for the channel) and decode it,
3600
                // accumulating each entry.
3601
                return chanIndex.ForEach(
3✔
3602
                        func(chanPointBytes, chanID []byte) error {
6✔
3603
                                chanPointReader := bytes.NewReader(
3✔
3604
                                        chanPointBytes,
3✔
3605
                                )
3✔
3606

3✔
3607
                                var chanPoint wire.OutPoint
3✔
3608
                                err := ReadOutpoint(chanPointReader, &chanPoint)
3✔
3609
                                if err != nil {
3✔
3610
                                        return err
×
3611
                                }
×
3612

3613
                                edgeInfo, err := fetchChanEdgeInfo(
3✔
3614
                                        edgeIndex, chanID,
3✔
3615
                                )
3✔
3616
                                if err != nil {
3✔
3617
                                        return err
×
3618
                                }
×
3619

3620
                                pkScript, err := genMultiSigP2WSH(
3✔
3621
                                        edgeInfo.BitcoinKey1Bytes[:],
3✔
3622
                                        edgeInfo.BitcoinKey2Bytes[:],
3✔
3623
                                )
3✔
3624
                                if err != nil {
3✔
3625
                                        return err
×
3626
                                }
×
3627

3628
                                edgePoints = append(edgePoints, EdgePoint{
3✔
3629
                                        FundingPkScript: pkScript,
3✔
3630
                                        OutPoint:        chanPoint,
3✔
3631
                                })
3✔
3632

3✔
3633
                                return nil
3✔
3634
                        },
3635
                )
3636
        }, func() {
3✔
3637
                edgePoints = nil
3✔
3638
        }); err != nil {
3✔
3639
                return nil, err
×
3640
        }
×
3641

3642
        return edgePoints, nil
3✔
3643
}
3644

3645
// MarkEdgeZombie attempts to mark a channel identified by its channel ID as a
3646
// zombie. This method is used on an ad-hoc basis, when channels need to be
3647
// marked as zombies outside the normal pruning cycle.
3648
func (c *ChannelGraph) MarkEdgeZombie(chanID uint64,
UNCOV
3649
        pubKey1, pubKey2 [33]byte) error {
×
UNCOV
3650

×
UNCOV
3651
        c.cacheMu.Lock()
×
UNCOV
3652
        defer c.cacheMu.Unlock()
×
UNCOV
3653

×
UNCOV
3654
        err := kvdb.Batch(c.db, func(tx kvdb.RwTx) error {
×
UNCOV
3655
                edges := tx.ReadWriteBucket(edgeBucket)
×
UNCOV
3656
                if edges == nil {
×
3657
                        return ErrGraphNoEdgesFound
×
3658
                }
×
UNCOV
3659
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
×
UNCOV
3660
                if err != nil {
×
3661
                        return fmt.Errorf("unable to create zombie "+
×
3662
                                "bucket: %w", err)
×
3663
                }
×
3664

UNCOV
3665
                if c.graphCache != nil {
×
UNCOV
3666
                        c.graphCache.RemoveChannel(pubKey1, pubKey2, chanID)
×
UNCOV
3667
                }
×
3668

UNCOV
3669
                return markEdgeZombie(zombieIndex, chanID, pubKey1, pubKey2)
×
3670
        })
UNCOV
3671
        if err != nil {
×
3672
                return err
×
3673
        }
×
3674

UNCOV
3675
        c.rejectCache.remove(chanID)
×
UNCOV
3676
        c.chanCache.remove(chanID)
×
UNCOV
3677

×
UNCOV
3678
        return nil
×
3679
}
3680

3681
// markEdgeZombie marks an edge as a zombie within our zombie index. The public
3682
// keys should represent the node public keys of the two parties involved in the
3683
// edge.
3684
func markEdgeZombie(zombieIndex kvdb.RwBucket, chanID uint64, pubKey1,
3685
        pubKey2 [33]byte) error {
3✔
3686

3✔
3687
        var k [8]byte
3✔
3688
        byteOrder.PutUint64(k[:], chanID)
3✔
3689

3✔
3690
        var v [66]byte
3✔
3691
        copy(v[:33], pubKey1[:])
3✔
3692
        copy(v[33:], pubKey2[:])
3✔
3693

3✔
3694
        return zombieIndex.Put(k[:], v[:])
3✔
3695
}
3✔
3696

3697
// MarkEdgeLive clears an edge from our zombie index, deeming it as live.
UNCOV
3698
func (c *ChannelGraph) MarkEdgeLive(chanID uint64) error {
×
UNCOV
3699
        c.cacheMu.Lock()
×
UNCOV
3700
        defer c.cacheMu.Unlock()
×
UNCOV
3701

×
UNCOV
3702
        return c.markEdgeLiveUnsafe(nil, chanID)
×
UNCOV
3703
}
×
3704

3705
// markEdgeLiveUnsafe clears an edge from the zombie index. This method can be
3706
// called with an existing kvdb.RwTx or the argument can be set to nil in which
3707
// case a new transaction will be created.
3708
//
3709
// NOTE: this method MUST only be called if the cacheMu has already been
3710
// acquired.
UNCOV
3711
func (c *ChannelGraph) markEdgeLiveUnsafe(tx kvdb.RwTx, chanID uint64) error {
×
UNCOV
3712
        dbFn := func(tx kvdb.RwTx) error {
×
UNCOV
3713
                edges := tx.ReadWriteBucket(edgeBucket)
×
UNCOV
3714
                if edges == nil {
×
3715
                        return ErrGraphNoEdgesFound
×
3716
                }
×
UNCOV
3717
                zombieIndex := edges.NestedReadWriteBucket(zombieBucket)
×
UNCOV
3718
                if zombieIndex == nil {
×
3719
                        return nil
×
3720
                }
×
3721

UNCOV
3722
                var k [8]byte
×
UNCOV
3723
                byteOrder.PutUint64(k[:], chanID)
×
UNCOV
3724

×
UNCOV
3725
                if len(zombieIndex.Get(k[:])) == 0 {
×
UNCOV
3726
                        return ErrZombieEdgeNotFound
×
UNCOV
3727
                }
×
3728

UNCOV
3729
                return zombieIndex.Delete(k[:])
×
3730
        }
3731

3732
        // If the transaction is nil, we'll create a new one. Otherwise, we use
3733
        // the existing transaction
UNCOV
3734
        var err error
×
UNCOV
3735
        if tx == nil {
×
UNCOV
3736
                err = kvdb.Update(c.db, dbFn, func() {})
×
UNCOV
3737
        } else {
×
UNCOV
3738
                err = dbFn(tx)
×
UNCOV
3739
        }
×
UNCOV
3740
        if err != nil {
×
UNCOV
3741
                return err
×
UNCOV
3742
        }
×
3743

UNCOV
3744
        c.rejectCache.remove(chanID)
×
UNCOV
3745
        c.chanCache.remove(chanID)
×
UNCOV
3746

×
UNCOV
3747
        // We need to add the channel back into our graph cache, otherwise we
×
UNCOV
3748
        // won't use it for path finding.
×
UNCOV
3749
        if c.graphCache != nil {
×
UNCOV
3750
                edgeInfos, err := c.fetchChanInfos(tx, []uint64{chanID})
×
UNCOV
3751
                if err != nil {
×
3752
                        return err
×
3753
                }
×
3754

UNCOV
3755
                for _, edgeInfo := range edgeInfos {
×
3756
                        c.graphCache.AddChannel(
×
3757
                                edgeInfo.Info, edgeInfo.Policy1,
×
3758
                                edgeInfo.Policy2,
×
3759
                        )
×
3760
                }
×
3761
        }
3762

UNCOV
3763
        return nil
×
3764
}
3765

3766
// IsZombieEdge returns whether the edge is considered zombie. If it is a
3767
// zombie, then the two node public keys corresponding to this edge are also
3768
// returned.
UNCOV
3769
func (c *ChannelGraph) IsZombieEdge(chanID uint64) (bool, [33]byte, [33]byte) {
×
UNCOV
3770
        var (
×
UNCOV
3771
                isZombie         bool
×
UNCOV
3772
                pubKey1, pubKey2 [33]byte
×
UNCOV
3773
        )
×
UNCOV
3774

×
UNCOV
3775
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
×
UNCOV
3776
                edges := tx.ReadBucket(edgeBucket)
×
UNCOV
3777
                if edges == nil {
×
3778
                        return ErrGraphNoEdgesFound
×
3779
                }
×
UNCOV
3780
                zombieIndex := edges.NestedReadBucket(zombieBucket)
×
UNCOV
3781
                if zombieIndex == nil {
×
3782
                        return nil
×
3783
                }
×
3784

UNCOV
3785
                isZombie, pubKey1, pubKey2 = isZombieEdge(zombieIndex, chanID)
×
UNCOV
3786
                return nil
×
UNCOV
3787
        }, func() {
×
UNCOV
3788
                isZombie = false
×
UNCOV
3789
                pubKey1 = [33]byte{}
×
UNCOV
3790
                pubKey2 = [33]byte{}
×
UNCOV
3791
        })
×
UNCOV
3792
        if err != nil {
×
3793
                return false, [33]byte{}, [33]byte{}
×
3794
        }
×
3795

UNCOV
3796
        return isZombie, pubKey1, pubKey2
×
3797
}
3798

3799
// isZombieEdge returns whether an entry exists for the given channel in the
3800
// zombie index. If an entry exists, then the two node public keys corresponding
3801
// to this edge are also returned.
3802
func isZombieEdge(zombieIndex kvdb.RBucket,
3803
        chanID uint64) (bool, [33]byte, [33]byte) {
3✔
3804

3✔
3805
        var k [8]byte
3✔
3806
        byteOrder.PutUint64(k[:], chanID)
3✔
3807

3✔
3808
        v := zombieIndex.Get(k[:])
3✔
3809
        if v == nil {
6✔
3810
                return false, [33]byte{}, [33]byte{}
3✔
3811
        }
3✔
3812

3813
        var pubKey1, pubKey2 [33]byte
3✔
3814
        copy(pubKey1[:], v[:33])
3✔
3815
        copy(pubKey2[:], v[33:])
3✔
3816

3✔
3817
        return true, pubKey1, pubKey2
3✔
3818
}
3819

3820
// NumZombies returns the current number of zombie channels in the graph.
UNCOV
3821
func (c *ChannelGraph) NumZombies() (uint64, error) {
×
UNCOV
3822
        var numZombies uint64
×
UNCOV
3823
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
×
UNCOV
3824
                edges := tx.ReadBucket(edgeBucket)
×
UNCOV
3825
                if edges == nil {
×
3826
                        return nil
×
3827
                }
×
UNCOV
3828
                zombieIndex := edges.NestedReadBucket(zombieBucket)
×
UNCOV
3829
                if zombieIndex == nil {
×
3830
                        return nil
×
3831
                }
×
3832

UNCOV
3833
                return zombieIndex.ForEach(func(_, _ []byte) error {
×
UNCOV
3834
                        numZombies++
×
UNCOV
3835
                        return nil
×
UNCOV
3836
                })
×
UNCOV
3837
        }, func() {
×
UNCOV
3838
                numZombies = 0
×
UNCOV
3839
        })
×
UNCOV
3840
        if err != nil {
×
3841
                return 0, err
×
3842
        }
×
3843

UNCOV
3844
        return numZombies, nil
×
3845
}
3846

3847
// PutClosedScid stores a SCID for a closed channel in the database. This is so
3848
// that we can ignore channel announcements that we know to be closed without
3849
// having to validate them and fetch a block.
UNCOV
3850
func (c *ChannelGraph) PutClosedScid(scid lnwire.ShortChannelID) error {
×
UNCOV
3851
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
×
UNCOV
3852
                closedScids, err := tx.CreateTopLevelBucket(closedScidBucket)
×
UNCOV
3853
                if err != nil {
×
3854
                        return err
×
3855
                }
×
3856

UNCOV
3857
                var k [8]byte
×
UNCOV
3858
                byteOrder.PutUint64(k[:], scid.ToUint64())
×
UNCOV
3859

×
UNCOV
3860
                return closedScids.Put(k[:], []byte{})
×
UNCOV
3861
        }, func() {})
×
3862
}
3863

3864
// IsClosedScid checks whether a channel identified by the passed in scid is
3865
// closed. This helps avoid having to perform expensive validation checks.
3866
// TODO: Add an LRU cache to cut down on disc reads.
3867
func (c *ChannelGraph) IsClosedScid(scid lnwire.ShortChannelID) (bool, error) {
3✔
3868
        var isClosed bool
3✔
3869
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
3870
                closedScids := tx.ReadBucket(closedScidBucket)
3✔
3871
                if closedScids == nil {
3✔
3872
                        return ErrClosedScidsNotFound
×
3873
                }
×
3874

3875
                var k [8]byte
3✔
3876
                byteOrder.PutUint64(k[:], scid.ToUint64())
3✔
3877

3✔
3878
                if closedScids.Get(k[:]) != nil {
3✔
UNCOV
3879
                        isClosed = true
×
UNCOV
3880
                        return nil
×
UNCOV
3881
                }
×
3882

3883
                return nil
3✔
3884
        }, func() {
3✔
3885
                isClosed = false
3✔
3886
        })
3✔
3887
        if err != nil {
3✔
3888
                return false, err
×
3889
        }
×
3890

3891
        return isClosed, nil
3✔
3892
}
3893

3894
// GraphSession will provide the call-back with access to a NodeTraverser
3895
// instance which can be used to perform queries against the channel graph. If
3896
// the graph cache is not enabled, then the call-back will  be provided with
3897
// access to the graph via a consistent read-only transaction.
3898
func (c *ChannelGraph) GraphSession(cb func(graph NodeTraverser) error) error {
3✔
3899
        if c.graphCache != nil {
6✔
3900
                return cb(&nodeTraverserSession{db: c})
3✔
3901
        }
3✔
3902

UNCOV
3903
        return c.db.View(func(tx walletdb.ReadTx) error {
×
UNCOV
3904
                return cb(&nodeTraverserSession{
×
UNCOV
3905
                        db: c,
×
UNCOV
3906
                        tx: tx,
×
UNCOV
3907
                })
×
UNCOV
3908
        }, func() {})
×
3909
}
3910

3911
// nodeTraverserSession implements the NodeTraverser interface but with a
3912
// backing read only transaction for a consistent view of the graph in the case
3913
// where the graph Cache has not been enabled.
3914
type nodeTraverserSession struct {
3915
        tx kvdb.RTx
3916
        db *ChannelGraph
3917
}
3918

3919
// ForEachNodeDirectedChannel calls the callback for every channel of the given
3920
// node.
3921
//
3922
// NOTE: Part of the NodeTraverser interface.
3923
func (c *nodeTraverserSession) ForEachNodeDirectedChannel(nodePub route.Vertex,
3924
        cb func(channel *DirectedChannel) error) error {
3✔
3925

3✔
3926
        return c.db.forEachNodeDirectedChannel(c.tx, nodePub, cb)
3✔
3927
}
3✔
3928

3929
// FetchNodeFeatures returns the features of the given node. If the node is
3930
// unknown, assume no additional features are supported.
3931
//
3932
// NOTE: Part of the NodeTraverser interface.
3933
func (c *nodeTraverserSession) FetchNodeFeatures(nodePub route.Vertex) (
3934
        *lnwire.FeatureVector, error) {
3✔
3935

3✔
3936
        return c.db.fetchNodeFeatures(c.tx, nodePub)
3✔
3937
}
3✔
3938

3939
func putLightningNode(nodeBucket kvdb.RwBucket, aliasBucket kvdb.RwBucket, // nolint:dupl
3940
        updateIndex kvdb.RwBucket, node *models.LightningNode) error {
3✔
3941

3✔
3942
        var (
3✔
3943
                scratch [16]byte
3✔
3944
                b       bytes.Buffer
3✔
3945
        )
3✔
3946

3✔
3947
        pub, err := node.PubKey()
3✔
3948
        if err != nil {
3✔
3949
                return err
×
3950
        }
×
3951
        nodePub := pub.SerializeCompressed()
3✔
3952

3✔
3953
        // If the node has the update time set, write it, else write 0.
3✔
3954
        updateUnix := uint64(0)
3✔
3955
        if node.LastUpdate.Unix() > 0 {
6✔
3956
                updateUnix = uint64(node.LastUpdate.Unix())
3✔
3957
        }
3✔
3958

3959
        byteOrder.PutUint64(scratch[:8], updateUnix)
3✔
3960
        if _, err := b.Write(scratch[:8]); err != nil {
3✔
3961
                return err
×
3962
        }
×
3963

3964
        if _, err := b.Write(nodePub); err != nil {
3✔
3965
                return err
×
3966
        }
×
3967

3968
        // If we got a node announcement for this node, we will have the rest
3969
        // of the data available. If not we don't have more data to write.
3970
        if !node.HaveNodeAnnouncement {
6✔
3971
                // Write HaveNodeAnnouncement=0.
3✔
3972
                byteOrder.PutUint16(scratch[:2], 0)
3✔
3973
                if _, err := b.Write(scratch[:2]); err != nil {
3✔
3974
                        return err
×
3975
                }
×
3976

3977
                return nodeBucket.Put(nodePub, b.Bytes())
3✔
3978
        }
3979

3980
        // Write HaveNodeAnnouncement=1.
3981
        byteOrder.PutUint16(scratch[:2], 1)
3✔
3982
        if _, err := b.Write(scratch[:2]); err != nil {
3✔
3983
                return err
×
3984
        }
×
3985

3986
        if err := binary.Write(&b, byteOrder, node.Color.R); err != nil {
3✔
3987
                return err
×
3988
        }
×
3989
        if err := binary.Write(&b, byteOrder, node.Color.G); err != nil {
3✔
3990
                return err
×
3991
        }
×
3992
        if err := binary.Write(&b, byteOrder, node.Color.B); err != nil {
3✔
3993
                return err
×
3994
        }
×
3995

3996
        if err := wire.WriteVarString(&b, 0, node.Alias); err != nil {
3✔
3997
                return err
×
3998
        }
×
3999

4000
        if err := node.Features.Encode(&b); err != nil {
3✔
4001
                return err
×
4002
        }
×
4003

4004
        numAddresses := uint16(len(node.Addresses))
3✔
4005
        byteOrder.PutUint16(scratch[:2], numAddresses)
3✔
4006
        if _, err := b.Write(scratch[:2]); err != nil {
3✔
4007
                return err
×
4008
        }
×
4009

4010
        for _, address := range node.Addresses {
6✔
4011
                if err := SerializeAddr(&b, address); err != nil {
3✔
4012
                        return err
×
4013
                }
×
4014
        }
4015

4016
        sigLen := len(node.AuthSigBytes)
3✔
4017
        if sigLen > 80 {
3✔
4018
                return fmt.Errorf("max sig len allowed is 80, had %v",
×
4019
                        sigLen)
×
4020
        }
×
4021

4022
        err = wire.WriteVarBytes(&b, 0, node.AuthSigBytes)
3✔
4023
        if err != nil {
3✔
4024
                return err
×
4025
        }
×
4026

4027
        if len(node.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
3✔
4028
                return ErrTooManyExtraOpaqueBytes(len(node.ExtraOpaqueData))
×
4029
        }
×
4030
        err = wire.WriteVarBytes(&b, 0, node.ExtraOpaqueData)
3✔
4031
        if err != nil {
3✔
4032
                return err
×
4033
        }
×
4034

4035
        if err := aliasBucket.Put(nodePub, []byte(node.Alias)); err != nil {
3✔
4036
                return err
×
4037
        }
×
4038

4039
        // With the alias bucket updated, we'll now update the index that
4040
        // tracks the time series of node updates.
4041
        var indexKey [8 + 33]byte
3✔
4042
        byteOrder.PutUint64(indexKey[:8], updateUnix)
3✔
4043
        copy(indexKey[8:], nodePub)
3✔
4044

3✔
4045
        // If there was already an old index entry for this node, then we'll
3✔
4046
        // delete the old one before we write the new entry.
3✔
4047
        if nodeBytes := nodeBucket.Get(nodePub); nodeBytes != nil {
6✔
4048
                // Extract out the old update time to we can reconstruct the
3✔
4049
                // prior index key to delete it from the index.
3✔
4050
                oldUpdateTime := nodeBytes[:8]
3✔
4051

3✔
4052
                var oldIndexKey [8 + 33]byte
3✔
4053
                copy(oldIndexKey[:8], oldUpdateTime)
3✔
4054
                copy(oldIndexKey[8:], nodePub)
3✔
4055

3✔
4056
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
3✔
4057
                        return err
×
4058
                }
×
4059
        }
4060

4061
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
3✔
4062
                return err
×
4063
        }
×
4064

4065
        return nodeBucket.Put(nodePub, b.Bytes())
3✔
4066
}
4067

4068
func fetchLightningNode(nodeBucket kvdb.RBucket,
4069
        nodePub []byte) (models.LightningNode, error) {
3✔
4070

3✔
4071
        nodeBytes := nodeBucket.Get(nodePub)
3✔
4072
        if nodeBytes == nil {
6✔
4073
                return models.LightningNode{}, ErrGraphNodeNotFound
3✔
4074
        }
3✔
4075

4076
        nodeReader := bytes.NewReader(nodeBytes)
3✔
4077
        return deserializeLightningNode(nodeReader)
3✔
4078
}
4079

4080
func deserializeLightningNodeCacheable(r io.Reader) (route.Vertex,
4081
        *lnwire.FeatureVector, error) {
3✔
4082

3✔
4083
        var (
3✔
4084
                pubKey      route.Vertex
3✔
4085
                features    = lnwire.EmptyFeatureVector()
3✔
4086
                nodeScratch [8]byte
3✔
4087
        )
3✔
4088

3✔
4089
        // Skip ahead:
3✔
4090
        // - LastUpdate (8 bytes)
3✔
4091
        if _, err := r.Read(nodeScratch[:]); err != nil {
3✔
4092
                return pubKey, nil, err
×
4093
        }
×
4094

4095
        if _, err := io.ReadFull(r, pubKey[:]); err != nil {
3✔
4096
                return pubKey, nil, err
×
4097
        }
×
4098

4099
        // Read the node announcement flag.
4100
        if _, err := r.Read(nodeScratch[:2]); err != nil {
3✔
4101
                return pubKey, nil, err
×
4102
        }
×
4103
        hasNodeAnn := byteOrder.Uint16(nodeScratch[:2])
3✔
4104

3✔
4105
        // The rest of the data is optional, and will only be there if we got a
3✔
4106
        // node announcement for this node.
3✔
4107
        if hasNodeAnn == 0 {
6✔
4108
                return pubKey, features, nil
3✔
4109
        }
3✔
4110

4111
        // We did get a node announcement for this node, so we'll have the rest
4112
        // of the data available.
4113
        var rgb uint8
3✔
4114
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
3✔
4115
                return pubKey, nil, err
×
4116
        }
×
4117
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
3✔
4118
                return pubKey, nil, err
×
4119
        }
×
4120
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
3✔
4121
                return pubKey, nil, err
×
4122
        }
×
4123

4124
        if _, err := wire.ReadVarString(r, 0); err != nil {
3✔
4125
                return pubKey, nil, err
×
4126
        }
×
4127

4128
        if err := features.Decode(r); err != nil {
3✔
4129
                return pubKey, nil, err
×
4130
        }
×
4131

4132
        return pubKey, features, nil
3✔
4133
}
4134

4135
func deserializeLightningNode(r io.Reader) (models.LightningNode, error) {
3✔
4136
        var (
3✔
4137
                node    models.LightningNode
3✔
4138
                scratch [8]byte
3✔
4139
                err     error
3✔
4140
        )
3✔
4141

3✔
4142
        // Always populate a feature vector, even if we don't have a node
3✔
4143
        // announcement and short circuit below.
3✔
4144
        node.Features = lnwire.EmptyFeatureVector()
3✔
4145

3✔
4146
        if _, err := r.Read(scratch[:]); err != nil {
3✔
4147
                return models.LightningNode{}, err
×
4148
        }
×
4149

4150
        unix := int64(byteOrder.Uint64(scratch[:]))
3✔
4151
        node.LastUpdate = time.Unix(unix, 0)
3✔
4152

3✔
4153
        if _, err := io.ReadFull(r, node.PubKeyBytes[:]); err != nil {
3✔
4154
                return models.LightningNode{}, err
×
4155
        }
×
4156

4157
        if _, err := r.Read(scratch[:2]); err != nil {
3✔
4158
                return models.LightningNode{}, err
×
4159
        }
×
4160

4161
        hasNodeAnn := byteOrder.Uint16(scratch[:2])
3✔
4162
        if hasNodeAnn == 1 {
6✔
4163
                node.HaveNodeAnnouncement = true
3✔
4164
        } else {
6✔
4165
                node.HaveNodeAnnouncement = false
3✔
4166
        }
3✔
4167

4168
        // The rest of the data is optional, and will only be there if we got a
4169
        // node announcement for this node.
4170
        if !node.HaveNodeAnnouncement {
6✔
4171
                return node, nil
3✔
4172
        }
3✔
4173

4174
        // We did get a node announcement for this node, so we'll have the rest
4175
        // of the data available.
4176
        if err := binary.Read(r, byteOrder, &node.Color.R); err != nil {
3✔
4177
                return models.LightningNode{}, err
×
4178
        }
×
4179
        if err := binary.Read(r, byteOrder, &node.Color.G); err != nil {
3✔
4180
                return models.LightningNode{}, err
×
4181
        }
×
4182
        if err := binary.Read(r, byteOrder, &node.Color.B); err != nil {
3✔
4183
                return models.LightningNode{}, err
×
4184
        }
×
4185

4186
        node.Alias, err = wire.ReadVarString(r, 0)
3✔
4187
        if err != nil {
3✔
4188
                return models.LightningNode{}, err
×
4189
        }
×
4190

4191
        err = node.Features.Decode(r)
3✔
4192
        if err != nil {
3✔
4193
                return models.LightningNode{}, err
×
4194
        }
×
4195

4196
        if _, err := r.Read(scratch[:2]); err != nil {
3✔
4197
                return models.LightningNode{}, err
×
4198
        }
×
4199
        numAddresses := int(byteOrder.Uint16(scratch[:2]))
3✔
4200

3✔
4201
        var addresses []net.Addr
3✔
4202
        for i := 0; i < numAddresses; i++ {
6✔
4203
                address, err := DeserializeAddr(r)
3✔
4204
                if err != nil {
3✔
4205
                        return models.LightningNode{}, err
×
4206
                }
×
4207
                addresses = append(addresses, address)
3✔
4208
        }
4209
        node.Addresses = addresses
3✔
4210

3✔
4211
        node.AuthSigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
3✔
4212
        if err != nil {
3✔
4213
                return models.LightningNode{}, err
×
4214
        }
×
4215

4216
        // We'll try and see if there are any opaque bytes left, if not, then
4217
        // we'll ignore the EOF error and return the node as is.
4218
        node.ExtraOpaqueData, err = wire.ReadVarBytes(
3✔
4219
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
3✔
4220
        )
3✔
4221
        switch {
3✔
4222
        case err == io.ErrUnexpectedEOF:
×
4223
        case err == io.EOF:
×
4224
        case err != nil:
×
4225
                return models.LightningNode{}, err
×
4226
        }
4227

4228
        return node, nil
3✔
4229
}
4230

4231
func putChanEdgeInfo(edgeIndex kvdb.RwBucket,
4232
        edgeInfo *models.ChannelEdgeInfo, chanID [8]byte) error {
3✔
4233

3✔
4234
        var b bytes.Buffer
3✔
4235

3✔
4236
        if _, err := b.Write(edgeInfo.NodeKey1Bytes[:]); err != nil {
3✔
4237
                return err
×
4238
        }
×
4239
        if _, err := b.Write(edgeInfo.NodeKey2Bytes[:]); err != nil {
3✔
4240
                return err
×
4241
        }
×
4242
        if _, err := b.Write(edgeInfo.BitcoinKey1Bytes[:]); err != nil {
3✔
4243
                return err
×
4244
        }
×
4245
        if _, err := b.Write(edgeInfo.BitcoinKey2Bytes[:]); err != nil {
3✔
4246
                return err
×
4247
        }
×
4248

4249
        if err := wire.WriteVarBytes(&b, 0, edgeInfo.Features); err != nil {
3✔
4250
                return err
×
4251
        }
×
4252

4253
        authProof := edgeInfo.AuthProof
3✔
4254
        var nodeSig1, nodeSig2, bitcoinSig1, bitcoinSig2 []byte
3✔
4255
        if authProof != nil {
6✔
4256
                nodeSig1 = authProof.NodeSig1Bytes
3✔
4257
                nodeSig2 = authProof.NodeSig2Bytes
3✔
4258
                bitcoinSig1 = authProof.BitcoinSig1Bytes
3✔
4259
                bitcoinSig2 = authProof.BitcoinSig2Bytes
3✔
4260
        }
3✔
4261

4262
        if err := wire.WriteVarBytes(&b, 0, nodeSig1); err != nil {
3✔
4263
                return err
×
4264
        }
×
4265
        if err := wire.WriteVarBytes(&b, 0, nodeSig2); err != nil {
3✔
4266
                return err
×
4267
        }
×
4268
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig1); err != nil {
3✔
4269
                return err
×
4270
        }
×
4271
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig2); err != nil {
3✔
4272
                return err
×
4273
        }
×
4274

4275
        if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
3✔
4276
                return err
×
4277
        }
×
4278
        err := binary.Write(&b, byteOrder, uint64(edgeInfo.Capacity))
3✔
4279
        if err != nil {
3✔
4280
                return err
×
4281
        }
×
4282
        if _, err := b.Write(chanID[:]); err != nil {
3✔
4283
                return err
×
4284
        }
×
4285
        if _, err := b.Write(edgeInfo.ChainHash[:]); err != nil {
3✔
4286
                return err
×
4287
        }
×
4288

4289
        if len(edgeInfo.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
3✔
4290
                return ErrTooManyExtraOpaqueBytes(len(edgeInfo.ExtraOpaqueData))
×
4291
        }
×
4292
        err = wire.WriteVarBytes(&b, 0, edgeInfo.ExtraOpaqueData)
3✔
4293
        if err != nil {
3✔
4294
                return err
×
4295
        }
×
4296

4297
        return edgeIndex.Put(chanID[:], b.Bytes())
3✔
4298
}
4299

4300
func fetchChanEdgeInfo(edgeIndex kvdb.RBucket,
4301
        chanID []byte) (models.ChannelEdgeInfo, error) {
3✔
4302

3✔
4303
        edgeInfoBytes := edgeIndex.Get(chanID)
3✔
4304
        if edgeInfoBytes == nil {
6✔
4305
                return models.ChannelEdgeInfo{}, ErrEdgeNotFound
3✔
4306
        }
3✔
4307

4308
        edgeInfoReader := bytes.NewReader(edgeInfoBytes)
3✔
4309
        return deserializeChanEdgeInfo(edgeInfoReader)
3✔
4310
}
4311

4312
func deserializeChanEdgeInfo(r io.Reader) (models.ChannelEdgeInfo, error) {
3✔
4313
        var (
3✔
4314
                err      error
3✔
4315
                edgeInfo models.ChannelEdgeInfo
3✔
4316
        )
3✔
4317

3✔
4318
        if _, err := io.ReadFull(r, edgeInfo.NodeKey1Bytes[:]); err != nil {
3✔
4319
                return models.ChannelEdgeInfo{}, err
×
4320
        }
×
4321
        if _, err := io.ReadFull(r, edgeInfo.NodeKey2Bytes[:]); err != nil {
3✔
4322
                return models.ChannelEdgeInfo{}, err
×
4323
        }
×
4324
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey1Bytes[:]); err != nil {
3✔
4325
                return models.ChannelEdgeInfo{}, err
×
4326
        }
×
4327
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey2Bytes[:]); err != nil {
3✔
4328
                return models.ChannelEdgeInfo{}, err
×
4329
        }
×
4330

4331
        edgeInfo.Features, err = wire.ReadVarBytes(r, 0, 900, "features")
3✔
4332
        if err != nil {
3✔
4333
                return models.ChannelEdgeInfo{}, err
×
4334
        }
×
4335

4336
        proof := &models.ChannelAuthProof{}
3✔
4337

3✔
4338
        proof.NodeSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4339
        if err != nil {
3✔
4340
                return models.ChannelEdgeInfo{}, err
×
4341
        }
×
4342
        proof.NodeSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4343
        if err != nil {
3✔
4344
                return models.ChannelEdgeInfo{}, err
×
4345
        }
×
4346
        proof.BitcoinSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4347
        if err != nil {
3✔
4348
                return models.ChannelEdgeInfo{}, err
×
4349
        }
×
4350
        proof.BitcoinSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4351
        if err != nil {
3✔
4352
                return models.ChannelEdgeInfo{}, err
×
4353
        }
×
4354

4355
        if !proof.IsEmpty() {
6✔
4356
                edgeInfo.AuthProof = proof
3✔
4357
        }
3✔
4358

4359
        edgeInfo.ChannelPoint = wire.OutPoint{}
3✔
4360
        if err := ReadOutpoint(r, &edgeInfo.ChannelPoint); err != nil {
3✔
4361
                return models.ChannelEdgeInfo{}, err
×
4362
        }
×
4363
        if err := binary.Read(r, byteOrder, &edgeInfo.Capacity); err != nil {
3✔
4364
                return models.ChannelEdgeInfo{}, err
×
4365
        }
×
4366
        if err := binary.Read(r, byteOrder, &edgeInfo.ChannelID); err != nil {
3✔
4367
                return models.ChannelEdgeInfo{}, err
×
4368
        }
×
4369

4370
        if _, err := io.ReadFull(r, edgeInfo.ChainHash[:]); err != nil {
3✔
4371
                return models.ChannelEdgeInfo{}, err
×
4372
        }
×
4373

4374
        // We'll try and see if there are any opaque bytes left, if not, then
4375
        // we'll ignore the EOF error and return the edge as is.
4376
        edgeInfo.ExtraOpaqueData, err = wire.ReadVarBytes(
3✔
4377
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
3✔
4378
        )
3✔
4379
        switch {
3✔
4380
        case err == io.ErrUnexpectedEOF:
×
4381
        case err == io.EOF:
×
4382
        case err != nil:
×
4383
                return models.ChannelEdgeInfo{}, err
×
4384
        }
4385

4386
        return edgeInfo, nil
3✔
4387
}
4388

4389
func putChanEdgePolicy(edges kvdb.RwBucket, edge *models.ChannelEdgePolicy,
4390
        from, to []byte) error {
3✔
4391

3✔
4392
        var edgeKey [33 + 8]byte
3✔
4393
        copy(edgeKey[:], from)
3✔
4394
        byteOrder.PutUint64(edgeKey[33:], edge.ChannelID)
3✔
4395

3✔
4396
        var b bytes.Buffer
3✔
4397
        if err := serializeChanEdgePolicy(&b, edge, to); err != nil {
3✔
4398
                return err
×
4399
        }
×
4400

4401
        // Before we write out the new edge, we'll create a new entry in the
4402
        // update index in order to keep it fresh.
4403
        updateUnix := uint64(edge.LastUpdate.Unix())
3✔
4404
        var indexKey [8 + 8]byte
3✔
4405
        byteOrder.PutUint64(indexKey[:8], updateUnix)
3✔
4406
        byteOrder.PutUint64(indexKey[8:], edge.ChannelID)
3✔
4407

3✔
4408
        updateIndex, err := edges.CreateBucketIfNotExists(edgeUpdateIndexBucket)
3✔
4409
        if err != nil {
3✔
4410
                return err
×
4411
        }
×
4412

4413
        // If there was already an entry for this edge, then we'll need to
4414
        // delete the old one to ensure we don't leave around any after-images.
4415
        // An unknown policy value does not have a update time recorded, so
4416
        // it also does not need to be removed.
4417
        if edgeBytes := edges.Get(edgeKey[:]); edgeBytes != nil &&
3✔
4418
                !bytes.Equal(edgeBytes[:], unknownPolicy) {
6✔
4419

3✔
4420
                // In order to delete the old entry, we'll need to obtain the
3✔
4421
                // *prior* update time in order to delete it. To do this, we'll
3✔
4422
                // need to deserialize the existing policy within the database
3✔
4423
                // (now outdated by the new one), and delete its corresponding
3✔
4424
                // entry within the update index. We'll ignore any
3✔
4425
                // ErrEdgePolicyOptionalFieldNotFound error, as we only need
3✔
4426
                // the channel ID and update time to delete the entry.
3✔
4427
                // TODO(halseth): get rid of these invalid policies in a
3✔
4428
                // migration.
3✔
4429
                oldEdgePolicy, err := deserializeChanEdgePolicy(
3✔
4430
                        bytes.NewReader(edgeBytes),
3✔
4431
                )
3✔
4432
                if err != nil && err != ErrEdgePolicyOptionalFieldNotFound {
3✔
4433
                        return err
×
4434
                }
×
4435

4436
                oldUpdateTime := uint64(oldEdgePolicy.LastUpdate.Unix())
3✔
4437

3✔
4438
                var oldIndexKey [8 + 8]byte
3✔
4439
                byteOrder.PutUint64(oldIndexKey[:8], oldUpdateTime)
3✔
4440
                byteOrder.PutUint64(oldIndexKey[8:], edge.ChannelID)
3✔
4441

3✔
4442
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
3✔
4443
                        return err
×
4444
                }
×
4445
        }
4446

4447
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
3✔
4448
                return err
×
4449
        }
×
4450

4451
        err = updateEdgePolicyDisabledIndex(
3✔
4452
                edges, edge.ChannelID,
3✔
4453
                edge.ChannelFlags&lnwire.ChanUpdateDirection > 0,
3✔
4454
                edge.IsDisabled(),
3✔
4455
        )
3✔
4456
        if err != nil {
3✔
4457
                return err
×
4458
        }
×
4459

4460
        return edges.Put(edgeKey[:], b.Bytes()[:])
3✔
4461
}
4462

4463
// updateEdgePolicyDisabledIndex is used to update the disabledEdgePolicyIndex
4464
// bucket by either add a new disabled ChannelEdgePolicy or remove an existing
4465
// one.
4466
// The direction represents the direction of the edge and disabled is used for
4467
// deciding whether to remove or add an entry to the bucket.
4468
// In general a channel is disabled if two entries for the same chanID exist
4469
// in this bucket.
4470
// Maintaining the bucket this way allows a fast retrieval of disabled
4471
// channels, for example when prune is needed.
4472
func updateEdgePolicyDisabledIndex(edges kvdb.RwBucket, chanID uint64,
4473
        direction bool, disabled bool) error {
3✔
4474

3✔
4475
        var disabledEdgeKey [8 + 1]byte
3✔
4476
        byteOrder.PutUint64(disabledEdgeKey[0:], chanID)
3✔
4477
        if direction {
6✔
4478
                disabledEdgeKey[8] = 1
3✔
4479
        }
3✔
4480

4481
        disabledEdgePolicyIndex, err := edges.CreateBucketIfNotExists(
3✔
4482
                disabledEdgePolicyBucket,
3✔
4483
        )
3✔
4484
        if err != nil {
3✔
4485
                return err
×
4486
        }
×
4487

4488
        if disabled {
6✔
4489
                return disabledEdgePolicyIndex.Put(disabledEdgeKey[:], []byte{})
3✔
4490
        }
3✔
4491

4492
        return disabledEdgePolicyIndex.Delete(disabledEdgeKey[:])
3✔
4493
}
4494

4495
// putChanEdgePolicyUnknown marks the edge policy as unknown
4496
// in the edges bucket.
4497
func putChanEdgePolicyUnknown(edges kvdb.RwBucket, channelID uint64,
4498
        from []byte) error {
3✔
4499

3✔
4500
        var edgeKey [33 + 8]byte
3✔
4501
        copy(edgeKey[:], from)
3✔
4502
        byteOrder.PutUint64(edgeKey[33:], channelID)
3✔
4503

3✔
4504
        if edges.Get(edgeKey[:]) != nil {
3✔
4505
                return fmt.Errorf("cannot write unknown policy for channel %v "+
×
4506
                        " when there is already a policy present", channelID)
×
4507
        }
×
4508

4509
        return edges.Put(edgeKey[:], unknownPolicy)
3✔
4510
}
4511

4512
func fetchChanEdgePolicy(edges kvdb.RBucket, chanID []byte,
4513
        nodePub []byte) (*models.ChannelEdgePolicy, error) {
3✔
4514

3✔
4515
        var edgeKey [33 + 8]byte
3✔
4516
        copy(edgeKey[:], nodePub)
3✔
4517
        copy(edgeKey[33:], chanID[:])
3✔
4518

3✔
4519
        edgeBytes := edges.Get(edgeKey[:])
3✔
4520
        if edgeBytes == nil {
3✔
4521
                return nil, ErrEdgeNotFound
×
4522
        }
×
4523

4524
        // No need to deserialize unknown policy.
4525
        if bytes.Equal(edgeBytes[:], unknownPolicy) {
6✔
4526
                return nil, nil
3✔
4527
        }
3✔
4528

4529
        edgeReader := bytes.NewReader(edgeBytes)
3✔
4530

3✔
4531
        ep, err := deserializeChanEdgePolicy(edgeReader)
3✔
4532
        switch {
3✔
4533
        // If the db policy was missing an expected optional field, we return
4534
        // nil as if the policy was unknown.
UNCOV
4535
        case err == ErrEdgePolicyOptionalFieldNotFound:
×
UNCOV
4536
                return nil, nil
×
4537

4538
        case err != nil:
×
4539
                return nil, err
×
4540
        }
4541

4542
        return ep, nil
3✔
4543
}
4544

4545
func fetchChanEdgePolicies(edgeIndex kvdb.RBucket, edges kvdb.RBucket,
4546
        chanID []byte) (*models.ChannelEdgePolicy, *models.ChannelEdgePolicy,
4547
        error) {
3✔
4548

3✔
4549
        edgeInfo := edgeIndex.Get(chanID)
3✔
4550
        if edgeInfo == nil {
3✔
4551
                return nil, nil, fmt.Errorf("%w: chanID=%x", ErrEdgeNotFound,
×
4552
                        chanID)
×
4553
        }
×
4554

4555
        // The first node is contained within the first half of the edge
4556
        // information. We only propagate the error here and below if it's
4557
        // something other than edge non-existence.
4558
        node1Pub := edgeInfo[:33]
3✔
4559
        edge1, err := fetchChanEdgePolicy(edges, chanID, node1Pub)
3✔
4560
        if err != nil {
3✔
4561
                return nil, nil, fmt.Errorf("%w: node1Pub=%x", ErrEdgeNotFound,
×
4562
                        node1Pub)
×
4563
        }
×
4564

4565
        // Similarly, the second node is contained within the latter
4566
        // half of the edge information.
4567
        node2Pub := edgeInfo[33:66]
3✔
4568
        edge2, err := fetchChanEdgePolicy(edges, chanID, node2Pub)
3✔
4569
        if err != nil {
3✔
4570
                return nil, nil, fmt.Errorf("%w: node2Pub=%x", ErrEdgeNotFound,
×
4571
                        node2Pub)
×
4572
        }
×
4573

4574
        return edge1, edge2, nil
3✔
4575
}
4576

4577
func serializeChanEdgePolicy(w io.Writer, edge *models.ChannelEdgePolicy,
4578
        to []byte) error {
3✔
4579

3✔
4580
        err := wire.WriteVarBytes(w, 0, edge.SigBytes)
3✔
4581
        if err != nil {
3✔
4582
                return err
×
4583
        }
×
4584

4585
        if err := binary.Write(w, byteOrder, edge.ChannelID); err != nil {
3✔
4586
                return err
×
4587
        }
×
4588

4589
        var scratch [8]byte
3✔
4590
        updateUnix := uint64(edge.LastUpdate.Unix())
3✔
4591
        byteOrder.PutUint64(scratch[:], updateUnix)
3✔
4592
        if _, err := w.Write(scratch[:]); err != nil {
3✔
4593
                return err
×
4594
        }
×
4595

4596
        if err := binary.Write(w, byteOrder, edge.MessageFlags); err != nil {
3✔
4597
                return err
×
4598
        }
×
4599
        if err := binary.Write(w, byteOrder, edge.ChannelFlags); err != nil {
3✔
4600
                return err
×
4601
        }
×
4602
        if err := binary.Write(w, byteOrder, edge.TimeLockDelta); err != nil {
3✔
4603
                return err
×
4604
        }
×
4605
        if err := binary.Write(w, byteOrder, uint64(edge.MinHTLC)); err != nil {
3✔
4606
                return err
×
4607
        }
×
4608
        err = binary.Write(w, byteOrder, uint64(edge.FeeBaseMSat))
3✔
4609
        if err != nil {
3✔
4610
                return err
×
4611
        }
×
4612
        err = binary.Write(
3✔
4613
                w, byteOrder, uint64(edge.FeeProportionalMillionths),
3✔
4614
        )
3✔
4615
        if err != nil {
3✔
4616
                return err
×
4617
        }
×
4618

4619
        if _, err := w.Write(to); err != nil {
3✔
4620
                return err
×
4621
        }
×
4622

4623
        // If the max_htlc field is present, we write it. To be compatible with
4624
        // older versions that wasn't aware of this field, we write it as part
4625
        // of the opaque data.
4626
        // TODO(halseth): clean up when moving to TLV.
4627
        var opaqueBuf bytes.Buffer
3✔
4628
        if edge.MessageFlags.HasMaxHtlc() {
6✔
4629
                err := binary.Write(&opaqueBuf, byteOrder, uint64(edge.MaxHTLC))
3✔
4630
                if err != nil {
3✔
4631
                        return err
×
4632
                }
×
4633
        }
4634

4635
        if len(edge.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
3✔
4636
                return ErrTooManyExtraOpaqueBytes(len(edge.ExtraOpaqueData))
×
4637
        }
×
4638
        if _, err := opaqueBuf.Write(edge.ExtraOpaqueData); err != nil {
3✔
4639
                return err
×
4640
        }
×
4641

4642
        if err := wire.WriteVarBytes(w, 0, opaqueBuf.Bytes()); err != nil {
3✔
4643
                return err
×
4644
        }
×
4645
        return nil
3✔
4646
}
4647

4648
func deserializeChanEdgePolicy(r io.Reader) (*models.ChannelEdgePolicy, error) {
3✔
4649
        // Deserialize the policy. Note that in case an optional field is not
3✔
4650
        // found, both an error and a populated policy object are returned.
3✔
4651
        edge, deserializeErr := deserializeChanEdgePolicyRaw(r)
3✔
4652
        if deserializeErr != nil &&
3✔
4653
                deserializeErr != ErrEdgePolicyOptionalFieldNotFound {
3✔
4654

×
4655
                return nil, deserializeErr
×
4656
        }
×
4657

4658
        return edge, deserializeErr
3✔
4659
}
4660

4661
func deserializeChanEdgePolicyRaw(r io.Reader) (*models.ChannelEdgePolicy,
4662
        error) {
3✔
4663

3✔
4664
        edge := &models.ChannelEdgePolicy{}
3✔
4665

3✔
4666
        var err error
3✔
4667
        edge.SigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
3✔
4668
        if err != nil {
3✔
4669
                return nil, err
×
4670
        }
×
4671

4672
        if err := binary.Read(r, byteOrder, &edge.ChannelID); err != nil {
3✔
4673
                return nil, err
×
4674
        }
×
4675

4676
        var scratch [8]byte
3✔
4677
        if _, err := r.Read(scratch[:]); err != nil {
3✔
4678
                return nil, err
×
4679
        }
×
4680
        unix := int64(byteOrder.Uint64(scratch[:]))
3✔
4681
        edge.LastUpdate = time.Unix(unix, 0)
3✔
4682

3✔
4683
        if err := binary.Read(r, byteOrder, &edge.MessageFlags); err != nil {
3✔
4684
                return nil, err
×
4685
        }
×
4686
        if err := binary.Read(r, byteOrder, &edge.ChannelFlags); err != nil {
3✔
4687
                return nil, err
×
4688
        }
×
4689
        if err := binary.Read(r, byteOrder, &edge.TimeLockDelta); err != nil {
3✔
4690
                return nil, err
×
4691
        }
×
4692

4693
        var n uint64
3✔
4694
        if err := binary.Read(r, byteOrder, &n); err != nil {
3✔
4695
                return nil, err
×
4696
        }
×
4697
        edge.MinHTLC = lnwire.MilliSatoshi(n)
3✔
4698

3✔
4699
        if err := binary.Read(r, byteOrder, &n); err != nil {
3✔
4700
                return nil, err
×
4701
        }
×
4702
        edge.FeeBaseMSat = lnwire.MilliSatoshi(n)
3✔
4703

3✔
4704
        if err := binary.Read(r, byteOrder, &n); err != nil {
3✔
4705
                return nil, err
×
4706
        }
×
4707
        edge.FeeProportionalMillionths = lnwire.MilliSatoshi(n)
3✔
4708

3✔
4709
        if _, err := r.Read(edge.ToNode[:]); err != nil {
3✔
4710
                return nil, err
×
4711
        }
×
4712

4713
        // We'll try and see if there are any opaque bytes left, if not, then
4714
        // we'll ignore the EOF error and return the edge as is.
4715
        edge.ExtraOpaqueData, err = wire.ReadVarBytes(
3✔
4716
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
3✔
4717
        )
3✔
4718
        switch {
3✔
4719
        case err == io.ErrUnexpectedEOF:
×
UNCOV
4720
        case err == io.EOF:
×
4721
        case err != nil:
×
4722
                return nil, err
×
4723
        }
4724

4725
        // See if optional fields are present.
4726
        if edge.MessageFlags.HasMaxHtlc() {
6✔
4727
                // The max_htlc field should be at the beginning of the opaque
3✔
4728
                // bytes.
3✔
4729
                opq := edge.ExtraOpaqueData
3✔
4730

3✔
4731
                // If the max_htlc field is not present, it might be old data
3✔
4732
                // stored before this field was validated. We'll return the
3✔
4733
                // edge along with an error.
3✔
4734
                if len(opq) < 8 {
3✔
UNCOV
4735
                        return edge, ErrEdgePolicyOptionalFieldNotFound
×
UNCOV
4736
                }
×
4737

4738
                maxHtlc := byteOrder.Uint64(opq[:8])
3✔
4739
                edge.MaxHTLC = lnwire.MilliSatoshi(maxHtlc)
3✔
4740

3✔
4741
                // Exclude the parsed field from the rest of the opaque data.
3✔
4742
                edge.ExtraOpaqueData = opq[8:]
3✔
4743
        }
4744

4745
        return edge, nil
3✔
4746
}
4747

4748
// chanGraphNodeTx is an implementation of the NodeRTx interface backed by the
4749
// ChannelGraph and a kvdb.RTx.
4750
type chanGraphNodeTx struct {
4751
        tx   kvdb.RTx
4752
        db   *ChannelGraph
4753
        node *models.LightningNode
4754
}
4755

4756
// A compile-time constraint to ensure chanGraphNodeTx implements the NodeRTx
4757
// interface.
4758
var _ NodeRTx = (*chanGraphNodeTx)(nil)
4759

4760
func newChanGraphNodeTx(tx kvdb.RTx, db *ChannelGraph,
4761
        node *models.LightningNode) *chanGraphNodeTx {
3✔
4762

3✔
4763
        return &chanGraphNodeTx{
3✔
4764
                tx:   tx,
3✔
4765
                db:   db,
3✔
4766
                node: node,
3✔
4767
        }
3✔
4768
}
3✔
4769

4770
// Node returns the raw information of the node.
4771
//
4772
// NOTE: This is a part of the NodeRTx interface.
4773
func (c *chanGraphNodeTx) Node() *models.LightningNode {
3✔
4774
        return c.node
3✔
4775
}
3✔
4776

4777
// FetchNode fetches the node with the given pub key under the same transaction
4778
// used to fetch the current node. The returned node is also a NodeRTx and any
4779
// operations on that NodeRTx will also be done under the same transaction.
4780
//
4781
// NOTE: This is a part of the NodeRTx interface.
UNCOV
4782
func (c *chanGraphNodeTx) FetchNode(nodePub route.Vertex) (NodeRTx, error) {
×
UNCOV
4783
        node, err := c.db.FetchLightningNodeTx(c.tx, nodePub)
×
UNCOV
4784
        if err != nil {
×
4785
                return nil, err
×
4786
        }
×
4787

UNCOV
4788
        return newChanGraphNodeTx(c.tx, c.db, node), nil
×
4789
}
4790

4791
// ForEachChannel can be used to iterate over the node's channels under
4792
// the same transaction used to fetch the node.
4793
//
4794
// NOTE: This is a part of the NodeRTx interface.
4795
func (c *chanGraphNodeTx) ForEachChannel(f func(*models.ChannelEdgeInfo,
UNCOV
4796
        *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
×
UNCOV
4797

×
UNCOV
4798
        return c.db.ForEachNodeChannelTx(c.tx, c.node.PubKeyBytes,
×
UNCOV
4799
                func(_ kvdb.RTx, info *models.ChannelEdgeInfo, policy1,
×
UNCOV
4800
                        policy2 *models.ChannelEdgePolicy) error {
×
UNCOV
4801

×
UNCOV
4802
                        return f(info, policy1, policy2)
×
UNCOV
4803
                },
×
4804
        )
4805
}
4806

4807
// MakeTestGraph creates a new instance of the ChannelGraph for testing
4808
// purposes.
4809
func MakeTestGraph(t testing.TB, modifiers ...OptionModifier) (*ChannelGraph,
UNCOV
4810
        error) {
×
UNCOV
4811

×
UNCOV
4812
        opts := DefaultOptions()
×
UNCOV
4813
        for _, modifier := range modifiers {
×
4814
                modifier(opts)
×
4815
        }
×
4816

4817
        // Next, create channelgraph for the first time.
UNCOV
4818
        backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
×
UNCOV
4819
        if err != nil {
×
4820
                backendCleanup()
×
4821
                return nil, err
×
4822
        }
×
4823

UNCOV
4824
        graph, err := NewChannelGraph(backend)
×
UNCOV
4825
        if err != nil {
×
4826
                backendCleanup()
×
4827
                return nil, err
×
4828
        }
×
4829

UNCOV
4830
        t.Cleanup(func() {
×
UNCOV
4831
                _ = backend.Close()
×
UNCOV
4832
                backendCleanup()
×
UNCOV
4833
        })
×
4834

UNCOV
4835
        return graph, nil
×
4836
}
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