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

lightningnetwork / lnd / 13543448815

26 Feb 2025 12:04PM UTC coverage: 49.359% (-9.5%) from 58.834%
13543448815

Pull #9552

github

ellemouton
graph/db: move cache write for UpdateEdgePolicy

To the ChannelGraph.
Pull Request #9552: graph: extract cache from CRUD [5]

15 of 19 new or added lines in 1 file covered. (78.95%)

27509 existing lines in 434 files now uncovered.

100968 of 204557 relevant lines covered (49.36%)

1.54 hits per line

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

67.63
/graph/db/kv_store.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
136
        // without 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
// KVStore 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 KVStore 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
// NewKVStore allocates a new KVStore backed by a DB instance. The
200
// returned instance has its own unique reject cache and channel cache.
201
func NewKVStore(db kvdb.Backend, options ...KVStoreOptionModifier) (*KVStore,
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 := initKVStore(db); err != nil {
3✔
211
                        return nil, err
×
212
                }
×
213
        }
214

215
        g := &KVStore{
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
        return g, nil
3✔
228
}
229

230
// setGraphCache sets the KVStore's graphCache.
231
//
232
// NOTE: this is temporary and will only be called from the ChannelGraph's
233
// constructor before the KVStore methods are available to be called. This will
234
// be removed once the graph cache is fully owned by the ChannelGraph.
235
func (c *KVStore) setGraphCache(cache *GraphCache) {
3✔
236
        c.graphCache = cache
3✔
237
}
3✔
238

239
// channelMapKey is the key structure used for storing channel edge policies.
240
type channelMapKey struct {
241
        nodeKey route.Vertex
242
        chanID  [8]byte
243
}
244

245
// getChannelMap loads all channel edge policies from the database and stores
246
// them in a map.
247
func (c *KVStore) getChannelMap(edges kvdb.RBucket) (
248
        map[channelMapKey]*models.ChannelEdgePolicy, error) {
3✔
249

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

3✔
253
        err := kvdb.ForAll(edges, func(k, edgeBytes []byte) error {
6✔
254
                // Skip embedded buckets.
3✔
255
                if bytes.Equal(k, edgeIndexBucket) ||
3✔
256
                        bytes.Equal(k, edgeUpdateIndexBucket) ||
3✔
257
                        bytes.Equal(k, zombieBucket) ||
3✔
258
                        bytes.Equal(k, disabledEdgePolicyBucket) ||
3✔
259
                        bytes.Equal(k, channelPointBucket) {
6✔
260

3✔
261
                        return nil
3✔
262
                }
3✔
263

264
                // Validate key length.
265
                if len(k) != 33+8 {
3✔
266
                        return fmt.Errorf("invalid edge key %x encountered", k)
×
267
                }
×
268

269
                var key channelMapKey
3✔
270
                copy(key.nodeKey[:], k[:33])
3✔
271
                copy(key.chanID[:], k[33:])
3✔
272

3✔
273
                // No need to deserialize unknown policy.
3✔
274
                if bytes.Equal(edgeBytes, unknownPolicy) {
3✔
275
                        return nil
×
276
                }
×
277

278
                edgeReader := bytes.NewReader(edgeBytes)
3✔
279
                edge, err := deserializeChanEdgePolicyRaw(
3✔
280
                        edgeReader,
3✔
281
                )
3✔
282

3✔
283
                switch {
3✔
284
                // If the db policy was missing an expected optional field, we
285
                // return nil as if the policy was unknown.
286
                case errors.Is(err, ErrEdgePolicyOptionalFieldNotFound):
×
287
                        return nil
×
288

289
                case err != nil:
×
290
                        return err
×
291
                }
292

293
                channelMap[key] = edge
3✔
294

3✔
295
                return nil
3✔
296
        })
297
        if err != nil {
3✔
298
                return nil, err
×
299
        }
×
300

301
        return channelMap, nil
3✔
302
}
303

304
var graphTopLevelBuckets = [][]byte{
305
        nodeBucket,
306
        edgeBucket,
307
        graphMetaBucket,
308
        closedScidBucket,
309
}
310

311
// Wipe completely deletes all saved state within all used buckets within the
312
// database. The deletion is done in a single transaction, therefore this
313
// operation is fully atomic.
314
func (c *KVStore) Wipe() error {
×
315
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
×
316
                for _, tlb := range graphTopLevelBuckets {
×
317
                        err := tx.DeleteTopLevelBucket(tlb)
×
318
                        if err != nil &&
×
319
                                !errors.Is(err, kvdb.ErrBucketNotFound) {
×
320

×
321
                                return err
×
322
                        }
×
323
                }
324

325
                return nil
×
326
        }, func() {})
×
327
        if err != nil {
×
328
                return err
×
329
        }
×
330

331
        return initKVStore(c.db)
×
332
}
333

334
// createChannelDB creates and initializes a fresh version of  In
335
// the case that the target path has not yet been created or doesn't yet exist,
336
// then the path is created. Additionally, all required top-level buckets used
337
// within the database are created.
338
func initKVStore(db kvdb.Backend) error {
3✔
339
        err := kvdb.Update(db, func(tx kvdb.RwTx) error {
6✔
340
                for _, tlb := range graphTopLevelBuckets {
6✔
341
                        if _, err := tx.CreateTopLevelBucket(tlb); err != nil {
3✔
342
                                return err
×
343
                        }
×
344
                }
345

346
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
347
                _, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
3✔
348
                if err != nil {
3✔
349
                        return err
×
350
                }
×
351
                _, err = nodes.CreateBucketIfNotExists(nodeUpdateIndexBucket)
3✔
352
                if err != nil {
3✔
353
                        return err
×
354
                }
×
355

356
                edges := tx.ReadWriteBucket(edgeBucket)
3✔
357
                _, err = edges.CreateBucketIfNotExists(edgeIndexBucket)
3✔
358
                if err != nil {
3✔
359
                        return err
×
360
                }
×
361
                _, err = edges.CreateBucketIfNotExists(edgeUpdateIndexBucket)
3✔
362
                if err != nil {
3✔
363
                        return err
×
364
                }
×
365
                _, err = edges.CreateBucketIfNotExists(channelPointBucket)
3✔
366
                if err != nil {
3✔
367
                        return err
×
368
                }
×
369
                _, err = edges.CreateBucketIfNotExists(zombieBucket)
3✔
370
                if err != nil {
3✔
371
                        return err
×
372
                }
×
373

374
                graphMeta := tx.ReadWriteBucket(graphMetaBucket)
3✔
375
                _, err = graphMeta.CreateBucketIfNotExists(pruneLogBucket)
3✔
376

3✔
377
                return err
3✔
378
        }, func() {})
3✔
379
        if err != nil {
3✔
380
                return fmt.Errorf("unable to create new channel graph: %w", err)
×
381
        }
×
382

383
        return nil
3✔
384
}
385

386
// AddrsForNode returns all known addresses for the target node public key that
387
// the graph DB is aware of. The returned boolean indicates if the given node is
388
// unknown to the graph DB or not.
389
//
390
// NOTE: this is part of the channeldb.AddrSource interface.
391
func (c *KVStore) AddrsForNode(nodePub *btcec.PublicKey) (bool, []net.Addr,
392
        error) {
3✔
393

3✔
394
        pubKey, err := route.NewVertexFromBytes(nodePub.SerializeCompressed())
3✔
395
        if err != nil {
3✔
396
                return false, nil, err
×
397
        }
×
398

399
        node, err := c.FetchLightningNode(pubKey)
3✔
400
        // We don't consider it an error if the graph is unaware of the node.
3✔
401
        switch {
3✔
402
        case err != nil && !errors.Is(err, ErrGraphNodeNotFound):
×
403
                return false, nil, err
×
404

405
        case errors.Is(err, ErrGraphNodeNotFound):
3✔
406
                return false, nil, nil
3✔
407
        }
408

409
        return true, node.Addresses, nil
3✔
410
}
411

412
// ForEachChannel iterates through all the channel edges stored within the
413
// graph and invokes the passed callback for each edge. The callback takes two
414
// edges as since this is a directed graph, both the in/out edges are visited.
415
// If the callback returns an error, then the transaction is aborted and the
416
// iteration stops early.
417
//
418
// NOTE: If an edge can't be found, or wasn't advertised, then a nil pointer
419
// for that particular channel edge routing policy will be passed into the
420
// callback.
421
func (c *KVStore) ForEachChannel(cb func(*models.ChannelEdgeInfo,
422
        *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
3✔
423

3✔
424
        return c.db.View(func(tx kvdb.RTx) error {
6✔
425
                edges := tx.ReadBucket(edgeBucket)
3✔
426
                if edges == nil {
3✔
427
                        return ErrGraphNoEdgesFound
×
428
                }
×
429

430
                // First, load all edges in memory indexed by node and channel
431
                // id.
432
                channelMap, err := c.getChannelMap(edges)
3✔
433
                if err != nil {
3✔
434
                        return err
×
435
                }
×
436

437
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
438
                if edgeIndex == nil {
3✔
439
                        return ErrGraphNoEdgesFound
×
440
                }
×
441

442
                // Load edge index, recombine each channel with the policies
443
                // loaded above and invoke the callback.
444
                return kvdb.ForAll(
3✔
445
                        edgeIndex, func(k, edgeInfoBytes []byte) error {
6✔
446
                                var chanID [8]byte
3✔
447
                                copy(chanID[:], k)
3✔
448

3✔
449
                                edgeInfoReader := bytes.NewReader(edgeInfoBytes)
3✔
450
                                info, err := deserializeChanEdgeInfo(
3✔
451
                                        edgeInfoReader,
3✔
452
                                )
3✔
453
                                if err != nil {
3✔
454
                                        return err
×
455
                                }
×
456

457
                                policy1 := channelMap[channelMapKey{
3✔
458
                                        nodeKey: info.NodeKey1Bytes,
3✔
459
                                        chanID:  chanID,
3✔
460
                                }]
3✔
461

3✔
462
                                policy2 := channelMap[channelMapKey{
3✔
463
                                        nodeKey: info.NodeKey2Bytes,
3✔
464
                                        chanID:  chanID,
3✔
465
                                }]
3✔
466

3✔
467
                                return cb(&info, policy1, policy2)
3✔
468
                        },
469
                )
470
        }, func() {})
3✔
471
}
472

473
// forEachNodeDirectedChannel iterates through all channels of a given node,
474
// executing the passed callback on the directed edge representing the channel
475
// and its incoming policy. If the callback returns an error, then the iteration
476
// is halted with the error propagated back up to the caller. An optional read
477
// transaction may be provided. If none is provided, a new one will be created.
478
//
479
// Unknown policies are passed into the callback as nil values.
480
func (c *KVStore) forEachNodeDirectedChannel(tx kvdb.RTx,
481
        node route.Vertex, cb func(channel *DirectedChannel) error) error {
3✔
482

3✔
483
        // Fallback that uses the database.
3✔
484
        toNodeCallback := func() route.Vertex {
6✔
485
                return node
3✔
486
        }
3✔
487
        toNodeFeatures, err := c.fetchNodeFeatures(tx, node)
3✔
488
        if err != nil {
3✔
489
                return err
×
490
        }
×
491

492
        dbCallback := func(tx kvdb.RTx, e *models.ChannelEdgeInfo, p1,
3✔
493
                p2 *models.ChannelEdgePolicy) error {
6✔
494

3✔
495
                var cachedInPolicy *models.CachedEdgePolicy
3✔
496
                if p2 != nil {
6✔
497
                        cachedInPolicy = models.NewCachedPolicy(p2)
3✔
498
                        cachedInPolicy.ToNodePubKey = toNodeCallback
3✔
499
                        cachedInPolicy.ToNodeFeatures = toNodeFeatures
3✔
500
                }
3✔
501

502
                var inboundFee lnwire.Fee
3✔
503
                if p1 != nil {
6✔
504
                        // Extract inbound fee. If there is a decoding error,
3✔
505
                        // skip this edge.
3✔
506
                        _, err := p1.ExtraOpaqueData.ExtractRecords(&inboundFee)
3✔
507
                        if err != nil {
3✔
UNCOV
508
                                return nil
×
UNCOV
509
                        }
×
510
                }
511

512
                directedChannel := &DirectedChannel{
3✔
513
                        ChannelID:    e.ChannelID,
3✔
514
                        IsNode1:      node == e.NodeKey1Bytes,
3✔
515
                        OtherNode:    e.NodeKey2Bytes,
3✔
516
                        Capacity:     e.Capacity,
3✔
517
                        OutPolicySet: p1 != nil,
3✔
518
                        InPolicy:     cachedInPolicy,
3✔
519
                        InboundFee:   inboundFee,
3✔
520
                }
3✔
521

3✔
522
                if node == e.NodeKey2Bytes {
6✔
523
                        directedChannel.OtherNode = e.NodeKey1Bytes
3✔
524
                }
3✔
525

526
                return cb(directedChannel)
3✔
527
        }
528

529
        return nodeTraversal(tx, node[:], c.db, dbCallback)
3✔
530
}
531

532
// fetchNodeFeatures returns the features of a given node. If no features are
533
// known for the node, an empty feature vector is returned. An optional read
534
// transaction may be provided. If none is provided, a new one will be created.
535
func (c *KVStore) fetchNodeFeatures(tx kvdb.RTx,
536
        node route.Vertex) (*lnwire.FeatureVector, error) {
3✔
537

3✔
538
        // Fallback that uses the database.
3✔
539
        targetNode, err := c.FetchLightningNodeTx(tx, node)
3✔
540
        switch {
3✔
541
        // If the node exists and has features, return them directly.
542
        case err == nil:
3✔
543
                return targetNode.Features, nil
3✔
544

545
        // If we couldn't find a node announcement, populate a blank feature
546
        // vector.
UNCOV
547
        case errors.Is(err, ErrGraphNodeNotFound):
×
UNCOV
548
                return lnwire.EmptyFeatureVector(), nil
×
549

550
        // Otherwise, bubble the error up.
551
        default:
×
552
                return nil, err
×
553
        }
554
}
555

556
// ForEachNodeDirectedChannel iterates through all channels of a given node,
557
// executing the passed callback on the directed edge representing the channel
558
// and its incoming policy. If the callback returns an error, then the iteration
559
// is halted with the error propagated back up to the caller.
560
//
561
// Unknown policies are passed into the callback as nil values.
562
//
563
// NOTE: this is part of the graphdb.NodeTraverser interface.
564
func (c *KVStore) ForEachNodeDirectedChannel(nodePub route.Vertex,
565
        cb func(channel *DirectedChannel) error) error {
3✔
566

3✔
567
        return c.forEachNodeDirectedChannel(nil, nodePub, cb)
3✔
568
}
3✔
569

570
// FetchNodeFeatures returns the features of the given node. If no features are
571
// known for the node, an empty feature vector is returned.
572
//
573
// NOTE: this is part of the graphdb.NodeTraverser interface.
574
func (c *KVStore) FetchNodeFeatures(nodePub route.Vertex) (
575
        *lnwire.FeatureVector, error) {
3✔
576

3✔
577
        return c.fetchNodeFeatures(nil, nodePub)
3✔
578
}
3✔
579

580
// ForEachNodeCached is similar to forEachNode, but it returns DirectedChannel
581
// data to the call-back.
582
//
583
// NOTE: The callback contents MUST not be modified.
584
func (c *KVStore) ForEachNodeCached(cb func(node route.Vertex,
UNCOV
585
        chans map[uint64]*DirectedChannel) error) error {
×
UNCOV
586

×
UNCOV
587
        // Otherwise call back to a version that uses the database directly.
×
UNCOV
588
        // We'll iterate over each node, then the set of channels for each
×
UNCOV
589
        // node, and construct a similar callback functiopn signature as the
×
UNCOV
590
        // main funcotin expects.
×
UNCOV
591
        return c.forEachNode(func(tx kvdb.RTx,
×
UNCOV
592
                node *models.LightningNode) error {
×
UNCOV
593

×
UNCOV
594
                channels := make(map[uint64]*DirectedChannel)
×
UNCOV
595

×
UNCOV
596
                err := c.ForEachNodeChannelTx(tx, node.PubKeyBytes,
×
UNCOV
597
                        func(tx kvdb.RTx, e *models.ChannelEdgeInfo,
×
UNCOV
598
                                p1 *models.ChannelEdgePolicy,
×
UNCOV
599
                                p2 *models.ChannelEdgePolicy) error {
×
UNCOV
600

×
UNCOV
601
                                toNodeCallback := func() route.Vertex {
×
602
                                        return node.PubKeyBytes
×
603
                                }
×
UNCOV
604
                                toNodeFeatures, err := c.fetchNodeFeatures(
×
UNCOV
605
                                        tx, node.PubKeyBytes,
×
UNCOV
606
                                )
×
UNCOV
607
                                if err != nil {
×
608
                                        return err
×
609
                                }
×
610

UNCOV
611
                                var cachedInPolicy *models.CachedEdgePolicy
×
UNCOV
612
                                if p2 != nil {
×
UNCOV
613
                                        cachedInPolicy =
×
UNCOV
614
                                                models.NewCachedPolicy(p2)
×
UNCOV
615
                                        cachedInPolicy.ToNodePubKey =
×
UNCOV
616
                                                toNodeCallback
×
UNCOV
617
                                        cachedInPolicy.ToNodeFeatures =
×
UNCOV
618
                                                toNodeFeatures
×
UNCOV
619
                                }
×
620

UNCOV
621
                                directedChannel := &DirectedChannel{
×
UNCOV
622
                                        ChannelID: e.ChannelID,
×
UNCOV
623
                                        IsNode1: node.PubKeyBytes ==
×
UNCOV
624
                                                e.NodeKey1Bytes,
×
UNCOV
625
                                        OtherNode:    e.NodeKey2Bytes,
×
UNCOV
626
                                        Capacity:     e.Capacity,
×
UNCOV
627
                                        OutPolicySet: p1 != nil,
×
UNCOV
628
                                        InPolicy:     cachedInPolicy,
×
UNCOV
629
                                }
×
UNCOV
630

×
UNCOV
631
                                if node.PubKeyBytes == e.NodeKey2Bytes {
×
UNCOV
632
                                        directedChannel.OtherNode =
×
UNCOV
633
                                                e.NodeKey1Bytes
×
UNCOV
634
                                }
×
635

UNCOV
636
                                channels[e.ChannelID] = directedChannel
×
UNCOV
637

×
UNCOV
638
                                return nil
×
639
                        })
UNCOV
640
                if err != nil {
×
641
                        return err
×
642
                }
×
643

UNCOV
644
                return cb(node.PubKeyBytes, channels)
×
645
        })
646
}
647

648
// DisabledChannelIDs returns the channel ids of disabled channels.
649
// A channel is disabled when two of the associated ChanelEdgePolicies
650
// have their disabled bit on.
UNCOV
651
func (c *KVStore) DisabledChannelIDs() ([]uint64, error) {
×
UNCOV
652
        var disabledChanIDs []uint64
×
UNCOV
653
        var chanEdgeFound map[uint64]struct{}
×
UNCOV
654

×
UNCOV
655
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
×
UNCOV
656
                edges := tx.ReadBucket(edgeBucket)
×
UNCOV
657
                if edges == nil {
×
658
                        return ErrGraphNoEdgesFound
×
659
                }
×
660

UNCOV
661
                disabledEdgePolicyIndex := edges.NestedReadBucket(
×
UNCOV
662
                        disabledEdgePolicyBucket,
×
UNCOV
663
                )
×
UNCOV
664
                if disabledEdgePolicyIndex == nil {
×
UNCOV
665
                        return nil
×
UNCOV
666
                }
×
667

668
                // We iterate over all disabled policies and we add each channel
669
                // that has more than one disabled policy to disabledChanIDs
670
                // array.
UNCOV
671
                return disabledEdgePolicyIndex.ForEach(
×
UNCOV
672
                        func(k, v []byte) error {
×
UNCOV
673
                                chanID := byteOrder.Uint64(k[:8])
×
UNCOV
674
                                _, edgeFound := chanEdgeFound[chanID]
×
UNCOV
675
                                if edgeFound {
×
UNCOV
676
                                        delete(chanEdgeFound, chanID)
×
UNCOV
677
                                        disabledChanIDs = append(
×
UNCOV
678
                                                disabledChanIDs, chanID,
×
UNCOV
679
                                        )
×
UNCOV
680

×
UNCOV
681
                                        return nil
×
UNCOV
682
                                }
×
683

UNCOV
684
                                chanEdgeFound[chanID] = struct{}{}
×
UNCOV
685

×
UNCOV
686
                                return nil
×
687
                        },
688
                )
UNCOV
689
        }, func() {
×
UNCOV
690
                disabledChanIDs = nil
×
UNCOV
691
                chanEdgeFound = make(map[uint64]struct{})
×
UNCOV
692
        })
×
UNCOV
693
        if err != nil {
×
694
                return nil, err
×
695
        }
×
696

UNCOV
697
        return disabledChanIDs, nil
×
698
}
699

700
// ForEachNode iterates through all the stored vertices/nodes in the graph,
701
// executing the passed callback with each node encountered. If the callback
702
// returns an error, then the transaction is aborted and the iteration stops
703
// early. Any operations performed on the NodeTx passed to the call-back are
704
// executed under the same read transaction and so, methods on the NodeTx object
705
// _MUST_ only be called from within the call-back.
706
func (c *KVStore) ForEachNode(cb func(tx NodeRTx) error) error {
3✔
707
        return c.forEachNode(func(tx kvdb.RTx,
3✔
708
                node *models.LightningNode) error {
6✔
709

3✔
710
                return cb(newChanGraphNodeTx(tx, c, node))
3✔
711
        })
3✔
712
}
713

714
// forEachNode iterates through all the stored vertices/nodes in the graph,
715
// executing the passed callback with each node encountered. If the callback
716
// returns an error, then the transaction is aborted and the iteration stops
717
// early.
718
//
719
// TODO(roasbeef): add iterator interface to allow for memory efficient graph
720
// traversal when graph gets mega.
721
func (c *KVStore) forEachNode(
722
        cb func(kvdb.RTx, *models.LightningNode) error) error {
3✔
723

3✔
724
        traversal := func(tx kvdb.RTx) error {
6✔
725
                // First grab the nodes bucket which stores the mapping from
3✔
726
                // pubKey to node information.
3✔
727
                nodes := tx.ReadBucket(nodeBucket)
3✔
728
                if nodes == nil {
3✔
729
                        return ErrGraphNotFound
×
730
                }
×
731

732
                return nodes.ForEach(func(pubKey, nodeBytes []byte) error {
6✔
733
                        // If this is the source key, then we skip this
3✔
734
                        // iteration as the value for this key is a pubKey
3✔
735
                        // rather than raw node information.
3✔
736
                        if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
6✔
737
                                return nil
3✔
738
                        }
3✔
739

740
                        nodeReader := bytes.NewReader(nodeBytes)
3✔
741
                        node, err := deserializeLightningNode(nodeReader)
3✔
742
                        if err != nil {
3✔
743
                                return err
×
744
                        }
×
745

746
                        // Execute the callback, the transaction will abort if
747
                        // this returns an error.
748
                        return cb(tx, &node)
3✔
749
                })
750
        }
751

752
        return kvdb.View(c.db, traversal, func() {})
6✔
753
}
754

755
// ForEachNodeCacheable iterates through all the stored vertices/nodes in the
756
// graph, executing the passed callback with each node encountered. If the
757
// callback returns an error, then the transaction is aborted and the iteration
758
// stops early.
759
func (c *KVStore) ForEachNodeCacheable(cb func(route.Vertex,
760
        *lnwire.FeatureVector) error) error {
3✔
761

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

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

778
                        nodeReader := bytes.NewReader(nodeBytes)
3✔
779
                        node, features, err := deserializeLightningNodeCacheable( //nolint:ll
3✔
780
                                nodeReader,
3✔
781
                        )
3✔
782
                        if err != nil {
3✔
783
                                return err
×
784
                        }
×
785

786
                        // Execute the callback, the transaction will abort if
787
                        // this returns an error.
788
                        return cb(node, features)
3✔
789
                })
790
        }
791

792
        return kvdb.View(c.db, traversal, func() {})
6✔
793
}
794

