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

lightningnetwork / lnd / 13414831183

19 Feb 2025 02:22PM UTC coverage: 58.821% (+0.03%) from 58.794%
13414831183

Pull #9529

github

ellemouton
graph/db: move Topology client management to ChannelGraph
Pull Request #9529: [Concept ACK 🙏 ] graph: move graph cache out of CRUD layer & move topology change subscription

2730 of 3482 new or added lines in 9 files covered. (78.4%)

43 existing lines in 11 files now uncovered.

136278 of 231682 relevant lines covered (58.82%)

19276.58 hits per line

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

77.19
/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
        "github.com/stretchr/testify/require"
30
)
31

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

188
        // cacheMu guards all caches (rejectCache and chanCache). If
189
        // this mutex will be acquired at the same time as the DB mutex then
190
        // the cacheMu MUST be acquired first to prevent deadlock.
191
        cacheMu     sync.RWMutex
192
        rejectCache *rejectCache
193
        chanCache   *channelCache
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) {
177✔
203

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

209
        if !opts.NoMigration {
354✔
210
                if err := initKVStore(db); err != nil {
177✔
NEW
211
                        return nil, err
×
NEW
212
                }
×
213
        }
214

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

177✔
227
        return g, nil
177✔
228
}
229

230
// channelMapKey is the key structure used for storing channel edge policies.
231
type channelMapKey struct {
232
        nodeKey route.Vertex
233
        chanID  [8]byte
234
}
235

236
// getChannelMap loads all channel edge policies from the database and stores
237
// them in a map.
238
func (c *KVStore) getChannelMap(edges kvdb.RBucket) (
239
        map[channelMapKey]*models.ChannelEdgePolicy, error) {
148✔
240

148✔
241
        // Create a map to store all channel edge policies.
148✔
242
        channelMap := make(map[channelMapKey]*models.ChannelEdgePolicy)
148✔
243

148✔
244
        err := kvdb.ForAll(edges, func(k, edgeBytes []byte) error {
1,726✔
245
                // Skip embedded buckets.
1,578✔
246
                if bytes.Equal(k, edgeIndexBucket) ||
1,578✔
247
                        bytes.Equal(k, edgeUpdateIndexBucket) ||
1,578✔
248
                        bytes.Equal(k, zombieBucket) ||
1,578✔
249
                        bytes.Equal(k, disabledEdgePolicyBucket) ||
1,578✔
250
                        bytes.Equal(k, channelPointBucket) {
2,166✔
251

588✔
252
                        return nil
588✔
253
                }
588✔
254

255
                // Validate key length.
256
                if len(k) != 33+8 {
993✔
NEW
257
                        return fmt.Errorf("invalid edge key %x encountered", k)
×
NEW
258
                }
×
259

260
                var key channelMapKey
993✔
261
                copy(key.nodeKey[:], k[:33])
993✔
262
                copy(key.chanID[:], k[33:])
993✔
263

993✔
264
                // No need to deserialize unknown policy.
993✔
265
                if bytes.Equal(edgeBytes, unknownPolicy) {
993✔
NEW
266
                        return nil
×
NEW
267
                }
×
268

269
                edgeReader := bytes.NewReader(edgeBytes)
993✔
270
                edge, err := deserializeChanEdgePolicyRaw(
993✔
271
                        edgeReader,
993✔
272
                )
993✔
273

993✔
274
                switch {
993✔
275
                // If the db policy was missing an expected optional field, we
276
                // return nil as if the policy was unknown.
NEW
277
                case errors.Is(err, ErrEdgePolicyOptionalFieldNotFound):
×
NEW
278
                        return nil
×
279

NEW
280
                case err != nil:
×
NEW
281
                        return err
×
282
                }
283

284
                channelMap[key] = edge
993✔
285

993✔
286
                return nil
993✔
287
        })
288
        if err != nil {
148✔
NEW
289
                return nil, err
×
NEW
290
        }
×
291

292
        return channelMap, nil
148✔
293
}
294

295
var graphTopLevelBuckets = [][]byte{
296
        nodeBucket,
297
        edgeBucket,
298
        graphMetaBucket,
299
        closedScidBucket,
300
}
301

302
// Wipe completely deletes all saved state within all used buckets within the
303
// database. The deletion is done in a single transaction, therefore this
304
// operation is fully atomic.
NEW
305
func (c *KVStore) Wipe() error {
×
NEW
306
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
×
NEW
307
                for _, tlb := range graphTopLevelBuckets {
×
NEW
308
                        err := tx.DeleteTopLevelBucket(tlb)
×
NEW
309
                        if err != nil &&
×
NEW
310
                                !errors.Is(err, kvdb.ErrBucketNotFound) {
×
NEW
311

×
NEW
312
                                return err
×
NEW
313
                        }
×
314
                }
315

NEW
316
                return nil
×
NEW
317
        }, func() {})
×
NEW
318
        if err != nil {
×
NEW
319
                return err
×
NEW
320
        }
×
321

NEW
322
        return initKVStore(c.db)
×
323
}
324

325
// createChannelDB creates and initializes a fresh version of  In
326
// the case that the target path has not yet been created or doesn't yet exist,
327
// then the path is created. Additionally, all required top-level buckets used
328
// within the database are created.
329
func initKVStore(db kvdb.Backend) error {
177✔
330
        err := kvdb.Update(db, func(tx kvdb.RwTx) error {
354✔
331
                for _, tlb := range graphTopLevelBuckets {
876✔
332
                        if _, err := tx.CreateTopLevelBucket(tlb); err != nil {
699✔
NEW
333
                                return err
×
NEW
334
                        }
×
335
                }
336

337
                nodes := tx.ReadWriteBucket(nodeBucket)
177✔
338
                _, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
177✔
339
                if err != nil {
177✔
NEW
340
                        return err
×
NEW
341
                }
×
342
                _, err = nodes.CreateBucketIfNotExists(nodeUpdateIndexBucket)
177✔
343
                if err != nil {
177✔
NEW
344
                        return err
×
NEW
345
                }
×
346

347
                edges := tx.ReadWriteBucket(edgeBucket)
177✔
348
                _, err = edges.CreateBucketIfNotExists(edgeIndexBucket)
177✔
349
                if err != nil {
177✔
NEW
350
                        return err
×
NEW
351
                }
×
352
                _, err = edges.CreateBucketIfNotExists(edgeUpdateIndexBucket)
177✔
353
                if err != nil {
177✔
NEW
354
                        return err
×
NEW
355
                }
×
356
                _, err = edges.CreateBucketIfNotExists(channelPointBucket)
177✔
357
                if err != nil {
177✔
NEW
358
                        return err
×
NEW
359
                }
×
360
                _, err = edges.CreateBucketIfNotExists(zombieBucket)
177✔
361
                if err != nil {
177✔
NEW
362
                        return err
×
NEW
363
                }
×
364

365
                graphMeta := tx.ReadWriteBucket(graphMetaBucket)
177✔
366
                _, err = graphMeta.CreateBucketIfNotExists(pruneLogBucket)
177✔
367

177✔
368
                return err
177✔
369
        }, func() {})
177✔
370
        if err != nil {
177✔
NEW
371
                return fmt.Errorf("unable to create new channel graph: %w", err)
×
NEW
372
        }
×
373

374
        return nil
177✔
375
}
376

377
// AddrsForNode returns all known addresses for the target node public key that
378
// the graph DB is aware of. The returned boolean indicates if the given node is
379
// unknown to the graph DB or not.
380
//
381
// NOTE: this is part of the channeldb.AddrSource interface.
382
func (c *KVStore) AddrsForNode(nodePub *btcec.PublicKey) (bool, []net.Addr,
383
        error) {
4✔
384

4✔
385
        pubKey, err := route.NewVertexFromBytes(nodePub.SerializeCompressed())
4✔
386
        if err != nil {
4✔
NEW
387
                return false, nil, err
×
NEW
388
        }
×
389

390
        node, err := c.FetchLightningNode(pubKey)
4✔
391
        // We don't consider it an error if the graph is unaware of the node.
4✔
392
        switch {
4✔
NEW
393
        case err != nil && !errors.Is(err, ErrGraphNodeNotFound):
×
NEW
394
                return false, nil, err
×
395

396
        case errors.Is(err, ErrGraphNodeNotFound):
3✔
397
                return false, nil, nil
3✔
398
        }
399

400
        return true, node.Addresses, nil
4✔
401
}
402

403
// ForEachChannel iterates through all the channel edges stored within the
404
// graph and invokes the passed callback for each edge. The callback takes two
405
// edges as since this is a directed graph, both the in/out edges are visited.
406
// If the callback returns an error, then the transaction is aborted and the
407
// iteration stops early.
408
//
409
// NOTE: If an edge can't be found, or wasn't advertised, then a nil pointer
410
// for that particular channel edge routing policy will be passed into the
411
// callback.
412
func (c *KVStore) ForEachChannel(cb func(*models.ChannelEdgeInfo,
413
        *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
148✔
414

148✔
415
        return c.db.View(func(tx kvdb.RTx) error {
296✔
416
                edges := tx.ReadBucket(edgeBucket)
148✔
417
                if edges == nil {
148✔
NEW
418
                        return ErrGraphNoEdgesFound
×
NEW
419
                }
×
420

421
                // First, load all edges in memory indexed by node and channel
422
                // id.
423
                channelMap, err := c.getChannelMap(edges)
148✔
424
                if err != nil {
148✔
NEW
425
                        return err
×
NEW
426
                }
×
427

428
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
148✔
429
                if edgeIndex == nil {
148✔
NEW
430
                        return ErrGraphNoEdgesFound
×
NEW
431
                }
×
432

433
                // Load edge index, recombine each channel with the policies
434
                // loaded above and invoke the callback.
435
                return kvdb.ForAll(
148✔
436
                        edgeIndex, func(k, edgeInfoBytes []byte) error {
646✔
437
                                var chanID [8]byte
498✔
438
                                copy(chanID[:], k)
498✔
439

498✔
440
                                edgeInfoReader := bytes.NewReader(edgeInfoBytes)
498✔
441
                                info, err := deserializeChanEdgeInfo(
498✔
442
                                        edgeInfoReader,
498✔
443
                                )
498✔
444
                                if err != nil {
498✔
NEW
445
                                        return err
×
NEW
446
                                }
×
447

448
                                policy1 := channelMap[channelMapKey{
498✔
449
                                        nodeKey: info.NodeKey1Bytes,
498✔
450
                                        chanID:  chanID,
498✔
451
                                }]
498✔
452

498✔
453
                                policy2 := channelMap[channelMapKey{
498✔
454
                                        nodeKey: info.NodeKey2Bytes,
498✔
455
                                        chanID:  chanID,
498✔
456
                                }]
498✔
457

498✔
458
                                return cb(&info, policy1, policy2)
498✔
459
                        },
460
                )
461
        }, func() {})
148✔
462
}
463

464
// forEachNodeDirectedChannel iterates through all channels of a given node,
465
// executing the passed callback on the directed edge representing the channel
466
// and its incoming policy. If the callback returns an error, then the iteration
467
// is halted with the error propagated back up to the caller. An optional read
468
// transaction may be provided. If none is provided, a new one will be created.
469
//
470
// Unknown policies are passed into the callback as nil values.
471
func (c *KVStore) forEachNodeDirectedChannel(tx kvdb.RTx,
472
        node route.Vertex, cb func(channel *DirectedChannel) error) error {
245✔
473

245✔
474
        // Fallback that uses the database.
245✔
475
        toNodeCallback := func() route.Vertex {
380✔
476
                return node
135✔
477
        }
135✔
478
        toNodeFeatures, err := c.fetchNodeFeatures(tx, node)
245✔
479
        if err != nil {
245✔
NEW
480
                return err
×
NEW
481
        }
×
482

483
        dbCallback := func(tx kvdb.RTx, e *models.ChannelEdgeInfo, p1,
245✔
484
                p2 *models.ChannelEdgePolicy) error {
744✔
485

499✔
486
                var cachedInPolicy *models.CachedEdgePolicy
499✔
487
                if p2 != nil {
995✔
488
                        cachedInPolicy = models.NewCachedPolicy(p2)
496✔
489
                        cachedInPolicy.ToNodePubKey = toNodeCallback
496✔
490
                        cachedInPolicy.ToNodeFeatures = toNodeFeatures
496✔
491
                }
496✔
492

493
                var inboundFee lnwire.Fee
499✔
494
                if p1 != nil {
997✔
495
                        // Extract inbound fee. If there is a decoding error,
498✔
496
                        // skip this edge.
498✔
497
                        _, err := p1.ExtraOpaqueData.ExtractRecords(&inboundFee)
498✔
498
                        if err != nil {
499✔
499
                                return nil
1✔
500
                        }
1✔
501
                }
502

503
                directedChannel := &DirectedChannel{
498✔
504
                        ChannelID:    e.ChannelID,
498✔
505
                        IsNode1:      node == e.NodeKey1Bytes,
498✔
506
                        OtherNode:    e.NodeKey2Bytes,
498✔
507
                        Capacity:     e.Capacity,
498✔
508
                        OutPolicySet: p1 != nil,
498✔
509
                        InPolicy:     cachedInPolicy,
498✔
510
                        InboundFee:   inboundFee,
498✔
511
                }
498✔
512

498✔
513
                if node == e.NodeKey2Bytes {
749✔
514
                        directedChannel.OtherNode = e.NodeKey1Bytes
251✔
515
                }
251✔
516

517
                return cb(directedChannel)
498✔
518
        }
519

520
        return nodeTraversal(tx, node[:], c.db, dbCallback)
245✔
521
}
522

523
// fetchNodeFeatures returns the features of a given node. If no features are
524
// known for the node, an empty feature vector is returned. An optional read
525
// transaction may be provided. If none is provided, a new one will be created.
526
func (c *KVStore) fetchNodeFeatures(tx kvdb.RTx,
527
        node route.Vertex) (*lnwire.FeatureVector, error) {
689✔
528

689✔
529
        // Fallback that uses the database.
689✔
530
        targetNode, err := c.FetchLightningNodeTx(tx, node)
689✔
531
        switch {
689✔
532
        // If the node exists and has features, return them directly.
533
        case err == nil:
678✔
534
                return targetNode.Features, nil
678✔
535

536
        // If we couldn't find a node announcement, populate a blank feature
537
        // vector.
538
        case errors.Is(err, ErrGraphNodeNotFound):
11✔
539
                return lnwire.EmptyFeatureVector(), nil
11✔
540

541
        // Otherwise, bubble the error up.
NEW
542
        default:
×
NEW
543
                return nil, err
×
544
        }
545
}
546

547
// ForEachNodeDirectedChannel iterates through all channels of a given node,
548
// executing the passed callback on the directed edge representing the channel
549
// and its incoming policy. If the callback returns an error, then the iteration
550
// is halted with the error propagated back up to the caller. If the graphCache
551
// is available, then it will be used to retrieve the node's channels instead
552
// of the database.
553
//
554
// Unknown policies are passed into the callback as nil values.
555
//
556
// NOTE: this is part of the graphdb.NodeTraverser interface.
557
func (c *KVStore) ForEachNodeDirectedChannel(nodePub route.Vertex,
558
        cb func(channel *DirectedChannel) error) error {
6✔
559

6✔
560
        return c.forEachNodeDirectedChannel(nil, nodePub, cb)
6✔
561
}
6✔
562

563
// FetchNodeFeatures returns the features of the given node. If no features are
564
// known for the node, an empty feature vector is returned.
565
// If the graphCache is available, then it will be used to retrieve the node's
566
// features instead of the database.
567
//
568
// NOTE: this is part of the graphdb.NodeTraverser interface.
569
func (c *KVStore) FetchNodeFeatures(nodePub route.Vertex) (
570
        *lnwire.FeatureVector, error) {
3✔
571

3✔
572
        return c.fetchNodeFeatures(nil, nodePub)
3✔
573
}
3✔
574

575
// ForEachNodeCached is similar to forEachNode, but it returns DirectedChannel
576
// data to the call-back.
577
//
578
// NOTE: The callback contents MUST not be modified.
579
func (c *KVStore) ForEachNodeCached(cb func(node route.Vertex,
580
        chans map[uint64]*DirectedChannel) error) error {
1✔
581

1✔
582
        // Otherwise call back to a version that uses the database directly.
1✔
583
        // We'll iterate over each node, then the set of channels for each
1✔
584
        // node, and construct a similar callback functiopn signature as the
1✔
585
        // main funcotin expects.
1✔
586
        return c.forEachNode(func(tx kvdb.RTx,
1✔
587
                node *models.LightningNode) error {
21✔
588

20✔
589
                channels := make(map[uint64]*DirectedChannel)
20✔
590

20✔
591
                err := c.ForEachNodeChannelTx(tx, node.PubKeyBytes,
20✔
592
                        func(tx kvdb.RTx, e *models.ChannelEdgeInfo,
20✔
593
                                p1 *models.ChannelEdgePolicy,
20✔
594
                                p2 *models.ChannelEdgePolicy) error {
210✔
595

190✔
596
                                toNodeCallback := func() route.Vertex {
190✔
NEW
597
                                        return node.PubKeyBytes
×
NEW
598
                                }
×
599
                                toNodeFeatures, err := c.fetchNodeFeatures(
190✔
600
                                        tx, node.PubKeyBytes,
190✔
601
                                )
190✔
602
                                if err != nil {
190✔
NEW
603
                                        return err
×
NEW
604
                                }
×
605

606
                                var cachedInPolicy *models.CachedEdgePolicy
190✔
607
                                if p2 != nil {
380✔
608
                                        cachedInPolicy =
190✔
609
                                                models.NewCachedPolicy(p2)
190✔
610
                                        cachedInPolicy.ToNodePubKey =
190✔
611
                                                toNodeCallback
190✔
612
                                        cachedInPolicy.ToNodeFeatures =
190✔
613
                                                toNodeFeatures
190✔
614
                                }
190✔
615

616
                                directedChannel := &DirectedChannel{
190✔
617
                                        ChannelID: e.ChannelID,
190✔
618
                                        IsNode1: node.PubKeyBytes ==
190✔
619
                                                e.NodeKey1Bytes,
190✔
620
                                        OtherNode:    e.NodeKey2Bytes,
190✔
621
                                        Capacity:     e.Capacity,
190✔
622
                                        OutPolicySet: p1 != nil,
190✔
623
                                        InPolicy:     cachedInPolicy,
190✔
624
                                }
190✔
625

190✔
626
                                if node.PubKeyBytes == e.NodeKey2Bytes {
285✔
627
                                        directedChannel.OtherNode =
95✔
628
                                                e.NodeKey1Bytes
95✔
629
                                }
95✔
630

631
                                channels[e.ChannelID] = directedChannel
190✔
632

190✔
633
                                return nil
190✔
634
                        })
635
                if err != nil {
20✔
NEW
636
                        return err
×
NEW
637
                }
×
638

639
                return cb(node.PubKeyBytes, channels)
20✔
640
        })
641
}
642

643
// DisabledChannelIDs returns the channel ids of disabled channels.
644
// A channel is disabled when two of the associated ChanelEdgePolicies
645
// have their disabled bit on.
646
func (c *KVStore) DisabledChannelIDs() ([]uint64, error) {
6✔
647
        var disabledChanIDs []uint64
6✔
648
        var chanEdgeFound map[uint64]struct{}
6✔
649

6✔
650
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
12✔
651
                edges := tx.ReadBucket(edgeBucket)
6✔
652
                if edges == nil {
6✔
NEW
653
                        return ErrGraphNoEdgesFound
×
NEW
654
                }
×
655

656
                disabledEdgePolicyIndex := edges.NestedReadBucket(
6✔
657
                        disabledEdgePolicyBucket,
6✔
658
                )
6✔
659
                if disabledEdgePolicyIndex == nil {
7✔
660
                        return nil
1✔
661
                }
1✔
662

663
                // We iterate over all disabled policies and we add each channel
664
                // that has more than one disabled policy to disabledChanIDs
665
                // array.
666
                return disabledEdgePolicyIndex.ForEach(
5✔
667
                        func(k, v []byte) error {
16✔
668
                                chanID := byteOrder.Uint64(k[:8])
11✔
669
                                _, edgeFound := chanEdgeFound[chanID]
11✔
670
                                if edgeFound {
15✔
671
                                        delete(chanEdgeFound, chanID)
4✔
672
                                        disabledChanIDs = append(
4✔
673
                                                disabledChanIDs, chanID,
4✔
674
                                        )
4✔
675

4✔
676
                                        return nil
4✔
677
                                }
4✔
678

679
                                chanEdgeFound[chanID] = struct{}{}
7✔
680

7✔
681
                                return nil
7✔
682
                        },
683
                )
684
        }, func() {
6✔
685
                disabledChanIDs = nil
6✔
686
                chanEdgeFound = make(map[uint64]struct{})
6✔
687
        })
6✔
688
        if err != nil {
6✔
NEW
689
                return nil, err
×
NEW
690
        }
×
691

692
        return disabledChanIDs, nil
6✔
693
}
694

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

973✔
705
                return cb(newChanGraphNodeTx(tx, c, node))
973✔
706
        })
973✔
707
}
708

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

132✔
719
        traversal := func(tx kvdb.RTx) error {
264✔
720
                // First grab the nodes bucket which stores the mapping from
132✔
721
                // pubKey to node information.
132✔
722
                nodes := tx.ReadBucket(nodeBucket)
132✔
723
                if nodes == nil {
132✔
NEW
724
                        return ErrGraphNotFound
×
NEW
725
                }
×
726

727
                return nodes.ForEach(func(pubKey, nodeBytes []byte) error {
1,574✔
728
                        // If this is the source key, then we skip this
1,442✔
729
                        // iteration as the value for this key is a pubKey
1,442✔
730
                        // rather than raw node information.
1,442✔
731
                        if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
1,706✔
732
                                return nil
264✔
733
                        }
264✔
734

735
                        nodeReader := bytes.NewReader(nodeBytes)
1,181✔
736
                        node, err := deserializeLightningNode(nodeReader)
1,181✔
737
                        if err != nil {
1,181✔
NEW
738
                                return err
×
NEW
739
                        }
×
740

741
                        // Execute the callback, the transaction will abort if
742
                        // this returns an error.
743
                        return cb(tx, &node)
1,181✔
744
                })
745
        }
746

747
        return kvdb.View(c.db, traversal, func() {})
264✔
748
}
749

750
// ForEachNodeCacheable iterates through all the stored vertices/nodes in the
751
// graph, executing the passed callback with each node encountered. If the
752
// callback returns an error, then the transaction is aborted and the iteration
753
// stops early.
754
func (c *KVStore) ForEachNodeCacheable(cb func(route.Vertex,
755
        *lnwire.FeatureVector) error) error {
145✔
756

145✔
757
        traversal := func(tx kvdb.RTx) error {
290✔
758
                // First grab the nodes bucket which stores the mapping from
145✔
759
                // pubKey to node information.
145✔
760
                nodes := tx.ReadBucket(nodeBucket)
145✔
761
                if nodes == nil {
145✔
NEW
762
                        return ErrGraphNotFound
×
NEW
763
                }
×
764

765
                return nodes.ForEach(func(pubKey, nodeBytes []byte) error {
552✔
766
                        // If this is the source key, then we skip this
407✔
767
                        // iteration as the value for this key is a pubKey
407✔
768
                        // rather than raw node information.
407✔
769
                        if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
694✔
770
                                return nil
287✔
771
                        }
287✔
772

773
                        nodeReader := bytes.NewReader(nodeBytes)
123✔
774
                        node, features, err := deserializeLightningNodeCacheable( //nolint:ll
123✔
775
                                nodeReader,
123✔
776
                        )
123✔
777
                        if err != nil {
123✔
NEW
778
                                return err
×
NEW
779
                        }
×
780

781
                        // Execute the callback, the transaction will abort if
782
                        // this returns an error.
783
                        return cb(node, features)
123✔
784
                })
785
        }
786

787
        return kvdb.View(c.db, traversal, func() {})
290✔
788
}
789

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

804
                node, err := c.sourceNode(nodes)
234✔
805
                if err != nil {
235✔
806
                        return err
1✔
807
                }
1✔
808
                source = node
233✔
809

233✔
810
                return nil
233✔
811
        }, func() {
234✔
812
                source = nil
234✔
813
        })
234✔
814
        if err != nil {
235✔
815
                return nil, err
1✔
816
        }
1✔
817

818
        return source, nil
233✔
819
}
820

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

488✔
828
        selfPub := nodes.Get(sourceKey)
