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

lightningnetwork / lnd / 15016440952

14 May 2025 08:51AM UTC coverage: 69.031% (+0.03%) from 68.997%
15016440952

Pull #9692

github

web-flow
Merge b7e72b2ef into b0cba7dd0
Pull Request #9692: [graph-work-side-branch]: temp side branch for graph work

292 of 349 new or added lines in 32 files covered. (83.67%)

45 existing lines in 13 files now uncovered.

134025 of 194151 relevant lines covered (69.03%)

22047.58 hits per line

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

77.77
/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
// A compile-time assertion to ensure that the KVStore struct implements the
200
// V1Store interface.
201
var _ V1Store = (*KVStore)(nil)
202

203
// NewKVStore allocates a new KVStore backed by a DB instance. The
204
// returned instance has its own unique reject cache and channel cache.
205
func NewKVStore(db kvdb.Backend, options ...KVStoreOptionModifier) (*KVStore,
206
        error) {
172✔
207

172✔
208
        opts := DefaultOptions()
172✔
209
        for _, o := range options {
175✔
210
                o(opts)
3✔
211
        }
3✔
212

213
        if !opts.NoMigration {
344✔
214
                if err := initKVStore(db); err != nil {
172✔
215
                        return nil, err
×
216
                }
×
217
        }
218

219
        g := &KVStore{
172✔
220
                db:          db,
172✔
221
                rejectCache: newRejectCache(opts.RejectCacheSize),
172✔
222
                chanCache:   newChannelCache(opts.ChannelCacheSize),
172✔
223
        }
172✔
224
        g.chanScheduler = batch.NewTimeScheduler(
172✔
225
                db, &g.cacheMu, opts.BatchCommitInterval,
172✔
226
        )
172✔
227
        g.nodeScheduler = batch.NewTimeScheduler(
172✔
228
                db, nil, opts.BatchCommitInterval,
172✔
229
        )
172✔
230

172✔
231
        return g, nil
172✔
232
}
233

234
// channelMapKey is the key structure used for storing channel edge policies.
235
type channelMapKey struct {
236
        nodeKey route.Vertex
237
        chanID  [8]byte
238
}
239

240
// getChannelMap loads all channel edge policies from the database and stores
241
// them in a map.
242
func (c *KVStore) getChannelMap(edges kvdb.RBucket) (
243
        map[channelMapKey]*models.ChannelEdgePolicy, error) {
144✔
244

144✔
245
        // Create a map to store all channel edge policies.
144✔
246
        channelMap := make(map[channelMapKey]*models.ChannelEdgePolicy)
144✔
247

144✔
248
        err := kvdb.ForAll(edges, func(k, edgeBytes []byte) error {
1,706✔
249
                // Skip embedded buckets.
1,562✔
250
                if bytes.Equal(k, edgeIndexBucket) ||
1,562✔
251
                        bytes.Equal(k, edgeUpdateIndexBucket) ||
1,562✔
252
                        bytes.Equal(k, zombieBucket) ||
1,562✔
253
                        bytes.Equal(k, disabledEdgePolicyBucket) ||
1,562✔
254
                        bytes.Equal(k, channelPointBucket) {
2,134✔
255

572✔
256
                        return nil
572✔
257
                }
572✔
258

259
                // Validate key length.
260
                if len(k) != 33+8 {
993✔
261
                        return fmt.Errorf("invalid edge key %x encountered", k)
×
262
                }
×
263

264
                var key channelMapKey
993✔
265
                copy(key.nodeKey[:], k[:33])
993✔
266
                copy(key.chanID[:], k[33:])
993✔
267

993✔
268
                // No need to deserialize unknown policy.
993✔
269
                if bytes.Equal(edgeBytes, unknownPolicy) {
993✔
270
                        return nil
×
271
                }
×
272

273
                edgeReader := bytes.NewReader(edgeBytes)
993✔
274
                edge, err := deserializeChanEdgePolicyRaw(
993✔
275
                        edgeReader,
993✔
276
                )
993✔
277

993✔
278
                switch {
993✔
279
                // If the db policy was missing an expected optional field, we
280
                // return nil as if the policy was unknown.
281
                case errors.Is(err, ErrEdgePolicyOptionalFieldNotFound):
×
282
                        return nil
×
283

284
                case err != nil:
×
285
                        return err
×
286
                }
287

288
                channelMap[key] = edge
993✔
289

993✔
290
                return nil
993✔
291
        })
292
        if err != nil {
144✔
293
                return nil, err
×
294
        }
×
295

296
        return channelMap, nil
144✔
297
}
298

299
var graphTopLevelBuckets = [][]byte{
300
        nodeBucket,
301
        edgeBucket,
302
        graphMetaBucket,
303
        closedScidBucket,
304
}
305

306
// createChannelDB creates and initializes a fresh version of  In
307
// the case that the target path has not yet been created or doesn't yet exist,
308
// then the path is created. Additionally, all required top-level buckets used
309
// within the database are created.
310
func initKVStore(db kvdb.Backend) error {
172✔
311
        err := kvdb.Update(db, func(tx kvdb.RwTx) error {
344✔
312
                for _, tlb := range graphTopLevelBuckets {
851✔
313
                        if _, err := tx.CreateTopLevelBucket(tlb); err != nil {
679✔
314
                                return err
×
315
                        }
×
316
                }
317

318
                nodes := tx.ReadWriteBucket(nodeBucket)
172✔
319
                _, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
172✔
320
                if err != nil {
172✔
321
                        return err
×
322
                }
×
323
                _, err = nodes.CreateBucketIfNotExists(nodeUpdateIndexBucket)
172✔
324
                if err != nil {
172✔
325
                        return err
×
326
                }
×
327

328
                edges := tx.ReadWriteBucket(edgeBucket)
172✔
329
                _, err = edges.CreateBucketIfNotExists(edgeIndexBucket)
172✔
330
                if err != nil {
172✔
331
                        return err
×
332
                }
×
333
                _, err = edges.CreateBucketIfNotExists(edgeUpdateIndexBucket)
172✔
334
                if err != nil {
172✔
335
                        return err
×
336
                }
×
337
                _, err = edges.CreateBucketIfNotExists(channelPointBucket)
172✔
338
                if err != nil {
172✔
339
                        return err
×
340
                }
×
341
                _, err = edges.CreateBucketIfNotExists(zombieBucket)
172✔
342
                if err != nil {
172✔
343
                        return err
×
344
                }
×
345

346
                graphMeta := tx.ReadWriteBucket(graphMetaBucket)
172✔
347
                _, err = graphMeta.CreateBucketIfNotExists(pruneLogBucket)
172✔
348

172✔
349
                return err
172✔
350
        }, func() {})
172✔
351
        if err != nil {
172✔
352
                return fmt.Errorf("unable to create new channel graph: %w", err)
×
353
        }
×
354

355
        return nil
172✔
356
}
357

358
// AddrsForNode returns all known addresses for the target node public key that
359
// the graph DB is aware of. The returned boolean indicates if the given node is
360
// unknown to the graph DB or not.
361
//
362
// NOTE: this is part of the channeldb.AddrSource interface.
363
func (c *KVStore) AddrsForNode(nodePub *btcec.PublicKey) (bool, []net.Addr,
364
        error) {
4✔
365

4✔
366
        pubKey, err := route.NewVertexFromBytes(nodePub.SerializeCompressed())
4✔
367
        if err != nil {
4✔
368
                return false, nil, err
×
369
        }
×
370

371
        node, err := c.FetchLightningNode(pubKey)
4✔
372
        // We don't consider it an error if the graph is unaware of the node.
4✔
373
        switch {
4✔
374
        case err != nil && !errors.Is(err, ErrGraphNodeNotFound):
×
375
                return false, nil, err
×
376

377
        case errors.Is(err, ErrGraphNodeNotFound):
3✔
378
                return false, nil, nil
3✔
379
        }
380

381
        return true, node.Addresses, nil
4✔
382
}
383

384
// ForEachChannel iterates through all the channel edges stored within the
385
// graph and invokes the passed callback for each edge. The callback takes two
386
// edges as since this is a directed graph, both the in/out edges are visited.
387
// If the callback returns an error, then the transaction is aborted and the
388
// iteration stops early.
389
//
390
// NOTE: If an edge can't be found, or wasn't advertised, then a nil pointer
391
// for that particular channel edge routing policy will be passed into the
392
// callback.
393
func (c *KVStore) ForEachChannel(cb func(*models.ChannelEdgeInfo,
394
        *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
144✔
395

144✔
396
        return c.db.View(func(tx kvdb.RTx) error {
288✔
397
                edges := tx.ReadBucket(edgeBucket)
144✔
398
                if edges == nil {
144✔
399
                        return ErrGraphNoEdgesFound
×
400
                }
×
401

402
                // First, load all edges in memory indexed by node and channel
403
                // id.
404
                channelMap, err := c.getChannelMap(edges)
144✔
405
                if err != nil {
144✔
406
                        return err
×
407
                }
×
408

409
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
144✔
410
                if edgeIndex == nil {
144✔
411
                        return ErrGraphNoEdgesFound
×
412
                }
×
413

414
                // Load edge index, recombine each channel with the policies
415
                // loaded above and invoke the callback.
416
                return kvdb.ForAll(
144✔
417
                        edgeIndex, func(k, edgeInfoBytes []byte) error {
642✔
418
                                var chanID [8]byte
498✔
419
                                copy(chanID[:], k)
498✔
420

498✔
421
                                edgeInfoReader := bytes.NewReader(edgeInfoBytes)
498✔
422
                                info, err := deserializeChanEdgeInfo(
498✔
423
                                        edgeInfoReader,
498✔
424
                                )
498✔
425
                                if err != nil {
498✔
426
                                        return err
×
427
                                }
×
428

429
                                policy1 := channelMap[channelMapKey{
498✔
430
                                        nodeKey: info.NodeKey1Bytes,
498✔
431
                                        chanID:  chanID,
498✔
432
                                }]
498✔
433

498✔
434
                                policy2 := channelMap[channelMapKey{
498✔
435
                                        nodeKey: info.NodeKey2Bytes,
498✔
436
                                        chanID:  chanID,
498✔
437
                                }]
498✔
438

498✔
439
                                return cb(&info, policy1, policy2)
498✔
440
                        },
441
                )
442
        }, func() {})
144✔
443
}
444

445
// forEachNodeDirectedChannel iterates through all channels of a given node,
446
// executing the passed callback on the directed edge representing the channel
447
// and its incoming policy. If the callback returns an error, then the iteration
448
// is halted with the error propagated back up to the caller. An optional read
449
// transaction may be provided. If none is provided, a new one will be created.
450
//
451
// Unknown policies are passed into the callback as nil values.
452
func (c *KVStore) forEachNodeDirectedChannel(tx kvdb.RTx,
453
        node route.Vertex, cb func(channel *DirectedChannel) error) error {
245✔
454

245✔
455
        // Fallback that uses the database.
245✔
456
        toNodeCallback := func() route.Vertex {
380✔
457
                return node
135✔
458
        }
135✔
459
        toNodeFeatures, err := c.fetchNodeFeatures(tx, node)
245✔
460
        if err != nil {
245✔
461
                return err
×
462
        }
×
463

464
        dbCallback := func(tx kvdb.RTx, e *models.ChannelEdgeInfo, p1,
245✔
465
                p2 *models.ChannelEdgePolicy) error {
744✔
466

499✔
467
                var cachedInPolicy *models.CachedEdgePolicy
499✔
468
                if p2 != nil {
995✔
469
                        cachedInPolicy = models.NewCachedPolicy(p2)
496✔
470
                        cachedInPolicy.ToNodePubKey = toNodeCallback
496✔
471
                        cachedInPolicy.ToNodeFeatures = toNodeFeatures
496✔
472
                }
496✔
473

474
                var inboundFee lnwire.Fee
499✔
475
                if p1 != nil {
997✔
476
                        // Extract inbound fee. If there is a decoding error,
498✔
477
                        // skip this edge.
498✔
478
                        _, err := p1.ExtraOpaqueData.ExtractRecords(&inboundFee)
498✔
479
                        if err != nil {
499✔
480
                                return nil
1✔
481
                        }
1✔
482
                }
483

484
                directedChannel := &DirectedChannel{
498✔
485
                        ChannelID:    e.ChannelID,
498✔
486
                        IsNode1:      node == e.NodeKey1Bytes,
498✔
487
                        OtherNode:    e.NodeKey2Bytes,
498✔
488
                        Capacity:     e.Capacity,
498✔
489
                        OutPolicySet: p1 != nil,
498✔
490
                        InPolicy:     cachedInPolicy,
498✔
491
                        InboundFee:   inboundFee,
498✔
492
                }
498✔
493

498✔
494
                if node == e.NodeKey2Bytes {
751✔
495
                        directedChannel.OtherNode = e.NodeKey1Bytes
253✔
496
                }
253✔
497

498
                return cb(directedChannel)
498✔
499
        }
500

501
        return nodeTraversal(tx, node[:], c.db, dbCallback)
245✔
502
}
503

504
// fetchNodeFeatures returns the features of a given node. If no features are
505
// known for the node, an empty feature vector is returned. An optional read
506
// transaction may be provided. If none is provided, a new one will be created.
507
func (c *KVStore) fetchNodeFeatures(tx kvdb.RTx,
508
        node route.Vertex) (*lnwire.FeatureVector, error) {
690✔
509

690✔
510
        // Fallback that uses the database.
690✔
511
        targetNode, err := c.FetchLightningNodeTx(tx, node)
690✔
512
        switch {
690✔
513
        // If the node exists and has features, return them directly.
514
        case err == nil:
679✔
515
                return targetNode.Features, nil
679✔
516

517
        // If we couldn't find a node announcement, populate a blank feature
518
        // vector.
519
        case errors.Is(err, ErrGraphNodeNotFound):
11✔
520
                return lnwire.EmptyFeatureVector(), nil
11✔
521

522
        // Otherwise, bubble the error up.
523
        default:
×
524
                return nil, err
×
525
        }
526
}
527

528
// ForEachNodeDirectedChannel iterates through all channels of a given node,
529
// executing the passed callback on the directed edge representing the channel
530
// and its incoming policy. If the callback returns an error, then the iteration
531
// is halted with the error propagated back up to the caller.
532
//
533
// Unknown policies are passed into the callback as nil values.
534
//
535
// NOTE: this is part of the graphdb.NodeTraverser interface.
536
func (c *KVStore) ForEachNodeDirectedChannel(nodePub route.Vertex,
537
        cb func(channel *DirectedChannel) error) error {
6✔
538

6✔
539
        return c.forEachNodeDirectedChannel(nil, nodePub, cb)
6✔
540
}
6✔
541

542
// FetchNodeFeatures returns the features of the given node. If no features are
543
// known for the node, an empty feature vector is returned.
544
//
545
// NOTE: this is part of the graphdb.NodeTraverser interface.
546
func (c *KVStore) FetchNodeFeatures(nodePub route.Vertex) (
547
        *lnwire.FeatureVector, error) {
4✔
548

4✔
549
        return c.fetchNodeFeatures(nil, nodePub)
4✔
550
}
4✔
551

552
// ForEachNodeCached is similar to forEachNode, but it returns DirectedChannel
553
// data to the call-back.
554
//
555
// NOTE: The callback contents MUST not be modified.
556
func (c *KVStore) ForEachNodeCached(cb func(node route.Vertex,
557
        chans map[uint64]*DirectedChannel) error) error {
1✔
558

1✔
559
        // Otherwise call back to a version that uses the database directly.
1✔
560
        // We'll iterate over each node, then the set of channels for each
1✔
561
        // node, and construct a similar callback functiopn signature as the
1✔
562
        // main funcotin expects.
1✔
563
        return c.forEachNode(func(tx kvdb.RTx,
1✔
564
                node *models.LightningNode) error {
21✔
565

20✔
566
                channels := make(map[uint64]*DirectedChannel)
20✔
567

20✔
568
                err := c.forEachNodeChannelTx(tx, node.PubKeyBytes,
20✔
569
                        func(tx kvdb.RTx, e *models.ChannelEdgeInfo,
20✔
570
                                p1 *models.ChannelEdgePolicy,
20✔
571
                                p2 *models.ChannelEdgePolicy) error {
210✔
572

190✔
573
                                toNodeCallback := func() route.Vertex {
190✔
574
                                        return node.PubKeyBytes
×
575
                                }
×
576
                                toNodeFeatures, err := c.fetchNodeFeatures(
190✔
577
                                        tx, node.PubKeyBytes,
190✔
578
                                )
190✔
579
                                if err != nil {
190✔
580
                                        return err
×
581
                                }
×
582

583
                                var cachedInPolicy *models.CachedEdgePolicy
190✔
584
                                if p2 != nil {
380✔
585
                                        cachedInPolicy =
190✔
586
                                                models.NewCachedPolicy(p2)
190✔
587
                                        cachedInPolicy.ToNodePubKey =
190✔
588
                                                toNodeCallback
190✔
589
                                        cachedInPolicy.ToNodeFeatures =
190✔
590
                                                toNodeFeatures
190✔
591
                                }
190✔
592

593
                                directedChannel := &DirectedChannel{
190✔
594
                                        ChannelID: e.ChannelID,
190✔
595
                                        IsNode1: node.PubKeyBytes ==
190✔
596
                                                e.NodeKey1Bytes,
190✔
597
                                        OtherNode:    e.NodeKey2Bytes,
190✔
598
                                        Capacity:     e.Capacity,
190✔
599
                                        OutPolicySet: p1 != nil,
190✔
600
                                        InPolicy:     cachedInPolicy,
190✔
601
                                }
190✔
602

190✔
603
                                if node.PubKeyBytes == e.NodeKey2Bytes {
285✔
604
                                        directedChannel.OtherNode =
95✔
605
                                                e.NodeKey1Bytes
95✔
606
                                }
95✔
607

608
                                channels[e.ChannelID] = directedChannel
190✔
609

190✔
610
                                return nil
190✔
611
                        })
612
                if err != nil {
20✔
613
                        return err
×
614
                }
×
615

616
                return cb(node.PubKeyBytes, channels)
20✔
617
        })
618
}
619

620
// DisabledChannelIDs returns the channel ids of disabled channels.
621
// A channel is disabled when two of the associated ChanelEdgePolicies
622
// have their disabled bit on.
623
func (c *KVStore) DisabledChannelIDs() ([]uint64, error) {
6✔
624
        var disabledChanIDs []uint64
6✔
625
        var chanEdgeFound map[uint64]struct{}
6✔
626

6✔
627
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
12✔
628
                edges := tx.ReadBucket(edgeBucket)
6✔
629
                if edges == nil {
6✔
630
                        return ErrGraphNoEdgesFound
×
631
                }
×
632

633
                disabledEdgePolicyIndex := edges.NestedReadBucket(
6✔
634
                        disabledEdgePolicyBucket,
6✔
635
                )
6✔
636
                if disabledEdgePolicyIndex == nil {
7✔
637
                        return nil
1✔
638
                }
1✔
639

640
                // We iterate over all disabled policies and we add each channel
641
                // that has more than one disabled policy to disabledChanIDs
642
                // array.
643
                return disabledEdgePolicyIndex.ForEach(
5✔
644
                        func(k, v []byte) error {
16✔
645
                                chanID := byteOrder.Uint64(k[:8])
11✔
646
                                _, edgeFound := chanEdgeFound[chanID]
11✔
647
                                if edgeFound {
15✔
648
                                        delete(chanEdgeFound, chanID)
4✔
649
                                        disabledChanIDs = append(
4✔
650
                                                disabledChanIDs, chanID,
4✔
651
                                        )
4✔
652

4✔
653
                                        return nil
4✔
654
                                }
4✔
655

656
                                chanEdgeFound[chanID] = struct{}{}
7✔
657

7✔
658
                                return nil
7✔
659
                        },
660
                )
661
        }, func() {
6✔
662
                disabledChanIDs = nil
6✔
663
                chanEdgeFound = make(map[uint64]struct{})
6✔
664
        })
6✔
665
        if err != nil {
6✔
666
                return nil, err
×
667
        }
×
668

669
        return disabledChanIDs, nil
6✔
670
}
671

672
// ForEachNode iterates through all the stored vertices/nodes in the graph,
673
// executing the passed callback with each node encountered. If the callback
674
// returns an error, then the transaction is aborted and the iteration stops
675
// early. Any operations performed on the NodeTx passed to the call-back are
676
// executed under the same read transaction and so, methods on the NodeTx object
677
// _MUST_ only be called from within the call-back.
678
func (c *KVStore) ForEachNode(cb func(tx NodeRTx) error) error {
131✔
679
        return c.forEachNode(func(tx kvdb.RTx,
131✔
680
                node *models.LightningNode) error {
1,292✔
681

1,161✔
682
                return cb(newChanGraphNodeTx(tx, c, node))
1,161✔
683
        })
1,161✔
684
}
685