795
// SourceNode returns the source node of the graph. The source node is treated
796
// as the center node within a star-graph. This method may be used to kick off
797
// a path finding algorithm in order to explore the reachability of another
798
// node based off the source node.
799
func (c *KVStore) SourceNode() (*models.LightningNode, error) {
3✔
800
        var source *models.LightningNode
3✔
801
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
802
                // First grab the nodes bucket which stores the mapping from
3✔
803
                // pubKey to node information.
3✔
804
                nodes := tx.ReadBucket(nodeBucket)
3✔
805
                if nodes == nil {
3✔
806
                        return ErrGraphNotFound
×
807
                }
×
808

809
                node, err := c.sourceNode(nodes)
3✔
810
                if err != nil {
3✔
UNCOV
811
                        return err
×
UNCOV
812
                }
×
813
                source = node
3✔
814

3✔
815
                return nil
3✔
816
        }, func() {
3✔
817
                source = nil
3✔
818
        })
3✔
819
        if err != nil {
3✔
UNCOV
820
                return nil, err
×
UNCOV
821
        }
×
822

823
        return source, nil
3✔
824
}
825

826
// sourceNode uses an existing database transaction and returns the source node
827
// of the graph. The source node is treated as the center node within a
828
// star-graph. This method may be used to kick off a path finding algorithm in
829
// order to explore the reachability of another node based off the source node.
830
func (c *KVStore) sourceNode(nodes kvdb.RBucket) (*models.LightningNode,
831
        error) {
3✔
832

3✔
833
        selfPub := nodes.Get(sourceKey)
3✔
834
        if selfPub == nil {
3✔
UNCOV
835
                return nil, ErrSourceNodeNotSet
×
UNCOV
836
        }
×
837

838
        // With the pubKey of the source node retrieved, we're able to
839
        // fetch the full node information.
840
        node, err := fetchLightningNode(nodes, selfPub)
3✔
841
        if err != nil {
3✔
842
                return nil, err
×
843
        }
×
844

845
        return &node, nil
3✔
846
}
847

848
// SetSourceNode sets the source node within the graph database. The source
849
// node is to be used as the center of a star-graph within path finding
850
// algorithms.
851
func (c *KVStore) SetSourceNode(node *models.LightningNode) error {
3✔
852
        nodePubBytes := node.PubKeyBytes[:]
3✔
853

3✔
854
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
855
                // First grab the nodes bucket which stores the mapping from
3✔
856
                // pubKey to node information.
3✔
857
                nodes, err := tx.CreateTopLevelBucket(nodeBucket)
3✔
858
                if err != nil {
3✔
859
                        return err
×
860
                }
×
861

862
                // Next we create the mapping from source to the targeted
863
                // public key.
864
                if err := nodes.Put(sourceKey, nodePubBytes); err != nil {
3✔
865
                        return err
×
866
                }
×
867

868
                // Finally, we commit the information of the lightning node
869
                // itself.
870
                return addLightningNode(tx, node)
3✔
871
        }, func() {})
3✔
872
}
873

874
// AddLightningNode adds a vertex/node to the graph database. If the node is not
875
// in the database from before, this will add a new, unconnected one to the
876
// graph. If it is present from before, this will update that node's
877
// information. Note that this method is expected to only be called to update an
878
// already present node from a node announcement, or to insert a node found in a
879
// channel update.
880
//
881
// TODO(roasbeef): also need sig of announcement.
882
func (c *KVStore) AddLightningNode(node *models.LightningNode,
883
        op ...batch.SchedulerOption) error {
3✔
884

3✔
885
        r := &batch.Request{
3✔
886
                Update: func(tx kvdb.RwTx) error {
6✔
887
                        return addLightningNode(tx, node)
3✔
888
                },
3✔
889
        }
890

891
        for _, f := range op {
6✔
892
                f(r)
3✔
893
        }
3✔
894

895
        return c.nodeScheduler.Execute(r)
3✔
896
}
897

898
func addLightningNode(tx kvdb.RwTx, node *models.LightningNode) error {
3✔
899
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
3✔
900
        if err != nil {
3✔
901
                return err
×
902
        }
×
903

904
        aliases, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
3✔
905
        if err != nil {
3✔
906
                return err
×
907
        }
×
908

909
        updateIndex, err := nodes.CreateBucketIfNotExists(
3✔
910
                nodeUpdateIndexBucket,
3✔
911
        )
3✔
912
        if err != nil {
3✔
913
                return err
×
914
        }
×
915

916
        return putLightningNode(nodes, aliases, updateIndex, node)
3✔
917
}
918

919
// LookupAlias attempts to return the alias as advertised by the target node.
920
// TODO(roasbeef): currently assumes that aliases are unique...
921
func (c *KVStore) LookupAlias(pub *btcec.PublicKey) (string, error) {
3✔
922
        var alias string
3✔
923

3✔
924
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
925
                nodes := tx.ReadBucket(nodeBucket)
3✔
926
                if nodes == nil {
3✔
927
                        return ErrGraphNodesNotFound
×
928
                }
×
929

930
                aliases := nodes.NestedReadBucket(aliasIndexBucket)
3✔
931
                if aliases == nil {
3✔
932
                        return ErrGraphNodesNotFound
×
933
                }
×
934

935
                nodePub := pub.SerializeCompressed()
3✔
936
                a := aliases.Get(nodePub)
3✔
937
                if a == nil {
3✔
UNCOV
938
                        return ErrNodeAliasNotFound
×
UNCOV
939
                }
×
940

941
                // TODO(roasbeef): should actually be using the utf-8
942
                // package...
943
                alias = string(a)
3✔
944

3✔
945
                return nil
3✔
946
        }, func() {
3✔
947
                alias = ""
3✔
948
        })
3✔
949
        if err != nil {
3✔
UNCOV
950
                return "", err
×
UNCOV
951
        }
×
952

953
        return alias, nil
3✔
954
}
955

956
// DeleteLightningNode starts a new database transaction to remove a vertex/node
957
// from the database according to the node's public key.
UNCOV
958
func (c *KVStore) DeleteLightningNode(nodePub route.Vertex) error {
×
UNCOV
959
        // TODO(roasbeef): ensure dangling edges are removed...
×
UNCOV
960
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
×
UNCOV
961
                nodes := tx.ReadWriteBucket(nodeBucket)
×
UNCOV
962
                if nodes == nil {
×
963
                        return ErrGraphNodeNotFound
×
964
                }
×
965

UNCOV
966
                return c.deleteLightningNode(nodes, nodePub[:])
×
UNCOV
967
        }, func() {})
×
968
}
969

970
// deleteLightningNode uses an existing database transaction to remove a
971
// vertex/node from the database according to the node's public key.
972
func (c *KVStore) deleteLightningNode(nodes kvdb.RwBucket,
973
        compressedPubKey []byte) error {
3✔
974

3✔
975
        aliases := nodes.NestedReadWriteBucket(aliasIndexBucket)
3✔
976
        if aliases == nil {
3✔
977
                return ErrGraphNodesNotFound
×
978
        }
×
979

980
        if err := aliases.Delete(compressedPubKey); err != nil {
3✔
981
                return err
×
982
        }
×
983

984
        // Before we delete the node, we'll fetch its current state so we can
985
        // determine when its last update was to clear out the node update
986
        // index.
987
        node, err := fetchLightningNode(nodes, compressedPubKey)
3✔
988
        if err != nil {
3✔
989
                return err
×
990
        }
×
991

992
        if err := nodes.Delete(compressedPubKey); err != nil {
3✔
993
                return err
×
994
        }
×
995

996
        // Finally, we'll delete the index entry for the node within the
997
        // nodeUpdateIndexBucket as this node is no longer active, so we don't
998
        // need to track its last update.
999
        nodeUpdateIndex := nodes.NestedReadWriteBucket(nodeUpdateIndexBucket)
3✔
1000
        if nodeUpdateIndex == nil {
3✔
1001
                return ErrGraphNodesNotFound
×
1002
        }
×
1003

1004
        // In order to delete the entry, we'll need to reconstruct the key for
1005
        // its last update.
1006
        updateUnix := uint64(node.LastUpdate.Unix())
3✔
1007
        var indexKey [8 + 33]byte
3✔
1008
        byteOrder.PutUint64(indexKey[:8], updateUnix)
3✔
1009
        copy(indexKey[8:], compressedPubKey)
3✔
1010

3✔
1011
        return nodeUpdateIndex.Delete(indexKey[:])
3✔
1012
}
1013

1014
// AddChannelEdge adds a new (undirected, blank) edge to the graph database. An
1015
// undirected edge from the two target nodes are created. The information stored
1016
// denotes the static attributes of the channel, such as the channelID, the keys
1017
// involved in creation of the channel, and the set of features that the channel
1018
// supports. The chanPoint and chanID are used to uniquely identify the edge
1019
// globally within the database.
1020
func (c *KVStore) AddChannelEdge(edge *models.ChannelEdgeInfo,
1021
        op ...batch.SchedulerOption) error {
3✔
1022

3✔
1023
        var alreadyExists bool
3✔
1024
        r := &batch.Request{
3✔
1025
                Reset: func() {
6✔
1026
                        alreadyExists = false
3✔
1027
                },
3✔
1028
                Update: func(tx kvdb.RwTx) error {
3✔
1029
                        err := c.addChannelEdge(tx, edge)
3✔
1030

3✔
1031
                        // Silence ErrEdgeAlreadyExist so that the batch can
3✔
1032
                        // succeed, but propagate the error via local state.
3✔
1033
                        if errors.Is(err, ErrEdgeAlreadyExist) {
3✔
UNCOV
1034
                                alreadyExists = true
×
UNCOV
1035
                                return nil
×
UNCOV
1036
                        }
×
1037

1038
                        return err
3✔
1039
                },
1040
                OnCommit: func(err error) error {
3✔
1041
                        switch {
3✔
1042
                        case err != nil:
×
1043
                                return err
×
UNCOV
1044
                        case alreadyExists:
×
UNCOV
1045
                                return ErrEdgeAlreadyExist
×
1046
                        default:
3✔
1047
                                c.rejectCache.remove(edge.ChannelID)
3✔
1048
                                c.chanCache.remove(edge.ChannelID)
3✔
1049
                                return nil
3✔
1050
                        }
1051
                },
1052
        }
1053

1054
        for _, f := range op {
6✔
1055
                if f == nil {
3✔
1056
                        return fmt.Errorf("nil scheduler option was used")
×
1057
                }
×
1058

1059
                f(r)
3✔
1060
        }
1061

1062
        return c.chanScheduler.Execute(r)
3✔
1063
}
1064

1065
// addChannelEdge is the private form of AddChannelEdge that allows callers to
1066
// utilize an existing db transaction.
1067
func (c *KVStore) addChannelEdge(tx kvdb.RwTx,
1068
        edge *models.ChannelEdgeInfo) error {
3✔
1069

3✔
1070
        // Construct the channel's primary key which is the 8-byte channel ID.
3✔
1071
        var chanKey [8]byte
3✔
1072
        binary.BigEndian.PutUint64(chanKey[:], edge.ChannelID)
3✔
1073

3✔
1074
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
3✔
1075
        if err != nil {
3✔
1076
                return err
×
1077
        }
×
1078
        edges, err := tx.CreateTopLevelBucket(edgeBucket)
3✔
1079
        if err != nil {
3✔
1080
                return err
×
1081
        }
×
1082
        edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
3✔
1083
        if err != nil {
3✔
1084
                return err
×
1085
        }
×
1086
        chanIndex, err := edges.CreateBucketIfNotExists(channelPointBucket)
3✔
1087
        if err != nil {
3✔
1088
                return err
×
1089
        }
×
1090

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

1097
        // Before we insert the channel into the database, we'll ensure that
1098
        // both nodes already exist in the channel graph. If either node
1099
        // doesn't, then we'll insert a "shell" node that just includes its
1100
        // public key, so subsequent validation and queries can work properly.
1101
        _, node1Err := fetchLightningNode(nodes, edge.NodeKey1Bytes[:])
3✔
1102
        switch {
3✔
1103
        case errors.Is(node1Err, ErrGraphNodeNotFound):
3✔
1104
                node1Shell := models.LightningNode{
3✔
1105
                        PubKeyBytes:          edge.NodeKey1Bytes,
3✔
1106
                        HaveNodeAnnouncement: false,
3✔
1107
                }
3✔
1108
                err := addLightningNode(tx, &node1Shell)
3✔
1109
                if err != nil {
3✔
1110
                        return fmt.Errorf("unable to create shell node "+
×
1111
                                "for: %x: %w", edge.NodeKey1Bytes, err)
×
1112
                }
×
1113
        case node1Err != nil:
×
1114
                return node1Err
×
1115
        }
1116

1117
        _, node2Err := fetchLightningNode(nodes, edge.NodeKey2Bytes[:])
3✔
1118
        switch {
3✔
1119
        case errors.Is(node2Err, ErrGraphNodeNotFound):
3✔
1120
                node2Shell := models.LightningNode{
3✔
1121
                        PubKeyBytes:          edge.NodeKey2Bytes,
3✔
1122
                        HaveNodeAnnouncement: false,
3✔
1123
                }
3✔
1124
                err := addLightningNode(tx, &node2Shell)
3✔
1125
                if err != nil {
3✔
1126
                        return fmt.Errorf("unable to create shell node "+
×
1127
                                "for: %x: %w", edge.NodeKey2Bytes, err)
×
1128
                }
×
1129
        case node2Err != nil:
×
1130
                return node2Err
×
1131
        }
1132

1133
        // If the edge hasn't been created yet, then we'll first add it to the
1134
        // edge index in order to associate the edge between two nodes and also
1135
        // store the static components of the channel.
1136
        if err := putChanEdgeInfo(edgeIndex, edge, chanKey); err != nil {
3✔
1137
                return err
×
1138
        }
×
1139

1140
        // Mark edge policies for both sides as unknown. This is to enable
1141
        // efficient incoming channel lookup for a node.
1142
        keys := []*[33]byte{
3✔
1143
                &edge.NodeKey1Bytes,
3✔
1144
                &edge.NodeKey2Bytes,
3✔
1145
        }
3✔
1146
        for _, key := range keys {
6✔
1147
                err := putChanEdgePolicyUnknown(edges, edge.ChannelID, key[:])
3✔
1148
                if err != nil {
3✔
1149
                        return err
×
1150
                }
×
1151
        }
1152

1153
        // Finally we add it to the channel index which maps channel points
1154
        // (outpoints) to the shorter channel ID's.
1155
        var b bytes.Buffer
3✔
1156
        if err := WriteOutpoint(&b, &edge.ChannelPoint); err != nil {
3✔
1157
                return err
×
1158
        }
×
1159

1160
        return chanIndex.Put(b.Bytes(), chanKey[:])
3✔
1161
}
1162

1163
// HasChannelEdge returns true if the database knows of a channel edge with the
1164
// passed channel ID, and false otherwise. If an edge with that ID is found
1165
// within the graph, then two time stamps representing the last time the edge
1166
// was updated for both directed edges are returned along with the boolean. If
1167
// it is not found, then the zombie index is checked and its result is returned
1168
// as the second boolean.
1169
func (c *KVStore) HasChannelEdge(
1170
        chanID uint64) (time.Time, time.Time, bool, bool, error) {
3✔
1171

3✔
1172
        var (
3✔
1173
                upd1Time time.Time
3✔
1174
                upd2Time time.Time
3✔
1175
                exists   bool
3✔
1176
                isZombie bool
3✔
1177
        )
3✔
1178

3✔
1179
        // We'll query the cache with the shared lock held to allow multiple
3✔
1180
        // readers to access values in the cache concurrently if they exist.
3✔
1181
        c.cacheMu.RLock()
3✔
1182
        if entry, ok := c.rejectCache.get(chanID); ok {
6✔
1183
                c.cacheMu.RUnlock()
3✔
1184
                upd1Time = time.Unix(entry.upd1Time, 0)
3✔
1185
                upd2Time = time.Unix(entry.upd2Time, 0)
3✔
1186
                exists, isZombie = entry.flags.unpack()
3✔
1187

3✔
1188
                return upd1Time, upd2Time, exists, isZombie, nil
3✔
1189
        }
3✔
1190
        c.cacheMu.RUnlock()
3✔
1191

3✔
1192
        c.cacheMu.Lock()
3✔
1193
        defer c.cacheMu.Unlock()
3✔
1194

3✔
1195
        // The item was not found with the shared lock, so we'll acquire the
3✔
1196
        // exclusive lock and check the cache again in case another method added
3✔
1197
        // the entry to the cache while no lock was held.
3✔
1198
        if entry, ok := c.rejectCache.get(chanID); ok {
6✔
1199
                upd1Time = time.Unix(entry.upd1Time, 0)
3✔
1200
                upd2Time = time.Unix(entry.upd2Time, 0)
3✔
1201
                exists, isZombie = entry.flags.unpack()
3✔
1202

3✔
1203
                return upd1Time, upd2Time, exists, isZombie, nil
3✔
1204
        }
3✔
1205

1206
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1207
                edges := tx.ReadBucket(edgeBucket)
3✔
1208
                if edges == nil {
3✔
1209
                        return ErrGraphNoEdgesFound
×
1210
                }
×
1211
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
1212
                if edgeIndex == nil {
3✔
1213
                        return ErrGraphNoEdgesFound
×
1214
                }
×
1215

1216
                var channelID [8]byte
3✔
1217
                byteOrder.PutUint64(channelID[:], chanID)
3✔
1218

3✔
1219
                // If the edge doesn't exist, then we'll also check our zombie
3✔
1220
                // index.
3✔
1221
                if edgeIndex.Get(channelID[:]) == nil {
6✔
1222
                        exists = false
3✔
1223
                        zombieIndex := edges.NestedReadBucket(zombieBucket)
3✔
1224
                        if zombieIndex != nil {
6✔
1225
                                isZombie, _, _ = isZombieEdge(
3✔
1226
                                        zombieIndex, chanID,
3✔
1227
                                )
3✔
1228
                        }
3✔
1229

1230
                        return nil
3✔
1231
                }
1232

1233
                exists = true
3✔
1234
                isZombie = false
3✔
1235

3✔
1236
                // If the channel has been found in the graph, then retrieve
3✔
1237
                // the edges itself so we can return the last updated
3✔
1238
                // timestamps.
3✔
1239
                nodes := tx.ReadBucket(nodeBucket)
3✔
1240
                if nodes == nil {
3✔
1241
                        return ErrGraphNodeNotFound
×
1242
                }
×
1243

1244
                e1, e2, err := fetchChanEdgePolicies(
3✔
1245
                        edgeIndex, edges, channelID[:],
3✔
1246
                )
3✔
1247
                if err != nil {
3✔
1248
                        return err
×
1249
                }
×
1250

1251
                // As we may have only one of the edges populated, only set the
1252
                // update time if the edge was found in the database.
1253
                if e1 != nil {
6✔
1254
                        upd1Time = e1.LastUpdate
3✔
1255
                }
3✔
1256
                if e2 != nil {
6✔
1257
                        upd2Time = e2.LastUpdate
3✔
1258
                }
3✔
1259

1260
                return nil
3✔
1261
        }, func() {}); err != nil {
3✔
1262
                return time.Time{}, time.Time{}, exists, isZombie, err
×
1263
        }
×
1264

1265
        c.rejectCache.insert(chanID, rejectCacheEntry{
3✔
1266
                upd1Time: upd1Time.Unix(),
3✔
1267
                upd2Time: upd2Time.Unix(),
3✔
1268
                flags:    packRejectFlags(exists, isZombie),
3✔
1269
        })
3✔
1270

3✔
1271
        return upd1Time, upd2Time, exists, isZombie, nil
3✔
1272
}
1273

1274
// AddEdgeProof sets the proof of an existing edge in the graph database.
1275
func (c *KVStore) AddEdgeProof(chanID lnwire.ShortChannelID,
1276
        proof *models.ChannelAuthProof) error {
3✔
1277

3✔
1278
        // Construct the channel's primary key which is the 8-byte channel ID.
3✔
1279
        var chanKey [8]byte
3✔
1280
        binary.BigEndian.PutUint64(chanKey[:], chanID.ToUint64())
3✔
1281

3✔
1282
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1283
                edges := tx.ReadWriteBucket(edgeBucket)
3✔
1284
                if edges == nil {
3✔
1285
                        return ErrEdgeNotFound
×
1286
                }
×
1287

1288
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
1289
                if edgeIndex == nil {
3✔
1290
                        return ErrEdgeNotFound
×
1291
                }
×
1292

1293
                edge, err := fetchChanEdgeInfo(edgeIndex, chanKey[:])
3✔
1294
                if err != nil {
3✔
1295
                        return err
×
1296
                }
×
1297

1298
                edge.AuthProof = proof
3✔
1299

3✔
1300
                return putChanEdgeInfo(edgeIndex, &edge, chanKey)
3✔
1301
        }, func() {})
3✔
1302
}
1303

1304
const (
1305
        // pruneTipBytes is the total size of the value which stores a prune
1306
        // entry of the graph in the prune log. The "prune tip" is the last
1307
        // entry in the prune log, and indicates if the channel graph is in
1308
        // sync with the current UTXO state. The structure of the value
1309
        // is: blockHash, taking 32 bytes total.
1310
        pruneTipBytes = 32
1311
)
1312

1313
// PruneGraph prunes newly closed channels from the channel graph in response
1314
// to a new block being solved on the network. Any transactions which spend the
1315
// funding output of any known channels within he graph will be deleted.
1316
// Additionally, the "prune tip", or the last block which has been used to
1317
// prune the graph is stored so callers can ensure the graph is fully in sync
1318
// with the current UTXO state. A slice of channels that have been closed by
1319
// the target block are returned if the function succeeds without error.
1320
func (c *KVStore) PruneGraph(spentOutputs []*wire.OutPoint,
1321
        blockHash *chainhash.Hash, blockHeight uint32) (
1322
        []*models.ChannelEdgeInfo, error) {
3✔
1323

3✔
1324
        c.cacheMu.Lock()
3✔
1325
        defer c.cacheMu.Unlock()
3✔
1326

3✔
1327
        var chansClosed []*models.ChannelEdgeInfo
3✔
1328

3✔
1329
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1330
                // First grab the edges bucket which houses the information
3✔
1331
                // we'd like to delete
3✔
1332
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
3✔
1333
                if err != nil {
3✔
1334
                        return err
×
1335
                }
×
1336

1337
                // Next grab the two edge indexes which will also need to be
1338
                // updated.
1339
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
3✔
1340
                if err != nil {
3✔
1341
                        return err
×
1342
                }
×
1343
                chanIndex, err := edges.CreateBucketIfNotExists(
3✔
1344
                        channelPointBucket,
3✔
1345
                )
3✔
1346
                if err != nil {
3✔
1347
                        return err
×
1348
                }
×
1349
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
1350
                if nodes == nil {
3✔
1351
                        return ErrSourceNodeNotSet
×
1352
                }
×
1353
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
3✔
1354
                if err != nil {
3✔
1355
                        return err
×
1356
                }
×
1357

1358
                // For each of the outpoints that have been spent within the
1359
                // block, we attempt to delete them from the graph as if that
1360
                // outpoint was a channel, then it has now been closed.
1361
                for _, chanPoint := range spentOutputs {
6✔
1362
                        // TODO(roasbeef): load channel bloom filter, continue
3✔
1363
                        // if NOT if filter
3✔
1364

3✔
1365
                        var opBytes bytes.Buffer
3✔
1366
                        err := WriteOutpoint(&opBytes, chanPoint)
3✔
1367
                        if err != nil {
3✔
1368
                                return err
×
1369
                        }
×
1370

1371
                        // First attempt to see if the channel exists within
1372
                        // the database, if not, then we can exit early.
1373
                        chanID := chanIndex.Get(opBytes.Bytes())
3✔
1374
                        if chanID == nil {
3✔
UNCOV
1375
                                continue
×
1376
                        }
1377

1378
                        // However, if it does, then we'll read out the full
1379
                        // version so we can add it to the set of deleted
1380
                        // channels.
1381
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
1382
                        if err != nil {
3✔
1383
                                return err
×
1384
                        }
×
1385

1386
                        // Attempt to delete the channel, an ErrEdgeNotFound
1387
                        // will be returned if that outpoint isn't known to be
1388
                        // a channel. If no error is returned, then a channel
1389
                        // was successfully pruned.
1390
                        err = c.delChannelEdgeUnsafe(
3✔
1391
                                edges, edgeIndex, chanIndex, zombieIndex,
3✔
1392
                                chanID, false, false,
3✔
1393
                        )
3✔
1394
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
3✔
1395
                                return err
×
1396
                        }
×
1397

1398
                        chansClosed = append(chansClosed, &edgeInfo)
3✔
1399
                }