488✔
829
        if selfPub == nil {
489✔
830
                return nil, ErrSourceNodeNotSet
1✔
831
        }
1✔
832

833
        // With the pubKey of the source node retrieved, we're able to
834
        // fetch the full node information.
835
        node, err := fetchLightningNode(nodes, selfPub)
487✔
836
        if err != nil {
487✔
NEW
837
                return nil, err
×
NEW
838
        }
×
839

840
        return &node, nil
487✔
841
}
842

843
// SetSourceNode sets the source node within the graph database. The source
844
// node is to be used as the center of a star-graph within path finding
845
// algorithms.
846
func (c *KVStore) SetSourceNode(node *models.LightningNode) error {
120✔
847
        nodePubBytes := node.PubKeyBytes[:]
120✔
848

120✔
849
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
240✔
850
                // First grab the nodes bucket which stores the mapping from
120✔
851
                // pubKey to node information.
120✔
852
                nodes, err := tx.CreateTopLevelBucket(nodeBucket)
120✔
853
                if err != nil {
120✔
NEW
854
                        return err
×
NEW
855
                }
×
856

857
                // Next we create the mapping from source to the targeted
858
                // public key.
859
                if err := nodes.Put(sourceKey, nodePubBytes); err != nil {
120✔
NEW
860
                        return err
×
NEW
861
                }
×
862

863
                // Finally, we commit the information of the lightning node
864
                // itself.
865
                return addLightningNode(tx, node)
120✔
866
        }, func() {})
120✔
867
}
868

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

803✔
880
        r := &batch.Request{
803✔
881
                Update: func(tx kvdb.RwTx) error {
1,606✔
882
                        return addLightningNode(tx, node)
803✔
883
                },
803✔
884
        }
885

886
        for _, f := range op {
806✔
887
                f(r)
3✔
888
        }
3✔
889

890
        return c.nodeScheduler.Execute(r)
803✔
891
}
892

893
func addLightningNode(tx kvdb.RwTx, node *models.LightningNode) error {
993✔
894
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
993✔
895
        if err != nil {
993✔
NEW
896
                return err
×
NEW
897
        }
×
898

899
        aliases, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
993✔
900
        if err != nil {
993✔
NEW
901
                return err
×
NEW
902
        }
×
903

904
        updateIndex, err := nodes.CreateBucketIfNotExists(
993✔
905
                nodeUpdateIndexBucket,
993✔
906
        )
993✔
907
        if err != nil {
993✔
NEW
908
                return err
×
NEW
909
        }
×
910

911
        return putLightningNode(nodes, aliases, updateIndex, node)
993✔
912
}
913

914
// LookupAlias attempts to return the alias as advertised by the target node.
915
// TODO(roasbeef): currently assumes that aliases are unique...
916
func (c *KVStore) LookupAlias(pub *btcec.PublicKey) (string, error) {
5✔
917
        var alias string
5✔
918

5✔
919
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
10✔
920
                nodes := tx.ReadBucket(nodeBucket)
5✔
921
                if nodes == nil {
5✔
NEW
922
                        return ErrGraphNodesNotFound
×
NEW
923
                }
×
924

925
                aliases := nodes.NestedReadBucket(aliasIndexBucket)
5✔
926
                if aliases == nil {
5✔
NEW
927
                        return ErrGraphNodesNotFound
×
NEW
928
                }
×
929

930
                nodePub := pub.SerializeCompressed()
5✔
931
                a := aliases.Get(nodePub)
5✔
932
                if a == nil {
6✔
933
                        return ErrNodeAliasNotFound
1✔
934
                }
1✔
935

936
                // TODO(roasbeef): should actually be using the utf-8
937
                // package...
938
                alias = string(a)
4✔
939

4✔
940
                return nil
4✔
941
        }, func() {
5✔
942
                alias = ""
5✔
943
        })
5✔
944
        if err != nil {
6✔
945
                return "", err
1✔
946
        }
1✔
947

948
        return alias, nil
4✔
949
}
950

951
// DeleteLightningNode starts a new database transaction to remove a vertex/node
952
// from the database according to the node's public key.
953
func (c *KVStore) DeleteLightningNode(nodePub route.Vertex) error {
3✔
954
        // TODO(roasbeef): ensure dangling edges are removed...
3✔
955
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
956
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
957
                if nodes == nil {
3✔
NEW
958
                        return ErrGraphNodeNotFound
×
NEW
959
                }
×
960

961
                return c.deleteLightningNode(nodes, nodePub[:])
3✔
962
        }, func() {})
3✔
963
}
964

965
// deleteLightningNode uses an existing database transaction to remove a
966
// vertex/node from the database according to the node's public key.
967
func (c *KVStore) deleteLightningNode(nodes kvdb.RwBucket,
968
        compressedPubKey []byte) error {
68✔
969

68✔
970
        aliases := nodes.NestedReadWriteBucket(aliasIndexBucket)
68✔
971
        if aliases == nil {
68✔
NEW
972
                return ErrGraphNodesNotFound
×
NEW
973
        }
×
974

975
        if err := aliases.Delete(compressedPubKey); err != nil {
68✔
NEW
976
                return err
×
NEW
977
        }
×
978

979
        // Before we delete the node, we'll fetch its current state so we can
980
        // determine when its last update was to clear out the node update
981
        // index.
982
        node, err := fetchLightningNode(nodes, compressedPubKey)
68✔
983
        if err != nil {
68✔
NEW
984
                return err
×
NEW
985
        }
×
986

987
        if err := nodes.Delete(compressedPubKey); err != nil {
68✔
NEW
988
                return err
×
NEW
989
        }
×
990

991
        // Finally, we'll delete the index entry for the node within the
992
        // nodeUpdateIndexBucket as this node is no longer active, so we don't
993
        // need to track its last update.
994
        nodeUpdateIndex := nodes.NestedReadWriteBucket(nodeUpdateIndexBucket)
68✔
995
        if nodeUpdateIndex == nil {
68✔
NEW
996
                return ErrGraphNodesNotFound
×
NEW
997
        }
×
998

999
        // In order to delete the entry, we'll need to reconstruct the key for
1000
        // its last update.
1001
        updateUnix := uint64(node.LastUpdate.Unix())
68✔
1002
        var indexKey [8 + 33]byte
68✔
1003
        byteOrder.PutUint64(indexKey[:8], updateUnix)
68✔
1004
        copy(indexKey[8:], compressedPubKey)
68✔
1005

68✔
1006
        return nodeUpdateIndex.Delete(indexKey[:])
68✔
1007
}
1008

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

1,719✔
1018
        var alreadyExists bool
1,719✔
1019
        r := &batch.Request{
1,719✔
1020
                Reset: func() {
3,438✔
1021
                        alreadyExists = false
1,719✔
1022
                },
1,719✔
1023
                Update: func(tx kvdb.RwTx) error {
1,719✔
1024
                        err := c.addChannelEdge(tx, edge)
1,719✔
1025

1,719✔
1026
                        // Silence ErrEdgeAlreadyExist so that the batch can
1,719✔
1027
                        // succeed, but propagate the error via local state.
1,719✔
1028
                        if errors.Is(err, ErrEdgeAlreadyExist) {
1,953✔
1029
                                alreadyExists = true
234✔
1030
                                return nil
234✔
1031
                        }
234✔
1032

1033
                        return err
1,485✔
1034
                },
1035
                OnCommit: func(err error) error {
1,719✔
1036
                        switch {
1,719✔
NEW
1037
                        case err != nil:
×
NEW
1038
                                return err
×
1039
                        case alreadyExists:
234✔
1040
                                return ErrEdgeAlreadyExist
234✔
1041
                        default:
1,485✔
1042
                                c.rejectCache.remove(edge.ChannelID)
1,485✔
1043
                                c.chanCache.remove(edge.ChannelID)
1,485✔
1044
                                return nil
1,485✔
1045
                        }
1046
                },
1047
        }
1048

1049
        for _, f := range op {
1,722✔
1050
                if f == nil {
3✔
NEW
1051
                        return fmt.Errorf("nil scheduler option was used")
×
NEW
1052
                }
×
1053

1054
                f(r)
3✔
1055
        }
1056

1057
        return c.chanScheduler.Execute(r)
1,719✔
1058
}
1059

1060
// addChannelEdge is the private form of AddChannelEdge that allows callers to
1061
// utilize an existing db transaction.
1062
func (c *KVStore) addChannelEdge(tx kvdb.RwTx,
1063
        edge *models.ChannelEdgeInfo) error {
1,719✔
1064

1,719✔
1065
        // Construct the channel's primary key which is the 8-byte channel ID.
1,719✔
1066
        var chanKey [8]byte
1,719✔
1067
        binary.BigEndian.PutUint64(chanKey[:], edge.ChannelID)
1,719✔
1068

1,719✔
1069
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
1,719✔
1070
        if err != nil {
1,719✔
NEW
1071
                return err
×
NEW
1072
        }
×
1073
        edges, err := tx.CreateTopLevelBucket(edgeBucket)
1,719✔
1074
        if err != nil {
1,719✔
NEW
1075
                return err
×
NEW
1076
        }
×
1077
        edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
1,719✔
1078
        if err != nil {
1,719✔
NEW
1079
                return err
×
NEW
1080
        }
×
1081
        chanIndex, err := edges.CreateBucketIfNotExists(channelPointBucket)
1,719✔
1082
        if err != nil {
1,719✔
NEW
1083
                return err
×
NEW
1084
        }
×
1085

1086
        // First, attempt to check if this edge has already been created. If
1087
        // so, then we can exit early as this method is meant to be idempotent.
1088
        if edgeInfo := edgeIndex.Get(chanKey[:]); edgeInfo != nil {
1,953✔
1089
                return ErrEdgeAlreadyExist
234✔
1090
        }
234✔
1091

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

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

1128
        // If the edge hasn't been created yet, then we'll first add it to the
1129
        // edge index in order to associate the edge between two nodes and also
1130
        // store the static components of the channel.
1131
        if err := putChanEdgeInfo(edgeIndex, edge, chanKey); err != nil {
1,485✔
NEW
1132
                return err
×
NEW
1133
        }
×
1134

1135
        // Mark edge policies for both sides as unknown. This is to enable
1136
        // efficient incoming channel lookup for a node.
1137
        keys := []*[33]byte{
1,485✔
1138
                &edge.NodeKey1Bytes,
1,485✔
1139
                &edge.NodeKey2Bytes,
1,485✔
1140
        }
1,485✔
1141
        for _, key := range keys {
4,452✔
1142
                err := putChanEdgePolicyUnknown(edges, edge.ChannelID, key[:])
2,967✔
1143
                if err != nil {
2,967✔
NEW
1144
                        return err
×
NEW
1145
                }
×
1146
        }
1147

1148
        // Finally we add it to the channel index which maps channel points
1149
        // (outpoints) to the shorter channel ID's.
1150
        var b bytes.Buffer
1,485✔
1151
        if err := WriteOutpoint(&b, &edge.ChannelPoint); err != nil {
1,485✔
NEW
1152
                return err
×
NEW
1153
        }
×
1154

1155
        return chanIndex.Put(b.Bytes(), chanKey[:])
1,485✔
1156
}
1157

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

215✔
1167
        var (
215✔
1168
                upd1Time time.Time
215✔
1169
                upd2Time time.Time
215✔
1170
                exists   bool
215✔
1171
                isZombie bool
215✔
1172
        )
215✔
1173

215✔
1174
        // We'll query the cache with the shared lock held to allow multiple
215✔
1175
        // readers to access values in the cache concurrently if they exist.
215✔
1176
        c.cacheMu.RLock()
215✔
1177
        if entry, ok := c.rejectCache.get(chanID); ok {
288✔
1178
                c.cacheMu.RUnlock()
73✔
1179
                upd1Time = time.Unix(entry.upd1Time, 0)
73✔
1180
                upd2Time = time.Unix(entry.upd2Time, 0)
73✔
1181
                exists, isZombie = entry.flags.unpack()
73✔
1182

73✔
1183
                return upd1Time, upd2Time, exists, isZombie, nil
73✔
1184
        }
73✔
1185
        c.cacheMu.RUnlock()
145✔
1186

145✔
1187
        c.cacheMu.Lock()
145✔
1188
        defer c.cacheMu.Unlock()
145✔
1189

145✔
1190
        // The item was not found with the shared lock, so we'll acquire the
145✔
1191
        // exclusive lock and check the cache again in case another method added
145✔
1192
        // the entry to the cache while no lock was held.
145✔
1193
        if entry, ok := c.rejectCache.get(chanID); ok {
152✔
1194
                upd1Time = time.Unix(entry.upd1Time, 0)
7✔
1195
                upd2Time = time.Unix(entry.upd2Time, 0)
7✔
1196
                exists, isZombie = entry.flags.unpack()
7✔
1197

7✔
1198
                return upd1Time, upd2Time, exists, isZombie, nil
7✔
1199
        }
7✔
1200

1201
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
282✔
1202
                edges := tx.ReadBucket(edgeBucket)
141✔
1203
                if edges == nil {
141✔
NEW
1204
                        return ErrGraphNoEdgesFound
×
NEW
1205
                }
×
1206
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
141✔
1207
                if edgeIndex == nil {
141✔
NEW
1208
                        return ErrGraphNoEdgesFound
×
NEW
1209
                }
×
1210

1211
                var channelID [8]byte
141✔
1212
                byteOrder.PutUint64(channelID[:], chanID)
141✔
1213

141✔
1214
                // If the edge doesn't exist, then we'll also check our zombie
141✔
1215
                // index.
141✔
1216
                if edgeIndex.Get(channelID[:]) == nil {
237✔
1217
                        exists = false
96✔
1218
                        zombieIndex := edges.NestedReadBucket(zombieBucket)
96✔
1219
                        if zombieIndex != nil {
192✔
1220
                                isZombie, _, _ = isZombieEdge(
96✔
1221
                                        zombieIndex, chanID,
96✔
1222
                                )
96✔
1223
                        }
96✔
1224

1225
                        return nil
96✔
1226
                }
1227

1228
                exists = true
48✔
1229
                isZombie = false
48✔
1230

48✔
1231
                // If the channel has been found in the graph, then retrieve
48✔
1232
                // the edges itself so we can return the last updated
48✔
1233
                // timestamps.
48✔
1234
                nodes := tx.ReadBucket(nodeBucket)
48✔
1235
                if nodes == nil {
48✔
NEW
1236
                        return ErrGraphNodeNotFound
×
NEW
1237
                }
×
1238

1239
                e1, e2, err := fetchChanEdgePolicies(
48✔
1240
                        edgeIndex, edges, channelID[:],
48✔
1241
                )
48✔
1242
                if err != nil {
48✔
NEW
1243
                        return err
×
NEW
1244
                }
×
1245

1246
                // As we may have only one of the edges populated, only set the
1247
                // update time if the edge was found in the database.
1248
                if e1 != nil {
69✔
1249
                        upd1Time = e1.LastUpdate
21✔
1250
                }
21✔
1251
                if e2 != nil {
67✔
1252
                        upd2Time = e2.LastUpdate
19✔
1253
                }
19✔
1254

1255
                return nil
48✔
1256
        }, func() {}); err != nil {
141✔
NEW
1257
                return time.Time{}, time.Time{}, exists, isZombie, err
×
NEW
1258
        }
×
1259

1260
        c.rejectCache.insert(chanID, rejectCacheEntry{
141✔
1261
                upd1Time: upd1Time.Unix(),
141✔
1262
                upd2Time: upd2Time.Unix(),
141✔
1263
                flags:    packRejectFlags(exists, isZombie),
141✔
1264
        })
141✔
1265

141✔
1266
        return upd1Time, upd2Time, exists, isZombie, nil
141✔
1267
}
1268

1269
// AddEdgeProof sets the proof of an existing edge in the graph database.
1270
func (c *KVStore) AddEdgeProof(chanID lnwire.ShortChannelID,
1271
        proof *models.ChannelAuthProof) error {
4✔
1272

4✔
1273
        // Construct the channel's primary key which is the 8-byte channel ID.
4✔
1274
        var chanKey [8]byte
4✔
1275
        binary.BigEndian.PutUint64(chanKey[:], chanID.ToUint64())
4✔
1276

4✔
1277
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
8✔
1278
                edges := tx.ReadWriteBucket(edgeBucket)
4✔
1279
                if edges == nil {
4✔
NEW
1280
                        return ErrEdgeNotFound
×
NEW
1281
                }
×
1282

1283
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
4✔
1284
                if edgeIndex == nil {
4✔
NEW
1285
                        return ErrEdgeNotFound
×
NEW
1286
                }
×
1287

1288
                edge, err := fetchChanEdgeInfo(edgeIndex, chanKey[:])
4✔
1289
                if err != nil {
4✔
NEW
1290
                        return err
×
NEW
1291
                }
×
1292

1293
                edge.AuthProof = proof
4✔
1294

4✔
1295
                return putChanEdgeInfo(edgeIndex, &edge, chanKey)
4✔
1296
        }, func() {})
4✔
1297
}
1298

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

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

234✔
1319
        c.cacheMu.Lock()
234✔
1320
        defer c.cacheMu.Unlock()
234✔
1321

234✔
1322
        var chansClosed []*models.ChannelEdgeInfo
234✔
1323

234✔
1324
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
468✔
1325
                // First grab the edges bucket which houses the information
234✔
1326
                // we'd like to delete
234✔
1327
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
234✔
1328
                if err != nil {
234✔
NEW
1329
                        return err
×
NEW
1330
                }
×
1331

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

1353
                // For each of the outpoints that have been spent within the
1354
                // block, we attempt to delete them from the graph as if that
1355
                // outpoint was a channel, then it has now been closed.
1356
                for _, chanPoint := range spentOutputs {
350✔
1357
                        // TODO(roasbeef): load channel bloom filter, continue
116✔
1358
                        // if NOT if filter
116✔
1359

116✔
1360
                        var opBytes bytes.Buffer
116✔
1361
                        err := WriteOutpoint(&opBytes, chanPoint)
116✔
1362
                        if err != nil {
116✔
NEW
1363
                                return err
×
NEW
1364
                        }
×
1365

1366
                        // First attempt to see if the channel exists within
1367
                        // the database, if not, then we can exit early.
1368
                        chanID := chanIndex.Get(opBytes.Bytes())
116✔
1369
                        if chanID == nil {
205✔
1370
                                continue
89✔
1371
                        }
1372

1373
                        // Attempt to delete the channel, an ErrEdgeNotFound
1374
                        // will be returned if that outpoint isn't known to be
1375
                        // a channel. If no error is returned, then a channel
1376
                        // was successfully pruned.
1377
                        edgeInfo, err := c.delChannelEdgeUnsafe(
27✔
1378
                                edges, edgeIndex, chanIndex, zombieIndex,
27✔
1379
                                chanID, false, false,
27✔
1380
                        )
27✔
1381
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
27✔
NEW
1382
                                return err
×
NEW
1383
                        }
×
1384

1385
                        chansClosed = append(chansClosed, edgeInfo)
27✔
1386
                }
1387

1388
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
234✔
1389
                if err != nil {
234✔
NEW
1390
                        return err
×
NEW
1391
                }
×
1392

1393
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
234✔
1394
                        pruneLogBucket,
234✔
1395
                )
234✔
1396
                if err != nil {
234✔
NEW
1397
                        return err
×
NEW
1398
                }
×
1399

1400
                // With the graph pruned, add a new entry to the prune log,
1401
                // which can be used to check if the graph is fully synced with
1402
                // the current UTXO state.
1403
                var blockHeightBytes [4]byte
234✔
1404
                byteOrder.PutUint32(blockHeightBytes[:], blockHeight)
234✔
1405

234✔
1406
                var newTip [pruneTipBytes]byte
234✔
1407
                copy(newTip[:], blockHash[:])
234✔
1408

234✔
1409
                return pruneBucket.Put(blockHeightBytes[:], newTip[:])
234✔
1410
        }, func() {
234✔
1411
                chansClosed = nil
234✔
1412
        })
234✔
1413
        if err != nil {
234✔
NEW
1414
                return nil, err
×
NEW
1415
        }
×
1416

1417
        for _, channel := range chansClosed {
261✔
1418
                c.rejectCache.remove(channel.ChannelID)
27✔
1419
                c.chanCache.remove(channel.ChannelID)
27✔
1420
        }
27✔
1421

1422
        return chansClosed, nil
234✔
1423
}
1424

1425
// PruneGraphNodes is a garbage collection method which attempts to prune out
1426
// any nodes from the channel graph that are currently unconnected. This ensure
1427
// that we only maintain a graph of reachable nodes. In the event that a pruned
1428
// node gains more channels, it will be re-added back to the graph.
1429
func (c *KVStore) PruneGraphNodes() ([]route.Vertex, error) {
257✔
1430
        var prunedNodes []route.Vertex
257✔
1431
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
514✔
1432
                nodes := tx.ReadWriteBucket(nodeBucket)
257✔
1433
                if nodes == nil {
257✔
NEW
1434
                        return ErrGraphNodesNotFound
×
NEW
1435
                }
×
1436
                edges := tx.ReadWriteBucket(edgeBucket)
257✔
1437
                if edges == nil {
257✔
NEW
1438
                        return ErrGraphNotFound
×
NEW
1439
                }
×
1440
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
257✔
1441
                if edgeIndex == nil {
257✔
NEW
1442
                        return ErrGraphNoEdgesFound
×
NEW
1443
                }
×
1444

1445
                var err error
257✔
1446
                prunedNodes, err = c.pruneGraphNodes(nodes, edgeIndex)
257✔
1447
                if err != nil {
257✔
NEW
1448
                        return err
×
NEW
1449
                }
×
1450

1451
                return nil
257✔
1452
        }, func() {
257✔
1453
                prunedNodes = nil
257✔
1454
        })
257✔
1455

1456
        return prunedNodes, err
257✔
1457
}
1458

1459
// pruneGraphNodes attempts to remove any nodes from the graph who have had a
1460
// channel closed within the current block. If the node still has existing
1461
// channels in the graph, this will act as a no-op.
1462
func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket,
1463
        edgeIndex kvdb.RwBucket) ([]route.Vertex, error) {
257✔
1464

257✔
1465
        log.Trace("Pruning nodes from graph with no open channels")
257✔
1466

257✔
1467
        // We'll retrieve the graph's source node to ensure we don't remove it
257✔
1468
        // even if it no longer has any open channels.
257✔
1469
        sourceNode, err := c.sourceNode(nodes)
257✔
1470
        if err != nil {
257✔
NEW
1471
                return nil, err
×
NEW
1472
        }
×
1473

1474
        // We'll use this map to keep count the number of references to a node
1475
        // in the graph. A node should only be removed once it has no more
1476
        // references in the graph.
1477
        nodeRefCounts := make(map[[33]byte]int)
257✔
1478
        err = nodes.ForEach(func(pubKey, nodeBytes []byte) error {
1,507✔
1479
                // If this is the source key, then we skip this
1,250✔
1480
                // iteration as the value for this key is a pubKey
1,250✔
1481
                // rather than raw node information.
1,250✔
1482
                if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
2,015✔
1483
                        return nil
765✔
1484
                }
765✔
1485

1486
                var nodePub [33]byte
488✔
1487
                copy(nodePub[:], pubKey)
488✔
1488
                nodeRefCounts[nodePub] = 0
488✔
1489

488✔
1490
                return nil
488✔
1491
        })
1492
        if err != nil {
257✔
NEW
1493
                return nil, err
×
NEW
1494
        }
×
1495

1496
        // To ensure we never delete the source node, we'll start off by
1497
        // bumping its ref count to 1.
1498
        nodeRefCounts[sourceNode.PubKeyBytes] = 1
257✔
1499

257✔
1500
        // Next, we'll run through the edgeIndex which maps a channel ID to the
257✔
1501
        // edge info. We'll use this scan to populate our reference count map
257✔
1502
        // above.
257✔
1503
        err = edgeIndex.ForEach(func(chanID, edgeInfoBytes []byte) error {
450✔
1504
                // The first 66 bytes of the edge info contain the pubkeys of
193✔
1505
                // the nodes that this edge attaches. We'll extract them, and
193✔
1506
                // add them to the ref count map.
193✔
1507
                var node1, node2 [33]byte
193✔
1508
                copy(node1[:], edgeInfoBytes[:33])
193✔
1509
                copy(node2[:], edgeInfoBytes[33:])
193✔
1510

193✔
1511
                // With the nodes extracted, we'll increase the ref count of
193✔
1512
                // each of the nodes.
193✔
1513
                nodeRefCounts[node1]++
193✔
1514
                nodeRefCounts[node2]++
193✔
1515

193✔
1516
                return nil
193✔
1517
        })