686
// forEachNode iterates through all the stored vertices/nodes in the graph,
687
// executing the passed callback with each node encountered. If the callback
688
// returns an error, then the transaction is aborted and the iteration stops
689
// early.
690
//
691
// TODO(roasbeef): add iterator interface to allow for memory efficient graph
692
// traversal when graph gets mega.
693
func (c *KVStore) forEachNode(
694
        cb func(kvdb.RTx, *models.LightningNode) error) error {
132✔
695

132✔
696
        traversal := func(tx kvdb.RTx) error {
264✔
697
                // First grab the nodes bucket which stores the mapping from
132✔
698
                // pubKey to node information.
132✔
699
                nodes := tx.ReadBucket(nodeBucket)
132✔
700
                if nodes == nil {
132✔
701
                        return ErrGraphNotFound
×
702
                }
×
703

704
                return nodes.ForEach(func(pubKey, nodeBytes []byte) error {
1,574✔
705
                        // If this is the source key, then we skip this
1,442✔
706
                        // iteration as the value for this key is a pubKey
1,442✔
707
                        // rather than raw node information.
1,442✔
708
                        if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
1,706✔
709
                                return nil
264✔
710
                        }
264✔
711

712
                        nodeReader := bytes.NewReader(nodeBytes)
1,181✔
713
                        node, err := deserializeLightningNode(nodeReader)
1,181✔
714
                        if err != nil {
1,181✔
715
                                return err
×
716
                        }
×
717

718
                        // Execute the callback, the transaction will abort if
719
                        // this returns an error.
720
                        return cb(tx, &node)
1,181✔
721
                })
722
        }
723

724
        return kvdb.View(c.db, traversal, func() {})
264✔
725
}
726

727
// ForEachNodeCacheable iterates through all the stored vertices/nodes in the
728
// graph, executing the passed callback with each node encountered. If the
729
// callback returns an error, then the transaction is aborted and the iteration
730
// stops early.
731
func (c *KVStore) ForEachNodeCacheable(cb func(route.Vertex,
732
        *lnwire.FeatureVector) error) error {
141✔
733

141✔
734
        traversal := func(tx kvdb.RTx) error {
282✔
735
                // First grab the nodes bucket which stores the mapping from
141✔
736
                // pubKey to node information.
141✔
737
                nodes := tx.ReadBucket(nodeBucket)
141✔
738
                if nodes == nil {
141✔
739
                        return ErrGraphNotFound
×
740
                }
×
741

742
                return nodes.ForEach(func(pubKey, nodeBytes []byte) error {
540✔
743
                        // If this is the source key, then we skip this
399✔
744
                        // iteration as the value for this key is a pubKey
399✔
745
                        // rather than raw node information.
399✔
746
                        if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
678✔
747
                                return nil
279✔
748
                        }
279✔
749

750
                        nodeReader := bytes.NewReader(nodeBytes)
123✔
751
                        node, features, err := deserializeLightningNodeCacheable( //nolint:ll
123✔
752
                                nodeReader,
123✔
753
                        )
123✔
754
                        if err != nil {
123✔
755
                                return err
×
756
                        }
×
757

758
                        // Execute the callback, the transaction will abort if
759
                        // this returns an error.
760
                        return cb(node, features)
123✔
761
                })
762
        }
763

764
        return kvdb.View(c.db, traversal, func() {})
282✔
765
}
766

767
// SourceNode returns the source node of the graph. The source node is treated
768
// as the center node within a star-graph. This method may be used to kick off
769
// a path finding algorithm in order to explore the reachability of another
770
// node based off the source node.
771
func (c *KVStore) SourceNode() (*models.LightningNode, error) {
234✔
772
        var source *models.LightningNode
234✔
773
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
468✔
774
                // First grab the nodes bucket which stores the mapping from
234✔
775
                // pubKey to node information.
234✔
776
                nodes := tx.ReadBucket(nodeBucket)
234✔
777
                if nodes == nil {
234✔
778
                        return ErrGraphNotFound
×
779
                }
×
780

781
                node, err := c.sourceNode(nodes)
234✔
782
                if err != nil {
235✔
783
                        return err
1✔
784
                }
1✔
785
                source = node
233✔
786

233✔
787
                return nil
233✔
788
        }, func() {
234✔
789
                source = nil
234✔
790
        })
234✔
791
        if err != nil {
235✔
792
                return nil, err
1✔
793
        }
1✔
794

795
        return source, nil
233✔
796
}
797

798
// sourceNode uses an existing database transaction and returns the source node
799
// of the graph. The source node is treated as the center node within a
800
// star-graph. This method may be used to kick off a path finding algorithm in
801
// order to explore the reachability of another node based off the source node.
802
func (c *KVStore) sourceNode(nodes kvdb.RBucket) (*models.LightningNode,
803
        error) {
498✔
804

498✔
805
        selfPub := nodes.Get(sourceKey)
498✔
806
        if selfPub == nil {
499✔
807
                return nil, ErrSourceNodeNotSet
1✔
808
        }
1✔
809

810
        // With the pubKey of the source node retrieved, we're able to
811
        // fetch the full node information.
812
        node, err := fetchLightningNode(nodes, selfPub)
497✔
813
        if err != nil {
497✔
814
                return nil, err
×
815
        }
×
816

817
        return &node, nil
497✔
818
}
819

820
// SetSourceNode sets the source node within the graph database. The source
821
// node is to be used as the center of a star-graph within path finding
822
// algorithms.
823
func (c *KVStore) SetSourceNode(node *models.LightningNode) error {
116✔
824
        nodePubBytes := node.PubKeyBytes[:]
116✔
825

116✔
826
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
232✔
827
                // First grab the nodes bucket which stores the mapping from
116✔
828
                // pubKey to node information.
116✔
829
                nodes, err := tx.CreateTopLevelBucket(nodeBucket)
116✔
830
                if err != nil {
116✔
831
                        return err
×
832
                }
×
833

834
                // Next we create the mapping from source to the targeted
835
                // public key.
836
                if err := nodes.Put(sourceKey, nodePubBytes); err != nil {
116✔
837
                        return err
×
838
                }
×
839

840
                // Finally, we commit the information of the lightning node
841
                // itself.
842
                return addLightningNode(tx, node)
116✔
843
        }, func() {})
116✔
844
}
845

846
// AddLightningNode adds a vertex/node to the graph database. If the node is not
847
// in the database from before, this will add a new, unconnected one to the
848
// graph. If it is present from before, this will update that node's
849
// information. Note that this method is expected to only be called to update an
850
// already present node from a node announcement, or to insert a node found in a
851
// channel update.
852
//
853
// TODO(roasbeef): also need sig of announcement.
854
func (c *KVStore) AddLightningNode(node *models.LightningNode,
855
        opts ...batch.SchedulerOption) error {
802✔
856

802✔
857
        r := &batch.Request{
802✔
858
                Opts: batch.NewSchedulerOptions(opts...),
802✔
859
                Update: func(tx kvdb.RwTx) error {
1,604✔
860
                        return addLightningNode(tx, node)
802✔
861
                },
802✔
862
        }
863

864
        return c.nodeScheduler.Execute(r)
802✔
865
}
866

867
func addLightningNode(tx kvdb.RwTx, node *models.LightningNode) error {
996✔
868
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
996✔
869
        if err != nil {
996✔
870
                return err
×
871
        }
×
872

873
        aliases, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
996✔
874
        if err != nil {
996✔
875
                return err
×
876
        }
×
877

878
        updateIndex, err := nodes.CreateBucketIfNotExists(
996✔
879
                nodeUpdateIndexBucket,
996✔
880
        )
996✔
881
        if err != nil {
996✔
882
                return err
×
883
        }
×
884

885
        return putLightningNode(nodes, aliases, updateIndex, node)
996✔
886
}
887

888
// LookupAlias attempts to return the alias as advertised by the target node.
889
// TODO(roasbeef): currently assumes that aliases are unique...
890
func (c *KVStore) LookupAlias(pub *btcec.PublicKey) (string, error) {
5✔
891
        var alias string
5✔
892

5✔
893
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
10✔
894
                nodes := tx.ReadBucket(nodeBucket)
5✔
895
                if nodes == nil {
5✔
896
                        return ErrGraphNodesNotFound
×
897
                }
×
898

899
                aliases := nodes.NestedReadBucket(aliasIndexBucket)
5✔
900
                if aliases == nil {
5✔
901
                        return ErrGraphNodesNotFound
×
902
                }
×
903

904
                nodePub := pub.SerializeCompressed()
5✔
905
                a := aliases.Get(nodePub)
5✔
906
                if a == nil {
6✔
907
                        return ErrNodeAliasNotFound
1✔
908
                }
1✔
909

910
                // TODO(roasbeef): should actually be using the utf-8
911
                // package...
912
                alias = string(a)
4✔
913

4✔
914
                return nil
4✔
915
        }, func() {
5✔
916
                alias = ""
5✔
917
        })
5✔
918
        if err != nil {
6✔
919
                return "", err
1✔
920
        }
1✔
921

922
        return alias, nil
4✔
923
}
924

925
// DeleteLightningNode starts a new database transaction to remove a vertex/node
926
// from the database according to the node's public key.
927
func (c *KVStore) DeleteLightningNode(nodePub route.Vertex) error {
3✔
928
        // TODO(roasbeef): ensure dangling edges are removed...
3✔
929
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
930
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
931
                if nodes == nil {
3✔
932
                        return ErrGraphNodeNotFound
×
933
                }
×
934

935
                return c.deleteLightningNode(nodes, nodePub[:])
3✔
936
        }, func() {})
3✔
937
}
938

939
// deleteLightningNode uses an existing database transaction to remove a
940
// vertex/node from the database according to the node's public key.
941
func (c *KVStore) deleteLightningNode(nodes kvdb.RwBucket,
942
        compressedPubKey []byte) error {
69✔
943

69✔
944
        aliases := nodes.NestedReadWriteBucket(aliasIndexBucket)
69✔
945
        if aliases == nil {
69✔
946
                return ErrGraphNodesNotFound
×
947
        }
×
948

949
        if err := aliases.Delete(compressedPubKey); err != nil {
69✔
950
                return err
×
951
        }
×
952

953
        // Before we delete the node, we'll fetch its current state so we can
954
        // determine when its last update was to clear out the node update
955
        // index.
956
        node, err := fetchLightningNode(nodes, compressedPubKey)
69✔
957
        if err != nil {
69✔
958
                return err
×
959
        }
×
960

961
        if err := nodes.Delete(compressedPubKey); err != nil {
69✔
962
                return err
×
963
        }
×
964

965
        // Finally, we'll delete the index entry for the node within the
966
        // nodeUpdateIndexBucket as this node is no longer active, so we don't
967
        // need to track its last update.
968
        nodeUpdateIndex := nodes.NestedReadWriteBucket(nodeUpdateIndexBucket)
69✔
969
        if nodeUpdateIndex == nil {
69✔
970
                return ErrGraphNodesNotFound
×
971
        }
×
972

973
        // In order to delete the entry, we'll need to reconstruct the key for
974
        // its last update.
975
        updateUnix := uint64(node.LastUpdate.Unix())
69✔
976
        var indexKey [8 + 33]byte
69✔
977
        byteOrder.PutUint64(indexKey[:8], updateUnix)
69✔
978
        copy(indexKey[8:], compressedPubKey)
69✔
979

69✔
980
        return nodeUpdateIndex.Delete(indexKey[:])
69✔
981
}
982

983
// AddChannelEdge adds a new (undirected, blank) edge to the graph database. An
984
// undirected edge from the two target nodes are created. The information stored
985
// denotes the static attributes of the channel, such as the channelID, the keys
986
// involved in creation of the channel, and the set of features that the channel
987
// supports. The chanPoint and chanID are used to uniquely identify the edge
988
// globally within the database.
989
func (c *KVStore) AddChannelEdge(edge *models.ChannelEdgeInfo,
990
        opts ...batch.SchedulerOption) error {
1,724✔
991

1,724✔
992
        var alreadyExists bool
1,724✔
993
        r := &batch.Request{
1,724✔
994
                Opts: batch.NewSchedulerOptions(opts...),
1,724✔
995
                Reset: func() {
3,448✔
996
                        alreadyExists = false
1,724✔
997
                },
1,724✔
998
                Update: func(tx kvdb.RwTx) error {
1,724✔
999
                        err := c.addChannelEdge(tx, edge)
1,724✔
1000

1,724✔
1001
                        // Silence ErrEdgeAlreadyExist so that the batch can
1,724✔
1002
                        // succeed, but propagate the error via local state.
1,724✔
1003
                        if errors.Is(err, ErrEdgeAlreadyExist) {
1,959✔
1004
                                alreadyExists = true
235✔
1005
                                return nil
235✔
1006
                        }
235✔
1007

1008
                        return err
1,489✔
1009
                },
1010
                OnCommit: func(err error) error {
1,724✔
1011
                        switch {
1,724✔
1012
                        case err != nil:
×
1013
                                return err
×
1014
                        case alreadyExists:
235✔
1015
                                return ErrEdgeAlreadyExist
235✔
1016
                        default:
1,489✔
1017
                                c.rejectCache.remove(edge.ChannelID)
1,489✔
1018
                                c.chanCache.remove(edge.ChannelID)
1,489✔
1019
                                return nil
1,489✔
1020
                        }
1021
                },
1022
        }
1023

1024
        return c.chanScheduler.Execute(r)
1,724✔
1025
}
1026

1027
// addChannelEdge is the private form of AddChannelEdge that allows callers to
1028
// utilize an existing db transaction.
1029
func (c *KVStore) addChannelEdge(tx kvdb.RwTx,
1030
        edge *models.ChannelEdgeInfo) error {
1,724✔
1031

1,724✔
1032
        // Construct the channel's primary key which is the 8-byte channel ID.
1,724✔
1033
        var chanKey [8]byte
1,724✔
1034
        binary.BigEndian.PutUint64(chanKey[:], edge.ChannelID)
1,724✔
1035

1,724✔
1036
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
1,724✔
1037
        if err != nil {
1,724✔
1038
                return err
×
1039
        }
×
1040
        edges, err := tx.CreateTopLevelBucket(edgeBucket)
1,724✔
1041
        if err != nil {
1,724✔
1042
                return err
×
1043
        }
×
1044
        edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
1,724✔
1045
        if err != nil {
1,724✔
1046
                return err
×
1047
        }
×
1048
        chanIndex, err := edges.CreateBucketIfNotExists(channelPointBucket)
1,724✔
1049
        if err != nil {
1,724✔
1050
                return err
×
1051
        }
×
1052

1053
        // First, attempt to check if this edge has already been created. If
1054
        // so, then we can exit early as this method is meant to be idempotent.
1055
        if edgeInfo := edgeIndex.Get(chanKey[:]); edgeInfo != nil {
1,959✔
1056
                return ErrEdgeAlreadyExist
235✔
1057
        }
235✔
1058

1059
        // Before we insert the channel into the database, we'll ensure that
1060
        // both nodes already exist in the channel graph. If either node
1061
        // doesn't, then we'll insert a "shell" node that just includes its
1062
        // public key, so subsequent validation and queries can work properly.
1063
        _, node1Err := fetchLightningNode(nodes, edge.NodeKey1Bytes[:])
1,489✔
1064
        switch {
1,489✔
1065
        case errors.Is(node1Err, ErrGraphNodeNotFound):
24✔
1066
                node1Shell := models.LightningNode{
24✔
1067
                        PubKeyBytes:          edge.NodeKey1Bytes,
24✔
1068
                        HaveNodeAnnouncement: false,
24✔
1069
                }
24✔
1070
                err := addLightningNode(tx, &node1Shell)
24✔
1071
                if err != nil {
24✔
1072
                        return fmt.Errorf("unable to create shell node "+
×
1073
                                "for: %x: %w", edge.NodeKey1Bytes, err)
×
1074
                }
×
1075
        case node1Err != nil:
×
1076
                return node1Err
×
1077
        }
1078

1079
        _, node2Err := fetchLightningNode(nodes, edge.NodeKey2Bytes[:])
1,489✔
1080
        switch {
1,489✔
1081
        case errors.Is(node2Err, ErrGraphNodeNotFound):
63✔
1082
                node2Shell := models.LightningNode{
63✔
1083
                        PubKeyBytes:          edge.NodeKey2Bytes,
63✔
1084
                        HaveNodeAnnouncement: false,
63✔
1085
                }
63✔
1086
                err := addLightningNode(tx, &node2Shell)
63✔
1087
                if err != nil {
63✔
1088
                        return fmt.Errorf("unable to create shell node "+
×
1089
                                "for: %x: %w", edge.NodeKey2Bytes, err)
×
1090
                }
×
1091
        case node2Err != nil:
×
1092
                return node2Err
×
1093
        }
1094

1095
        // If the edge hasn't been created yet, then we'll first add it to the
1096
        // edge index in order to associate the edge between two nodes and also
1097
        // store the static components of the channel.
1098
        if err := putChanEdgeInfo(edgeIndex, edge, chanKey); err != nil {
1,489✔
1099
                return err
×
1100
        }
×
1101

1102
        // Mark edge policies for both sides as unknown. This is to enable
1103
        // efficient incoming channel lookup for a node.
1104
        keys := []*[33]byte{
1,489✔
1105
                &edge.NodeKey1Bytes,
1,489✔
1106
                &edge.NodeKey2Bytes,
1,489✔
1107
        }
1,489✔
1108
        for _, key := range keys {
4,464✔
1109
                err := putChanEdgePolicyUnknown(edges, edge.ChannelID, key[:])
2,975✔
1110
                if err != nil {
2,975✔
1111
                        return err
×
1112
                }
×
1113
        }
1114

1115
        // Finally we add it to the channel index which maps channel points
1116
        // (outpoints) to the shorter channel ID's.
1117
        var b bytes.Buffer
1,489✔
1118
        if err := WriteOutpoint(&b, &edge.ChannelPoint); err != nil {
1,489✔
1119
                return err
×
1120
        }
×
1121

1122
        return chanIndex.Put(b.Bytes(), chanKey[:])
1,489✔
1123
}
1124

1125
// HasChannelEdge returns true if the database knows of a channel edge with the
1126
// passed channel ID, and false otherwise. If an edge with that ID is found
1127
// within the graph, then two time stamps representing the last time the edge
1128
// was updated for both directed edges are returned along with the boolean. If
1129
// it is not found, then the zombie index is checked and its result is returned
1130
// as the second boolean.
1131
func (c *KVStore) HasChannelEdge(
1132
        chanID uint64) (time.Time, time.Time, bool, bool, error) {
219✔
1133

219✔
1134
        var (
219✔
1135
                upd1Time time.Time
219✔
1136
                upd2Time time.Time
219✔
1137
                exists   bool
219✔
1138
                isZombie bool
219✔
1139
        )
219✔
1140

219✔
1141
        // We'll query the cache with the shared lock held to allow multiple
219✔
1142
        // readers to access values in the cache concurrently if they exist.
219✔
1143
        c.cacheMu.RLock()
219✔
1144
        if entry, ok := c.rejectCache.get(chanID); ok {
287✔
1145
                c.cacheMu.RUnlock()
68✔
1146
                upd1Time = time.Unix(entry.upd1Time, 0)
68✔
1147
                upd2Time = time.Unix(entry.upd2Time, 0)
68✔
1148
                exists, isZombie = entry.flags.unpack()
68✔
1149

68✔
1150
                return upd1Time, upd2Time, exists, isZombie, nil
68✔
1151
        }
68✔
1152
        c.cacheMu.RUnlock()
154✔
1153

154✔
1154
        c.cacheMu.Lock()
154✔
1155
        defer c.cacheMu.Unlock()
154✔
1156

154✔
1157
        // The item was not found with the shared lock, so we'll acquire the
154✔
1158
        // exclusive lock and check the cache again in case another method added
154✔
1159
        // the entry to the cache while no lock was held.
154✔
1160
        if entry, ok := c.rejectCache.get(chanID); ok {
162✔
1161
                upd1Time = time.Unix(entry.upd1Time, 0)
8✔
1162
                upd2Time = time.Unix(entry.upd2Time, 0)
8✔
1163
                exists, isZombie = entry.flags.unpack()
8✔
1164

8✔
1165
                return upd1Time, upd2Time, exists, isZombie, nil
8✔
1166
        }
8✔
1167

1168
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
296✔
1169
                edges := tx.ReadBucket(edgeBucket)
148✔
1170
                if edges == nil {
148✔
1171
                        return ErrGraphNoEdgesFound
×
1172
                }
×
1173
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
148✔
1174
                if edgeIndex == nil {
148✔
1175
                        return ErrGraphNoEdgesFound
×
1176
                }
×
1177

1178
                var channelID [8]byte
148✔
1179
                byteOrder.PutUint64(channelID[:], chanID)
148✔
1180

148✔
1181
                // If the edge doesn't exist, then we'll also check our zombie
148✔
1182
                // index.
148✔
1183
                if edgeIndex.Get(channelID[:]) == nil {
247✔
1184
                        exists = false
99✔
1185
                        zombieIndex := edges.NestedReadBucket(zombieBucket)
99✔
1186
                        if zombieIndex != nil {
198✔
1187
                                isZombie, _, _ = isZombieEdge(
99✔
1188
                                        zombieIndex, chanID,
99✔
1189
                                )
99✔
1190
                        }
99✔
1191

1192
                        return nil
99✔
1193
                }
1194

1195
                exists = true
52✔
1196
                isZombie = false
52✔
1197

52✔
1198
                // If the channel has been found in the graph, then retrieve
52✔
1199
                // the edges itself so we can return the last updated
52✔
1200
                // timestamps.
52✔
1201
                nodes := tx.ReadBucket(nodeBucket)
52✔
1202
                if nodes == nil {
52✔
1203
                        return ErrGraphNodeNotFound
×
1204
                }
×
1205

1206
                e1, e2, err := fetchChanEdgePolicies(
52✔
1207
                        edgeIndex, edges, channelID[:],
52✔
1208
                )
52✔
1209
                if err != nil {
52✔
1210
                        return err
×
1211
                }
×
1212

1213
                // As we may have only one of the edges populated, only set the
1214
                // update time if the edge was found in the database.
1215
                if e1 != nil {
73✔
1216
                        upd1Time = e1.LastUpdate
21✔
1217
                }
21✔
1218
                if e2 != nil {
71✔
1219
                        upd2Time = e2.LastUpdate
19✔
1220
                }
19✔
1221

1222
                return nil
52✔
1223
        }, func() {}); err != nil {
148✔
1224
                return time.Time{}, time.Time{}, exists, isZombie, err
×
1225
        }