1400

1401
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
3✔
1402
                if err != nil {
3✔
1403
                        return err
×
1404
                }
×
1405

1406
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
3✔
1407
                        pruneLogBucket,
3✔
1408
                )
3✔
1409
                if err != nil {
3✔
1410
                        return err
×
1411
                }
×
1412

1413
                // With the graph pruned, add a new entry to the prune log,
1414
                // which can be used to check if the graph is fully synced with
1415
                // the current UTXO state.
1416
                var blockHeightBytes [4]byte
3✔
1417
                byteOrder.PutUint32(blockHeightBytes[:], blockHeight)
3✔
1418

3✔
1419
                var newTip [pruneTipBytes]byte
3✔
1420
                copy(newTip[:], blockHash[:])
3✔
1421

3✔
1422
                err = pruneBucket.Put(blockHeightBytes[:], newTip[:])
3✔
1423
                if err != nil {
3✔
1424
                        return err
×
1425
                }
×
1426

1427
                // Now that the graph has been pruned, we'll also attempt to
1428
                // prune any nodes that have had a channel closed within the
1429
                // latest block.
1430
                return c.pruneGraphNodes(nodes, edgeIndex)
3✔
1431
        }, func() {
3✔
1432
                chansClosed = nil
3✔
1433
        })
3✔
1434
        if err != nil {
3✔
1435
                return nil, err
×
1436
        }
×
1437

1438
        for _, channel := range chansClosed {
6✔
1439
                c.rejectCache.remove(channel.ChannelID)
3✔
1440
                c.chanCache.remove(channel.ChannelID)
3✔
1441
        }
3✔
1442

1443
        if c.graphCache != nil {
6✔
1444
                log.Debugf("Pruned graph, cache now has %s",
3✔
1445
                        c.graphCache.Stats())
3✔
1446
        }
3✔
1447

1448
        return chansClosed, nil
3✔
1449
}
1450

1451
// PruneGraphNodes is a garbage collection method which attempts to prune out
1452
// any nodes from the channel graph that are currently unconnected. This ensure
1453
// that we only maintain a graph of reachable nodes. In the event that a pruned
1454
// node gains more channels, it will be re-added back to the graph.
1455
func (c *KVStore) PruneGraphNodes() error {
3✔
1456
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1457
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
1458
                if nodes == nil {
3✔
1459
                        return ErrGraphNodesNotFound
×
1460
                }
×
1461
                edges := tx.ReadWriteBucket(edgeBucket)
3✔
1462
                if edges == nil {
3✔
1463
                        return ErrGraphNotFound
×
1464
                }
×
1465
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
1466
                if edgeIndex == nil {
3✔
1467
                        return ErrGraphNoEdgesFound
×
1468
                }
×
1469

1470
                return c.pruneGraphNodes(nodes, edgeIndex)
3✔
1471
        }, func() {})
3✔
1472
}
1473

1474
// pruneGraphNodes attempts to remove any nodes from the graph who have had a
1475
// channel closed within the current block. If the node still has existing
1476
// channels in the graph, this will act as a no-op.
1477
func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket,
1478
        edgeIndex kvdb.RwBucket) error {
3✔
1479

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

3✔
1482
        // We'll retrieve the graph's source node to ensure we don't remove it
3✔
1483
        // even if it no longer has any open channels.
3✔
1484
        sourceNode, err := c.sourceNode(nodes)
3✔
1485
        if err != nil {
3✔
1486
                return err
×
1487
        }
×
1488

1489
        // We'll use this map to keep count the number of references to a node
1490
        // in the graph. A node should only be removed once it has no more
1491
        // references in the graph.
1492
        nodeRefCounts := make(map[[33]byte]int)
3✔
1493
        err = nodes.ForEach(func(pubKey, nodeBytes []byte) error {
6✔
1494
                // If this is the source key, then we skip this
3✔
1495
                // iteration as the value for this key is a pubKey
3✔
1496
                // rather than raw node information.
3✔
1497
                if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
6✔
1498
                        return nil
3✔
1499
                }
3✔
1500

1501
                var nodePub [33]byte
3✔
1502
                copy(nodePub[:], pubKey)
3✔
1503
                nodeRefCounts[nodePub] = 0
3✔
1504

3✔
1505
                return nil
3✔
1506
        })
1507
        if err != nil {
3✔
1508
                return err
×
1509
        }
×
1510

1511
        // To ensure we never delete the source node, we'll start off by
1512
        // bumping its ref count to 1.
1513
        nodeRefCounts[sourceNode.PubKeyBytes] = 1
3✔
1514

3✔
1515
        // Next, we'll run through the edgeIndex which maps a channel ID to the
3✔
1516
        // edge info. We'll use this scan to populate our reference count map
3✔
1517
        // above.
3✔
1518
        err = edgeIndex.ForEach(func(chanID, edgeInfoBytes []byte) error {
6✔
1519
                // The first 66 bytes of the edge info contain the pubkeys of
3✔
1520
                // the nodes that this edge attaches. We'll extract them, and
3✔
1521
                // add them to the ref count map.
3✔
1522
                var node1, node2 [33]byte
3✔
1523
                copy(node1[:], edgeInfoBytes[:33])
3✔
1524
                copy(node2[:], edgeInfoBytes[33:])
3✔
1525

3✔
1526
                // With the nodes extracted, we'll increase the ref count of
3✔
1527
                // each of the nodes.
3✔
1528
                nodeRefCounts[node1]++
3✔
1529
                nodeRefCounts[node2]++
3✔
1530

3✔
1531
                return nil
3✔
1532
        })
3✔
1533
        if err != nil {
3✔
1534
                return err
×
1535
        }
×
1536

1537
        // Finally, we'll make a second pass over the set of nodes, and delete
1538
        // any nodes that have a ref count of zero.
1539
        var numNodesPruned int
3✔
1540
        for nodePubKey, refCount := range nodeRefCounts {
6✔
1541
                // If the ref count of the node isn't zero, then we can safely
3✔
1542
                // skip it as it still has edges to or from it within the
3✔
1543
                // graph.
3✔
1544
                if refCount != 0 {
6✔
1545
                        continue
3✔
1546
                }
1547

1548
                if c.graphCache != nil {
6✔
1549
                        c.graphCache.RemoveNode(nodePubKey)
3✔
1550
                }
3✔
1551

1552
                // If we reach this point, then there are no longer any edges
1553
                // that connect this node, so we can delete it.
1554
                err := c.deleteLightningNode(nodes, nodePubKey[:])
3✔
1555
                if err != nil {
3✔
1556
                        if errors.Is(err, ErrGraphNodeNotFound) ||
×
1557
                                errors.Is(err, ErrGraphNodesNotFound) {
×
1558

×
1559
                                log.Warnf("Unable to prune node %x from the "+
×
1560
                                        "graph: %v", nodePubKey, err)
×
1561
                                continue
×
1562
                        }
1563

1564
                        return err
×
1565
                }
1566

1567
                log.Infof("Pruned unconnected node %x from channel graph",
3✔
1568
                        nodePubKey[:])
3✔
1569

3✔
1570
                numNodesPruned++
3✔
1571
        }
1572

1573
        if numNodesPruned > 0 {
6✔
1574
                log.Infof("Pruned %v unconnected nodes from the channel graph",
3✔
1575
                        numNodesPruned)
3✔
1576
        }
3✔
1577

1578
        return nil
3✔
1579
}
1580

1581
// DisconnectBlockAtHeight is used to indicate that the block specified
1582
// by the passed height has been disconnected from the main chain. This
1583
// will "rewind" the graph back to the height below, deleting channels
1584
// that are no longer confirmed from the graph. The prune log will be
1585
// set to the last prune height valid for the remaining chain.
1586
// Channels that were removed from the graph resulting from the
1587
// disconnected block are returned.
1588
func (c *KVStore) DisconnectBlockAtHeight(height uint32) (
1589
        []*models.ChannelEdgeInfo, error) {
2✔
1590

2✔
1591
        // Every channel having a ShortChannelID starting at 'height'
2✔
1592
        // will no longer be confirmed.
2✔
1593
        startShortChanID := lnwire.ShortChannelID{
2✔
1594
                BlockHeight: height,
2✔
1595
        }
2✔
1596

2✔
1597
        // Delete everything after this height from the db up until the
2✔
1598
        // SCID alias range.
2✔
1599
        endShortChanID := aliasmgr.StartingAlias
2✔
1600

2✔
1601
        // The block height will be the 3 first bytes of the channel IDs.
2✔
1602
        var chanIDStart [8]byte
2✔
1603
        byteOrder.PutUint64(chanIDStart[:], startShortChanID.ToUint64())
2✔
1604
        var chanIDEnd [8]byte
2✔
1605
        byteOrder.PutUint64(chanIDEnd[:], endShortChanID.ToUint64())
2✔
1606

2✔
1607
        c.cacheMu.Lock()
2✔
1608
        defer c.cacheMu.Unlock()
2✔
1609

2✔
1610
        // Keep track of the channels that are removed from the graph.
2✔
1611
        var removedChans []*models.ChannelEdgeInfo
2✔
1612

2✔
1613
        if err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
4✔
1614
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
2✔
1615
                if err != nil {
2✔
1616
                        return err
×
1617
                }
×
1618
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
2✔
1619
                if err != nil {
2✔
1620
                        return err
×
1621
                }
×
1622
                chanIndex, err := edges.CreateBucketIfNotExists(
2✔
1623
                        channelPointBucket,
2✔
1624
                )
2✔
1625
                if err != nil {
2✔
1626
                        return err
×
1627
                }
×
1628
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
2✔
1629
                if err != nil {
2✔
1630
                        return err
×
1631
                }
×
1632

1633
                // Scan from chanIDStart to chanIDEnd, deleting every
1634
                // found edge.
1635
                // NOTE: we must delete the edges after the cursor loop, since
1636
                // modifying the bucket while traversing is not safe.
1637
                // NOTE: We use a < comparison in bytes.Compare instead of <=
1638
                // so that the StartingAlias itself isn't deleted.
1639
                var keys [][]byte
2✔
1640
                cursor := edgeIndex.ReadWriteCursor()
2✔
1641

2✔
1642
                //nolint:ll
2✔
1643
                for k, v := cursor.Seek(chanIDStart[:]); k != nil &&
2✔
1644
                        bytes.Compare(k, chanIDEnd[:]) < 0; k, v = cursor.Next() {
4✔
1645
                        edgeInfoReader := bytes.NewReader(v)
2✔
1646
                        edgeInfo, err := deserializeChanEdgeInfo(edgeInfoReader)
2✔
1647
                        if err != nil {
2✔
1648
                                return err
×
1649
                        }
×
1650

1651
                        keys = append(keys, k)
2✔
1652
                        removedChans = append(removedChans, &edgeInfo)
2✔
1653
                }
1654

1655
                for _, k := range keys {
4✔
1656
                        err = c.delChannelEdgeUnsafe(
2✔
1657
                                edges, edgeIndex, chanIndex, zombieIndex,
2✔
1658
                                k, false, false,
2✔
1659
                        )
2✔
1660
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
2✔
1661
                                return err
×
1662
                        }
×
1663
                }
1664

1665
                // Delete all the entries in the prune log having a height
1666
                // greater or equal to the block disconnected.
1667
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
2✔
1668
                if err != nil {
2✔
1669
                        return err
×
1670
                }
×
1671

1672
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
2✔
1673
                        pruneLogBucket,
2✔
1674
                )
2✔
1675
                if err != nil {
2✔
1676
                        return err
×
1677
                }
×
1678

1679
                var pruneKeyStart [4]byte
2✔
1680
                byteOrder.PutUint32(pruneKeyStart[:], height)
2✔
1681

2✔
1682
                var pruneKeyEnd [4]byte
2✔
1683
                byteOrder.PutUint32(pruneKeyEnd[:], math.MaxUint32)
2✔
1684

2✔
1685
                // To avoid modifying the bucket while traversing, we delete
2✔
1686
                // the keys in a second loop.
2✔
1687
                var pruneKeys [][]byte
2✔
1688
                pruneCursor := pruneBucket.ReadWriteCursor()
2✔
1689
                //nolint:ll
2✔
1690
                for k, _ := pruneCursor.Seek(pruneKeyStart[:]); k != nil &&
2✔
1691
                        bytes.Compare(k, pruneKeyEnd[:]) <= 0; k, _ = pruneCursor.Next() {
4✔
1692
                        pruneKeys = append(pruneKeys, k)
2✔
1693
                }
2✔
1694

1695
                for _, k := range pruneKeys {
4✔
1696
                        if err := pruneBucket.Delete(k); err != nil {
2✔
1697
                                return err
×
1698
                        }
×
1699
                }
1700

1701
                return nil
2✔
1702
        }, func() {
2✔
1703
                removedChans = nil
2✔
1704
        }); err != nil {
2✔
1705
                return nil, err
×
1706
        }
×
1707

1708
        for _, channel := range removedChans {
4✔
1709
                c.rejectCache.remove(channel.ChannelID)
2✔
1710
                c.chanCache.remove(channel.ChannelID)
2✔
1711
        }
2✔
1712

1713
        return removedChans, nil
2✔
1714
}
1715

1716
// PruneTip returns the block height and hash of the latest block that has been
1717
// used to prune channels in the graph. Knowing the "prune tip" allows callers
1718
// to tell if the graph is currently in sync with the current best known UTXO
1719
// state.
1720
func (c *KVStore) PruneTip() (*chainhash.Hash, uint32, error) {
3✔
1721
        var (
3✔
1722
                tipHash   chainhash.Hash
3✔
1723
                tipHeight uint32
3✔
1724
        )
3✔
1725

3✔
1726
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1727
                graphMeta := tx.ReadBucket(graphMetaBucket)
3✔
1728
                if graphMeta == nil {
3✔
1729
                        return ErrGraphNotFound
×
1730
                }
×
1731
                pruneBucket := graphMeta.NestedReadBucket(pruneLogBucket)
3✔
1732
                if pruneBucket == nil {
3✔
1733
                        return ErrGraphNeverPruned
×
1734
                }
×
1735

1736
                pruneCursor := pruneBucket.ReadCursor()
3✔
1737

3✔
1738
                // The prune key with the largest block height will be our
3✔
1739
                // prune tip.
3✔
1740
                k, v := pruneCursor.Last()
3✔
1741
                if k == nil {
6✔
1742
                        return ErrGraphNeverPruned
3✔
1743
                }
3✔
1744

1745
                // Once we have the prune tip, the value will be the block hash,
1746
                // and the key the block height.
1747
                copy(tipHash[:], v)
3✔
1748
                tipHeight = byteOrder.Uint32(k)
3✔
1749

3✔
1750
                return nil
3✔
1751
        }, func() {})
3✔
1752
        if err != nil {
6✔
1753
                return nil, 0, err
3✔
1754
        }
3✔
1755

1756
        return &tipHash, tipHeight, nil
3✔
1757
}
1758

1759
// DeleteChannelEdges removes edges with the given channel IDs from the
1760
// database and marks them as zombies. This ensures that we're unable to re-add
1761
// it to our database once again. If an edge does not exist within the
1762
// database, then ErrEdgeNotFound will be returned. If strictZombiePruning is
1763
// true, then when we mark these edges as zombies, we'll set up the keys such
1764
// that we require the node that failed to send the fresh update to be the one
1765
// that resurrects the channel from its zombie state. The markZombie bool
1766
// denotes whether or not to mark the channel as a zombie.
1767
func (c *KVStore) DeleteChannelEdges(strictZombiePruning, markZombie bool,
1768
        chanIDs ...uint64) error {
3✔
1769

3✔
1770
        // TODO(roasbeef): possibly delete from node bucket if node has no more
3✔
1771
        // channels
3✔
1772
        // TODO(roasbeef): don't delete both edges?
3✔
1773

3✔
1774
        c.cacheMu.Lock()
3✔
1775
        defer c.cacheMu.Unlock()
3✔
1776

3✔
1777
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1778
                edges := tx.ReadWriteBucket(edgeBucket)
3✔
1779
                if edges == nil {
3✔
1780
                        return ErrEdgeNotFound
×
1781
                }
×
1782
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
1783
                if edgeIndex == nil {
3✔
1784
                        return ErrEdgeNotFound
×
1785
                }
×
1786
                chanIndex := edges.NestedReadWriteBucket(channelPointBucket)
3✔
1787
                if chanIndex == nil {
3✔
1788
                        return ErrEdgeNotFound
×
1789
                }
×
1790
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
1791
                if nodes == nil {
3✔
1792
                        return ErrGraphNodeNotFound
×
1793
                }
×
1794
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
3✔
1795
                if err != nil {
3✔
1796
                        return err
×
1797
                }
×
1798

1799
                var rawChanID [8]byte
3✔
1800
                for _, chanID := range chanIDs {
6✔
1801
                        byteOrder.PutUint64(rawChanID[:], chanID)
3✔
1802
                        err := c.delChannelEdgeUnsafe(
3✔
1803
                                edges, edgeIndex, chanIndex, zombieIndex,
3✔
1804
                                rawChanID[:], markZombie, strictZombiePruning,
3✔
1805
                        )
3✔
1806
                        if err != nil {
3✔
UNCOV
1807
                                return err
×
UNCOV
1808
                        }
×
1809
                }
1810

1811
                return nil
3✔
1812
        }, func() {})
3✔
1813
        if err != nil {
3✔
UNCOV
1814
                return err
×
UNCOV
1815
        }
×
1816

1817
        for _, chanID := range chanIDs {
6✔
1818
                c.rejectCache.remove(chanID)
3✔
1819
                c.chanCache.remove(chanID)
3✔
1820
        }
3✔
1821

1822
        return nil
3✔
1823
}
1824

1825
// ChannelID attempt to lookup the 8-byte compact channel ID which maps to the
1826
// passed channel point (outpoint). If the passed channel doesn't exist within
1827
// the database, then ErrEdgeNotFound is returned.
1828
func (c *KVStore) ChannelID(chanPoint *wire.OutPoint) (uint64, error) {
3✔
1829
        var chanID uint64
3✔
1830
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1831
                var err error
3✔
1832
                chanID, err = getChanID(tx, chanPoint)
3✔
1833
                return err
3✔
1834
        }, func() {
6✔
1835
                chanID = 0
3✔
1836
        }); err != nil {
6✔
1837
                return 0, err
3✔
1838
        }
3✔
1839

1840
        return chanID, nil
3✔
1841
}
1842

1843
// getChanID returns the assigned channel ID for a given channel point.
1844
func getChanID(tx kvdb.RTx, chanPoint *wire.OutPoint) (uint64, error) {
3✔
1845
        var b bytes.Buffer
3✔
1846
        if err := WriteOutpoint(&b, chanPoint); err != nil {
3✔
1847
                return 0, err
×
1848
        }
×
1849

1850
        edges := tx.ReadBucket(edgeBucket)
3✔
1851
        if edges == nil {
3✔
1852
                return 0, ErrGraphNoEdgesFound
×
1853
        }
×
1854
        chanIndex := edges.NestedReadBucket(channelPointBucket)
3✔
1855
        if chanIndex == nil {
3✔
1856
                return 0, ErrGraphNoEdgesFound
×
1857
        }
×
1858

1859
        chanIDBytes := chanIndex.Get(b.Bytes())
3✔
1860
        if chanIDBytes == nil {
6✔
1861
                return 0, ErrEdgeNotFound
3✔
1862
        }
3✔
1863

1864
        chanID := byteOrder.Uint64(chanIDBytes)
3✔
1865

3✔
1866
        return chanID, nil
3✔
1867
}
1868

1869
// TODO(roasbeef): allow updates to use Batch?
1870

1871
// HighestChanID returns the "highest" known channel ID in the channel graph.
1872
// This represents the "newest" channel from the PoV of the chain. This method
1873
// can be used by peers to quickly determine if they're graphs are in sync.
1874
func (c *KVStore) HighestChanID() (uint64, error) {
3✔
1875
        var cid uint64
3✔
1876

3✔
1877
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1878
                edges := tx.ReadBucket(edgeBucket)
3✔
1879
                if edges == nil {
3✔
1880
                        return ErrGraphNoEdgesFound
×
1881
                }
×
1882
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
1883
                if edgeIndex == nil {
3✔
1884
                        return ErrGraphNoEdgesFound
×
1885
                }
×
1886

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

3✔
1891
                lastChanID, _ := cidCursor.Last()
3✔
1892

3✔
1893
                // If there's no key, then this means that we don't actually
3✔
1894
                // know of any channels, so we'll return a predicable error.
3✔
1895
                if lastChanID == nil {
6✔
1896
                        return ErrGraphNoEdgesFound
3✔
1897
                }
3✔
1898

1899
                // Otherwise, we'll de serialize the channel ID and return it
1900
                // to the caller.
1901
                cid = byteOrder.Uint64(lastChanID)
3✔
1902

3✔
1903
                return nil
3✔
1904
        }, func() {
3✔
1905
                cid = 0
3✔
1906
        })
3✔
1907
        if err != nil && !errors.Is(err, ErrGraphNoEdgesFound) {
3✔
1908
                return 0, err
×
1909
        }
×
1910

1911
        return cid, nil
3✔
1912
}
1913

1914
// ChannelEdge represents the complete set of information for a channel edge in
1915
// the known channel graph. This struct couples the core information of the
1916
// edge as well as each of the known advertised edge policies.
1917
type ChannelEdge struct {
1918
        // Info contains all the static information describing the channel.
1919
        Info *models.ChannelEdgeInfo
1920

1921
        // Policy1 points to the "first" edge policy of the channel containing
1922
        // the dynamic information required to properly route through the edge.
1923
        Policy1 *models.ChannelEdgePolicy
1924

1925
        // Policy2 points to the "second" edge policy of the channel containing
1926
        // the dynamic information required to properly route through the edge.
1927
        Policy2 *models.ChannelEdgePolicy
1928

1929
        // Node1 is "node 1" in the channel. This is the node that would have
1930
        // produced Policy1 if it exists.
1931
        Node1 *models.LightningNode
1932

1933
        // Node2 is "node 2" in the channel. This is the node that would have
1934
        // produced Policy2 if it exists.
1935
        Node2 *models.LightningNode
1936
}
1937