193✔
1518
        if err != nil {
257✔
NEW
1519
                return nil, err
×
NEW
1520
        }
×
1521

1522
        // Finally, we'll make a second pass over the set of nodes, and delete
1523
        // any nodes that have a ref count of zero.
1524
        var pruned []route.Vertex
257✔
1525
        for nodePubKey, refCount := range nodeRefCounts {
745✔
1526
                // If the ref count of the node isn't zero, then we can safely
488✔
1527
                // skip it as it still has edges to or from it within the
488✔
1528
                // graph.
488✔
1529
                if refCount != 0 {
914✔
1530
                        continue
426✔
1531
                }
1532

1533
                // If we reach this point, then there are no longer any edges
1534
                // that connect this node, so we can delete it.
1535
                err := c.deleteLightningNode(nodes, nodePubKey[:])
65✔
1536
                if err != nil {
65✔
NEW
1537
                        if errors.Is(err, ErrGraphNodeNotFound) ||
×
NEW
1538
                                errors.Is(err, ErrGraphNodesNotFound) {
×
NEW
1539

×
NEW
1540
                                log.Warnf("Unable to prune node %x from the "+
×
NEW
1541
                                        "graph: %v", nodePubKey, err)
×
NEW
1542
                                continue
×
1543
                        }
1544

NEW
1545
                        return nil, err
×
1546
                }
1547

1548
                log.Infof("Pruned unconnected node %x from channel graph",
65✔
1549
                        nodePubKey[:])
65✔
1550

65✔
1551
                pruned = append(pruned, nodePubKey)
65✔
1552
        }
1553

1554
        if len(pruned) > 0 {
306✔
1555
                log.Infof("Pruned %v unconnected nodes from the channel graph",
49✔
1556
                        len(pruned))
49✔
1557
        }
49✔
1558

1559
        return pruned, err
257✔
1560
}
1561

1562
// DisconnectBlockAtHeight is used to indicate that the block specified
1563
// by the passed height has been disconnected from the main chain. This
1564
// will "rewind" the graph back to the height below, deleting channels
1565
// that are no longer confirmed from the graph. The prune log will be
1566
// set to the last prune height valid for the remaining chain.
1567
// Channels that were removed from the graph resulting from the
1568
// disconnected block are returned.
1569
func (c *KVStore) DisconnectBlockAtHeight(height uint32) (
1570
        []*models.ChannelEdgeInfo, error) {
152✔
1571

152✔
1572
        // Every channel having a ShortChannelID starting at 'height'
152✔
1573
        // will no longer be confirmed.
152✔
1574
        startShortChanID := lnwire.ShortChannelID{
152✔
1575
                BlockHeight: height,
152✔
1576
        }
152✔
1577

152✔
1578
        // Delete everything after this height from the db up until the
152✔
1579
        // SCID alias range.
152✔
1580
        endShortChanID := aliasmgr.StartingAlias
152✔
1581

152✔
1582
        // The block height will be the 3 first bytes of the channel IDs.
152✔
1583
        var chanIDStart [8]byte
152✔
1584
        byteOrder.PutUint64(chanIDStart[:], startShortChanID.ToUint64())
152✔
1585
        var chanIDEnd [8]byte
152✔
1586
        byteOrder.PutUint64(chanIDEnd[:], endShortChanID.ToUint64())
152✔
1587

152✔
1588
        c.cacheMu.Lock()
152✔
1589
        defer c.cacheMu.Unlock()
152✔
1590

152✔
1591
        // Keep track of the channels that are removed from the graph.
152✔
1592
        var removedChans []*models.ChannelEdgeInfo
152✔
1593

152✔
1594
        if err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
304✔
1595
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
152✔
1596
                if err != nil {
152✔
NEW
1597
                        return err
×
NEW
1598
                }
×
1599
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
152✔
1600
                if err != nil {
152✔
NEW
1601
                        return err
×
NEW
1602
                }
×
1603
                chanIndex, err := edges.CreateBucketIfNotExists(
152✔
1604
                        channelPointBucket,
152✔
1605
                )
152✔
1606
                if err != nil {
152✔
NEW
1607
                        return err
×
NEW
1608
                }
×
1609
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
152✔
1610
                if err != nil {
152✔
NEW
1611
                        return err
×
NEW
1612
                }
×
1613

1614
                // Scan from chanIDStart to chanIDEnd, deleting every
1615
                // found edge.
1616
                // NOTE: we must delete the edges after the cursor loop, since
1617
                // modifying the bucket while traversing is not safe.
1618
                // NOTE: We use a < comparison in bytes.Compare instead of <=
1619
                // so that the StartingAlias itself isn't deleted.
1620
                var keys [][]byte
152✔
1621
                cursor := edgeIndex.ReadWriteCursor()
152✔
1622

152✔
1623
                //nolint:ll
152✔
1624
                for k, _ := cursor.Seek(chanIDStart[:]); k != nil &&
152✔
1625
                        bytes.Compare(k, chanIDEnd[:]) < 0; k, _ = cursor.Next() {
249✔
1626
                        keys = append(keys, k)
97✔
1627
                }
97✔
1628

1629
                for _, k := range keys {
249✔
1630
                        edgeInfo, err := c.delChannelEdgeUnsafe(
97✔
1631
                                edges, edgeIndex, chanIndex, zombieIndex,
97✔
1632
                                k, false, false,
97✔
1633
                        )
97✔
1634
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
97✔
NEW
1635
                                return err
×
NEW
1636
                        }
×
1637

1638
                        removedChans = append(removedChans, edgeInfo)
97✔
1639
                }
1640

1641
                // Delete all the entries in the prune log having a height
1642
                // greater or equal to the block disconnected.
1643
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
152✔
1644
                if err != nil {
152✔
NEW
1645
                        return err
×
NEW
1646
                }
×
1647

1648
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
152✔
1649
                        pruneLogBucket,
152✔
1650
                )
152✔
1651
                if err != nil {
152✔
NEW
1652
                        return err
×
NEW
1653
                }
×
1654

1655
                var pruneKeyStart [4]byte
152✔
1656
                byteOrder.PutUint32(pruneKeyStart[:], height)
152✔
1657

152✔
1658
                var pruneKeyEnd [4]byte
152✔
1659
                byteOrder.PutUint32(pruneKeyEnd[:], math.MaxUint32)
152✔
1660

152✔
1661
                // To avoid modifying the bucket while traversing, we delete
152✔
1662
                // the keys in a second loop.
152✔
1663
                var pruneKeys [][]byte
152✔
1664
                pruneCursor := pruneBucket.ReadWriteCursor()
152✔
1665
                //nolint:ll
152✔
1666
                for k, _ := pruneCursor.Seek(pruneKeyStart[:]); k != nil &&
152✔
1667
                        bytes.Compare(k, pruneKeyEnd[:]) <= 0; k, _ = pruneCursor.Next() {
246✔
1668
                        pruneKeys = append(pruneKeys, k)
94✔
1669
                }
94✔
1670

1671
                for _, k := range pruneKeys {
246✔
1672
                        if err := pruneBucket.Delete(k); err != nil {
94✔
NEW
1673
                                return err
×
NEW
1674
                        }
×
1675
                }
1676

1677
                return nil
152✔
1678
        }, func() {
152✔
1679
                removedChans = nil
152✔
1680
        }); err != nil {
152✔
NEW
1681
                return nil, err
×
NEW
1682
        }
×
1683

1684
        for _, channel := range removedChans {
249✔
1685
                c.rejectCache.remove(channel.ChannelID)
97✔
1686
                c.chanCache.remove(channel.ChannelID)
97✔
1687
        }
97✔
1688

1689
        return removedChans, nil
152✔
1690
}
1691

1692
// PruneTip returns the block height and hash of the latest block that has been
1693
// used to prune channels in the graph. Knowing the "prune tip" allows callers
1694
// to tell if the graph is currently in sync with the current best known UTXO
1695
// state.
1696
func (c *KVStore) PruneTip() (*chainhash.Hash, uint32, error) {
56✔
1697
        var (
56✔
1698
                tipHash   chainhash.Hash
56✔
1699
                tipHeight uint32
56✔
1700
        )
56✔
1701

56✔
1702
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
112✔
1703
                graphMeta := tx.ReadBucket(graphMetaBucket)
56✔
1704
                if graphMeta == nil {
56✔
NEW
1705
                        return ErrGraphNotFound
×
NEW
1706
                }
×
1707
                pruneBucket := graphMeta.NestedReadBucket(pruneLogBucket)
56✔
1708
                if pruneBucket == nil {
56✔
NEW
1709
                        return ErrGraphNeverPruned
×
NEW
1710
                }
×
1711

1712
                pruneCursor := pruneBucket.ReadCursor()
56✔
1713

56✔
1714
                // The prune key with the largest block height will be our
56✔
1715
                // prune tip.
56✔
1716
                k, v := pruneCursor.Last()
56✔
1717
                if k == nil {
77✔
1718
                        return ErrGraphNeverPruned
21✔
1719
                }
21✔
1720

1721
                // Once we have the prune tip, the value will be the block hash,
1722
                // and the key the block height.
1723
                copy(tipHash[:], v)
38✔
1724
                tipHeight = byteOrder.Uint32(k)
38✔
1725

38✔
1726
                return nil
38✔
1727
        }, func() {})
56✔
1728
        if err != nil {
77✔
1729
                return nil, 0, err
21✔
1730
        }
21✔
1731

1732
        return &tipHash, tipHeight, nil
38✔
1733
}
1734

1735
// DeleteChannelEdges removes edges with the given channel IDs from the
1736
// database and marks them as zombies. This ensures that we're unable to re-add
1737
// it to our database once again. If an edge does not exist within the
1738
// database, then ErrEdgeNotFound will be returned. If strictZombiePruning is
1739
// true, then when we mark these edges as zombies, we'll set up the keys such
1740
// that we require the node that failed to send the fresh update to be the one
1741
// that resurrects the channel from its zombie state. The markZombie bool
1742
// denotes whether or not to mark the channel as a zombie.
1743
func (c *KVStore) DeleteChannelEdges(strictZombiePruning, markZombie bool,
1744
        chanIDs ...uint64) ([]*models.ChannelEdgeInfo, error) {
151✔
1745

151✔
1746
        // TODO(roasbeef): possibly delete from node bucket if node has no more
151✔
1747
        // channels
151✔
1748
        // TODO(roasbeef): don't delete both edges?
151✔
1749

151✔
1750
        c.cacheMu.Lock()
151✔
1751
        defer c.cacheMu.Unlock()
151✔
1752

151✔
1753
        var infos []*models.ChannelEdgeInfo
151✔
1754
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
302✔
1755
                edges := tx.ReadWriteBucket(edgeBucket)
151✔
1756
                if edges == nil {
151✔
NEW
1757
                        return ErrEdgeNotFound
×
NEW
1758
                }
×
1759
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
151✔
1760
                if edgeIndex == nil {
151✔
NEW
1761
                        return ErrEdgeNotFound
×
NEW
1762
                }
×
1763
                chanIndex := edges.NestedReadWriteBucket(channelPointBucket)
151✔
1764
                if chanIndex == nil {
151✔
NEW
1765
                        return ErrEdgeNotFound
×
NEW
1766
                }
×
1767
                nodes := tx.ReadWriteBucket(nodeBucket)
151✔
1768
                if nodes == nil {
151✔
NEW
1769
                        return ErrGraphNodeNotFound
×
NEW
1770
                }
×
1771
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
151✔
1772
                if err != nil {
151✔
NEW
1773
                        return err
×
NEW
1774
                }
×
1775

1776
                var rawChanID [8]byte
151✔
1777
                for _, chanID := range chanIDs {
244✔
1778
                        byteOrder.PutUint64(rawChanID[:], chanID)
93✔
1779
                        edgeInfo, err := c.delChannelEdgeUnsafe(
93✔
1780
                                edges, edgeIndex, chanIndex, zombieIndex,
93✔
1781
                                rawChanID[:], markZombie, strictZombiePruning,
93✔
1782
                        )
93✔
1783
                        if err != nil {
161✔
1784
                                return err
68✔
1785
                        }
68✔
1786

1787
                        infos = append(infos, edgeInfo)
25✔
1788
                }
1789

1790
                return nil
83✔
1791
        }, func() {
151✔
1792
                infos = nil
151✔
1793
        })
151✔
1794
        if err != nil {
219✔
1795
                return nil, err
68✔
1796
        }
68✔
1797

1798
        for _, chanID := range chanIDs {
108✔
1799
                c.rejectCache.remove(chanID)
25✔
1800
                c.chanCache.remove(chanID)
25✔
1801
        }
25✔
1802

1803
        return infos, nil
83✔
1804
}
1805

1806
// ChannelID attempt to lookup the 8-byte compact channel ID which maps to the
1807
// passed channel point (outpoint). If the passed channel doesn't exist within
1808
// the database, then ErrEdgeNotFound is returned.
1809
func (c *KVStore) ChannelID(chanPoint *wire.OutPoint) (uint64, error) {
4✔
1810
        var chanID uint64
4✔
1811
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
8✔
1812
                var err error
4✔
1813
                chanID, err = getChanID(tx, chanPoint)
4✔
1814
                return err
4✔
1815
        }, func() {
8✔
1816
                chanID = 0
4✔
1817
        }); err != nil {
7✔
1818
                return 0, err
3✔
1819
        }
3✔
1820

1821
        return chanID, nil
4✔
1822
}
1823

1824
// getChanID returns the assigned channel ID for a given channel point.
1825
func getChanID(tx kvdb.RTx, chanPoint *wire.OutPoint) (uint64, error) {
4✔
1826
        var b bytes.Buffer
4✔
1827
        if err := WriteOutpoint(&b, chanPoint); err != nil {
4✔
NEW
1828
                return 0, err
×
NEW
1829
        }
×
1830

1831
        edges := tx.ReadBucket(edgeBucket)
4✔
1832
        if edges == nil {
4✔
NEW
1833
                return 0, ErrGraphNoEdgesFound
×
NEW
1834
        }
×
1835
        chanIndex := edges.NestedReadBucket(channelPointBucket)
4✔
1836
        if chanIndex == nil {
4✔
NEW
1837
                return 0, ErrGraphNoEdgesFound
×
NEW
1838
        }
×
1839

1840
        chanIDBytes := chanIndex.Get(b.Bytes())
4✔
1841
        if chanIDBytes == nil {
7✔
1842
                return 0, ErrEdgeNotFound
3✔
1843
        }
3✔
1844

1845
        chanID := byteOrder.Uint64(chanIDBytes)
4✔
1846

4✔
1847
        return chanID, nil
4✔
1848
}
1849

1850
// TODO(roasbeef): allow updates to use Batch?
1851

1852
// HighestChanID returns the "highest" known channel ID in the channel graph.
1853
// This represents the "newest" channel from the PoV of the chain. This method
1854
// can be used by peers to quickly determine if they're graphs are in sync.
1855
func (c *KVStore) HighestChanID() (uint64, error) {
6✔
1856
        var cid uint64
6✔
1857

6✔
1858
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
12✔
1859
                edges := tx.ReadBucket(edgeBucket)
6✔
1860
                if edges == nil {
6✔
NEW
1861
                        return ErrGraphNoEdgesFound
×
NEW
1862
                }
×
1863
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
6✔
1864
                if edgeIndex == nil {
6✔
NEW
1865
                        return ErrGraphNoEdgesFound
×
NEW
1866
                }
×
1867

1868
                // In order to find the highest chan ID, we'll fetch a cursor
1869
                // and use that to seek to the "end" of our known rage.
1870
                cidCursor := edgeIndex.ReadCursor()
6✔
1871

6✔
1872
                lastChanID, _ := cidCursor.Last()
6✔
1873

6✔
1874
                // If there's no key, then this means that we don't actually
6✔
1875
                // know of any channels, so we'll return a predicable error.
6✔
1876
                if lastChanID == nil {
10✔
1877
                        return ErrGraphNoEdgesFound
4✔
1878
                }
4✔
1879

1880
                // Otherwise, we'll de serialize the channel ID and return it
1881
                // to the caller.
1882
                cid = byteOrder.Uint64(lastChanID)
5✔
1883

5✔
1884
                return nil
5✔
1885
        }, func() {
6✔
1886
                cid = 0
6✔
1887
        })
6✔
1888
        if err != nil && !errors.Is(err, ErrGraphNoEdgesFound) {
6✔
NEW
1889
                return 0, err
×
NEW
1890
        }
×
1891

1892
        return cid, nil
6✔
1893
}
1894

1895
// ChannelEdge represents the complete set of information for a channel edge in
1896
// the known channel graph. This struct couples the core information of the
1897
// edge as well as each of the known advertised edge policies.
1898
type ChannelEdge struct {
1899
        // Info contains all the static information describing the channel.
1900
        Info *models.ChannelEdgeInfo
1901

1902
        // Policy1 points to the "first" edge policy of the channel containing
1903
        // the dynamic information required to properly route through the edge.
1904
        Policy1 *models.ChannelEdgePolicy
1905

1906
        // Policy2 points to the "second" edge policy of the channel containing
1907
        // the dynamic information required to properly route through the edge.
1908
        Policy2 *models.ChannelEdgePolicy
1909

1910
        // Node1 is "node 1" in the channel. This is the node that would have
1911
        // produced Policy1 if it exists.
1912
        Node1 *models.LightningNode
1913

1914
        // Node2 is "node 2" in the channel. This is the node that would have
1915
        // produced Policy2 if it exists.
1916
        Node2 *models.LightningNode
1917
}
1918

1919
// ChanUpdatesInHorizon returns all the known channel edges which have at least
1920
// one edge that has an update timestamp within the specified horizon.
1921
func (c *KVStore) ChanUpdatesInHorizon(startTime,
1922
        endTime time.Time) ([]ChannelEdge, error) {
142✔
1923

142✔
1924
        // To ensure we don't return duplicate ChannelEdges, we'll use an
142✔
1925
        // additional map to keep track of the edges already seen to prevent
142✔
1926
        // re-adding it.
142✔
1927
        var edgesSeen map[uint64]struct{}
142✔
1928
        var edgesToCache map[uint64]ChannelEdge
142✔
1929
        var edgesInHorizon []ChannelEdge
142✔
1930

142✔
1931
        c.cacheMu.Lock()
142✔
1932
        defer c.cacheMu.Unlock()
142✔
1933

142✔
1934
        var hits int
142✔
1935
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
284✔
1936
                edges := tx.ReadBucket(edgeBucket)
142✔
1937
                if edges == nil {
142✔
NEW
1938
                        return ErrGraphNoEdgesFound
×
NEW
1939
                }
×
1940
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
142✔
1941
                if edgeIndex == nil {
142✔
NEW
1942
                        return ErrGraphNoEdgesFound
×
NEW
1943
                }
×
1944
                edgeUpdateIndex := edges.NestedReadBucket(edgeUpdateIndexBucket)
142✔
1945
                if edgeUpdateIndex == nil {
142✔
NEW
1946
                        return ErrGraphNoEdgesFound
×
NEW
1947
                }
×
1948

1949
                nodes := tx.ReadBucket(nodeBucket)
142✔
1950
                if nodes == nil {
142✔
NEW
1951
                        return ErrGraphNodesNotFound
×
NEW
1952
                }
×
1953

1954
                // We'll now obtain a cursor to perform a range query within
1955
                // the index to find all channels within the horizon.
1956
                updateCursor := edgeUpdateIndex.ReadCursor()
142✔
1957

142✔
1958
                var startTimeBytes, endTimeBytes [8 + 8]byte
142✔
1959
                byteOrder.PutUint64(
142✔
1960
                        startTimeBytes[:8], uint64(startTime.Unix()),
142✔
1961
                )
142✔
1962
                byteOrder.PutUint64(
142✔
1963
                        endTimeBytes[:8], uint64(endTime.Unix()),
142✔
1964
                )
142✔
1965

142✔
1966
                // With our start and end times constructed, we'll step through
142✔
1967
                // the index collecting the info and policy of each update of
142✔
1968
                // each channel that has a last update within the time range.
142✔
1969
                //
142✔
1970
                //nolint:ll
142✔
1971
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
142✔
1972
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
191✔
1973
                        // We have a new eligible entry, so we'll slice of the
49✔
1974
                        // chan ID so we can query it in the DB.
49✔
1975
                        chanID := indexKey[8:]
49✔
1976

49✔
1977
                        // If we've already retrieved the info and policies for
49✔
1978
                        // this edge, then we can skip it as we don't need to do
49✔
1979
                        // so again.
49✔
1980
                        chanIDInt := byteOrder.Uint64(chanID)
49✔
1981
                        if _, ok := edgesSeen[chanIDInt]; ok {
68✔
1982
                                continue
19✔
1983
                        }
1984

1985
                        if channel, ok := c.chanCache.get(chanIDInt); ok {
42✔
1986
                                hits++
12✔
1987
                                edgesSeen[chanIDInt] = struct{}{}
12✔
1988
                                edgesInHorizon = append(edgesInHorizon, channel)
12✔
1989

12✔
1990
                                continue
12✔
1991
                        }
1992

1993
                        // First, we'll fetch the static edge information.
1994
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
21✔
1995
                        if err != nil {
21✔
NEW
1996
                                chanID := byteOrder.Uint64(chanID)
×
NEW
1997
                                return fmt.Errorf("unable to fetch info for "+
×
NEW
1998
                                        "edge with chan_id=%v: %v", chanID, err)
×
NEW
1999
                        }
×
2000

2001
                        // With the static information obtained, we'll now
2002
                        // fetch the dynamic policy info.
2003
                        edge1, edge2, err := fetchChanEdgePolicies(
21✔
2004
                                edgeIndex, edges, chanID,
21✔
2005
                        )
21✔
2006
                        if err != nil {
21✔
NEW
2007
                                chanID := byteOrder.Uint64(chanID)
×
NEW
2008
                                return fmt.Errorf("unable to fetch policies "+
×
NEW
2009
                                        "for edge with chan_id=%v: %v", chanID,
×
NEW
2010
                                        err)
×
NEW
2011
                        }
×
2012

2013
                        node1, err := fetchLightningNode(
21✔
2014
                                nodes, edgeInfo.NodeKey1Bytes[:],
21✔
2015
                        )
21✔
2016
                        if err != nil {
21✔
NEW
2017
                                return err
×
NEW
2018
                        }
×
2019

2020
                        node2, err := fetchLightningNode(
21✔
2021
                                nodes, edgeInfo.NodeKey2Bytes[:],
21✔
2022
                        )
21✔
2023
                        if err != nil {
21✔
NEW
2024
                                return err
×
NEW
2025
                        }
×
2026

2027
                        // Finally, we'll collate this edge with the rest of
2028
                        // edges to be returned.
2029
                        edgesSeen[chanIDInt] = struct{}{}
21✔
2030
                        channel := ChannelEdge{
21✔
2031
                                Info:    &edgeInfo,
21✔
2032
                                Policy1: edge1,
21✔
2033
                                Policy2: edge2,
21✔
2034
                                Node1:   &node1,
21✔
2035
                                Node2:   &node2,
21✔
2036
                        }
21✔
2037
                        edgesInHorizon = append(edgesInHorizon, channel)
21✔
2038
                        edgesToCache[chanIDInt] = channel
21✔
2039
                }
2040

2041
                return nil
142✔
2042
        }, func() {
142✔
2043
                edgesSeen = make(map[uint64]struct{})
142✔
2044
                edgesToCache = make(map[uint64]ChannelEdge)
142✔
2045
                edgesInHorizon = nil
142✔
2046
        })
142✔
2047
        switch {
142✔
NEW
2048
        case errors.Is(err, ErrGraphNoEdgesFound):
×
NEW
2049
                fallthrough
×
NEW
2050
        case errors.Is(err, ErrGraphNodesNotFound):
×
NEW
2051
                break
×
2052

NEW
2053
        case err != nil:
×
NEW
2054
                return nil, err
×
2055
        }
2056

2057
        // Insert any edges loaded from disk into the cache.
2058
        for chanid, channel := range edgesToCache {
163✔
2059
                c.chanCache.insert(chanid, channel)
21✔
2060
        }
21✔
2061

2062
        log.Debugf("ChanUpdatesInHorizon hit percentage: %f (%d/%d)",
142✔
2063
                float64(hits)/float64(len(edgesInHorizon)), hits,
142✔
2064
                len(edgesInHorizon))
142✔
2065

142✔
2066
        return edgesInHorizon, nil
142✔
2067
}
2068