×
1226

1227
        c.rejectCache.insert(chanID, rejectCacheEntry{
148✔
1228
                upd1Time: upd1Time.Unix(),
148✔
1229
                upd2Time: upd2Time.Unix(),
148✔
1230
                flags:    packRejectFlags(exists, isZombie),
148✔
1231
        })
148✔
1232

148✔
1233
        return upd1Time, upd2Time, exists, isZombie, nil
148✔
1234
}
1235

1236
// AddEdgeProof sets the proof of an existing edge in the graph database.
1237
func (c *KVStore) AddEdgeProof(chanID lnwire.ShortChannelID,
1238
        proof *models.ChannelAuthProof) error {
5✔
1239

5✔
1240
        // Construct the channel's primary key which is the 8-byte channel ID.
5✔
1241
        var chanKey [8]byte
5✔
1242
        binary.BigEndian.PutUint64(chanKey[:], chanID.ToUint64())
5✔
1243

5✔
1244
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
10✔
1245
                edges := tx.ReadWriteBucket(edgeBucket)
5✔
1246
                if edges == nil {
5✔
1247
                        return ErrEdgeNotFound
×
1248
                }
×
1249

1250
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
5✔
1251
                if edgeIndex == nil {
5✔
1252
                        return ErrEdgeNotFound
×
1253
                }
×
1254

1255
                edge, err := fetchChanEdgeInfo(edgeIndex, chanKey[:])
5✔
1256
                if err != nil {
5✔
1257
                        return err
×
1258
                }
×
1259

1260
                edge.AuthProof = proof
5✔
1261

5✔
1262
                return putChanEdgeInfo(edgeIndex, &edge, chanKey)
5✔
1263
        }, func() {})
5✔
1264
}
1265

1266
const (
1267
        // pruneTipBytes is the total size of the value which stores a prune
1268
        // entry of the graph in the prune log. The "prune tip" is the last
1269
        // entry in the prune log, and indicates if the channel graph is in
1270
        // sync with the current UTXO state. The structure of the value
1271
        // is: blockHash, taking 32 bytes total.
1272
        pruneTipBytes = 32
1273
)
1274

1275
// PruneGraph prunes newly closed channels from the channel graph in response
1276
// to a new block being solved on the network. Any transactions which spend the
1277
// funding output of any known channels within he graph will be deleted.
1278
// Additionally, the "prune tip", or the last block which has been used to
1279
// prune the graph is stored so callers can ensure the graph is fully in sync
1280
// with the current UTXO state. A slice of channels that have been closed by
1281
// the target block along with any pruned nodes are returned if the function
1282
// succeeds without error.
1283
func (c *KVStore) PruneGraph(spentOutputs []*wire.OutPoint,
1284
        blockHash *chainhash.Hash, blockHeight uint32) (
1285
        []*models.ChannelEdgeInfo, []route.Vertex, error) {
243✔
1286

243✔
1287
        c.cacheMu.Lock()
243✔
1288
        defer c.cacheMu.Unlock()
243✔
1289

243✔
1290
        var (
243✔
1291
                chansClosed []*models.ChannelEdgeInfo
243✔
1292
                prunedNodes []route.Vertex
243✔
1293
        )
243✔
1294

243✔
1295
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
486✔
1296
                // First grab the edges bucket which houses the information
243✔
1297
                // we'd like to delete
243✔
1298
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
243✔
1299
                if err != nil {
243✔
1300
                        return err
×
1301
                }
×
1302

1303
                // Next grab the two edge indexes which will also need to be
1304
                // updated.
1305
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
243✔
1306
                if err != nil {
243✔
1307
                        return err
×
1308
                }
×
1309
                chanIndex, err := edges.CreateBucketIfNotExists(
243✔
1310
                        channelPointBucket,
243✔
1311
                )
243✔
1312
                if err != nil {
243✔
1313
                        return err
×
1314
                }
×
1315
                nodes := tx.ReadWriteBucket(nodeBucket)
243✔
1316
                if nodes == nil {
243✔
1317
                        return ErrSourceNodeNotSet
×
1318
                }
×
1319
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
243✔
1320
                if err != nil {
243✔
1321
                        return err
×
1322
                }
×
1323

1324
                // For each of the outpoints that have been spent within the
1325
                // block, we attempt to delete them from the graph as if that
1326
                // outpoint was a channel, then it has now been closed.
1327
                for _, chanPoint := range spentOutputs {
385✔
1328
                        // TODO(roasbeef): load channel bloom filter, continue
142✔
1329
                        // if NOT if filter
142✔
1330

142✔
1331
                        var opBytes bytes.Buffer
142✔
1332
                        err := WriteOutpoint(&opBytes, chanPoint)
142✔
1333
                        if err != nil {
142✔
1334
                                return err
×
1335
                        }
×
1336

1337
                        // First attempt to see if the channel exists within
1338
                        // the database, if not, then we can exit early.
1339
                        chanID := chanIndex.Get(opBytes.Bytes())
142✔
1340
                        if chanID == nil {
256✔
1341
                                continue
114✔
1342
                        }
1343

1344
                        // Attempt to delete the channel, an ErrEdgeNotFound
1345
                        // will be returned if that outpoint isn't known to be
1346
                        // a channel. If no error is returned, then a channel
1347
                        // was successfully pruned.
1348
                        edgeInfo, err := c.delChannelEdgeUnsafe(
28✔
1349
                                edges, edgeIndex, chanIndex, zombieIndex,
28✔
1350
                                chanID, false, false,
28✔
1351
                        )
28✔
1352
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
28✔
1353
                                return err
×
1354
                        }
×
1355

1356
                        chansClosed = append(chansClosed, edgeInfo)
28✔
1357
                }
1358

1359
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
243✔
1360
                if err != nil {
243✔
1361
                        return err
×
1362
                }
×
1363

1364
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
243✔
1365
                        pruneLogBucket,
243✔
1366
                )
243✔
1367
                if err != nil {
243✔
1368
                        return err
×
1369
                }
×
1370

1371
                // With the graph pruned, add a new entry to the prune log,
1372
                // which can be used to check if the graph is fully synced with
1373
                // the current UTXO state.
1374
                var blockHeightBytes [4]byte
243✔
1375
                byteOrder.PutUint32(blockHeightBytes[:], blockHeight)
243✔
1376

243✔
1377
                var newTip [pruneTipBytes]byte
243✔
1378
                copy(newTip[:], blockHash[:])
243✔
1379

243✔
1380
                err = pruneBucket.Put(blockHeightBytes[:], newTip[:])
243✔
1381
                if err != nil {
243✔
1382
                        return err
×
1383
                }
×
1384

1385
                // Now that the graph has been pruned, we'll also attempt to
1386
                // prune any nodes that have had a channel closed within the
1387
                // latest block.
1388
                prunedNodes, err = c.pruneGraphNodes(nodes, edgeIndex)
243✔
1389

243✔
1390
                return err
243✔
1391
        }, func() {
243✔
1392
                chansClosed = nil
243✔
1393
                prunedNodes = nil
243✔
1394
        })
243✔
1395
        if err != nil {
243✔
1396
                return nil, nil, err
×
1397
        }
×
1398

1399
        for _, channel := range chansClosed {
271✔
1400
                c.rejectCache.remove(channel.ChannelID)
28✔
1401
                c.chanCache.remove(channel.ChannelID)
28✔
1402
        }
28✔
1403

1404
        return chansClosed, prunedNodes, nil
243✔
1405
}
1406

1407
// PruneGraphNodes is a garbage collection method which attempts to prune out
1408
// any nodes from the channel graph that are currently unconnected. This ensure
1409
// that we only maintain a graph of reachable nodes. In the event that a pruned
1410
// node gains more channels, it will be re-added back to the graph.
1411
func (c *KVStore) PruneGraphNodes() ([]route.Vertex, error) {
26✔
1412
        var prunedNodes []route.Vertex
26✔
1413
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
52✔
1414
                nodes := tx.ReadWriteBucket(nodeBucket)
26✔
1415
                if nodes == nil {
26✔
1416
                        return ErrGraphNodesNotFound
×
1417
                }
×
1418
                edges := tx.ReadWriteBucket(edgeBucket)
26✔
1419
                if edges == nil {
26✔
1420
                        return ErrGraphNotFound
×
1421
                }
×
1422
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
26✔
1423
                if edgeIndex == nil {
26✔
1424
                        return ErrGraphNoEdgesFound
×
1425
                }
×
1426

1427
                var err error
26✔
1428
                prunedNodes, err = c.pruneGraphNodes(nodes, edgeIndex)
26✔
1429
                if err != nil {
26✔
1430
                        return err
×
1431
                }
×
1432

1433
                return nil
26✔
1434
        }, func() {
26✔
1435
                prunedNodes = nil
26✔
1436
        })
26✔
1437

1438
        return prunedNodes, err
26✔
1439
}
1440

1441
// pruneGraphNodes attempts to remove any nodes from the graph who have had a
1442
// channel closed within the current block. If the node still has existing
1443
// channels in the graph, this will act as a no-op.
1444
func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket,
1445
        edgeIndex kvdb.RwBucket) ([]route.Vertex, error) {
266✔
1446

266✔
1447
        log.Trace("Pruning nodes from graph with no open channels")
266✔
1448

266✔
1449
        // We'll retrieve the graph's source node to ensure we don't remove it
266✔
1450
        // even if it no longer has any open channels.
266✔
1451
        sourceNode, err := c.sourceNode(nodes)
266✔
1452
        if err != nil {
266✔
1453
                return nil, err
×
1454
        }
×
1455

1456
        // We'll use this map to keep count the number of references to a node
1457
        // in the graph. A node should only be removed once it has no more
1458
        // references in the graph.
1459
        nodeRefCounts := make(map[[33]byte]int)
266✔
1460
        err = nodes.ForEach(func(pubKey, nodeBytes []byte) error {
1,561✔
1461
                // If this is the source key, then we skip this
1,295✔
1462
                // iteration as the value for this key is a pubKey
1,295✔
1463
                // rather than raw node information.
1,295✔
1464
                if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
2,087✔
1465
                        return nil
792✔
1466
                }
792✔
1467

1468
                var nodePub [33]byte
506✔
1469
                copy(nodePub[:], pubKey)
506✔
1470
                nodeRefCounts[nodePub] = 0
506✔
1471

506✔
1472
                return nil
506✔
1473
        })
1474
        if err != nil {
266✔
1475
                return nil, err
×
1476
        }
×
1477

1478
        // To ensure we never delete the source node, we'll start off by
1479
        // bumping its ref count to 1.
1480
        nodeRefCounts[sourceNode.PubKeyBytes] = 1
266✔
1481

266✔
1482
        // Next, we'll run through the edgeIndex which maps a channel ID to the
266✔
1483
        // edge info. We'll use this scan to populate our reference count map
266✔
1484
        // above.
266✔
1485
        err = edgeIndex.ForEach(func(chanID, edgeInfoBytes []byte) error {
461✔
1486
                // The first 66 bytes of the edge info contain the pubkeys of
195✔
1487
                // the nodes that this edge attaches. We'll extract them, and
195✔
1488
                // add them to the ref count map.
195✔
1489
                var node1, node2 [33]byte
195✔
1490
                copy(node1[:], edgeInfoBytes[:33])
195✔
1491
                copy(node2[:], edgeInfoBytes[33:])
195✔
1492

195✔
1493
                // With the nodes extracted, we'll increase the ref count of
195✔
1494
                // each of the nodes.
195✔
1495
                nodeRefCounts[node1]++
195✔
1496
                nodeRefCounts[node2]++
195✔
1497

195✔
1498
                return nil
195✔
1499
        })
195✔
1500
        if err != nil {
266✔
1501
                return nil, err
×
1502
        }
×
1503

1504
        // Finally, we'll make a second pass over the set of nodes, and delete
1505
        // any nodes that have a ref count of zero.
1506
        var pruned []route.Vertex
266✔
1507
        for nodePubKey, refCount := range nodeRefCounts {
772✔
1508
                // If the ref count of the node isn't zero, then we can safely
506✔
1509
                // skip it as it still has edges to or from it within the
506✔
1510
                // graph.
506✔
1511
                if refCount != 0 {
949✔
1512
                        continue
443✔
1513
                }
1514

1515
                // If we reach this point, then there are no longer any edges
1516
                // that connect this node, so we can delete it.
1517
                err := c.deleteLightningNode(nodes, nodePubKey[:])
66✔
1518
                if err != nil {
66✔
1519
                        if errors.Is(err, ErrGraphNodeNotFound) ||
×
1520
                                errors.Is(err, ErrGraphNodesNotFound) {
×
1521

×
1522
                                log.Warnf("Unable to prune node %x from the "+
×
1523
                                        "graph: %v", nodePubKey, err)
×
1524
                                continue
×
1525
                        }
1526

1527
                        return nil, err
×
1528
                }
1529

1530
                log.Infof("Pruned unconnected node %x from channel graph",
66✔
1531
                        nodePubKey[:])
66✔
1532

66✔
1533
                pruned = append(pruned, nodePubKey)
66✔
1534
        }
1535

1536
        if len(pruned) > 0 {
316✔
1537
                log.Infof("Pruned %v unconnected nodes from the channel graph",
50✔
1538
                        len(pruned))
50✔
1539
        }
50✔
1540

1541
        return pruned, err
266✔
1542
}
1543

1544
// DisconnectBlockAtHeight is used to indicate that the block specified
1545
// by the passed height has been disconnected from the main chain. This
1546
// will "rewind" the graph back to the height below, deleting channels
1547
// that are no longer confirmed from the graph. The prune log will be
1548
// set to the last prune height valid for the remaining chain.
1549
// Channels that were removed from the graph resulting from the
1550
// disconnected block are returned.
1551
func (c *KVStore) DisconnectBlockAtHeight(height uint32) (
1552
        []*models.ChannelEdgeInfo, error) {
154✔
1553

154✔
1554
        // Every channel having a ShortChannelID starting at 'height'
154✔
1555
        // will no longer be confirmed.
154✔
1556
        startShortChanID := lnwire.ShortChannelID{
154✔
1557
                BlockHeight: height,
154✔
1558
        }
154✔
1559

154✔
1560
        // Delete everything after this height from the db up until the
154✔
1561
        // SCID alias range.
154✔
1562
        endShortChanID := aliasmgr.StartingAlias
154✔
1563

154✔
1564
        // The block height will be the 3 first bytes of the channel IDs.
154✔
1565
        var chanIDStart [8]byte
154✔
1566
        byteOrder.PutUint64(chanIDStart[:], startShortChanID.ToUint64())
154✔
1567
        var chanIDEnd [8]byte
154✔
1568
        byteOrder.PutUint64(chanIDEnd[:], endShortChanID.ToUint64())
154✔
1569

154✔
1570
        c.cacheMu.Lock()
154✔
1571
        defer c.cacheMu.Unlock()
154✔
1572

154✔
1573
        // Keep track of the channels that are removed from the graph.
154✔
1574
        var removedChans []*models.ChannelEdgeInfo
154✔
1575

154✔
1576
        if err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
308✔
1577
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
154✔
1578
                if err != nil {
154✔
1579
                        return err
×
1580
                }
×
1581
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
154✔
1582
                if err != nil {
154✔
1583
                        return err
×
1584
                }
×
1585
                chanIndex, err := edges.CreateBucketIfNotExists(
154✔
1586
                        channelPointBucket,
154✔
1587
                )
154✔
1588
                if err != nil {
154✔
1589
                        return err
×
1590
                }
×
1591
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
154✔
1592
                if err != nil {
154✔
1593
                        return err
×
1594
                }
×
1595

1596
                // Scan from chanIDStart to chanIDEnd, deleting every
1597
                // found edge.
1598
                // NOTE: we must delete the edges after the cursor loop, since
1599
                // modifying the bucket while traversing is not safe.
1600
                // NOTE: We use a < comparison in bytes.Compare instead of <=
1601
                // so that the StartingAlias itself isn't deleted.
1602
                var keys [][]byte
154✔
1603
                cursor := edgeIndex.ReadWriteCursor()
154✔
1604

154✔
1605
                //nolint:ll
154✔
1606
                for k, _ := cursor.Seek(chanIDStart[:]); k != nil &&
154✔
1607
                        bytes.Compare(k, chanIDEnd[:]) < 0; k, _ = cursor.Next() {
246✔
1608
                        keys = append(keys, k)
92✔
1609
                }
92✔
1610

1611
                for _, k := range keys {
246✔
1612
                        edgeInfo, err := c.delChannelEdgeUnsafe(
92✔
1613
                                edges, edgeIndex, chanIndex, zombieIndex,
92✔
1614
                                k, false, false,
92✔
1615
                        )
92✔
1616
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
92✔
1617
                                return err
×
1618
                        }
×
1619

1620
                        removedChans = append(removedChans, edgeInfo)
92✔
1621
                }
1622

1623
                // Delete all the entries in the prune log having a height
1624
                // greater or equal to the block disconnected.
1625
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
154✔
1626
                if err != nil {
154✔
1627
                        return err
×
1628
                }
×
1629

1630
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
154✔
1631
                        pruneLogBucket,
154✔
1632
                )
154✔
1633
                if err != nil {
154✔
1634
                        return err
×
1635
                }
×
1636

1637
                var pruneKeyStart [4]byte
154✔
1638
                byteOrder.PutUint32(pruneKeyStart[:], height)
154✔
1639

154✔
1640
                var pruneKeyEnd [4]byte
154✔
1641
                byteOrder.PutUint32(pruneKeyEnd[:], math.MaxUint32)
154✔
1642

154✔
1643
                // To avoid modifying the bucket while traversing, we delete
154✔
1644
                // the keys in a second loop.
154✔
1645
                var pruneKeys [][]byte
154✔
1646
                pruneCursor := pruneBucket.ReadWriteCursor()
154✔
1647
                //nolint:ll
154✔
1648
                for k, _ := pruneCursor.Seek(pruneKeyStart[:]); k != nil &&
154✔
1649
                        bytes.Compare(k, pruneKeyEnd[:]) <= 0; k, _ = pruneCursor.Next() {
251✔
1650
                        pruneKeys = append(pruneKeys, k)
97✔
1651
                }
97✔
1652

1653
                for _, k := range pruneKeys {
251✔
1654
                        if err := pruneBucket.Delete(k); err != nil {
97✔
1655
                                return err
×
1656
                        }
×
1657
                }
1658

1659
                return nil
154✔
1660
        }, func() {
154✔
1661
                removedChans = nil
154✔
1662
        }); err != nil {
154✔
1663
                return nil, err
×
1664
        }
×
1665

1666
        for _, channel := range removedChans {
246✔
1667
                c.rejectCache.remove(channel.ChannelID)
92✔
1668
                c.chanCache.remove(channel.ChannelID)
92✔
1669
        }
92✔
1670

1671
        return removedChans, nil
154✔
1672
}
1673

1674
// PruneTip returns the block height and hash of the latest block that has been
1675
// used to prune channels in the graph. Knowing the "prune tip" allows callers
1676
// to tell if the graph is currently in sync with the current best known UTXO
1677
// state.
1678
func (c *KVStore) PruneTip() (*chainhash.Hash, uint32, error) {
56✔
1679
        var (
56✔
1680
                tipHash   chainhash.Hash
56✔
1681
                tipHeight uint32
56✔
1682
        )
56✔
1683

56✔
1684
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
112✔
1685
                graphMeta := tx.ReadBucket(graphMetaBucket)
56✔
1686
                if graphMeta == nil {
56✔
1687
                        return ErrGraphNotFound
×
1688
                }
×
1689
                pruneBucket := graphMeta.NestedReadBucket(pruneLogBucket)
56✔
1690
                if pruneBucket == nil {
56✔
1691
                        return ErrGraphNeverPruned
×
1692
                }
×
1693

1694
                pruneCursor := pruneBucket.ReadCursor()
56✔
1695

56✔
1696
                // The prune key with the largest block height will be our
56✔
1697
                // prune tip.
56✔
1698
                k, v := pruneCursor.Last()
56✔
1699
                if k == nil {
77✔
1700
                        return ErrGraphNeverPruned
21✔
1701
                }
21✔
1702

1703
                // Once we have the prune tip, the value will be the block hash,
1704
                // and the key the block height.
1705
                copy(tipHash[:], v)
38✔
1706
                tipHeight = byteOrder.Uint32(k)
38✔
1707

38✔
1708
                return nil
38✔
1709
        }, func() {})
56✔
1710
        if err != nil {
77✔
1711
                return nil, 0, err
21✔
1712
        }