1938
// ChanUpdatesInHorizon returns all the known channel edges which have at least
1939
// one edge that has an update timestamp within the specified horizon.
1940
func (c *KVStore) ChanUpdatesInHorizon(startTime,
1941
        endTime time.Time) ([]ChannelEdge, error) {
3✔
1942

3✔
1943
        // To ensure we don't return duplicate ChannelEdges, we'll use an
3✔
1944
        // additional map to keep track of the edges already seen to prevent
3✔
1945
        // re-adding it.
3✔
1946
        var edgesSeen map[uint64]struct{}
3✔
1947
        var edgesToCache map[uint64]ChannelEdge
3✔
1948
        var edgesInHorizon []ChannelEdge
3✔
1949

3✔
1950
        c.cacheMu.Lock()
3✔
1951
        defer c.cacheMu.Unlock()
3✔
1952

3✔
1953
        var hits int
3✔
1954
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1955
                edges := tx.ReadBucket(edgeBucket)
3✔
1956
                if edges == nil {
3✔
1957
                        return ErrGraphNoEdgesFound
×
1958
                }
×
1959
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
1960
                if edgeIndex == nil {
3✔
1961
                        return ErrGraphNoEdgesFound
×
1962
                }
×
1963
                edgeUpdateIndex := edges.NestedReadBucket(edgeUpdateIndexBucket)
3✔
1964
                if edgeUpdateIndex == nil {
3✔
1965
                        return ErrGraphNoEdgesFound
×
1966
                }
×
1967

1968
                nodes := tx.ReadBucket(nodeBucket)
3✔
1969
                if nodes == nil {
3✔
1970
                        return ErrGraphNodesNotFound
×
1971
                }
×
1972

1973
                // We'll now obtain a cursor to perform a range query within
1974
                // the index to find all channels within the horizon.
1975
                updateCursor := edgeUpdateIndex.ReadCursor()
3✔
1976

3✔
1977
                var startTimeBytes, endTimeBytes [8 + 8]byte
3✔
1978
                byteOrder.PutUint64(
3✔
1979
                        startTimeBytes[:8], uint64(startTime.Unix()),
3✔
1980
                )
3✔
1981
                byteOrder.PutUint64(
3✔
1982
                        endTimeBytes[:8], uint64(endTime.Unix()),
3✔
1983
                )
3✔
1984

3✔
1985
                // With our start and end times constructed, we'll step through
3✔
1986
                // the index collecting the info and policy of each update of
3✔
1987
                // each channel that has a last update within the time range.
3✔
1988
                //
3✔
1989
                //nolint:ll
3✔
1990
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
3✔
1991
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
6✔
1992
                        // We have a new eligible entry, so we'll slice of the
3✔
1993
                        // chan ID so we can query it in the DB.
3✔
1994
                        chanID := indexKey[8:]
3✔
1995

3✔
1996
                        // If we've already retrieved the info and policies for
3✔
1997
                        // this edge, then we can skip it as we don't need to do
3✔
1998
                        // so again.
3✔
1999
                        chanIDInt := byteOrder.Uint64(chanID)
3✔
2000
                        if _, ok := edgesSeen[chanIDInt]; ok {
3✔
UNCOV
2001
                                continue
×
2002
                        }
2003

2004
                        if channel, ok := c.chanCache.get(chanIDInt); ok {
6✔
2005
                                hits++
3✔
2006
                                edgesSeen[chanIDInt] = struct{}{}
3✔
2007
                                edgesInHorizon = append(edgesInHorizon, channel)
3✔
2008

3✔
2009
                                continue
3✔
2010
                        }
2011

2012
                        // First, we'll fetch the static edge information.
2013
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
2014
                        if err != nil {
3✔
2015
                                chanID := byteOrder.Uint64(chanID)
×
2016
                                return fmt.Errorf("unable to fetch info for "+
×
2017
                                        "edge with chan_id=%v: %v", chanID, err)
×
2018
                        }
×
2019

2020
                        // With the static information obtained, we'll now
2021
                        // fetch the dynamic policy info.
2022
                        edge1, edge2, err := fetchChanEdgePolicies(
3✔
2023
                                edgeIndex, edges, chanID,
3✔
2024
                        )
3✔
2025
                        if err != nil {
3✔
2026
                                chanID := byteOrder.Uint64(chanID)
×
2027
                                return fmt.Errorf("unable to fetch policies "+
×
2028
                                        "for edge with chan_id=%v: %v", chanID,
×
2029
                                        err)
×
2030
                        }
×
2031

2032
                        node1, err := fetchLightningNode(
3✔
2033
                                nodes, edgeInfo.NodeKey1Bytes[:],
3✔
2034
                        )
3✔
2035
                        if err != nil {
3✔
2036
                                return err
×
2037
                        }
×
2038

2039
                        node2, err := fetchLightningNode(
3✔
2040
                                nodes, edgeInfo.NodeKey2Bytes[:],
3✔
2041
                        )
3✔
2042
                        if err != nil {
3✔
2043
                                return err
×
2044
                        }
×
2045

2046
                        // Finally, we'll collate this edge with the rest of
2047
                        // edges to be returned.
2048
                        edgesSeen[chanIDInt] = struct{}{}
3✔
2049
                        channel := ChannelEdge{
3✔
2050
                                Info:    &edgeInfo,
3✔
2051
                                Policy1: edge1,
3✔
2052
                                Policy2: edge2,
3✔
2053
                                Node1:   &node1,
3✔
2054
                                Node2:   &node2,
3✔
2055
                        }
3✔
2056
                        edgesInHorizon = append(edgesInHorizon, channel)
3✔
2057
                        edgesToCache[chanIDInt] = channel
3✔
2058
                }
2059

2060
                return nil
3✔
2061
        }, func() {
3✔
2062
                edgesSeen = make(map[uint64]struct{})
3✔
2063
                edgesToCache = make(map[uint64]ChannelEdge)
3✔
2064
                edgesInHorizon = nil
3✔
2065
        })
3✔
2066
        switch {
3✔
2067
        case errors.Is(err, ErrGraphNoEdgesFound):
×
2068
                fallthrough
×
2069
        case errors.Is(err, ErrGraphNodesNotFound):
×
2070
                break
×
2071

2072
        case err != nil:
×
2073
                return nil, err
×
2074
        }
2075

2076
        // Insert any edges loaded from disk into the cache.
2077
        for chanid, channel := range edgesToCache {
6✔
2078
                c.chanCache.insert(chanid, channel)
3✔
2079
        }
3✔
2080

2081
        log.Debugf("ChanUpdatesInHorizon hit percentage: %f (%d/%d)",
3✔
2082
                float64(hits)/float64(len(edgesInHorizon)), hits,
3✔
2083
                len(edgesInHorizon))
3✔
2084

3✔
2085
        return edgesInHorizon, nil
3✔
2086
}
2087

2088
// NodeUpdatesInHorizon returns all the known lightning node which have an
2089
// update timestamp within the passed range. This method can be used by two
2090
// nodes to quickly determine if they have the same set of up to date node
2091
// announcements.
2092
func (c *KVStore) NodeUpdatesInHorizon(startTime,
2093
        endTime time.Time) ([]models.LightningNode, error) {
3✔
2094

3✔
2095
        var nodesInHorizon []models.LightningNode
3✔
2096

3✔
2097
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
2098
                nodes := tx.ReadBucket(nodeBucket)
3✔
2099
                if nodes == nil {
3✔
2100
                        return ErrGraphNodesNotFound
×
2101
                }
×
2102

2103
                nodeUpdateIndex := nodes.NestedReadBucket(nodeUpdateIndexBucket)
3✔
2104
                if nodeUpdateIndex == nil {
3✔
2105
                        return ErrGraphNodesNotFound
×
2106
                }
×
2107

2108
                // We'll now obtain a cursor to perform a range query within
2109
                // the index to find all node announcements within the horizon.
2110
                updateCursor := nodeUpdateIndex.ReadCursor()
3✔
2111

3✔
2112
                var startTimeBytes, endTimeBytes [8 + 33]byte
3✔
2113
                byteOrder.PutUint64(
3✔
2114
                        startTimeBytes[:8], uint64(startTime.Unix()),
3✔
2115
                )
3✔
2116
                byteOrder.PutUint64(
3✔
2117
                        endTimeBytes[:8], uint64(endTime.Unix()),
3✔
2118
                )
3✔
2119

3✔
2120
                // With our start and end times constructed, we'll step through
3✔
2121
                // the index collecting info for each node within the time
3✔
2122
                // range.
3✔
2123
                //
3✔
2124
                //nolint:ll
3✔
2125
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
3✔
2126
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
6✔
2127
                        nodePub := indexKey[8:]
3✔
2128
                        node, err := fetchLightningNode(nodes, nodePub)
3✔
2129
                        if err != nil {
3✔
2130
                                return err
×
2131
                        }
×
2132

2133
                        nodesInHorizon = append(nodesInHorizon, node)
3✔
2134
                }
2135

2136
                return nil
3✔
2137
        }, func() {
3✔
2138
                nodesInHorizon = nil
3✔
2139
        })
3✔
2140
        switch {
3✔
2141
        case errors.Is(err, ErrGraphNoEdgesFound):
×
2142
                fallthrough
×
2143
        case errors.Is(err, ErrGraphNodesNotFound):
×
2144
                break
×
2145

2146
        case err != nil:
×
2147
                return nil, err
×
2148
        }
2149

2150
        return nodesInHorizon, nil
3✔
2151
}
2152

2153
// FilterKnownChanIDs takes a set of channel IDs and return the subset of chan
2154
// ID's that we don't know and are not known zombies of the passed set. In other
2155
// words, we perform a set difference of our set of chan ID's and the ones
2156
// passed in. This method can be used by callers to determine the set of
2157
// channels another peer knows of that we don't. The ChannelUpdateInfos for the
2158
// known zombies is also returned.
2159
func (c *KVStore) FilterKnownChanIDs(chansInfo []ChannelUpdateInfo) ([]uint64,
2160
        []ChannelUpdateInfo, error) {
3✔
2161

3✔
2162
        var (
3✔
2163
                newChanIDs   []uint64
3✔
2164
                knownZombies []ChannelUpdateInfo
3✔
2165
        )
3✔
2166

3✔
2167
        c.cacheMu.Lock()
3✔
2168
        defer c.cacheMu.Unlock()
3✔
2169

3✔
2170
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
2171
                edges := tx.ReadBucket(edgeBucket)
3✔
2172
                if edges == nil {
3✔
2173
                        return ErrGraphNoEdgesFound
×
2174
                }
×
2175
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
2176
                if edgeIndex == nil {
3✔
2177
                        return ErrGraphNoEdgesFound
×
2178
                }
×
2179

2180
                // Fetch the zombie index, it may not exist if no edges have
2181
                // ever been marked as zombies. If the index has been
2182
                // initialized, we will use it later to skip known zombie edges.
2183
                zombieIndex := edges.NestedReadBucket(zombieBucket)
3✔
2184

3✔
2185
                // We'll run through the set of chanIDs and collate only the
3✔
2186
                // set of channel that are unable to be found within our db.
3✔
2187
                var cidBytes [8]byte
3✔
2188
                for _, info := range chansInfo {
6✔
2189
                        scid := info.ShortChannelID.ToUint64()
3✔
2190
                        byteOrder.PutUint64(cidBytes[:], scid)
3✔
2191

3✔
2192
                        // If the edge is already known, skip it.
3✔
2193
                        if v := edgeIndex.Get(cidBytes[:]); v != nil {
6✔
2194
                                continue
3✔
2195
                        }
2196

2197
                        // If the edge is a known zombie, skip it.
2198
                        if zombieIndex != nil {
6✔
2199
                                isZombie, _, _ := isZombieEdge(
3✔
2200
                                        zombieIndex, scid,
3✔
2201
                                )
3✔
2202

3✔
2203
                                if isZombie {
3✔
NEW
2204
                                        knownZombies = append(
×
NEW
2205
                                                knownZombies, info,
×
NEW
2206
                                        )
×
NEW
2207

×
UNCOV
2208
                                        continue
×
2209
                                }
2210
                        }
2211

2212
                        newChanIDs = append(newChanIDs, scid)
3✔
2213
                }
2214

2215
                return nil
3✔
2216
        }, func() {
3✔
2217
                newChanIDs = nil
3✔
2218
                knownZombies = nil
3✔
2219
        })
3✔
2220
        switch {
3✔
2221
        // If we don't know of any edges yet, then we'll return the entire set
2222
        // of chan IDs specified.
2223
        case errors.Is(err, ErrGraphNoEdgesFound):
×
2224
                ogChanIDs := make([]uint64, len(chansInfo))
×
2225
                for i, info := range chansInfo {
×
2226
                        ogChanIDs[i] = info.ShortChannelID.ToUint64()
×
2227
                }
×
2228

2229
                return ogChanIDs, nil, nil
×
2230

2231
        case err != nil:
×
2232
                return nil, nil, err
×
2233
        }
2234

2235
        return newChanIDs, knownZombies, nil
3✔
2236
}
2237

2238
// ChannelUpdateInfo couples the SCID of a channel with the timestamps of the
2239
// latest received channel updates for the channel.
2240
type ChannelUpdateInfo struct {
2241
        // ShortChannelID is the SCID identifier of the channel.
2242
        ShortChannelID lnwire.ShortChannelID
2243

2244
        // Node1UpdateTimestamp is the timestamp of the latest received update
2245
        // from the node 1 channel peer. This will be set to zero time if no
2246
        // update has yet been received from this node.
2247
        Node1UpdateTimestamp time.Time
2248

2249
        // Node2UpdateTimestamp is the timestamp of the latest received update
2250
        // from the node 2 channel peer. This will be set to zero time if no
2251
        // update has yet been received from this node.
2252
        Node2UpdateTimestamp time.Time
2253
}
2254

2255
// NewChannelUpdateInfo is a constructor which makes sure we initialize the
2256
// timestamps with zero seconds unix timestamp which equals
2257
// `January 1, 1970, 00:00:00 UTC` in case the value is `time.Time{}`.
2258
func NewChannelUpdateInfo(scid lnwire.ShortChannelID, node1Timestamp,
2259
        node2Timestamp time.Time) ChannelUpdateInfo {
3✔
2260

3✔
2261
        chanInfo := ChannelUpdateInfo{
3✔
2262
                ShortChannelID:       scid,
3✔
2263
                Node1UpdateTimestamp: node1Timestamp,
3✔
2264
                Node2UpdateTimestamp: node2Timestamp,
3✔
2265
        }
3✔
2266

3✔
2267
        if node1Timestamp.IsZero() {
6✔
2268
                chanInfo.Node1UpdateTimestamp = time.Unix(0, 0)
3✔
2269
        }
3✔
2270

2271
        if node2Timestamp.IsZero() {
6✔
2272
                chanInfo.Node2UpdateTimestamp = time.Unix(0, 0)
3✔
2273
        }
3✔
2274

2275
        return chanInfo
3✔
2276
}
2277

2278
// BlockChannelRange represents a range of channels for a given block height.
2279
type BlockChannelRange struct {
2280
        // Height is the height of the block all of the channels below were
2281
        // included in.
2282
        Height uint32
2283

2284
        // Channels is the list of channels identified by their short ID
2285
        // representation known to us that were included in the block height
2286
        // above. The list may include channel update timestamp information if
2287
        // requested.
2288
        Channels []ChannelUpdateInfo
2289
}
2290

2291
// FilterChannelRange returns the channel ID's of all known channels which were
2292
// mined in a block height within the passed range. The channel IDs are grouped
2293
// by their common block height. This method can be used to quickly share with a
2294
// peer the set of channels we know of within a particular range to catch them
2295
// up after a period of time offline. If withTimestamps is true then the
2296
// timestamp info of the latest received channel update messages of the channel
2297
// will be included in the response.
2298
func (c *KVStore) FilterChannelRange(startHeight,
2299
        endHeight uint32, withTimestamps bool) ([]BlockChannelRange, error) {
3✔
2300

3✔
2301
        startChanID := &lnwire.ShortChannelID{
3✔
2302
                BlockHeight: startHeight,
3✔
2303
        }
3✔
2304

3✔
2305
        endChanID := lnwire.ShortChannelID{
3✔
2306
                BlockHeight: endHeight,
3✔
2307
                TxIndex:     math.MaxUint32 & 0x00ffffff,
3✔
2308
                TxPosition:  math.MaxUint16,
3✔
2309
        }
3✔
2310

3✔
2311
        // As we need to perform a range scan, we'll convert the starting and
3✔
2312
        // ending height to their corresponding values when encoded using short
3✔
2313
        // channel ID's.
3✔
2314
        var chanIDStart, chanIDEnd [8]byte
3✔
2315
        byteOrder.PutUint64(chanIDStart[:], startChanID.ToUint64())
3✔
2316
        byteOrder.PutUint64(chanIDEnd[:], endChanID.ToUint64())
3✔
2317

3✔
2318
        var channelsPerBlock map[uint32][]ChannelUpdateInfo
3✔
2319
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
2320
                edges := tx.ReadBucket(edgeBucket)
3✔
2321
                if edges == nil {
3✔
2322
                        return ErrGraphNoEdgesFound
×
2323
                }
×
2324
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
2325
                if edgeIndex == nil {
3✔
2326
                        return ErrGraphNoEdgesFound
×
2327
                }
×
2328

2329
                cursor := edgeIndex.ReadCursor()
3✔
2330

3✔
2331
                // We'll now iterate through the database, and find each
3✔
2332
                // channel ID that resides within the specified range.
3✔
2333
                //
3✔
2334
                //nolint:ll
3✔
2335
                for k, v := cursor.Seek(chanIDStart[:]); k != nil &&
3✔
2336
                        bytes.Compare(k, chanIDEnd[:]) <= 0; k, v = cursor.Next() {
6✔
2337
                        // Don't send alias SCIDs during gossip sync.
3✔
2338
                        edgeReader := bytes.NewReader(v)
3✔
2339
                        edgeInfo, err := deserializeChanEdgeInfo(edgeReader)
3✔
2340
                        if err != nil {
3✔
2341
                                return err
×
2342
                        }
×
2343

2344
                        if edgeInfo.AuthProof == nil {
6✔
2345
                                continue
3✔
2346
                        }
2347

2348
                        // This channel ID rests within the target range, so
2349
                        // we'll add it to our returned set.
2350
                        rawCid := byteOrder.Uint64(k)
3✔
2351
                        cid := lnwire.NewShortChanIDFromInt(rawCid)
3✔
2352

3✔
2353
                        chanInfo := NewChannelUpdateInfo(
3✔
2354
                                cid, time.Time{}, time.Time{},
3✔
2355
                        )
3✔
2356

3✔
2357
                        if !withTimestamps {
3✔
UNCOV
2358
                                channelsPerBlock[cid.BlockHeight] = append(
×
UNCOV
2359
                                        channelsPerBlock[cid.BlockHeight],
×
UNCOV
2360
                                        chanInfo,
×
UNCOV
2361
                                )
×
UNCOV
2362

×
UNCOV
2363
                                continue
×
2364
                        }
2365

2366
                        node1Key, node2Key := computeEdgePolicyKeys(&edgeInfo)
3✔
2367

3✔
2368
                        rawPolicy := edges.Get(node1Key)
3✔
2369
                        if len(rawPolicy) != 0 {
6✔
2370
                                r := bytes.NewReader(rawPolicy)
3✔
2371

3✔
2372
                                edge, err := deserializeChanEdgePolicyRaw(r)
3✔
2373
                                if err != nil && !errors.Is(
3✔
2374
                                        err, ErrEdgePolicyOptionalFieldNotFound,
3✔
2375
                                ) {
3✔
2376

×
2377
                                        return err
×
2378
                                }
×
2379

2380
                                chanInfo.Node1UpdateTimestamp = edge.LastUpdate
3✔
2381
                        }
2382

2383
                        rawPolicy = edges.Get(node2Key)
3✔
2384
                        if len(rawPolicy) != 0 {
6✔
2385
                                r := bytes.NewReader(rawPolicy)
3✔
2386

3✔
2387
                                edge, err := deserializeChanEdgePolicyRaw(r)
3✔
2388
                                if err != nil && !errors.Is(
3✔
2389
                                        err, ErrEdgePolicyOptionalFieldNotFound,
3✔
2390
                                ) {
3✔
2391

×
2392
                                        return err
×
2393
                                }
×
2394

2395
                                chanInfo.Node2UpdateTimestamp = edge.LastUpdate
3✔
2396
                        }
2397

2398
                        channelsPerBlock[cid.BlockHeight] = append(
3✔
2399
                                channelsPerBlock[cid.BlockHeight], chanInfo,
3✔
2400
                        )
3✔
2401
                }
2402

2403
                return nil
3✔
2404
        }, func() {
3✔
2405
                channelsPerBlock = make(map[uint32][]ChannelUpdateInfo)
3✔
2406
        })
3✔
2407

2408
        switch {
3✔
2409
        // If we don't know of any channels yet, then there's nothing to
2410
        // filter, so we'll return an empty slice.
2411
        case errors.Is(err, ErrGraphNoEdgesFound) || len(channelsPerBlock) == 0:
3✔
2412
                return nil, nil
3✔
2413

2414
        case err != nil:
×
2415
                return nil, err
×
2416
        }
2417

2418
        // Return the channel ranges in ascending block height order.
2419
        blocks := make([]uint32, 0, len(channelsPerBlock))
3✔
2420
        for block := range channelsPerBlock {
6✔
2421
                blocks = append(blocks, block)
3✔
2422
        }
3✔
2423
        sort.Slice(blocks, func(i, j int) bool {
6✔
2424
                return blocks[i] < blocks[j]
3✔
2425
        })
3✔
2426

2427
        channelRanges := make([]BlockChannelRange, 0, len(channelsPerBlock))
3✔
2428
        for _, block := range blocks {
6✔
2429
                channelRanges = append(channelRanges, BlockChannelRange{
3✔
2430
                        Height:   block,
3✔
2431
                        Channels: channelsPerBlock[block],
3✔
2432
                })
3✔
2433
        }
3✔
2434

2435
        return channelRanges, nil
3✔
2436
}
2437

2438
// FetchChanInfos returns the set of channel edges that correspond to the passed
2439
// channel ID's. If an edge is the query is unknown to the database, it will
2440
// skipped and the result will contain only those edges that exist at the time
2441
// of the query. This can be used to respond to peer queries that are seeking to
2442
// fill in gaps in their view of the channel graph.
2443
func (c *KVStore) FetchChanInfos(chanIDs []uint64) ([]ChannelEdge, error) {
3✔
2444
        return c.fetchChanInfos(nil, chanIDs)
3✔
2445
}
3✔
2446

2447
// fetchChanInfos returns the set of channel edges that correspond to the passed
2448
// channel ID's. If an edge is the query is unknown to the database, it will
2449
// skipped and the result will contain only those edges that exist at the time
2450
// of the query. This can be used to respond to peer queries that are seeking to
2451
// fill in gaps in their view of the channel graph.
2452
//
2453
// NOTE: An optional transaction may be provided. If none is provided, then a
2454
// new one will be created.
2455
func (c *KVStore) fetchChanInfos(tx kvdb.RTx, chanIDs []uint64) (
2456
        []ChannelEdge, error) {
3✔
2457
        // TODO(roasbeef): sort cids?
3✔
2458

3✔
2459
        var (
3✔
2460
                chanEdges []ChannelEdge
3✔
2461
                cidBytes  [8]byte
3✔
2462
        )
3✔
2463

3✔
2464
        fetchChanInfos := func(tx kvdb.RTx) error {
6✔
2465
                edges := tx.ReadBucket(edgeBucket)
3✔
2466
                if edges == nil {
3✔
2467
                        return ErrGraphNoEdgesFound
×
2468
                }
×
2469
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
2470
                if edgeIndex == nil {
3✔
2471
                        return ErrGraphNoEdgesFound
×
2472
                }
×
2473
                nodes := tx.ReadBucket(nodeBucket)
3✔
2474
                if nodes == nil {
3✔
2475
                        return ErrGraphNotFound
×
2476
                }
×
2477

2478
                for _, cid := range chanIDs {
6✔
2479
                        byteOrder.PutUint64(cidBytes[:], cid)
3✔
2480

3✔
2481
                        // First, we'll fetch the static edge information. If
3✔
2482
                        // the edge is unknown, we will skip the edge and
3✔
2483
                        // continue gathering all known edges.
3✔
2484
                        edgeInfo, err := fetchChanEdgeInfo(
3✔
2485
                                edgeIndex, cidBytes[:],
3✔
2486
                        )
3✔
2487
                        switch {
3✔
UNCOV
2488
                        case errors.Is(err, ErrEdgeNotFound):
×
UNCOV
2489
                                continue
×
2490
                        case err != nil:
×
2491
                                return err
×
2492
                        }
2493

2494
                        // With the static information obtained, we'll now
2495
                        // fetch the dynamic policy info.
2496
                        edge1, edge2, err := fetchChanEdgePolicies(
3✔
2497
                                edgeIndex, edges, cidBytes[:],
3✔
2498
                        )
3✔
2499
                        if err != nil {
3✔
2500
                                return err
×
2501
                        }
×
2502

2503
                        node1, err := fetchLightningNode(
3✔
2504
                                nodes, edgeInfo.NodeKey1Bytes[:],
3✔
2505
                        )
3✔
2506
                        if err != nil {
3✔
2507
                                return err
×
2508
                        }
×
2509

2510
                        node2, err := fetchLightningNode(
3✔
2511
                                nodes, edgeInfo.NodeKey2Bytes[:],
3✔
2512
                        )
3✔
2513
                        if err != nil {
3✔
2514
                                return err
×
2515
                        }
×
2516

2517
                        chanEdges = append(chanEdges, ChannelEdge{
3✔
2518
                                Info:    &edgeInfo,
3✔
2519
                                Policy1: edge1,
3✔
2520
                                Policy2: edge2,
3✔
2521
                                Node1:   &node1,
3✔
2522
                                Node2:   &node2,
3✔
2523
                        })
3✔
2524
                }
2525

2526
                return nil
3✔
2527
        }
2528

2529
        if tx == nil {
6✔
2530
                err := kvdb.View(c.db, fetchChanInfos, func() {
6✔
2531
                        chanEdges = nil
3✔
2532
                })
3✔
2533
                if err != nil {
3✔
2534
                        return nil, err
×
2535
                }
×
2536

2537
                return chanEdges, nil
3✔
2538
        }
2539

2540
        err := fetchChanInfos(tx)
×
2541
        if err != nil {
×
2542
                return nil, err
×
2543
        }
×
2544

2545
        return chanEdges, nil
×
2546
}
2547