2069
// NodeUpdatesInHorizon returns all the known lightning node which have an
2070
// update timestamp within the passed range. This method can be used by two
2071
// nodes to quickly determine if they have the same set of up to date node
2072
// announcements.
2073
func (c *KVStore) NodeUpdatesInHorizon(startTime,
2074
        endTime time.Time) ([]models.LightningNode, error) {
11✔
2075

11✔
2076
        var nodesInHorizon []models.LightningNode
11✔
2077

11✔
2078
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
22✔
2079
                nodes := tx.ReadBucket(nodeBucket)
11✔
2080
                if nodes == nil {
11✔
NEW
2081
                        return ErrGraphNodesNotFound
×
NEW
2082
                }
×
2083

2084
                nodeUpdateIndex := nodes.NestedReadBucket(nodeUpdateIndexBucket)
11✔
2085
                if nodeUpdateIndex == nil {
11✔
NEW
2086
                        return ErrGraphNodesNotFound
×
NEW
2087
                }
×
2088

2089
                // We'll now obtain a cursor to perform a range query within
2090
                // the index to find all node announcements within the horizon.
2091
                updateCursor := nodeUpdateIndex.ReadCursor()
11✔
2092

11✔
2093
                var startTimeBytes, endTimeBytes [8 + 33]byte
11✔
2094
                byteOrder.PutUint64(
11✔
2095
                        startTimeBytes[:8], uint64(startTime.Unix()),
11✔
2096
                )
11✔
2097
                byteOrder.PutUint64(
11✔
2098
                        endTimeBytes[:8], uint64(endTime.Unix()),
11✔
2099
                )
11✔
2100

11✔
2101
                // With our start and end times constructed, we'll step through
11✔
2102
                // the index collecting info for each node within the time
11✔
2103
                // range.
11✔
2104
                //
11✔
2105
                //nolint:ll
11✔
2106
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
11✔
2107
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
43✔
2108
                        nodePub := indexKey[8:]
32✔
2109
                        node, err := fetchLightningNode(nodes, nodePub)
32✔
2110
                        if err != nil {
32✔
NEW
2111
                                return err
×
NEW
2112
                        }
×
2113

2114
                        nodesInHorizon = append(nodesInHorizon, node)
32✔
2115
                }
2116

2117
                return nil
11✔
2118
        }, func() {
11✔
2119
                nodesInHorizon = nil
11✔
2120
        })
11✔
2121
        switch {
11✔
NEW
2122
        case errors.Is(err, ErrGraphNoEdgesFound):
×
NEW
2123
                fallthrough
×
NEW
2124
        case errors.Is(err, ErrGraphNodesNotFound):
×
NEW
2125
                break
×
2126

NEW
2127
        case err != nil:
×
NEW
2128
                return nil, err
×
2129
        }
2130

2131
        return nodesInHorizon, nil
11✔
2132
}
2133

2134
// FilterKnownChanIDs takes a set of channel IDs and return the subset of chan
2135
// ID's that we don't know and are not known zombies of the passed set. In other
2136
// words, we perform a set difference of our set of chan ID's and the ones
2137
// passed in. This method can be used by callers to determine the set of
2138
// channels another peer knows of that we don't. The ChannelUpdateInfos for the
2139
// known zombies is also returned.
2140
func (c *KVStore) FilterKnownChanIDs(chansInfo []ChannelUpdateInfo) ([]uint64,
2141
        []ChannelUpdateInfo, error) {
130✔
2142

130✔
2143
        var (
130✔
2144
                newChanIDs   []uint64
130✔
2145
                knownZombies []ChannelUpdateInfo
130✔
2146
        )
130✔
2147

130✔
2148
        c.cacheMu.Lock()
130✔
2149
        defer c.cacheMu.Unlock()
130✔
2150

130✔
2151
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
260✔
2152
                edges := tx.ReadBucket(edgeBucket)
130✔
2153
                if edges == nil {
130✔
NEW
2154
                        return ErrGraphNoEdgesFound
×
NEW
2155
                }
×
2156
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
130✔
2157
                if edgeIndex == nil {
130✔
NEW
2158
                        return ErrGraphNoEdgesFound
×
NEW
2159
                }
×
2160

2161
                // Fetch the zombie index, it may not exist if no edges have
2162
                // ever been marked as zombies. If the index has been
2163
                // initialized, we will use it later to skip known zombie edges.
2164
                zombieIndex := edges.NestedReadBucket(zombieBucket)
130✔
2165

130✔
2166
                // We'll run through the set of chanIDs and collate only the
130✔
2167
                // set of channel that are unable to be found within our db.
130✔
2168
                var cidBytes [8]byte
130✔
2169
                for _, info := range chansInfo {
241✔
2170
                        scid := info.ShortChannelID.ToUint64()
111✔
2171
                        byteOrder.PutUint64(cidBytes[:], scid)
111✔
2172

111✔
2173
                        // If the edge is already known, skip it.
111✔
2174
                        if v := edgeIndex.Get(cidBytes[:]); v != nil {
134✔
2175
                                continue
23✔
2176
                        }
2177

2178
                        // If the edge is a known zombie, skip it.
2179
                        if zombieIndex != nil {
182✔
2180
                                isZombie, _, _ := isZombieEdge(
91✔
2181
                                        zombieIndex, scid,
91✔
2182
                                )
91✔
2183

91✔
2184
                                if isZombie {
131✔
2185
                                        knownZombies = append(
40✔
2186
                                                knownZombies, info,
40✔
2187
                                        )
40✔
2188

40✔
2189
                                        continue
40✔
2190
                                }
2191
                        }
2192

2193
                        newChanIDs = append(newChanIDs, scid)
51✔
2194
                }
2195

2196
                return nil
130✔
2197
        }, func() {
130✔
2198
                newChanIDs = nil
130✔
2199
                knownZombies = nil
130✔
2200
        })
130✔
2201
        switch {
130✔
2202
        // If we don't know of any edges yet, then we'll return the entire set
2203
        // of chan IDs specified.
NEW
2204
        case errors.Is(err, ErrGraphNoEdgesFound):
×
NEW
2205
                ogChanIDs := make([]uint64, len(chansInfo))
×
NEW
2206
                for i, info := range chansInfo {
×
NEW
2207
                        ogChanIDs[i] = info.ShortChannelID.ToUint64()
×
NEW
2208
                }
×
2209

NEW
2210
                return ogChanIDs, nil, nil
×
2211

NEW
2212
        case err != nil:
×
NEW
2213
                return nil, nil, err
×
2214
        }
2215

2216
        return newChanIDs, knownZombies, nil
130✔
2217
}
2218

2219
// ChannelUpdateInfo couples the SCID of a channel with the timestamps of the
2220
// latest received channel updates for the channel.
2221
type ChannelUpdateInfo struct {
2222
        // ShortChannelID is the SCID identifier of the channel.
2223
        ShortChannelID lnwire.ShortChannelID
2224

2225
        // Node1UpdateTimestamp is the timestamp of the latest received update
2226
        // from the node 1 channel peer. This will be set to zero time if no
2227
        // update has yet been received from this node.
2228
        Node1UpdateTimestamp time.Time
2229

2230
        // Node2UpdateTimestamp is the timestamp of the latest received update
2231
        // from the node 2 channel peer. This will be set to zero time if no
2232
        // update has yet been received from this node.
2233
        Node2UpdateTimestamp time.Time
2234
}
2235

2236
// NewChannelUpdateInfo is a constructor which makes sure we initialize the
2237
// timestamps with zero seconds unix timestamp which equals
2238
// `January 1, 1970, 00:00:00 UTC` in case the value is `time.Time{}`.
2239
func NewChannelUpdateInfo(scid lnwire.ShortChannelID, node1Timestamp,
2240
        node2Timestamp time.Time) ChannelUpdateInfo {
221✔
2241

221✔
2242
        chanInfo := ChannelUpdateInfo{
221✔
2243
                ShortChannelID:       scid,
221✔
2244
                Node1UpdateTimestamp: node1Timestamp,
221✔
2245
                Node2UpdateTimestamp: node2Timestamp,
221✔
2246
        }
221✔
2247

221✔
2248
        if node1Timestamp.IsZero() {
432✔
2249
                chanInfo.Node1UpdateTimestamp = time.Unix(0, 0)
211✔
2250
        }
211✔
2251

2252
        if node2Timestamp.IsZero() {
432✔
2253
                chanInfo.Node2UpdateTimestamp = time.Unix(0, 0)
211✔
2254
        }
211✔
2255

2256
        return chanInfo
221✔
2257
}
2258

2259
// BlockChannelRange represents a range of channels for a given block height.
2260
type BlockChannelRange struct {
2261
        // Height is the height of the block all of the channels below were
2262
        // included in.
2263
        Height uint32
2264

2265
        // Channels is the list of channels identified by their short ID
2266
        // representation known to us that were included in the block height
2267
        // above. The list may include channel update timestamp information if
2268
        // requested.
2269
        Channels []ChannelUpdateInfo
2270
}
2271

2272
// FilterChannelRange returns the channel ID's of all known channels which were
2273
// mined in a block height within the passed range. The channel IDs are grouped
2274
// by their common block height. This method can be used to quickly share with a
2275
// peer the set of channels we know of within a particular range to catch them
2276
// up after a period of time offline. If withTimestamps is true then the
2277
// timestamp info of the latest received channel update messages of the channel
2278
// will be included in the response.
2279
func (c *KVStore) FilterChannelRange(startHeight,
2280
        endHeight uint32, withTimestamps bool) ([]BlockChannelRange, error) {
14✔
2281

14✔
2282
        startChanID := &lnwire.ShortChannelID{
14✔
2283
                BlockHeight: startHeight,
14✔
2284
        }
14✔
2285

14✔
2286
        endChanID := lnwire.ShortChannelID{
14✔
2287
                BlockHeight: endHeight,
14✔
2288
                TxIndex:     math.MaxUint32 & 0x00ffffff,
14✔
2289
                TxPosition:  math.MaxUint16,
14✔
2290
        }
14✔
2291

14✔
2292
        // As we need to perform a range scan, we'll convert the starting and
14✔
2293
        // ending height to their corresponding values when encoded using short
14✔
2294
        // channel ID's.
14✔
2295
        var chanIDStart, chanIDEnd [8]byte
14✔
2296
        byteOrder.PutUint64(chanIDStart[:], startChanID.ToUint64())
14✔
2297
        byteOrder.PutUint64(chanIDEnd[:], endChanID.ToUint64())
14✔
2298

14✔
2299
        var channelsPerBlock map[uint32][]ChannelUpdateInfo
14✔
2300
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
28✔
2301
                edges := tx.ReadBucket(edgeBucket)
14✔
2302
                if edges == nil {
14✔
NEW
2303
                        return ErrGraphNoEdgesFound
×
NEW
2304
                }
×
2305
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
14✔
2306
                if edgeIndex == nil {
14✔
NEW
2307
                        return ErrGraphNoEdgesFound
×
NEW
2308
                }
×
2309

2310
                cursor := edgeIndex.ReadCursor()
14✔
2311

14✔
2312
                // We'll now iterate through the database, and find each
14✔
2313
                // channel ID that resides within the specified range.
14✔
2314
                //
14✔
2315
                //nolint:ll
14✔
2316
                for k, v := cursor.Seek(chanIDStart[:]); k != nil &&
14✔
2317
                        bytes.Compare(k, chanIDEnd[:]) <= 0; k, v = cursor.Next() {
61✔
2318
                        // Don't send alias SCIDs during gossip sync.
47✔
2319
                        edgeReader := bytes.NewReader(v)
47✔
2320
                        edgeInfo, err := deserializeChanEdgeInfo(edgeReader)
47✔
2321
                        if err != nil {
47✔
NEW
2322
                                return err
×
NEW
2323
                        }
×
2324

2325
                        if edgeInfo.AuthProof == nil {
50✔
2326
                                continue
3✔
2327
                        }
2328

2329
                        // This channel ID rests within the target range, so
2330
                        // we'll add it to our returned set.
2331
                        rawCid := byteOrder.Uint64(k)
47✔
2332
                        cid := lnwire.NewShortChanIDFromInt(rawCid)
47✔
2333

47✔
2334
                        chanInfo := NewChannelUpdateInfo(
47✔
2335
                                cid, time.Time{}, time.Time{},
47✔
2336
                        )
47✔
2337

47✔
2338
                        if !withTimestamps {
69✔
2339
                                channelsPerBlock[cid.BlockHeight] = append(
22✔
2340
                                        channelsPerBlock[cid.BlockHeight],
22✔
2341
                                        chanInfo,
22✔
2342
                                )
22✔
2343

22✔
2344
                                continue
22✔
2345
                        }
2346

2347
                        node1Key, node2Key := computeEdgePolicyKeys(&edgeInfo)
25✔
2348

25✔
2349
                        rawPolicy := edges.Get(node1Key)
25✔
2350
                        if len(rawPolicy) != 0 {
34✔
2351
                                r := bytes.NewReader(rawPolicy)
9✔
2352

9✔
2353
                                edge, err := deserializeChanEdgePolicyRaw(r)
9✔
2354
                                if err != nil && !errors.Is(
9✔
2355
                                        err, ErrEdgePolicyOptionalFieldNotFound,
9✔
2356
                                ) {
9✔
NEW
2357

×
NEW
2358
                                        return err
×
NEW
2359
                                }
×
2360

2361
                                chanInfo.Node1UpdateTimestamp = edge.LastUpdate
9✔
2362
                        }
2363

2364
                        rawPolicy = edges.Get(node2Key)
25✔
2365
                        if len(rawPolicy) != 0 {
39✔
2366
                                r := bytes.NewReader(rawPolicy)
14✔
2367

14✔
2368
                                edge, err := deserializeChanEdgePolicyRaw(r)
14✔
2369
                                if err != nil && !errors.Is(
14✔
2370
                                        err, ErrEdgePolicyOptionalFieldNotFound,
14✔
2371
                                ) {
14✔
NEW
2372

×
NEW
2373
                                        return err
×
NEW
2374
                                }
×
2375

2376
                                chanInfo.Node2UpdateTimestamp = edge.LastUpdate
14✔
2377
                        }
2378

2379
                        channelsPerBlock[cid.BlockHeight] = append(
25✔
2380
                                channelsPerBlock[cid.BlockHeight], chanInfo,
25✔
2381
                        )
25✔
2382
                }
2383

2384
                return nil
14✔
2385
        }, func() {
14✔
2386
                channelsPerBlock = make(map[uint32][]ChannelUpdateInfo)
14✔
2387
        })
14✔
2388

2389
        switch {
14✔
2390
        // If we don't know of any channels yet, then there's nothing to
2391
        // filter, so we'll return an empty slice.
2392
        case errors.Is(err, ErrGraphNoEdgesFound) || len(channelsPerBlock) == 0:
6✔
2393
                return nil, nil
6✔
2394

NEW
2395
        case err != nil:
×
NEW
2396
                return nil, err
×
2397
        }
2398

2399
        // Return the channel ranges in ascending block height order.
2400
        blocks := make([]uint32, 0, len(channelsPerBlock))
11✔
2401
        for block := range channelsPerBlock {
36✔
2402
                blocks = append(blocks, block)
25✔
2403
        }
25✔
2404
        sort.Slice(blocks, func(i, j int) bool {
34✔
2405
                return blocks[i] < blocks[j]
23✔
2406
        })
23✔
2407

2408
        channelRanges := make([]BlockChannelRange, 0, len(channelsPerBlock))
11✔
2409
        for _, block := range blocks {
36✔
2410
                channelRanges = append(channelRanges, BlockChannelRange{
25✔
2411
                        Height:   block,
25✔
2412
                        Channels: channelsPerBlock[block],
25✔
2413
                })
25✔
2414
        }
25✔
2415

2416
        return channelRanges, nil
11✔
2417
}
2418

2419
// FetchChanInfos returns the set of channel edges that correspond to the passed
2420
// channel ID's. If an edge is the query is unknown to the database, it will
2421
// skipped and the result will contain only those edges that exist at the time
2422
// of the query. This can be used to respond to peer queries that are seeking to
2423
// fill in gaps in their view of the channel graph.
2424
func (c *KVStore) FetchChanInfos(chanIDs []uint64) ([]ChannelEdge, error) {
7✔
2425
        return c.fetchChanInfos(nil, chanIDs)
7✔
2426
}
7✔
2427

2428
// fetchChanInfos returns the set of channel edges that correspond to the passed
2429
// channel ID's. If an edge is the query is unknown to the database, it will
2430
// skipped and the result will contain only those edges that exist at the time
2431
// of the query. This can be used to respond to peer queries that are seeking to
2432
// fill in gaps in their view of the channel graph.
2433
//
2434
// NOTE: An optional transaction may be provided. If none is provided, then a
2435
// new one will be created.
2436
func (c *KVStore) fetchChanInfos(tx kvdb.RTx, chanIDs []uint64) (
2437
        []ChannelEdge, error) {
7✔
2438
        // TODO(roasbeef): sort cids?
7✔
2439

7✔
2440
        var (
7✔
2441
                chanEdges []ChannelEdge
7✔
2442
                cidBytes  [8]byte
7✔
2443
        )
7✔
2444

7✔
2445
        fetchChanInfos := func(tx kvdb.RTx) error {
14✔
2446
                edges := tx.ReadBucket(edgeBucket)
7✔
2447
                if edges == nil {
7✔
NEW
2448
                        return ErrGraphNoEdgesFound
×
NEW
2449
                }
×
2450
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
7✔
2451
                if edgeIndex == nil {
7✔
NEW
2452
                        return ErrGraphNoEdgesFound
×
NEW
2453
                }
×
2454
                nodes := tx.ReadBucket(nodeBucket)
7✔
2455
                if nodes == nil {
7✔
NEW
2456
                        return ErrGraphNotFound
×
NEW
2457
                }
×
2458

2459
                for _, cid := range chanIDs {
21✔
2460
                        byteOrder.PutUint64(cidBytes[:], cid)
14✔
2461

14✔
2462
                        // First, we'll fetch the static edge information. If
14✔
2463
                        // the edge is unknown, we will skip the edge and
14✔
2464
                        // continue gathering all known edges.
14✔
2465
                        edgeInfo, err := fetchChanEdgeInfo(
14✔
2466
                                edgeIndex, cidBytes[:],
14✔
2467
                        )
14✔
2468
                        switch {
14✔
2469
                        case errors.Is(err, ErrEdgeNotFound):
3✔
2470
                                continue
3✔
NEW
2471
                        case err != nil:
×
NEW
2472
                                return err
×
2473
                        }
2474

2475
                        // With the static information obtained, we'll now
2476
                        // fetch the dynamic policy info.
2477
                        edge1, edge2, err := fetchChanEdgePolicies(
11✔
2478
                                edgeIndex, edges, cidBytes[:],
11✔
2479
                        )
11✔
2480
                        if err != nil {
11✔
NEW
2481
                                return err
×
NEW
2482
                        }
×
2483

2484
                        node1, err := fetchLightningNode(
11✔
2485
                                nodes, edgeInfo.NodeKey1Bytes[:],
11✔
2486
                        )
11✔
2487
                        if err != nil {
11✔
NEW
2488
                                return err
×
NEW
2489
                        }
×
2490

2491
                        node2, err := fetchLightningNode(
11✔
2492
                                nodes, edgeInfo.NodeKey2Bytes[:],
11✔
2493
                        )
11✔
2494
                        if err != nil {
11✔
NEW
2495
                                return err
×
NEW
2496
                        }
×
2497

2498
                        chanEdges = append(chanEdges, ChannelEdge{
11✔
2499
                                Info:    &edgeInfo,
11✔
2500
                                Policy1: edge1,
11✔
2501
                                Policy2: edge2,
11✔
2502
                                Node1:   &node1,
11✔
2503
                                Node2:   &node2,
11✔
2504
                        })
11✔
2505
                }
2506

2507
                return nil
7✔
2508
        }
2509

2510
        if tx == nil {
14✔
2511
                err := kvdb.View(c.db, fetchChanInfos, func() {
14✔
2512
                        chanEdges = nil
7✔
2513
                })
7✔
2514
                if err != nil {
7✔
NEW
2515
                        return nil, err
×
NEW
2516
                }
×
2517

2518
                return chanEdges, nil
7✔
2519
        }
2520

NEW
2521
        err := fetchChanInfos(tx)
×
NEW
2522
        if err != nil {
×
NEW
2523
                return nil, err
×
NEW
2524
        }
×
2525

NEW
2526
        return chanEdges, nil
×
2527
}
2528

2529
func delEdgeUpdateIndexEntry(edgesBucket kvdb.RwBucket, chanID uint64,
2530
        edge1, edge2 *models.ChannelEdgePolicy) error {
144✔
2531

144✔
2532
        // First, we'll fetch the edge update index bucket which currently
144✔
2533
        // stores an entry for the channel we're about to delete.
144✔
2534
        updateIndex := edgesBucket.NestedReadWriteBucket(edgeUpdateIndexBucket)
144✔
2535
        if updateIndex == nil {
144✔
NEW
2536
                // No edges in bucket, return early.
×
NEW
2537
                return nil
×
NEW
2538
        }
×
2539

2540
        // Now that we have the bucket, we'll attempt to construct a template
2541
        // for the index key: updateTime || chanid.
2542
        var indexKey [8 + 8]byte
144✔
2543
        byteOrder.PutUint64(indexKey[8:], chanID)
144✔
2544

144✔
2545
        // With the template constructed, we'll attempt to delete an entry that
144✔
2546
        // would have been created by both edges: we'll alternate the update
144✔
2547
        // times, as one may had overridden the other.
144✔
2548
        if edge1 != nil {
157✔
2549
                byteOrder.PutUint64(
13✔
2550
                        indexKey[:8], uint64(edge1.LastUpdate.Unix()),
13✔
2551
                )
13✔
2552
                if err := updateIndex.Delete(indexKey[:]); err != nil {
13✔
NEW
2553
                        return err
×
NEW
2554
                }
×
2555
        }
2556

2557
        // We'll also attempt to delete the entry that may have been created by
2558
        // the second edge.
2559
        if edge2 != nil {
159✔
2560
                byteOrder.PutUint64(
15✔
2561
                        indexKey[:8], uint64(edge2.LastUpdate.Unix()),
15✔
2562
                )
15✔
2563
                if err := updateIndex.Delete(indexKey[:]); err != nil {
15✔
NEW
2564
                        return err
×
NEW
2565
                }
×
2566
        }
2567

2568
        return nil
144✔
2569
}
2570

2571
// delChannelEdgeUnsafe deletes the edge with the given chanID from the graph
2572
// cache. It then goes on to delete any policy info and edge info for this
2573
// channel from the DB and finally, if isZombie is true, it will add an entry
2574
// for this channel in the zombie index.
2575
//
2576
// NOTE: this method MUST only be called if the cacheMu has already been
2577
// acquired.
2578
func (c *KVStore) delChannelEdgeUnsafe(edges, edgeIndex, chanIndex,
2579
        zombieIndex kvdb.RwBucket, chanID []byte, isZombie,
2580
        strictZombie bool) (*models.ChannelEdgeInfo, error) {
212✔
2581

212✔
2582
        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
212✔
2583
        if err != nil {
280✔
2584
                return nil, err
68✔
2585
        }
68✔
2586

2587
        // We'll also remove the entry in the edge update index bucket before
2588
        // we delete the edges themselves so we can access their last update
2589
        // times.
2590
        cid := byteOrder.Uint64(chanID)
144✔
2591
        edge1, edge2, err := fetchChanEdgePolicies(edgeIndex, edges, chanID)
144✔
2592
        if err != nil {
144✔
NEW
2593
                return nil, err
×
NEW
2594
        }
×
2595
        err = delEdgeUpdateIndexEntry(edges, cid, edge1, edge2)
144✔
2596
        if err != nil {
144✔
NEW
2597
                return nil, err
×
NEW
2598
        }
×
2599

2600
        // The edge key is of the format pubKey || chanID. First we construct
2601
        // the latter half, populating the channel ID.
2602
        var edgeKey [33 + 8]byte
144✔
2603
        copy(edgeKey[33:], chanID)
144✔
2604

144✔
2605
        // With the latter half constructed, copy over the first public key to
144✔
2606
        // delete the edge in this direction, then the second to delete the
144✔
2607
        // edge in the opposite direction.
144✔
2608
        copy(edgeKey[:33], edgeInfo.NodeKey1Bytes[:])
144✔
2609
        if edges.Get(edgeKey[:]) != nil {
288✔
2610
                if err := edges.Delete(edgeKey[:]); err != nil {
144✔
NEW
2611
                        return nil, err
×
NEW
2612
                }
×
2613
        }
2614
        copy(edgeKey[:33], edgeInfo.NodeKey2Bytes[:])
144✔
2615
        if edges.Get(edgeKey[:]) != nil {
288✔
2616
                if err := edges.Delete(edgeKey[:]); err != nil {
144✔
NEW
2617
                        return nil, err
×
NEW
2618
                }
×
2619
        }
2620

2621
        // As part of deleting the edge we also remove all disabled entries
2622
        // from the edgePolicyDisabledIndex bucket. We do that for both
2623
        // directions.
2624
        err = updateEdgePolicyDisabledIndex(edges, cid, false, false)
144✔
2625
        if err != nil {
144✔
NEW
2626
                return nil, err
×
NEW
2627
        }
×
2628
        err = updateEdgePolicyDisabledIndex(edges, cid, true, false)
144✔
2629
        if err != nil {
144✔
NEW
2630
                return nil, err
×
NEW
2631
        }
×
2632

2633
        // With the edge data deleted, we can purge the information from the two
2634
        // edge indexes.
2635
        if err := edgeIndex.Delete(chanID); err != nil {
144✔
NEW
2636
                return nil, err
×
NEW
2637
        }
×
2638
        var b bytes.Buffer
144✔
2639
        if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
144✔
NEW
2640
                return nil, err
×
NEW
2641
        }