21✔
1713

1714
        return &tipHash, tipHeight, nil
38✔
1715
}
1716

1717
// DeleteChannelEdges removes edges with the given channel IDs from the
1718
// database and marks them as zombies. This ensures that we're unable to re-add
1719
// it to our database once again. If an edge does not exist within the
1720
// database, then ErrEdgeNotFound will be returned. If strictZombiePruning is
1721
// true, then when we mark these edges as zombies, we'll set up the keys such
1722
// that we require the node that failed to send the fresh update to be the one
1723
// that resurrects the channel from its zombie state. The markZombie bool
1724
// denotes whether or not to mark the channel as a zombie.
1725
func (c *KVStore) DeleteChannelEdges(strictZombiePruning, markZombie bool,
1726
        chanIDs ...uint64) ([]*models.ChannelEdgeInfo, error) {
147✔
1727

147✔
1728
        // TODO(roasbeef): possibly delete from node bucket if node has no more
147✔
1729
        // channels
147✔
1730
        // TODO(roasbeef): don't delete both edges?
147✔
1731

147✔
1732
        c.cacheMu.Lock()
147✔
1733
        defer c.cacheMu.Unlock()
147✔
1734

147✔
1735
        var infos []*models.ChannelEdgeInfo
147✔
1736
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
294✔
1737
                edges := tx.ReadWriteBucket(edgeBucket)
147✔
1738
                if edges == nil {
147✔
1739
                        return ErrEdgeNotFound
×
1740
                }
×
1741
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
147✔
1742
                if edgeIndex == nil {
147✔
1743
                        return ErrEdgeNotFound
×
1744
                }
×
1745
                chanIndex := edges.NestedReadWriteBucket(channelPointBucket)
147✔
1746
                if chanIndex == nil {
147✔
1747
                        return ErrEdgeNotFound
×
1748
                }
×
1749
                nodes := tx.ReadWriteBucket(nodeBucket)
147✔
1750
                if nodes == nil {
147✔
1751
                        return ErrGraphNodeNotFound
×
1752
                }
×
1753
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
147✔
1754
                if err != nil {
147✔
1755
                        return err
×
1756
                }
×
1757

1758
                var rawChanID [8]byte
147✔
1759
                for _, chanID := range chanIDs {
235✔
1760
                        byteOrder.PutUint64(rawChanID[:], chanID)
88✔
1761
                        edgeInfo, err := c.delChannelEdgeUnsafe(
88✔
1762
                                edges, edgeIndex, chanIndex, zombieIndex,
88✔
1763
                                rawChanID[:], markZombie, strictZombiePruning,
88✔
1764
                        )
88✔
1765
                        if err != nil {
150✔
1766
                                return err
62✔
1767
                        }
62✔
1768

1769
                        infos = append(infos, edgeInfo)
26✔
1770
                }
1771

1772
                return nil
85✔
1773
        }, func() {
147✔
1774
                infos = nil
147✔
1775
        })
147✔
1776
        if err != nil {
209✔
1777
                return nil, err
62✔
1778
        }
62✔
1779

1780
        for _, chanID := range chanIDs {
111✔
1781
                c.rejectCache.remove(chanID)
26✔
1782
                c.chanCache.remove(chanID)
26✔
1783
        }
26✔
1784

1785
        return infos, nil
85✔
1786
}
1787

1788
// ChannelID attempt to lookup the 8-byte compact channel ID which maps to the
1789
// passed channel point (outpoint). If the passed channel doesn't exist within
1790
// the database, then ErrEdgeNotFound is returned.
1791
func (c *KVStore) ChannelID(chanPoint *wire.OutPoint) (uint64, error) {
4✔
1792
        var chanID uint64
4✔
1793
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
8✔
1794
                var err error
4✔
1795
                chanID, err = getChanID(tx, chanPoint)
4✔
1796
                return err
4✔
1797
        }, func() {
8✔
1798
                chanID = 0
4✔
1799
        }); err != nil {
7✔
1800
                return 0, err
3✔
1801
        }
3✔
1802

1803
        return chanID, nil
4✔
1804
}
1805

1806
// getChanID returns the assigned channel ID for a given channel point.
1807
func getChanID(tx kvdb.RTx, chanPoint *wire.OutPoint) (uint64, error) {
4✔
1808
        var b bytes.Buffer
4✔
1809
        if err := WriteOutpoint(&b, chanPoint); err != nil {
4✔
1810
                return 0, err
×
1811
        }
×
1812

1813
        edges := tx.ReadBucket(edgeBucket)
4✔
1814
        if edges == nil {
4✔
1815
                return 0, ErrGraphNoEdgesFound
×
1816
        }
×
1817
        chanIndex := edges.NestedReadBucket(channelPointBucket)
4✔
1818
        if chanIndex == nil {
4✔
1819
                return 0, ErrGraphNoEdgesFound
×
1820
        }
×
1821

1822
        chanIDBytes := chanIndex.Get(b.Bytes())
4✔
1823
        if chanIDBytes == nil {
7✔
1824
                return 0, ErrEdgeNotFound
3✔
1825
        }
3✔
1826

1827
        chanID := byteOrder.Uint64(chanIDBytes)
4✔
1828

4✔
1829
        return chanID, nil
4✔
1830
}
1831

1832
// TODO(roasbeef): allow updates to use Batch?
1833

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

6✔
1840
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
12✔
1841
                edges := tx.ReadBucket(edgeBucket)
6✔
1842
                if edges == nil {
6✔
1843
                        return ErrGraphNoEdgesFound
×
1844
                }
×
1845
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
6✔
1846
                if edgeIndex == nil {
6✔
1847
                        return ErrGraphNoEdgesFound
×
1848
                }
×
1849

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

6✔
1854
                lastChanID, _ := cidCursor.Last()
6✔
1855

6✔
1856
                // If there's no key, then this means that we don't actually
6✔
1857
                // know of any channels, so we'll return a predicable error.
6✔
1858
                if lastChanID == nil {
10✔
1859
                        return ErrGraphNoEdgesFound
4✔
1860
                }
4✔
1861

1862
                // Otherwise, we'll de serialize the channel ID and return it
1863
                // to the caller.
1864
                cid = byteOrder.Uint64(lastChanID)
5✔
1865

5✔
1866
                return nil
5✔
1867
        }, func() {
6✔
1868
                cid = 0
6✔
1869
        })
6✔
1870
        if err != nil && !errors.Is(err, ErrGraphNoEdgesFound) {
6✔
1871
                return 0, err
×
1872
        }
×
1873

1874
        return cid, nil
6✔
1875
}
1876

1877
// ChannelEdge represents the complete set of information for a channel edge in
1878
// the known channel graph. This struct couples the core information of the
1879
// edge as well as each of the known advertised edge policies.
1880
type ChannelEdge struct {
1881
        // Info contains all the static information describing the channel.
1882
        Info *models.ChannelEdgeInfo
1883

1884
        // Policy1 points to the "first" edge policy of the channel containing
1885
        // the dynamic information required to properly route through the edge.
1886
        Policy1 *models.ChannelEdgePolicy
1887

1888
        // Policy2 points to the "second" edge policy of the channel containing
1889
        // the dynamic information required to properly route through the edge.
1890
        Policy2 *models.ChannelEdgePolicy
1891

1892
        // Node1 is "node 1" in the channel. This is the node that would have
1893
        // produced Policy1 if it exists.
1894
        Node1 *models.LightningNode
1895

1896
        // Node2 is "node 2" in the channel. This is the node that would have
1897
        // produced Policy2 if it exists.
1898
        Node2 *models.LightningNode
1899
}
1900

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

142✔
1906
        // To ensure we don't return duplicate ChannelEdges, we'll use an
142✔
1907
        // additional map to keep track of the edges already seen to prevent
142✔
1908
        // re-adding it.
142✔
1909
        var edgesSeen map[uint64]struct{}
142✔
1910
        var edgesToCache map[uint64]ChannelEdge
142✔
1911
        var edgesInHorizon []ChannelEdge
142✔
1912

142✔
1913
        c.cacheMu.Lock()
142✔
1914
        defer c.cacheMu.Unlock()
142✔
1915

142✔
1916
        var hits int
142✔
1917
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
284✔
1918
                edges := tx.ReadBucket(edgeBucket)
142✔
1919
                if edges == nil {
142✔
1920
                        return ErrGraphNoEdgesFound
×
1921
                }
×
1922
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
142✔
1923
                if edgeIndex == nil {
142✔
1924
                        return ErrGraphNoEdgesFound
×
1925
                }
×
1926
                edgeUpdateIndex := edges.NestedReadBucket(edgeUpdateIndexBucket)
142✔
1927
                if edgeUpdateIndex == nil {
142✔
1928
                        return ErrGraphNoEdgesFound
×
1929
                }
×
1930

1931
                nodes := tx.ReadBucket(nodeBucket)
142✔
1932
                if nodes == nil {
142✔
1933
                        return ErrGraphNodesNotFound
×
1934
                }
×
1935

1936
                // We'll now obtain a cursor to perform a range query within
1937
                // the index to find all channels within the horizon.
1938
                updateCursor := edgeUpdateIndex.ReadCursor()
142✔
1939

142✔
1940
                var startTimeBytes, endTimeBytes [8 + 8]byte
142✔
1941
                byteOrder.PutUint64(
142✔
1942
                        startTimeBytes[:8], uint64(startTime.Unix()),
142✔
1943
                )
142✔
1944
                byteOrder.PutUint64(
142✔
1945
                        endTimeBytes[:8], uint64(endTime.Unix()),
142✔
1946
                )
142✔
1947

142✔
1948
                // With our start and end times constructed, we'll step through
142✔
1949
                // the index collecting the info and policy of each update of
142✔
1950
                // each channel that has a last update within the time range.
142✔
1951
                //
142✔
1952
                //nolint:ll
142✔
1953
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
142✔
1954
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
191✔
1955
                        // We have a new eligible entry, so we'll slice of the
49✔
1956
                        // chan ID so we can query it in the DB.
49✔
1957
                        chanID := indexKey[8:]
49✔
1958

49✔
1959
                        // If we've already retrieved the info and policies for
49✔
1960
                        // this edge, then we can skip it as we don't need to do
49✔
1961
                        // so again.
49✔
1962
                        chanIDInt := byteOrder.Uint64(chanID)
49✔
1963
                        if _, ok := edgesSeen[chanIDInt]; ok {
68✔
1964
                                continue
19✔
1965
                        }
1966

1967
                        if channel, ok := c.chanCache.get(chanIDInt); ok {
42✔
1968
                                hits++
12✔
1969
                                edgesSeen[chanIDInt] = struct{}{}
12✔
1970
                                edgesInHorizon = append(edgesInHorizon, channel)
12✔
1971

12✔
1972
                                continue
12✔
1973
                        }
1974

1975
                        // First, we'll fetch the static edge information.
1976
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
21✔
1977
                        if err != nil {
21✔
1978
                                chanID := byteOrder.Uint64(chanID)
×
1979
                                return fmt.Errorf("unable to fetch info for "+
×
1980
                                        "edge with chan_id=%v: %v", chanID, err)
×
1981
                        }
×
1982

1983
                        // With the static information obtained, we'll now
1984
                        // fetch the dynamic policy info.
1985
                        edge1, edge2, err := fetchChanEdgePolicies(
21✔
1986
                                edgeIndex, edges, chanID,
21✔
1987
                        )
21✔
1988
                        if err != nil {
21✔
1989
                                chanID := byteOrder.Uint64(chanID)
×
1990
                                return fmt.Errorf("unable to fetch policies "+
×
1991
                                        "for edge with chan_id=%v: %v", chanID,
×
1992
                                        err)
×
1993
                        }
×
1994

1995
                        node1, err := fetchLightningNode(
21✔
1996
                                nodes, edgeInfo.NodeKey1Bytes[:],
21✔
1997
                        )
21✔
1998
                        if err != nil {
21✔
1999
                                return err
×
2000
                        }
×
2001

2002
                        node2, err := fetchLightningNode(
21✔
2003
                                nodes, edgeInfo.NodeKey2Bytes[:],
21✔
2004
                        )
21✔
2005
                        if err != nil {
21✔
2006
                                return err
×
2007
                        }
×
2008

2009
                        // Finally, we'll collate this edge with the rest of
2010
                        // edges to be returned.
2011
                        edgesSeen[chanIDInt] = struct{}{}
21✔
2012
                        channel := ChannelEdge{
21✔
2013
                                Info:    &edgeInfo,
21✔
2014
                                Policy1: edge1,
21✔
2015
                                Policy2: edge2,
21✔
2016
                                Node1:   &node1,
21✔
2017
                                Node2:   &node2,
21✔
2018
                        }
21✔
2019
                        edgesInHorizon = append(edgesInHorizon, channel)
21✔
2020
                        edgesToCache[chanIDInt] = channel
21✔
2021
                }
2022

2023
                return nil
142✔
2024
        }, func() {
142✔
2025
                edgesSeen = make(map[uint64]struct{})
142✔
2026
                edgesToCache = make(map[uint64]ChannelEdge)
142✔
2027
                edgesInHorizon = nil
142✔
2028
        })
142✔
2029
        switch {
142✔
2030
        case errors.Is(err, ErrGraphNoEdgesFound):
×
2031
                fallthrough
×
2032
        case errors.Is(err, ErrGraphNodesNotFound):
×
2033
                break
×
2034

2035
        case err != nil:
×
2036
                return nil, err
×
2037
        }
2038

2039
        // Insert any edges loaded from disk into the cache.
2040
        for chanid, channel := range edgesToCache {
163✔
2041
                c.chanCache.insert(chanid, channel)
21✔
2042
        }
21✔
2043

2044
        log.Debugf("ChanUpdatesInHorizon hit percentage: %f (%d/%d)",
142✔
2045
                float64(hits)/float64(len(edgesInHorizon)), hits,
142✔
2046
                len(edgesInHorizon))
142✔
2047

142✔
2048
        return edgesInHorizon, nil
142✔
2049
}
2050

2051
// NodeUpdatesInHorizon returns all the known lightning node which have an
2052
// update timestamp within the passed range. This method can be used by two
2053
// nodes to quickly determine if they have the same set of up to date node
2054
// announcements.
2055
func (c *KVStore) NodeUpdatesInHorizon(startTime,
2056
        endTime time.Time) ([]models.LightningNode, error) {
11✔
2057

11✔
2058
        var nodesInHorizon []models.LightningNode
11✔
2059

11✔
2060
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
22✔
2061
                nodes := tx.ReadBucket(nodeBucket)
11✔
2062
                if nodes == nil {
11✔
2063
                        return ErrGraphNodesNotFound
×
2064
                }
×
2065

2066
                nodeUpdateIndex := nodes.NestedReadBucket(nodeUpdateIndexBucket)
11✔
2067
                if nodeUpdateIndex == nil {
11✔
2068
                        return ErrGraphNodesNotFound
×
2069
                }
×
2070

2071
                // We'll now obtain a cursor to perform a range query within
2072
                // the index to find all node announcements within the horizon.
2073
                updateCursor := nodeUpdateIndex.ReadCursor()
11✔
2074

11✔
2075
                var startTimeBytes, endTimeBytes [8 + 33]byte
11✔
2076
                byteOrder.PutUint64(
11✔
2077
                        startTimeBytes[:8], uint64(startTime.Unix()),
11✔
2078
                )
11✔
2079
                byteOrder.PutUint64(
11✔
2080
                        endTimeBytes[:8], uint64(endTime.Unix()),
11✔
2081
                )
11✔
2082

11✔
2083
                // With our start and end times constructed, we'll step through
11✔
2084
                // the index collecting info for each node within the time
11✔
2085
                // range.
11✔
2086
                //
11✔
2087
                //nolint:ll
11✔
2088
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
11✔
2089
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
43✔
2090
                        nodePub := indexKey[8:]
32✔
2091
                        node, err := fetchLightningNode(nodes, nodePub)
32✔
2092
                        if err != nil {
32✔
2093
                                return err
×
2094
                        }
×
2095

2096
                        nodesInHorizon = append(nodesInHorizon, node)
32✔
2097
                }
2098

2099
                return nil
11✔
2100
        }, func() {
11✔
2101
                nodesInHorizon = nil
11✔
2102
        })
11✔
2103
        switch {
11✔
2104
        case errors.Is(err, ErrGraphNoEdgesFound):
×
2105
                fallthrough
×
2106
        case errors.Is(err, ErrGraphNodesNotFound):
×
2107
                break
×
2108

2109
        case err != nil:
×
2110
                return nil, err
×
2111
        }
2112

2113
        return nodesInHorizon, nil
11✔
2114
}
2115

2116
// FilterKnownChanIDs takes a set of channel IDs and return the subset of chan
2117
// ID's that we don't know and are not known zombies of the passed set. In other
2118
// words, we perform a set difference of our set of chan ID's and the ones
2119
// passed in. This method can be used by callers to determine the set of
2120
// channels another peer knows of that we don't. The ChannelUpdateInfos for the
2121
// known zombies is also returned.
2122
func (c *KVStore) FilterKnownChanIDs(chansInfo []ChannelUpdateInfo) ([]uint64,
2123
        []ChannelUpdateInfo, error) {
127✔
2124

127✔
2125
        var (
127✔
2126
                newChanIDs   []uint64
127✔
2127
                knownZombies []ChannelUpdateInfo
127✔
2128
        )
127✔
2129

127✔
2130
        c.cacheMu.Lock()
127✔
2131
        defer c.cacheMu.Unlock()
127✔
2132

127✔
2133
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
254✔
2134
                edges := tx.ReadBucket(edgeBucket)
127✔
2135
                if edges == nil {
127✔
2136
                        return ErrGraphNoEdgesFound
×
2137
                }
×
2138
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
127✔
2139
                if edgeIndex == nil {
127✔
2140
                        return ErrGraphNoEdgesFound
×
2141
                }
×
2142

2143
                // Fetch the zombie index, it may not exist if no edges have
2144
                // ever been marked as zombies. If the index has been
2145
                // initialized, we will use it later to skip known zombie edges.
2146
                zombieIndex := edges.NestedReadBucket(zombieBucket)
127✔
2147

127✔
2148
                // We'll run through the set of chanIDs and collate only the
127✔
2149
                // set of channel that are unable to be found within our db.
127✔
2150
                var cidBytes [8]byte
127✔
2151
                for _, info := range chansInfo {
238✔
2152
                        scid := info.ShortChannelID.ToUint64()
111✔
2153
                        byteOrder.PutUint64(cidBytes[:], scid)
111✔
2154

111✔
2155
                        // If the edge is already known, skip it.
111✔
2156
                        if v := edgeIndex.Get(cidBytes[:]); v != nil {
134✔
2157
                                continue
23✔
2158
                        }
2159

2160
                        // If the edge is a known zombie, skip it.
2161
                        if zombieIndex != nil {
182✔
2162
                                isZombie, _, _ := isZombieEdge(
91✔
2163
                                        zombieIndex, scid,
91✔
2164
                                )
91✔
2165

91✔
2166
                                if isZombie {
134✔
2167
                                        knownZombies = append(
43✔
2168
                                                knownZombies, info,
43✔
2169
                                        )
43✔
2170

43✔
2171
                                        continue
43✔
2172
                                }
2173
                        }
2174

2175
                        newChanIDs = append(newChanIDs, scid)
48✔
2176
                }
2177

2178
                return nil
127✔
2179
        }, func() {
127✔
2180
                newChanIDs = nil
127✔
2181
                knownZombies = nil
127✔
2182
        })
127✔
2183
        switch {
127✔
2184
        // If we don't know of any edges yet, then we'll return the entire set
2185
        // of chan IDs specified.
2186
        case errors.Is(err, ErrGraphNoEdgesFound):
×
2187
                ogChanIDs := make([]uint64, len(chansInfo))
×
2188
                for i, info := range chansInfo {
×
2189
                        ogChanIDs[i] = info.ShortChannelID.ToUint64()
×
2190
                }
×
2191

2192
                return ogChanIDs, nil, nil
×
2193

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

2198
        return newChanIDs, knownZombies, nil
127✔
2199
}
2200

2201
// ChannelUpdateInfo couples the SCID of a channel with the timestamps of the
2202
// latest received channel updates for the channel.
2203
type ChannelUpdateInfo struct {
2204
        // ShortChannelID is the SCID identifier of the channel.
2205
        ShortChannelID lnwire.ShortChannelID
2206

2207
        // Node1UpdateTimestamp is the timestamp of the latest received update
2208
        // from the node 1 channel peer. This will be set to zero time if no
2209
        // update has yet been received from this node.
2210
        Node1UpdateTimestamp time.Time
2211

2212
        // Node2UpdateTimestamp is the timestamp of the latest received update
2213
        // from the node 2 channel peer. This will be set to zero time if no
2214
        // update has yet been received from this node.
2215
        Node2UpdateTimestamp time.Time
2216
}
2217