2548
func delEdgeUpdateIndexEntry(edgesBucket kvdb.RwBucket, chanID uint64,
2549
        edge1, edge2 *models.ChannelEdgePolicy) error {
3✔
2550

3✔
2551
        // First, we'll fetch the edge update index bucket which currently
3✔
2552
        // stores an entry for the channel we're about to delete.
3✔
2553
        updateIndex := edgesBucket.NestedReadWriteBucket(edgeUpdateIndexBucket)
3✔
2554
        if updateIndex == nil {
3✔
2555
                // No edges in bucket, return early.
×
2556
                return nil
×
2557
        }
×
2558

2559
        // Now that we have the bucket, we'll attempt to construct a template
2560
        // for the index key: updateTime || chanid.
2561
        var indexKey [8 + 8]byte
3✔
2562
        byteOrder.PutUint64(indexKey[8:], chanID)
3✔
2563

3✔
2564
        // With the template constructed, we'll attempt to delete an entry that
3✔
2565
        // would have been created by both edges: we'll alternate the update
3✔
2566
        // times, as one may had overridden the other.
3✔
2567
        if edge1 != nil {
6✔
2568
                byteOrder.PutUint64(
3✔
2569
                        indexKey[:8], uint64(edge1.LastUpdate.Unix()),
3✔
2570
                )
3✔
2571
                if err := updateIndex.Delete(indexKey[:]); err != nil {
3✔
2572
                        return err
×
2573
                }
×
2574
        }
2575

2576
        // We'll also attempt to delete the entry that may have been created by
2577
        // the second edge.
2578
        if edge2 != nil {
6✔
2579
                byteOrder.PutUint64(
3✔
2580
                        indexKey[:8], uint64(edge2.LastUpdate.Unix()),
3✔
2581
                )
3✔
2582
                if err := updateIndex.Delete(indexKey[:]); err != nil {
3✔
2583
                        return err
×
2584
                }
×
2585
        }
2586

2587
        return nil
3✔
2588
}
2589

2590
// delChannelEdgeUnsafe deletes the edge with the given chanID from the graph
2591
// cache. It then goes on to delete any policy info and edge info for this
2592
// channel from the DB and finally, if isZombie is true, it will add an entry
2593
// for this channel in the zombie index.
2594
//
2595
// NOTE: this method MUST only be called if the cacheMu has already been
2596
// acquired.
2597
func (c *KVStore) delChannelEdgeUnsafe(edges, edgeIndex, chanIndex,
2598
        zombieIndex kvdb.RwBucket, chanID []byte, isZombie,
2599
        strictZombie bool) error {
3✔
2600

3✔
2601
        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
2602
        if err != nil {
3✔
UNCOV
2603
                return err
×
UNCOV
2604
        }
×
2605

2606
        if c.graphCache != nil {
6✔
2607
                c.graphCache.RemoveChannel(
3✔
2608
                        edgeInfo.NodeKey1Bytes, edgeInfo.NodeKey2Bytes,
3✔
2609
                        edgeInfo.ChannelID,
3✔
2610
                )
3✔
2611
        }
3✔
2612

2613
        // We'll also remove the entry in the edge update index bucket before
2614
        // we delete the edges themselves so we can access their last update
2615
        // times.
2616
        cid := byteOrder.Uint64(chanID)
3✔
2617
        edge1, edge2, err := fetchChanEdgePolicies(edgeIndex, edges, chanID)
3✔
2618
        if err != nil {
3✔
2619
                return err
×
2620
        }
×
2621
        err = delEdgeUpdateIndexEntry(edges, cid, edge1, edge2)
3✔
2622
        if err != nil {
3✔
2623
                return err
×
2624
        }
×
2625

2626
        // The edge key is of the format pubKey || chanID. First we construct
2627
        // the latter half, populating the channel ID.
2628
        var edgeKey [33 + 8]byte
3✔
2629
        copy(edgeKey[33:], chanID)
3✔
2630

3✔
2631
        // With the latter half constructed, copy over the first public key to
3✔
2632
        // delete the edge in this direction, then the second to delete the
3✔
2633
        // edge in the opposite direction.
3✔
2634
        copy(edgeKey[:33], edgeInfo.NodeKey1Bytes[:])
3✔
2635
        if edges.Get(edgeKey[:]) != nil {
6✔
2636
                if err := edges.Delete(edgeKey[:]); err != nil {
3✔
2637
                        return err
×
2638
                }
×
2639
        }
2640
        copy(edgeKey[:33], edgeInfo.NodeKey2Bytes[:])
3✔
2641
        if edges.Get(edgeKey[:]) != nil {
6✔
2642
                if err := edges.Delete(edgeKey[:]); err != nil {
3✔
2643
                        return err
×
2644
                }
×
2645
        }
2646

2647
        // As part of deleting the edge we also remove all disabled entries
2648
        // from the edgePolicyDisabledIndex bucket. We do that for both
2649
        // directions.
2650
        err = updateEdgePolicyDisabledIndex(edges, cid, false, false)
3✔
2651
        if err != nil {
3✔
2652
                return err
×
2653
        }
×
2654
        err = updateEdgePolicyDisabledIndex(edges, cid, true, false)
3✔
2655
        if err != nil {
3✔
2656
                return err
×
2657
        }
×
2658

2659
        // With the edge data deleted, we can purge the information from the two
2660
        // edge indexes.
2661
        if err := edgeIndex.Delete(chanID); err != nil {
3✔
2662
                return err
×
2663
        }
×
2664
        var b bytes.Buffer
3✔
2665
        if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
3✔
2666
                return err
×
2667
        }
×
2668
        if err := chanIndex.Delete(b.Bytes()); err != nil {
3✔
2669
                return err
×
2670
        }
×
2671

2672
        // Finally, we'll mark the edge as a zombie within our index if it's
2673
        // being removed due to the channel becoming a zombie. We do this to
2674
        // ensure we don't store unnecessary data for spent channels.
2675
        if !isZombie {
6✔
2676
                return nil
3✔
2677
        }
3✔
2678

2679
        nodeKey1, nodeKey2 := edgeInfo.NodeKey1Bytes, edgeInfo.NodeKey2Bytes
3✔
2680
        if strictZombie {
3✔
UNCOV
2681
                nodeKey1, nodeKey2 = makeZombiePubkeys(&edgeInfo, edge1, edge2)
×
UNCOV
2682
        }
×
2683

2684
        return markEdgeZombie(
3✔
2685
                zombieIndex, byteOrder.Uint64(chanID), nodeKey1, nodeKey2,
3✔
2686
        )
3✔
2687
}
2688

2689
// makeZombiePubkeys derives the node pubkeys to store in the zombie index for a
2690
// particular pair of channel policies. The return values are one of:
2691
//  1. (pubkey1, pubkey2)
2692
//  2. (pubkey1, blank)
2693
//  3. (blank, pubkey2)
2694
//
2695
// A blank pubkey means that corresponding node will be unable to resurrect a
2696
// channel on its own. For example, node1 may continue to publish recent
2697
// updates, but node2 has fallen way behind. After marking an edge as a zombie,
2698
// we don't want another fresh update from node1 to resurrect, as the edge can
2699
// only become live once node2 finally sends something recent.
2700
//
2701
// In the case where we have neither update, we allow either party to resurrect
2702
// the channel. If the channel were to be marked zombie again, it would be
2703
// marked with the correct lagging channel since we received an update from only
2704
// one side.
2705
func makeZombiePubkeys(info *models.ChannelEdgeInfo,
UNCOV
2706
        e1, e2 *models.ChannelEdgePolicy) ([33]byte, [33]byte) {
×
UNCOV
2707

×
UNCOV
2708
        switch {
×
2709
        // If we don't have either edge policy, we'll return both pubkeys so
2710
        // that the channel can be resurrected by either party.
2711
        case e1 == nil && e2 == nil:
×
2712
                return info.NodeKey1Bytes, info.NodeKey2Bytes
×
2713

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

2721
        // Otherwise, we're missing edge2 or edge2 is the older side, so we
2722
        // return a blank pubkey for edge1. In this case, only an update from
2723
        // edge2 can resurect the channel.
UNCOV
2724
        default:
×
UNCOV
2725
                return [33]byte{}, info.NodeKey2Bytes
×
2726
        }
2727
}
2728

2729
// UpdateEdgePolicy updates the edge routing policy for a single directed edge
2730
// within the database for the referenced channel. The `flags` attribute within
2731
// the ChannelEdgePolicy determines which of the directed edges are being
2732
// updated. If the flag is 1, then the first node's information is being
2733
// updated, otherwise it's the second node's information. The node ordering is
2734
// determined by the lexicographical ordering of the identity public keys of the
2735
// nodes on either side of the channel.
2736
func (c *KVStore) UpdateEdgePolicy(edge *models.ChannelEdgePolicy,
2737
        op ...batch.SchedulerOption) (route.Vertex, route.Vertex, error) {
3✔
2738

3✔
2739
        var (
3✔
2740
                isUpdate1    bool
3✔
2741
                edgeNotFound bool
3✔
2742
                from, to     route.Vertex
3✔
2743
        )
3✔
2744

3✔
2745
        r := &batch.Request{
3✔
2746
                Reset: func() {
6✔
2747
                        isUpdate1 = false
3✔
2748
                        edgeNotFound = false
3✔
2749
                },
3✔
2750
                Update: func(tx kvdb.RwTx) error {
3✔
2751
                        var err error
3✔
2752
                        from, to, isUpdate1, err = updateEdgePolicy(tx, edge)
3✔
2753
                        if err != nil {
3✔
UNCOV
2754
                                log.Errorf("UpdateEdgePolicy faild: %v", err)
×
UNCOV
2755
                        }
×
2756

2757
                        // Silence ErrEdgeNotFound so that the batch can
2758
                        // succeed, but propagate the error via local state.
2759
                        if errors.Is(err, ErrEdgeNotFound) {
3✔
UNCOV
2760
                                edgeNotFound = true
×
UNCOV
2761
                                return nil
×
UNCOV
2762
                        }
×
2763

2764
                        return err
3✔
2765
                },
2766
                OnCommit: func(err error) error {
3✔
2767
                        switch {
3✔
2768
                        case err != nil:
×
2769
                                return err
×
UNCOV
2770
                        case edgeNotFound:
×
UNCOV
2771
                                return ErrEdgeNotFound
×
2772
                        default:
3✔
2773
                                c.updateEdgeCache(edge, isUpdate1)
3✔
2774
                                return nil
3✔
2775
                        }
2776
                },
2777
        }
2778

2779
        for _, f := range op {
6✔
2780
                f(r)
3✔
2781
        }
3✔
2782

2783
        err := c.chanScheduler.Execute(r)
3✔
2784

3✔
2785
        return from, to, err
3✔
2786
}
2787

2788
func (c *KVStore) updateEdgeCache(e *models.ChannelEdgePolicy,
2789
        isUpdate1 bool) {
3✔
2790

3✔
2791
        // If an entry for this channel is found in reject cache, we'll modify
3✔
2792
        // the entry with the updated timestamp for the direction that was just
3✔
2793
        // written. If the edge doesn't exist, we'll load the cache entry lazily
3✔
2794
        // during the next query for this edge.
3✔
2795
        if entry, ok := c.rejectCache.get(e.ChannelID); ok {
6✔
2796
                if isUpdate1 {
6✔
2797
                        entry.upd1Time = e.LastUpdate.Unix()
3✔
2798
                } else {
6✔
2799
                        entry.upd2Time = e.LastUpdate.Unix()
3✔
2800
                }
3✔
2801
                c.rejectCache.insert(e.ChannelID, entry)
3✔
2802
        }
2803

2804
        // If an entry for this channel is found in channel cache, we'll modify
2805
        // the entry with the updated policy for the direction that was just
2806
        // written. If the edge doesn't exist, we'll defer loading the info and
2807
        // policies and lazily read from disk during the next query.
2808
        if channel, ok := c.chanCache.get(e.ChannelID); ok {
6✔
2809
                if isUpdate1 {
6✔
2810
                        channel.Policy1 = e
3✔
2811
                } else {
6✔
2812
                        channel.Policy2 = e
3✔
2813
                }
3✔
2814
                c.chanCache.insert(e.ChannelID, channel)
3✔
2815
        }
2816
}
2817

2818
// updateEdgePolicy attempts to update an edge's policy within the relevant
2819
// buckets using an existing database transaction. The returned boolean will be
2820
// true if the updated policy belongs to node1, and false if the policy belonged
2821
// to node2.
2822
func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy) (
2823
        route.Vertex, route.Vertex, bool, error) {
3✔
2824

3✔
2825
        var noVertex route.Vertex
3✔
2826

3✔
2827
        edges := tx.ReadWriteBucket(edgeBucket)
3✔
2828
        if edges == nil {
3✔
2829
                return noVertex, noVertex, false, ErrEdgeNotFound
×
2830
        }
×
2831
        edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
2832
        if edgeIndex == nil {
3✔
2833
                return noVertex, noVertex, false, ErrEdgeNotFound
×
2834
        }
×
2835

2836
        // Create the channelID key be converting the channel ID
2837
        // integer into a byte slice.
2838
        var chanID [8]byte
3✔
2839
        byteOrder.PutUint64(chanID[:], edge.ChannelID)
3✔
2840

3✔
2841
        // With the channel ID, we then fetch the value storing the two
3✔
2842
        // nodes which connect this channel edge.
3✔
2843
        nodeInfo := edgeIndex.Get(chanID[:])
3✔
2844
        if nodeInfo == nil {
3✔
UNCOV
2845
                return noVertex, noVertex, false, ErrEdgeNotFound
×
UNCOV
2846
        }
×
2847

2848
        // Depending on the flags value passed above, either the first
2849
        // or second edge policy is being updated.
2850
        var fromNode, toNode []byte
3✔
2851
        var isUpdate1 bool
3✔
2852
        if edge.ChannelFlags&lnwire.ChanUpdateDirection == 0 {
6✔
2853
                fromNode = nodeInfo[:33]
3✔
2854
                toNode = nodeInfo[33:66]
3✔
2855
                isUpdate1 = true
3✔
2856
        } else {
6✔
2857
                fromNode = nodeInfo[33:66]
3✔
2858
                toNode = nodeInfo[:33]
3✔
2859
                isUpdate1 = false
3✔
2860
        }
3✔
2861

2862
        // Finally, with the direction of the edge being updated
2863
        // identified, we update the on-disk edge representation.
2864
        err := putChanEdgePolicy(edges, edge, fromNode, toNode)
3✔
2865
        if err != nil {
3✔
2866
                return noVertex, noVertex, false, err
×
2867
        }
×
2868

2869
        var (
3✔
2870
                fromNodePubKey route.Vertex
3✔
2871
                toNodePubKey   route.Vertex
3✔
2872
        )
3✔
2873
        copy(fromNodePubKey[:], fromNode)
3✔
2874
        copy(toNodePubKey[:], toNode)
3✔
2875

3✔
2876
        return fromNodePubKey, toNodePubKey, isUpdate1, nil
3✔
2877
}
2878

2879
// isPublic determines whether the node is seen as public within the graph from
2880
// the source node's point of view. An existing database transaction can also be
2881
// specified.
2882
func (c *KVStore) isPublic(tx kvdb.RTx, nodePub route.Vertex,
2883
        sourcePubKey []byte) (bool, error) {
3✔
2884

3✔
2885
        // In order to determine whether this node is publicly advertised within
3✔
2886
        // the graph, we'll need to look at all of its edges and check whether
3✔
2887
        // they extend to any other node than the source node. errDone will be
3✔
2888
        // used to terminate the check early.
3✔
2889
        nodeIsPublic := false
3✔
2890
        errDone := errors.New("done")
3✔
2891
        err := c.ForEachNodeChannelTx(tx, nodePub, func(tx kvdb.RTx,
3✔
2892
                info *models.ChannelEdgeInfo, _ *models.ChannelEdgePolicy,
3✔
2893
                _ *models.ChannelEdgePolicy) error {
6✔
2894

3✔
2895
                // If this edge doesn't extend to the source node, we'll
3✔
2896
                // terminate our search as we can now conclude that the node is
3✔
2897
                // publicly advertised within the graph due to the local node
3✔
2898
                // knowing of the current edge.
3✔
2899
                if !bytes.Equal(info.NodeKey1Bytes[:], sourcePubKey) &&
3✔
2900
                        !bytes.Equal(info.NodeKey2Bytes[:], sourcePubKey) {
6✔
2901

3✔
2902
                        nodeIsPublic = true
3✔
2903
                        return errDone
3✔
2904
                }
3✔
2905

2906
                // Since the edge _does_ extend to the source node, we'll also
2907
                // need to ensure that this is a public edge.
2908
                if info.AuthProof != nil {
6✔
2909
                        nodeIsPublic = true
3✔
2910
                        return errDone
3✔
2911
                }
3✔
2912

2913
                // Otherwise, we'll continue our search.
2914
                return nil
3✔
2915
        })
2916
        if err != nil && !errors.Is(err, errDone) {
3✔
2917
                return false, err
×
2918
        }
×
2919

2920
        return nodeIsPublic, nil
3✔
2921
}
2922

2923
// FetchLightningNodeTx attempts to look up a target node by its identity
2924
// public key. If the node isn't found in the database, then
2925
// ErrGraphNodeNotFound is returned. An optional transaction may be provided.
2926
// If none is provided, then a new one will be created.
2927
func (c *KVStore) FetchLightningNodeTx(tx kvdb.RTx, nodePub route.Vertex) (
2928
        *models.LightningNode, error) {
3✔
2929

3✔
2930
        return c.fetchLightningNode(tx, nodePub)
3✔
2931
}
3✔
2932

2933
// FetchLightningNode attempts to look up a target node by its identity public
2934
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
2935
// returned.
2936
func (c *KVStore) FetchLightningNode(nodePub route.Vertex) (
2937
        *models.LightningNode, error) {
3✔
2938

3✔
2939
        return c.fetchLightningNode(nil, nodePub)
3✔
2940
}
3✔
2941

2942
// fetchLightningNode attempts to look up a target node by its identity public
2943
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
2944
// returned. An optional transaction may be provided. If none is provided, then
2945
// a new one will be created.
2946
func (c *KVStore) fetchLightningNode(tx kvdb.RTx,
2947
        nodePub route.Vertex) (*models.LightningNode, error) {
3✔
2948

3✔
2949
        var node *models.LightningNode
3✔
2950
        fetch := func(tx kvdb.RTx) error {
6✔
2951
                // First grab the nodes bucket which stores the mapping from
3✔
2952
                // pubKey to node information.
3✔
2953
                nodes := tx.ReadBucket(nodeBucket)
3✔
2954
                if nodes == nil {
3✔
2955
                        return ErrGraphNotFound
×
2956
                }
×
2957

2958
                // If a key for this serialized public key isn't found, then
2959
                // the target node doesn't exist within the database.
2960
                nodeBytes := nodes.Get(nodePub[:])
3✔
2961
                if nodeBytes == nil {
6✔
2962
                        return ErrGraphNodeNotFound
3✔
2963
                }
3✔
2964

2965
                // If the node is found, then we can de deserialize the node
2966
                // information to return to the user.
2967
                nodeReader := bytes.NewReader(nodeBytes)
3✔
2968
                n, err := deserializeLightningNode(nodeReader)
3✔
2969
                if err != nil {
3✔
2970
                        return err
×
2971
                }
×
2972

2973
                node = &n
3✔
2974

3✔
2975
                return nil
3✔
2976
        }
2977

2978
        if tx == nil {
6✔
2979
                err := kvdb.View(
3✔
2980
                        c.db, fetch, func() {
6✔
2981
                                node = nil
3✔
2982
                        },
3✔
2983
                )
2984
                if err != nil {
6✔
2985
                        return nil, err
3✔
2986
                }
3✔
2987

2988
                return node, nil
3✔
2989
        }
2990

UNCOV
2991
        err := fetch(tx)
×
UNCOV
2992
        if err != nil {
×
UNCOV
2993
                return nil, err
×
UNCOV
2994
        }
×
2995

UNCOV
2996
        return node, nil
×
2997
}
2998

2999
// HasLightningNode determines if the graph has a vertex identified by the
3000
// target node identity public key. If the node exists in the database, a
3001
// timestamp of when the data for the node was lasted updated is returned along
3002
// with a true boolean. Otherwise, an empty time.Time is returned with a false
3003
// boolean.
3004
func (c *KVStore) HasLightningNode(nodePub [33]byte) (time.Time, bool,
3005
        error) {
3✔
3006

3✔
3007
        var (
3✔
3008
                updateTime time.Time
3✔
3009
                exists     bool
3✔
3010
        )
3✔
3011

3✔
3012
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
3013
                // First grab the nodes bucket which stores the mapping from
3✔
3014
                // pubKey to node information.
3✔
3015
                nodes := tx.ReadBucket(nodeBucket)
3✔
3016
                if nodes == nil {
3✔
3017
                        return ErrGraphNotFound
×
3018
                }
×
3019

3020
                // If a key for this serialized public key isn't found, we can
3021
                // exit early.
3022
                nodeBytes := nodes.Get(nodePub[:])
3✔
3023
                if nodeBytes == nil {
6✔
3024
                        exists = false
3✔
3025
                        return nil
3✔
3026
                }
3✔
3027

3028
                // Otherwise we continue on to obtain the time stamp
3029
                // representing the last time the data for this node was
3030
                // updated.
3031
                nodeReader := bytes.NewReader(nodeBytes)
3✔
3032
                node, err := deserializeLightningNode(nodeReader)
3✔
3033
                if err != nil {
3✔
3034
                        return err
×
3035
                }
×
3036

3037
                exists = true
3✔
3038
                updateTime = node.LastUpdate
3✔
3039

3✔
3040
                return nil
3✔
3041
        }, func() {
3✔
3042
                updateTime = time.Time{}
3✔
3043
                exists = false
3✔
3044
        })
3✔
3045
        if err != nil {
3✔
3046
                return time.Time{}, exists, err
×
3047
        }
×
3048

3049
        return updateTime, exists, nil
3✔
3050
}
3051