×
2642
        if err := chanIndex.Delete(b.Bytes()); err != nil {
144✔
NEW
2643
                return nil, err
×
NEW
2644
        }
×
2645

2646
        // Finally, we'll mark the edge as a zombie within our index if it's
2647
        // being removed due to the channel becoming a zombie. We do this to
2648
        // ensure we don't store unnecessary data for spent channels.
2649
        if !isZombie {
266✔
2650
                return &edgeInfo, nil
122✔
2651
        }
122✔
2652

2653
        nodeKey1, nodeKey2 := edgeInfo.NodeKey1Bytes, edgeInfo.NodeKey2Bytes
25✔
2654
        if strictZombie {
28✔
2655
                nodeKey1, nodeKey2 = makeZombiePubkeys(&edgeInfo, edge1, edge2)
3✔
2656
        }
3✔
2657

2658
        return &edgeInfo, markEdgeZombie(
25✔
2659
                zombieIndex, byteOrder.Uint64(chanID), nodeKey1, nodeKey2,
25✔
2660
        )
25✔
2661
}
2662

2663
// makeZombiePubkeys derives the node pubkeys to store in the zombie index for a
2664
// particular pair of channel policies. The return values are one of:
2665
//  1. (pubkey1, pubkey2)
2666
//  2. (pubkey1, blank)
2667
//  3. (blank, pubkey2)
2668
//
2669
// A blank pubkey means that corresponding node will be unable to resurrect a
2670
// channel on its own. For example, node1 may continue to publish recent
2671
// updates, but node2 has fallen way behind. After marking an edge as a zombie,
2672
// we don't want another fresh update from node1 to resurrect, as the edge can
2673
// only become live once node2 finally sends something recent.
2674
//
2675
// In the case where we have neither update, we allow either party to resurrect
2676
// the channel. If the channel were to be marked zombie again, it would be
2677
// marked with the correct lagging channel since we received an update from only
2678
// one side.
2679
func makeZombiePubkeys(info *models.ChannelEdgeInfo,
2680
        e1, e2 *models.ChannelEdgePolicy) ([33]byte, [33]byte) {
3✔
2681

3✔
2682
        switch {
3✔
2683
        // If we don't have either edge policy, we'll return both pubkeys so
2684
        // that the channel can be resurrected by either party.
NEW
2685
        case e1 == nil && e2 == nil:
×
NEW
2686
                return info.NodeKey1Bytes, info.NodeKey2Bytes
×
2687

2688
        // If we're missing edge1, or if both edges are present but edge1 is
2689
        // older, we'll return edge1's pubkey and a blank pubkey for edge2. This
2690
        // means that only an update from edge1 will be able to resurrect the
2691
        // channel.
2692
        case e1 == nil || (e2 != nil && e1.LastUpdate.Before(e2.LastUpdate)):
1✔
2693
                return info.NodeKey1Bytes, [33]byte{}
1✔
2694

2695
        // Otherwise, we're missing edge2 or edge2 is the older side, so we
2696
        // return a blank pubkey for edge1. In this case, only an update from
2697
        // edge2 can resurect the channel.
2698
        default:
2✔
2699
                return [33]byte{}, info.NodeKey2Bytes
2✔
2700
        }
2701
}
2702

2703
// UpdateEdgePolicy updates the edge routing policy for a single directed edge
2704
// within the database for the referenced channel. The `flags` attribute within
2705
// the ChannelEdgePolicy determines which of the directed edges are being
2706
// updated. If the flag is 1, then the first node's information is being
2707
// updated, otherwise it's the second node's information. The node ordering is
2708
// determined by the lexicographical ordering of the identity public keys of the
2709
// nodes on either side of the channel.
2710
func (c *KVStore) UpdateEdgePolicy(edge *models.ChannelEdgePolicy,
2711
        op ...batch.SchedulerOption) (route.Vertex, route.Vertex, error) {
2,666✔
2712

2,666✔
2713
        var (
2,666✔
2714
                isUpdate1    bool
2,666✔
2715
                edgeNotFound bool
2,666✔
2716
                from, to     route.Vertex
2,666✔
2717
        )
2,666✔
2718

2,666✔
2719
        r := &batch.Request{
2,666✔
2720
                Reset: func() {
5,332✔
2721
                        isUpdate1 = false
2,666✔
2722
                        edgeNotFound = false
2,666✔
2723
                },
2,666✔
2724
                Update: func(tx kvdb.RwTx) error {
2,666✔
2725
                        var err error
2,666✔
2726
                        from, to, isUpdate1, err = updateEdgePolicy(tx, edge)
2,666✔
2727
                        if err != nil {
2,669✔
2728
                                log.Errorf("UpdateEdgePolicy faild: %v", err)
3✔
2729
                        }
3✔
2730

2731
                        // Silence ErrEdgeNotFound so that the batch can
2732
                        // succeed, but propagate the error via local state.
2733
                        if errors.Is(err, ErrEdgeNotFound) {
2,669✔
2734
                                edgeNotFound = true
3✔
2735
                                return nil
3✔
2736
                        }
3✔
2737

2738
                        return err
2,663✔
2739
                },
2740
                OnCommit: func(err error) error {
2,666✔
2741
                        switch {
2,666✔
NEW
2742
                        case err != nil:
×
NEW
2743
                                return err
×
2744
                        case edgeNotFound:
3✔
2745
                                return ErrEdgeNotFound
3✔
2746
                        default:
2,663✔
2747
                                c.updateEdgeCache(edge, isUpdate1)
2,663✔
2748
                                return nil
2,663✔
2749
                        }
2750
                },
2751
        }
2752

2753
        for _, f := range op {
2,669✔
2754
                f(r)
3✔
2755
        }
3✔
2756

2757
        err := c.chanScheduler.Execute(r)
2,666✔
2758

2,666✔
2759
        return from, to, err
2,666✔
2760
}
2761

2762
func (c *KVStore) updateEdgeCache(e *models.ChannelEdgePolicy,
2763
        isUpdate1 bool) {
2,663✔
2764

2,663✔
2765
        // If an entry for this channel is found in reject cache, we'll modify
2,663✔
2766
        // the entry with the updated timestamp for the direction that was just
2,663✔
2767
        // written. If the edge doesn't exist, we'll load the cache entry lazily
2,663✔
2768
        // during the next query for this edge.
2,663✔
2769
        if entry, ok := c.rejectCache.get(e.ChannelID); ok {
2,671✔
2770
                if isUpdate1 {
14✔
2771
                        entry.upd1Time = e.LastUpdate.Unix()
6✔
2772
                } else {
11✔
2773
                        entry.upd2Time = e.LastUpdate.Unix()
5✔
2774
                }
5✔
2775
                c.rejectCache.insert(e.ChannelID, entry)
8✔
2776
        }
2777

2778
        // If an entry for this channel is found in channel cache, we'll modify
2779
        // the entry with the updated policy for the direction that was just
2780
        // written. If the edge doesn't exist, we'll defer loading the info and
2781
        // policies and lazily read from disk during the next query.
2782
        if channel, ok := c.chanCache.get(e.ChannelID); ok {
2,666✔
2783
                if isUpdate1 {
6✔
2784
                        channel.Policy1 = e
3✔
2785
                } else {
6✔
2786
                        channel.Policy2 = e
3✔
2787
                }
3✔
2788
                c.chanCache.insert(e.ChannelID, channel)
3✔
2789
        }
2790
}
2791

2792
// updateEdgePolicy attempts to update an edge's policy within the relevant
2793
// buckets using an existing database transaction. The returned boolean will be
2794
// true if the updated policy belongs to node1, and false if the policy belonged
2795
// to node2.
2796
func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy) (
2797
        route.Vertex, route.Vertex, bool, error) {
2,666✔
2798

2,666✔
2799
        var noVertex route.Vertex
2,666✔
2800

2,666✔
2801
        edges := tx.ReadWriteBucket(edgeBucket)
2,666✔
2802
        if edges == nil {
2,666✔
NEW
2803
                return noVertex, noVertex, false, ErrEdgeNotFound
×
NEW
2804
        }
×
2805
        edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
2,666✔
2806
        if edgeIndex == nil {
2,666✔
NEW
2807
                return noVertex, noVertex, false, ErrEdgeNotFound
×
NEW
2808
        }
×
2809

2810
        // Create the channelID key be converting the channel ID
2811
        // integer into a byte slice.
2812
        var chanID [8]byte
2,666✔
2813
        byteOrder.PutUint64(chanID[:], edge.ChannelID)
2,666✔
2814

2,666✔
2815
        // With the channel ID, we then fetch the value storing the two
2,666✔
2816
        // nodes which connect this channel edge.
2,666✔
2817
        nodeInfo := edgeIndex.Get(chanID[:])
2,666✔
2818
        if nodeInfo == nil {
2,669✔
2819
                return noVertex, noVertex, false, ErrEdgeNotFound
3✔
2820
        }
3✔
2821

2822
        // Depending on the flags value passed above, either the first
2823
        // or second edge policy is being updated.
2824
        var fromNode, toNode []byte
2,663✔
2825
        var isUpdate1 bool
2,663✔
2826
        if edge.ChannelFlags&lnwire.ChanUpdateDirection == 0 {
4,000✔
2827
                fromNode = nodeInfo[:33]
1,337✔
2828
                toNode = nodeInfo[33:66]
1,337✔
2829
                isUpdate1 = true
1,337✔
2830
        } else {
2,666✔
2831
                fromNode = nodeInfo[33:66]
1,329✔
2832
                toNode = nodeInfo[:33]
1,329✔
2833
                isUpdate1 = false
1,329✔
2834
        }
1,329✔
2835

2836
        // Finally, with the direction of the edge being updated
2837
        // identified, we update the on-disk edge representation.
2838
        err := putChanEdgePolicy(edges, edge, fromNode, toNode)
2,663✔
2839
        if err != nil {
2,663✔
NEW
2840
                return noVertex, noVertex, false, err
×
NEW
2841
        }
×
2842

2843
        var (
2,663✔
2844
                fromNodePubKey route.Vertex
2,663✔
2845
                toNodePubKey   route.Vertex
2,663✔
2846
        )
2,663✔
2847
        copy(fromNodePubKey[:], fromNode)
2,663✔
2848
        copy(toNodePubKey[:], toNode)
2,663✔
2849

2,663✔
2850
        return fromNodePubKey, toNodePubKey, isUpdate1, nil
2,663✔
2851
}
2852

2853
// isPublic determines whether the node is seen as public within the graph from
2854
// the source node's point of view. An existing database transaction can also be
2855
// specified.
2856
func (c *KVStore) isPublic(tx kvdb.RTx, nodePub route.Vertex,
2857
        sourcePubKey []byte) (bool, error) {
16✔
2858

16✔
2859
        // In order to determine whether this node is publicly advertised within
16✔
2860
        // the graph, we'll need to look at all of its edges and check whether
16✔
2861
        // they extend to any other node than the source node. errDone will be
16✔
2862
        // used to terminate the check early.
16✔
2863
        nodeIsPublic := false
16✔
2864
        errDone := errors.New("done")
16✔
2865
        err := c.ForEachNodeChannelTx(tx, nodePub, func(tx kvdb.RTx,
16✔
2866
                info *models.ChannelEdgeInfo, _ *models.ChannelEdgePolicy,
16✔
2867
                _ *models.ChannelEdgePolicy) error {
29✔
2868

13✔
2869
                // If this edge doesn't extend to the source node, we'll
13✔
2870
                // terminate our search as we can now conclude that the node is
13✔
2871
                // publicly advertised within the graph due to the local node
13✔
2872
                // knowing of the current edge.
13✔
2873
                if !bytes.Equal(info.NodeKey1Bytes[:], sourcePubKey) &&
13✔
2874
                        !bytes.Equal(info.NodeKey2Bytes[:], sourcePubKey) {
19✔
2875

6✔
2876
                        nodeIsPublic = true
6✔
2877
                        return errDone
6✔
2878
                }
6✔
2879

2880
                // Since the edge _does_ extend to the source node, we'll also
2881
                // need to ensure that this is a public edge.
2882
                if info.AuthProof != nil {
19✔
2883
                        nodeIsPublic = true
9✔
2884
                        return errDone
9✔
2885
                }
9✔
2886

2887
                // Otherwise, we'll continue our search.
2888
                return nil
4✔
2889
        })
2890
        if err != nil && !errors.Is(err, errDone) {
16✔
NEW
2891
                return false, err
×
NEW
2892
        }
×
2893

2894
        return nodeIsPublic, nil
16✔
2895
}
2896

2897
// FetchLightningNodeTx attempts to look up a target node by its identity
2898
// public key. If the node isn't found in the database, then
2899
// ErrGraphNodeNotFound is returned. An optional transaction may be provided.
2900
// If none is provided, then a new one will be created.
2901
func (c *KVStore) FetchLightningNodeTx(tx kvdb.RTx, nodePub route.Vertex) (
2902
        *models.LightningNode, error) {
3,633✔
2903

3,633✔
2904
        return c.fetchLightningNode(tx, nodePub)
3,633✔
2905
}
3,633✔
2906

2907
// FetchLightningNode attempts to look up a target node by its identity public
2908
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
2909
// returned.
2910
func (c *KVStore) FetchLightningNode(nodePub route.Vertex) (
2911
        *models.LightningNode, error) {
155✔
2912

155✔
2913
        return c.fetchLightningNode(nil, nodePub)
155✔
2914
}
155✔
2915

2916
// fetchLightningNode attempts to look up a target node by its identity public
2917
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
2918
// returned. An optional transaction may be provided. If none is provided, then
2919
// a new one will be created.
2920
func (c *KVStore) fetchLightningNode(tx kvdb.RTx,
2921
        nodePub route.Vertex) (*models.LightningNode, error) {
3,785✔
2922

3,785✔
2923
        var node *models.LightningNode
3,785✔
2924
        fetch := func(tx kvdb.RTx) error {
7,570✔
2925
                // First grab the nodes bucket which stores the mapping from
3,785✔
2926
                // pubKey to node information.
3,785✔
2927
                nodes := tx.ReadBucket(nodeBucket)
3,785✔
2928
                if nodes == nil {
3,785✔
NEW
2929
                        return ErrGraphNotFound
×
NEW
2930
                }
×
2931

2932
                // If a key for this serialized public key isn't found, then
2933
                // the target node doesn't exist within the database.
2934
                nodeBytes := nodes.Get(nodePub[:])
3,785✔
2935
                if nodeBytes == nil {
3,802✔
2936
                        return ErrGraphNodeNotFound
17✔
2937
                }
17✔
2938

2939
                // If the node is found, then we can de deserialize the node
2940
                // information to return to the user.
2941
                nodeReader := bytes.NewReader(nodeBytes)
3,771✔
2942
                n, err := deserializeLightningNode(nodeReader)
3,771✔
2943
                if err != nil {
3,771✔
NEW
2944
                        return err
×
NEW
2945
                }
×
2946

2947
                node = &n
3,771✔
2948

3,771✔
2949
                return nil
3,771✔
2950
        }
2951

2952
        if tx == nil {
3,943✔
2953
                err := kvdb.View(
158✔
2954
                        c.db, fetch, func() {
316✔
2955
                                node = nil
158✔
2956
                        },
158✔
2957
                )
2958
                if err != nil {
164✔
2959
                        return nil, err
6✔
2960
                }
6✔
2961

2962
                return node, nil
155✔
2963
        }
2964

2965
        err := fetch(tx)
3,627✔
2966
        if err != nil {
3,638✔
2967
                return nil, err
11✔
2968
        }
11✔
2969

2970
        return node, nil
3,616✔
2971
}
2972

2973
// HasLightningNode determines if the graph has a vertex identified by the
2974
// target node identity public key. If the node exists in the database, a
2975
// timestamp of when the data for the node was lasted updated is returned along
2976
// with a true boolean. Otherwise, an empty time.Time is returned with a false
2977
// boolean.
2978
func (c *KVStore) HasLightningNode(nodePub [33]byte) (time.Time, bool,
2979
        error) {
19✔
2980

19✔
2981
        var (
19✔
2982
                updateTime time.Time
19✔
2983
                exists     bool
19✔
2984
        )
19✔
2985

19✔
2986
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
38✔
2987
                // First grab the nodes bucket which stores the mapping from
19✔
2988
                // pubKey to node information.
19✔
2989
                nodes := tx.ReadBucket(nodeBucket)
19✔
2990
                if nodes == nil {
19✔
NEW
2991
                        return ErrGraphNotFound
×
NEW
2992
                }
×
2993

2994
                // If a key for this serialized public key isn't found, we can
2995
                // exit early.
2996
                nodeBytes := nodes.Get(nodePub[:])
19✔
2997
                if nodeBytes == nil {
25✔
2998
                        exists = false
6✔
2999
                        return nil
6✔
3000
                }
6✔
3001

3002
                // Otherwise we continue on to obtain the time stamp
3003
                // representing the last time the data for this node was
3004
                // updated.
3005
                nodeReader := bytes.NewReader(nodeBytes)
16✔
3006
                node, err := deserializeLightningNode(nodeReader)
16✔
3007
                if err != nil {
16✔
NEW
3008
                        return err
×
NEW
3009
                }
×
3010

3011
                exists = true
16✔
3012
                updateTime = node.LastUpdate
16✔
3013

16✔
3014
                return nil
16✔
3015
        }, func() {
19✔
3016
                updateTime = time.Time{}
19✔
3017
                exists = false
19✔
3018
        })
19✔
3019
        if err != nil {
19✔
NEW
3020
                return time.Time{}, exists, err
×
NEW
3021
        }
×
3022

3023
        return updateTime, exists, nil
19✔
3024
}
3025

3026
// nodeTraversal is used to traverse all channels of a node given by its
3027
// public key and passes channel information into the specified callback.
3028
func nodeTraversal(tx kvdb.RTx, nodePub []byte, db kvdb.Backend,
3029
        cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3030
                *models.ChannelEdgePolicy) error) error {
1,269✔
3031

1,269✔
3032
        traversal := func(tx kvdb.RTx) error {
2,538✔
3033
                edges := tx.ReadBucket(edgeBucket)
1,269✔
3034
                if edges == nil {
1,269✔
NEW
3035
                        return ErrGraphNotFound
×
NEW
3036
                }
×
3037
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
1,269✔
3038
                if edgeIndex == nil {
1,269✔
NEW
3039
                        return ErrGraphNoEdgesFound
×
NEW
3040
                }
×
3041

3042
                // In order to reach all the edges for this node, we take
3043
                // advantage of the construction of the key-space within the
3044
                // edge bucket. The keys are stored in the form: pubKey ||
3045
                // chanID. Therefore, starting from a chanID of zero, we can
3046
                // scan forward in the bucket, grabbing all the edges for the
3047
                // node. Once the prefix no longer matches, then we know we're
3048
                // done.
3049
                var nodeStart [33 + 8]byte
1,269✔
3050
                copy(nodeStart[:], nodePub)
1,269✔
3051
                copy(nodeStart[33:], chanStart[:])
1,269✔
3052

1,269✔
3053
                // Starting from the key pubKey || 0, we seek forward in the
1,269✔
3054
                // bucket until the retrieved key no longer has the public key
1,269✔
3055
                // as its prefix. This indicates that we've stepped over into
1,269✔
3056
                // another node's edges, so we can terminate our scan.
1,269✔
3057
                edgeCursor := edges.ReadCursor()
1,269✔
3058
                for nodeEdge, _ := edgeCursor.Seek(nodeStart[:]); bytes.HasPrefix(nodeEdge, nodePub); nodeEdge, _ = edgeCursor.Next() { //nolint:ll
5,112✔
3059
                        // If the prefix still matches, the channel id is
3,843✔
3060
                        // returned in nodeEdge. Channel id is used to lookup
3,843✔
3061
                        // the node at the other end of the channel and both
3,843✔
3062
                        // edge policies.
3,843✔
3063
                        chanID := nodeEdge[33:]
3,843✔
3064
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3,843✔
3065
                        if err != nil {
3,843✔
NEW
3066
                                return err
×
NEW
3067
                        }
×
3068

3069
                        outgoingPolicy, err := fetchChanEdgePolicy(
3,843✔
3070
                                edges, chanID, nodePub,
3,843✔
3071
                        )
3,843✔
3072
                        if err != nil {
3,843✔
NEW
3073
                                return err
×
NEW
3074
                        }
×
3075

3076
                        otherNode, err := edgeInfo.OtherNodeKeyBytes(nodePub)
3,843✔
3077
                        if err != nil {
3,843✔
NEW
3078
                                return err
×
NEW
3079
                        }
×
3080

3081
                        incomingPolicy, err := fetchChanEdgePolicy(
3,843✔
3082
                                edges, chanID, otherNode[:],
3,843✔
3083
                        )
3,843✔
3084
                        if err != nil {
3,843✔
NEW
3085
                                return err
×
NEW
3086
                        }
×
3087

3088
                        // Finally, we execute the callback.
3089
                        err = cb(tx, &edgeInfo, outgoingPolicy, incomingPolicy)
3,843✔
3090
                        if err != nil {
3,855✔
3091
                                return err
12✔
3092
                        }
12✔
3093
                }
3094

3095
                return nil
1,260✔
3096
        }
3097

3098
        // If no transaction was provided, then we'll create a new transaction
3099
        // to execute the transaction within.
3100
        if tx == nil {
1,281✔
3101
                return kvdb.View(db, traversal, func() {})
24✔
3102
        }
3103

3104
        // Otherwise, we re-use the existing transaction to execute the graph
3105
        // traversal.
3106
        return traversal(tx)
1,260✔
3107
}
3108

3109
// ForEachNodeChannel iterates through all channels of the given node,
3110
// executing the passed callback with an edge info structure and the policies
3111
// of each end of the channel. The first edge policy is the outgoing edge *to*
3112
// the connecting node, while the second is the incoming edge *from* the
3113
// connecting node. If the callback returns an error, then the iteration is
3114
// halted with the error propagated back up to the caller.
3115
//
3116
// Unknown policies are passed into the callback as nil values.
3117
func (c *KVStore) ForEachNodeChannel(nodePub route.Vertex,
3118
        cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3119
                *models.ChannelEdgePolicy) error) error {
9✔
3120

9✔
3121
        return nodeTraversal(nil, nodePub[:], c.db, cb)
9✔
3122
}
9✔
3123

3124
// ForEachNodeChannelTx iterates through all channels of the given node,
3125
// executing the passed callback with an edge info structure and the policies
3126
// of each end of the channel. The first edge policy is the outgoing edge *to*
3127
// the connecting node, while the second is the incoming edge *from* the
3128
// connecting node. If the callback returns an error, then the iteration is
3129
// halted with the error propagated back up to the caller.
3130
//
3131
// Unknown policies are passed into the callback as nil values.
3132
//
3133
// If the caller wishes to re-use an existing boltdb transaction, then it
3134
// should be passed as the first argument.  Otherwise, the first argument should
3135
// be nil and a fresh transaction will be created to execute the graph
3136
// traversal.
3137
func (c *KVStore) ForEachNodeChannelTx(tx kvdb.RTx,
3138
        nodePub route.Vertex, cb func(kvdb.RTx, *models.ChannelEdgeInfo,
3139
                *models.ChannelEdgePolicy,
3140
                *models.ChannelEdgePolicy) error) error {
1,021✔
3141

1,021✔
3142
        return nodeTraversal(tx, nodePub[:], c.db, cb)
1,021✔
3143
}
1,021✔
3144