2218
// NewChannelUpdateInfo is a constructor which makes sure we initialize the
2219
// timestamps with zero seconds unix timestamp which equals
2220
// `January 1, 1970, 00:00:00 UTC` in case the value is `time.Time{}`.
2221
func NewChannelUpdateInfo(scid lnwire.ShortChannelID, node1Timestamp,
2222
        node2Timestamp time.Time) ChannelUpdateInfo {
199✔
2223

199✔
2224
        chanInfo := ChannelUpdateInfo{
199✔
2225
                ShortChannelID:       scid,
199✔
2226
                Node1UpdateTimestamp: node1Timestamp,
199✔
2227
                Node2UpdateTimestamp: node2Timestamp,
199✔
2228
        }
199✔
2229

199✔
2230
        if node1Timestamp.IsZero() {
388✔
2231
                chanInfo.Node1UpdateTimestamp = time.Unix(0, 0)
189✔
2232
        }
189✔
2233

2234
        if node2Timestamp.IsZero() {
388✔
2235
                chanInfo.Node2UpdateTimestamp = time.Unix(0, 0)
189✔
2236
        }
189✔
2237

2238
        return chanInfo
199✔
2239
}
2240

2241
// BlockChannelRange represents a range of channels for a given block height.
2242
type BlockChannelRange struct {
2243
        // Height is the height of the block all of the channels below were
2244
        // included in.
2245
        Height uint32
2246

2247
        // Channels is the list of channels identified by their short ID
2248
        // representation known to us that were included in the block height
2249
        // above. The list may include channel update timestamp information if
2250
        // requested.
2251
        Channels []ChannelUpdateInfo
2252
}
2253

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

14✔
2264
        startChanID := &lnwire.ShortChannelID{
14✔
2265
                BlockHeight: startHeight,
14✔
2266
        }
14✔
2267

14✔
2268
        endChanID := lnwire.ShortChannelID{
14✔
2269
                BlockHeight: endHeight,
14✔
2270
                TxIndex:     math.MaxUint32 & 0x00ffffff,
14✔
2271
                TxPosition:  math.MaxUint16,
14✔
2272
        }
14✔
2273

14✔
2274
        // As we need to perform a range scan, we'll convert the starting and
14✔
2275
        // ending height to their corresponding values when encoded using short
14✔
2276
        // channel ID's.
14✔
2277
        var chanIDStart, chanIDEnd [8]byte
14✔
2278
        byteOrder.PutUint64(chanIDStart[:], startChanID.ToUint64())
14✔
2279
        byteOrder.PutUint64(chanIDEnd[:], endChanID.ToUint64())
14✔
2280

14✔
2281
        var channelsPerBlock map[uint32][]ChannelUpdateInfo
14✔
2282
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
28✔
2283
                edges := tx.ReadBucket(edgeBucket)
14✔
2284
                if edges == nil {
14✔
2285
                        return ErrGraphNoEdgesFound
×
2286
                }
×
2287
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
14✔
2288
                if edgeIndex == nil {
14✔
2289
                        return ErrGraphNoEdgesFound
×
2290
                }
×
2291

2292
                cursor := edgeIndex.ReadCursor()
14✔
2293

14✔
2294
                // We'll now iterate through the database, and find each
14✔
2295
                // channel ID that resides within the specified range.
14✔
2296
                //
14✔
2297
                //nolint:ll
14✔
2298
                for k, v := cursor.Seek(chanIDStart[:]); k != nil &&
14✔
2299
                        bytes.Compare(k, chanIDEnd[:]) <= 0; k, v = cursor.Next() {
61✔
2300
                        // Don't send alias SCIDs during gossip sync.
47✔
2301
                        edgeReader := bytes.NewReader(v)
47✔
2302
                        edgeInfo, err := deserializeChanEdgeInfo(edgeReader)
47✔
2303
                        if err != nil {
47✔
2304
                                return err
×
2305
                        }
×
2306

2307
                        if edgeInfo.AuthProof == nil {
50✔
2308
                                continue
3✔
2309
                        }
2310

2311
                        // This channel ID rests within the target range, so
2312
                        // we'll add it to our returned set.
2313
                        rawCid := byteOrder.Uint64(k)
47✔
2314
                        cid := lnwire.NewShortChanIDFromInt(rawCid)
47✔
2315

47✔
2316
                        chanInfo := NewChannelUpdateInfo(
47✔
2317
                                cid, time.Time{}, time.Time{},
47✔
2318
                        )
47✔
2319

47✔
2320
                        if !withTimestamps {
69✔
2321
                                channelsPerBlock[cid.BlockHeight] = append(
22✔
2322
                                        channelsPerBlock[cid.BlockHeight],
22✔
2323
                                        chanInfo,
22✔
2324
                                )
22✔
2325

22✔
2326
                                continue
22✔
2327
                        }
2328

2329
                        node1Key, node2Key := computeEdgePolicyKeys(&edgeInfo)
25✔
2330

25✔
2331
                        rawPolicy := edges.Get(node1Key)
25✔
2332
                        if len(rawPolicy) != 0 {
34✔
2333
                                r := bytes.NewReader(rawPolicy)
9✔
2334

9✔
2335
                                edge, err := deserializeChanEdgePolicyRaw(r)
9✔
2336
                                if err != nil && !errors.Is(
9✔
2337
                                        err, ErrEdgePolicyOptionalFieldNotFound,
9✔
2338
                                ) {
9✔
2339

×
2340
                                        return err
×
2341
                                }
×
2342

2343
                                chanInfo.Node1UpdateTimestamp = edge.LastUpdate
9✔
2344
                        }
2345

2346
                        rawPolicy = edges.Get(node2Key)
25✔
2347
                        if len(rawPolicy) != 0 {
39✔
2348
                                r := bytes.NewReader(rawPolicy)
14✔
2349

14✔
2350
                                edge, err := deserializeChanEdgePolicyRaw(r)
14✔
2351
                                if err != nil && !errors.Is(
14✔
2352
                                        err, ErrEdgePolicyOptionalFieldNotFound,
14✔
2353
                                ) {
14✔
2354

×
2355
                                        return err
×
2356
                                }
×
2357

2358
                                chanInfo.Node2UpdateTimestamp = edge.LastUpdate
14✔
2359
                        }
2360

2361
                        channelsPerBlock[cid.BlockHeight] = append(
25✔
2362
                                channelsPerBlock[cid.BlockHeight], chanInfo,
25✔
2363
                        )
25✔
2364
                }
2365

2366
                return nil
14✔
2367
        }, func() {
14✔
2368
                channelsPerBlock = make(map[uint32][]ChannelUpdateInfo)
14✔
2369
        })
14✔
2370

2371
        switch {
14✔
2372
        // If we don't know of any channels yet, then there's nothing to
2373
        // filter, so we'll return an empty slice.
2374
        case errors.Is(err, ErrGraphNoEdgesFound) || len(channelsPerBlock) == 0:
6✔
2375
                return nil, nil
6✔
2376

2377
        case err != nil:
×
2378
                return nil, err
×
2379
        }
2380

2381
        // Return the channel ranges in ascending block height order.
2382
        blocks := make([]uint32, 0, len(channelsPerBlock))
11✔
2383
        for block := range channelsPerBlock {
36✔
2384
                blocks = append(blocks, block)
25✔
2385
        }
25✔
2386
        sort.Slice(blocks, func(i, j int) bool {
33✔
2387
                return blocks[i] < blocks[j]
22✔
2388
        })
22✔
2389

2390
        channelRanges := make([]BlockChannelRange, 0, len(channelsPerBlock))
11✔
2391
        for _, block := range blocks {
36✔
2392
                channelRanges = append(channelRanges, BlockChannelRange{
25✔
2393
                        Height:   block,
25✔
2394
                        Channels: channelsPerBlock[block],
25✔
2395
                })
25✔
2396
        }
25✔
2397

2398
        return channelRanges, nil
11✔
2399
}
2400

2401
// FetchChanInfos returns the set of channel edges that correspond to the passed
2402
// channel ID's. If an edge is the query is unknown to the database, it will
2403
// skipped and the result will contain only those edges that exist at the time
2404
// of the query. This can be used to respond to peer queries that are seeking to
2405
// fill in gaps in their view of the channel graph.
2406
func (c *KVStore) FetchChanInfos(chanIDs []uint64) ([]ChannelEdge, error) {
7✔
2407
        return c.fetchChanInfos(nil, chanIDs)
7✔
2408
}
7✔
2409

2410
// fetchChanInfos returns the set of channel edges that correspond to the passed
2411
// channel ID's. If an edge is the query is unknown to the database, it will
2412
// skipped and the result will contain only those edges that exist at the time
2413
// of the query. This can be used to respond to peer queries that are seeking to
2414
// fill in gaps in their view of the channel graph.
2415
//
2416
// NOTE: An optional transaction may be provided. If none is provided, then a
2417
// new one will be created.
2418
func (c *KVStore) fetchChanInfos(tx kvdb.RTx, chanIDs []uint64) (
2419
        []ChannelEdge, error) {
7✔
2420
        // TODO(roasbeef): sort cids?
7✔
2421

7✔
2422
        var (
7✔
2423
                chanEdges []ChannelEdge
7✔
2424
                cidBytes  [8]byte
7✔
2425
        )
7✔
2426

7✔
2427
        fetchChanInfos := func(tx kvdb.RTx) error {
14✔
2428
                edges := tx.ReadBucket(edgeBucket)
7✔
2429
                if edges == nil {
7✔
2430
                        return ErrGraphNoEdgesFound
×
2431
                }
×
2432
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
7✔
2433
                if edgeIndex == nil {
7✔
2434
                        return ErrGraphNoEdgesFound
×
2435
                }
×
2436
                nodes := tx.ReadBucket(nodeBucket)
7✔
2437
                if nodes == nil {
7✔
2438
                        return ErrGraphNotFound
×
2439
                }
×
2440

2441
                for _, cid := range chanIDs {
21✔
2442
                        byteOrder.PutUint64(cidBytes[:], cid)
14✔
2443

14✔
2444
                        // First, we'll fetch the static edge information. If
14✔
2445
                        // the edge is unknown, we will skip the edge and
14✔
2446
                        // continue gathering all known edges.
14✔
2447
                        edgeInfo, err := fetchChanEdgeInfo(
14✔
2448
                                edgeIndex, cidBytes[:],
14✔
2449
                        )
14✔
2450
                        switch {
14✔
2451
                        case errors.Is(err, ErrEdgeNotFound):
3✔
2452
                                continue
3✔
2453
                        case err != nil:
×
2454
                                return err
×
2455
                        }
2456

2457
                        // With the static information obtained, we'll now
2458
                        // fetch the dynamic policy info.
2459
                        edge1, edge2, err := fetchChanEdgePolicies(
11✔
2460
                                edgeIndex, edges, cidBytes[:],
11✔
2461
                        )
11✔
2462
                        if err != nil {
11✔
2463
                                return err
×
2464
                        }
×
2465

2466
                        node1, err := fetchLightningNode(
11✔
2467
                                nodes, edgeInfo.NodeKey1Bytes[:],
11✔
2468
                        )
11✔
2469
                        if err != nil {
11✔
2470
                                return err
×
2471
                        }
×
2472

2473
                        node2, err := fetchLightningNode(
11✔
2474
                                nodes, edgeInfo.NodeKey2Bytes[:],
11✔
2475
                        )
11✔
2476
                        if err != nil {
11✔
2477
                                return err
×
2478
                        }
×
2479

2480
                        chanEdges = append(chanEdges, ChannelEdge{
11✔
2481
                                Info:    &edgeInfo,
11✔
2482
                                Policy1: edge1,
11✔
2483
                                Policy2: edge2,
11✔
2484
                                Node1:   &node1,
11✔
2485
                                Node2:   &node2,
11✔
2486
                        })
11✔
2487
                }
2488

2489
                return nil
7✔
2490
        }
2491

2492
        if tx == nil {
14✔
2493
                err := kvdb.View(c.db, fetchChanInfos, func() {
14✔
2494
                        chanEdges = nil
7✔
2495
                })
7✔
2496
                if err != nil {
7✔
2497
                        return nil, err
×
2498
                }
×
2499

2500
                return chanEdges, nil
7✔
2501
        }
2502

2503
        err := fetchChanInfos(tx)
×
2504
        if err != nil {
×
2505
                return nil, err
×
2506
        }
×
2507

2508
        return chanEdges, nil
×
2509
}
2510

2511
func delEdgeUpdateIndexEntry(edgesBucket kvdb.RwBucket, chanID uint64,
2512
        edge1, edge2 *models.ChannelEdgePolicy) error {
141✔
2513

141✔
2514
        // First, we'll fetch the edge update index bucket which currently
141✔
2515
        // stores an entry for the channel we're about to delete.
141✔
2516
        updateIndex := edgesBucket.NestedReadWriteBucket(edgeUpdateIndexBucket)
141✔
2517
        if updateIndex == nil {
141✔
2518
                // No edges in bucket, return early.
×
2519
                return nil
×
2520
        }
×
2521

2522
        // Now that we have the bucket, we'll attempt to construct a template
2523
        // for the index key: updateTime || chanid.
2524
        var indexKey [8 + 8]byte
141✔
2525
        byteOrder.PutUint64(indexKey[8:], chanID)
141✔
2526

141✔
2527
        // With the template constructed, we'll attempt to delete an entry that
141✔
2528
        // would have been created by both edges: we'll alternate the update
141✔
2529
        // times, as one may had overridden the other.
141✔
2530
        if edge1 != nil {
154✔
2531
                byteOrder.PutUint64(
13✔
2532
                        indexKey[:8], uint64(edge1.LastUpdate.Unix()),
13✔
2533
                )
13✔
2534
                if err := updateIndex.Delete(indexKey[:]); err != nil {
13✔
2535
                        return err
×
2536
                }
×
2537
        }
2538

2539
        // We'll also attempt to delete the entry that may have been created by
2540
        // the second edge.
2541
        if edge2 != nil {
156✔
2542
                byteOrder.PutUint64(
15✔
2543
                        indexKey[:8], uint64(edge2.LastUpdate.Unix()),
15✔
2544
                )
15✔
2545
                if err := updateIndex.Delete(indexKey[:]); err != nil {
15✔
2546
                        return err
×
2547
                }
×
2548
        }
2549

2550
        return nil
141✔
2551
}
2552

2553
// delChannelEdgeUnsafe deletes the edge with the given chanID from the graph
2554
// cache. It then goes on to delete any policy info and edge info for this
2555
// channel from the DB and finally, if isZombie is true, it will add an entry
2556
// for this channel in the zombie index.
2557
//
2558
// NOTE: this method MUST only be called if the cacheMu has already been
2559
// acquired.
2560
func (c *KVStore) delChannelEdgeUnsafe(edges, edgeIndex, chanIndex,
2561
        zombieIndex kvdb.RwBucket, chanID []byte, isZombie,
2562
        strictZombie bool) (*models.ChannelEdgeInfo, error) {
203✔
2563

203✔
2564
        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
203✔
2565
        if err != nil {
265✔
2566
                return nil, err
62✔
2567
        }
62✔
2568

2569
        // We'll also remove the entry in the edge update index bucket before
2570
        // we delete the edges themselves so we can access their last update
2571
        // times.
2572
        cid := byteOrder.Uint64(chanID)
141✔
2573
        edge1, edge2, err := fetchChanEdgePolicies(edgeIndex, edges, chanID)
141✔
2574
        if err != nil {
141✔
2575
                return nil, err
×
2576
        }
×
2577
        err = delEdgeUpdateIndexEntry(edges, cid, edge1, edge2)
141✔
2578
        if err != nil {
141✔
2579
                return nil, err
×
2580
        }
×
2581

2582
        // The edge key is of the format pubKey || chanID. First we construct
2583
        // the latter half, populating the channel ID.
2584
        var edgeKey [33 + 8]byte
141✔
2585
        copy(edgeKey[33:], chanID)
141✔
2586

141✔
2587
        // With the latter half constructed, copy over the first public key to
141✔
2588
        // delete the edge in this direction, then the second to delete the
141✔
2589
        // edge in the opposite direction.
141✔
2590
        copy(edgeKey[:33], edgeInfo.NodeKey1Bytes[:])
141✔
2591
        if edges.Get(edgeKey[:]) != nil {
282✔
2592
                if err := edges.Delete(edgeKey[:]); err != nil {
141✔
2593
                        return nil, err
×
2594
                }
×
2595
        }
2596
        copy(edgeKey[:33], edgeInfo.NodeKey2Bytes[:])
141✔
2597
        if edges.Get(edgeKey[:]) != nil {
282✔
2598
                if err := edges.Delete(edgeKey[:]); err != nil {
141✔
2599
                        return nil, err
×
2600
                }
×
2601
        }
2602

2603
        // As part of deleting the edge we also remove all disabled entries
2604
        // from the edgePolicyDisabledIndex bucket. We do that for both
2605
        // directions.
2606
        err = updateEdgePolicyDisabledIndex(edges, cid, false, false)
141✔
2607
        if err != nil {
141✔
2608
                return nil, err
×
2609
        }
×
2610
        err = updateEdgePolicyDisabledIndex(edges, cid, true, false)
141✔
2611
        if err != nil {
141✔
2612
                return nil, err
×
2613
        }
×
2614

2615
        // With the edge data deleted, we can purge the information from the two
2616
        // edge indexes.
2617
        if err := edgeIndex.Delete(chanID); err != nil {
141✔
2618
                return nil, err
×
2619
        }
×
2620
        var b bytes.Buffer
141✔
2621
        if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
141✔
2622
                return nil, err
×
2623
        }
×
2624
        if err := chanIndex.Delete(b.Bytes()); err != nil {
141✔
2625
                return nil, err
×
2626
        }
×
2627

2628
        // Finally, we'll mark the edge as a zombie within our index if it's
2629
        // being removed due to the channel becoming a zombie. We do this to
2630
        // ensure we don't store unnecessary data for spent channels.
2631
        if !isZombie {
260✔
2632
                return &edgeInfo, nil
119✔
2633
        }
119✔
2634

2635
        nodeKey1, nodeKey2 := edgeInfo.NodeKey1Bytes, edgeInfo.NodeKey2Bytes
25✔
2636
        if strictZombie {
28✔
2637
                nodeKey1, nodeKey2 = makeZombiePubkeys(&edgeInfo, edge1, edge2)
3✔
2638
        }
3✔
2639

2640
        return &edgeInfo, markEdgeZombie(
25✔
2641
                zombieIndex, byteOrder.Uint64(chanID), nodeKey1, nodeKey2,
25✔
2642
        )
25✔
2643
}
2644

2645
// makeZombiePubkeys derives the node pubkeys to store in the zombie index for a
2646
// particular pair of channel policies. The return values are one of:
2647
//  1. (pubkey1, pubkey2)
2648
//  2. (pubkey1, blank)
2649
//  3. (blank, pubkey2)
2650
//
2651
// A blank pubkey means that corresponding node will be unable to resurrect a
2652
// channel on its own. For example, node1 may continue to publish recent
2653
// updates, but node2 has fallen way behind. After marking an edge as a zombie,
2654
// we don't want another fresh update from node1 to resurrect, as the edge can
2655
// only become live once node2 finally sends something recent.
2656
//
2657
// In the case where we have neither update, we allow either party to resurrect
2658
// the channel. If the channel were to be marked zombie again, it would be
2659
// marked with the correct lagging channel since we received an update from only
2660
// one side.
2661
func makeZombiePubkeys(info *models.ChannelEdgeInfo,
2662
        e1, e2 *models.ChannelEdgePolicy) ([33]byte, [33]byte) {
3✔
2663

3✔
2664
        switch {
3✔
2665
        // If we don't have either edge policy, we'll return both pubkeys so
2666
        // that the channel can be resurrected by either party.
UNCOV
2667
        case e1 == nil && e2 == nil:
×
UNCOV
2668
                return info.NodeKey1Bytes, info.NodeKey2Bytes
×
2669

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

2677
        // Otherwise, we're missing edge2 or edge2 is the older side, so we
2678
        // return a blank pubkey for edge1. In this case, only an update from
2679
        // edge2 can resurect the channel.
2680
        default:
2✔
2681
                return [33]byte{}, info.NodeKey2Bytes
2✔
2682
        }
2683
}
2684

2685
// UpdateEdgePolicy updates the edge routing policy for a single directed edge
2686
// within the database for the referenced channel. The `flags` attribute within
2687
// the ChannelEdgePolicy determines which of the directed edges are being
2688
// updated. If the flag is 1, then the first node's information is being
2689
// updated, otherwise it's the second node's information. The node ordering is
2690
// determined by the lexicographical ordering of the identity public keys of the
2691
// nodes on either side of the channel.
2692
func (c *KVStore) UpdateEdgePolicy(edge *models.ChannelEdgePolicy,
2693
        opts ...batch.SchedulerOption) (route.Vertex, route.Vertex, error) {
2,668✔
2694

2,668✔
2695
        var (
2,668✔
2696
                isUpdate1    bool
2,668✔
2697
                edgeNotFound bool
2,668✔
2698
                from, to     route.Vertex
2,668✔
2699
        )
2,668✔
2700

2,668✔
2701
        r := &batch.Request{
2,668✔
2702
                Opts: batch.NewSchedulerOptions(opts...),
2,668✔
2703
                Reset: func() {
5,336✔
2704
                        isUpdate1 = false
2,668✔
2705
                        edgeNotFound = false
2,668✔
2706
                },
2,668✔
2707
                Update: func(tx kvdb.RwTx) error {
2,668✔
2708
                        var err error
2,668✔
2709
                        from, to, isUpdate1, err = updateEdgePolicy(tx, edge)
2,668✔
2710
                        if err != nil {
2,671✔
2711
                                log.Errorf("UpdateEdgePolicy faild: %v", err)
3✔
2712
                        }
3✔
2713

2714
                        // Silence ErrEdgeNotFound so that the batch can
2715
                        // succeed, but propagate the error via local state.
2716
                        if errors.Is(err, ErrEdgeNotFound) {
2,671✔
2717
                                edgeNotFound = true
3✔
2718
                                return nil
3✔
2719
                        }
3✔
2720

2721
                        return err
2,665✔
2722
                },
2723
                OnCommit: func(err error) error {
2,668✔
2724
                        switch {
2,668✔
2725
                        case err != nil:
×
2726
                                return err
×
2727
                        case edgeNotFound:
3✔
2728
                                return ErrEdgeNotFound
3✔
2729
                        default:
2,665✔
2730
                                c.updateEdgeCache(edge, isUpdate1)
2,665✔
2731
                                return nil
2,665✔
2732
                        }
2733
                },
2734
        }
2735

2736
        err := c.chanScheduler.Execute(r)
2,668✔
2737

2,668✔
2738
        return from, to, err
2,668✔
2739
}
2740