3052
// nodeTraversal is used to traverse all channels of a node given by its
3053
// public key and passes channel information into the specified callback.
3054
func nodeTraversal(tx kvdb.RTx, nodePub []byte, db kvdb.Backend,
3055
        cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3056
                *models.ChannelEdgePolicy) error) error {
3✔
3057

3✔
3058
        traversal := func(tx kvdb.RTx) error {
6✔
3059
                edges := tx.ReadBucket(edgeBucket)
3✔
3060
                if edges == nil {
3✔
3061
                        return ErrGraphNotFound
×
3062
                }
×
3063
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
3064
                if edgeIndex == nil {
3✔
3065
                        return ErrGraphNoEdgesFound
×
3066
                }
×
3067

3068
                // In order to reach all the edges for this node, we take
3069
                // advantage of the construction of the key-space within the
3070
                // edge bucket. The keys are stored in the form: pubKey ||
3071
                // chanID. Therefore, starting from a chanID of zero, we can
3072
                // scan forward in the bucket, grabbing all the edges for the
3073
                // node. Once the prefix no longer matches, then we know we're
3074
                // done.
3075
                var nodeStart [33 + 8]byte
3✔
3076
                copy(nodeStart[:], nodePub)
3✔
3077
                copy(nodeStart[33:], chanStart[:])
3✔
3078

3✔
3079
                // Starting from the key pubKey || 0, we seek forward in the
3✔
3080
                // bucket until the retrieved key no longer has the public key
3✔
3081
                // as its prefix. This indicates that we've stepped over into
3✔
3082
                // another node's edges, so we can terminate our scan.
3✔
3083
                edgeCursor := edges.ReadCursor()
3✔
3084
                for nodeEdge, _ := edgeCursor.Seek(nodeStart[:]); bytes.HasPrefix(nodeEdge, nodePub); nodeEdge, _ = edgeCursor.Next() { //nolint:ll
6✔
3085
                        // If the prefix still matches, the channel id is
3✔
3086
                        // returned in nodeEdge. Channel id is used to lookup
3✔
3087
                        // the node at the other end of the channel and both
3✔
3088
                        // edge policies.
3✔
3089
                        chanID := nodeEdge[33:]
3✔
3090
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
3091
                        if err != nil {
3✔
3092
                                return err
×
3093
                        }
×
3094

3095
                        outgoingPolicy, err := fetchChanEdgePolicy(
3✔
3096
                                edges, chanID, nodePub,
3✔
3097
                        )
3✔
3098
                        if err != nil {
3✔
3099
                                return err
×
3100
                        }
×
3101

3102
                        otherNode, err := edgeInfo.OtherNodeKeyBytes(nodePub)
3✔
3103
                        if err != nil {
3✔
3104
                                return err
×
3105
                        }
×
3106

3107
                        incomingPolicy, err := fetchChanEdgePolicy(
3✔
3108
                                edges, chanID, otherNode[:],
3✔
3109
                        )
3✔
3110
                        if err != nil {
3✔
3111
                                return err
×
3112
                        }
×
3113

3114
                        // Finally, we execute the callback.
3115
                        err = cb(tx, &edgeInfo, outgoingPolicy, incomingPolicy)
3✔
3116
                        if err != nil {
6✔
3117
                                return err
3✔
3118
                        }
3✔
3119
                }
3120

3121
                return nil
3✔
3122
        }
3123

3124
        // If no transaction was provided, then we'll create a new transaction
3125
        // to execute the transaction within.
3126
        if tx == nil {
6✔
3127
                return kvdb.View(db, traversal, func() {})
6✔
3128
        }
3129

3130
        // Otherwise, we re-use the existing transaction to execute the graph
3131
        // traversal.
3132
        return traversal(tx)
3✔
3133
}
3134

3135
// ForEachNodeChannel iterates through all channels of the given node,
3136
// executing the passed callback with an edge info structure and the policies
3137
// of each end of the channel. The first edge policy is the outgoing edge *to*
3138
// the connecting node, while the second is the incoming edge *from* the
3139
// connecting node. If the callback returns an error, then the iteration is
3140
// halted with the error propagated back up to the caller.
3141
//
3142
// Unknown policies are passed into the callback as nil values.
3143
func (c *KVStore) ForEachNodeChannel(nodePub route.Vertex,
3144
        cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3145
                *models.ChannelEdgePolicy) error) error {
3✔
3146

3✔
3147
        return nodeTraversal(nil, nodePub[:], c.db, cb)
3✔
3148
}
3✔
3149

3150
// ForEachNodeChannelTx iterates through all channels of the given node,
3151
// executing the passed callback with an edge info structure and the policies
3152
// of each end of the channel. The first edge policy is the outgoing edge *to*
3153
// the connecting node, while the second is the incoming edge *from* the
3154
// connecting node. If the callback returns an error, then the iteration is
3155
// halted with the error propagated back up to the caller.
3156
//
3157
// Unknown policies are passed into the callback as nil values.
3158
//
3159
// If the caller wishes to re-use an existing boltdb transaction, then it
3160
// should be passed as the first argument.  Otherwise, the first argument should
3161
// be nil and a fresh transaction will be created to execute the graph
3162
// traversal.
3163
func (c *KVStore) ForEachNodeChannelTx(tx kvdb.RTx,
3164
        nodePub route.Vertex, cb func(kvdb.RTx, *models.ChannelEdgeInfo,
3165
                *models.ChannelEdgePolicy,
3166
                *models.ChannelEdgePolicy) error) error {
3✔
3167

3✔
3168
        return nodeTraversal(tx, nodePub[:], c.db, cb)
3✔
3169
}
3✔
3170

3171
// FetchOtherNode attempts to fetch the full LightningNode that's opposite of
3172
// the target node in the channel. This is useful when one knows the pubkey of
3173
// one of the nodes, and wishes to obtain the full LightningNode for the other
3174
// end of the channel.
3175
func (c *KVStore) FetchOtherNode(tx kvdb.RTx,
3176
        channel *models.ChannelEdgeInfo, thisNodeKey []byte) (
3177
        *models.LightningNode, error) {
3✔
3178

3✔
3179
        // Ensure that the node passed in is actually a member of the channel.
3✔
3180
        var targetNodeBytes [33]byte
3✔
3181
        switch {
3✔
3182
        case bytes.Equal(channel.NodeKey1Bytes[:], thisNodeKey):
3✔
3183
                targetNodeBytes = channel.NodeKey2Bytes
3✔
3184
        case bytes.Equal(channel.NodeKey2Bytes[:], thisNodeKey):
3✔
3185
                targetNodeBytes = channel.NodeKey1Bytes
3✔
3186
        default:
×
3187
                return nil, fmt.Errorf("node not participating in this channel")
×
3188
        }
3189

3190
        var targetNode *models.LightningNode
3✔
3191
        fetchNodeFunc := func(tx kvdb.RTx) error {
6✔
3192
                // First grab the nodes bucket which stores the mapping from
3✔
3193
                // pubKey to node information.
3✔
3194
                nodes := tx.ReadBucket(nodeBucket)
3✔
3195
                if nodes == nil {
3✔
3196
                        return ErrGraphNotFound
×
3197
                }
×
3198

3199
                node, err := fetchLightningNode(nodes, targetNodeBytes[:])
3✔
3200
                if err != nil {
3✔
3201
                        return err
×
3202
                }
×
3203

3204
                targetNode = &node
3✔
3205

3✔
3206
                return nil
3✔
3207
        }
3208

3209
        // If the transaction is nil, then we'll need to create a new one,
3210
        // otherwise we can use the existing db transaction.
3211
        var err error
3✔
3212
        if tx == nil {
3✔
3213
                err = kvdb.View(c.db, fetchNodeFunc, func() {
×
3214
                        targetNode = nil
×
3215
                })
×
3216
        } else {
3✔
3217
                err = fetchNodeFunc(tx)
3✔
3218
        }
3✔
3219

3220
        return targetNode, err
3✔
3221
}
3222

3223
// computeEdgePolicyKeys is a helper function that can be used to compute the
3224
// keys used to index the channel edge policy info for the two nodes of the
3225
// edge. The keys for node 1 and node 2 are returned respectively.
3226
func computeEdgePolicyKeys(info *models.ChannelEdgeInfo) ([]byte, []byte) {
3✔
3227
        var (
3✔
3228
                node1Key [33 + 8]byte
3✔
3229
                node2Key [33 + 8]byte
3✔
3230
        )
3✔
3231

3✔
3232
        copy(node1Key[:], info.NodeKey1Bytes[:])
3✔
3233
        copy(node2Key[:], info.NodeKey2Bytes[:])
3✔
3234

3✔
3235
        byteOrder.PutUint64(node1Key[33:], info.ChannelID)
3✔
3236
        byteOrder.PutUint64(node2Key[33:], info.ChannelID)
3✔
3237

3✔
3238
        return node1Key[:], node2Key[:]
3✔
3239
}
3✔
3240

3241
// FetchChannelEdgesByOutpoint attempts to lookup the two directed edges for
3242
// the channel identified by the funding outpoint. If the channel can't be
3243
// found, then ErrEdgeNotFound is returned. A struct which houses the general
3244
// information for the channel itself is returned as well as two structs that
3245
// contain the routing policies for the channel in either direction.
3246
func (c *KVStore) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
3247
        *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3248
        *models.ChannelEdgePolicy, error) {
3✔
3249

3✔
3250
        var (
3✔
3251
                edgeInfo *models.ChannelEdgeInfo
3✔
3252
                policy1  *models.ChannelEdgePolicy
3✔
3253
                policy2  *models.ChannelEdgePolicy
3✔
3254
        )
3✔
3255

3✔
3256
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
3257
                // First, grab the node bucket. This will be used to populate
3✔
3258
                // the Node pointers in each edge read from disk.
3✔
3259
                nodes := tx.ReadBucket(nodeBucket)
3✔
3260
                if nodes == nil {
3✔
3261
                        return ErrGraphNotFound
×
3262
                }
×
3263

3264
                // Next, grab the edge bucket which stores the edges, and also
3265
                // the index itself so we can group the directed edges together
3266
                // logically.
3267
                edges := tx.ReadBucket(edgeBucket)
3✔
3268
                if edges == nil {
3✔
3269
                        return ErrGraphNoEdgesFound
×
3270
                }
×
3271
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
3272
                if edgeIndex == nil {
3✔
3273
                        return ErrGraphNoEdgesFound
×
3274
                }
×
3275

3276
                // If the channel's outpoint doesn't exist within the outpoint
3277
                // index, then the edge does not exist.
3278
                chanIndex := edges.NestedReadBucket(channelPointBucket)
3✔
3279
                if chanIndex == nil {
3✔
3280
                        return ErrGraphNoEdgesFound
×
3281
                }
×
3282
                var b bytes.Buffer
3✔
3283
                if err := WriteOutpoint(&b, op); err != nil {
3✔
3284
                        return err
×
3285
                }
×
3286
                chanID := chanIndex.Get(b.Bytes())
3✔
3287
                if chanID == nil {
6✔
3288
                        return fmt.Errorf("%w: op=%v", ErrEdgeNotFound, op)
3✔
3289
                }
3✔
3290

3291
                // If the channel is found to exists, then we'll first retrieve
3292
                // the general information for the channel.
3293
                edge, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
3294
                if err != nil {
3✔
3295
                        return fmt.Errorf("%w: chanID=%x", err, chanID)
×
3296
                }
×
3297
                edgeInfo = &edge
3✔
3298

3✔
3299
                // Once we have the information about the channels' parameters,
3✔
3300
                // we'll fetch the routing policies for each for the directed
3✔
3301
                // edges.
3✔
3302
                e1, e2, err := fetchChanEdgePolicies(edgeIndex, edges, chanID)
3✔
3303
                if err != nil {
3✔
3304
                        return fmt.Errorf("failed to find policy: %w", err)
×
3305
                }
×
3306

3307
                policy1 = e1
3✔
3308
                policy2 = e2
3✔
3309

3✔
3310
                return nil
3✔
3311
        }, func() {
3✔
3312
                edgeInfo = nil
3✔
3313
                policy1 = nil
3✔
3314
                policy2 = nil
3✔
3315
        })
3✔
3316
        if err != nil {
6✔
3317
                return nil, nil, nil, err
3✔
3318
        }
3✔
3319

3320
        return edgeInfo, policy1, policy2, nil
3✔
3321
}
3322

3323
// FetchChannelEdgesByID attempts to lookup the two directed edges for the
3324
// channel identified by the channel ID. If the channel can't be found, then
3325
// ErrEdgeNotFound is returned. A struct which houses the general information
3326
// for the channel itself is returned as well as two structs that contain the
3327
// routing policies for the channel in either direction.
3328
//
3329
// ErrZombieEdge an be returned if the edge is currently marked as a zombie
3330
// within the database. In this case, the ChannelEdgePolicy's will be nil, and
3331
// the ChannelEdgeInfo will only include the public keys of each node.
3332
func (c *KVStore) FetchChannelEdgesByID(chanID uint64) (
3333
        *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3334
        *models.ChannelEdgePolicy, error) {
3✔
3335

3✔
3336
        var (
3✔
3337
                edgeInfo  *models.ChannelEdgeInfo
3✔
3338
                policy1   *models.ChannelEdgePolicy
3✔
3339
                policy2   *models.ChannelEdgePolicy
3✔
3340
                channelID [8]byte
3✔
3341
        )
3✔
3342

3✔
3343
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
3344
                // First, grab the node bucket. This will be used to populate
3✔
3345
                // the Node pointers in each edge read from disk.
3✔
3346
                nodes := tx.ReadBucket(nodeBucket)
3✔
3347
                if nodes == nil {
3✔
3348
                        return ErrGraphNotFound
×
3349
                }
×
3350

3351
                // Next, grab the edge bucket which stores the edges, and also
3352
                // the index itself so we can group the directed edges together
3353
                // logically.
3354
                edges := tx.ReadBucket(edgeBucket)
3✔
3355
                if edges == nil {
3✔
3356
                        return ErrGraphNoEdgesFound
×
3357
                }
×
3358
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
3359
                if edgeIndex == nil {
3✔
3360
                        return ErrGraphNoEdgesFound
×
3361
                }
×
3362

3363
                byteOrder.PutUint64(channelID[:], chanID)
3✔
3364

3✔
3365
                // Now, attempt to fetch edge.
3✔
3366
                edge, err := fetchChanEdgeInfo(edgeIndex, channelID[:])
3✔
3367

3✔
3368
                // If it doesn't exist, we'll quickly check our zombie index to
3✔
3369
                // see if we've previously marked it as so.
3✔
3370
                if errors.Is(err, ErrEdgeNotFound) {
6✔
3371
                        // If the zombie index doesn't exist, or the edge is not
3✔
3372
                        // marked as a zombie within it, then we'll return the
3✔
3373
                        // original ErrEdgeNotFound error.
3✔
3374
                        zombieIndex := edges.NestedReadBucket(zombieBucket)
3✔
3375
                        if zombieIndex == nil {
3✔
3376
                                return ErrEdgeNotFound
×
3377
                        }
×
3378

3379
                        isZombie, pubKey1, pubKey2 := isZombieEdge(
3✔
3380
                                zombieIndex, chanID,
3✔
3381
                        )
3✔
3382
                        if !isZombie {
6✔
3383
                                return ErrEdgeNotFound
3✔
3384
                        }
3✔
3385

3386
                        // Otherwise, the edge is marked as a zombie, so we'll
3387
                        // populate the edge info with the public keys of each
3388
                        // party as this is the only information we have about
3389
                        // it and return an error signaling so.
3390
                        edgeInfo = &models.ChannelEdgeInfo{
3✔
3391
                                NodeKey1Bytes: pubKey1,
3✔
3392
                                NodeKey2Bytes: pubKey2,
3✔
3393
                        }
3✔
3394

3✔
3395
                        return ErrZombieEdge
3✔
3396
                }
3397

3398
                // Otherwise, we'll just return the error if any.
3399
                if err != nil {
3✔
3400
                        return err
×
3401
                }
×
3402

3403
                edgeInfo = &edge
3✔
3404

3✔
3405
                // Then we'll attempt to fetch the accompanying policies of this
3✔
3406
                // edge.
3✔
3407
                e1, e2, err := fetchChanEdgePolicies(
3✔
3408
                        edgeIndex, edges, channelID[:],
3✔
3409
                )
3✔
3410
                if err != nil {
3✔
3411
                        return err
×
3412
                }
×
3413

3414
                policy1 = e1
3✔
3415
                policy2 = e2
3✔
3416

3✔
3417
                return nil
3✔
3418
        }, func() {
3✔
3419
                edgeInfo = nil
3✔
3420
                policy1 = nil
3✔
3421
                policy2 = nil
3✔
3422
        })
3✔
3423
        if errors.Is(err, ErrZombieEdge) {
6✔
3424
                return edgeInfo, nil, nil, err
3✔
3425
        }
3✔
3426
        if err != nil {
6✔
3427
                return nil, nil, nil, err
3✔
3428
        }
3✔
3429

3430
        return edgeInfo, policy1, policy2, nil
3✔
3431
}
3432

3433
// IsPublicNode is a helper method that determines whether the node with the
3434
// given public key is seen as a public node in the graph from the graph's
3435
// source node's point of view.
3436
func (c *KVStore) IsPublicNode(pubKey [33]byte) (bool, error) {
3✔
3437
        var nodeIsPublic bool
3✔
3438
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
3439
                nodes := tx.ReadBucket(nodeBucket)
3✔
3440
                if nodes == nil {
3✔
3441
                        return ErrGraphNodesNotFound
×
3442
                }
×
3443
                ourPubKey := nodes.Get(sourceKey)
3✔
3444
                if ourPubKey == nil {
3✔
3445
                        return ErrSourceNodeNotSet
×
3446
                }
×
3447
                node, err := fetchLightningNode(nodes, pubKey[:])
3✔
3448
                if err != nil {
3✔
3449
                        return err
×
3450
                }
×
3451

3452
                nodeIsPublic, err = c.isPublic(tx, node.PubKeyBytes, ourPubKey)
3✔
3453

3✔
3454
                return err
3✔
3455
        }, func() {
3✔
3456
                nodeIsPublic = false
3✔
3457
        })
3✔
3458
        if err != nil {
3✔
3459
                return false, err
×
3460
        }
×
3461

3462
        return nodeIsPublic, nil
3✔
3463
}
3464

3465
// genMultiSigP2WSH generates the p2wsh'd multisig script for 2 of 2 pubkeys.
3466
func genMultiSigP2WSH(aPub, bPub []byte) ([]byte, error) {
3✔
3467
        witnessScript, err := input.GenMultiSigScript(aPub, bPub)
3✔
3468
        if err != nil {
3✔
3469
                return nil, err
×
3470
        }
×
3471

3472
        // With the witness script generated, we'll now turn it into a p2wsh
3473
        // script:
3474
        //  * OP_0 <sha256(script)>
3475
        bldr := txscript.NewScriptBuilder(
3✔
3476
                txscript.WithScriptAllocSize(input.P2WSHSize),
3✔
3477
        )
3✔
3478
        bldr.AddOp(txscript.OP_0)
3✔
3479
        scriptHash := sha256.Sum256(witnessScript)
3✔
3480
        bldr.AddData(scriptHash[:])
3✔
3481

3✔
3482
        return bldr.Script()
3✔
3483
}
3484

3485
// EdgePoint couples the outpoint of a channel with the funding script that it
3486
// creates. The FilteredChainView will use this to watch for spends of this
3487
// edge point on chain. We require both of these values as depending on the
3488
// concrete implementation, either the pkScript, or the out point will be used.
3489
type EdgePoint struct {
3490
        // FundingPkScript is the p2wsh multi-sig script of the target channel.
3491
        FundingPkScript []byte
3492

3493
        // OutPoint is the outpoint of the target channel.
3494
        OutPoint wire.OutPoint
3495
}
3496

3497
// String returns a human readable version of the target EdgePoint. We return
3498
// the outpoint directly as it is enough to uniquely identify the edge point.
3499
func (e *EdgePoint) String() string {
×
3500
        return e.OutPoint.String()
×
3501
}
×
3502

3503
// ChannelView returns the verifiable edge information for each active channel
3504
// within the known channel graph. The set of UTXO's (along with their scripts)
3505
// returned are the ones that need to be watched on chain to detect channel
3506
// closes on the resident blockchain.
3507
func (c *KVStore) ChannelView() ([]EdgePoint, error) {
3✔
3508
        var edgePoints []EdgePoint
3✔
3509
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
3510
                // We're going to iterate over the entire channel index, so
3✔
3511
                // we'll need to fetch the edgeBucket to get to the index as
3✔
3512
                // it's a sub-bucket.
3✔
3513
                edges := tx.ReadBucket(edgeBucket)
3✔
3514
                if edges == nil {
3✔
3515
                        return ErrGraphNoEdgesFound
×
3516
                }
×
3517
                chanIndex := edges.NestedReadBucket(channelPointBucket)
3✔
3518
                if chanIndex == nil {
3✔
3519
                        return ErrGraphNoEdgesFound
×
3520
                }
×
3521
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
3522
                if edgeIndex == nil {
3✔
3523
                        return ErrGraphNoEdgesFound
×
3524
                }
×
3525

3526
                // Once we have the proper bucket, we'll range over each key
3527
                // (which is the channel point for the channel) and decode it,
3528
                // accumulating each entry.
3529
                return chanIndex.ForEach(
3✔
3530
                        func(chanPointBytes, chanID []byte) error {
6✔
3531
                                chanPointReader := bytes.NewReader(
3✔
3532
                                        chanPointBytes,
3✔
3533
                                )
3✔
3534

3✔
3535
                                var chanPoint wire.OutPoint
3✔
3536
                                err := ReadOutpoint(chanPointReader, &chanPoint)
3✔
3537
                                if err != nil {
3✔
3538
                                        return err
×
3539
                                }
×
3540

3541
                                edgeInfo, err := fetchChanEdgeInfo(
3✔
3542
                                        edgeIndex, chanID,
3✔
3543
                                )
3✔
3544
                                if err != nil {
3✔
3545
                                        return err
×
3546
                                }
×
3547

3548
                                pkScript, err := genMultiSigP2WSH(
3✔
3549
                                        edgeInfo.BitcoinKey1Bytes[:],
3✔
3550
                                        edgeInfo.BitcoinKey2Bytes[:],
3✔
3551
                                )
3✔
3552
                                if err != nil {
3✔
3553
                                        return err
×
3554
                                }
×
3555

3556
                                edgePoints = append(edgePoints, EdgePoint{
3✔
3557
                                        FundingPkScript: pkScript,
3✔
3558
                                        OutPoint:        chanPoint,
3✔
3559
                                })
3✔
3560

3✔
3561
                                return nil
3✔
3562
                        },
3563
                )
3564
        }, func() {
3✔
3565
                edgePoints = nil
3✔
3566
        }); err != nil {
3✔
3567
                return nil, err
×
3568
        }
×
3569

3570
        return edgePoints, nil
3✔
3571
}
3572

3573
// MarkEdgeZombie attempts to mark a channel identified by its channel ID as a
3574
// zombie. This method is used on an ad-hoc basis, when channels need to be
3575
// marked as zombies outside the normal pruning cycle.
3576
func (c *KVStore) MarkEdgeZombie(chanID uint64,
UNCOV
3577
        pubKey1, pubKey2 [33]byte) error {
×
UNCOV
3578

×
UNCOV
3579
        c.cacheMu.Lock()
×
UNCOV
3580
        defer c.cacheMu.Unlock()
×
UNCOV
3581

×
UNCOV
3582
        err := kvdb.Batch(c.db, func(tx kvdb.RwTx) error {
×
UNCOV
3583
                edges := tx.ReadWriteBucket(edgeBucket)
×
UNCOV
3584
                if edges == nil {
×
3585
                        return ErrGraphNoEdgesFound
×
3586
                }
×
UNCOV
3587
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
×
UNCOV
3588
                if err != nil {
×
UNCOV
3589
                        return fmt.Errorf("unable to create zombie "+
×
UNCOV
3590
                                "bucket: %w", err)
×
UNCOV
3591
                }
×
3592

UNCOV
3593
                return markEdgeZombie(zombieIndex, chanID, pubKey1, pubKey2)
×
3594
        })
UNCOV
3595
        if err != nil {
×
3596
                return err
×
3597
        }
×
3598

UNCOV
3599
        c.rejectCache.remove(chanID)
×
UNCOV
3600
        c.chanCache.remove(chanID)
×
UNCOV
3601

×
UNCOV
3602
        return nil
×
3603
}
3604

3605
// markEdgeZombie marks an edge as a zombie within our zombie index. The public
3606
// keys should represent the node public keys of the two parties involved in the
3607
// edge.
3608
func markEdgeZombie(zombieIndex kvdb.RwBucket, chanID uint64, pubKey1,
3609
        pubKey2 [33]byte) error {
3✔
3610

3✔
3611
        var k [8]byte
3✔
3612
        byteOrder.PutUint64(k[:], chanID)
3✔
3613

3✔
3614
        var v [66]byte
3✔
3615
        copy(v[:33], pubKey1[:])
3✔
3616
        copy(v[33:], pubKey2[:])
3✔
3617

3✔
3618
        return zombieIndex.Put(k[:], v[:])
3✔
3619
}
3✔
3620

3621
// MarkEdgeLive clears an edge from our zombie index, deeming it as live.
UNCOV
3622
func (c *KVStore) MarkEdgeLive(chanID uint64) error {
×
UNCOV
3623
        c.cacheMu.Lock()
×
UNCOV
3624
        defer c.cacheMu.Unlock()
×
UNCOV
3625

×
UNCOV
3626
        return c.markEdgeLiveUnsafe(nil, chanID)
×
UNCOV
3627
}
×
3628