3145
// FetchOtherNode attempts to fetch the full LightningNode that's opposite of
3146
// the target node in the channel. This is useful when one knows the pubkey of
3147
// one of the nodes, and wishes to obtain the full LightningNode for the other
3148
// end of the channel.
3149
func (c *KVStore) FetchOtherNode(tx kvdb.RTx,
3150
        channel *models.ChannelEdgeInfo, thisNodeKey []byte) (
3151
        *models.LightningNode, error) {
3✔
3152

3✔
3153
        // Ensure that the node passed in is actually a member of the channel.
3✔
3154
        var targetNodeBytes [33]byte
3✔
3155
        switch {
3✔
3156
        case bytes.Equal(channel.NodeKey1Bytes[:], thisNodeKey):
3✔
3157
                targetNodeBytes = channel.NodeKey2Bytes
3✔
3158
        case bytes.Equal(channel.NodeKey2Bytes[:], thisNodeKey):
3✔
3159
                targetNodeBytes = channel.NodeKey1Bytes
3✔
NEW
3160
        default:
×
NEW
3161
                return nil, fmt.Errorf("node not participating in this channel")
×
3162
        }
3163

3164
        var targetNode *models.LightningNode
3✔
3165
        fetchNodeFunc := func(tx kvdb.RTx) error {
6✔
3166
                // First grab the nodes bucket which stores the mapping from
3✔
3167
                // pubKey to node information.
3✔
3168
                nodes := tx.ReadBucket(nodeBucket)
3✔
3169
                if nodes == nil {
3✔
NEW
3170
                        return ErrGraphNotFound
×
NEW
3171
                }
×
3172

3173
                node, err := fetchLightningNode(nodes, targetNodeBytes[:])
3✔
3174
                if err != nil {
3✔
NEW
3175
                        return err
×
NEW
3176
                }
×
3177

3178
                targetNode = &node
3✔
3179

3✔
3180
                return nil
3✔
3181
        }
3182

3183
        // If the transaction is nil, then we'll need to create a new one,
3184
        // otherwise we can use the existing db transaction.
3185
        var err error
3✔
3186
        if tx == nil {
3✔
NEW
3187
                err = kvdb.View(c.db, fetchNodeFunc, func() {
×
NEW
3188
                        targetNode = nil
×
NEW
3189
                })
×
3190
        } else {
3✔
3191
                err = fetchNodeFunc(tx)
3✔
3192
        }
3✔
3193

3194
        return targetNode, err
3✔
3195
}
3196

3197
// computeEdgePolicyKeys is a helper function that can be used to compute the
3198
// keys used to index the channel edge policy info for the two nodes of the
3199
// edge. The keys for node 1 and node 2 are returned respectively.
3200
func computeEdgePolicyKeys(info *models.ChannelEdgeInfo) ([]byte, []byte) {
25✔
3201
        var (
25✔
3202
                node1Key [33 + 8]byte
25✔
3203
                node2Key [33 + 8]byte
25✔
3204
        )
25✔
3205

25✔
3206
        copy(node1Key[:], info.NodeKey1Bytes[:])
25✔
3207
        copy(node2Key[:], info.NodeKey2Bytes[:])
25✔
3208

25✔
3209
        byteOrder.PutUint64(node1Key[33:], info.ChannelID)
25✔
3210
        byteOrder.PutUint64(node2Key[33:], info.ChannelID)
25✔
3211

25✔
3212
        return node1Key[:], node2Key[:]
25✔
3213
}
25✔
3214

3215
// FetchChannelEdgesByOutpoint attempts to lookup the two directed edges for
3216
// the channel identified by the funding outpoint. If the channel can't be
3217
// found, then ErrEdgeNotFound is returned. A struct which houses the general
3218
// information for the channel itself is returned as well as two structs that
3219
// contain the routing policies for the channel in either direction.
3220
func (c *KVStore) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
3221
        *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3222
        *models.ChannelEdgePolicy, error) {
14✔
3223

14✔
3224
        var (
14✔
3225
                edgeInfo *models.ChannelEdgeInfo
14✔
3226
                policy1  *models.ChannelEdgePolicy
14✔
3227
                policy2  *models.ChannelEdgePolicy
14✔
3228
        )
14✔
3229

14✔
3230
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
28✔
3231
                // First, grab the node bucket. This will be used to populate
14✔
3232
                // the Node pointers in each edge read from disk.
14✔
3233
                nodes := tx.ReadBucket(nodeBucket)
14✔
3234
                if nodes == nil {
14✔
NEW
3235
                        return ErrGraphNotFound
×
NEW
3236
                }
×
3237

3238
                // Next, grab the edge bucket which stores the edges, and also
3239
                // the index itself so we can group the directed edges together
3240
                // logically.
3241
                edges := tx.ReadBucket(edgeBucket)
14✔
3242
                if edges == nil {
14✔
NEW
3243
                        return ErrGraphNoEdgesFound
×
NEW
3244
                }
×
3245
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
14✔
3246
                if edgeIndex == nil {
14✔
NEW
3247
                        return ErrGraphNoEdgesFound
×
NEW
3248
                }
×
3249

3250
                // If the channel's outpoint doesn't exist within the outpoint
3251
                // index, then the edge does not exist.
3252
                chanIndex := edges.NestedReadBucket(channelPointBucket)
14✔
3253
                if chanIndex == nil {
14✔
NEW
3254
                        return ErrGraphNoEdgesFound
×
NEW
3255
                }
×
3256
                var b bytes.Buffer
14✔
3257
                if err := WriteOutpoint(&b, op); err != nil {
14✔
NEW
3258
                        return err
×
NEW
3259
                }
×
3260
                chanID := chanIndex.Get(b.Bytes())
14✔
3261
                if chanID == nil {
27✔
3262
                        return fmt.Errorf("%w: op=%v", ErrEdgeNotFound, op)
13✔
3263
                }
13✔
3264

3265
                // If the channel is found to exists, then we'll first retrieve
3266
                // the general information for the channel.
3267
                edge, err := fetchChanEdgeInfo(edgeIndex, chanID)
4✔
3268
                if err != nil {
4✔
NEW
3269
                        return fmt.Errorf("%w: chanID=%x", err, chanID)
×
NEW
3270
                }
×
3271
                edgeInfo = &edge
4✔
3272

4✔
3273
                // Once we have the information about the channels' parameters,
4✔
3274
                // we'll fetch the routing policies for each for the directed
4✔
3275
                // edges.
4✔
3276
                e1, e2, err := fetchChanEdgePolicies(edgeIndex, edges, chanID)
4✔
3277
                if err != nil {
4✔
NEW
3278
                        return fmt.Errorf("failed to find policy: %w", err)
×
NEW
3279
                }
×
3280

3281
                policy1 = e1
4✔
3282
                policy2 = e2
4✔
3283

4✔
3284
                return nil
4✔
3285
        }, func() {
14✔
3286
                edgeInfo = nil
14✔
3287
                policy1 = nil
14✔
3288
                policy2 = nil
14✔
3289
        })
14✔
3290
        if err != nil {
27✔
3291
                return nil, nil, nil, err
13✔
3292
        }
13✔
3293

3294
        return edgeInfo, policy1, policy2, nil
4✔
3295
}
3296

3297
// FetchChannelEdgesByID attempts to lookup the two directed edges for the
3298
// channel identified by the channel ID. If the channel can't be found, then
3299
// ErrEdgeNotFound is returned. A struct which houses the general information
3300
// for the channel itself is returned as well as two structs that contain the
3301
// routing policies for the channel in either direction.
3302
//
3303
// ErrZombieEdge an be returned if the edge is currently marked as a zombie
3304
// within the database. In this case, the ChannelEdgePolicy's will be nil, and
3305
// the ChannelEdgeInfo will only include the public keys of each node.
3306
func (c *KVStore) FetchChannelEdgesByID(chanID uint64) (
3307
        *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3308
        *models.ChannelEdgePolicy, error) {
2,682✔
3309

2,682✔
3310
        var (
2,682✔
3311
                edgeInfo  *models.ChannelEdgeInfo
2,682✔
3312
                policy1   *models.ChannelEdgePolicy
2,682✔
3313
                policy2   *models.ChannelEdgePolicy
2,682✔
3314
                channelID [8]byte
2,682✔
3315
        )
2,682✔
3316

2,682✔
3317
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
5,359✔
3318
                // First, grab the node bucket. This will be used to populate
2,677✔
3319
                // the Node pointers in each edge read from disk.
2,677✔
3320
                nodes := tx.ReadBucket(nodeBucket)
2,677✔
3321
                if nodes == nil {
2,677✔
NEW
3322
                        return ErrGraphNotFound
×
NEW
3323
                }
×
3324

3325
                // Next, grab the edge bucket which stores the edges, and also
3326
                // the index itself so we can group the directed edges together
3327
                // logically.
3328
                edges := tx.ReadBucket(edgeBucket)
2,677✔
3329
                if edges == nil {
2,677✔
NEW
3330
                        return ErrGraphNoEdgesFound
×
NEW
3331
                }
×
3332
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
2,677✔
3333
                if edgeIndex == nil {
2,677✔
NEW
3334
                        return ErrGraphNoEdgesFound
×
NEW
3335
                }
×
3336

3337
                byteOrder.PutUint64(channelID[:], chanID)
2,677✔
3338

2,677✔
3339
                // Now, attempt to fetch edge.
2,677✔
3340
                edge, err := fetchChanEdgeInfo(edgeIndex, channelID[:])
2,677✔
3341

2,677✔
3342
                // If it doesn't exist, we'll quickly check our zombie index to
2,677✔
3343
                // see if we've previously marked it as so.
2,677✔
3344
                if errors.Is(err, ErrEdgeNotFound) {
2,681✔
3345
                        // If the zombie index doesn't exist, or the edge is not
4✔
3346
                        // marked as a zombie within it, then we'll return the
4✔
3347
                        // original ErrEdgeNotFound error.
4✔
3348
                        zombieIndex := edges.NestedReadBucket(zombieBucket)
4✔
3349
                        if zombieIndex == nil {
4✔
NEW
3350
                                return ErrEdgeNotFound
×
NEW
3351
                        }
×
3352

3353
                        isZombie, pubKey1, pubKey2 := isZombieEdge(
4✔
3354
                                zombieIndex, chanID,
4✔
3355
                        )
4✔
3356
                        if !isZombie {
7✔
3357
                                return ErrEdgeNotFound
3✔
3358
                        }
3✔
3359

3360
                        // Otherwise, the edge is marked as a zombie, so we'll
3361
                        // populate the edge info with the public keys of each
3362
                        // party as this is the only information we have about
3363
                        // it and return an error signaling so.
3364
                        edgeInfo = &models.ChannelEdgeInfo{
4✔
3365
                                NodeKey1Bytes: pubKey1,
4✔
3366
                                NodeKey2Bytes: pubKey2,
4✔
3367
                        }
4✔
3368

4✔
3369
                        return ErrZombieEdge
4✔
3370
                }
3371

3372
                // Otherwise, we'll just return the error if any.
3373
                if err != nil {
2,676✔
NEW
3374
                        return err
×
NEW
3375
                }
×
3376

3377
                edgeInfo = &edge
2,676✔
3378

2,676✔
3379
                // Then we'll attempt to fetch the accompanying policies of this
2,676✔
3380
                // edge.
2,676✔
3381
                e1, e2, err := fetchChanEdgePolicies(
2,676✔
3382
                        edgeIndex, edges, channelID[:],
2,676✔
3383
                )
2,676✔
3384
                if err != nil {
2,676✔
NEW
3385
                        return err
×
NEW
3386
                }
×
3387

3388
                policy1 = e1
2,676✔
3389
                policy2 = e2
2,676✔
3390

2,676✔
3391
                return nil
2,676✔
3392
        }, func() {
2,682✔
3393
                edgeInfo = nil
2,682✔
3394
                policy1 = nil
2,682✔
3395
                policy2 = nil
2,682✔
3396
        })
2,682✔
3397
        if errors.Is(err, ErrZombieEdge) {
2,686✔
3398
                return edgeInfo, nil, nil, err
4✔
3399
        }
4✔
3400
        if err != nil {
2,689✔
3401
                return nil, nil, nil, err
8✔
3402
        }
8✔
3403

3404
        return edgeInfo, policy1, policy2, nil
2,676✔
3405
}
3406

3407
// IsPublicNode is a helper method that determines whether the node with the
3408
// given public key is seen as a public node in the graph from the graph's
3409
// source node's point of view.
3410
func (c *KVStore) IsPublicNode(pubKey [33]byte) (bool, error) {
16✔
3411
        var nodeIsPublic bool
16✔
3412
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
32✔
3413
                nodes := tx.ReadBucket(nodeBucket)
16✔
3414
                if nodes == nil {
16✔
NEW
3415
                        return ErrGraphNodesNotFound
×
NEW
3416
                }
×
3417
                ourPubKey := nodes.Get(sourceKey)
16✔
3418
                if ourPubKey == nil {
16✔
NEW
3419
                        return ErrSourceNodeNotSet
×
NEW
3420
                }
×
3421
                node, err := fetchLightningNode(nodes, pubKey[:])
16✔
3422
                if err != nil {
16✔
NEW
3423
                        return err
×
NEW
3424
                }
×
3425

3426
                nodeIsPublic, err = c.isPublic(tx, node.PubKeyBytes, ourPubKey)
16✔
3427
                return err
16✔
3428
        }, func() {
16✔
3429
                nodeIsPublic = false
16✔
3430
        })
16✔
3431
        if err != nil {
16✔
NEW
3432
                return false, err
×
NEW
3433
        }
×
3434

3435
        return nodeIsPublic, nil
16✔
3436
}
3437

3438
// genMultiSigP2WSH generates the p2wsh'd multisig script for 2 of 2 pubkeys.
3439
func genMultiSigP2WSH(aPub, bPub []byte) ([]byte, error) {
49✔
3440
        witnessScript, err := input.GenMultiSigScript(aPub, bPub)
49✔
3441
        if err != nil {
49✔
NEW
3442
                return nil, err
×
NEW
3443
        }
×
3444

3445
        // With the witness script generated, we'll now turn it into a p2wsh
3446
        // script:
3447
        //  * OP_0 <sha256(script)>
3448
        bldr := txscript.NewScriptBuilder(
49✔
3449
                txscript.WithScriptAllocSize(input.P2WSHSize),
49✔
3450
        )
49✔
3451
        bldr.AddOp(txscript.OP_0)
49✔
3452
        scriptHash := sha256.Sum256(witnessScript)
49✔
3453
        bldr.AddData(scriptHash[:])
49✔
3454

49✔
3455
        return bldr.Script()
49✔
3456
}
3457

3458
// EdgePoint couples the outpoint of a channel with the funding script that it
3459
// creates. The FilteredChainView will use this to watch for spends of this
3460
// edge point on chain. We require both of these values as depending on the
3461
// concrete implementation, either the pkScript, or the out point will be used.
3462
type EdgePoint struct {
3463
        // FundingPkScript is the p2wsh multi-sig script of the target channel.
3464
        FundingPkScript []byte
3465

3466
        // OutPoint is the outpoint of the target channel.
3467
        OutPoint wire.OutPoint
3468
}
3469

3470
// String returns a human readable version of the target EdgePoint. We return
3471
// the outpoint directly as it is enough to uniquely identify the edge point.
NEW
3472
func (e *EdgePoint) String() string {
×
NEW
3473
        return e.OutPoint.String()
×
NEW
3474
}
×
3475

3476
// ChannelView returns the verifiable edge information for each active channel
3477
// within the known channel graph. The set of UTXO's (along with their scripts)
3478
// returned are the ones that need to be watched on chain to detect channel
3479
// closes on the resident blockchain.
3480
func (c *KVStore) ChannelView() ([]EdgePoint, error) {
25✔
3481
        var edgePoints []EdgePoint
25✔
3482
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
50✔
3483
                // We're going to iterate over the entire channel index, so
25✔
3484
                // we'll need to fetch the edgeBucket to get to the index as
25✔
3485
                // it's a sub-bucket.
25✔
3486
                edges := tx.ReadBucket(edgeBucket)
25✔
3487
                if edges == nil {
25✔
NEW
3488
                        return ErrGraphNoEdgesFound
×
NEW
3489
                }
×
3490
                chanIndex := edges.NestedReadBucket(channelPointBucket)
25✔
3491
                if chanIndex == nil {
25✔
NEW
3492
                        return ErrGraphNoEdgesFound
×
NEW
3493
                }
×
3494
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
25✔
3495
                if edgeIndex == nil {
25✔
NEW
3496
                        return ErrGraphNoEdgesFound
×
NEW
3497
                }
×
3498

3499
                // Once we have the proper bucket, we'll range over each key
3500
                // (which is the channel point for the channel) and decode it,
3501
                // accumulating each entry.
3502
                return chanIndex.ForEach(
25✔
3503
                        func(chanPointBytes, chanID []byte) error {
70✔
3504
                                chanPointReader := bytes.NewReader(
45✔
3505
                                        chanPointBytes,
45✔
3506
                                )
45✔
3507

45✔
3508
                                var chanPoint wire.OutPoint
45✔
3509
                                err := ReadOutpoint(chanPointReader, &chanPoint)
45✔
3510
                                if err != nil {
45✔
NEW
3511
                                        return err
×
NEW
3512
                                }
×
3513

3514
                                edgeInfo, err := fetchChanEdgeInfo(
45✔
3515
                                        edgeIndex, chanID,
45✔
3516
                                )
45✔
3517
                                if err != nil {
45✔
NEW
3518
                                        return err
×
NEW
3519
                                }
×
3520

3521
                                pkScript, err := genMultiSigP2WSH(
45✔
3522
                                        edgeInfo.BitcoinKey1Bytes[:],
45✔
3523
                                        edgeInfo.BitcoinKey2Bytes[:],
45✔
3524
                                )
45✔
3525
                                if err != nil {
45✔
NEW
3526
                                        return err
×
NEW
3527
                                }
×
3528

3529
                                edgePoints = append(edgePoints, EdgePoint{
45✔
3530
                                        FundingPkScript: pkScript,
45✔
3531
                                        OutPoint:        chanPoint,
45✔
3532
                                })
45✔
3533

45✔
3534
                                return nil
45✔
3535
                        },
3536
                )
3537
        }, func() {
25✔
3538
                edgePoints = nil
25✔
3539
        }); err != nil {
25✔
NEW
3540
                return nil, err
×
NEW
3541
        }
×
3542

3543
        return edgePoints, nil
25✔
3544
}
3545

3546
// MarkEdgeZombie attempts to mark a channel identified by its channel ID as a
3547
// zombie. This method is used on an ad-hoc basis, when channels need to be
3548
// marked as zombies outside the normal pruning cycle.
3549
func (c *KVStore) MarkEdgeZombie(chanID uint64,
3550
        pubKey1, pubKey2 [33]byte) error {
133✔
3551

133✔
3552
        c.cacheMu.Lock()
133✔
3553
        defer c.cacheMu.Unlock()
133✔
3554

133✔
3555
        err := kvdb.Batch(c.db, func(tx kvdb.RwTx) error {
266✔
3556
                edges := tx.ReadWriteBucket(edgeBucket)
133✔
3557
                if edges == nil {
133✔
NEW
3558
                        return ErrGraphNoEdgesFound
×
NEW
3559
                }
×
3560
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
133✔
3561
                if err != nil {
133✔
NEW
3562
                        return fmt.Errorf("unable to create zombie "+
×
NEW
3563
                                "bucket: %w", err)
×
NEW
3564
                }
×
3565

3566
                return markEdgeZombie(zombieIndex, chanID, pubKey1, pubKey2)
133✔
3567
        })
3568
        if err != nil {
133✔
NEW
3569
                return err
×
NEW
3570
        }
×
3571

3572
        c.rejectCache.remove(chanID)
133✔
3573
        c.chanCache.remove(chanID)
133✔
3574

133✔
3575
        return nil
133✔
3576
}
3577

3578
// markEdgeZombie marks an edge as a zombie within our zombie index. The public
3579
// keys should represent the node public keys of the two parties involved in the
3580
// edge.
3581
func markEdgeZombie(zombieIndex kvdb.RwBucket, chanID uint64, pubKey1,
3582
        pubKey2 [33]byte) error {
158✔
3583

158✔
3584
        var k [8]byte
158✔
3585
        byteOrder.PutUint64(k[:], chanID)
158✔
3586

158✔
3587
        var v [66]byte
158✔
3588
        copy(v[:33], pubKey1[:])
158✔
3589
        copy(v[33:], pubKey2[:])
158✔
3590

158✔
3591
        return zombieIndex.Put(k[:], v[:])
158✔
3592
}
158✔
3593

3594
// MarkEdgeLive clears an edge from our zombie index, deeming it as live.
3595
func (c *KVStore) MarkEdgeLive(chanID uint64) error {
15✔
3596
        c.cacheMu.Lock()
15✔
3597
        defer c.cacheMu.Unlock()
15✔
3598

15✔
3599
        return c.markEdgeLiveUnsafe(nil, chanID)
15✔
3600
}
15✔
3601

3602
// markEdgeLiveUnsafe clears an edge from the zombie index. This method can be
3603
// called with an existing kvdb.RwTx or the argument can be set to nil in which
3604
// case a new transaction will be created.
3605
//
3606
// NOTE: this method MUST only be called if the cacheMu has already been
3607
// acquired.
3608
func (c *KVStore) markEdgeLiveUnsafe(tx kvdb.RwTx, chanID uint64) error {
15✔
3609
        dbFn := func(tx kvdb.RwTx) error {
30✔
3610
                edges := tx.ReadWriteBucket(edgeBucket)
15✔
3611
                if edges == nil {
15✔
NEW
3612
                        return ErrGraphNoEdgesFound
×
NEW
3613
                }
×
3614
                zombieIndex := edges.NestedReadWriteBucket(zombieBucket)
15✔
3615
                if zombieIndex == nil {
15✔
NEW
3616
                        return nil
×
NEW
3617
                }
×
3618

3619
                var k [8]byte
15✔
3620
                byteOrder.PutUint64(k[:], chanID)
15✔
3621

15✔
3622
                if len(zombieIndex.Get(k[:])) == 0 {
16✔
3623
                        return ErrZombieEdgeNotFound
1✔
3624
                }
1✔
3625

3626
                return zombieIndex.Delete(k[:])
14✔
3627
        }
3628

3629
        // If the transaction is nil, we'll create a new one. Otherwise, we use
3630
        // the existing transaction
3631
        var err error
15✔
3632
        if tx == nil {
30✔
3633
                err = kvdb.Update(c.db, dbFn, func() {})
30✔
NEW
3634
        } else {
×
NEW
3635
                err = dbFn(tx)
×
NEW
3636
        }
×
3637
        if err != nil {
16✔
3638
                return err
1✔
3639
        }
1✔
3640

3641
        c.rejectCache.remove(chanID)
14✔
3642
        c.chanCache.remove(chanID)
14✔
3643

14✔
3644
        return nil
14✔
3645
}
3646

3647
// IsZombieEdge returns whether the edge is considered zombie. If it is a
3648
// zombie, then the two node public keys corresponding to this edge are also
3649
// returned.
3650
func (c *KVStore) IsZombieEdge(chanID uint64) (bool, [33]byte, [33]byte) {
14✔
3651
        var (
14✔
3652
                isZombie         bool
14✔
3653
                pubKey1, pubKey2 [33]byte
14✔
3654
        )
14✔
3655

14✔
3656
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
28✔
3657
                edges := tx.ReadBucket(edgeBucket)
14✔
3658
                if edges == nil {
14✔
NEW
3659
                        return ErrGraphNoEdgesFound
×
NEW
3660
                }
×
3661
                zombieIndex := edges.NestedReadBucket(zombieBucket)
14✔
3662
                if zombieIndex == nil {
14✔
NEW
3663
                        return nil
×
NEW
3664
                }
×
3665

3666
                isZombie, pubKey1, pubKey2 = isZombieEdge(zombieIndex, chanID)
14✔
3667

14✔
3668
                return nil
14✔
3669
        }, func() {
14✔
3670
                isZombie = false
14✔
3671
                pubKey1 = [33]byte{}
14✔
3672
                pubKey2 = [33]byte{}
14✔
3673
        })
14✔
3674
        if err != nil {
14✔
NEW
3675
                return false, [33]byte{}, [33]byte{}
×
NEW
3676
        }