2741
func (c *KVStore) updateEdgeCache(e *models.ChannelEdgePolicy,
2742
        isUpdate1 bool) {
2,665✔
2743

2,665✔
2744
        // If an entry for this channel is found in reject cache, we'll modify
2,665✔
2745
        // the entry with the updated timestamp for the direction that was just
2,665✔
2746
        // written. If the edge doesn't exist, we'll load the cache entry lazily
2,665✔
2747
        // during the next query for this edge.
2,665✔
2748
        if entry, ok := c.rejectCache.get(e.ChannelID); ok {
2,673✔
2749
                if isUpdate1 {
14✔
2750
                        entry.upd1Time = e.LastUpdate.Unix()
6✔
2751
                } else {
11✔
2752
                        entry.upd2Time = e.LastUpdate.Unix()
5✔
2753
                }
5✔
2754
                c.rejectCache.insert(e.ChannelID, entry)
8✔
2755
        }
2756

2757
        // If an entry for this channel is found in channel cache, we'll modify
2758
        // the entry with the updated policy for the direction that was just
2759
        // written. If the edge doesn't exist, we'll defer loading the info and
2760
        // policies and lazily read from disk during the next query.
2761
        if channel, ok := c.chanCache.get(e.ChannelID); ok {
2,668✔
2762
                if isUpdate1 {
6✔
2763
                        channel.Policy1 = e
3✔
2764
                } else {
6✔
2765
                        channel.Policy2 = e
3✔
2766
                }
3✔
2767
                c.chanCache.insert(e.ChannelID, channel)
3✔
2768
        }
2769
}
2770

2771
// updateEdgePolicy attempts to update an edge's policy within the relevant
2772
// buckets using an existing database transaction. The returned boolean will be
2773
// true if the updated policy belongs to node1, and false if the policy belonged
2774
// to node2.
2775
func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy) (
2776
        route.Vertex, route.Vertex, bool, error) {
2,668✔
2777

2,668✔
2778
        var noVertex route.Vertex
2,668✔
2779

2,668✔
2780
        edges := tx.ReadWriteBucket(edgeBucket)
2,668✔
2781
        if edges == nil {
2,668✔
2782
                return noVertex, noVertex, false, ErrEdgeNotFound
×
2783
        }
×
2784
        edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
2,668✔
2785
        if edgeIndex == nil {
2,668✔
2786
                return noVertex, noVertex, false, ErrEdgeNotFound
×
2787
        }
×
2788

2789
        // Create the channelID key be converting the channel ID
2790
        // integer into a byte slice.
2791
        var chanID [8]byte
2,668✔
2792
        byteOrder.PutUint64(chanID[:], edge.ChannelID)
2,668✔
2793

2,668✔
2794
        // With the channel ID, we then fetch the value storing the two
2,668✔
2795
        // nodes which connect this channel edge.
2,668✔
2796
        nodeInfo := edgeIndex.Get(chanID[:])
2,668✔
2797
        if nodeInfo == nil {
2,671✔
2798
                return noVertex, noVertex, false, ErrEdgeNotFound
3✔
2799
        }
3✔
2800

2801
        // Depending on the flags value passed above, either the first
2802
        // or second edge policy is being updated.
2803
        var fromNode, toNode []byte
2,665✔
2804
        var isUpdate1 bool
2,665✔
2805
        if edge.ChannelFlags&lnwire.ChanUpdateDirection == 0 {
4,002✔
2806
                fromNode = nodeInfo[:33]
1,337✔
2807
                toNode = nodeInfo[33:66]
1,337✔
2808
                isUpdate1 = true
1,337✔
2809
        } else {
2,668✔
2810
                fromNode = nodeInfo[33:66]
1,331✔
2811
                toNode = nodeInfo[:33]
1,331✔
2812
                isUpdate1 = false
1,331✔
2813
        }
1,331✔
2814

2815
        // Finally, with the direction of the edge being updated
2816
        // identified, we update the on-disk edge representation.
2817
        err := putChanEdgePolicy(edges, edge, fromNode, toNode)
2,665✔
2818
        if err != nil {
2,665✔
2819
                return noVertex, noVertex, false, err
×
2820
        }
×
2821

2822
        var (
2,665✔
2823
                fromNodePubKey route.Vertex
2,665✔
2824
                toNodePubKey   route.Vertex
2,665✔
2825
        )
2,665✔
2826
        copy(fromNodePubKey[:], fromNode)
2,665✔
2827
        copy(toNodePubKey[:], toNode)
2,665✔
2828

2,665✔
2829
        return fromNodePubKey, toNodePubKey, isUpdate1, nil
2,665✔
2830
}
2831

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

16✔
2838
        // In order to determine whether this node is publicly advertised within
16✔
2839
        // the graph, we'll need to look at all of its edges and check whether
16✔
2840
        // they extend to any other node than the source node. errDone will be
16✔
2841
        // used to terminate the check early.
16✔
2842
        nodeIsPublic := false
16✔
2843
        errDone := errors.New("done")
16✔
2844
        err := c.forEachNodeChannelTx(tx, nodePub, func(tx kvdb.RTx,
16✔
2845
                info *models.ChannelEdgeInfo, _ *models.ChannelEdgePolicy,
16✔
2846
                _ *models.ChannelEdgePolicy) error {
29✔
2847

13✔
2848
                // If this edge doesn't extend to the source node, we'll
13✔
2849
                // terminate our search as we can now conclude that the node is
13✔
2850
                // publicly advertised within the graph due to the local node
13✔
2851
                // knowing of the current edge.
13✔
2852
                if !bytes.Equal(info.NodeKey1Bytes[:], sourcePubKey) &&
13✔
2853
                        !bytes.Equal(info.NodeKey2Bytes[:], sourcePubKey) {
19✔
2854

6✔
2855
                        nodeIsPublic = true
6✔
2856
                        return errDone
6✔
2857
                }
6✔
2858

2859
                // Since the edge _does_ extend to the source node, we'll also
2860
                // need to ensure that this is a public edge.
2861
                if info.AuthProof != nil {
19✔
2862
                        nodeIsPublic = true
9✔
2863
                        return errDone
9✔
2864
                }
9✔
2865

2866
                // Otherwise, we'll continue our search.
2867
                return nil
4✔
2868
        })
2869
        if err != nil && !errors.Is(err, errDone) {
16✔
2870
                return false, err
×
2871
        }
×
2872

2873
        return nodeIsPublic, nil
16✔
2874
}
2875

2876
// FetchLightningNodeTx attempts to look up a target node by its identity
2877
// public key. If the node isn't found in the database, then
2878
// ErrGraphNodeNotFound is returned. An optional transaction may be provided.
2879
// If none is provided, then a new one will be created.
2880
func (c *KVStore) FetchLightningNodeTx(tx kvdb.RTx, nodePub route.Vertex) (
2881
        *models.LightningNode, error) {
3,634✔
2882

3,634✔
2883
        return c.fetchLightningNode(tx, nodePub)
3,634✔
2884
}
3,634✔
2885

2886
// FetchLightningNode attempts to look up a target node by its identity public
2887
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
2888
// returned.
2889
func (c *KVStore) FetchLightningNode(nodePub route.Vertex) (
2890
        *models.LightningNode, error) {
156✔
2891

156✔
2892
        return c.fetchLightningNode(nil, nodePub)
156✔
2893
}
156✔
2894

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

3,787✔
2902
        var node *models.LightningNode
3,787✔
2903
        fetch := func(tx kvdb.RTx) error {
7,574✔
2904
                // First grab the nodes bucket which stores the mapping from
3,787✔
2905
                // pubKey to node information.
3,787✔
2906
                nodes := tx.ReadBucket(nodeBucket)
3,787✔
2907
                if nodes == nil {
3,787✔
2908
                        return ErrGraphNotFound
×
2909
                }
×
2910

2911
                // If a key for this serialized public key isn't found, then
2912
                // the target node doesn't exist within the database.
2913
                nodeBytes := nodes.Get(nodePub[:])
3,787✔
2914
                if nodeBytes == nil {
3,804✔
2915
                        return ErrGraphNodeNotFound
17✔
2916
                }
17✔
2917

2918
                // If the node is found, then we can de deserialize the node
2919
                // information to return to the user.
2920
                nodeReader := bytes.NewReader(nodeBytes)
3,773✔
2921
                n, err := deserializeLightningNode(nodeReader)
3,773✔
2922
                if err != nil {
3,773✔
2923
                        return err
×
2924
                }
×
2925

2926
                node = &n
3,773✔
2927

3,773✔
2928
                return nil
3,773✔
2929
        }
2930

2931
        if tx == nil {
3,947✔
2932
                err := kvdb.View(
160✔
2933
                        c.db, fetch, func() {
320✔
2934
                                node = nil
160✔
2935
                        },
160✔
2936
                )
2937
                if err != nil {
166✔
2938
                        return nil, err
6✔
2939
                }
6✔
2940

2941
                return node, nil
157✔
2942
        }
2943

2944
        err := fetch(tx)
3,627✔
2945
        if err != nil {
3,638✔
2946
                return nil, err
11✔
2947
        }
11✔
2948

2949
        return node, nil
3,616✔
2950
}
2951

2952
// HasLightningNode determines if the graph has a vertex identified by the
2953
// target node identity public key. If the node exists in the database, a
2954
// timestamp of when the data for the node was lasted updated is returned along
2955
// with a true boolean. Otherwise, an empty time.Time is returned with a false
2956
// boolean.
2957
func (c *KVStore) HasLightningNode(nodePub [33]byte) (time.Time, bool,
2958
        error) {
20✔
2959

20✔
2960
        var (
20✔
2961
                updateTime time.Time
20✔
2962
                exists     bool
20✔
2963
        )
20✔
2964

20✔
2965
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
40✔
2966
                // First grab the nodes bucket which stores the mapping from
20✔
2967
                // pubKey to node information.
20✔
2968
                nodes := tx.ReadBucket(nodeBucket)
20✔
2969
                if nodes == nil {
20✔
2970
                        return ErrGraphNotFound
×
2971
                }
×
2972

2973
                // If a key for this serialized public key isn't found, we can
2974
                // exit early.
2975
                nodeBytes := nodes.Get(nodePub[:])
20✔
2976
                if nodeBytes == nil {
26✔
2977
                        exists = false
6✔
2978
                        return nil
6✔
2979
                }
6✔
2980

2981
                // Otherwise we continue on to obtain the time stamp
2982
                // representing the last time the data for this node was
2983
                // updated.
2984
                nodeReader := bytes.NewReader(nodeBytes)
17✔
2985
                node, err := deserializeLightningNode(nodeReader)
17✔
2986
                if err != nil {
17✔
2987
                        return err
×
2988
                }
×
2989

2990
                exists = true
17✔
2991
                updateTime = node.LastUpdate
17✔
2992

17✔
2993
                return nil
17✔
2994
        }, func() {
20✔
2995
                updateTime = time.Time{}
20✔
2996
                exists = false
20✔
2997
        })
20✔
2998
        if err != nil {
20✔
2999
                return time.Time{}, exists, err
×
3000
        }
×
3001

3002
        return updateTime, exists, nil
20✔
3003
}
3004

3005
// nodeTraversal is used to traverse all channels of a node given by its
3006
// public key and passes channel information into the specified callback.
3007
func nodeTraversal(tx kvdb.RTx, nodePub []byte, db kvdb.Backend,
3008
        cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3009
                *models.ChannelEdgePolicy) error) error {
1,250✔
3010

1,250✔
3011
        traversal := func(tx kvdb.RTx) error {
2,500✔
3012
                edges := tx.ReadBucket(edgeBucket)
1,250✔
3013
                if edges == nil {
1,250✔
3014
                        return ErrGraphNotFound
×
3015
                }
×
3016
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
1,250✔
3017
                if edgeIndex == nil {
1,250✔
3018
                        return ErrGraphNoEdgesFound
×
3019
                }
×
3020

3021
                // In order to reach all the edges for this node, we take
3022
                // advantage of the construction of the key-space within the
3023
                // edge bucket. The keys are stored in the form: pubKey ||
3024
                // chanID. Therefore, starting from a chanID of zero, we can
3025
                // scan forward in the bucket, grabbing all the edges for the
3026
                // node. Once the prefix no longer matches, then we know we're
3027
                // done.
3028
                var nodeStart [33 + 8]byte
1,250✔
3029
                copy(nodeStart[:], nodePub)
1,250✔
3030
                copy(nodeStart[33:], chanStart[:])
1,250✔
3031

1,250✔
3032
                // Starting from the key pubKey || 0, we seek forward in the
1,250✔
3033
                // bucket until the retrieved key no longer has the public key
1,250✔
3034
                // as its prefix. This indicates that we've stepped over into
1,250✔
3035
                // another node's edges, so we can terminate our scan.
1,250✔
3036
                edgeCursor := edges.ReadCursor()
1,250✔
3037
                for nodeEdge, _ := edgeCursor.Seek(nodeStart[:]); bytes.HasPrefix(nodeEdge, nodePub); nodeEdge, _ = edgeCursor.Next() { //nolint:ll
4,905✔
3038
                        // If the prefix still matches, the channel id is
3,655✔
3039
                        // returned in nodeEdge. Channel id is used to lookup
3,655✔
3040
                        // the node at the other end of the channel and both
3,655✔
3041
                        // edge policies.
3,655✔
3042
                        chanID := nodeEdge[33:]
3,655✔
3043
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3,655✔
3044
                        if err != nil {
3,655✔
3045
                                return err
×
3046
                        }
×
3047

3048
                        outgoingPolicy, err := fetchChanEdgePolicy(
3,655✔
3049
                                edges, chanID, nodePub,
3,655✔
3050
                        )
3,655✔
3051
                        if err != nil {
3,655✔
3052
                                return err
×
3053
                        }
×
3054

3055
                        otherNode, err := edgeInfo.OtherNodeKeyBytes(nodePub)
3,655✔
3056
                        if err != nil {
3,655✔
3057
                                return err
×
3058
                        }
×
3059

3060
                        incomingPolicy, err := fetchChanEdgePolicy(
3,655✔
3061
                                edges, chanID, otherNode[:],
3,655✔
3062
                        )
3,655✔
3063
                        if err != nil {
3,655✔
3064
                                return err
×
3065
                        }
×
3066

3067
                        // Finally, we execute the callback.
3068
                        err = cb(tx, &edgeInfo, outgoingPolicy, incomingPolicy)
3,655✔
3069
                        if err != nil {
3,667✔
3070
                                return err
12✔
3071
                        }
12✔
3072
                }
3073

3074
                return nil
1,241✔
3075
        }
3076

3077
        // If no transaction was provided, then we'll create a new transaction
3078
        // to execute the transaction within.
3079
        if tx == nil {
1,262✔
3080
                return kvdb.View(db, traversal, func() {})
24✔
3081
        }
3082

3083
        // Otherwise, we re-use the existing transaction to execute the graph
3084
        // traversal.
3085
        return traversal(tx)
1,241✔
3086
}
3087

3088
// ForEachNodeChannel iterates through all channels of the given node,
3089
// executing the passed callback with an edge info structure and the policies
3090
// of each end of the channel. The first edge policy is the outgoing edge *to*
3091
// the connecting node, while the second is the incoming edge *from* the
3092
// connecting node. If the callback returns an error, then the iteration is
3093
// halted with the error propagated back up to the caller.
3094
//
3095
// Unknown policies are passed into the callback as nil values.
3096
func (c *KVStore) ForEachNodeChannel(nodePub route.Vertex,
3097
        cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3098
                *models.ChannelEdgePolicy) error) error {
9✔
3099

9✔
3100
        return nodeTraversal(nil, nodePub[:], c.db, func(_ kvdb.RTx,
9✔
3101
                info *models.ChannelEdgeInfo, policy,
9✔
3102
                policy2 *models.ChannelEdgePolicy) error {
22✔
3103

13✔
3104
                return cb(info, policy, policy2)
13✔
3105
        })
13✔
3106
}
3107

3108
// ForEachSourceNodeChannel iterates through all channels of the source node,
3109
// executing the passed callback on each. The callback is provided with the
3110
// channel's outpoint, whether we have a policy for the channel and the channel
3111
// peer's node information.
3112
func (c *KVStore) ForEachSourceNodeChannel(cb func(chanPoint wire.OutPoint,
3113
        havePolicy bool, otherNode *models.LightningNode) error) error {
4✔
3114

4✔
3115
        return kvdb.View(c.db, func(tx kvdb.RTx) error {
8✔
3116
                nodes := tx.ReadBucket(nodeBucket)
4✔
3117
                if nodes == nil {
4✔
NEW
3118
                        return ErrGraphNotFound
×
NEW
3119
                }
×
3120

3121
                node, err := c.sourceNode(nodes)
4✔
3122
                if err != nil {
4✔
NEW
3123
                        return err
×
NEW
3124
                }
×
3125

3126
                return nodeTraversal(
4✔
3127
                        tx, node.PubKeyBytes[:], c.db, func(tx kvdb.RTx,
4✔
3128
                                info *models.ChannelEdgeInfo,
4✔
3129
                                policy, _ *models.ChannelEdgePolicy) error {
9✔
3130

5✔
3131
                                peer, err := c.fetchOtherNode(
5✔
3132
                                        tx, info, node.PubKeyBytes[:],
5✔
3133
                                )
5✔
3134
                                if err != nil {
5✔
NEW
3135
                                        return err
×
NEW
3136
                                }
×
3137

3138
                                return cb(
5✔
3139
                                        info.ChannelPoint, policy != nil, peer,
5✔
3140
                                )
5✔
3141
                        },
3142
                )
3143
        }, func() {})
4✔
3144
}
3145

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

1,001✔
3164
        return nodeTraversal(tx, nodePub[:], c.db, cb)
1,001✔
3165
}
1,001✔
3166

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

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

3186
        var targetNode *models.LightningNode
5✔
3187
        fetchNodeFunc := func(tx kvdb.RTx) error {
10✔
3188
                // First grab the nodes bucket which stores the mapping from
5✔
3189
                // pubKey to node information.
5✔
3190
                nodes := tx.ReadBucket(nodeBucket)
5✔
3191
                if nodes == nil {
5✔
3192
                        return ErrGraphNotFound
×
3193
                }
×
3194

3195
                node, err := fetchLightningNode(nodes, targetNodeBytes[:])
5✔
3196
                if err != nil {
5✔
3197
                        return err
×
3198
                }
×
3199

3200
                targetNode = &node
5✔
3201

5✔
3202
                return nil
5✔
3203
        }
3204

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

3216
        return targetNode, err
5✔
3217
}
3218

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

25✔
3228
        copy(node1Key[:], info.NodeKey1Bytes[:])
25✔
3229
        copy(node2Key[:], info.NodeKey2Bytes[:])
25✔
3230

25✔
3231
        byteOrder.PutUint64(node1Key[33:], info.ChannelID)
25✔
3232
        byteOrder.PutUint64(node2Key[33:], info.ChannelID)
25✔
3233

25✔
3234
        return node1Key[:], node2Key[:]
25✔
3235
}
25✔
3236

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

14✔
3246
        var (
14✔
3247
                edgeInfo *models.ChannelEdgeInfo
14✔
3248
                policy1  *models.ChannelEdgePolicy
14✔
3249
                policy2  *models.ChannelEdgePolicy
14✔
3250
        )
14✔
3251

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

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

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

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

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

3303
                policy1 = e1
4✔
3304
                policy2 = e2
4✔
3305

4✔
3306
                return nil
4✔
3307
        }, func() {
14✔
3308
                edgeInfo = nil
14✔
3309
                policy1 = nil
14✔
3310
                policy2 = nil
14✔
3311
        })
14✔
3312
        if err != nil {
27✔
3313
                return nil, nil, nil, err
13✔
3314
        }
13✔
3315

3316
        return edgeInfo, policy1, policy2, nil
4✔
3317
}
3318

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

2,687✔
3332
        var (
2,687✔
3333
                edgeInfo  *models.ChannelEdgeInfo
2,687✔
3334
                policy1   *models.ChannelEdgePolicy
2,687✔
3335
                policy2   *models.ChannelEdgePolicy
2,687✔
3336
                channelID [8]byte
2,687✔
3337
        )
2,687✔
3338

2,687✔
3339
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
5,374✔
3340
                // First, grab the node bucket. This will be used to populate