3629
// markEdgeLiveUnsafe clears an edge from the zombie index. This method can be
3630
// called with an existing kvdb.RwTx or the argument can be set to nil in which
3631
// case a new transaction will be created.
3632
//
3633
// NOTE: this method MUST only be called if the cacheMu has already been
3634
// acquired.
UNCOV
3635
func (c *KVStore) markEdgeLiveUnsafe(tx kvdb.RwTx, chanID uint64) error {
×
UNCOV
3636
        dbFn := func(tx kvdb.RwTx) error {
×
UNCOV
3637
                edges := tx.ReadWriteBucket(edgeBucket)
×
UNCOV
3638
                if edges == nil {
×
3639
                        return ErrGraphNoEdgesFound
×
3640
                }
×
UNCOV
3641
                zombieIndex := edges.NestedReadWriteBucket(zombieBucket)
×
UNCOV
3642
                if zombieIndex == nil {
×
3643
                        return nil
×
3644
                }
×
3645

UNCOV
3646
                var k [8]byte
×
UNCOV
3647
                byteOrder.PutUint64(k[:], chanID)
×
UNCOV
3648

×
UNCOV
3649
                if len(zombieIndex.Get(k[:])) == 0 {
×
UNCOV
3650
                        return ErrZombieEdgeNotFound
×
UNCOV
3651
                }
×
3652

UNCOV
3653
                return zombieIndex.Delete(k[:])
×
3654
        }
3655

3656
        // If the transaction is nil, we'll create a new one. Otherwise, we use
3657
        // the existing transaction
UNCOV
3658
        var err error
×
UNCOV
3659
        if tx == nil {
×
UNCOV
3660
                err = kvdb.Update(c.db, dbFn, func() {})
×
UNCOV
3661
        } else {
×
UNCOV
3662
                err = dbFn(tx)
×
UNCOV
3663
        }
×
UNCOV
3664
        if err != nil {
×
UNCOV
3665
                return err
×
UNCOV
3666
        }
×
3667

UNCOV
3668
        c.rejectCache.remove(chanID)
×
UNCOV
3669
        c.chanCache.remove(chanID)
×
UNCOV
3670

×
UNCOV
3671
        return nil
×
3672
}
3673

3674
// IsZombieEdge returns whether the edge is considered zombie. If it is a
3675
// zombie, then the two node public keys corresponding to this edge are also
3676
// returned.
UNCOV
3677
func (c *KVStore) IsZombieEdge(chanID uint64) (bool, [33]byte, [33]byte) {
×
UNCOV
3678
        var (
×
UNCOV
3679
                isZombie         bool
×
UNCOV
3680
                pubKey1, pubKey2 [33]byte
×
UNCOV
3681
        )
×
UNCOV
3682

×
UNCOV
3683
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
×
UNCOV
3684
                edges := tx.ReadBucket(edgeBucket)
×
UNCOV
3685
                if edges == nil {
×
3686
                        return ErrGraphNoEdgesFound
×
3687
                }
×
UNCOV
3688
                zombieIndex := edges.NestedReadBucket(zombieBucket)
×
UNCOV
3689
                if zombieIndex == nil {
×
3690
                        return nil
×
3691
                }
×
3692

UNCOV
3693
                isZombie, pubKey1, pubKey2 = isZombieEdge(zombieIndex, chanID)
×
UNCOV
3694

×
UNCOV
3695
                return nil
×
UNCOV
3696
        }, func() {
×
UNCOV
3697
                isZombie = false
×
UNCOV
3698
                pubKey1 = [33]byte{}
×
UNCOV
3699
                pubKey2 = [33]byte{}
×
UNCOV
3700
        })
×
UNCOV
3701
        if err != nil {
×
3702
                return false, [33]byte{}, [33]byte{}
×
3703
        }
×
3704

UNCOV
3705
        return isZombie, pubKey1, pubKey2
×
3706
}
3707

3708
// isZombieEdge returns whether an entry exists for the given channel in the
3709
// zombie index. If an entry exists, then the two node public keys corresponding
3710
// to this edge are also returned.
3711
func isZombieEdge(zombieIndex kvdb.RBucket,
3712
        chanID uint64) (bool, [33]byte, [33]byte) {
3✔
3713

3✔
3714
        var k [8]byte
3✔
3715
        byteOrder.PutUint64(k[:], chanID)
3✔
3716

3✔
3717
        v := zombieIndex.Get(k[:])
3✔
3718
        if v == nil {
6✔
3719
                return false, [33]byte{}, [33]byte{}
3✔
3720
        }
3✔
3721

3722
        var pubKey1, pubKey2 [33]byte
3✔
3723
        copy(pubKey1[:], v[:33])
3✔
3724
        copy(pubKey2[:], v[33:])
3✔
3725

3✔
3726
        return true, pubKey1, pubKey2
3✔
3727
}
3728

3729
// NumZombies returns the current number of zombie channels in the graph.
UNCOV
3730
func (c *KVStore) NumZombies() (uint64, error) {
×
UNCOV
3731
        var numZombies uint64
×
UNCOV
3732
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
×
UNCOV
3733
                edges := tx.ReadBucket(edgeBucket)
×
UNCOV
3734
                if edges == nil {
×
3735
                        return nil
×
3736
                }
×
UNCOV
3737
                zombieIndex := edges.NestedReadBucket(zombieBucket)
×
UNCOV
3738
                if zombieIndex == nil {
×
3739
                        return nil
×
3740
                }
×
3741

UNCOV
3742
                return zombieIndex.ForEach(func(_, _ []byte) error {
×
UNCOV
3743
                        numZombies++
×
UNCOV
3744
                        return nil
×
UNCOV
3745
                })
×
UNCOV
3746
        }, func() {
×
UNCOV
3747
                numZombies = 0
×
UNCOV
3748
        })
×
UNCOV
3749
        if err != nil {
×
3750
                return 0, err
×
3751
        }
×
3752

UNCOV
3753
        return numZombies, nil
×
3754
}
3755

3756
// PutClosedScid stores a SCID for a closed channel in the database. This is so
3757
// that we can ignore channel announcements that we know to be closed without
3758
// having to validate them and fetch a block.
UNCOV
3759
func (c *KVStore) PutClosedScid(scid lnwire.ShortChannelID) error {
×
UNCOV
3760
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
×
UNCOV
3761
                closedScids, err := tx.CreateTopLevelBucket(closedScidBucket)
×
UNCOV
3762
                if err != nil {
×
3763
                        return err
×
3764
                }
×
3765

UNCOV
3766
                var k [8]byte
×
UNCOV
3767
                byteOrder.PutUint64(k[:], scid.ToUint64())
×
UNCOV
3768

×
UNCOV
3769
                return closedScids.Put(k[:], []byte{})
×
UNCOV
3770
        }, func() {})
×
3771
}
3772

3773
// IsClosedScid checks whether a channel identified by the passed in scid is
3774
// closed. This helps avoid having to perform expensive validation checks.
3775
// TODO: Add an LRU cache to cut down on disc reads.
3776
func (c *KVStore) IsClosedScid(scid lnwire.ShortChannelID) (bool, error) {
3✔
3777
        var isClosed bool
3✔
3778
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
3779
                closedScids := tx.ReadBucket(closedScidBucket)
3✔
3780
                if closedScids == nil {
3✔
3781
                        return ErrClosedScidsNotFound
×
3782
                }
×
3783

3784
                var k [8]byte
3✔
3785
                byteOrder.PutUint64(k[:], scid.ToUint64())
3✔
3786

3✔
3787
                if closedScids.Get(k[:]) != nil {
3✔
UNCOV
3788
                        isClosed = true
×
UNCOV
3789
                        return nil
×
UNCOV
3790
                }
×
3791

3792
                return nil
3✔
3793
        }, func() {
3✔
3794
                isClosed = false
3✔
3795
        })
3✔
3796
        if err != nil {
3✔
3797
                return false, err
×
3798
        }
×
3799

3800
        return isClosed, nil
3✔
3801
}
3802

3803
// GraphSession will provide the call-back with access to a NodeTraverser
3804
// instance which can be used to perform queries against the channel graph.
UNCOV
3805
func (c *KVStore) GraphSession(cb func(graph NodeTraverser) error) error {
×
UNCOV
3806
        return c.db.View(func(tx walletdb.ReadTx) error {
×
UNCOV
3807
                return cb(&nodeTraverserSession{
×
UNCOV
3808
                        db: c,
×
UNCOV
3809
                        tx: tx,
×
UNCOV
3810
                })
×
UNCOV
3811
        }, func() {})
×
3812
}
3813

3814
// nodeTraverserSession implements the NodeTraverser interface but with a
3815
// backing read only transaction for a consistent view of the graph.
3816
type nodeTraverserSession struct {
3817
        tx kvdb.RTx
3818
        db *KVStore
3819
}
3820

3821
// ForEachNodeDirectedChannel calls the callback for every channel of the given
3822
// node.
3823
//
3824
// NOTE: Part of the NodeTraverser interface.
3825
func (c *nodeTraverserSession) ForEachNodeDirectedChannel(nodePub route.Vertex,
UNCOV
3826
        cb func(channel *DirectedChannel) error) error {
×
UNCOV
3827

×
UNCOV
3828
        return c.db.forEachNodeDirectedChannel(c.tx, nodePub, cb)
×
UNCOV
3829
}
×
3830

3831
// FetchNodeFeatures returns the features of the given node. If the node is
3832
// unknown, assume no additional features are supported.
3833
//
3834
// NOTE: Part of the NodeTraverser interface.
3835
func (c *nodeTraverserSession) FetchNodeFeatures(nodePub route.Vertex) (
UNCOV
3836
        *lnwire.FeatureVector, error) {
×
UNCOV
3837

×
UNCOV
3838
        return c.db.fetchNodeFeatures(c.tx, nodePub)
×
UNCOV
3839
}
×
3840

3841
func putLightningNode(nodeBucket, aliasBucket, updateIndex kvdb.RwBucket,
3842
        node *models.LightningNode) error {
3✔
3843

3✔
3844
        var (
3✔
3845
                scratch [16]byte
3✔
3846
                b       bytes.Buffer
3✔
3847
        )
3✔
3848

3✔
3849
        pub, err := node.PubKey()
3✔
3850
        if err != nil {
3✔
3851
                return err
×
3852
        }
×
3853
        nodePub := pub.SerializeCompressed()
3✔
3854

3✔
3855
        // If the node has the update time set, write it, else write 0.
3✔
3856
        updateUnix := uint64(0)
3✔
3857
        if node.LastUpdate.Unix() > 0 {
6✔
3858
                updateUnix = uint64(node.LastUpdate.Unix())
3✔
3859
        }
3✔
3860

3861
        byteOrder.PutUint64(scratch[:8], updateUnix)
3✔
3862
        if _, err := b.Write(scratch[:8]); err != nil {
3✔
3863
                return err
×
3864
        }
×
3865

3866
        if _, err := b.Write(nodePub); err != nil {
3✔
3867
                return err
×
3868
        }
×
3869

3870
        // If we got a node announcement for this node, we will have the rest
3871
        // of the data available. If not we don't have more data to write.
3872
        if !node.HaveNodeAnnouncement {
6✔
3873
                // Write HaveNodeAnnouncement=0.
3✔
3874
                byteOrder.PutUint16(scratch[:2], 0)
3✔
3875
                if _, err := b.Write(scratch[:2]); err != nil {
3✔
3876
                        return err
×
3877
                }
×
3878

3879
                return nodeBucket.Put(nodePub, b.Bytes())
3✔
3880
        }
3881

3882
        // Write HaveNodeAnnouncement=1.
3883
        byteOrder.PutUint16(scratch[:2], 1)
3✔
3884
        if _, err := b.Write(scratch[:2]); err != nil {
3✔
3885
                return err
×
3886
        }
×
3887

3888
        if err := binary.Write(&b, byteOrder, node.Color.R); err != nil {
3✔
3889
                return err
×
3890
        }
×
3891
        if err := binary.Write(&b, byteOrder, node.Color.G); err != nil {
3✔
3892
                return err
×
3893
        }
×
3894
        if err := binary.Write(&b, byteOrder, node.Color.B); err != nil {
3✔
3895
                return err
×
3896
        }
×
3897

3898
        if err := wire.WriteVarString(&b, 0, node.Alias); err != nil {
3✔
3899
                return err
×
3900
        }
×
3901

3902
        if err := node.Features.Encode(&b); err != nil {
3✔
3903
                return err
×
3904
        }
×
3905

3906
        numAddresses := uint16(len(node.Addresses))
3✔
3907
        byteOrder.PutUint16(scratch[:2], numAddresses)
3✔
3908
        if _, err := b.Write(scratch[:2]); err != nil {
3✔
3909
                return err
×
3910
        }
×
3911

3912
        for _, address := range node.Addresses {
6✔
3913
                if err := SerializeAddr(&b, address); err != nil {
3✔
3914
                        return err
×
3915
                }
×
3916
        }
3917

3918
        sigLen := len(node.AuthSigBytes)
3✔
3919
        if sigLen > 80 {
3✔
3920
                return fmt.Errorf("max sig len allowed is 80, had %v",
×
3921
                        sigLen)
×
3922
        }
×
3923

3924
        err = wire.WriteVarBytes(&b, 0, node.AuthSigBytes)
3✔
3925
        if err != nil {
3✔
3926
                return err
×
3927
        }
×
3928

3929
        if len(node.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
3✔
3930
                return ErrTooManyExtraOpaqueBytes(len(node.ExtraOpaqueData))
×
3931
        }
×
3932
        err = wire.WriteVarBytes(&b, 0, node.ExtraOpaqueData)
3✔
3933
        if err != nil {
3✔
3934
                return err
×
3935
        }
×
3936

3937
        if err := aliasBucket.Put(nodePub, []byte(node.Alias)); err != nil {
3✔
3938
                return err
×
3939
        }
×
3940

3941
        // With the alias bucket updated, we'll now update the index that
3942
        // tracks the time series of node updates.
3943
        var indexKey [8 + 33]byte
3✔
3944
        byteOrder.PutUint64(indexKey[:8], updateUnix)
3✔
3945
        copy(indexKey[8:], nodePub)
3✔
3946

3✔
3947
        // If there was already an old index entry for this node, then we'll
3✔
3948
        // delete the old one before we write the new entry.
3✔
3949
        if nodeBytes := nodeBucket.Get(nodePub); nodeBytes != nil {
6✔
3950
                // Extract out the old update time to we can reconstruct the
3✔
3951
                // prior index key to delete it from the index.
3✔
3952
                oldUpdateTime := nodeBytes[:8]
3✔
3953

3✔
3954
                var oldIndexKey [8 + 33]byte
3✔
3955
                copy(oldIndexKey[:8], oldUpdateTime)
3✔
3956
                copy(oldIndexKey[8:], nodePub)
3✔
3957

3✔
3958
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
3✔
3959
                        return err
×
3960
                }
×
3961
        }
3962

3963
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
3✔
3964
                return err
×
3965
        }
×
3966

3967
        return nodeBucket.Put(nodePub, b.Bytes())
3✔
3968
}
3969

3970
func fetchLightningNode(nodeBucket kvdb.RBucket,
3971
        nodePub []byte) (models.LightningNode, error) {
3✔
3972

3✔
3973
        nodeBytes := nodeBucket.Get(nodePub)
3✔
3974
        if nodeBytes == nil {
6✔
3975
                return models.LightningNode{}, ErrGraphNodeNotFound
3✔
3976
        }
3✔
3977

3978
        nodeReader := bytes.NewReader(nodeBytes)
3✔
3979

3✔
3980
        return deserializeLightningNode(nodeReader)
3✔
3981
}
3982

3983
func deserializeLightningNodeCacheable(r io.Reader) (route.Vertex,
3984
        *lnwire.FeatureVector, error) {
3✔
3985

3✔
3986
        var (
3✔
3987
                pubKey      route.Vertex
3✔
3988
                features    = lnwire.EmptyFeatureVector()
3✔
3989
                nodeScratch [8]byte
3✔
3990
        )
3✔
3991

3✔
3992
        // Skip ahead:
3✔
3993
        // - LastUpdate (8 bytes)
3✔
3994
        if _, err := r.Read(nodeScratch[:]); err != nil {
3✔
3995
                return pubKey, nil, err
×
3996
        }
×
3997

3998
        if _, err := io.ReadFull(r, pubKey[:]); err != nil {
3✔
3999
                return pubKey, nil, err
×
4000
        }
×
4001

4002
        // Read the node announcement flag.
4003
        if _, err := r.Read(nodeScratch[:2]); err != nil {
3✔
4004
                return pubKey, nil, err
×
4005
        }
×
4006
        hasNodeAnn := byteOrder.Uint16(nodeScratch[:2])
3✔
4007

3✔
4008
        // The rest of the data is optional, and will only be there if we got a
3✔
4009
        // node announcement for this node.
3✔
4010
        if hasNodeAnn == 0 {
6✔
4011
                return pubKey, features, nil
3✔
4012
        }
3✔
4013

4014
        // We did get a node announcement for this node, so we'll have the rest
4015
        // of the data available.
4016
        var rgb uint8
3✔
4017
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
3✔
4018
                return pubKey, nil, err
×
4019
        }
×
4020
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
3✔
4021
                return pubKey, nil, err
×
4022
        }
×
4023
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
3✔
4024
                return pubKey, nil, err
×
4025
        }
×
4026

4027
        if _, err := wire.ReadVarString(r, 0); err != nil {
3✔
4028
                return pubKey, nil, err
×
4029
        }
×
4030

4031
        if err := features.Decode(r); err != nil {
3✔
4032
                return pubKey, nil, err
×
4033
        }
×
4034

4035
        return pubKey, features, nil
3✔
4036
}
4037

4038
func deserializeLightningNode(r io.Reader) (models.LightningNode, error) {
3✔
4039
        var (
3✔
4040
                node    models.LightningNode
3✔
4041
                scratch [8]byte
3✔
4042
                err     error
3✔
4043
        )
3✔
4044

3✔
4045
        // Always populate a feature vector, even if we don't have a node
3✔
4046
        // announcement and short circuit below.
3✔
4047
        node.Features = lnwire.EmptyFeatureVector()
3✔
4048

3✔
4049
        if _, err := r.Read(scratch[:]); err != nil {
3✔
4050
                return models.LightningNode{}, err
×
4051
        }
×
4052

4053
        unix := int64(byteOrder.Uint64(scratch[:]))
3✔
4054
        node.LastUpdate = time.Unix(unix, 0)
3✔
4055

3✔
4056
        if _, err := io.ReadFull(r, node.PubKeyBytes[:]); err != nil {
3✔
4057
                return models.LightningNode{}, err
×
4058
        }
×
4059

4060
        if _, err := r.Read(scratch[:2]); err != nil {
3✔
4061
                return models.LightningNode{}, err
×
4062
        }
×
4063

4064
        hasNodeAnn := byteOrder.Uint16(scratch[:2])
3✔
4065
        if hasNodeAnn == 1 {
6✔
4066
                node.HaveNodeAnnouncement = true
3✔
4067
        } else {
6✔
4068
                node.HaveNodeAnnouncement = false
3✔
4069
        }
3✔
4070

4071
        // The rest of the data is optional, and will only be there if we got a
4072
        // node announcement for this node.
4073
        if !node.HaveNodeAnnouncement {
6✔
4074
                return node, nil
3✔
4075
        }
3✔
4076

4077
        // We did get a node announcement for this node, so we'll have the rest
4078
        // of the data available.
4079
        if err := binary.Read(r, byteOrder, &node.Color.R); err != nil {
3✔
4080
                return models.LightningNode{}, err
×
4081
        }
×
4082
        if err := binary.Read(r, byteOrder, &node.Color.G); err != nil {
3✔
4083
                return models.LightningNode{}, err
×
4084
        }
×
4085
        if err := binary.Read(r, byteOrder, &node.Color.B); err != nil {
3✔
4086
                return models.LightningNode{}, err
×
4087
        }
×
4088

4089
        node.Alias, err = wire.ReadVarString(r, 0)
3✔
4090
        if err != nil {
3✔
4091
                return models.LightningNode{}, err
×
4092
        }
×
4093

4094
        err = node.Features.Decode(r)
3✔
4095
        if err != nil {
3✔
4096
                return models.LightningNode{}, err
×
4097
        }
×
4098

4099
        if _, err := r.Read(scratch[:2]); err != nil {
3✔
4100
                return models.LightningNode{}, err
×
4101
        }
×
4102
        numAddresses := int(byteOrder.Uint16(scratch[:2]))
3✔
4103

3✔
4104
        var addresses []net.Addr
3✔
4105
        for i := 0; i < numAddresses; i++ {
6✔
4106
                address, err := DeserializeAddr(r)
3✔
4107
                if err != nil {
3✔
4108
                        return models.LightningNode{}, err
×
4109
                }
×
4110
                addresses = append(addresses, address)
3✔
4111
        }
4112
        node.Addresses = addresses
3✔
4113

3✔
4114
        node.AuthSigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
3✔
4115
        if err != nil {
3✔
4116
                return models.LightningNode{}, err
×
4117
        }
×
4118

4119
        // We'll try and see if there are any opaque bytes left, if not, then
4120
        // we'll ignore the EOF error and return the node as is.
4121
        node.ExtraOpaqueData, err = wire.ReadVarBytes(
3✔
4122
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
3✔
4123
        )
3✔
4124
        switch {
3✔
4125
        case errors.Is(err, io.ErrUnexpectedEOF):
×
4126
        case errors.Is(err, io.EOF):
×
4127
        case err != nil:
×
4128
                return models.LightningNode{}, err
×
4129
        }
4130

4131
        return node, nil
3✔
4132
}
4133

4134
func putChanEdgeInfo(edgeIndex kvdb.RwBucket,
4135
        edgeInfo *models.ChannelEdgeInfo, chanID [8]byte) error {
3✔
4136

3✔
4137
        var b bytes.Buffer
3✔
4138

3✔
4139
        if _, err := b.Write(edgeInfo.NodeKey1Bytes[:]); err != nil {
3✔
4140
                return err
×
4141
        }
×
4142
        if _, err := b.Write(edgeInfo.NodeKey2Bytes[:]); err != nil {
3✔
4143
                return err
×
4144
        }
×
4145
        if _, err := b.Write(edgeInfo.BitcoinKey1Bytes[:]); err != nil {
3✔
4146
                return err
×
4147
        }
×
4148
        if _, err := b.Write(edgeInfo.BitcoinKey2Bytes[:]); err != nil {
3✔
4149
                return err
×
4150
        }
×
4151

4152
        if err := wire.WriteVarBytes(&b, 0, edgeInfo.Features); err != nil {
3✔
4153
                return err
×
4154
        }
×
4155

4156
        authProof := edgeInfo.AuthProof
3✔
4157
        var nodeSig1, nodeSig2, bitcoinSig1, bitcoinSig2 []byte
3✔
4158
        if authProof != nil {
6✔
4159
                nodeSig1 = authProof.NodeSig1Bytes
3✔
4160
                nodeSig2 = authProof.NodeSig2Bytes
3✔
4161
                bitcoinSig1 = authProof.BitcoinSig1Bytes
3✔
4162
                bitcoinSig2 = authProof.BitcoinSig2Bytes
3✔
4163
        }
3✔
4164

4165
        if err := wire.WriteVarBytes(&b, 0, nodeSig1); err != nil {
3✔
4166
                return err
×
4167
        }
×
4168
        if err := wire.WriteVarBytes(&b, 0, nodeSig2); err != nil {
3✔
4169
                return err
×
4170
        }
×
4171
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig1); err != nil {
3✔
4172
                return err
×
4173
        }
×
4174
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig2); err != nil {
3✔
4175
                return err
×
4176
        }
×
4177

4178
        if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
3✔
4179
                return err
×
4180
        }
×
4181
        err := binary.Write(&b, byteOrder, uint64(edgeInfo.Capacity))
3✔
4182
        if err != nil {
3✔
4183
                return err
×
4184
        }
×
4185
        if _, err := b.Write(chanID[:]); err != nil {
3✔
4186
                return err
×
4187
        }
×
4188
        if _, err := b.Write(edgeInfo.ChainHash[:]); err != nil {
3✔
4189
                return err
×
4190
        }
×
4191

4192
        if len(edgeInfo.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
3✔
4193
                return ErrTooManyExtraOpaqueBytes(len(edgeInfo.ExtraOpaqueData))
×
4194
        }
×
4195
        err = wire.WriteVarBytes(&b, 0, edgeInfo.ExtraOpaqueData)
3✔
4196
        if err != nil {
3✔
4197
                return err
×
4198
        }
×
4199

4200
        return edgeIndex.Put(chanID[:], b.Bytes())