×
3677

3678
        return isZombie, pubKey1, pubKey2
14✔
3679
}
3680

3681
// isZombieEdge returns whether an entry exists for the given channel in the
3682
// zombie index. If an entry exists, then the two node public keys corresponding
3683
// to this edge are also returned.
3684
func isZombieEdge(zombieIndex kvdb.RBucket,
3685
        chanID uint64) (bool, [33]byte, [33]byte) {
199✔
3686

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

199✔
3690
        v := zombieIndex.Get(k[:])
199✔
3691
        if v == nil {
317✔
3692
                return false, [33]byte{}, [33]byte{}
118✔
3693
        }
118✔
3694

3695
        var pubKey1, pubKey2 [33]byte
84✔
3696
        copy(pubKey1[:], v[:33])
84✔
3697
        copy(pubKey2[:], v[33:])
84✔
3698

84✔
3699
        return true, pubKey1, pubKey2
84✔
3700
}
3701

3702
// NumZombies returns the current number of zombie channels in the graph.
3703
func (c *KVStore) NumZombies() (uint64, error) {
4✔
3704
        var numZombies uint64
4✔
3705
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
8✔
3706
                edges := tx.ReadBucket(edgeBucket)
4✔
3707
                if edges == nil {
4✔
NEW
3708
                        return nil
×
NEW
3709
                }
×
3710
                zombieIndex := edges.NestedReadBucket(zombieBucket)
4✔
3711
                if zombieIndex == nil {
4✔
NEW
3712
                        return nil
×
NEW
3713
                }
×
3714

3715
                return zombieIndex.ForEach(func(_, _ []byte) error {
6✔
3716
                        numZombies++
2✔
3717
                        return nil
2✔
3718
                })
2✔
3719
        }, func() {
4✔
3720
                numZombies = 0
4✔
3721
        })
4✔
3722
        if err != nil {
4✔
NEW
3723
                return 0, err
×
NEW
3724
        }
×
3725

3726
        return numZombies, nil
4✔
3727
}
3728

3729
// PutClosedScid stores a SCID for a closed channel in the database. This is so
3730
// that we can ignore channel announcements that we know to be closed without
3731
// having to validate them and fetch a block.
3732
func (c *KVStore) PutClosedScid(scid lnwire.ShortChannelID) error {
1✔
3733
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
2✔
3734
                closedScids, err := tx.CreateTopLevelBucket(closedScidBucket)
1✔
3735
                if err != nil {
1✔
NEW
3736
                        return err
×
NEW
3737
                }
×
3738

3739
                var k [8]byte
1✔
3740
                byteOrder.PutUint64(k[:], scid.ToUint64())
1✔
3741

1✔
3742
                return closedScids.Put(k[:], []byte{})
1✔
3743
        }, func() {})
1✔
3744
}
3745

3746
// IsClosedScid checks whether a channel identified by the passed in scid is
3747
// closed. This helps avoid having to perform expensive validation checks.
3748
// TODO: Add an LRU cache to cut down on disc reads.
3749
func (c *KVStore) IsClosedScid(scid lnwire.ShortChannelID) (bool, error) {
5✔
3750
        var isClosed bool
5✔
3751
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
10✔
3752
                closedScids := tx.ReadBucket(closedScidBucket)
5✔
3753
                if closedScids == nil {
5✔
NEW
3754
                        return ErrClosedScidsNotFound
×
NEW
3755
                }
×
3756

3757
                var k [8]byte
5✔
3758
                byteOrder.PutUint64(k[:], scid.ToUint64())
5✔
3759

5✔
3760
                if closedScids.Get(k[:]) != nil {
6✔
3761
                        isClosed = true
1✔
3762
                        return nil
1✔
3763
                }
1✔
3764

3765
                return nil
4✔
3766
        }, func() {
5✔
3767
                isClosed = false
5✔
3768
        })
5✔
3769
        if err != nil {
5✔
NEW
3770
                return false, err
×
NEW
3771
        }
×
3772

3773
        return isClosed, nil
5✔
3774
}
3775

3776
// GraphSession will provide the call-back with access to a NodeTraverser
3777
// instance which can be used to perform queries against the channel graph. If
3778
// the graph cache is not enabled, then the call-back will  be provided with
3779
// access to the graph via a consistent read-only transaction.
3780
func (c *KVStore) GraphSession(cb func(graph NodeTraverser) error) error {
54✔
3781
        return c.db.View(func(tx walletdb.ReadTx) error {
108✔
3782
                return cb(&nodeTraverserSession{
54✔
3783
                        db: c,
54✔
3784
                        tx: tx,
54✔
3785
                })
54✔
3786
        }, func() {})
108✔
3787
}
3788

3789
// nodeTraverserSession implements the NodeTraverser interface but with a
3790
// backing read only transaction for a consistent view of the graph in the case
3791
// where the graph Cache has not been enabled.
3792
type nodeTraverserSession struct {
3793
        tx kvdb.RTx
3794
        db *KVStore
3795
}
3796

3797
// ForEachNodeDirectedChannel calls the callback for every channel of the given
3798
// node.
3799
//
3800
// NOTE: Part of the NodeTraverser interface.
3801
func (c *nodeTraverserSession) ForEachNodeDirectedChannel(nodePub route.Vertex,
3802
        cb func(channel *DirectedChannel) error) error {
239✔
3803

239✔
3804
        return c.db.forEachNodeDirectedChannel(c.tx, nodePub, cb)
239✔
3805
}
239✔
3806

3807
// FetchNodeFeatures returns the features of the given node. If the node is
3808
// unknown, assume no additional features are supported.
3809
//
3810
// NOTE: Part of the NodeTraverser interface.
3811
func (c *nodeTraverserSession) FetchNodeFeatures(nodePub route.Vertex) (
3812
        *lnwire.FeatureVector, error) {
254✔
3813

254✔
3814
        return c.db.fetchNodeFeatures(c.tx, nodePub)
254✔
3815
}
254✔
3816

3817
func putLightningNode(nodeBucket, aliasBucket, updateIndex kvdb.RwBucket,
3818
        node *models.LightningNode) error {
993✔
3819

993✔
3820
        var (
993✔
3821
                scratch [16]byte
993✔
3822
                b       bytes.Buffer
993✔
3823
        )
993✔
3824

993✔
3825
        pub, err := node.PubKey()
993✔
3826
        if err != nil {
993✔
NEW
3827
                return err
×
NEW
3828
        }
×
3829
        nodePub := pub.SerializeCompressed()
993✔
3830

993✔
3831
        // If the node has the update time set, write it, else write 0.
993✔
3832
        updateUnix := uint64(0)
993✔
3833
        if node.LastUpdate.Unix() > 0 {
1,859✔
3834
                updateUnix = uint64(node.LastUpdate.Unix())
866✔
3835
        }
866✔
3836

3837
        byteOrder.PutUint64(scratch[:8], updateUnix)
993✔
3838
        if _, err := b.Write(scratch[:8]); err != nil {
993✔
NEW
3839
                return err
×
NEW
3840
        }
×
3841

3842
        if _, err := b.Write(nodePub); err != nil {
993✔
NEW
3843
                return err
×
NEW
3844
        }
×
3845

3846
        // If we got a node announcement for this node, we will have the rest
3847
        // of the data available. If not we don't have more data to write.
3848
        if !node.HaveNodeAnnouncement {
1,070✔
3849
                // Write HaveNodeAnnouncement=0.
77✔
3850
                byteOrder.PutUint16(scratch[:2], 0)
77✔
3851
                if _, err := b.Write(scratch[:2]); err != nil {
77✔
NEW
3852
                        return err
×
NEW
3853
                }
×
3854

3855
                return nodeBucket.Put(nodePub, b.Bytes())
77✔
3856
        }
3857

3858
        // Write HaveNodeAnnouncement=1.
3859
        byteOrder.PutUint16(scratch[:2], 1)
919✔
3860
        if _, err := b.Write(scratch[:2]); err != nil {
919✔
NEW
3861
                return err
×
NEW
3862
        }
×
3863

3864
        if err := binary.Write(&b, byteOrder, node.Color.R); err != nil {
919✔
NEW
3865
                return err
×
NEW
3866
        }
×
3867
        if err := binary.Write(&b, byteOrder, node.Color.G); err != nil {
919✔
NEW
3868
                return err
×
NEW
3869
        }
×
3870
        if err := binary.Write(&b, byteOrder, node.Color.B); err != nil {
919✔
NEW
3871
                return err
×
NEW
3872
        }
×
3873

3874
        if err := wire.WriteVarString(&b, 0, node.Alias); err != nil {
919✔
NEW
3875
                return err
×
NEW
3876
        }
×
3877

3878
        if err := node.Features.Encode(&b); err != nil {
919✔
NEW
3879
                return err
×
NEW
3880
        }
×
3881

3882
        numAddresses := uint16(len(node.Addresses))
919✔
3883
        byteOrder.PutUint16(scratch[:2], numAddresses)
919✔
3884
        if _, err := b.Write(scratch[:2]); err != nil {
919✔
NEW
3885
                return err
×
NEW
3886
        }
×
3887

3888
        for _, address := range node.Addresses {
2,066✔
3889
                if err := SerializeAddr(&b, address); err != nil {
1,147✔
NEW
3890
                        return err
×
NEW
3891
                }
×
3892
        }
3893

3894
        sigLen := len(node.AuthSigBytes)
919✔
3895
        if sigLen > 80 {
919✔
NEW
3896
                return fmt.Errorf("max sig len allowed is 80, had %v",
×
NEW
3897
                        sigLen)
×
NEW
3898
        }
×
3899

3900
        err = wire.WriteVarBytes(&b, 0, node.AuthSigBytes)
919✔
3901
        if err != nil {
919✔
NEW
3902
                return err
×
NEW
3903
        }
×
3904

3905
        if len(node.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
919✔
NEW
3906
                return ErrTooManyExtraOpaqueBytes(len(node.ExtraOpaqueData))
×
NEW
3907
        }
×
3908
        err = wire.WriteVarBytes(&b, 0, node.ExtraOpaqueData)
919✔
3909
        if err != nil {
919✔
NEW
3910
                return err
×
NEW
3911
        }
×
3912

3913
        if err := aliasBucket.Put(nodePub, []byte(node.Alias)); err != nil {
919✔
NEW
3914
                return err
×
NEW
3915
        }
×
3916

3917
        // With the alias bucket updated, we'll now update the index that
3918
        // tracks the time series of node updates.
3919
        var indexKey [8 + 33]byte
919✔
3920
        byteOrder.PutUint64(indexKey[:8], updateUnix)
919✔
3921
        copy(indexKey[8:], nodePub)
919✔
3922

919✔
3923
        // If there was already an old index entry for this node, then we'll
919✔
3924
        // delete the old one before we write the new entry.
919✔
3925
        if nodeBytes := nodeBucket.Get(nodePub); nodeBytes != nil {
1,026✔
3926
                // Extract out the old update time to we can reconstruct the
107✔
3927
                // prior index key to delete it from the index.
107✔
3928
                oldUpdateTime := nodeBytes[:8]
107✔
3929

107✔
3930
                var oldIndexKey [8 + 33]byte
107✔
3931
                copy(oldIndexKey[:8], oldUpdateTime)
107✔
3932
                copy(oldIndexKey[8:], nodePub)
107✔
3933

107✔
3934
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
107✔
NEW
3935
                        return err
×
NEW
3936
                }
×
3937
        }
3938

3939
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
919✔
NEW
3940
                return err
×
NEW
3941
        }
×
3942

3943
        return nodeBucket.Put(nodePub, b.Bytes())
919✔
3944
}
3945

3946
func fetchLightningNode(nodeBucket kvdb.RBucket,
3947
        nodePub []byte) (models.LightningNode, error) {
3,610✔
3948

3,610✔
3949
        nodeBytes := nodeBucket.Get(nodePub)
3,610✔
3950
        if nodeBytes == nil {
3,686✔
3951
                return models.LightningNode{}, ErrGraphNodeNotFound
76✔
3952
        }
76✔
3953

3954
        nodeReader := bytes.NewReader(nodeBytes)
3,537✔
3955

3,537✔
3956
        return deserializeLightningNode(nodeReader)
3,537✔
3957
}
3958

3959
func deserializeLightningNodeCacheable(r io.Reader) (route.Vertex,
3960
        *lnwire.FeatureVector, error) {
123✔
3961

123✔
3962
        var (
123✔
3963
                pubKey      route.Vertex
123✔
3964
                features    = lnwire.EmptyFeatureVector()
123✔
3965
                nodeScratch [8]byte
123✔
3966
        )
123✔
3967

123✔
3968
        // Skip ahead:
123✔
3969
        // - LastUpdate (8 bytes)
123✔
3970
        if _, err := r.Read(nodeScratch[:]); err != nil {
123✔
NEW
3971
                return pubKey, nil, err
×
NEW
3972
        }
×
3973

3974
        if _, err := io.ReadFull(r, pubKey[:]); err != nil {
123✔
NEW
3975
                return pubKey, nil, err
×
NEW
3976
        }
×
3977

3978
        // Read the node announcement flag.
3979
        if _, err := r.Read(nodeScratch[:2]); err != nil {
123✔
NEW
3980
                return pubKey, nil, err
×
NEW
3981
        }
×
3982
        hasNodeAnn := byteOrder.Uint16(nodeScratch[:2])
123✔
3983

123✔
3984
        // The rest of the data is optional, and will only be there if we got a
123✔
3985
        // node announcement for this node.
123✔
3986
        if hasNodeAnn == 0 {
126✔
3987
                return pubKey, features, nil
3✔
3988
        }
3✔
3989

3990
        // We did get a node announcement for this node, so we'll have the rest
3991
        // of the data available.
3992
        var rgb uint8
123✔
3993
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
123✔
NEW
3994
                return pubKey, nil, err
×
NEW
3995
        }
×
3996
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
123✔
NEW
3997
                return pubKey, nil, err
×
NEW
3998
        }
×
3999
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
123✔
NEW
4000
                return pubKey, nil, err
×
NEW
4001
        }
×
4002

4003
        if _, err := wire.ReadVarString(r, 0); err != nil {
123✔
NEW
4004
                return pubKey, nil, err
×
NEW
4005
        }
×
4006

4007
        if err := features.Decode(r); err != nil {
123✔
NEW
4008
                return pubKey, nil, err
×
NEW
4009
        }
×
4010

4011
        return pubKey, features, nil
123✔
4012
}
4013

4014
func deserializeLightningNode(r io.Reader) (models.LightningNode, error) {
8,496✔
4015
        var (
8,496✔
4016
                node    models.LightningNode
8,496✔
4017
                scratch [8]byte
8,496✔
4018
                err     error
8,496✔
4019
        )
8,496✔
4020

8,496✔
4021
        // Always populate a feature vector, even if we don't have a node
8,496✔
4022
        // announcement and short circuit below.
8,496✔
4023
        node.Features = lnwire.EmptyFeatureVector()
8,496✔
4024

8,496✔
4025
        if _, err := r.Read(scratch[:]); err != nil {
8,496✔
NEW
4026
                return models.LightningNode{}, err
×
NEW
4027
        }
×
4028

4029
        unix := int64(byteOrder.Uint64(scratch[:]))
8,496✔
4030
        node.LastUpdate = time.Unix(unix, 0)
8,496✔
4031

8,496✔
4032
        if _, err := io.ReadFull(r, node.PubKeyBytes[:]); err != nil {
8,496✔
NEW
4033
                return models.LightningNode{}, err
×
NEW
4034
        }
×
4035

4036
        if _, err := r.Read(scratch[:2]); err != nil {
8,496✔
NEW
4037
                return models.LightningNode{}, err
×
NEW
4038
        }
×
4039

4040
        hasNodeAnn := byteOrder.Uint16(scratch[:2])
8,496✔
4041
        if hasNodeAnn == 1 {
16,851✔
4042
                node.HaveNodeAnnouncement = true
8,355✔
4043
        } else {
8,499✔
4044
                node.HaveNodeAnnouncement = false
144✔
4045
        }
144✔
4046

4047
        // The rest of the data is optional, and will only be there if we got a
4048
        // node announcement for this node.
4049
        if !node.HaveNodeAnnouncement {
8,640✔
4050
                return node, nil
144✔
4051
        }
144✔
4052

4053
        // We did get a node announcement for this node, so we'll have the rest
4054
        // of the data available.
4055
        if err := binary.Read(r, byteOrder, &node.Color.R); err != nil {
8,355✔
NEW
4056
                return models.LightningNode{}, err
×
NEW
4057
        }
×
4058
        if err := binary.Read(r, byteOrder, &node.Color.G); err != nil {
8,355✔
NEW
4059
                return models.LightningNode{}, err
×
NEW
4060
        }
×
4061
        if err := binary.Read(r, byteOrder, &node.Color.B); err != nil {
8,355✔
NEW
4062
                return models.LightningNode{}, err
×
NEW
4063
        }
×
4064

4065
        node.Alias, err = wire.ReadVarString(r, 0)
8,355✔
4066
        if err != nil {
8,355✔
NEW
4067
                return models.LightningNode{}, err
×
NEW
4068
        }
×
4069

4070
        err = node.Features.Decode(r)
8,355✔
4071
        if err != nil {
8,355✔
NEW
4072
                return models.LightningNode{}, err
×
NEW
4073
        }
×
4074

4075
        if _, err := r.Read(scratch[:2]); err != nil {
8,355✔
NEW
4076
                return models.LightningNode{}, err
×
NEW
4077
        }
×
4078
        numAddresses := int(byteOrder.Uint16(scratch[:2]))
8,355✔
4079

8,355✔
4080
        var addresses []net.Addr
8,355✔
4081
        for i := 0; i < numAddresses; i++ {
18,933✔
4082
                address, err := DeserializeAddr(r)
10,578✔
4083
                if err != nil {
10,578✔
NEW
4084
                        return models.LightningNode{}, err
×
NEW
4085
                }
×
4086
                addresses = append(addresses, address)
10,578✔
4087
        }
4088
        node.Addresses = addresses
8,355✔
4089

8,355✔
4090
        node.AuthSigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
8,355✔
4091
        if err != nil {
8,355✔
NEW
4092
                return models.LightningNode{}, err
×
NEW
4093
        }
×
4094

4095
        // We'll try and see if there are any opaque bytes left, if not, then
4096
        // we'll ignore the EOF error and return the node as is.
4097
        node.ExtraOpaqueData, err = wire.ReadVarBytes(
8,355✔
4098
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
8,355✔
4099
        )
8,355✔
4100
        switch {
8,355✔
NEW
4101
        case errors.Is(err, io.ErrUnexpectedEOF):
×
NEW
4102
        case errors.Is(err, io.EOF):
×
NEW
4103
        case err != nil:
×
NEW
4104
                return models.LightningNode{}, err
×
4105
        }
4106

4107
        return node, nil
8,355✔
4108
}
4109

4110
func putChanEdgeInfo(edgeIndex kvdb.RwBucket,
4111
        edgeInfo *models.ChannelEdgeInfo, chanID [8]byte) error {
1,486✔
4112

1,486✔
4113
        var b bytes.Buffer
1,486✔
4114

1,486✔
4115
        if _, err := b.Write(edgeInfo.NodeKey1Bytes[:]); err != nil {
1,486✔
NEW
4116
                return err
×
NEW
4117
        }
×
4118
        if _, err := b.Write(edgeInfo.NodeKey2Bytes[:]); err != nil {
1,486✔
NEW
4119
                return err
×
NEW
4120
        }
×
4121
        if _, err := b.Write(edgeInfo.BitcoinKey1Bytes[:]); err != nil {
1,486✔
NEW
4122
                return err
×
NEW
4123
        }
×
4124
        if _, err := b.Write(edgeInfo.BitcoinKey2Bytes[:]); err != nil {
1,486✔
NEW
4125
                return err
×
NEW
4126
        }
×
4127

4128
        if err := wire.WriteVarBytes(&b, 0, edgeInfo.Features); err != nil {
1,486✔
NEW
4129
                return err
×
NEW
4130
        }
×
4131

4132
        authProof := edgeInfo.AuthProof
1,486✔
4133
        var nodeSig1, nodeSig2, bitcoinSig1, bitcoinSig2 []byte
1,486✔
4134
        if authProof != nil {
2,889✔
4135
                nodeSig1 = authProof.NodeSig1Bytes
1,403✔
4136
                nodeSig2 = authProof.NodeSig2Bytes
1,403✔
4137
                bitcoinSig1 = authProof.BitcoinSig1Bytes
1,403✔
4138
                bitcoinSig2 = authProof.BitcoinSig2Bytes
1,403✔
4139
        }
1,403✔
4140

4141
        if err := wire.WriteVarBytes(&b, 0, nodeSig1); err != nil {
1,486✔
NEW
4142
                return err
×
NEW
4143
        }
×
4144
        if err := wire.WriteVarBytes(&b, 0, nodeSig2); err != nil {
1,486✔
NEW
4145
                return err
×
NEW
4146
        }
×
4147
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig1); err != nil {
1,486✔
NEW
4148
                return err
×
NEW
4149
        }
×
4150
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig2); err != nil {
1,486✔
NEW
4151
                return err
×
NEW
4152
        }
×
4153

4154
        if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
1,486✔
NEW
4155
                return err
×
NEW
4156
        }
×
4157
        err := binary.Write(&b, byteOrder, uint64(edgeInfo.Capacity))
1,486✔
4158
        if err != nil {
1,486✔
NEW
4159
                return err
×
NEW
4160
        }
×
4161
        if _, err := b.Write(chanID[:]); err != nil {
1,486✔
NEW
4162
                return err
×
NEW
4163
        }
×
4164
        if _, err := b.Write(edgeInfo.ChainHash[:]); err != nil {
1,486✔
NEW
4165
                return err
×
NEW
4166
        }
×
4167

4168
        if len(edgeInfo.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
1,486✔
NEW
4169
                return ErrTooManyExtraOpaqueBytes(len(edgeInfo.ExtraOpaqueData))
×
NEW
4170
        }
×
4171
        err = wire.WriteVarBytes(&b, 0, edgeInfo.ExtraOpaqueData)
1,486✔
4172
        if err != nil {
1,486✔
NEW
4173
                return err
×
NEW
4174
        }
×
4175

4176
        return edgeIndex.Put(chanID[:], b.Bytes())
1,486✔
4177
}
4178

4179
func fetchChanEdgeInfo(edgeIndex kvdb.RBucket,
4180
        chanID []byte) (models.ChannelEdgeInfo, error) {
6,799✔
4181

6,799✔
4182
        edgeInfoBytes := edgeIndex.Get(chanID)
6,799✔
4183
        if edgeInfoBytes == nil {
6,874✔
4184
                return models.ChannelEdgeInfo{}, ErrEdgeNotFound
75✔
4185
        }
75✔
4186

4187
        edgeInfoReader := bytes.NewReader(edgeInfoBytes)
6,727✔
4188
        return deserializeChanEdgeInfo(edgeInfoReader)
6,727✔
4189
}
4190

4191
func deserializeChanEdgeInfo(r io.Reader) (models.ChannelEdgeInfo, error) {
7,266✔
4192
        var (
7,266✔
4193
                err      error
7,266✔
4194
                edgeInfo models.ChannelEdgeInfo
7,266✔
4195
        )
7,266✔
4196

7,266✔
4197
        if _, err := io.ReadFull(r, edgeInfo.NodeKey1Bytes[:]); err != nil {
7,266✔
NEW
4198
                return models.ChannelEdgeInfo{}, err
×
NEW
4199
        }
×
4200
        if _, err := io.ReadFull(r, edgeInfo.NodeKey2Bytes[:]); err != nil {
7,266✔
NEW
4201
                return models.ChannelEdgeInfo{}, err
×
NEW
4202
        }
×
4203
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey1Bytes[:]); err != nil {
7,266✔
NEW
4204
                return models.ChannelEdgeInfo{}, err
×
NEW
4205
        }
×
4206
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey2Bytes[:]); err != nil {
7,266✔
NEW
4207
                return models.ChannelEdgeInfo{}, err
×
NEW
4208
        }
×
4209

4210
        edgeInfo.Features, err = wire.ReadVarBytes(r, 0, 900, "features")
7,266✔
4211
        if err != nil {
7,266✔
NEW
4212
                return models.ChannelEdgeInfo{}, err
×
NEW
4213
        }
×
4214

4215
        proof := &models.ChannelAuthProof{}
7,266✔
4216

7,266✔
4217
        proof.NodeSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