2,687✔
3341
                // the Node pointers in each edge read from disk.
2,687✔
3342
                nodes := tx.ReadBucket(nodeBucket)
2,687✔
3343
                if nodes == nil {
2,687✔
3344
                        return ErrGraphNotFound
×
3345
                }
×
3346

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

3359
                byteOrder.PutUint64(channelID[:], chanID)
2,687✔
3360

2,687✔
3361
                // Now, attempt to fetch edge.
2,687✔
3362
                edge, err := fetchChanEdgeInfo(edgeIndex, channelID[:])
2,687✔
3363

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

3375
                        isZombie, pubKey1, pubKey2 := isZombieEdge(
6✔
3376
                                zombieIndex, chanID,
6✔
3377
                        )
6✔
3378
                        if !isZombie {
10✔
3379
                                return ErrEdgeNotFound
4✔
3380
                        }
4✔
3381

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

5✔
3391
                        return ErrZombieEdge
5✔
3392
                }
3393

3394
                // Otherwise, we'll just return the error if any.
3395
                if err != nil {
2,684✔
3396
                        return err
×
3397
                }
×
3398

3399
                edgeInfo = &edge
2,684✔
3400

2,684✔
3401
                // Then we'll attempt to fetch the accompanying policies of this
2,684✔
3402
                // edge.
2,684✔
3403
                e1, e2, err := fetchChanEdgePolicies(
2,684✔
3404
                        edgeIndex, edges, channelID[:],
2,684✔
3405
                )
2,684✔
3406
                if err != nil {
2,684✔
3407
                        return err
×
3408
                }
×
3409

3410
                policy1 = e1
2,684✔
3411
                policy2 = e2
2,684✔
3412

2,684✔
3413
                return nil
2,684✔
3414
        }, func() {
2,687✔
3415
                edgeInfo = nil
2,687✔
3416
                policy1 = nil
2,687✔
3417
                policy2 = nil
2,687✔
3418
        })
2,687✔
3419
        if errors.Is(err, ErrZombieEdge) {
2,692✔
3420
                return edgeInfo, nil, nil, err
5✔
3421
        }
5✔
3422
        if err != nil {
2,689✔
3423
                return nil, nil, nil, err
4✔
3424
        }
4✔
3425

3426
        return edgeInfo, policy1, policy2, nil
2,684✔
3427
}
3428

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

3448
                nodeIsPublic, err = c.isPublic(tx, node.PubKeyBytes, ourPubKey)
16✔
3449

16✔
3450
                return err
16✔
3451
        }, func() {
16✔
3452
                nodeIsPublic = false
16✔
3453
        })
16✔
3454
        if err != nil {
16✔
3455
                return false, err
×
3456
        }
×
3457

3458
        return nodeIsPublic, nil
16✔
3459
}
3460

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

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

49✔
3478
        return bldr.Script()
49✔
3479
}
3480

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

3489
        // OutPoint is the outpoint of the target channel.
3490
        OutPoint wire.OutPoint
3491
}
3492

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

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

3522
                // Once we have the proper bucket, we'll range over each key
3523
                // (which is the channel point for the channel) and decode it,
3524
                // accumulating each entry.
3525
                return chanIndex.ForEach(
25✔
3526
                        func(chanPointBytes, chanID []byte) error {
70✔
3527
                                chanPointReader := bytes.NewReader(
45✔
3528
                                        chanPointBytes,
45✔
3529
                                )
45✔
3530

45✔
3531
                                var chanPoint wire.OutPoint
45✔
3532
                                err := ReadOutpoint(chanPointReader, &chanPoint)
45✔
3533
                                if err != nil {
45✔
3534
                                        return err
×
3535
                                }
×
3536

3537
                                edgeInfo, err := fetchChanEdgeInfo(
45✔
3538
                                        edgeIndex, chanID,
45✔
3539
                                )
45✔
3540
                                if err != nil {
45✔
3541
                                        return err
×
3542
                                }
×
3543

3544
                                pkScript, err := genMultiSigP2WSH(
45✔
3545
                                        edgeInfo.BitcoinKey1Bytes[:],
45✔
3546
                                        edgeInfo.BitcoinKey2Bytes[:],
45✔
3547
                                )
45✔
3548
                                if err != nil {
45✔
3549
                                        return err
×
3550
                                }
×
3551

3552
                                edgePoints = append(edgePoints, EdgePoint{
45✔
3553
                                        FundingPkScript: pkScript,
45✔
3554
                                        OutPoint:        chanPoint,
45✔
3555
                                })
45✔
3556

45✔
3557
                                return nil
45✔
3558
                        },
3559
                )
3560
        }, func() {
25✔
3561
                edgePoints = nil
25✔
3562
        }); err != nil {
25✔
3563
                return nil, err
×
3564
        }
×
3565

3566
        return edgePoints, nil
25✔
3567
}
3568

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

129✔
3575
        c.cacheMu.Lock()
129✔
3576
        defer c.cacheMu.Unlock()
129✔
3577

129✔
3578
        err := kvdb.Batch(c.db, func(tx kvdb.RwTx) error {
258✔
3579
                edges := tx.ReadWriteBucket(edgeBucket)
129✔
3580
                if edges == nil {
129✔
3581
                        return ErrGraphNoEdgesFound
×
3582
                }
×
3583
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
129✔
3584
                if err != nil {
129✔
3585
                        return fmt.Errorf("unable to create zombie "+
×
3586
                                "bucket: %w", err)
×
3587
                }
×
3588

3589
                return markEdgeZombie(zombieIndex, chanID, pubKey1, pubKey2)
129✔
3590
        })
3591
        if err != nil {
129✔
3592
                return err
×
3593
        }
×
3594

3595
        c.rejectCache.remove(chanID)
129✔
3596
        c.chanCache.remove(chanID)
129✔
3597

129✔
3598
        return nil
129✔
3599
}
3600

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

154✔
3607
        var k [8]byte
154✔
3608
        byteOrder.PutUint64(k[:], chanID)
154✔
3609

154✔
3610
        var v [66]byte
154✔
3611
        copy(v[:33], pubKey1[:])
154✔
3612
        copy(v[33:], pubKey2[:])
154✔
3613

154✔
3614
        return zombieIndex.Put(k[:], v[:])
154✔
3615
}
154✔
3616

3617
// MarkEdgeLive clears an edge from our zombie index, deeming it as live.
3618
func (c *KVStore) MarkEdgeLive(chanID uint64) error {
23✔
3619
        c.cacheMu.Lock()
23✔
3620
        defer c.cacheMu.Unlock()
23✔
3621

23✔
3622
        return c.markEdgeLiveUnsafe(nil, chanID)
23✔
3623
}
23✔
3624

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

3642
                var k [8]byte
23✔
3643
                byteOrder.PutUint64(k[:], chanID)
23✔
3644

23✔
3645
                if len(zombieIndex.Get(k[:])) == 0 {
24✔
3646
                        return ErrZombieEdgeNotFound
1✔
3647
                }
1✔
3648

3649
                return zombieIndex.Delete(k[:])
22✔
3650
        }
3651

3652
        // If the transaction is nil, we'll create a new one. Otherwise, we use
3653
        // the existing transaction
3654
        var err error
23✔
3655
        if tx == nil {
46✔
3656
                err = kvdb.Update(c.db, dbFn, func() {})
46✔
3657
        } else {
×
3658
                err = dbFn(tx)
×
3659
        }
×
3660
        if err != nil {
24✔
3661
                return err
1✔
3662
        }
1✔
3663

3664
        c.rejectCache.remove(chanID)
22✔
3665
        c.chanCache.remove(chanID)
22✔
3666

22✔
3667
        return nil
22✔
3668
}
3669

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

14✔
3679
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
28✔
3680
                edges := tx.ReadBucket(edgeBucket)
14✔
3681
                if edges == nil {
14✔
3682
                        return ErrGraphNoEdgesFound
×
3683
                }
×
3684
                zombieIndex := edges.NestedReadBucket(zombieBucket)
14✔
3685
                if zombieIndex == nil {
14✔
3686
                        return nil
×
3687
                }
×
3688

3689
                isZombie, pubKey1, pubKey2 = isZombieEdge(zombieIndex, chanID)
14✔
3690

14✔
3691
                return nil
14✔
3692
        }, func() {
14✔
3693
                isZombie = false
14✔
3694
                pubKey1 = [33]byte{}
14✔
3695
                pubKey2 = [33]byte{}
14✔
3696
        })
14✔
3697
        if err != nil {
14✔
3698
                return false, [33]byte{}, [33]byte{}
×
3699
        }
×
3700

3701
        return isZombie, pubKey1, pubKey2
14✔
3702
}
3703

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

204✔
3710
        var k [8]byte
204✔
3711
        byteOrder.PutUint64(k[:], chanID)
204✔
3712

204✔
3713
        v := zombieIndex.Get(k[:])
204✔
3714
        if v == nil {
315✔
3715
                return false, [33]byte{}, [33]byte{}
111✔
3716
        }
111✔
3717

3718
        var pubKey1, pubKey2 [33]byte
96✔
3719
        copy(pubKey1[:], v[:33])
96✔
3720
        copy(pubKey2[:], v[33:])
96✔
3721

96✔
3722
        return true, pubKey1, pubKey2
96✔
3723
}
3724

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

3738
                return zombieIndex.ForEach(func(_, _ []byte) error {
6✔
3739
                        numZombies++
2✔
3740
                        return nil
2✔
3741
                })
2✔
3742
        }, func() {
4✔
3743
                numZombies = 0
4✔
3744
        })
4✔
3745
        if err != nil {
4✔
3746
                return 0, err
×
3747
        }
×
3748

3749
        return numZombies, nil
4✔
3750
}
3751

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

3762
                var k [8]byte
1✔
3763
                byteOrder.PutUint64(k[:], scid.ToUint64())
1✔
3764

1✔
3765
                return closedScids.Put(k[:], []byte{})
1✔
3766
        }, func() {})
1✔
3767
}
3768

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

3780
                var k [8]byte
5✔
3781
                byteOrder.PutUint64(k[:], scid.ToUint64())
5✔
3782

5✔
3783
                if closedScids.Get(k[:]) != nil {
6✔
3784
                        isClosed = true
1✔
3785
                        return nil
1✔
3786
                }
1✔
3787

3788
                return nil
4✔
3789
        }, func() {
5✔
3790
                isClosed = false
5✔
3791
        })
5✔
3792
        if err != nil {
5✔
3793
                return false, err
×
3794
        }
×
3795

3796
        return isClosed, nil
5✔
3797
}
3798

3799
// GraphSession will provide the call-back with access to a NodeTraverser
3800
// instance which can be used to perform queries against the channel graph.
3801
func (c *KVStore) GraphSession(cb func(graph NodeTraverser) error) error {
54✔
3802
        return c.db.View(func(tx walletdb.ReadTx) error {
108✔
3803
                return cb(&nodeTraverserSession{
54✔
3804
                        db: c,
54✔
3805
                        tx: tx,
54✔
3806
                })
54✔
3807
        }, func() {})
108✔
3808
}
3809

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

3817
// ForEachNodeDirectedChannel calls the callback for every channel of the given
3818
// node.
3819
//
3820
// NOTE: Part of the NodeTraverser interface.
3821
func (c *nodeTraverserSession) ForEachNodeDirectedChannel(nodePub route.Vertex,
3822
        cb func(channel *DirectedChannel) error) error {
239✔
3823

239✔
3824
        return c.db.forEachNodeDirectedChannel(c.tx, nodePub, cb)
239✔
3825
}
239✔
3826

3827
// FetchNodeFeatures returns the features of the given node. If the node is
3828
// unknown, assume no additional features are supported.
3829
//
3830
// NOTE: Part of the NodeTraverser interface.
3831
func (c *nodeTraverserSession) FetchNodeFeatures(nodePub route.Vertex) (
3832
        *lnwire.FeatureVector, error) {
254✔
3833

254✔
3834
        return c.db.fetchNodeFeatures(c.tx, nodePub)
254✔
3835
}
254✔
3836

3837
func putLightningNode(nodeBucket, aliasBucket, updateIndex kvdb.RwBucket,
3838
        node *models.LightningNode) error {
996✔
3839

996✔
3840
        var (
996✔
3841
                scratch [16]byte
996✔
3842
                b       bytes.Buffer
996✔
3843
        )
996✔
3844

996✔
3845
        pub, err := node.PubKey()
996✔
3846
        if err != nil {
996✔
3847
                return err
×
3848
        }
×
3849
        nodePub := pub.SerializeCompressed()
996✔
3850

996✔
3851
        // If the node has the update time set, write it, else write 0.
996✔
3852
        updateUnix := uint64(0)
996✔
3853
        if node.LastUpdate.Unix() > 0 {
1,858✔
3854
                updateUnix = uint64(node.LastUpdate.Unix())
862✔
3855
        }
862✔
3856

3857
        byteOrder.PutUint64(scratch[:8], updateUnix)
996✔
3858
        if _, err := b.Write(scratch[:8]); err != nil {
996✔
3859
                return err
×
3860
        }
×
3861

3862
        if _, err := b.Write(nodePub); err != nil {
996✔
3863
                return err
×
3864
        }
×
3865

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

3875
                return nodeBucket.Put(nodePub, b.Bytes())
84✔
3876
        }
3877

3878
        // Write HaveNodeAnnouncement=1.
3879
        byteOrder.PutUint16(scratch[:2], 1)
915✔
3880
        if _, err := b.Write(scratch[:2]); err != nil {
915✔
3881
                return err
×
3882
        }
×
3883

3884
        if err := binary.Write(&b, byteOrder, node.Color.R); err != nil {
915✔
3885
                return err
×
3886
        }
×
3887
        if err := binary.Write(&b, byteOrder, node.Color.G); err != nil {
915✔
3888
                return err
×
3889
        }
×
3890
        if err := binary.Write(&b, byteOrder, node.Color.B); err != nil {
915✔
3891
                return err
×
3892
        }
×
3893

3894
        if err := wire.WriteVarString(&b, 0, node.Alias); err != nil {
915✔
3895
                return err
×
3896
        }
×
3897

3898
        if err := node.Features.Encode(&b); err != nil {
915✔
3899
                return err
×
3900
        }
×
3901

3902
        numAddresses := uint16(len(node.Addresses))
915✔
3903
        byteOrder.PutUint16(scratch[:2], numAddresses)
915✔
3904
        if _, err := b.Write(scratch[:2]); err != nil {
915✔
3905
                return err
×
3906
        }
×
3907

3908
        for _, address := range node.Addresses {
2,059✔
3909
                if err := SerializeAddr(&b, address); err != nil {
1,144✔
3910
                        return err
×
3911
                }
×
3912
        }
3913

3914
        sigLen := len(node.AuthSigBytes)
915✔
3915
        if sigLen > 80 {
915✔
3916
                return fmt.Errorf("max sig len allowed is 80, had %v",
×
3917
                        sigLen)
×
3918
        }
×
3919

3920
        err = wire.WriteVarBytes(&b, 0, node.AuthSigBytes)
915✔
3921
        if err != nil {
915✔
3922
                return err
×
3923
        }
×
3924

3925
        if len(node.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
915✔
3926
                return ErrTooManyExtraOpaqueBytes(len(node.ExtraOpaqueData))
×
3927
        }
×
3928
        err = wire.WriteVarBytes(&b, 0, node.ExtraOpaqueData)
915✔
3929
        if err != nil {
915✔
3930
                return err
×
3931
        }
×
3932

3933
        if err := aliasBucket.Put(nodePub, []byte(node.Alias)); err != nil {
915✔
3934
                return err
×
3935
        }
×
3936

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

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

107✔
3950
                var oldIndexKey [8 + 33]byte
107✔
3951
                copy(oldIndexKey[:8], oldUpdateTime)
107✔
3952
                copy(oldIndexKey[8:], nodePub)
107✔
3953

107✔
3954
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
107✔
3955
                        return err
×
3956
                }
×
3957
        }
3958

3959
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
915✔
3960
                return err
×
3961
        }
×
3962

3963
        return nodeBucket.Put(nodePub, b.Bytes())
915✔
3964
}
3965

3966
func fetchLightningNode(nodeBucket kvdb.RBucket,
3967
        nodePub []byte) (models.LightningNode, error) {
3,631✔
3968

3,631✔
3969
        nodeBytes := nodeBucket.Get(nodePub)
3,631✔
3970
        if nodeBytes == nil {
3,715✔
3971
                return models.LightningNode{}, ErrGraphNodeNotFound
84✔
3972
        }
84✔
3973

3974
        nodeReader := bytes.NewReader(nodeBytes)
3,550✔
3975

3,550✔
3976
        return deserializeLightningNode(nodeReader)
3,550✔
3977
}
3978

3979
func deserializeLightningNodeCacheable(r io.Reader) (route.Vertex,
3980
        *lnwire.FeatureVector, error) {
123✔
3981

123✔
3982
        var (
123✔
3983
                pubKey      route.Vertex
123✔
3984
                features    = lnwire.EmptyFeatureVector()
123✔
3985
                nodeScratch [8]byte
123✔
3986
        )
123✔
3987

123✔
3988
        // Skip ahead:
123✔
3989
        // - LastUpdate (8 bytes)
123✔
3990
        if _, err := r.Read(nodeScratch[:]); err != nil {
123✔
3991
                return pubKey, nil, err
×
3992
        }
×
3993

3994
        if _, err := io.ReadFull(r, pubKey[:]); err != nil {
123✔
3995
                return pubKey, nil, err
×
3996
        }
×
3997

3998
        // Read the node announcement flag.
3999
        if _, err := r.Read(nodeScratch[:2]); err != nil {
123✔
4000
                return pubKey, nil, err
×
4001
        }
×
4002
        hasNodeAnn := byteOrder.Uint16(nodeScratch[:2])
123✔
4003

123✔
4004
        // The rest of the data is optional, and will only be there if we got a
123✔
4005
        // node announcement for this node.
123✔
4006
        if hasNodeAnn == 0 {
126✔
4007
                return pubKey, features, nil
3✔
4008
        }
3✔
4009

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

4023
        if _, err := wire.ReadVarString(r, 0); err != nil {
123✔
4024
                return pubKey, nil, err
×
4025
        }
×
4026

4027
        if err := features.Decode(r); err != nil {
123✔
4028
                return pubKey, nil, err
×
4029
        }
×
4030

4031
        return pubKey, features, nil
123✔
4032
}
4033

4034
func deserializeLightningNode(r io.Reader) (models.LightningNode, error) {
8,512✔
4035
        var (
8,512✔
4036
                node    models.LightningNode
8,512✔
4037
                scratch [8]byte
8,512✔
4038
                err     error
8,512✔
4039
        )
8,512✔
4040

8,512✔
4041
        // Always populate a feature vector, even if we don't have a node
8,512✔
4042
        // announcement and short circuit below.
8,512✔
4043
        node.Features = lnwire.EmptyFeatureVector()
8,512✔
4044

8,512✔
4045
        if _, err := r.Read(scratch[:]); err != nil {
8,512✔
4046
                return models.LightningNode{}, err
×
4047
        }
×
4048

4049
        unix := int64(byteOrder.Uint64(scratch[:]))
8,512✔
4050
        node.LastUpdate = time.Unix(unix, 0)
8,512✔
4051

8,512✔
4052
        if _, err := io.ReadFull(r, node.PubKeyBytes[:]); err != nil {
8,512✔
4053
                return models.LightningNode{}, err
×
4054
        }
×
4055

4056
        if _, err := r.Read(scratch[:2]); err != nil {
8,512✔
4057
                return models.LightningNode{}, err
×
4058
        }
×
4059

4060
        hasNodeAnn := byteOrder.Uint16(scratch[:2])
8,512✔
4061
        if hasNodeAnn == 1 {
16,878✔
4062
                node.HaveNodeAnnouncement = true
8,366✔
4063
        } else {
8,515✔
4064
                node.HaveNodeAnnouncement = false
149✔
4065
        }
149✔
4066

4067
        // The rest of the data is optional, and will only be there if we got a
4068
        // node announcement for this node.
4069
        if !node.HaveNodeAnnouncement {
8,661✔
4070
                return node, nil
149✔
4071
        }
149✔
4072

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

4085
        node.Alias, err = wire.ReadVarString(r, 0)
8,366✔
4086
        if err != nil {
8,366✔
4087
                return models.LightningNode{}, err
×
4088
        }
×
4089

4090
        err = node.Features.Decode(r)
8,366✔
4091
        if err != nil {
8,366✔
4092
                return models.LightningNode{}, err
×
4093
        }
×
4094

4095
        if _, err := r.Read(scratch[:2]); err != nil {
8,366✔
4096
                return models.LightningNode{}, err
×
4097
        }
×
4098
        numAddresses := int(byteOrder.Uint16(scratch[:2]))
8,366✔
4099

8,366✔
4100
        var addresses []net.Addr
8,366✔
4101
        for i := 0; i < numAddresses; i++ {
18,965✔
4102
                address, err := DeserializeAddr(r)
10,599✔
4103
                if err != nil {
10,599✔
4104
                        return models.LightningNode{}, err
×
4105
                }
×
4106
                addresses = append(addresses, address)
10,599✔
4107
        }
4108
        node.Addresses = addresses
8,366✔
4109

8,366✔
4110
        node.AuthSigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
8,366✔
4111
        if err != nil {
8,366✔
4112
                return models.LightningNode{}, err
×
4113
        }
×
4114

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

4127
        return node, nil
8,366✔
4128
}
4129