3✔
4201
}
4202

4203
func fetchChanEdgeInfo(edgeIndex kvdb.RBucket,
4204
        chanID []byte) (models.ChannelEdgeInfo, error) {
3✔
4205

3✔
4206
        edgeInfoBytes := edgeIndex.Get(chanID)
3✔
4207
        if edgeInfoBytes == nil {
6✔
4208
                return models.ChannelEdgeInfo{}, ErrEdgeNotFound
3✔
4209
        }
3✔
4210

4211
        edgeInfoReader := bytes.NewReader(edgeInfoBytes)
3✔
4212

3✔
4213
        return deserializeChanEdgeInfo(edgeInfoReader)
3✔
4214
}
4215

4216
func deserializeChanEdgeInfo(r io.Reader) (models.ChannelEdgeInfo, error) {
3✔
4217
        var (
3✔
4218
                err      error
3✔
4219
                edgeInfo models.ChannelEdgeInfo
3✔
4220
        )
3✔
4221

3✔
4222
        if _, err := io.ReadFull(r, edgeInfo.NodeKey1Bytes[:]); err != nil {
3✔
4223
                return models.ChannelEdgeInfo{}, err
×
4224
        }
×
4225
        if _, err := io.ReadFull(r, edgeInfo.NodeKey2Bytes[:]); err != nil {
3✔
4226
                return models.ChannelEdgeInfo{}, err
×
4227
        }
×
4228
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey1Bytes[:]); err != nil {
3✔
4229
                return models.ChannelEdgeInfo{}, err
×
4230
        }
×
4231
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey2Bytes[:]); err != nil {
3✔
4232
                return models.ChannelEdgeInfo{}, err
×
4233
        }
×
4234

4235
        edgeInfo.Features, err = wire.ReadVarBytes(r, 0, 900, "features")
3✔
4236
        if err != nil {
3✔
4237
                return models.ChannelEdgeInfo{}, err
×
4238
        }
×
4239

4240
        proof := &models.ChannelAuthProof{}
3✔
4241

3✔
4242
        proof.NodeSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4243
        if err != nil {
3✔
4244
                return models.ChannelEdgeInfo{}, err
×
4245
        }
×
4246
        proof.NodeSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4247
        if err != nil {
3✔
4248
                return models.ChannelEdgeInfo{}, err
×
4249
        }
×
4250
        proof.BitcoinSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4251
        if err != nil {
3✔
4252
                return models.ChannelEdgeInfo{}, err
×
4253
        }
×
4254
        proof.BitcoinSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4255
        if err != nil {
3✔
4256
                return models.ChannelEdgeInfo{}, err
×
4257
        }
×
4258

4259
        if !proof.IsEmpty() {
6✔
4260
                edgeInfo.AuthProof = proof
3✔
4261
        }
3✔
4262

4263
        edgeInfo.ChannelPoint = wire.OutPoint{}
3✔
4264
        if err := ReadOutpoint(r, &edgeInfo.ChannelPoint); err != nil {
3✔
4265
                return models.ChannelEdgeInfo{}, err
×
4266
        }
×
4267
        if err := binary.Read(r, byteOrder, &edgeInfo.Capacity); err != nil {
3✔
4268
                return models.ChannelEdgeInfo{}, err
×
4269
        }
×
4270
        if err := binary.Read(r, byteOrder, &edgeInfo.ChannelID); err != nil {
3✔
4271
                return models.ChannelEdgeInfo{}, err
×
4272
        }
×
4273

4274
        if _, err := io.ReadFull(r, edgeInfo.ChainHash[:]); err != nil {
3✔
4275
                return models.ChannelEdgeInfo{}, err
×
4276
        }
×
4277

4278
        // We'll try and see if there are any opaque bytes left, if not, then
4279
        // we'll ignore the EOF error and return the edge as is.
4280
        edgeInfo.ExtraOpaqueData, err = wire.ReadVarBytes(
3✔
4281
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
3✔
4282
        )
3✔
4283
        switch {
3✔
4284
        case errors.Is(err, io.ErrUnexpectedEOF):
×
4285
        case errors.Is(err, io.EOF):
×
4286
        case err != nil:
×
4287
                return models.ChannelEdgeInfo{}, err
×
4288
        }
4289

4290
        return edgeInfo, nil
3✔
4291
}
4292

4293
func putChanEdgePolicy(edges kvdb.RwBucket, edge *models.ChannelEdgePolicy,
4294
        from, to []byte) error {
3✔
4295

3✔
4296
        var edgeKey [33 + 8]byte
3✔
4297
        copy(edgeKey[:], from)
3✔
4298
        byteOrder.PutUint64(edgeKey[33:], edge.ChannelID)
3✔
4299

3✔
4300
        var b bytes.Buffer
3✔
4301
        if err := serializeChanEdgePolicy(&b, edge, to); err != nil {
3✔
4302
                return err
×
4303
        }
×
4304

4305
        // Before we write out the new edge, we'll create a new entry in the
4306
        // update index in order to keep it fresh.
4307
        updateUnix := uint64(edge.LastUpdate.Unix())
3✔
4308
        var indexKey [8 + 8]byte
3✔
4309
        byteOrder.PutUint64(indexKey[:8], updateUnix)
3✔
4310
        byteOrder.PutUint64(indexKey[8:], edge.ChannelID)
3✔
4311

3✔
4312
        updateIndex, err := edges.CreateBucketIfNotExists(edgeUpdateIndexBucket)
3✔
4313
        if err != nil {
3✔
4314
                return err
×
4315
        }
×
4316

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

3✔
4324
                // In order to delete the old entry, we'll need to obtain the
3✔
4325
                // *prior* update time in order to delete it. To do this, we'll
3✔
4326
                // need to deserialize the existing policy within the database
3✔
4327
                // (now outdated by the new one), and delete its corresponding
3✔
4328
                // entry within the update index. We'll ignore any
3✔
4329
                // ErrEdgePolicyOptionalFieldNotFound error, as we only need
3✔
4330
                // the channel ID and update time to delete the entry.
3✔
4331
                // TODO(halseth): get rid of these invalid policies in a
3✔
4332
                // migration.
3✔
4333
                oldEdgePolicy, err := deserializeChanEdgePolicy(
3✔
4334
                        bytes.NewReader(edgeBytes),
3✔
4335
                )
3✔
4336
                if err != nil &&
3✔
4337
                        !errors.Is(err, ErrEdgePolicyOptionalFieldNotFound) {
3✔
4338

×
4339
                        return err
×
4340
                }
×
4341

4342
                oldUpdateTime := uint64(oldEdgePolicy.LastUpdate.Unix())
3✔
4343

3✔
4344
                var oldIndexKey [8 + 8]byte
3✔
4345
                byteOrder.PutUint64(oldIndexKey[:8], oldUpdateTime)
3✔
4346
                byteOrder.PutUint64(oldIndexKey[8:], edge.ChannelID)
3✔
4347

3✔
4348
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
3✔
4349
                        return err
×
4350
                }
×
4351
        }
4352

4353
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
3✔
4354
                return err
×
4355
        }
×
4356

4357
        err = updateEdgePolicyDisabledIndex(
3✔
4358
                edges, edge.ChannelID,
3✔
4359
                edge.ChannelFlags&lnwire.ChanUpdateDirection > 0,
3✔
4360
                edge.IsDisabled(),
3✔
4361
        )
3✔
4362
        if err != nil {
3✔
4363
                return err
×
4364
        }
×
4365

4366
        return edges.Put(edgeKey[:], b.Bytes())
3✔
4367
}
4368

4369
// updateEdgePolicyDisabledIndex is used to update the disabledEdgePolicyIndex
4370
// bucket by either add a new disabled ChannelEdgePolicy or remove an existing
4371
// one.
4372
// The direction represents the direction of the edge and disabled is used for
4373
// deciding whether to remove or add an entry to the bucket.
4374
// In general a channel is disabled if two entries for the same chanID exist
4375
// in this bucket.
4376
// Maintaining the bucket this way allows a fast retrieval of disabled
4377
// channels, for example when prune is needed.
4378
func updateEdgePolicyDisabledIndex(edges kvdb.RwBucket, chanID uint64,
4379
        direction bool, disabled bool) error {
3✔
4380

3✔
4381
        var disabledEdgeKey [8 + 1]byte
3✔
4382
        byteOrder.PutUint64(disabledEdgeKey[0:], chanID)
3✔
4383
        if direction {
6✔
4384
                disabledEdgeKey[8] = 1
3✔
4385
        }
3✔
4386

4387
        disabledEdgePolicyIndex, err := edges.CreateBucketIfNotExists(
3✔
4388
                disabledEdgePolicyBucket,
3✔
4389
        )
3✔
4390
        if err != nil {
3✔
4391
                return err
×
4392
        }
×
4393

4394
        if disabled {
6✔
4395
                return disabledEdgePolicyIndex.Put(disabledEdgeKey[:], []byte{})
3✔
4396
        }
3✔
4397

4398
        return disabledEdgePolicyIndex.Delete(disabledEdgeKey[:])
3✔
4399
}
4400

4401
// putChanEdgePolicyUnknown marks the edge policy as unknown
4402
// in the edges bucket.
4403
func putChanEdgePolicyUnknown(edges kvdb.RwBucket, channelID uint64,
4404
        from []byte) error {
3✔
4405

3✔
4406
        var edgeKey [33 + 8]byte
3✔
4407
        copy(edgeKey[:], from)
3✔
4408
        byteOrder.PutUint64(edgeKey[33:], channelID)
3✔
4409

3✔
4410
        if edges.Get(edgeKey[:]) != nil {
3✔
4411
                return fmt.Errorf("cannot write unknown policy for channel %v "+
×
4412
                        " when there is already a policy present", channelID)
×
4413
        }
×
4414

4415
        return edges.Put(edgeKey[:], unknownPolicy)
3✔
4416
}
4417

4418
func fetchChanEdgePolicy(edges kvdb.RBucket, chanID []byte,
4419
        nodePub []byte) (*models.ChannelEdgePolicy, error) {
3✔
4420

3✔
4421
        var edgeKey [33 + 8]byte
3✔
4422
        copy(edgeKey[:], nodePub)
3✔
4423
        copy(edgeKey[33:], chanID)
3✔
4424

3✔
4425
        edgeBytes := edges.Get(edgeKey[:])
3✔
4426
        if edgeBytes == nil {
3✔
4427
                return nil, ErrEdgeNotFound
×
4428
        }
×
4429

4430
        // No need to deserialize unknown policy.
4431
        if bytes.Equal(edgeBytes, unknownPolicy) {
6✔
4432
                return nil, nil
3✔
4433
        }
3✔
4434

4435
        edgeReader := bytes.NewReader(edgeBytes)
3✔
4436

3✔
4437
        ep, err := deserializeChanEdgePolicy(edgeReader)
3✔
4438
        switch {
3✔
4439
        // If the db policy was missing an expected optional field, we return
4440
        // nil as if the policy was unknown.
UNCOV
4441
        case errors.Is(err, ErrEdgePolicyOptionalFieldNotFound):
×
UNCOV
4442
                return nil, nil
×
4443

4444
        case err != nil:
×
4445
                return nil, err
×
4446
        }
4447

4448
        return ep, nil
3✔
4449
}
4450

4451
func fetchChanEdgePolicies(edgeIndex kvdb.RBucket, edges kvdb.RBucket,
4452
        chanID []byte) (*models.ChannelEdgePolicy, *models.ChannelEdgePolicy,
4453
        error) {
3✔
4454

3✔
4455
        edgeInfo := edgeIndex.Get(chanID)
3✔
4456
        if edgeInfo == nil {
3✔
4457
                return nil, nil, fmt.Errorf("%w: chanID=%x", ErrEdgeNotFound,
×
4458
                        chanID)
×
4459
        }
×
4460

4461
        // The first node is contained within the first half of the edge
4462
        // information. We only propagate the error here and below if it's
4463
        // something other than edge non-existence.
4464
        node1Pub := edgeInfo[:33]
3✔
4465
        edge1, err := fetchChanEdgePolicy(edges, chanID, node1Pub)
3✔
4466
        if err != nil {
3✔
4467
                return nil, nil, fmt.Errorf("%w: node1Pub=%x", ErrEdgeNotFound,
×
4468
                        node1Pub)
×
4469
        }
×
4470

4471
        // Similarly, the second node is contained within the latter
4472
        // half of the edge information.
4473
        node2Pub := edgeInfo[33:66]
3✔
4474
        edge2, err := fetchChanEdgePolicy(edges, chanID, node2Pub)
3✔
4475
        if err != nil {
3✔
4476
                return nil, nil, fmt.Errorf("%w: node2Pub=%x", ErrEdgeNotFound,
×
4477
                        node2Pub)
×
4478
        }
×
4479

4480
        return edge1, edge2, nil
3✔
4481
}
4482

4483
func serializeChanEdgePolicy(w io.Writer, edge *models.ChannelEdgePolicy,
4484
        to []byte) error {
3✔
4485

3✔
4486
        err := wire.WriteVarBytes(w, 0, edge.SigBytes)
3✔
4487
        if err != nil {
3✔
4488
                return err
×
4489
        }
×
4490

4491
        if err := binary.Write(w, byteOrder, edge.ChannelID); err != nil {
3✔
4492
                return err
×
4493
        }
×
4494

4495
        var scratch [8]byte
3✔
4496
        updateUnix := uint64(edge.LastUpdate.Unix())
3✔
4497
        byteOrder.PutUint64(scratch[:], updateUnix)
3✔
4498
        if _, err := w.Write(scratch[:]); err != nil {
3✔
4499
                return err
×
4500
        }
×
4501

4502
        if err := binary.Write(w, byteOrder, edge.MessageFlags); err != nil {
3✔
4503
                return err
×
4504
        }
×
4505
        if err := binary.Write(w, byteOrder, edge.ChannelFlags); err != nil {
3✔
4506
                return err
×
4507
        }
×
4508
        if err := binary.Write(w, byteOrder, edge.TimeLockDelta); err != nil {
3✔
4509
                return err
×
4510
        }
×
4511
        if err := binary.Write(w, byteOrder, uint64(edge.MinHTLC)); err != nil {
3✔
4512
                return err
×
4513
        }
×
4514
        err = binary.Write(w, byteOrder, uint64(edge.FeeBaseMSat))
3✔
4515
        if err != nil {
3✔
4516
                return err
×
4517
        }
×
4518
        err = binary.Write(
3✔
4519
                w, byteOrder, uint64(edge.FeeProportionalMillionths),
3✔
4520
        )
3✔
4521
        if err != nil {
3✔
4522
                return err
×
4523
        }
×
4524

4525
        if _, err := w.Write(to); err != nil {
3✔
4526
                return err
×
4527
        }
×
4528

4529
        // If the max_htlc field is present, we write it. To be compatible with
4530
        // older versions that wasn't aware of this field, we write it as part
4531
        // of the opaque data.
4532
        // TODO(halseth): clean up when moving to TLV.
4533
        var opaqueBuf bytes.Buffer
3✔
4534
        if edge.MessageFlags.HasMaxHtlc() {
6✔
4535
                err := binary.Write(&opaqueBuf, byteOrder, uint64(edge.MaxHTLC))
3✔
4536
                if err != nil {
3✔
4537
                        return err
×
4538
                }
×
4539
        }
4540

4541
        if len(edge.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
3✔
4542
                return ErrTooManyExtraOpaqueBytes(len(edge.ExtraOpaqueData))
×
4543
        }
×
4544
        if _, err := opaqueBuf.Write(edge.ExtraOpaqueData); err != nil {
3✔
4545
                return err
×
4546
        }
×
4547

4548
        if err := wire.WriteVarBytes(w, 0, opaqueBuf.Bytes()); err != nil {
3✔
4549
                return err
×
4550
        }
×
4551

4552
        return nil
3✔
4553
}
4554

4555
func deserializeChanEdgePolicy(r io.Reader) (*models.ChannelEdgePolicy, error) {
3✔
4556
        // Deserialize the policy. Note that in case an optional field is not
3✔
4557
        // found, both an error and a populated policy object are returned.
3✔
4558
        edge, deserializeErr := deserializeChanEdgePolicyRaw(r)
3✔
4559
        if deserializeErr != nil &&
3✔
4560
                !errors.Is(deserializeErr, ErrEdgePolicyOptionalFieldNotFound) {
3✔
4561

×
4562
                return nil, deserializeErr
×
4563
        }
×
4564

4565
        return edge, deserializeErr
3✔
4566
}
4567

4568
func deserializeChanEdgePolicyRaw(r io.Reader) (*models.ChannelEdgePolicy,
4569
        error) {
3✔
4570

3✔
4571
        edge := &models.ChannelEdgePolicy{}
3✔
4572

3✔
4573
        var err error
3✔
4574
        edge.SigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
3✔
4575
        if err != nil {
3✔
4576
                return nil, err
×
4577
        }
×
4578

4579
        if err := binary.Read(r, byteOrder, &edge.ChannelID); err != nil {
3✔
4580
                return nil, err
×
4581
        }
×
4582

4583
        var scratch [8]byte
3✔
4584
        if _, err := r.Read(scratch[:]); err != nil {
3✔
4585
                return nil, err
×
4586
        }
×
4587
        unix := int64(byteOrder.Uint64(scratch[:]))
3✔
4588
        edge.LastUpdate = time.Unix(unix, 0)
3✔
4589

3✔
4590
        if err := binary.Read(r, byteOrder, &edge.MessageFlags); err != nil {
3✔
4591
                return nil, err
×
4592
        }
×
4593
        if err := binary.Read(r, byteOrder, &edge.ChannelFlags); err != nil {
3✔
4594
                return nil, err
×
4595
        }
×
4596
        if err := binary.Read(r, byteOrder, &edge.TimeLockDelta); err != nil {
3✔
4597
                return nil, err
×
4598
        }
×
4599

4600
        var n uint64
3✔
4601
        if err := binary.Read(r, byteOrder, &n); err != nil {
3✔
4602
                return nil, err
×
4603
        }
×
4604
        edge.MinHTLC = lnwire.MilliSatoshi(n)
3✔
4605

3✔
4606
        if err := binary.Read(r, byteOrder, &n); err != nil {
3✔
4607
                return nil, err
×
4608
        }
×
4609
        edge.FeeBaseMSat = lnwire.MilliSatoshi(n)
3✔
4610

3✔
4611
        if err := binary.Read(r, byteOrder, &n); err != nil {
3✔
4612
                return nil, err
×
4613
        }
×
4614
        edge.FeeProportionalMillionths = lnwire.MilliSatoshi(n)
3✔
4615

3✔
4616
        if _, err := r.Read(edge.ToNode[:]); err != nil {
3✔
4617
                return nil, err
×
4618
        }
×
4619

4620
        // We'll try and see if there are any opaque bytes left, if not, then
4621
        // we'll ignore the EOF error and return the edge as is.
4622
        edge.ExtraOpaqueData, err = wire.ReadVarBytes(
3✔
4623
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
3✔
4624
        )
3✔
4625
        switch {
3✔
4626
        case errors.Is(err, io.ErrUnexpectedEOF):
×
UNCOV
4627
        case errors.Is(err, io.EOF):
×
4628
        case err != nil:
×
4629
                return nil, err
×
4630
        }
4631

4632
        // See if optional fields are present.
4633
        if edge.MessageFlags.HasMaxHtlc() {
6✔
4634
                // The max_htlc field should be at the beginning of the opaque
3✔
4635
                // bytes.
3✔
4636
                opq := edge.ExtraOpaqueData
3✔
4637

3✔
4638
                // If the max_htlc field is not present, it might be old data
3✔
4639
                // stored before this field was validated. We'll return the
3✔
4640
                // edge along with an error.
3✔
4641
                if len(opq) < 8 {
3✔
UNCOV
4642
                        return edge, ErrEdgePolicyOptionalFieldNotFound
×
UNCOV
4643
                }
×
4644

4645
                maxHtlc := byteOrder.Uint64(opq[:8])
3✔
4646
                edge.MaxHTLC = lnwire.MilliSatoshi(maxHtlc)
3✔
4647

3✔
4648
                // Exclude the parsed field from the rest of the opaque data.
3✔
4649
                edge.ExtraOpaqueData = opq[8:]
3✔
4650
        }
4651

4652
        return edge, nil
3✔
4653
}
4654

4655
// chanGraphNodeTx is an implementation of the NodeRTx interface backed by the
4656
// KVStore and a kvdb.RTx.
4657
type chanGraphNodeTx struct {
4658
        tx   kvdb.RTx
4659
        db   *KVStore
4660
        node *models.LightningNode
4661
}
4662

4663
// A compile-time constraint to ensure chanGraphNodeTx implements the NodeRTx
4664
// interface.
4665
var _ NodeRTx = (*chanGraphNodeTx)(nil)
4666

4667
func newChanGraphNodeTx(tx kvdb.RTx, db *KVStore,
4668
        node *models.LightningNode) *chanGraphNodeTx {
3✔
4669

3✔
4670
        return &chanGraphNodeTx{
3✔
4671
                tx:   tx,
3✔
4672
                db:   db,
3✔
4673
                node: node,
3✔
4674
        }
3✔
4675
}
3✔
4676

4677
// Node returns the raw information of the node.
4678
//
4679
// NOTE: This is a part of the NodeRTx interface.
4680
func (c *chanGraphNodeTx) Node() *models.LightningNode {
3✔
4681
        return c.node
3✔
4682
}
3✔
4683

4684
// FetchNode fetches the node with the given pub key under the same transaction
4685
// used to fetch the current node. The returned node is also a NodeRTx and any
4686
// operations on that NodeRTx will also be done under the same transaction.
4687
//
4688
// NOTE: This is a part of the NodeRTx interface.
UNCOV
4689
func (c *chanGraphNodeTx) FetchNode(nodePub route.Vertex) (NodeRTx, error) {
×
UNCOV
4690
        node, err := c.db.FetchLightningNodeTx(c.tx, nodePub)
×
UNCOV
4691
        if err != nil {
×
4692
                return nil, err
×
4693
        }
×
4694

UNCOV
4695
        return newChanGraphNodeTx(c.tx, c.db, node), nil
×
4696
}
4697

4698
// ForEachChannel can be used to iterate over the node's channels under
4699
// the same transaction used to fetch the node.
4700
//
4701
// NOTE: This is a part of the NodeRTx interface.
4702
func (c *chanGraphNodeTx) ForEachChannel(f func(*models.ChannelEdgeInfo,
UNCOV
4703
        *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
×
UNCOV
4704

×
UNCOV
4705
        return c.db.ForEachNodeChannelTx(c.tx, c.node.PubKeyBytes,
×
UNCOV
4706
                func(_ kvdb.RTx, info *models.ChannelEdgeInfo, policy1,
×
UNCOV
4707
                        policy2 *models.ChannelEdgePolicy) error {
×
UNCOV
4708

×
UNCOV
4709
                        return f(info, policy1, policy2)
×
UNCOV
4710
                },
×
4711
        )
4712
}
4713

4714
// MakeTestGraph creates a new instance of the KVStore for testing
4715
// purposes.
4716
func MakeTestGraph(t testing.TB, modifiers ...KVStoreOptionModifier) (
UNCOV
4717
        *ChannelGraph, error) {
×
UNCOV
4718

×
UNCOV
4719
        opts := DefaultOptions()
×
UNCOV
4720
        for _, modifier := range modifiers {
×
4721
                modifier(opts)
×
4722
        }
×
4723

4724
        // Next, create KVStore for the first time.
UNCOV
4725
        backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
×
UNCOV
4726
        if err != nil {
×
4727
                backendCleanup()
×
4728

×
4729
                return nil, err
×
4730
        }
×
4731

UNCOV
4732
        graph, err := NewChannelGraph(&Config{
×
UNCOV
4733
                KVDB:        backend,
×
UNCOV
4734
                KVStoreOpts: modifiers,
×
UNCOV
4735
        })
×
UNCOV
4736
        if err != nil {
×
4737
                backendCleanup()
×
4738

×
4739
                return nil, err
×
4740
        }
×
4741

UNCOV
4742
        t.Cleanup(func() {
×
UNCOV
4743
                _ = backend.Close()
×
UNCOV
4744
                backendCleanup()
×
UNCOV
4745
        })
×
4746

UNCOV
4747
        return graph, nil
×
4748
}
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