7,266✔
4218
        if err != nil {
7,266✔
NEW
4219
                return models.ChannelEdgeInfo{}, err
×
NEW
4220
        }
×
4221
        proof.NodeSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
7,266✔
4222
        if err != nil {
7,266✔
NEW
4223
                return models.ChannelEdgeInfo{}, err
×
NEW
4224
        }
×
4225
        proof.BitcoinSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
7,266✔
4226
        if err != nil {
7,266✔
NEW
4227
                return models.ChannelEdgeInfo{}, err
×
NEW
4228
        }
×
4229
        proof.BitcoinSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
7,266✔
4230
        if err != nil {
7,266✔
NEW
4231
                return models.ChannelEdgeInfo{}, err
×
NEW
4232
        }
×
4233

4234
        if !proof.IsEmpty() {
11,431✔
4235
                edgeInfo.AuthProof = proof
4,165✔
4236
        }
4,165✔
4237

4238
        edgeInfo.ChannelPoint = wire.OutPoint{}
7,266✔
4239
        if err := ReadOutpoint(r, &edgeInfo.ChannelPoint); err != nil {
7,266✔
NEW
4240
                return models.ChannelEdgeInfo{}, err
×
NEW
4241
        }
×
4242
        if err := binary.Read(r, byteOrder, &edgeInfo.Capacity); err != nil {
7,266✔
NEW
4243
                return models.ChannelEdgeInfo{}, err
×
NEW
4244
        }
×
4245
        if err := binary.Read(r, byteOrder, &edgeInfo.ChannelID); err != nil {
7,266✔
NEW
4246
                return models.ChannelEdgeInfo{}, err
×
NEW
4247
        }
×
4248

4249
        if _, err := io.ReadFull(r, edgeInfo.ChainHash[:]); err != nil {
7,266✔
NEW
4250
                return models.ChannelEdgeInfo{}, err
×
NEW
4251
        }
×
4252

4253
        // We'll try and see if there are any opaque bytes left, if not, then
4254
        // we'll ignore the EOF error and return the edge as is.
4255
        edgeInfo.ExtraOpaqueData, err = wire.ReadVarBytes(
7,266✔
4256
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
7,266✔
4257
        )
7,266✔
4258
        switch {
7,266✔
NEW
4259
        case errors.Is(err, io.ErrUnexpectedEOF):
×
NEW
4260
        case errors.Is(err, io.EOF):
×
NEW
4261
        case err != nil:
×
NEW
4262
                return models.ChannelEdgeInfo{}, err
×
4263
        }
4264

4265
        return edgeInfo, nil
7,266✔
4266
}
4267

4268
func putChanEdgePolicy(edges kvdb.RwBucket, edge *models.ChannelEdgePolicy,
4269
        from, to []byte) error {
2,663✔
4270

2,663✔
4271
        var edgeKey [33 + 8]byte
2,663✔
4272
        copy(edgeKey[:], from)
2,663✔
4273
        byteOrder.PutUint64(edgeKey[33:], edge.ChannelID)
2,663✔
4274

2,663✔
4275
        var b bytes.Buffer
2,663✔
4276
        if err := serializeChanEdgePolicy(&b, edge, to); err != nil {
2,663✔
NEW
4277
                return err
×
NEW
4278
        }
×
4279

4280
        // Before we write out the new edge, we'll create a new entry in the
4281
        // update index in order to keep it fresh.
4282
        updateUnix := uint64(edge.LastUpdate.Unix())
2,663✔
4283
        var indexKey [8 + 8]byte
2,663✔
4284
        byteOrder.PutUint64(indexKey[:8], updateUnix)
2,663✔
4285
        byteOrder.PutUint64(indexKey[8:], edge.ChannelID)
2,663✔
4286

2,663✔
4287
        updateIndex, err := edges.CreateBucketIfNotExists(edgeUpdateIndexBucket)
2,663✔
4288
        if err != nil {
2,663✔
NEW
4289
                return err
×
NEW
4290
        }
×
4291

4292
        // If there was already an entry for this edge, then we'll need to
4293
        // delete the old one to ensure we don't leave around any after-images.
4294
        // An unknown policy value does not have a update time recorded, so
4295
        // it also does not need to be removed.
4296
        if edgeBytes := edges.Get(edgeKey[:]); edgeBytes != nil &&
2,663✔
4297
                !bytes.Equal(edgeBytes, unknownPolicy) {
2,690✔
4298

27✔
4299
                // In order to delete the old entry, we'll need to obtain the
27✔
4300
                // *prior* update time in order to delete it. To do this, we'll
27✔
4301
                // need to deserialize the existing policy within the database
27✔
4302
                // (now outdated by the new one), and delete its corresponding
27✔
4303
                // entry within the update index. We'll ignore any
27✔
4304
                // ErrEdgePolicyOptionalFieldNotFound error, as we only need
27✔
4305
                // the channel ID and update time to delete the entry.
27✔
4306
                // TODO(halseth): get rid of these invalid policies in a
27✔
4307
                // migration.
27✔
4308
                oldEdgePolicy, err := deserializeChanEdgePolicy(
27✔
4309
                        bytes.NewReader(edgeBytes),
27✔
4310
                )
27✔
4311
                if err != nil &&
27✔
4312
                        !errors.Is(err, ErrEdgePolicyOptionalFieldNotFound) {
27✔
NEW
4313

×
NEW
4314
                        return err
×
NEW
4315
                }
×
4316

4317
                oldUpdateTime := uint64(oldEdgePolicy.LastUpdate.Unix())
27✔
4318

27✔
4319
                var oldIndexKey [8 + 8]byte
27✔
4320
                byteOrder.PutUint64(oldIndexKey[:8], oldUpdateTime)
27✔
4321
                byteOrder.PutUint64(oldIndexKey[8:], edge.ChannelID)
27✔
4322

27✔
4323
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
27✔
NEW
4324
                        return err
×
NEW
4325
                }
×
4326
        }
4327

4328
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
2,663✔
NEW
4329
                return err
×
NEW
4330
        }
×
4331

4332
        err = updateEdgePolicyDisabledIndex(
2,663✔
4333
                edges, edge.ChannelID,
2,663✔
4334
                edge.ChannelFlags&lnwire.ChanUpdateDirection > 0,
2,663✔
4335
                edge.IsDisabled(),
2,663✔
4336
        )
2,663✔
4337
        if err != nil {
2,663✔
NEW
4338
                return err
×
NEW
4339
        }
×
4340

4341
        return edges.Put(edgeKey[:], b.Bytes())
2,663✔
4342
}
4343

4344
// updateEdgePolicyDisabledIndex is used to update the disabledEdgePolicyIndex
4345
// bucket by either add a new disabled ChannelEdgePolicy or remove an existing
4346
// one.
4347
// The direction represents the direction of the edge and disabled is used for
4348
// deciding whether to remove or add an entry to the bucket.
4349
// In general a channel is disabled if two entries for the same chanID exist
4350
// in this bucket.
4351
// Maintaining the bucket this way allows a fast retrieval of disabled
4352
// channels, for example when prune is needed.
4353
func updateEdgePolicyDisabledIndex(edges kvdb.RwBucket, chanID uint64,
4354
        direction bool, disabled bool) error {
2,945✔
4355

2,945✔
4356
        var disabledEdgeKey [8 + 1]byte
2,945✔
4357
        byteOrder.PutUint64(disabledEdgeKey[0:], chanID)
2,945✔
4358
        if direction {
4,415✔
4359
                disabledEdgeKey[8] = 1
1,470✔
4360
        }
1,470✔
4361

4362
        disabledEdgePolicyIndex, err := edges.CreateBucketIfNotExists(
2,945✔
4363
                disabledEdgePolicyBucket,
2,945✔
4364
        )
2,945✔
4365
        if err != nil {
2,945✔
NEW
4366
                return err
×
NEW
4367
        }
×
4368

4369
        if disabled {
2,974✔
4370
                return disabledEdgePolicyIndex.Put(disabledEdgeKey[:], []byte{})
29✔
4371
        }
29✔
4372

4373
        return disabledEdgePolicyIndex.Delete(disabledEdgeKey[:])
2,919✔
4374
}
4375

4376
// putChanEdgePolicyUnknown marks the edge policy as unknown
4377
// in the edges bucket.
4378
func putChanEdgePolicyUnknown(edges kvdb.RwBucket, channelID uint64,
4379
        from []byte) error {
2,967✔
4380

2,967✔
4381
        var edgeKey [33 + 8]byte
2,967✔
4382
        copy(edgeKey[:], from)
2,967✔
4383
        byteOrder.PutUint64(edgeKey[33:], channelID)
2,967✔
4384

2,967✔
4385
        if edges.Get(edgeKey[:]) != nil {
2,967✔
NEW
4386
                return fmt.Errorf("cannot write unknown policy for channel %v "+
×
NEW
4387
                        " when there is already a policy present", channelID)
×
NEW
4388
        }
×
4389

4390
        return edges.Put(edgeKey[:], unknownPolicy)
2,967✔
4391
}
4392

4393
func fetchChanEdgePolicy(edges kvdb.RBucket, chanID []byte,
4394
        nodePub []byte) (*models.ChannelEdgePolicy, error) {
13,455✔
4395

13,455✔
4396
        var edgeKey [33 + 8]byte
13,455✔
4397
        copy(edgeKey[:], nodePub)
13,455✔
4398
        copy(edgeKey[33:], chanID)
13,455✔
4399

13,455✔
4400
        edgeBytes := edges.Get(edgeKey[:])
13,455✔
4401
        if edgeBytes == nil {
13,455✔
NEW
4402
                return nil, ErrEdgeNotFound
×
NEW
4403
        }
×
4404

4405
        // No need to deserialize unknown policy.
4406
        if bytes.Equal(edgeBytes, unknownPolicy) {
14,911✔
4407
                return nil, nil
1,456✔
4408
        }
1,456✔
4409

4410
        edgeReader := bytes.NewReader(edgeBytes)
12,002✔
4411

12,002✔
4412
        ep, err := deserializeChanEdgePolicy(edgeReader)
12,002✔
4413
        switch {
12,002✔
4414
        // If the db policy was missing an expected optional field, we return
4415
        // nil as if the policy was unknown.
4416
        case errors.Is(err, ErrEdgePolicyOptionalFieldNotFound):
1✔
4417
                return nil, nil
1✔
4418

NEW
4419
        case err != nil:
×
NEW
4420
                return nil, err
×
4421
        }
4422

4423
        return ep, nil
12,001✔
4424
}
4425

4426
func fetchChanEdgePolicies(edgeIndex kvdb.RBucket, edges kvdb.RBucket,
4427
        chanID []byte) (*models.ChannelEdgePolicy, *models.ChannelEdgePolicy,
4428
        error) {
2,889✔
4429

2,889✔
4430
        edgeInfo := edgeIndex.Get(chanID)
2,889✔
4431
        if edgeInfo == nil {
2,889✔
NEW
4432
                return nil, nil, fmt.Errorf("%w: chanID=%x", ErrEdgeNotFound,
×
NEW
4433
                        chanID)
×
NEW
4434
        }
×
4435

4436
        // The first node is contained within the first half of the edge
4437
        // information. We only propagate the error here and below if it's
4438
        // something other than edge non-existence.
4439
        node1Pub := edgeInfo[:33]
2,889✔
4440
        edge1, err := fetchChanEdgePolicy(edges, chanID, node1Pub)
2,889✔
4441
        if err != nil {
2,889✔
NEW
4442
                return nil, nil, fmt.Errorf("%w: node1Pub=%x", ErrEdgeNotFound,
×
NEW
4443
                        node1Pub)
×
NEW
4444
        }
×
4445

4446
        // Similarly, the second node is contained within the latter
4447
        // half of the edge information.
4448
        node2Pub := edgeInfo[33:66]
2,889✔
4449
        edge2, err := fetchChanEdgePolicy(edges, chanID, node2Pub)
2,889✔
4450
        if err != nil {
2,889✔
NEW
4451
                return nil, nil, fmt.Errorf("%w: node2Pub=%x", ErrEdgeNotFound,
×
NEW
4452
                        node2Pub)
×
NEW
4453
        }
×
4454

4455
        return edge1, edge2, nil
2,889✔
4456
}
4457

4458
func serializeChanEdgePolicy(w io.Writer, edge *models.ChannelEdgePolicy,
4459
        to []byte) error {
2,665✔
4460

2,665✔
4461
        err := wire.WriteVarBytes(w, 0, edge.SigBytes)
2,665✔
4462
        if err != nil {
2,665✔
NEW
4463
                return err
×
NEW
4464
        }
×
4465

4466
        if err := binary.Write(w, byteOrder, edge.ChannelID); err != nil {
2,665✔
NEW
4467
                return err
×
NEW
4468
        }
×
4469

4470
        var scratch [8]byte
2,665✔
4471
        updateUnix := uint64(edge.LastUpdate.Unix())
2,665✔
4472
        byteOrder.PutUint64(scratch[:], updateUnix)
2,665✔
4473
        if _, err := w.Write(scratch[:]); err != nil {
2,665✔
NEW
4474
                return err
×
NEW
4475
        }
×
4476

4477
        if err := binary.Write(w, byteOrder, edge.MessageFlags); err != nil {
2,665✔
NEW
4478
                return err
×
NEW
4479
        }
×
4480
        if err := binary.Write(w, byteOrder, edge.ChannelFlags); err != nil {
2,665✔
NEW
4481
                return err
×
NEW
4482
        }
×
4483
        if err := binary.Write(w, byteOrder, edge.TimeLockDelta); err != nil {
2,665✔
NEW
4484
                return err
×
NEW
4485
        }
×
4486
        if err := binary.Write(w, byteOrder, uint64(edge.MinHTLC)); err != nil {
2,665✔
NEW
4487
                return err
×
NEW
4488
        }
×
4489
        err = binary.Write(w, byteOrder, uint64(edge.FeeBaseMSat))
2,665✔
4490
        if err != nil {
2,665✔
NEW
4491
                return err
×
NEW
4492
        }
×
4493
        err = binary.Write(
2,665✔
4494
                w, byteOrder, uint64(edge.FeeProportionalMillionths),
2,665✔
4495
        )
2,665✔
4496
        if err != nil {
2,665✔
NEW
4497
                return err
×
NEW
4498
        }
×
4499

4500
        if _, err := w.Write(to); err != nil {
2,665✔
NEW
4501
                return err
×
NEW
4502
        }
×
4503

4504
        // If the max_htlc field is present, we write it. To be compatible with
4505
        // older versions that wasn't aware of this field, we write it as part
4506
        // of the opaque data.
4507
        // TODO(halseth): clean up when moving to TLV.
4508
        var opaqueBuf bytes.Buffer
2,665✔
4509
        if edge.MessageFlags.HasMaxHtlc() {
4,946✔
4510
                err := binary.Write(&opaqueBuf, byteOrder, uint64(edge.MaxHTLC))
2,281✔
4511
                if err != nil {
2,281✔
NEW
4512
                        return err
×
NEW
4513
                }
×
4514
        }
4515

4516
        if len(edge.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
2,665✔
NEW
4517
                return ErrTooManyExtraOpaqueBytes(len(edge.ExtraOpaqueData))
×
NEW
4518
        }
×
4519
        if _, err := opaqueBuf.Write(edge.ExtraOpaqueData); err != nil {
2,665✔
NEW
4520
                return err
×
NEW
4521
        }
×
4522

4523
        if err := wire.WriteVarBytes(w, 0, opaqueBuf.Bytes()); err != nil {
2,665✔
NEW
4524
                return err
×
NEW
4525
        }
×
4526
        return nil
2,665✔
4527
}
4528

4529
func deserializeChanEdgePolicy(r io.Reader) (*models.ChannelEdgePolicy, error) {
12,027✔
4530
        // Deserialize the policy. Note that in case an optional field is not
12,027✔
4531
        // found, both an error and a populated policy object are returned.
12,027✔
4532
        edge, deserializeErr := deserializeChanEdgePolicyRaw(r)
12,027✔
4533
        if deserializeErr != nil &&
12,027✔
4534
                !errors.Is(deserializeErr, ErrEdgePolicyOptionalFieldNotFound) {
12,027✔
NEW
4535

×
NEW
4536
                return nil, deserializeErr
×
NEW
4537
        }
×
4538

4539
        return edge, deserializeErr
12,027✔
4540
}
4541

4542
func deserializeChanEdgePolicyRaw(r io.Reader) (*models.ChannelEdgePolicy,
4543
        error) {
13,034✔
4544

13,034✔
4545
        edge := &models.ChannelEdgePolicy{}
13,034✔
4546

13,034✔
4547
        var err error
13,034✔
4548
        edge.SigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
13,034✔
4549
        if err != nil {
13,034✔
NEW
4550
                return nil, err
×
NEW
4551
        }
×
4552

4553
        if err := binary.Read(r, byteOrder, &edge.ChannelID); err != nil {
13,034✔
NEW
4554
                return nil, err
×
NEW
4555
        }
×
4556

4557
        var scratch [8]byte
13,034✔
4558
        if _, err := r.Read(scratch[:]); err != nil {
13,034✔
NEW
4559
                return nil, err
×
NEW
4560
        }
×
4561
        unix := int64(byteOrder.Uint64(scratch[:]))
13,034✔
4562
        edge.LastUpdate = time.Unix(unix, 0)
13,034✔
4563

13,034✔
4564
        if err := binary.Read(r, byteOrder, &edge.MessageFlags); err != nil {
13,034✔
NEW
4565
                return nil, err
×
NEW
4566
        }
×
4567
        if err := binary.Read(r, byteOrder, &edge.ChannelFlags); err != nil {
13,034✔
NEW
4568
                return nil, err
×
NEW
4569
        }
×
4570
        if err := binary.Read(r, byteOrder, &edge.TimeLockDelta); err != nil {
13,034✔
NEW
4571
                return nil, err
×
NEW
4572
        }
×
4573

4574
        var n uint64
13,034✔
4575
        if err := binary.Read(r, byteOrder, &n); err != nil {
13,034✔
NEW
4576
                return nil, err
×
NEW
4577
        }
×
4578
        edge.MinHTLC = lnwire.MilliSatoshi(n)
13,034✔
4579

13,034✔
4580
        if err := binary.Read(r, byteOrder, &n); err != nil {
13,034✔
NEW
4581
                return nil, err
×
NEW
4582
        }
×
4583
        edge.FeeBaseMSat = lnwire.MilliSatoshi(n)
13,034✔
4584

13,034✔
4585
        if err := binary.Read(r, byteOrder, &n); err != nil {
13,034✔
NEW
4586
                return nil, err
×
NEW
4587
        }
×
4588
        edge.FeeProportionalMillionths = lnwire.MilliSatoshi(n)
13,034✔
4589

13,034✔
4590
        if _, err := r.Read(edge.ToNode[:]); err != nil {
13,034✔
NEW
4591
                return nil, err
×
NEW
4592
        }
×
4593

4594
        // We'll try and see if there are any opaque bytes left, if not, then
4595
        // we'll ignore the EOF error and return the edge as is.
4596
        edge.ExtraOpaqueData, err = wire.ReadVarBytes(
13,034✔
4597
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
13,034✔
4598
        )
13,034✔
4599
        switch {
13,034✔
NEW
4600
        case errors.Is(err, io.ErrUnexpectedEOF):
×
4601
        case errors.Is(err, io.EOF):
3✔
NEW
4602
        case err != nil:
×
NEW
4603
                return nil, err
×
4604
        }
4605

4606
        // See if optional fields are present.
4607
        if edge.MessageFlags.HasMaxHtlc() {
25,089✔
4608
                // The max_htlc field should be at the beginning of the opaque
12,055✔
4609
                // bytes.
12,055✔
4610
                opq := edge.ExtraOpaqueData
12,055✔
4611

12,055✔
4612
                // If the max_htlc field is not present, it might be old data
12,055✔
4613
                // stored before this field was validated. We'll return the
12,055✔
4614
                // edge along with an error.
12,055✔
4615
                if len(opq) < 8 {
12,058✔
4616
                        return edge, ErrEdgePolicyOptionalFieldNotFound
3✔
4617
                }
3✔
4618

4619
                maxHtlc := byteOrder.Uint64(opq[:8])
12,052✔
4620
                edge.MaxHTLC = lnwire.MilliSatoshi(maxHtlc)
12,052✔
4621

12,052✔
4622
                // Exclude the parsed field from the rest of the opaque data.
12,052✔
4623
                edge.ExtraOpaqueData = opq[8:]
12,052✔
4624
        }
4625

4626
        return edge, nil
13,031✔
4627
}
4628

4629
// chanGraphNodeTx is an implementation of the NodeRTx interface backed by the
4630
// KVStore and a kvdb.RTx.
4631
type chanGraphNodeTx struct {
4632
        tx   kvdb.RTx
4633
        db   *KVStore
4634
        node *models.LightningNode
4635
}
4636

4637
// A compile-time constraint to ensure chanGraphNodeTx implements the NodeRTx
4638
// interface.
4639
var _ NodeRTx = (*chanGraphNodeTx)(nil)
4640

4641
func newChanGraphNodeTx(tx kvdb.RTx, db *KVStore,
4642
        node *models.LightningNode) *chanGraphNodeTx {
3,917✔
4643

3,917✔
4644
        return &chanGraphNodeTx{
3,917✔
4645
                tx:   tx,
3,917✔
4646
                db:   db,
3,917✔
4647
                node: node,
3,917✔
4648
        }
3,917✔
4649
}
3,917✔
4650

4651
// Node returns the raw information of the node.
4652
//
4653
// NOTE: This is a part of the NodeRTx interface.
4654
func (c *chanGraphNodeTx) Node() *models.LightningNode {
4,842✔
4655
        return c.node
4,842✔
4656
}
4,842✔
4657

4658
// FetchNode fetches the node with the given pub key under the same transaction
4659
// used to fetch the current node. The returned node is also a NodeRTx and any
4660
// operations on that NodeRTx will also be done under the same transaction.
4661
//
4662
// NOTE: This is a part of the NodeRTx interface.
4663
func (c *chanGraphNodeTx) FetchNode(nodePub route.Vertex) (NodeRTx, error) {
2,944✔
4664
        node, err := c.db.FetchLightningNodeTx(c.tx, nodePub)
2,944✔
4665
        if err != nil {
2,944✔
NEW
4666
                return nil, err
×
NEW
4667
        }
×
4668

4669
        return newChanGraphNodeTx(c.tx, c.db, node), nil
2,944✔
4670
}
4671

4672
// ForEachChannel can be used to iterate over the node's channels under
4673
// the same transaction used to fetch the node.
4674
//
4675
// NOTE: This is a part of the NodeRTx interface.
4676
func (c *chanGraphNodeTx) ForEachChannel(f func(*models.ChannelEdgeInfo,
4677
        *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
965✔
4678

965✔
4679
        return c.db.ForEachNodeChannelTx(c.tx, c.node.PubKeyBytes,
965✔
4680
                func(_ kvdb.RTx, info *models.ChannelEdgeInfo, policy1,
965✔
4681
                        policy2 *models.ChannelEdgePolicy) error {
3,909✔
4682

2,944✔
4683
                        return f(info, policy1, policy2)
2,944✔
4684
                },
2,944✔
4685
        )
4686
}
4687

4688
// MakeTestGraph creates a new instance of the KVStore for testing
4689
// purposes.
4690
func MakeTestGraph(t testing.TB, modifiers ...KVStoreOptionModifier) (
4691
        *ChannelGraph, error) {
41✔
4692

41✔
4693
        opts := DefaultOptions()
41✔
4694
        for _, modifier := range modifiers {
41✔
NEW
4695
                modifier(opts)
×
NEW
4696
        }
×
4697

4698
        // Next, create KVStore for the first time.
4699
        backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
41✔
4700
        if err != nil {
41✔
NEW
4701
                backendCleanup()
×
NEW
4702
                return nil, err
×
NEW
4703
        }
×
4704

4705
        graph, err := NewChannelGraph(&Config{
41✔
4706
                KVDB:        backend,
41✔
4707
                KVStoreOpts: modifiers,
41✔
4708
        })
41✔
4709
        if err != nil {
41✔
NEW
4710
                backendCleanup()
×
NEW
4711
                return nil, err
×
NEW
4712
        }
×
4713
        require.NoError(t, graph.Start())
41✔
4714

41✔
4715
        t.Cleanup(func() {
82✔
4716
                _ = backend.Close()
41✔
4717
                backendCleanup()
41✔
4718
                require.NoError(t, graph.Stop())
41✔
4719
        })
41✔
4720

4721
        return graph, nil
41✔
4722
}
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