4130
func putChanEdgeInfo(edgeIndex kvdb.RwBucket,
4131
        edgeInfo *models.ChannelEdgeInfo, chanID [8]byte) error {
1,491✔
4132

1,491✔
4133
        var b bytes.Buffer
1,491✔
4134

1,491✔
4135
        if _, err := b.Write(edgeInfo.NodeKey1Bytes[:]); err != nil {
1,491✔
4136
                return err
×
4137
        }
×
4138
        if _, err := b.Write(edgeInfo.NodeKey2Bytes[:]); err != nil {
1,491✔
4139
                return err
×
4140
        }
×
4141
        if _, err := b.Write(edgeInfo.BitcoinKey1Bytes[:]); err != nil {
1,491✔
4142
                return err
×
4143
        }
×
4144
        if _, err := b.Write(edgeInfo.BitcoinKey2Bytes[:]); err != nil {
1,491✔
4145
                return err
×
4146
        }
×
4147

4148
        if err := wire.WriteVarBytes(&b, 0, edgeInfo.Features); err != nil {
1,491✔
4149
                return err
×
4150
        }
×
4151

4152
        authProof := edgeInfo.AuthProof
1,491✔
4153
        var nodeSig1, nodeSig2, bitcoinSig1, bitcoinSig2 []byte
1,491✔
4154
        if authProof != nil {
2,898✔
4155
                nodeSig1 = authProof.NodeSig1Bytes
1,407✔
4156
                nodeSig2 = authProof.NodeSig2Bytes
1,407✔
4157
                bitcoinSig1 = authProof.BitcoinSig1Bytes
1,407✔
4158
                bitcoinSig2 = authProof.BitcoinSig2Bytes
1,407✔
4159
        }
1,407✔
4160

4161
        if err := wire.WriteVarBytes(&b, 0, nodeSig1); err != nil {
1,491✔
4162
                return err
×
4163
        }
×
4164
        if err := wire.WriteVarBytes(&b, 0, nodeSig2); err != nil {
1,491✔
4165
                return err
×
4166
        }
×
4167
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig1); err != nil {
1,491✔
4168
                return err
×
4169
        }
×
4170
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig2); err != nil {
1,491✔
4171
                return err
×
4172
        }
×
4173

4174
        if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
1,491✔
4175
                return err
×
4176
        }
×
4177
        err := binary.Write(&b, byteOrder, uint64(edgeInfo.Capacity))
1,491✔
4178
        if err != nil {
1,491✔
4179
                return err
×
4180
        }
×
4181
        if _, err := b.Write(chanID[:]); err != nil {
1,491✔
4182
                return err
×
4183
        }
×
4184
        if _, err := b.Write(edgeInfo.ChainHash[:]); err != nil {
1,491✔
4185
                return err
×
4186
        }
×
4187

4188
        if len(edgeInfo.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
1,491✔
4189
                return ErrTooManyExtraOpaqueBytes(len(edgeInfo.ExtraOpaqueData))
×
4190
        }
×
4191
        err = wire.WriteVarBytes(&b, 0, edgeInfo.ExtraOpaqueData)
1,491✔
4192
        if err != nil {
1,491✔
4193
                return err
×
4194
        }
×
4195

4196
        return edgeIndex.Put(chanID[:], b.Bytes())
1,491✔
4197
}
4198

4199
func fetchChanEdgeInfo(edgeIndex kvdb.RBucket,
4200
        chanID []byte) (models.ChannelEdgeInfo, error) {
6,613✔
4201

6,613✔
4202
        edgeInfoBytes := edgeIndex.Get(chanID)
6,613✔
4203
        if edgeInfoBytes == nil {
6,684✔
4204
                return models.ChannelEdgeInfo{}, ErrEdgeNotFound
71✔
4205
        }
71✔
4206

4207
        edgeInfoReader := bytes.NewReader(edgeInfoBytes)
6,545✔
4208

6,545✔
4209
        return deserializeChanEdgeInfo(edgeInfoReader)
6,545✔
4210
}
4211

4212
func deserializeChanEdgeInfo(r io.Reader) (models.ChannelEdgeInfo, error) {
7,084✔
4213
        var (
7,084✔
4214
                err      error
7,084✔
4215
                edgeInfo models.ChannelEdgeInfo
7,084✔
4216
        )
7,084✔
4217

7,084✔
4218
        if _, err := io.ReadFull(r, edgeInfo.NodeKey1Bytes[:]); err != nil {
7,084✔
4219
                return models.ChannelEdgeInfo{}, err
×
4220
        }
×
4221
        if _, err := io.ReadFull(r, edgeInfo.NodeKey2Bytes[:]); err != nil {
7,084✔
4222
                return models.ChannelEdgeInfo{}, err
×
4223
        }
×
4224
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey1Bytes[:]); err != nil {
7,084✔
4225
                return models.ChannelEdgeInfo{}, err
×
4226
        }
×
4227
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey2Bytes[:]); err != nil {
7,084✔
4228
                return models.ChannelEdgeInfo{}, err
×
4229
        }
×
4230

4231
        edgeInfo.Features, err = wire.ReadVarBytes(r, 0, 900, "features")
7,084✔
4232
        if err != nil {
7,084✔
4233
                return models.ChannelEdgeInfo{}, err
×
4234
        }
×
4235

4236
        proof := &models.ChannelAuthProof{}
7,084✔
4237

7,084✔
4238
        proof.NodeSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
7,084✔
4239
        if err != nil {
7,084✔
4240
                return models.ChannelEdgeInfo{}, err
×
4241
        }
×
4242
        proof.NodeSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
7,084✔
4243
        if err != nil {
7,084✔
4244
                return models.ChannelEdgeInfo{}, err
×
4245
        }
×
4246
        proof.BitcoinSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
7,084✔
4247
        if err != nil {
7,084✔
4248
                return models.ChannelEdgeInfo{}, err
×
4249
        }
×
4250
        proof.BitcoinSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
7,084✔
4251
        if err != nil {
7,084✔
4252
                return models.ChannelEdgeInfo{}, err
×
4253
        }
×
4254

4255
        if !proof.IsEmpty() {
11,065✔
4256
                edgeInfo.AuthProof = proof
3,981✔
4257
        }
3,981✔
4258

4259
        edgeInfo.ChannelPoint = wire.OutPoint{}
7,084✔
4260
        if err := ReadOutpoint(r, &edgeInfo.ChannelPoint); err != nil {
7,084✔
4261
                return models.ChannelEdgeInfo{}, err
×
4262
        }
×
4263
        if err := binary.Read(r, byteOrder, &edgeInfo.Capacity); err != nil {
7,084✔
4264
                return models.ChannelEdgeInfo{}, err
×
4265
        }
×
4266
        if err := binary.Read(r, byteOrder, &edgeInfo.ChannelID); err != nil {
7,084✔
4267
                return models.ChannelEdgeInfo{}, err
×
4268
        }
×
4269

4270
        if _, err := io.ReadFull(r, edgeInfo.ChainHash[:]); err != nil {
7,084✔
4271
                return models.ChannelEdgeInfo{}, err
×
4272
        }
×
4273

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

4286
        return edgeInfo, nil
7,084✔
4287
}
4288

4289
func putChanEdgePolicy(edges kvdb.RwBucket, edge *models.ChannelEdgePolicy,
4290
        from, to []byte) error {
2,665✔
4291

2,665✔
4292
        var edgeKey [33 + 8]byte
2,665✔
4293
        copy(edgeKey[:], from)
2,665✔
4294
        byteOrder.PutUint64(edgeKey[33:], edge.ChannelID)
2,665✔
4295

2,665✔
4296
        var b bytes.Buffer
2,665✔
4297
        if err := serializeChanEdgePolicy(&b, edge, to); err != nil {
2,665✔
4298
                return err
×
4299
        }
×
4300

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

2,665✔
4308
        updateIndex, err := edges.CreateBucketIfNotExists(edgeUpdateIndexBucket)
2,665✔
4309
        if err != nil {
2,665✔
4310
                return err
×
4311
        }
×
4312

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

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

×
4335
                        return err
×
4336
                }
×
4337

4338
                oldUpdateTime := uint64(oldEdgePolicy.LastUpdate.Unix())
27✔
4339

27✔
4340
                var oldIndexKey [8 + 8]byte
27✔
4341
                byteOrder.PutUint64(oldIndexKey[:8], oldUpdateTime)
27✔
4342
                byteOrder.PutUint64(oldIndexKey[8:], edge.ChannelID)
27✔
4343

27✔
4344
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
27✔
4345
                        return err
×
4346
                }
×
4347
        }
4348

4349
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
2,665✔
4350
                return err
×
4351
        }
×
4352

4353
        err = updateEdgePolicyDisabledIndex(
2,665✔
4354
                edges, edge.ChannelID,
2,665✔
4355
                edge.ChannelFlags&lnwire.ChanUpdateDirection > 0,
2,665✔
4356
                edge.IsDisabled(),
2,665✔
4357
        )
2,665✔
4358
        if err != nil {
2,665✔
4359
                return err
×
4360
        }
×
4361

4362
        return edges.Put(edgeKey[:], b.Bytes())
2,665✔
4363
}
4364

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

2,941✔
4377
        var disabledEdgeKey [8 + 1]byte
2,941✔
4378
        byteOrder.PutUint64(disabledEdgeKey[0:], chanID)
2,941✔
4379
        if direction {
4,410✔
4380
                disabledEdgeKey[8] = 1
1,469✔
4381
        }
1,469✔
4382

4383
        disabledEdgePolicyIndex, err := edges.CreateBucketIfNotExists(
2,941✔
4384
                disabledEdgePolicyBucket,
2,941✔
4385
        )
2,941✔
4386
        if err != nil {
2,941✔
4387
                return err
×
4388
        }
×
4389

4390
        if disabled {
2,970✔
4391
                return disabledEdgePolicyIndex.Put(disabledEdgeKey[:], []byte{})
29✔
4392
        }
29✔
4393

4394
        return disabledEdgePolicyIndex.Delete(disabledEdgeKey[:])
2,915✔
4395
}
4396

4397
// putChanEdgePolicyUnknown marks the edge policy as unknown
4398
// in the edges bucket.
4399
func putChanEdgePolicyUnknown(edges kvdb.RwBucket, channelID uint64,
4400
        from []byte) error {
2,975✔
4401

2,975✔
4402
        var edgeKey [33 + 8]byte
2,975✔
4403
        copy(edgeKey[:], from)
2,975✔
4404
        byteOrder.PutUint64(edgeKey[33:], channelID)
2,975✔
4405

2,975✔
4406
        if edges.Get(edgeKey[:]) != nil {
2,975✔
4407
                return fmt.Errorf("cannot write unknown policy for channel %v "+
×
4408
                        " when there is already a policy present", channelID)
×
4409
        }
×
4410

4411
        return edges.Put(edgeKey[:], unknownPolicy)
2,975✔
4412
}
4413

4414
func fetchChanEdgePolicy(edges kvdb.RBucket, chanID []byte,
4415
        nodePub []byte) (*models.ChannelEdgePolicy, error) {
13,097✔
4416

13,097✔
4417
        var edgeKey [33 + 8]byte
13,097✔
4418
        copy(edgeKey[:], nodePub)
13,097✔
4419
        copy(edgeKey[33:], chanID)
13,097✔
4420

13,097✔
4421
        edgeBytes := edges.Get(edgeKey[:])
13,097✔
4422
        if edgeBytes == nil {
13,097✔
4423
                return nil, ErrEdgeNotFound
×
4424
        }
×
4425

4426
        // No need to deserialize unknown policy.
4427
        if bytes.Equal(edgeBytes, unknownPolicy) {
14,475✔
4428
                return nil, nil
1,378✔
4429
        }
1,378✔
4430

4431
        edgeReader := bytes.NewReader(edgeBytes)
11,722✔
4432

11,722✔
4433
        ep, err := deserializeChanEdgePolicy(edgeReader)
11,722✔
4434
        switch {
11,722✔
4435
        // If the db policy was missing an expected optional field, we return
4436
        // nil as if the policy was unknown.
4437
        case errors.Is(err, ErrEdgePolicyOptionalFieldNotFound):
2✔
4438
                return nil, nil
2✔
4439

4440
        case err != nil:
×
4441
                return nil, err
×
4442
        }
4443

4444
        return ep, nil
11,720✔
4445
}
4446

4447
func fetchChanEdgePolicies(edgeIndex kvdb.RBucket, edges kvdb.RBucket,
4448
        chanID []byte) (*models.ChannelEdgePolicy, *models.ChannelEdgePolicy,
4449
        error) {
2,898✔
4450

2,898✔
4451
        edgeInfo := edgeIndex.Get(chanID)
2,898✔
4452
        if edgeInfo == nil {
2,898✔
4453
                return nil, nil, fmt.Errorf("%w: chanID=%x", ErrEdgeNotFound,
×
4454
                        chanID)
×
4455
        }
×
4456

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

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

4476
        return edge1, edge2, nil
2,898✔
4477
}
4478

4479
func serializeChanEdgePolicy(w io.Writer, edge *models.ChannelEdgePolicy,
4480
        to []byte) error {
2,667✔
4481

2,667✔
4482
        err := wire.WriteVarBytes(w, 0, edge.SigBytes)
2,667✔
4483
        if err != nil {
2,667✔
4484
                return err
×
4485
        }
×
4486

4487
        if err := binary.Write(w, byteOrder, edge.ChannelID); err != nil {
2,667✔
4488
                return err
×
4489
        }
×
4490

4491
        var scratch [8]byte
2,667✔
4492
        updateUnix := uint64(edge.LastUpdate.Unix())
2,667✔
4493
        byteOrder.PutUint64(scratch[:], updateUnix)
2,667✔
4494
        if _, err := w.Write(scratch[:]); err != nil {
2,667✔
4495
                return err
×
4496
        }
×
4497

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

4521
        if _, err := w.Write(to); err != nil {
2,667✔
4522
                return err
×
4523
        }
×
4524

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

4537
        if len(edge.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
2,667✔
4538
                return ErrTooManyExtraOpaqueBytes(len(edge.ExtraOpaqueData))
×
4539
        }
×
4540
        if _, err := opaqueBuf.Write(edge.ExtraOpaqueData); err != nil {
2,667✔
4541
                return err
×
4542
        }
×
4543

4544
        if err := wire.WriteVarBytes(w, 0, opaqueBuf.Bytes()); err != nil {
2,667✔
4545
                return err
×
4546
        }
×
4547

4548
        return nil
2,667✔
4549
}
4550

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

×
4558
                return nil, deserializeErr
×
4559
        }
×
4560

4561
        return edge, deserializeErr
11,747✔
4562
}
4563

4564
func deserializeChanEdgePolicyRaw(r io.Reader) (*models.ChannelEdgePolicy,
4565
        error) {
12,754✔
4566

12,754✔
4567
        edge := &models.ChannelEdgePolicy{}
12,754✔
4568

12,754✔
4569
        var err error
12,754✔
4570
        edge.SigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
12,754✔
4571
        if err != nil {
12,754✔
4572
                return nil, err
×
4573
        }
×
4574

4575
        if err := binary.Read(r, byteOrder, &edge.ChannelID); err != nil {
12,754✔
4576
                return nil, err
×
4577
        }
×
4578

4579
        var scratch [8]byte
12,754✔
4580
        if _, err := r.Read(scratch[:]); err != nil {
12,754✔
4581
                return nil, err
×
4582
        }
×
4583
        unix := int64(byteOrder.Uint64(scratch[:]))
12,754✔
4584
        edge.LastUpdate = time.Unix(unix, 0)
12,754✔
4585

12,754✔
4586
        if err := binary.Read(r, byteOrder, &edge.MessageFlags); err != nil {
12,754✔
4587
                return nil, err
×
4588
        }
×
4589
        if err := binary.Read(r, byteOrder, &edge.ChannelFlags); err != nil {
12,754✔
4590
                return nil, err
×
4591
        }
×
4592
        if err := binary.Read(r, byteOrder, &edge.TimeLockDelta); err != nil {
12,754✔
4593
                return nil, err
×
4594
        }
×
4595

4596
        var n uint64
12,754✔
4597
        if err := binary.Read(r, byteOrder, &n); err != nil {
12,754✔
4598
                return nil, err
×
4599
        }
×
4600
        edge.MinHTLC = lnwire.MilliSatoshi(n)
12,754✔
4601

12,754✔
4602
        if err := binary.Read(r, byteOrder, &n); err != nil {
12,754✔
4603
                return nil, err
×
4604
        }
×
4605
        edge.FeeBaseMSat = lnwire.MilliSatoshi(n)
12,754✔
4606

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

12,754✔
4612
        if _, err := r.Read(edge.ToNode[:]); err != nil {
12,754✔
4613
                return nil, err
×
4614
        }
×
4615

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

4628
        // See if optional fields are present.
4629
        if edge.MessageFlags.HasMaxHtlc() {
24,454✔
4630
                // The max_htlc field should be at the beginning of the opaque
11,700✔
4631
                // bytes.
11,700✔
4632
                opq := edge.ExtraOpaqueData
11,700✔
4633

11,700✔
4634
                // If the max_htlc field is not present, it might be old data
11,700✔
4635
                // stored before this field was validated. We'll return the
11,700✔
4636
                // edge along with an error.
11,700✔
4637
                if len(opq) < 8 {
11,704✔
4638
                        return edge, ErrEdgePolicyOptionalFieldNotFound
4✔
4639
                }
4✔
4640

4641
                maxHtlc := byteOrder.Uint64(opq[:8])
11,696✔
4642
                edge.MaxHTLC = lnwire.MilliSatoshi(maxHtlc)
11,696✔
4643

11,696✔
4644
                // Exclude the parsed field from the rest of the opaque data.
11,696✔
4645
                edge.ExtraOpaqueData = opq[8:]
11,696✔
4646
        }
4647

4648
        return edge, nil
12,750✔
4649
}
4650

4651
// chanGraphNodeTx is an implementation of the NodeRTx interface backed by the
4652
// KVStore and a kvdb.RTx.
4653
type chanGraphNodeTx struct {
4654
        tx   kvdb.RTx
4655
        db   *KVStore
4656
        node *models.LightningNode
4657
}
4658

4659
// A compile-time constraint to ensure chanGraphNodeTx implements the NodeRTx
4660
// interface.
4661
var _ NodeRTx = (*chanGraphNodeTx)(nil)
4662

4663
func newChanGraphNodeTx(tx kvdb.RTx, db *KVStore,
4664
        node *models.LightningNode) *chanGraphNodeTx {
4,105✔
4665

4,105✔
4666
        return &chanGraphNodeTx{
4,105✔
4667
                tx:   tx,
4,105✔
4668
                db:   db,
4,105✔
4669
                node: node,
4,105✔
4670
        }
4,105✔
4671
}
4,105✔
4672

4673
// Node returns the raw information of the node.
4674
//
4675
// NOTE: This is a part of the NodeRTx interface.
4676
func (c *chanGraphNodeTx) Node() *models.LightningNode {
5,022✔
4677
        return c.node
5,022✔
4678
}
5,022✔
4679

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

4691
        return newChanGraphNodeTx(c.tx, c.db, node), nil
2,944✔
4692
}
4693

4694
// ForEachChannel can be used to iterate over the node's channels under
4695
// the same transaction used to fetch the node.
4696
//
4697
// NOTE: This is a part of the NodeRTx interface.
4698
func (c *chanGraphNodeTx) ForEachChannel(f func(*models.ChannelEdgeInfo,
4699
        *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
965✔
4700

965✔
4701
        return c.db.forEachNodeChannelTx(c.tx, c.node.PubKeyBytes,
965✔
4702
                func(_ kvdb.RTx, info *models.ChannelEdgeInfo, policy1,
965✔
4703
                        policy2 *models.ChannelEdgePolicy) error {
3,909✔
4704

2,944✔
4705
                        return f(info, policy1, policy2)
2,944✔
4706
                },
2,944✔
4707
        )
4708
}
4709

4710
// MakeTestGraph creates a new instance of the KVStore for testing
4711
// purposes.
4712
func MakeTestGraph(t testing.TB,
4713
        modifiers ...KVStoreOptionModifier) *ChannelGraph {
37✔
4714

37✔
4715
        opts := DefaultOptions()
37✔
4716
        for _, modifier := range modifiers {
37✔
4717
                modifier(opts)
×
4718
        }
×
4719

4720
        // Next, create KVStore for the first time.
4721
        backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
37✔
4722
        t.Cleanup(backendCleanup)
37✔
4723
        require.NoError(t, err)
37✔
4724
        t.Cleanup(func() {
74✔
4725
                require.NoError(t, backend.Close())
37✔
4726
        })
37✔
4727

4728
        graphStore, err := NewKVStore(backend, modifiers...)
37✔
4729
        require.NoError(t, err)
37✔
4730

37✔
4731
        graph, err := NewChannelGraph(graphStore)
37✔
4732
        require.NoError(t, err)
37✔
4733
        require.NoError(t, graph.Start())
37✔
4734
        t.Cleanup(func() {
74✔
4735
                require.NoError(t, graph.Stop())
37✔
4736
        })
37✔
4737

4738
        return graph
37✔
4739
}
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