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

lightningnetwork / lnd / 15561477203

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

Pull #9356

github

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

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

28366 existing lines in 455 files now uncovered.

97715 of 167461 relevant lines covered (58.35%)

1.81 hits per line

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

68.24
/graph/db/kv_store.go
1
package graphdb
2

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

196
        chanScheduler batch.Scheduler[kvdb.RwTx]
197
        nodeScheduler batch.Scheduler[kvdb.RwTx]
198
}
199

200
// A compile-time assertion to ensure that the KVStore struct implements the
201
// V1Store interface.
202
var _ V1Store = (*KVStore)(nil)
203

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

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

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

220
        g := &KVStore{
3✔
221
                db:          db,
3✔
222
                rejectCache: newRejectCache(opts.RejectCacheSize),
3✔
223
                chanCache:   newChannelCache(opts.ChannelCacheSize),
3✔
224
        }
3✔
225
        g.chanScheduler = batch.NewTimeScheduler(
3✔
226
                batch.NewBoltBackend[kvdb.RwTx](db), &g.cacheMu,
3✔
227
                opts.BatchCommitInterval,
3✔
228
        )
3✔
229
        g.nodeScheduler = batch.NewTimeScheduler(
3✔
230
                batch.NewBoltBackend[kvdb.RwTx](db), nil,
3✔
231
                opts.BatchCommitInterval,
3✔
232
        )
3✔
233

3✔
234
        return g, nil
3✔
235
}
236

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

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

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

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

3✔
259
                        return nil
3✔
260
                }
3✔
261

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

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

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

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

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

287
                case err != nil:
×
288
                        return err
×
289
                }
290

291
                channelMap[key] = edge
3✔
292

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

299
        return channelMap, nil
3✔
300
}
301

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

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

321
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
322
                _, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
3✔
323
                if err != nil {
3✔
324
                        return err
×
325
                }
×
326
                _, err = nodes.CreateBucketIfNotExists(nodeUpdateIndexBucket)
3✔
327
                if err != nil {
3✔
328
                        return err
×
329
                }
×
330

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

349
                graphMeta := tx.ReadWriteBucket(graphMetaBucket)
3✔
350
                _, err = graphMeta.CreateBucketIfNotExists(pruneLogBucket)
3✔
351

3✔
352
                return err
3✔
353
        }, func() {})
3✔
354
        if err != nil {
3✔
355
                return fmt.Errorf("unable to create new channel graph: %w", err)
×
356
        }
×
357

358
        return nil
3✔
359
}
360

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

3✔
369
        pubKey, err := route.NewVertexFromBytes(nodePub.SerializeCompressed())
3✔
370
        if err != nil {
3✔
371
                return false, nil, err
×
372
        }
×
373

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

380
        case errors.Is(err, ErrGraphNodeNotFound):
3✔
381
                return false, nil, nil
3✔
382
        }
383

384
        return true, node.Addresses, nil
3✔
385
}
386

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

3✔
399
        return c.db.View(func(tx kvdb.RTx) error {
6✔
400
                edges := tx.ReadBucket(edgeBucket)
3✔
401
                if edges == nil {
3✔
402
                        return ErrGraphNoEdgesFound
×
403
                }
×
404

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

412
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
413
                if edgeIndex == nil {
3✔
414
                        return ErrGraphNoEdgesFound
×
415
                }
×
416

417
                // Load edge index, recombine each channel with the policies
418
                // loaded above and invoke the callback.
419
                return kvdb.ForAll(
3✔
420
                        edgeIndex, func(k, edgeInfoBytes []byte) error {
6✔
421
                                var chanID [8]byte
3✔
422
                                copy(chanID[:], k)
3✔
423

3✔
424
                                edgeInfoReader := bytes.NewReader(edgeInfoBytes)
3✔
425
                                info, err := deserializeChanEdgeInfo(
3✔
426
                                        edgeInfoReader,
3✔
427
                                )
3✔
428
                                if err != nil {
3✔
429
                                        return err
×
430
                                }
×
431

432
                                policy1 := channelMap[channelMapKey{
3✔
433
                                        nodeKey: info.NodeKey1Bytes,
3✔
434
                                        chanID:  chanID,
3✔
435
                                }]
3✔
436

3✔
437
                                policy2 := channelMap[channelMapKey{
3✔
438
                                        nodeKey: info.NodeKey2Bytes,
3✔
439
                                        chanID:  chanID,
3✔
440
                                }]
3✔
441

3✔
442
                                return cb(&info, policy1, policy2)
3✔
443
                        },
444
                )
445
        }, func() {})
3✔
446
}
447

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

3✔
458
        // Fallback that uses the database.
3✔
459
        toNodeCallback := func() route.Vertex {
6✔
460
                return node
3✔
461
        }
3✔
462
        toNodeFeatures, err := c.fetchNodeFeatures(tx, node)
3✔
463
        if err != nil {
3✔
464
                return err
×
465
        }
×
466

467
        dbCallback := func(tx kvdb.RTx, e *models.ChannelEdgeInfo, p1,
3✔
468
                p2 *models.ChannelEdgePolicy) error {
6✔
469

3✔
470
                var cachedInPolicy *models.CachedEdgePolicy
3✔
471
                if p2 != nil {
6✔
472
                        cachedInPolicy = models.NewCachedPolicy(p2)
3✔
473
                        cachedInPolicy.ToNodePubKey = toNodeCallback
3✔
474
                        cachedInPolicy.ToNodeFeatures = toNodeFeatures
3✔
475
                }
3✔
476

477
                var inboundFee lnwire.Fee
3✔
478
                if p1 != nil {
6✔
479
                        // Extract inbound fee. If there is a decoding error,
3✔
480
                        // skip this edge.
3✔
481
                        _, err := p1.ExtraOpaqueData.ExtractRecords(&inboundFee)
3✔
482
                        if err != nil {
3✔
UNCOV
483
                                return nil
×
UNCOV
484
                        }
×
485
                }
486

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

3✔
497
                if node == e.NodeKey2Bytes {
6✔
498
                        directedChannel.OtherNode = e.NodeKey1Bytes
3✔
499
                }
3✔
500

501
                return cb(directedChannel)
3✔
502
        }
503

504
        return nodeTraversal(tx, node[:], c.db, dbCallback)
3✔
505
}
506

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

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

520
        // If we couldn't find a node announcement, populate a blank feature
521
        // vector.
UNCOV
522
        case errors.Is(err, ErrGraphNodeNotFound):
×
UNCOV
523
                return lnwire.EmptyFeatureVector(), nil
×
524

525
        // Otherwise, bubble the error up.
526
        default:
×
527
                return nil, err
×
528
        }
529
}
530

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

3✔
542
        return c.forEachNodeDirectedChannel(nil, nodePub, cb)
3✔
543
}
3✔
544

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

3✔
552
        return c.fetchNodeFeatures(nil, nodePub)
3✔
553
}
3✔
554

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

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

×
UNCOV
569
                channels := make(map[uint64]*DirectedChannel)
×
UNCOV
570

×
UNCOV
571
                err := c.forEachNodeChannelTx(tx, node.PubKeyBytes,
×
UNCOV
572
                        func(tx kvdb.RTx, e *models.ChannelEdgeInfo,
×
UNCOV
573
                                p1 *models.ChannelEdgePolicy,
×
UNCOV
574
                                p2 *models.ChannelEdgePolicy) error {
×
UNCOV
575

×
UNCOV
576
                                toNodeCallback := func() route.Vertex {
×
577
                                        return node.PubKeyBytes
×
578
                                }
×
UNCOV
579
                                toNodeFeatures, err := c.fetchNodeFeatures(
×
UNCOV
580
                                        tx, node.PubKeyBytes,
×
UNCOV
581
                                )
×
UNCOV
582
                                if err != nil {
×
583
                                        return err
×
584
                                }
×
585

UNCOV
586
                                var cachedInPolicy *models.CachedEdgePolicy
×
UNCOV
587
                                if p2 != nil {
×
UNCOV
588
                                        cachedInPolicy =
×
UNCOV
589
                                                models.NewCachedPolicy(p2)
×
UNCOV
590
                                        cachedInPolicy.ToNodePubKey =
×
UNCOV
591
                                                toNodeCallback
×
UNCOV
592
                                        cachedInPolicy.ToNodeFeatures =
×
UNCOV
593
                                                toNodeFeatures
×
UNCOV
594
                                }
×
595

UNCOV
596
                                directedChannel := &DirectedChannel{
×
UNCOV
597
                                        ChannelID: e.ChannelID,
×
UNCOV
598
                                        IsNode1: node.PubKeyBytes ==
×
UNCOV
599
                                                e.NodeKey1Bytes,
×
UNCOV
600
                                        OtherNode:    e.NodeKey2Bytes,
×
UNCOV
601
                                        Capacity:     e.Capacity,
×
UNCOV
602
                                        OutPolicySet: p1 != nil,
×
UNCOV
603
                                        InPolicy:     cachedInPolicy,
×
UNCOV
604
                                }
×
UNCOV
605

×
UNCOV
606
                                if node.PubKeyBytes == e.NodeKey2Bytes {
×
UNCOV
607
                                        directedChannel.OtherNode =
×
UNCOV
608
                                                e.NodeKey1Bytes
×
UNCOV
609
                                }
×
610

UNCOV
611
                                channels[e.ChannelID] = directedChannel
×
UNCOV
612

×
UNCOV
613
                                return nil
×
614
                        })
UNCOV
615
                if err != nil {
×
616
                        return err
×
617
                }
×
618

UNCOV
619
                return cb(node.PubKeyBytes, channels)
×
620
        })
621
}
622

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

×
UNCOV
630
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
×
UNCOV
631
                edges := tx.ReadBucket(edgeBucket)
×
UNCOV
632
                if edges == nil {
×
633
                        return ErrGraphNoEdgesFound
×
634
                }
×
635

UNCOV
636
                disabledEdgePolicyIndex := edges.NestedReadBucket(
×
UNCOV
637
                        disabledEdgePolicyBucket,
×
UNCOV
638
                )
×
UNCOV
639
                if disabledEdgePolicyIndex == nil {
×
UNCOV
640
                        return nil
×
UNCOV
641
                }
×
642

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

×
UNCOV
656
                                        return nil
×
UNCOV
657
                                }
×
658

UNCOV
659
                                chanEdgeFound[chanID] = struct{}{}
×
UNCOV
660

×
UNCOV
661
                                return nil
×
662
                        },
663
                )
UNCOV
664
        }, func() {
×
UNCOV
665
                disabledChanIDs = nil
×
UNCOV
666
                chanEdgeFound = make(map[uint64]struct{})
×
UNCOV
667
        })
×
UNCOV
668
        if err != nil {
×
669
                return nil, err
×
670
        }
×
671

UNCOV
672
        return disabledChanIDs, nil
×
673
}
674

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

3✔
685
                return cb(newChanGraphNodeTx(tx, c, node))
3✔
686
        })
3✔
687
}
688

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

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

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

715
                        nodeReader := bytes.NewReader(nodeBytes)
3✔
716
                        node, err := deserializeLightningNode(nodeReader)
3✔
717
                        if err != nil {
3✔
718
                                return err
×
719
                        }
×
720

721
                        // Execute the callback, the transaction will abort if
722
                        // this returns an error.
723
                        return cb(tx, &node)
3✔
724
                })
725
        }
726

727
        return kvdb.View(c.db, traversal, func() {})
6✔
728
}
729

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

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

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

753
                        nodeReader := bytes.NewReader(nodeBytes)
3✔
754
                        node, features, err := deserializeLightningNodeCacheable( //nolint:ll
3✔
755
                                nodeReader,
3✔
756
                        )
3✔
757
                        if err != nil {
3✔
758
                                return err
×
759
                        }
×
760

761
                        // Execute the callback, the transaction will abort if
762
                        // this returns an error.
763
                        return cb(node, features)
3✔
764
                })
765
        }
766

767
        return kvdb.View(c.db, traversal, func() {})
6✔
768
}
769

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

784
                node, err := c.sourceNode(nodes)
3✔
785
                if err != nil {
3✔
UNCOV
786
                        return err
×
UNCOV
787
                }
×
788
                source = node
3✔
789

3✔
790
                return nil
3✔
791
        }, func() {
3✔
792
                source = nil
3✔
793
        })
3✔
794
        if err != nil {
3✔
UNCOV
795
                return nil, err
×
UNCOV
796
        }
×
797

798
        return source, nil
3✔
799
}
800

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

3✔
808
        selfPub := nodes.Get(sourceKey)
3✔
809
        if selfPub == nil {
3✔
UNCOV
810
                return nil, ErrSourceNodeNotSet
×
UNCOV
811
        }
×
812

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

820
        return &node, nil
3✔
821
}
822

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

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

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

843
                // Finally, we commit the information of the lightning node
844
                // itself.
845
                return addLightningNode(tx, node)
3✔
846
        }, func() {})
3✔
847
}
848

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

3✔
860
        ctx := context.TODO()
3✔
861

3✔
862
        r := &batch.Request[kvdb.RwTx]{
3✔
863
                Opts: batch.NewSchedulerOptions(opts...),
3✔
864
                Do: func(tx kvdb.RwTx) error {
6✔
865
                        return addLightningNode(tx, node)
3✔
866
                },
3✔
867
        }
868

869
        return c.nodeScheduler.Execute(ctx, r)
3✔
870
}
871

872
func addLightningNode(tx kvdb.RwTx, node *models.LightningNode) error {
3✔
873
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
3✔
874
        if err != nil {
3✔
875
                return err
×
876
        }
×
877

878
        aliases, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
3✔
879
        if err != nil {
3✔
880
                return err
×
881
        }
×
882

883
        updateIndex, err := nodes.CreateBucketIfNotExists(
3✔
884
                nodeUpdateIndexBucket,
3✔
885
        )
3✔
886
        if err != nil {
3✔
887
                return err
×
888
        }
×
889

890
        return putLightningNode(nodes, aliases, updateIndex, node)
3✔
891
}
892

893
// LookupAlias attempts to return the alias as advertised by the target node.
894
// TODO(roasbeef): currently assumes that aliases are unique...
895
func (c *KVStore) LookupAlias(pub *btcec.PublicKey) (string, error) {
3✔
896
        var alias string
3✔
897

3✔
898
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
899
                nodes := tx.ReadBucket(nodeBucket)
3✔
900
                if nodes == nil {
3✔
901
                        return ErrGraphNodesNotFound
×
902
                }
×
903

904
                aliases := nodes.NestedReadBucket(aliasIndexBucket)
3✔
905
                if aliases == nil {
3✔
906
                        return ErrGraphNodesNotFound
×
907
                }
×
908

909
                nodePub := pub.SerializeCompressed()
3✔
910
                a := aliases.Get(nodePub)
3✔
911
                if a == nil {
3✔
UNCOV
912
                        return ErrNodeAliasNotFound
×
UNCOV
913
                }
×
914

915
                // TODO(roasbeef): should actually be using the utf-8
916
                // package...
917
                alias = string(a)
3✔
918

3✔
919
                return nil
3✔
920
        }, func() {
3✔
921
                alias = ""
3✔
922
        })
3✔
923
        if err != nil {
3✔
UNCOV
924
                return "", err
×
UNCOV
925
        }
×
926

927
        return alias, nil
3✔
928
}
929

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

UNCOV
940
                return c.deleteLightningNode(nodes, nodePub[:])
×
UNCOV
941
        }, func() {})
×
942
}
943

944
// deleteLightningNode uses an existing database transaction to remove a
945
// vertex/node from the database according to the node's public key.
946
func (c *KVStore) deleteLightningNode(nodes kvdb.RwBucket,
947
        compressedPubKey []byte) error {
3✔
948

3✔
949
        aliases := nodes.NestedReadWriteBucket(aliasIndexBucket)
3✔
950
        if aliases == nil {
3✔
951
                return ErrGraphNodesNotFound
×
952
        }
×
953

954
        if err := aliases.Delete(compressedPubKey); err != nil {
3✔
955
                return err
×
956
        }
×
957

958
        // Before we delete the node, we'll fetch its current state so we can
959
        // determine when its last update was to clear out the node update
960
        // index.
961
        node, err := fetchLightningNode(nodes, compressedPubKey)
3✔
962
        if err != nil {
3✔
UNCOV
963
                return err
×
UNCOV
964
        }
×
965

966
        if err := nodes.Delete(compressedPubKey); err != nil {
3✔
967
                return err
×
968
        }
×
969

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

978
        // In order to delete the entry, we'll need to reconstruct the key for
979
        // its last update.
980
        updateUnix := uint64(node.LastUpdate.Unix())
3✔
981
        var indexKey [8 + 33]byte
3✔
982
        byteOrder.PutUint64(indexKey[:8], updateUnix)
3✔
983
        copy(indexKey[8:], compressedPubKey)
3✔
984

3✔
985
        return nodeUpdateIndex.Delete(indexKey[:])
3✔
986
}
987

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

3✔
997
        ctx := context.TODO()
3✔
998

3✔
999
        var alreadyExists bool
3✔
1000
        r := &batch.Request[kvdb.RwTx]{
3✔
1001
                Opts: batch.NewSchedulerOptions(opts...),
3✔
1002
                Reset: func() {
6✔
1003
                        alreadyExists = false
3✔
1004
                },
3✔
1005
                Do: func(tx kvdb.RwTx) error {
3✔
1006
                        err := c.addChannelEdge(tx, edge)
3✔
1007

3✔
1008
                        // Silence ErrEdgeAlreadyExist so that the batch can
3✔
1009
                        // succeed, but propagate the error via local state.
3✔
1010
                        if errors.Is(err, ErrEdgeAlreadyExist) {
3✔
UNCOV
1011
                                alreadyExists = true
×
UNCOV
1012
                                return nil
×
UNCOV
1013
                        }
×
1014

1015
                        return err
3✔
1016
                },
1017
                OnCommit: func(err error) error {
3✔
1018
                        switch {
3✔
1019
                        case err != nil:
×
1020
                                return err
×
UNCOV
1021
                        case alreadyExists:
×
UNCOV
1022
                                return ErrEdgeAlreadyExist
×
1023
                        default:
3✔
1024
                                c.rejectCache.remove(edge.ChannelID)
3✔
1025
                                c.chanCache.remove(edge.ChannelID)
3✔
1026
                                return nil
3✔
1027
                        }
1028
                },
1029
        }
1030

1031
        return c.chanScheduler.Execute(ctx, r)
3✔
1032
}
1033

1034
// addChannelEdge is the private form of AddChannelEdge that allows callers to
1035
// utilize an existing db transaction.
1036
func (c *KVStore) addChannelEdge(tx kvdb.RwTx,
1037
        edge *models.ChannelEdgeInfo) error {
3✔
1038

3✔
1039
        // Construct the channel's primary key which is the 8-byte channel ID.
3✔
1040
        var chanKey [8]byte
3✔
1041
        binary.BigEndian.PutUint64(chanKey[:], edge.ChannelID)
3✔
1042

3✔
1043
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
3✔
1044
        if err != nil {
3✔
1045
                return err
×
1046
        }
×
1047
        edges, err := tx.CreateTopLevelBucket(edgeBucket)
3✔
1048
        if err != nil {
3✔
1049
                return err
×
1050
        }
×
1051
        edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
3✔
1052
        if err != nil {
3✔
1053
                return err
×
1054
        }
×
1055
        chanIndex, err := edges.CreateBucketIfNotExists(channelPointBucket)
3✔
1056
        if err != nil {
3✔
1057
                return err
×
1058
        }
×
1059

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

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

1086
        _, node2Err := fetchLightningNode(nodes, edge.NodeKey2Bytes[:])
3✔
1087
        switch {
3✔
1088
        case errors.Is(node2Err, ErrGraphNodeNotFound):
3✔
1089
                node2Shell := models.LightningNode{
3✔
1090
                        PubKeyBytes:          edge.NodeKey2Bytes,
3✔
1091
                        HaveNodeAnnouncement: false,
3✔
1092
                }
3✔
1093
                err := addLightningNode(tx, &node2Shell)
3✔
1094
                if err != nil {
3✔
1095
                        return fmt.Errorf("unable to create shell node "+
×
1096
                                "for: %x: %w", edge.NodeKey2Bytes, err)
×
1097
                }
×
1098
        case node2Err != nil:
×
1099
                return node2Err
×
1100
        }
1101

1102
        // If the edge hasn't been created yet, then we'll first add it to the
1103
        // edge index in order to associate the edge between two nodes and also
1104
        // store the static components of the channel.
1105
        if err := putChanEdgeInfo(edgeIndex, edge, chanKey); err != nil {
3✔
1106
                return err
×
1107
        }
×
1108

1109
        // Mark edge policies for both sides as unknown. This is to enable
1110
        // efficient incoming channel lookup for a node.
1111
        keys := []*[33]byte{
3✔
1112
                &edge.NodeKey1Bytes,
3✔
1113
                &edge.NodeKey2Bytes,
3✔
1114
        }
3✔
1115
        for _, key := range keys {
6✔
1116
                err := putChanEdgePolicyUnknown(edges, edge.ChannelID, key[:])
3✔
1117
                if err != nil {
3✔
1118
                        return err
×
1119
                }
×
1120
        }
1121

1122
        // Finally we add it to the channel index which maps channel points
1123
        // (outpoints) to the shorter channel ID's.
1124
        var b bytes.Buffer
3✔
1125
        if err := WriteOutpoint(&b, &edge.ChannelPoint); err != nil {
3✔
1126
                return err
×
1127
        }
×
1128

1129
        return chanIndex.Put(b.Bytes(), chanKey[:])
3✔
1130
}
1131

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

3✔
1141
        var (
3✔
1142
                upd1Time time.Time
3✔
1143
                upd2Time time.Time
3✔
1144
                exists   bool
3✔
1145
                isZombie bool
3✔
1146
        )
3✔
1147

3✔
1148
        // We'll query the cache with the shared lock held to allow multiple
3✔
1149
        // readers to access values in the cache concurrently if they exist.
3✔
1150
        c.cacheMu.RLock()
3✔
1151
        if entry, ok := c.rejectCache.get(chanID); ok {
6✔
1152
                c.cacheMu.RUnlock()
3✔
1153
                upd1Time = time.Unix(entry.upd1Time, 0)
3✔
1154
                upd2Time = time.Unix(entry.upd2Time, 0)
3✔
1155
                exists, isZombie = entry.flags.unpack()
3✔
1156

3✔
1157
                return upd1Time, upd2Time, exists, isZombie, nil
3✔
1158
        }
3✔
1159
        c.cacheMu.RUnlock()
3✔
1160

3✔
1161
        c.cacheMu.Lock()
3✔
1162
        defer c.cacheMu.Unlock()
3✔
1163

3✔
1164
        // The item was not found with the shared lock, so we'll acquire the
3✔
1165
        // exclusive lock and check the cache again in case another method added
3✔
1166
        // the entry to the cache while no lock was held.
3✔
1167
        if entry, ok := c.rejectCache.get(chanID); ok {
5✔
1168
                upd1Time = time.Unix(entry.upd1Time, 0)
2✔
1169
                upd2Time = time.Unix(entry.upd2Time, 0)
2✔
1170
                exists, isZombie = entry.flags.unpack()
2✔
1171

2✔
1172
                return upd1Time, upd2Time, exists, isZombie, nil
2✔
1173
        }
2✔
1174

1175
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1176
                edges := tx.ReadBucket(edgeBucket)
3✔
1177
                if edges == nil {
3✔
1178
                        return ErrGraphNoEdgesFound
×
1179
                }
×
1180
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
1181
                if edgeIndex == nil {
3✔
1182
                        return ErrGraphNoEdgesFound
×
1183
                }
×
1184

1185
                var channelID [8]byte
3✔
1186
                byteOrder.PutUint64(channelID[:], chanID)
3✔
1187

3✔
1188
                // If the edge doesn't exist, then we'll also check our zombie
3✔
1189
                // index.
3✔
1190
                if edgeIndex.Get(channelID[:]) == nil {
6✔
1191
                        exists = false
3✔
1192
                        zombieIndex := edges.NestedReadBucket(zombieBucket)
3✔
1193
                        if zombieIndex != nil {
6✔
1194
                                isZombie, _, _ = isZombieEdge(
3✔
1195
                                        zombieIndex, chanID,
3✔
1196
                                )
3✔
1197
                        }
3✔
1198

1199
                        return nil
3✔
1200
                }
1201

1202
                exists = true
3✔
1203
                isZombie = false
3✔
1204

3✔
1205
                // If the channel has been found in the graph, then retrieve
3✔
1206
                // the edges itself so we can return the last updated
3✔
1207
                // timestamps.
3✔
1208
                nodes := tx.ReadBucket(nodeBucket)
3✔
1209
                if nodes == nil {
3✔
1210
                        return ErrGraphNodeNotFound
×
1211
                }
×
1212

1213
                e1, e2, err := fetchChanEdgePolicies(
3✔
1214
                        edgeIndex, edges, channelID[:],
3✔
1215
                )
3✔
1216
                if err != nil {
3✔
1217
                        return err
×
1218
                }
×
1219

1220
                // As we may have only one of the edges populated, only set the
1221
                // update time if the edge was found in the database.
1222
                if e1 != nil {
6✔
1223
                        upd1Time = e1.LastUpdate
3✔
1224
                }
3✔
1225
                if e2 != nil {
6✔
1226
                        upd2Time = e2.LastUpdate
3✔
1227
                }
3✔
1228

1229
                return nil
3✔
1230
        }, func() {}); err != nil {
3✔
1231
                return time.Time{}, time.Time{}, exists, isZombie, err
×
1232
        }
×
1233

1234
        c.rejectCache.insert(chanID, rejectCacheEntry{
3✔
1235
                upd1Time: upd1Time.Unix(),
3✔
1236
                upd2Time: upd2Time.Unix(),
3✔
1237
                flags:    packRejectFlags(exists, isZombie),
3✔
1238
        })
3✔
1239

3✔
1240
        return upd1Time, upd2Time, exists, isZombie, nil
3✔
1241
}
1242

1243
// AddEdgeProof sets the proof of an existing edge in the graph database.
1244
func (c *KVStore) AddEdgeProof(chanID lnwire.ShortChannelID,
1245
        proof *models.ChannelAuthProof) error {
3✔
1246

3✔
1247
        // Construct the channel's primary key which is the 8-byte channel ID.
3✔
1248
        var chanKey [8]byte
3✔
1249
        binary.BigEndian.PutUint64(chanKey[:], chanID.ToUint64())
3✔
1250

3✔
1251
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1252
                edges := tx.ReadWriteBucket(edgeBucket)
3✔
1253
                if edges == nil {
3✔
1254
                        return ErrEdgeNotFound
×
1255
                }
×
1256

1257
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
1258
                if edgeIndex == nil {
3✔
1259
                        return ErrEdgeNotFound
×
1260
                }
×
1261

1262
                edge, err := fetchChanEdgeInfo(edgeIndex, chanKey[:])
3✔
1263
                if err != nil {
3✔
1264
                        return err
×
1265
                }
×
1266

1267
                edge.AuthProof = proof
3✔
1268

3✔
1269
                return putChanEdgeInfo(edgeIndex, &edge, chanKey)
3✔
1270
        }, func() {})
3✔
1271
}
1272

1273
const (
1274
        // pruneTipBytes is the total size of the value which stores a prune
1275
        // entry of the graph in the prune log. The "prune tip" is the last
1276
        // entry in the prune log, and indicates if the channel graph is in
1277
        // sync with the current UTXO state. The structure of the value
1278
        // is: blockHash, taking 32 bytes total.
1279
        pruneTipBytes = 32
1280
)
1281

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

3✔
1294
        c.cacheMu.Lock()
3✔
1295
        defer c.cacheMu.Unlock()
3✔
1296

3✔
1297
        var (
3✔
1298
                chansClosed []*models.ChannelEdgeInfo
3✔
1299
                prunedNodes []route.Vertex
3✔
1300
        )
3✔
1301

3✔
1302
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1303
                // First grab the edges bucket which houses the information
3✔
1304
                // we'd like to delete
3✔
1305
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
3✔
1306
                if err != nil {
3✔
1307
                        return err
×
1308
                }
×
1309

1310
                // Next grab the two edge indexes which will also need to be
1311
                // updated.
1312
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
3✔
1313
                if err != nil {
3✔
1314
                        return err
×
1315
                }
×
1316
                chanIndex, err := edges.CreateBucketIfNotExists(
3✔
1317
                        channelPointBucket,
3✔
1318
                )
3✔
1319
                if err != nil {
3✔
1320
                        return err
×
1321
                }
×
1322
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
1323
                if nodes == nil {
3✔
1324
                        return ErrSourceNodeNotSet
×
1325
                }
×
1326
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
3✔
1327
                if err != nil {
3✔
1328
                        return err
×
1329
                }
×
1330

1331
                // For each of the outpoints that have been spent within the
1332
                // block, we attempt to delete them from the graph as if that
1333
                // outpoint was a channel, then it has now been closed.
1334
                for _, chanPoint := range spentOutputs {
6✔
1335
                        // TODO(roasbeef): load channel bloom filter, continue
3✔
1336
                        // if NOT if filter
3✔
1337

3✔
1338
                        var opBytes bytes.Buffer
3✔
1339
                        err := WriteOutpoint(&opBytes, chanPoint)
3✔
1340
                        if err != nil {
3✔
1341
                                return err
×
1342
                        }
×
1343

1344
                        // First attempt to see if the channel exists within
1345
                        // the database, if not, then we can exit early.
1346
                        chanID := chanIndex.Get(opBytes.Bytes())
3✔
1347
                        if chanID == nil {
3✔
UNCOV
1348
                                continue
×
1349
                        }
1350

1351
                        // Attempt to delete the channel, an ErrEdgeNotFound
1352
                        // will be returned if that outpoint isn't known to be
1353
                        // a channel. If no error is returned, then a channel
1354
                        // was successfully pruned.
1355
                        edgeInfo, err := c.delChannelEdgeUnsafe(
3✔
1356
                                edges, edgeIndex, chanIndex, zombieIndex,
3✔
1357
                                chanID, false, false,
3✔
1358
                        )
3✔
1359
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
3✔
1360
                                return err
×
1361
                        }
×
1362

1363
                        chansClosed = append(chansClosed, edgeInfo)
3✔
1364
                }
1365

1366
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
3✔
1367
                if err != nil {
3✔
1368
                        return err
×
1369
                }
×
1370

1371
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
3✔
1372
                        pruneLogBucket,
3✔
1373
                )
3✔
1374
                if err != nil {
3✔
1375
                        return err
×
1376
                }
×
1377

1378
                // With the graph pruned, add a new entry to the prune log,
1379
                // which can be used to check if the graph is fully synced with
1380
                // the current UTXO state.
1381
                var blockHeightBytes [4]byte
3✔
1382
                byteOrder.PutUint32(blockHeightBytes[:], blockHeight)
3✔
1383

3✔
1384
                var newTip [pruneTipBytes]byte
3✔
1385
                copy(newTip[:], blockHash[:])
3✔
1386

3✔
1387
                err = pruneBucket.Put(blockHeightBytes[:], newTip[:])
3✔
1388
                if err != nil {
3✔
1389
                        return err
×
1390
                }
×
1391

1392
                // Now that the graph has been pruned, we'll also attempt to
1393
                // prune any nodes that have had a channel closed within the
1394
                // latest block.
1395
                prunedNodes, err = c.pruneGraphNodes(nodes, edgeIndex)
3✔
1396

3✔
1397
                return err
3✔
1398
        }, func() {
3✔
1399
                chansClosed = nil
3✔
1400
                prunedNodes = nil
3✔
1401
        })
3✔
1402
        if err != nil {
3✔
1403
                return nil, nil, err
×
1404
        }
×
1405

1406
        for _, channel := range chansClosed {
6✔
1407
                c.rejectCache.remove(channel.ChannelID)
3✔
1408
                c.chanCache.remove(channel.ChannelID)
3✔
1409
        }
3✔
1410

1411
        return chansClosed, prunedNodes, nil
3✔
1412
}
1413

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

1434
                var err error
3✔
1435
                prunedNodes, err = c.pruneGraphNodes(nodes, edgeIndex)
3✔
1436
                if err != nil {
3✔
1437
                        return err
×
1438
                }
×
1439

1440
                return nil
3✔
1441
        }, func() {
3✔
1442
                prunedNodes = nil
3✔
1443
        })
3✔
1444

1445
        return prunedNodes, err
3✔
1446
}
1447

1448
// pruneGraphNodes attempts to remove any nodes from the graph who have had a
1449
// channel closed within the current block. If the node still has existing
1450
// channels in the graph, this will act as a no-op.
1451
func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket,
1452
        edgeIndex kvdb.RwBucket) ([]route.Vertex, error) {
3✔
1453

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

3✔
1456
        // We'll retrieve the graph's source node to ensure we don't remove it
3✔
1457
        // even if it no longer has any open channels.
3✔
1458
        sourceNode, err := c.sourceNode(nodes)
3✔
1459
        if err != nil {
3✔
1460
                return nil, err
×
1461
        }
×
1462

1463
        // We'll use this map to keep count the number of references to a node
1464
        // in the graph. A node should only be removed once it has no more
1465
        // references in the graph.
1466
        nodeRefCounts := make(map[[33]byte]int)
3✔
1467
        err = nodes.ForEach(func(pubKey, nodeBytes []byte) error {
6✔
1468
                // If this is the source key, then we skip this
3✔
1469
                // iteration as the value for this key is a pubKey
3✔
1470
                // rather than raw node information.
3✔
1471
                if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
6✔
1472
                        return nil
3✔
1473
                }
3✔
1474

1475
                var nodePub [33]byte
3✔
1476
                copy(nodePub[:], pubKey)
3✔
1477
                nodeRefCounts[nodePub] = 0
3✔
1478

3✔
1479
                return nil
3✔
1480
        })
1481
        if err != nil {
3✔
1482
                return nil, err
×
1483
        }
×
1484

1485
        // To ensure we never delete the source node, we'll start off by
1486
        // bumping its ref count to 1.
1487
        nodeRefCounts[sourceNode.PubKeyBytes] = 1
3✔
1488

3✔
1489
        // Next, we'll run through the edgeIndex which maps a channel ID to the
3✔
1490
        // edge info. We'll use this scan to populate our reference count map
3✔
1491
        // above.
3✔
1492
        err = edgeIndex.ForEach(func(chanID, edgeInfoBytes []byte) error {
6✔
1493
                // The first 66 bytes of the edge info contain the pubkeys of
3✔
1494
                // the nodes that this edge attaches. We'll extract them, and
3✔
1495
                // add them to the ref count map.
3✔
1496
                var node1, node2 [33]byte
3✔
1497
                copy(node1[:], edgeInfoBytes[:33])
3✔
1498
                copy(node2[:], edgeInfoBytes[33:])
3✔
1499

3✔
1500
                // With the nodes extracted, we'll increase the ref count of
3✔
1501
                // each of the nodes.
3✔
1502
                nodeRefCounts[node1]++
3✔
1503
                nodeRefCounts[node2]++
3✔
1504

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

1511
        // Finally, we'll make a second pass over the set of nodes, and delete
1512
        // any nodes that have a ref count of zero.
1513
        var pruned []route.Vertex
3✔
1514
        for nodePubKey, refCount := range nodeRefCounts {
6✔
1515
                // If the ref count of the node isn't zero, then we can safely
3✔
1516
                // skip it as it still has edges to or from it within the
3✔
1517
                // graph.
3✔
1518
                if refCount != 0 {
6✔
1519
                        continue
3✔
1520
                }
1521

1522
                // If we reach this point, then there are no longer any edges
1523
                // that connect this node, so we can delete it.
1524
                err := c.deleteLightningNode(nodes, nodePubKey[:])
3✔
1525
                if err != nil {
3✔
1526
                        if errors.Is(err, ErrGraphNodeNotFound) ||
×
1527
                                errors.Is(err, ErrGraphNodesNotFound) {
×
1528

×
1529
                                log.Warnf("Unable to prune node %x from the "+
×
1530
                                        "graph: %v", nodePubKey, err)
×
1531
                                continue
×
1532
                        }
1533

1534
                        return nil, err
×
1535
                }
1536

1537
                log.Infof("Pruned unconnected node %x from channel graph",
3✔
1538
                        nodePubKey[:])
3✔
1539

3✔
1540
                pruned = append(pruned, nodePubKey)
3✔
1541
        }
1542

1543
        if len(pruned) > 0 {
6✔
1544
                log.Infof("Pruned %v unconnected nodes from the channel graph",
3✔
1545
                        len(pruned))
3✔
1546
        }
3✔
1547

1548
        return pruned, err
3✔
1549
}
1550

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

2✔
1561
        // Every channel having a ShortChannelID starting at 'height'
2✔
1562
        // will no longer be confirmed.
2✔
1563
        startShortChanID := lnwire.ShortChannelID{
2✔
1564
                BlockHeight: height,
2✔
1565
        }
2✔
1566

2✔
1567
        // Delete everything after this height from the db up until the
2✔
1568
        // SCID alias range.
2✔
1569
        endShortChanID := aliasmgr.StartingAlias
2✔
1570

2✔
1571
        // The block height will be the 3 first bytes of the channel IDs.
2✔
1572
        var chanIDStart [8]byte
2✔
1573
        byteOrder.PutUint64(chanIDStart[:], startShortChanID.ToUint64())
2✔
1574
        var chanIDEnd [8]byte
2✔
1575
        byteOrder.PutUint64(chanIDEnd[:], endShortChanID.ToUint64())
2✔
1576

2✔
1577
        c.cacheMu.Lock()
2✔
1578
        defer c.cacheMu.Unlock()
2✔
1579

2✔
1580
        // Keep track of the channels that are removed from the graph.
2✔
1581
        var removedChans []*models.ChannelEdgeInfo
2✔
1582

2✔
1583
        if err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
4✔
1584
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
2✔
1585
                if err != nil {
2✔
1586
                        return err
×
1587
                }
×
1588
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
2✔
1589
                if err != nil {
2✔
1590
                        return err
×
1591
                }
×
1592
                chanIndex, err := edges.CreateBucketIfNotExists(
2✔
1593
                        channelPointBucket,
2✔
1594
                )
2✔
1595
                if err != nil {
2✔
1596
                        return err
×
1597
                }
×
1598
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
2✔
1599
                if err != nil {
2✔
1600
                        return err
×
1601
                }
×
1602

1603
                // Scan from chanIDStart to chanIDEnd, deleting every
1604
                // found edge.
1605
                // NOTE: we must delete the edges after the cursor loop, since
1606
                // modifying the bucket while traversing is not safe.
1607
                // NOTE: We use a < comparison in bytes.Compare instead of <=
1608
                // so that the StartingAlias itself isn't deleted.
1609
                var keys [][]byte
2✔
1610
                cursor := edgeIndex.ReadWriteCursor()
2✔
1611

2✔
1612
                //nolint:ll
2✔
1613
                for k, _ := cursor.Seek(chanIDStart[:]); k != nil &&
2✔
1614
                        bytes.Compare(k, chanIDEnd[:]) < 0; k, _ = cursor.Next() {
4✔
1615
                        keys = append(keys, k)
2✔
1616
                }
2✔
1617

1618
                for _, k := range keys {
4✔
1619
                        edgeInfo, err := c.delChannelEdgeUnsafe(
2✔
1620
                                edges, edgeIndex, chanIndex, zombieIndex,
2✔
1621
                                k, false, false,
2✔
1622
                        )
2✔
1623
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
2✔
1624
                                return err
×
1625
                        }
×
1626

1627
                        removedChans = append(removedChans, edgeInfo)
2✔
1628
                }
1629

1630
                // Delete all the entries in the prune log having a height
1631
                // greater or equal to the block disconnected.
1632
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
2✔
1633
                if err != nil {
2✔
1634
                        return err
×
1635
                }
×
1636

1637
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
2✔
1638
                        pruneLogBucket,
2✔
1639
                )
2✔
1640
                if err != nil {
2✔
1641
                        return err
×
1642
                }
×
1643

1644
                var pruneKeyStart [4]byte
2✔
1645
                byteOrder.PutUint32(pruneKeyStart[:], height)
2✔
1646

2✔
1647
                var pruneKeyEnd [4]byte
2✔
1648
                byteOrder.PutUint32(pruneKeyEnd[:], math.MaxUint32)
2✔
1649

2✔
1650
                // To avoid modifying the bucket while traversing, we delete
2✔
1651
                // the keys in a second loop.
2✔
1652
                var pruneKeys [][]byte
2✔
1653
                pruneCursor := pruneBucket.ReadWriteCursor()
2✔
1654
                //nolint:ll
2✔
1655
                for k, _ := pruneCursor.Seek(pruneKeyStart[:]); k != nil &&
2✔
1656
                        bytes.Compare(k, pruneKeyEnd[:]) <= 0; k, _ = pruneCursor.Next() {
4✔
1657
                        pruneKeys = append(pruneKeys, k)
2✔
1658
                }
2✔
1659

1660
                for _, k := range pruneKeys {
4✔
1661
                        if err := pruneBucket.Delete(k); err != nil {
2✔
1662
                                return err
×
1663
                        }
×
1664
                }
1665

1666
                return nil
2✔
1667
        }, func() {
2✔
1668
                removedChans = nil
2✔
1669
        }); err != nil {
2✔
1670
                return nil, err
×
1671
        }
×
1672

1673
        for _, channel := range removedChans {
4✔
1674
                c.rejectCache.remove(channel.ChannelID)
2✔
1675
                c.chanCache.remove(channel.ChannelID)
2✔
1676
        }
2✔
1677

1678
        return removedChans, nil
2✔
1679
}
1680

1681
// PruneTip returns the block height and hash of the latest block that has been
1682
// used to prune channels in the graph. Knowing the "prune tip" allows callers
1683
// to tell if the graph is currently in sync with the current best known UTXO
1684
// state.
1685
func (c *KVStore) PruneTip() (*chainhash.Hash, uint32, error) {
3✔
1686
        var (
3✔
1687
                tipHash   chainhash.Hash
3✔
1688
                tipHeight uint32
3✔
1689
        )
3✔
1690

3✔
1691
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1692
                graphMeta := tx.ReadBucket(graphMetaBucket)
3✔
1693
                if graphMeta == nil {
3✔
1694
                        return ErrGraphNotFound
×
1695
                }
×
1696
                pruneBucket := graphMeta.NestedReadBucket(pruneLogBucket)
3✔
1697
                if pruneBucket == nil {
3✔
1698
                        return ErrGraphNeverPruned
×
1699
                }
×
1700

1701
                pruneCursor := pruneBucket.ReadCursor()
3✔
1702

3✔
1703
                // The prune key with the largest block height will be our
3✔
1704
                // prune tip.
3✔
1705
                k, v := pruneCursor.Last()
3✔
1706
                if k == nil {
6✔
1707
                        return ErrGraphNeverPruned
3✔
1708
                }
3✔
1709

1710
                // Once we have the prune tip, the value will be the block hash,
1711
                // and the key the block height.
1712
                copy(tipHash[:], v)
3✔
1713
                tipHeight = byteOrder.Uint32(k)
3✔
1714

3✔
1715
                return nil
3✔
1716
        }, func() {})
3✔
1717
        if err != nil {
6✔
1718
                return nil, 0, err
3✔
1719
        }
3✔
1720

1721
        return &tipHash, tipHeight, nil
3✔
1722
}
1723

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

3✔
1735
        // TODO(roasbeef): possibly delete from node bucket if node has no more
3✔
1736
        // channels
3✔
1737
        // TODO(roasbeef): don't delete both edges?
3✔
1738

3✔
1739
        c.cacheMu.Lock()
3✔
1740
        defer c.cacheMu.Unlock()
3✔
1741

3✔
1742
        var infos []*models.ChannelEdgeInfo
3✔
1743
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1744
                edges := tx.ReadWriteBucket(edgeBucket)
3✔
1745
                if edges == nil {
3✔
1746
                        return ErrEdgeNotFound
×
1747
                }
×
1748
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
1749
                if edgeIndex == nil {
3✔
1750
                        return ErrEdgeNotFound
×
1751
                }
×
1752
                chanIndex := edges.NestedReadWriteBucket(channelPointBucket)
3✔
1753
                if chanIndex == nil {
3✔
1754
                        return ErrEdgeNotFound
×
1755
                }
×
1756
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
1757
                if nodes == nil {
3✔
1758
                        return ErrGraphNodeNotFound
×
1759
                }
×
1760
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
3✔
1761
                if err != nil {
3✔
1762
                        return err
×
1763
                }
×
1764

1765
                var rawChanID [8]byte
3✔
1766
                for _, chanID := range chanIDs {
6✔
1767
                        byteOrder.PutUint64(rawChanID[:], chanID)
3✔
1768
                        edgeInfo, err := c.delChannelEdgeUnsafe(
3✔
1769
                                edges, edgeIndex, chanIndex, zombieIndex,
3✔
1770
                                rawChanID[:], markZombie, strictZombiePruning,
3✔
1771
                        )
3✔
1772
                        if err != nil {
3✔
UNCOV
1773
                                return err
×
UNCOV
1774
                        }
×
1775

1776
                        infos = append(infos, edgeInfo)
3✔
1777
                }
1778

1779
                return nil
3✔
1780
        }, func() {
3✔
1781
                infos = nil
3✔
1782
        })
3✔
1783
        if err != nil {
3✔
UNCOV
1784
                return nil, err
×
UNCOV
1785
        }
×
1786

1787
        for _, chanID := range chanIDs {
6✔
1788
                c.rejectCache.remove(chanID)
3✔
1789
                c.chanCache.remove(chanID)
3✔
1790
        }
3✔
1791

1792
        return infos, nil
3✔
1793
}
1794

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

1810
        return chanID, nil
3✔
1811
}
1812

1813
// getChanID returns the assigned channel ID for a given channel point.
1814
func getChanID(tx kvdb.RTx, chanPoint *wire.OutPoint) (uint64, error) {
3✔
1815
        var b bytes.Buffer
3✔
1816
        if err := WriteOutpoint(&b, chanPoint); err != nil {
3✔
1817
                return 0, err
×
1818
        }
×
1819

1820
        edges := tx.ReadBucket(edgeBucket)
3✔
1821
        if edges == nil {
3✔
1822
                return 0, ErrGraphNoEdgesFound
×
1823
        }
×
1824
        chanIndex := edges.NestedReadBucket(channelPointBucket)
3✔
1825
        if chanIndex == nil {
3✔
1826
                return 0, ErrGraphNoEdgesFound
×
1827
        }
×
1828

1829
        chanIDBytes := chanIndex.Get(b.Bytes())
3✔
1830
        if chanIDBytes == nil {
6✔
1831
                return 0, ErrEdgeNotFound
3✔
1832
        }
3✔
1833

1834
        chanID := byteOrder.Uint64(chanIDBytes)
3✔
1835

3✔
1836
        return chanID, nil
3✔
1837
}
1838

1839
// TODO(roasbeef): allow updates to use Batch?
1840

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

3✔
1847
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1848
                edges := tx.ReadBucket(edgeBucket)
3✔
1849
                if edges == nil {
3✔
1850
                        return ErrGraphNoEdgesFound
×
1851
                }
×
1852
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
1853
                if edgeIndex == nil {
3✔
1854
                        return ErrGraphNoEdgesFound
×
1855
                }
×
1856

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

3✔
1861
                lastChanID, _ := cidCursor.Last()
3✔
1862

3✔
1863
                // If there's no key, then this means that we don't actually
3✔
1864
                // know of any channels, so we'll return a predicable error.
3✔
1865
                if lastChanID == nil {
6✔
1866
                        return ErrGraphNoEdgesFound
3✔
1867
                }
3✔
1868

1869
                // Otherwise, we'll de serialize the channel ID and return it
1870
                // to the caller.
1871
                cid = byteOrder.Uint64(lastChanID)
3✔
1872

3✔
1873
                return nil
3✔
1874
        }, func() {
3✔
1875
                cid = 0
3✔
1876
        })
3✔
1877
        if err != nil && !errors.Is(err, ErrGraphNoEdgesFound) {
3✔
1878
                return 0, err
×
1879
        }
×
1880

1881
        return cid, nil
3✔
1882
}
1883

1884
// ChannelEdge represents the complete set of information for a channel edge in
1885
// the known channel graph. This struct couples the core information of the
1886
// edge as well as each of the known advertised edge policies.
1887
type ChannelEdge struct {
1888
        // Info contains all the static information describing the channel.
1889
        Info *models.ChannelEdgeInfo
1890

1891
        // Policy1 points to the "first" edge policy of the channel containing
1892
        // the dynamic information required to properly route through the edge.
1893
        Policy1 *models.ChannelEdgePolicy
1894

1895
        // Policy2 points to the "second" edge policy of the channel containing
1896
        // the dynamic information required to properly route through the edge.
1897
        Policy2 *models.ChannelEdgePolicy
1898

1899
        // Node1 is "node 1" in the channel. This is the node that would have
1900
        // produced Policy1 if it exists.
1901
        Node1 *models.LightningNode
1902

1903
        // Node2 is "node 2" in the channel. This is the node that would have
1904
        // produced Policy2 if it exists.
1905
        Node2 *models.LightningNode
1906
}
1907

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

3✔
1913
        // To ensure we don't return duplicate ChannelEdges, we'll use an
3✔
1914
        // additional map to keep track of the edges already seen to prevent
3✔
1915
        // re-adding it.
3✔
1916
        var edgesSeen map[uint64]struct{}
3✔
1917
        var edgesToCache map[uint64]ChannelEdge
3✔
1918
        var edgesInHorizon []ChannelEdge
3✔
1919

3✔
1920
        c.cacheMu.Lock()
3✔
1921
        defer c.cacheMu.Unlock()
3✔
1922

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

1938
                nodes := tx.ReadBucket(nodeBucket)
3✔
1939
                if nodes == nil {
3✔
1940
                        return ErrGraphNodesNotFound
×
1941
                }
×
1942

1943
                // We'll now obtain a cursor to perform a range query within
1944
                // the index to find all channels within the horizon.
1945
                updateCursor := edgeUpdateIndex.ReadCursor()
3✔
1946

3✔
1947
                var startTimeBytes, endTimeBytes [8 + 8]byte
3✔
1948
                byteOrder.PutUint64(
3✔
1949
                        startTimeBytes[:8], uint64(startTime.Unix()),
3✔
1950
                )
3✔
1951
                byteOrder.PutUint64(
3✔
1952
                        endTimeBytes[:8], uint64(endTime.Unix()),
3✔
1953
                )
3✔
1954

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

3✔
1966
                        // If we've already retrieved the info and policies for
3✔
1967
                        // this edge, then we can skip it as we don't need to do
3✔
1968
                        // so again.
3✔
1969
                        chanIDInt := byteOrder.Uint64(chanID)
3✔
1970
                        if _, ok := edgesSeen[chanIDInt]; ok {
3✔
UNCOV
1971
                                continue
×
1972
                        }
1973

1974
                        if channel, ok := c.chanCache.get(chanIDInt); ok {
6✔
1975
                                hits++
3✔
1976
                                edgesSeen[chanIDInt] = struct{}{}
3✔
1977
                                edgesInHorizon = append(edgesInHorizon, channel)
3✔
1978

3✔
1979
                                continue
3✔
1980
                        }
1981

1982
                        // First, we'll fetch the static edge information.
1983
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
1984
                        if err != nil {
3✔
1985
                                chanID := byteOrder.Uint64(chanID)
×
1986
                                return fmt.Errorf("unable to fetch info for "+
×
1987
                                        "edge with chan_id=%v: %v", chanID, err)
×
1988
                        }
×
1989

1990
                        // With the static information obtained, we'll now
1991
                        // fetch the dynamic policy info.
1992
                        edge1, edge2, err := fetchChanEdgePolicies(
3✔
1993
                                edgeIndex, edges, chanID,
3✔
1994
                        )
3✔
1995
                        if err != nil {
3✔
1996
                                chanID := byteOrder.Uint64(chanID)
×
1997
                                return fmt.Errorf("unable to fetch policies "+
×
1998
                                        "for edge with chan_id=%v: %v", chanID,
×
1999
                                        err)
×
2000
                        }
×
2001

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

2009
                        node2, err := fetchLightningNode(
3✔
2010
                                nodes, edgeInfo.NodeKey2Bytes[:],
3✔
2011
                        )
3✔
2012
                        if err != nil {
3✔
2013
                                return err
×
2014
                        }
×
2015

2016
                        // Finally, we'll collate this edge with the rest of
2017
                        // edges to be returned.
2018
                        edgesSeen[chanIDInt] = struct{}{}
3✔
2019
                        channel := ChannelEdge{
3✔
2020
                                Info:    &edgeInfo,
3✔
2021
                                Policy1: edge1,
3✔
2022
                                Policy2: edge2,
3✔
2023
                                Node1:   &node1,
3✔
2024
                                Node2:   &node2,
3✔
2025
                        }
3✔
2026
                        edgesInHorizon = append(edgesInHorizon, channel)
3✔
2027
                        edgesToCache[chanIDInt] = channel
3✔
2028
                }
2029

2030
                return nil
3✔
2031
        }, func() {
3✔
2032
                edgesSeen = make(map[uint64]struct{})
3✔
2033
                edgesToCache = make(map[uint64]ChannelEdge)
3✔
2034
                edgesInHorizon = nil
3✔
2035
        })
3✔
2036
        switch {
3✔
2037
        case errors.Is(err, ErrGraphNoEdgesFound):
×
2038
                fallthrough
×
2039
        case errors.Is(err, ErrGraphNodesNotFound):
×
2040
                break
×
2041

2042
        case err != nil:
×
2043
                return nil, err
×
2044
        }
2045

2046
        // Insert any edges loaded from disk into the cache.
2047
        for chanid, channel := range edgesToCache {
6✔
2048
                c.chanCache.insert(chanid, channel)
3✔
2049
        }
3✔
2050

2051
        log.Debugf("ChanUpdatesInHorizon hit percentage: %f (%d/%d)",
3✔
2052
                float64(hits)/float64(len(edgesInHorizon)), hits,
3✔
2053
                len(edgesInHorizon))
3✔
2054

3✔
2055
        return edgesInHorizon, nil
3✔
2056
}
2057

2058
// NodeUpdatesInHorizon returns all the known lightning node which have an
2059
// update timestamp within the passed range. This method can be used by two
2060
// nodes to quickly determine if they have the same set of up to date node
2061
// announcements.
2062
func (c *KVStore) NodeUpdatesInHorizon(startTime,
2063
        endTime time.Time) ([]models.LightningNode, error) {
3✔
2064

3✔
2065
        var nodesInHorizon []models.LightningNode
3✔
2066

3✔
2067
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
2068
                nodes := tx.ReadBucket(nodeBucket)
3✔
2069
                if nodes == nil {
3✔
2070
                        return ErrGraphNodesNotFound
×
2071
                }
×
2072

2073
                nodeUpdateIndex := nodes.NestedReadBucket(nodeUpdateIndexBucket)
3✔
2074
                if nodeUpdateIndex == nil {
3✔
2075
                        return ErrGraphNodesNotFound
×
2076
                }
×
2077

2078
                // We'll now obtain a cursor to perform a range query within
2079
                // the index to find all node announcements within the horizon.
2080
                updateCursor := nodeUpdateIndex.ReadCursor()
3✔
2081

3✔
2082
                var startTimeBytes, endTimeBytes [8 + 33]byte
3✔
2083
                byteOrder.PutUint64(
3✔
2084
                        startTimeBytes[:8], uint64(startTime.Unix()),
3✔
2085
                )
3✔
2086
                byteOrder.PutUint64(
3✔
2087
                        endTimeBytes[:8], uint64(endTime.Unix()),
3✔
2088
                )
3✔
2089

3✔
2090
                // With our start and end times constructed, we'll step through
3✔
2091
                // the index collecting info for each node within the time
3✔
2092
                // range.
3✔
2093
                //
3✔
2094
                //nolint:ll
3✔
2095
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
3✔
2096
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
6✔
2097
                        nodePub := indexKey[8:]
3✔
2098
                        node, err := fetchLightningNode(nodes, nodePub)
3✔
2099
                        if err != nil {
3✔
2100
                                return err
×
2101
                        }
×
2102

2103
                        nodesInHorizon = append(nodesInHorizon, node)
3✔
2104
                }
2105

2106
                return nil
3✔
2107
        }, func() {
3✔
2108
                nodesInHorizon = nil
3✔
2109
        })
3✔
2110
        switch {
3✔
2111
        case errors.Is(err, ErrGraphNoEdgesFound):
×
2112
                fallthrough
×
2113
        case errors.Is(err, ErrGraphNodesNotFound):
×
2114
                break
×
2115

2116
        case err != nil:
×
2117
                return nil, err
×
2118
        }
2119

2120
        return nodesInHorizon, nil
3✔
2121
}
2122

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

3✔
2132
        var (
3✔
2133
                newChanIDs   []uint64
3✔
2134
                knownZombies []ChannelUpdateInfo
3✔
2135
        )
3✔
2136

3✔
2137
        c.cacheMu.Lock()
3✔
2138
        defer c.cacheMu.Unlock()
3✔
2139

3✔
2140
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
2141
                edges := tx.ReadBucket(edgeBucket)
3✔
2142
                if edges == nil {
3✔
2143
                        return ErrGraphNoEdgesFound
×
2144
                }
×
2145
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
2146
                if edgeIndex == nil {
3✔
2147
                        return ErrGraphNoEdgesFound
×
2148
                }
×
2149

2150
                // Fetch the zombie index, it may not exist if no edges have
2151
                // ever been marked as zombies. If the index has been
2152
                // initialized, we will use it later to skip known zombie edges.
2153
                zombieIndex := edges.NestedReadBucket(zombieBucket)
3✔
2154

3✔
2155
                // We'll run through the set of chanIDs and collate only the
3✔
2156
                // set of channel that are unable to be found within our db.
3✔
2157
                var cidBytes [8]byte
3✔
2158
                for _, info := range chansInfo {
6✔
2159
                        scid := info.ShortChannelID.ToUint64()
3✔
2160
                        byteOrder.PutUint64(cidBytes[:], scid)
3✔
2161

3✔
2162
                        // If the edge is already known, skip it.
3✔
2163
                        if v := edgeIndex.Get(cidBytes[:]); v != nil {
6✔
2164
                                continue
3✔
2165
                        }
2166

2167
                        // If the edge is a known zombie, skip it.
2168
                        if zombieIndex != nil {
6✔
2169
                                isZombie, _, _ := isZombieEdge(
3✔
2170
                                        zombieIndex, scid,
3✔
2171
                                )
3✔
2172

3✔
2173
                                if isZombie {
3✔
UNCOV
2174
                                        knownZombies = append(
×
UNCOV
2175
                                                knownZombies, info,
×
UNCOV
2176
                                        )
×
UNCOV
2177

×
UNCOV
2178
                                        continue
×
2179
                                }
2180
                        }
2181

2182
                        newChanIDs = append(newChanIDs, scid)
3✔
2183
                }
2184

2185
                return nil
3✔
2186
        }, func() {
3✔
2187
                newChanIDs = nil
3✔
2188
                knownZombies = nil
3✔
2189
        })
3✔
2190
        switch {
3✔
2191
        // If we don't know of any edges yet, then we'll return the entire set
2192
        // of chan IDs specified.
2193
        case errors.Is(err, ErrGraphNoEdgesFound):
×
2194
                ogChanIDs := make([]uint64, len(chansInfo))
×
2195
                for i, info := range chansInfo {
×
2196
                        ogChanIDs[i] = info.ShortChannelID.ToUint64()
×
2197
                }
×
2198

2199
                return ogChanIDs, nil, nil
×
2200

2201
        case err != nil:
×
2202
                return nil, nil, err
×
2203
        }
2204

2205
        return newChanIDs, knownZombies, nil
3✔
2206
}
2207

2208
// ChannelUpdateInfo couples the SCID of a channel with the timestamps of the
2209
// latest received channel updates for the channel.
2210
type ChannelUpdateInfo struct {
2211
        // ShortChannelID is the SCID identifier of the channel.
2212
        ShortChannelID lnwire.ShortChannelID
2213

2214
        // Node1UpdateTimestamp is the timestamp of the latest received update
2215
        // from the node 1 channel peer. This will be set to zero time if no
2216
        // update has yet been received from this node.
2217
        Node1UpdateTimestamp time.Time
2218

2219
        // Node2UpdateTimestamp is the timestamp of the latest received update
2220
        // from the node 2 channel peer. This will be set to zero time if no
2221
        // update has yet been received from this node.
2222
        Node2UpdateTimestamp time.Time
2223
}
2224

2225
// NewChannelUpdateInfo is a constructor which makes sure we initialize the
2226
// timestamps with zero seconds unix timestamp which equals
2227
// `January 1, 1970, 00:00:00 UTC` in case the value is `time.Time{}`.
2228
func NewChannelUpdateInfo(scid lnwire.ShortChannelID, node1Timestamp,
2229
        node2Timestamp time.Time) ChannelUpdateInfo {
3✔
2230

3✔
2231
        chanInfo := ChannelUpdateInfo{
3✔
2232
                ShortChannelID:       scid,
3✔
2233
                Node1UpdateTimestamp: node1Timestamp,
3✔
2234
                Node2UpdateTimestamp: node2Timestamp,
3✔
2235
        }
3✔
2236

3✔
2237
        if node1Timestamp.IsZero() {
6✔
2238
                chanInfo.Node1UpdateTimestamp = time.Unix(0, 0)
3✔
2239
        }
3✔
2240

2241
        if node2Timestamp.IsZero() {
6✔
2242
                chanInfo.Node2UpdateTimestamp = time.Unix(0, 0)
3✔
2243
        }
3✔
2244

2245
        return chanInfo
3✔
2246
}
2247

2248
// BlockChannelRange represents a range of channels for a given block height.
2249
type BlockChannelRange struct {
2250
        // Height is the height of the block all of the channels below were
2251
        // included in.
2252
        Height uint32
2253

2254
        // Channels is the list of channels identified by their short ID
2255
        // representation known to us that were included in the block height
2256
        // above. The list may include channel update timestamp information if
2257
        // requested.
2258
        Channels []ChannelUpdateInfo
2259
}
2260

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

3✔
2271
        startChanID := &lnwire.ShortChannelID{
3✔
2272
                BlockHeight: startHeight,
3✔
2273
        }
3✔
2274

3✔
2275
        endChanID := lnwire.ShortChannelID{
3✔
2276
                BlockHeight: endHeight,
3✔
2277
                TxIndex:     math.MaxUint32 & 0x00ffffff,
3✔
2278
                TxPosition:  math.MaxUint16,
3✔
2279
        }
3✔
2280

3✔
2281
        // As we need to perform a range scan, we'll convert the starting and
3✔
2282
        // ending height to their corresponding values when encoded using short
3✔
2283
        // channel ID's.
3✔
2284
        var chanIDStart, chanIDEnd [8]byte
3✔
2285
        byteOrder.PutUint64(chanIDStart[:], startChanID.ToUint64())
3✔
2286
        byteOrder.PutUint64(chanIDEnd[:], endChanID.ToUint64())
3✔
2287

3✔
2288
        var channelsPerBlock map[uint32][]ChannelUpdateInfo
3✔
2289
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
2290
                edges := tx.ReadBucket(edgeBucket)
3✔
2291
                if edges == nil {
3✔
2292
                        return ErrGraphNoEdgesFound
×
2293
                }
×
2294
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
2295
                if edgeIndex == nil {
3✔
2296
                        return ErrGraphNoEdgesFound
×
2297
                }
×
2298

2299
                cursor := edgeIndex.ReadCursor()
3✔
2300

3✔
2301
                // We'll now iterate through the database, and find each
3✔
2302
                // channel ID that resides within the specified range.
3✔
2303
                //
3✔
2304
                //nolint:ll
3✔
2305
                for k, v := cursor.Seek(chanIDStart[:]); k != nil &&
3✔
2306
                        bytes.Compare(k, chanIDEnd[:]) <= 0; k, v = cursor.Next() {
6✔
2307
                        // Don't send alias SCIDs during gossip sync.
3✔
2308
                        edgeReader := bytes.NewReader(v)
3✔
2309
                        edgeInfo, err := deserializeChanEdgeInfo(edgeReader)
3✔
2310
                        if err != nil {
3✔
2311
                                return err
×
2312
                        }
×
2313

2314
                        if edgeInfo.AuthProof == nil {
6✔
2315
                                continue
3✔
2316
                        }
2317

2318
                        // This channel ID rests within the target range, so
2319
                        // we'll add it to our returned set.
2320
                        rawCid := byteOrder.Uint64(k)
3✔
2321
                        cid := lnwire.NewShortChanIDFromInt(rawCid)
3✔
2322

3✔
2323
                        chanInfo := NewChannelUpdateInfo(
3✔
2324
                                cid, time.Time{}, time.Time{},
3✔
2325
                        )
3✔
2326

3✔
2327
                        if !withTimestamps {
3✔
UNCOV
2328
                                channelsPerBlock[cid.BlockHeight] = append(
×
UNCOV
2329
                                        channelsPerBlock[cid.BlockHeight],
×
UNCOV
2330
                                        chanInfo,
×
UNCOV
2331
                                )
×
UNCOV
2332

×
UNCOV
2333
                                continue
×
2334
                        }
2335

2336
                        node1Key, node2Key := computeEdgePolicyKeys(&edgeInfo)
3✔
2337

3✔
2338
                        rawPolicy := edges.Get(node1Key)
3✔
2339
                        if len(rawPolicy) != 0 {
6✔
2340
                                r := bytes.NewReader(rawPolicy)
3✔
2341

3✔
2342
                                edge, err := deserializeChanEdgePolicyRaw(r)
3✔
2343
                                if err != nil && !errors.Is(
3✔
2344
                                        err, ErrEdgePolicyOptionalFieldNotFound,
3✔
2345
                                ) {
3✔
2346

×
2347
                                        return err
×
2348
                                }
×
2349

2350
                                chanInfo.Node1UpdateTimestamp = edge.LastUpdate
3✔
2351
                        }
2352

2353
                        rawPolicy = edges.Get(node2Key)
3✔
2354
                        if len(rawPolicy) != 0 {
6✔
2355
                                r := bytes.NewReader(rawPolicy)
3✔
2356

3✔
2357
                                edge, err := deserializeChanEdgePolicyRaw(r)
3✔
2358
                                if err != nil && !errors.Is(
3✔
2359
                                        err, ErrEdgePolicyOptionalFieldNotFound,
3✔
2360
                                ) {
3✔
2361

×
2362
                                        return err
×
2363
                                }
×
2364

2365
                                chanInfo.Node2UpdateTimestamp = edge.LastUpdate
3✔
2366
                        }
2367

2368
                        channelsPerBlock[cid.BlockHeight] = append(
3✔
2369
                                channelsPerBlock[cid.BlockHeight], chanInfo,
3✔
2370
                        )
3✔
2371
                }
2372

2373
                return nil
3✔
2374
        }, func() {
3✔
2375
                channelsPerBlock = make(map[uint32][]ChannelUpdateInfo)
3✔
2376
        })
3✔
2377

2378
        switch {
3✔
2379
        // If we don't know of any channels yet, then there's nothing to
2380
        // filter, so we'll return an empty slice.
2381
        case errors.Is(err, ErrGraphNoEdgesFound) || len(channelsPerBlock) == 0:
3✔
2382
                return nil, nil
3✔
2383

2384
        case err != nil:
×
2385
                return nil, err
×
2386
        }
2387

2388
        // Return the channel ranges in ascending block height order.
2389
        blocks := make([]uint32, 0, len(channelsPerBlock))
3✔
2390
        for block := range channelsPerBlock {
6✔
2391
                blocks = append(blocks, block)
3✔
2392
        }
3✔
2393
        sort.Slice(blocks, func(i, j int) bool {
6✔
2394
                return blocks[i] < blocks[j]
3✔
2395
        })
3✔
2396

2397
        channelRanges := make([]BlockChannelRange, 0, len(channelsPerBlock))
3✔
2398
        for _, block := range blocks {
6✔
2399
                channelRanges = append(channelRanges, BlockChannelRange{
3✔
2400
                        Height:   block,
3✔
2401
                        Channels: channelsPerBlock[block],
3✔
2402
                })
3✔
2403
        }
3✔
2404

2405
        return channelRanges, nil
3✔
2406
}
2407

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

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

3✔
2429
        var (
3✔
2430
                chanEdges []ChannelEdge
3✔
2431
                cidBytes  [8]byte
3✔
2432
        )
3✔
2433

3✔
2434
        fetchChanInfos := func(tx kvdb.RTx) error {
6✔
2435
                edges := tx.ReadBucket(edgeBucket)
3✔
2436
                if edges == nil {
3✔
2437
                        return ErrGraphNoEdgesFound
×
2438
                }
×
2439
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
2440
                if edgeIndex == nil {
3✔
2441
                        return ErrGraphNoEdgesFound
×
2442
                }
×
2443
                nodes := tx.ReadBucket(nodeBucket)
3✔
2444
                if nodes == nil {
3✔
2445
                        return ErrGraphNotFound
×
2446
                }
×
2447

2448
                for _, cid := range chanIDs {
6✔
2449
                        byteOrder.PutUint64(cidBytes[:], cid)
3✔
2450

3✔
2451
                        // First, we'll fetch the static edge information. If
3✔
2452
                        // the edge is unknown, we will skip the edge and
3✔
2453
                        // continue gathering all known edges.
3✔
2454
                        edgeInfo, err := fetchChanEdgeInfo(
3✔
2455
                                edgeIndex, cidBytes[:],
3✔
2456
                        )
3✔
2457
                        switch {
3✔
UNCOV
2458
                        case errors.Is(err, ErrEdgeNotFound):
×
UNCOV
2459
                                continue
×
2460
                        case err != nil:
×
2461
                                return err
×
2462
                        }
2463

2464
                        // With the static information obtained, we'll now
2465
                        // fetch the dynamic policy info.
2466
                        edge1, edge2, err := fetchChanEdgePolicies(
3✔
2467
                                edgeIndex, edges, cidBytes[:],
3✔
2468
                        )
3✔
2469
                        if err != nil {
3✔
2470
                                return err
×
2471
                        }
×
2472

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

2480
                        node2, err := fetchLightningNode(
3✔
2481
                                nodes, edgeInfo.NodeKey2Bytes[:],
3✔
2482
                        )
3✔
2483
                        if err != nil {
3✔
2484
                                return err
×
2485
                        }
×
2486

2487
                        chanEdges = append(chanEdges, ChannelEdge{
3✔
2488
                                Info:    &edgeInfo,
3✔
2489
                                Policy1: edge1,
3✔
2490
                                Policy2: edge2,
3✔
2491
                                Node1:   &node1,
3✔
2492
                                Node2:   &node2,
3✔
2493
                        })
3✔
2494
                }
2495

2496
                return nil
3✔
2497
        }
2498

2499
        if tx == nil {
6✔
2500
                err := kvdb.View(c.db, fetchChanInfos, func() {
6✔
2501
                        chanEdges = nil
3✔
2502
                })
3✔
2503
                if err != nil {
3✔
2504
                        return nil, err
×
2505
                }
×
2506

2507
                return chanEdges, nil
3✔
2508
        }
2509

2510
        err := fetchChanInfos(tx)
×
2511
        if err != nil {
×
2512
                return nil, err
×
2513
        }
×
2514

2515
        return chanEdges, nil
×
2516
}
2517

2518
func delEdgeUpdateIndexEntry(edgesBucket kvdb.RwBucket, chanID uint64,
2519
        edge1, edge2 *models.ChannelEdgePolicy) error {
3✔
2520

3✔
2521
        // First, we'll fetch the edge update index bucket which currently
3✔
2522
        // stores an entry for the channel we're about to delete.
3✔
2523
        updateIndex := edgesBucket.NestedReadWriteBucket(edgeUpdateIndexBucket)
3✔
2524
        if updateIndex == nil {
3✔
2525
                // No edges in bucket, return early.
×
2526
                return nil
×
2527
        }
×
2528

2529
        // Now that we have the bucket, we'll attempt to construct a template
2530
        // for the index key: updateTime || chanid.
2531
        var indexKey [8 + 8]byte
3✔
2532
        byteOrder.PutUint64(indexKey[8:], chanID)
3✔
2533

3✔
2534
        // With the template constructed, we'll attempt to delete an entry that
3✔
2535
        // would have been created by both edges: we'll alternate the update
3✔
2536
        // times, as one may had overridden the other.
3✔
2537
        if edge1 != nil {
6✔
2538
                byteOrder.PutUint64(
3✔
2539
                        indexKey[:8], uint64(edge1.LastUpdate.Unix()),
3✔
2540
                )
3✔
2541
                if err := updateIndex.Delete(indexKey[:]); err != nil {
3✔
2542
                        return err
×
2543
                }
×
2544
        }
2545

2546
        // We'll also attempt to delete the entry that may have been created by
2547
        // the second edge.
2548
        if edge2 != nil {
6✔
2549
                byteOrder.PutUint64(
3✔
2550
                        indexKey[:8], uint64(edge2.LastUpdate.Unix()),
3✔
2551
                )
3✔
2552
                if err := updateIndex.Delete(indexKey[:]); err != nil {
3✔
2553
                        return err
×
2554
                }
×
2555
        }
2556

2557
        return nil
3✔
2558
}
2559

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

3✔
2571
        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
2572
        if err != nil {
3✔
UNCOV
2573
                return nil, err
×
UNCOV
2574
        }
×
2575

2576
        // We'll also remove the entry in the edge update index bucket before
2577
        // we delete the edges themselves so we can access their last update
2578
        // times.
2579
        cid := byteOrder.Uint64(chanID)
3✔
2580
        edge1, edge2, err := fetchChanEdgePolicies(edgeIndex, edges, chanID)
3✔
2581
        if err != nil {
3✔
2582
                return nil, err
×
2583
        }
×
2584
        err = delEdgeUpdateIndexEntry(edges, cid, edge1, edge2)
3✔
2585
        if err != nil {
3✔
2586
                return nil, err
×
2587
        }
×
2588

2589
        // The edge key is of the format pubKey || chanID. First we construct
2590
        // the latter half, populating the channel ID.
2591
        var edgeKey [33 + 8]byte
3✔
2592
        copy(edgeKey[33:], chanID)
3✔
2593

3✔
2594
        // With the latter half constructed, copy over the first public key to
3✔
2595
        // delete the edge in this direction, then the second to delete the
3✔
2596
        // edge in the opposite direction.
3✔
2597
        copy(edgeKey[:33], edgeInfo.NodeKey1Bytes[:])
3✔
2598
        if edges.Get(edgeKey[:]) != nil {
6✔
2599
                if err := edges.Delete(edgeKey[:]); err != nil {
3✔
2600
                        return nil, err
×
2601
                }
×
2602
        }
2603
        copy(edgeKey[:33], edgeInfo.NodeKey2Bytes[:])
3✔
2604
        if edges.Get(edgeKey[:]) != nil {
6✔
2605
                if err := edges.Delete(edgeKey[:]); err != nil {
3✔
2606
                        return nil, err
×
2607
                }
×
2608
        }
2609

2610
        // As part of deleting the edge we also remove all disabled entries
2611
        // from the edgePolicyDisabledIndex bucket. We do that for both
2612
        // directions.
2613
        err = updateEdgePolicyDisabledIndex(edges, cid, false, false)
3✔
2614
        if err != nil {
3✔
2615
                return nil, err
×
2616
        }
×
2617
        err = updateEdgePolicyDisabledIndex(edges, cid, true, false)
3✔
2618
        if err != nil {
3✔
2619
                return nil, err
×
2620
        }
×
2621

2622
        // With the edge data deleted, we can purge the information from the two
2623
        // edge indexes.
2624
        if err := edgeIndex.Delete(chanID); err != nil {
3✔
2625
                return nil, err
×
2626
        }
×
2627
        var b bytes.Buffer
3✔
2628
        if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
3✔
2629
                return nil, err
×
2630
        }
×
2631
        if err := chanIndex.Delete(b.Bytes()); err != nil {
3✔
2632
                return nil, err
×
2633
        }
×
2634

2635
        // Finally, we'll mark the edge as a zombie within our index if it's
2636
        // being removed due to the channel becoming a zombie. We do this to
2637
        // ensure we don't store unnecessary data for spent channels.
2638
        if !isZombie {
6✔
2639
                return &edgeInfo, nil
3✔
2640
        }
3✔
2641

2642
        nodeKey1, nodeKey2 := edgeInfo.NodeKey1Bytes, edgeInfo.NodeKey2Bytes
3✔
2643
        if strictZombie {
3✔
UNCOV
2644
                nodeKey1, nodeKey2 = makeZombiePubkeys(&edgeInfo, edge1, edge2)
×
UNCOV
2645
        }
×
2646

2647
        return &edgeInfo, markEdgeZombie(
3✔
2648
                zombieIndex, byteOrder.Uint64(chanID), nodeKey1, nodeKey2,
3✔
2649
        )
3✔
2650
}
2651

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

×
UNCOV
2671
        switch {
×
2672
        // If we don't have either edge policy, we'll return both pubkeys so
2673
        // that the channel can be resurrected by either party.
UNCOV
2674
        case e1 == nil && e2 == nil:
×
UNCOV
2675
                return info.NodeKey1Bytes, info.NodeKey2Bytes
×
2676

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

2684
        // Otherwise, we're missing edge2 or edge2 is the older side, so we
2685
        // return a blank pubkey for edge1. In this case, only an update from
2686
        // edge2 can resurect the channel.
UNCOV
2687
        default:
×
UNCOV
2688
                return [33]byte{}, info.NodeKey2Bytes
×
2689
        }
2690
}
2691

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

3✔
2702
        var (
3✔
2703
                ctx          = context.TODO()
3✔
2704
                isUpdate1    bool
3✔
2705
                edgeNotFound bool
3✔
2706
                from, to     route.Vertex
3✔
2707
        )
3✔
2708

3✔
2709
        r := &batch.Request[kvdb.RwTx]{
3✔
2710
                Opts: batch.NewSchedulerOptions(opts...),
3✔
2711
                Reset: func() {
6✔
2712
                        isUpdate1 = false
3✔
2713
                        edgeNotFound = false
3✔
2714
                },
3✔
2715
                Do: func(tx kvdb.RwTx) error {
3✔
2716
                        var err error
3✔
2717
                        from, to, isUpdate1, err = updateEdgePolicy(tx, edge)
3✔
2718
                        if err != nil {
3✔
UNCOV
2719
                                log.Errorf("UpdateEdgePolicy faild: %v", err)
×
UNCOV
2720
                        }
×
2721

2722
                        // Silence ErrEdgeNotFound so that the batch can
2723
                        // succeed, but propagate the error via local state.
2724
                        if errors.Is(err, ErrEdgeNotFound) {
3✔
UNCOV
2725
                                edgeNotFound = true
×
UNCOV
2726
                                return nil
×
UNCOV
2727
                        }
×
2728

2729
                        return err
3✔
2730
                },
2731
                OnCommit: func(err error) error {
3✔
2732
                        switch {
3✔
2733
                        case err != nil:
×
2734
                                return err
×
UNCOV
2735
                        case edgeNotFound:
×
UNCOV
2736
                                return ErrEdgeNotFound
×
2737
                        default:
3✔
2738
                                c.updateEdgeCache(edge, isUpdate1)
3✔
2739
                                return nil
3✔
2740
                        }
2741
                },
2742
        }
2743

2744
        err := c.chanScheduler.Execute(ctx, r)
3✔
2745

3✔
2746
        return from, to, err
3✔
2747
}
2748

2749
func (c *KVStore) updateEdgeCache(e *models.ChannelEdgePolicy,
2750
        isUpdate1 bool) {
3✔
2751

3✔
2752
        // If an entry for this channel is found in reject cache, we'll modify
3✔
2753
        // the entry with the updated timestamp for the direction that was just
3✔
2754
        // written. If the edge doesn't exist, we'll load the cache entry lazily
3✔
2755
        // during the next query for this edge.
3✔
2756
        if entry, ok := c.rejectCache.get(e.ChannelID); ok {
6✔
2757
                if isUpdate1 {
6✔
2758
                        entry.upd1Time = e.LastUpdate.Unix()
3✔
2759
                } else {
6✔
2760
                        entry.upd2Time = e.LastUpdate.Unix()
3✔
2761
                }
3✔
2762
                c.rejectCache.insert(e.ChannelID, entry)
3✔
2763
        }
2764

2765
        // If an entry for this channel is found in channel cache, we'll modify
2766
        // the entry with the updated policy for the direction that was just
2767
        // written. If the edge doesn't exist, we'll defer loading the info and
2768
        // policies and lazily read from disk during the next query.
2769
        if channel, ok := c.chanCache.get(e.ChannelID); ok {
6✔
2770
                if isUpdate1 {
6✔
2771
                        channel.Policy1 = e
3✔
2772
                } else {
6✔
2773
                        channel.Policy2 = e
3✔
2774
                }
3✔
2775
                c.chanCache.insert(e.ChannelID, channel)
3✔
2776
        }
2777
}
2778

2779
// updateEdgePolicy attempts to update an edge's policy within the relevant
2780
// buckets using an existing database transaction. The returned boolean will be
2781
// true if the updated policy belongs to node1, and false if the policy belonged
2782
// to node2.
2783
func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy) (
2784
        route.Vertex, route.Vertex, bool, error) {
3✔
2785

3✔
2786
        var noVertex route.Vertex
3✔
2787

3✔
2788
        edges := tx.ReadWriteBucket(edgeBucket)
3✔
2789
        if edges == nil {
3✔
2790
                return noVertex, noVertex, false, ErrEdgeNotFound
×
2791
        }
×
2792
        edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
2793
        if edgeIndex == nil {
3✔
2794
                return noVertex, noVertex, false, ErrEdgeNotFound
×
2795
        }
×
2796

2797
        // Create the channelID key be converting the channel ID
2798
        // integer into a byte slice.
2799
        var chanID [8]byte
3✔
2800
        byteOrder.PutUint64(chanID[:], edge.ChannelID)
3✔
2801

3✔
2802
        // With the channel ID, we then fetch the value storing the two
3✔
2803
        // nodes which connect this channel edge.
3✔
2804
        nodeInfo := edgeIndex.Get(chanID[:])
3✔
2805
        if nodeInfo == nil {
3✔
UNCOV
2806
                return noVertex, noVertex, false, ErrEdgeNotFound
×
UNCOV
2807
        }
×
2808

2809
        // Depending on the flags value passed above, either the first
2810
        // or second edge policy is being updated.
2811
        var fromNode, toNode []byte
3✔
2812
        var isUpdate1 bool
3✔
2813
        if edge.ChannelFlags&lnwire.ChanUpdateDirection == 0 {
6✔
2814
                fromNode = nodeInfo[:33]
3✔
2815
                toNode = nodeInfo[33:66]
3✔
2816
                isUpdate1 = true
3✔
2817
        } else {
6✔
2818
                fromNode = nodeInfo[33:66]
3✔
2819
                toNode = nodeInfo[:33]
3✔
2820
                isUpdate1 = false
3✔
2821
        }
3✔
2822

2823
        // Finally, with the direction of the edge being updated
2824
        // identified, we update the on-disk edge representation.
2825
        err := putChanEdgePolicy(edges, edge, fromNode, toNode)
3✔
2826
        if err != nil {
3✔
2827
                return noVertex, noVertex, false, err
×
2828
        }
×
2829

2830
        var (
3✔
2831
                fromNodePubKey route.Vertex
3✔
2832
                toNodePubKey   route.Vertex
3✔
2833
        )
3✔
2834
        copy(fromNodePubKey[:], fromNode)
3✔
2835
        copy(toNodePubKey[:], toNode)
3✔
2836

3✔
2837
        return fromNodePubKey, toNodePubKey, isUpdate1, nil
3✔
2838
}
2839

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

3✔
2846
        // In order to determine whether this node is publicly advertised within
3✔
2847
        // the graph, we'll need to look at all of its edges and check whether
3✔
2848
        // they extend to any other node than the source node. errDone will be
3✔
2849
        // used to terminate the check early.
3✔
2850
        nodeIsPublic := false
3✔
2851
        errDone := errors.New("done")
3✔
2852
        err := c.forEachNodeChannelTx(tx, nodePub, func(tx kvdb.RTx,
3✔
2853
                info *models.ChannelEdgeInfo, _ *models.ChannelEdgePolicy,
3✔
2854
                _ *models.ChannelEdgePolicy) error {
6✔
2855

3✔
2856
                // If this edge doesn't extend to the source node, we'll
3✔
2857
                // terminate our search as we can now conclude that the node is
3✔
2858
                // publicly advertised within the graph due to the local node
3✔
2859
                // knowing of the current edge.
3✔
2860
                if !bytes.Equal(info.NodeKey1Bytes[:], sourcePubKey) &&
3✔
2861
                        !bytes.Equal(info.NodeKey2Bytes[:], sourcePubKey) {
6✔
2862

3✔
2863
                        nodeIsPublic = true
3✔
2864
                        return errDone
3✔
2865
                }
3✔
2866

2867
                // Since the edge _does_ extend to the source node, we'll also
2868
                // need to ensure that this is a public edge.
2869
                if info.AuthProof != nil {
6✔
2870
                        nodeIsPublic = true
3✔
2871
                        return errDone
3✔
2872
                }
3✔
2873

2874
                // Otherwise, we'll continue our search.
2875
                return nil
3✔
2876
        })
2877
        if err != nil && !errors.Is(err, errDone) {
3✔
2878
                return false, err
×
2879
        }
×
2880

2881
        return nodeIsPublic, nil
3✔
2882
}
2883

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

3✔
2891
        return c.fetchLightningNode(tx, nodePub)
3✔
2892
}
3✔
2893

2894
// FetchLightningNode attempts to look up a target node by its identity public
2895
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
2896
// returned.
2897
func (c *KVStore) FetchLightningNode(nodePub route.Vertex) (
2898
        *models.LightningNode, error) {
3✔
2899

3✔
2900
        return c.fetchLightningNode(nil, nodePub)
3✔
2901
}
3✔
2902

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

3✔
2910
        var node *models.LightningNode
3✔
2911
        fetch := func(tx kvdb.RTx) error {
6✔
2912
                // First grab the nodes bucket which stores the mapping from
3✔
2913
                // pubKey to node information.
3✔
2914
                nodes := tx.ReadBucket(nodeBucket)
3✔
2915
                if nodes == nil {
3✔
2916
                        return ErrGraphNotFound
×
2917
                }
×
2918

2919
                // If a key for this serialized public key isn't found, then
2920
                // the target node doesn't exist within the database.
2921
                nodeBytes := nodes.Get(nodePub[:])
3✔
2922
                if nodeBytes == nil {
6✔
2923
                        return ErrGraphNodeNotFound
3✔
2924
                }
3✔
2925

2926
                // If the node is found, then we can de deserialize the node
2927
                // information to return to the user.
2928
                nodeReader := bytes.NewReader(nodeBytes)
3✔
2929
                n, err := deserializeLightningNode(nodeReader)
3✔
2930
                if err != nil {
3✔
2931
                        return err
×
2932
                }
×
2933

2934
                node = &n
3✔
2935

3✔
2936
                return nil
3✔
2937
        }
2938

2939
        if tx == nil {
6✔
2940
                err := kvdb.View(
3✔
2941
                        c.db, fetch, func() {
6✔
2942
                                node = nil
3✔
2943
                        },
3✔
2944
                )
2945
                if err != nil {
6✔
2946
                        return nil, err
3✔
2947
                }
3✔
2948

2949
                return node, nil
3✔
2950
        }
2951

UNCOV
2952
        err := fetch(tx)
×
UNCOV
2953
        if err != nil {
×
UNCOV
2954
                return nil, err
×
UNCOV
2955
        }
×
2956

UNCOV
2957
        return node, nil
×
2958
}
2959

2960
// HasLightningNode determines if the graph has a vertex identified by the
2961
// target node identity public key. If the node exists in the database, a
2962
// timestamp of when the data for the node was lasted updated is returned along
2963
// with a true boolean. Otherwise, an empty time.Time is returned with a false
2964
// boolean.
2965
func (c *KVStore) HasLightningNode(nodePub [33]byte) (time.Time, bool,
2966
        error) {
3✔
2967

3✔
2968
        var (
3✔
2969
                updateTime time.Time
3✔
2970
                exists     bool
3✔
2971
        )
3✔
2972

3✔
2973
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
2974
                // First grab the nodes bucket which stores the mapping from
3✔
2975
                // pubKey to node information.
3✔
2976
                nodes := tx.ReadBucket(nodeBucket)
3✔
2977
                if nodes == nil {
3✔
2978
                        return ErrGraphNotFound
×
2979
                }
×
2980

2981
                // If a key for this serialized public key isn't found, we can
2982
                // exit early.
2983
                nodeBytes := nodes.Get(nodePub[:])
3✔
2984
                if nodeBytes == nil {
6✔
2985
                        exists = false
3✔
2986
                        return nil
3✔
2987
                }
3✔
2988

2989
                // Otherwise we continue on to obtain the time stamp
2990
                // representing the last time the data for this node was
2991
                // updated.
2992
                nodeReader := bytes.NewReader(nodeBytes)
3✔
2993
                node, err := deserializeLightningNode(nodeReader)
3✔
2994
                if err != nil {
3✔
2995
                        return err
×
2996
                }
×
2997

2998
                exists = true
3✔
2999
                updateTime = node.LastUpdate
3✔
3000

3✔
3001
                return nil
3✔
3002
        }, func() {
3✔
3003
                updateTime = time.Time{}
3✔
3004
                exists = false
3✔
3005
        })
3✔
3006
        if err != nil {
3✔
3007
                return time.Time{}, exists, err
×
3008
        }
×
3009

3010
        return updateTime, exists, nil
3✔
3011
}
3012

3013
// nodeTraversal is used to traverse all channels of a node given by its
3014
// public key and passes channel information into the specified callback.
3015
func nodeTraversal(tx kvdb.RTx, nodePub []byte, db kvdb.Backend,
3016
        cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3017
                *models.ChannelEdgePolicy) error) error {
3✔
3018

3✔
3019
        traversal := func(tx kvdb.RTx) error {
6✔
3020
                edges := tx.ReadBucket(edgeBucket)
3✔
3021
                if edges == nil {
3✔
3022
                        return ErrGraphNotFound
×
3023
                }
×
3024
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
3025
                if edgeIndex == nil {
3✔
3026
                        return ErrGraphNoEdgesFound
×
3027
                }
×
3028

3029
                // In order to reach all the edges for this node, we take
3030
                // advantage of the construction of the key-space within the
3031
                // edge bucket. The keys are stored in the form: pubKey ||
3032
                // chanID. Therefore, starting from a chanID of zero, we can
3033
                // scan forward in the bucket, grabbing all the edges for the
3034
                // node. Once the prefix no longer matches, then we know we're
3035
                // done.
3036
                var nodeStart [33 + 8]byte
3✔
3037
                copy(nodeStart[:], nodePub)
3✔
3038
                copy(nodeStart[33:], chanStart[:])
3✔
3039

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

3056
                        outgoingPolicy, err := fetchChanEdgePolicy(
3✔
3057
                                edges, chanID, nodePub,
3✔
3058
                        )
3✔
3059
                        if err != nil {
3✔
3060
                                return err
×
3061
                        }
×
3062

3063
                        otherNode, err := edgeInfo.OtherNodeKeyBytes(nodePub)
3✔
3064
                        if err != nil {
3✔
3065
                                return err
×
3066
                        }
×
3067

3068
                        incomingPolicy, err := fetchChanEdgePolicy(
3✔
3069
                                edges, chanID, otherNode[:],
3✔
3070
                        )
3✔
3071
                        if err != nil {
3✔
3072
                                return err
×
3073
                        }
×
3074

3075
                        // Finally, we execute the callback.
3076
                        err = cb(tx, &edgeInfo, outgoingPolicy, incomingPolicy)
3✔
3077
                        if err != nil {
6✔
3078
                                return err
3✔
3079
                        }
3✔
3080
                }
3081

3082
                return nil
3✔
3083
        }
3084

3085
        // If no transaction was provided, then we'll create a new transaction
3086
        // to execute the transaction within.
3087
        if tx == nil {
6✔
3088
                return kvdb.View(db, traversal, func() {})
6✔
3089
        }
3090

3091
        // Otherwise, we re-use the existing transaction to execute the graph
3092
        // traversal.
3093
        return traversal(tx)
3✔
3094
}
3095

3096
// ForEachNodeChannel iterates through all channels of the given node,
3097
// executing the passed callback with an edge info structure and the policies
3098
// of each end of the channel. The first edge policy is the outgoing edge *to*
3099
// the connecting node, while the second is the incoming edge *from* the
3100
// connecting node. If the callback returns an error, then the iteration is
3101
// halted with the error propagated back up to the caller.
3102
//
3103
// Unknown policies are passed into the callback as nil values.
3104
func (c *KVStore) ForEachNodeChannel(nodePub route.Vertex,
3105
        cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3106
                *models.ChannelEdgePolicy) error) error {
3✔
3107

3✔
3108
        return nodeTraversal(nil, nodePub[:], c.db, func(_ kvdb.RTx,
3✔
3109
                info *models.ChannelEdgeInfo, policy,
3✔
3110
                policy2 *models.ChannelEdgePolicy) error {
6✔
3111

3✔
3112
                return cb(info, policy, policy2)
3✔
3113
        })
3✔
3114
}
3115

3116
// ForEachSourceNodeChannel iterates through all channels of the source node,
3117
// executing the passed callback on each. The callback is provided with the
3118
// channel's outpoint, whether we have a policy for the channel and the channel
3119
// peer's node information.
3120
func (c *KVStore) ForEachSourceNodeChannel(cb func(chanPoint wire.OutPoint,
3121
        havePolicy bool, otherNode *models.LightningNode) error) error {
3✔
3122

3✔
3123
        return kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
3124
                nodes := tx.ReadBucket(nodeBucket)
3✔
3125
                if nodes == nil {
3✔
3126
                        return ErrGraphNotFound
×
3127
                }
×
3128

3129
                node, err := c.sourceNode(nodes)
3✔
3130
                if err != nil {
3✔
3131
                        return err
×
3132
                }
×
3133

3134
                return nodeTraversal(
3✔
3135
                        tx, node.PubKeyBytes[:], c.db, func(tx kvdb.RTx,
3✔
3136
                                info *models.ChannelEdgeInfo,
3✔
3137
                                policy, _ *models.ChannelEdgePolicy) error {
6✔
3138

3✔
3139
                                peer, err := c.fetchOtherNode(
3✔
3140
                                        tx, info, node.PubKeyBytes[:],
3✔
3141
                                )
3✔
3142
                                if err != nil {
3✔
3143
                                        return err
×
3144
                                }
×
3145

3146
                                return cb(
3✔
3147
                                        info.ChannelPoint, policy != nil, peer,
3✔
3148
                                )
3✔
3149
                        },
3150
                )
3151
        }, func() {})
3✔
3152
}
3153

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

3✔
3172
        return nodeTraversal(tx, nodePub[:], c.db, cb)
3✔
3173
}
3✔
3174

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

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

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

3203
                node, err := fetchLightningNode(nodes, targetNodeBytes[:])
3✔
3204
                if err != nil {
3✔
3205
                        return err
×
3206
                }
×
3207

3208
                targetNode = &node
3✔
3209

3✔
3210
                return nil
3✔
3211
        }
3212

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

3224
        return targetNode, err
3✔
3225
}
3226

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

3✔
3236
        copy(node1Key[:], info.NodeKey1Bytes[:])
3✔
3237
        copy(node2Key[:], info.NodeKey2Bytes[:])
3✔
3238

3✔
3239
        byteOrder.PutUint64(node1Key[33:], info.ChannelID)
3✔
3240
        byteOrder.PutUint64(node2Key[33:], info.ChannelID)
3✔
3241

3✔
3242
        return node1Key[:], node2Key[:]
3✔
3243
}
3✔
3244

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

3✔
3254
        var (
3✔
3255
                edgeInfo *models.ChannelEdgeInfo
3✔
3256
                policy1  *models.ChannelEdgePolicy
3✔
3257
                policy2  *models.ChannelEdgePolicy
3✔
3258
        )
3✔
3259

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

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

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

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

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

3311
                policy1 = e1
3✔
3312
                policy2 = e2
3✔
3313

3✔
3314
                return nil
3✔
3315
        }, func() {
3✔
3316
                edgeInfo = nil
3✔
3317
                policy1 = nil
3✔
3318
                policy2 = nil
3✔
3319
        })
3✔
3320
        if err != nil {
6✔
3321
                return nil, nil, nil, err
3✔
3322
        }
3✔
3323

3324
        return edgeInfo, policy1, policy2, nil
3✔
3325
}
3326

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

3✔
3340
        var (
3✔
3341
                edgeInfo  *models.ChannelEdgeInfo
3✔
3342
                policy1   *models.ChannelEdgePolicy
3✔
3343
                policy2   *models.ChannelEdgePolicy
3✔
3344
                channelID [8]byte
3✔
3345
        )
3✔
3346

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

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

3367
                byteOrder.PutUint64(channelID[:], chanID)
3✔
3368

3✔
3369
                // Now, attempt to fetch edge.
3✔
3370
                edge, err := fetchChanEdgeInfo(edgeIndex, channelID[:])
3✔
3371

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

3383
                        isZombie, pubKey1, pubKey2 := isZombieEdge(
3✔
3384
                                zombieIndex, chanID,
3✔
3385
                        )
3✔
3386
                        if !isZombie {
6✔
3387
                                return ErrEdgeNotFound
3✔
3388
                        }
3✔
3389

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

3✔
3399
                        return ErrZombieEdge
3✔
3400
                }
3401

3402
                // Otherwise, we'll just return the error if any.
3403
                if err != nil {
3✔
3404
                        return err
×
3405
                }
×
3406

3407
                edgeInfo = &edge
3✔
3408

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

3418
                policy1 = e1
3✔
3419
                policy2 = e2
3✔
3420

3✔
3421
                return nil
3✔
3422
        }, func() {
3✔
3423
                edgeInfo = nil
3✔
3424
                policy1 = nil
3✔
3425
                policy2 = nil
3✔
3426
        })
3✔
3427
        if errors.Is(err, ErrZombieEdge) {
6✔
3428
                return edgeInfo, nil, nil, err
3✔
3429
        }
3✔
3430
        if err != nil {
6✔
3431
                return nil, nil, nil, err
3✔
3432
        }
3✔
3433

3434
        return edgeInfo, policy1, policy2, nil
3✔
3435
}
3436

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

3456
                nodeIsPublic, err = c.isPublic(tx, node.PubKeyBytes, ourPubKey)
3✔
3457

3✔
3458
                return err
3✔
3459
        }, func() {
3✔
3460
                nodeIsPublic = false
3✔
3461
        })
3✔
3462
        if err != nil {
3✔
3463
                return false, err
×
3464
        }
×
3465

3466
        return nodeIsPublic, nil
3✔
3467
}
3468

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

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

3✔
3486
        return bldr.Script()
3✔
3487
}
3488

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

3497
        // OutPoint is the outpoint of the target channel.
3498
        OutPoint wire.OutPoint
3499
}
3500

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

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

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

3✔
3539
                                var chanPoint wire.OutPoint
3✔
3540
                                err := ReadOutpoint(chanPointReader, &chanPoint)
3✔
3541
                                if err != nil {
3✔
3542
                                        return err
×
3543
                                }
×
3544

3545
                                edgeInfo, err := fetchChanEdgeInfo(
3✔
3546
                                        edgeIndex, chanID,
3✔
3547
                                )
3✔
3548
                                if err != nil {
3✔
3549
                                        return err
×
3550
                                }
×
3551

3552
                                pkScript, err := genMultiSigP2WSH(
3✔
3553
                                        edgeInfo.BitcoinKey1Bytes[:],
3✔
3554
                                        edgeInfo.BitcoinKey2Bytes[:],
3✔
3555
                                )
3✔
3556
                                if err != nil {
3✔
3557
                                        return err
×
3558
                                }
×
3559

3560
                                edgePoints = append(edgePoints, EdgePoint{
3✔
3561
                                        FundingPkScript: pkScript,
3✔
3562
                                        OutPoint:        chanPoint,
3✔
3563
                                })
3✔
3564

3✔
3565
                                return nil
3✔
3566
                        },
3567
                )
3568
        }, func() {
3✔
3569
                edgePoints = nil
3✔
3570
        }); err != nil {
3✔
3571
                return nil, err
×
3572
        }
×
3573

3574
        return edgePoints, nil
3✔
3575
}
3576

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

×
UNCOV
3583
        c.cacheMu.Lock()
×
UNCOV
3584
        defer c.cacheMu.Unlock()
×
UNCOV
3585

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

UNCOV
3597
                return markEdgeZombie(zombieIndex, chanID, pubKey1, pubKey2)
×
3598
        })
UNCOV
3599
        if err != nil {
×
3600
                return err
×
3601
        }
×
3602

UNCOV
3603
        c.rejectCache.remove(chanID)
×
UNCOV
3604
        c.chanCache.remove(chanID)
×
UNCOV
3605

×
UNCOV
3606
        return nil
×
3607
}
3608

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

3✔
3615
        var k [8]byte
3✔
3616
        byteOrder.PutUint64(k[:], chanID)
3✔
3617

3✔
3618
        var v [66]byte
3✔
3619
        copy(v[:33], pubKey1[:])
3✔
3620
        copy(v[33:], pubKey2[:])
3✔
3621

3✔
3622
        return zombieIndex.Put(k[:], v[:])
3✔
3623
}
3✔
3624

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

×
UNCOV
3630
        return c.markEdgeLiveUnsafe(nil, chanID)
×
UNCOV
3631
}
×
3632

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

UNCOV
3650
                var k [8]byte
×
UNCOV
3651
                byteOrder.PutUint64(k[:], chanID)
×
UNCOV
3652

×
UNCOV
3653
                if len(zombieIndex.Get(k[:])) == 0 {
×
UNCOV
3654
                        return ErrZombieEdgeNotFound
×
UNCOV
3655
                }
×
3656

UNCOV
3657
                return zombieIndex.Delete(k[:])
×
3658
        }
3659

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

UNCOV
3672
        c.rejectCache.remove(chanID)
×
UNCOV
3673
        c.chanCache.remove(chanID)
×
UNCOV
3674

×
UNCOV
3675
        return nil
×
3676
}
3677

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

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

UNCOV
3697
                isZombie, pubKey1, pubKey2 = isZombieEdge(zombieIndex, chanID)
×
UNCOV
3698

×
UNCOV
3699
                return nil
×
UNCOV
3700
        }, func() {
×
UNCOV
3701
                isZombie = false
×
UNCOV
3702
                pubKey1 = [33]byte{}
×
UNCOV
3703
                pubKey2 = [33]byte{}
×
UNCOV
3704
        })
×
UNCOV
3705
        if err != nil {
×
3706
                return false, [33]byte{}, [33]byte{}
×
3707
        }
×
3708

UNCOV
3709
        return isZombie, pubKey1, pubKey2
×
3710
}
3711

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

3✔
3718
        var k [8]byte
3✔
3719
        byteOrder.PutUint64(k[:], chanID)
3✔
3720

3✔
3721
        v := zombieIndex.Get(k[:])
3✔
3722
        if v == nil {
6✔
3723
                return false, [33]byte{}, [33]byte{}
3✔
3724
        }
3✔
3725

3726
        var pubKey1, pubKey2 [33]byte
3✔
3727
        copy(pubKey1[:], v[:33])
3✔
3728
        copy(pubKey2[:], v[33:])
3✔
3729

3✔
3730
        return true, pubKey1, pubKey2
3✔
3731
}
3732

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

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

UNCOV
3757
        return numZombies, nil
×
3758
}
3759

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

UNCOV
3770
                var k [8]byte
×
UNCOV
3771
                byteOrder.PutUint64(k[:], scid.ToUint64())
×
UNCOV
3772

×
UNCOV
3773
                return closedScids.Put(k[:], []byte{})
×
UNCOV
3774
        }, func() {})
×
3775
}
3776

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

3788
                var k [8]byte
3✔
3789
                byteOrder.PutUint64(k[:], scid.ToUint64())
3✔
3790

3✔
3791
                if closedScids.Get(k[:]) != nil {
3✔
UNCOV
3792
                        isClosed = true
×
UNCOV
3793
                        return nil
×
UNCOV
3794
                }
×
3795

3796
                return nil
3✔
3797
        }, func() {
3✔
3798
                isClosed = false
3✔
3799
        })
3✔
3800
        if err != nil {
3✔
3801
                return false, err
×
3802
        }
×
3803

3804
        return isClosed, nil
3✔
3805
}
3806

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

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

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

×
UNCOV
3832
        return c.db.forEachNodeDirectedChannel(c.tx, nodePub, cb)
×
UNCOV
3833
}
×
3834

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

×
UNCOV
3842
        return c.db.fetchNodeFeatures(c.tx, nodePub)
×
UNCOV
3843
}
×
3844

3845
func putLightningNode(nodeBucket, aliasBucket, updateIndex kvdb.RwBucket,
3846
        node *models.LightningNode) error {
3✔
3847

3✔
3848
        var (
3✔
3849
                scratch [16]byte
3✔
3850
                b       bytes.Buffer
3✔
3851
        )
3✔
3852

3✔
3853
        pub, err := node.PubKey()
3✔
3854
        if err != nil {
3✔
3855
                return err
×
3856
        }
×
3857
        nodePub := pub.SerializeCompressed()
3✔
3858

3✔
3859
        // If the node has the update time set, write it, else write 0.
3✔
3860
        updateUnix := uint64(0)
3✔
3861
        if node.LastUpdate.Unix() > 0 {
6✔
3862
                updateUnix = uint64(node.LastUpdate.Unix())
3✔
3863
        }
3✔
3864

3865
        byteOrder.PutUint64(scratch[:8], updateUnix)
3✔
3866
        if _, err := b.Write(scratch[:8]); err != nil {
3✔
3867
                return err
×
3868
        }
×
3869

3870
        if _, err := b.Write(nodePub); err != nil {
3✔
3871
                return err
×
3872
        }
×
3873

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

3883
                return nodeBucket.Put(nodePub, b.Bytes())
3✔
3884
        }
3885

3886
        // Write HaveNodeAnnouncement=1.
3887
        byteOrder.PutUint16(scratch[:2], 1)
3✔
3888
        if _, err := b.Write(scratch[:2]); err != nil {
3✔
3889
                return err
×
3890
        }
×
3891

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

3902
        if err := wire.WriteVarString(&b, 0, node.Alias); err != nil {
3✔
3903
                return err
×
3904
        }
×
3905

3906
        if err := node.Features.Encode(&b); err != nil {
3✔
3907
                return err
×
3908
        }
×
3909

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

3916
        for _, address := range node.Addresses {
6✔
3917
                if err := SerializeAddr(&b, address); err != nil {
3✔
3918
                        return err
×
3919
                }
×
3920
        }
3921

3922
        sigLen := len(node.AuthSigBytes)
3✔
3923
        if sigLen > 80 {
3✔
3924
                return fmt.Errorf("max sig len allowed is 80, had %v",
×
3925
                        sigLen)
×
3926
        }
×
3927

3928
        err = wire.WriteVarBytes(&b, 0, node.AuthSigBytes)
3✔
3929
        if err != nil {
3✔
3930
                return err
×
3931
        }
×
3932

3933
        if len(node.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
3✔
3934
                return ErrTooManyExtraOpaqueBytes(len(node.ExtraOpaqueData))
×
3935
        }
×
3936
        err = wire.WriteVarBytes(&b, 0, node.ExtraOpaqueData)
3✔
3937
        if err != nil {
3✔
3938
                return err
×
3939
        }
×
3940

3941
        if err := aliasBucket.Put(nodePub, []byte(node.Alias)); err != nil {
3✔
3942
                return err
×
3943
        }
×
3944

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

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

3✔
3958
                var oldIndexKey [8 + 33]byte
3✔
3959
                copy(oldIndexKey[:8], oldUpdateTime)
3✔
3960
                copy(oldIndexKey[8:], nodePub)
3✔
3961

3✔
3962
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
3✔
3963
                        return err
×
3964
                }
×
3965
        }
3966

3967
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
3✔
3968
                return err
×
3969
        }
×
3970

3971
        return nodeBucket.Put(nodePub, b.Bytes())
3✔
3972
}
3973

3974
func fetchLightningNode(nodeBucket kvdb.RBucket,
3975
        nodePub []byte) (models.LightningNode, error) {
3✔
3976

3✔
3977
        nodeBytes := nodeBucket.Get(nodePub)
3✔
3978
        if nodeBytes == nil {
6✔
3979
                return models.LightningNode{}, ErrGraphNodeNotFound
3✔
3980
        }
3✔
3981

3982
        nodeReader := bytes.NewReader(nodeBytes)
3✔
3983

3✔
3984
        return deserializeLightningNode(nodeReader)
3✔
3985
}
3986

3987
func deserializeLightningNodeCacheable(r io.Reader) (route.Vertex,
3988
        *lnwire.FeatureVector, error) {
3✔
3989

3✔
3990
        var (
3✔
3991
                pubKey      route.Vertex
3✔
3992
                features    = lnwire.EmptyFeatureVector()
3✔
3993
                nodeScratch [8]byte
3✔
3994
        )
3✔
3995

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

4002
        if _, err := io.ReadFull(r, pubKey[:]); err != nil {
3✔
4003
                return pubKey, nil, err
×
4004
        }
×
4005

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

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

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

4031
        if _, err := wire.ReadVarString(r, 0); err != nil {
3✔
4032
                return pubKey, nil, err
×
4033
        }
×
4034

4035
        if err := features.Decode(r); err != nil {
3✔
4036
                return pubKey, nil, err
×
4037
        }
×
4038

4039
        return pubKey, features, nil
3✔
4040
}
4041

4042
func deserializeLightningNode(r io.Reader) (models.LightningNode, error) {
3✔
4043
        var (
3✔
4044
                node    models.LightningNode
3✔
4045
                scratch [8]byte
3✔
4046
                err     error
3✔
4047
        )
3✔
4048

3✔
4049
        // Always populate a feature vector, even if we don't have a node
3✔
4050
        // announcement and short circuit below.
3✔
4051
        node.Features = lnwire.EmptyFeatureVector()
3✔
4052

3✔
4053
        if _, err := r.Read(scratch[:]); err != nil {
3✔
4054
                return models.LightningNode{}, err
×
4055
        }
×
4056

4057
        unix := int64(byteOrder.Uint64(scratch[:]))
3✔
4058
        node.LastUpdate = time.Unix(unix, 0)
3✔
4059

3✔
4060
        if _, err := io.ReadFull(r, node.PubKeyBytes[:]); err != nil {
3✔
4061
                return models.LightningNode{}, err
×
4062
        }
×
4063

4064
        if _, err := r.Read(scratch[:2]); err != nil {
3✔
4065
                return models.LightningNode{}, err
×
4066
        }
×
4067

4068
        hasNodeAnn := byteOrder.Uint16(scratch[:2])
3✔
4069
        if hasNodeAnn == 1 {
6✔
4070
                node.HaveNodeAnnouncement = true
3✔
4071
        } else {
6✔
4072
                node.HaveNodeAnnouncement = false
3✔
4073
        }
3✔
4074

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

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

4093
        node.Alias, err = wire.ReadVarString(r, 0)
3✔
4094
        if err != nil {
3✔
4095
                return models.LightningNode{}, err
×
4096
        }
×
4097

4098
        err = node.Features.Decode(r)
3✔
4099
        if err != nil {
3✔
4100
                return models.LightningNode{}, err
×
4101
        }
×
4102

4103
        if _, err := r.Read(scratch[:2]); err != nil {
3✔
4104
                return models.LightningNode{}, err
×
4105
        }
×
4106
        numAddresses := int(byteOrder.Uint16(scratch[:2]))
3✔
4107

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

3✔
4118
        node.AuthSigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
3✔
4119
        if err != nil {
3✔
4120
                return models.LightningNode{}, err
×
4121
        }
×
4122

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

4135
        if len(extraBytes) > 0 {
3✔
UNCOV
4136
                node.ExtraOpaqueData = extraBytes
×
UNCOV
4137
        }
×
4138

4139
        return node, nil
3✔
4140
}
4141

4142
func putChanEdgeInfo(edgeIndex kvdb.RwBucket,
4143
        edgeInfo *models.ChannelEdgeInfo, chanID [8]byte) error {
3✔
4144

3✔
4145
        var b bytes.Buffer
3✔
4146

3✔
4147
        if _, err := b.Write(edgeInfo.NodeKey1Bytes[:]); err != nil {
3✔
4148
                return err
×
4149
        }
×
4150
        if _, err := b.Write(edgeInfo.NodeKey2Bytes[:]); err != nil {
3✔
4151
                return err
×
4152
        }
×
4153
        if _, err := b.Write(edgeInfo.BitcoinKey1Bytes[:]); err != nil {
3✔
4154
                return err
×
4155
        }
×
4156
        if _, err := b.Write(edgeInfo.BitcoinKey2Bytes[:]); err != nil {
3✔
4157
                return err
×
4158
        }
×
4159

4160
        if err := wire.WriteVarBytes(&b, 0, edgeInfo.Features); err != nil {
3✔
4161
                return err
×
4162
        }
×
4163

4164
        authProof := edgeInfo.AuthProof
3✔
4165
        var nodeSig1, nodeSig2, bitcoinSig1, bitcoinSig2 []byte
3✔
4166
        if authProof != nil {
6✔
4167
                nodeSig1 = authProof.NodeSig1Bytes
3✔
4168
                nodeSig2 = authProof.NodeSig2Bytes
3✔
4169
                bitcoinSig1 = authProof.BitcoinSig1Bytes
3✔
4170
                bitcoinSig2 = authProof.BitcoinSig2Bytes
3✔
4171
        }
3✔
4172

4173
        if err := wire.WriteVarBytes(&b, 0, nodeSig1); err != nil {
3✔
4174
                return err
×
4175
        }
×
4176
        if err := wire.WriteVarBytes(&b, 0, nodeSig2); err != nil {
3✔
4177
                return err
×
4178
        }
×
4179
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig1); err != nil {
3✔
4180
                return err
×
4181
        }
×
4182
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig2); err != nil {
3✔
4183
                return err
×
4184
        }
×
4185

4186
        if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
3✔
4187
                return err
×
4188
        }
×
4189
        err := binary.Write(&b, byteOrder, uint64(edgeInfo.Capacity))
3✔
4190
        if err != nil {
3✔
4191
                return err
×
4192
        }
×
4193
        if _, err := b.Write(chanID[:]); err != nil {
3✔
4194
                return err
×
4195
        }
×
4196
        if _, err := b.Write(edgeInfo.ChainHash[:]); err != nil {
3✔
4197
                return err
×
4198
        }
×
4199

4200
        if len(edgeInfo.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
3✔
4201
                return ErrTooManyExtraOpaqueBytes(len(edgeInfo.ExtraOpaqueData))
×
4202
        }
×
4203
        err = wire.WriteVarBytes(&b, 0, edgeInfo.ExtraOpaqueData)
3✔
4204
        if err != nil {
3✔
4205
                return err
×
4206
        }
×
4207

4208
        return edgeIndex.Put(chanID[:], b.Bytes())
3✔
4209
}
4210

4211
func fetchChanEdgeInfo(edgeIndex kvdb.RBucket,
4212
        chanID []byte) (models.ChannelEdgeInfo, error) {
3✔
4213

3✔
4214
        edgeInfoBytes := edgeIndex.Get(chanID)
3✔
4215
        if edgeInfoBytes == nil {
6✔
4216
                return models.ChannelEdgeInfo{}, ErrEdgeNotFound
3✔
4217
        }
3✔
4218

4219
        edgeInfoReader := bytes.NewReader(edgeInfoBytes)
3✔
4220

3✔
4221
        return deserializeChanEdgeInfo(edgeInfoReader)
3✔
4222
}
4223

4224
func deserializeChanEdgeInfo(r io.Reader) (models.ChannelEdgeInfo, error) {
3✔
4225
        var (
3✔
4226
                err      error
3✔
4227
                edgeInfo models.ChannelEdgeInfo
3✔
4228
        )
3✔
4229

3✔
4230
        if _, err := io.ReadFull(r, edgeInfo.NodeKey1Bytes[:]); err != nil {
3✔
4231
                return models.ChannelEdgeInfo{}, err
×
4232
        }
×
4233
        if _, err := io.ReadFull(r, edgeInfo.NodeKey2Bytes[:]); err != nil {
3✔
4234
                return models.ChannelEdgeInfo{}, err
×
4235
        }
×
4236
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey1Bytes[:]); err != nil {
3✔
4237
                return models.ChannelEdgeInfo{}, err
×
4238
        }
×
4239
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey2Bytes[:]); err != nil {
3✔
4240
                return models.ChannelEdgeInfo{}, err
×
4241
        }
×
4242

4243
        edgeInfo.Features, err = wire.ReadVarBytes(r, 0, 900, "features")
3✔
4244
        if err != nil {
3✔
4245
                return models.ChannelEdgeInfo{}, err
×
4246
        }
×
4247

4248
        proof := &models.ChannelAuthProof{}
3✔
4249

3✔
4250
        proof.NodeSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4251
        if err != nil {
3✔
4252
                return models.ChannelEdgeInfo{}, err
×
4253
        }
×
4254
        proof.NodeSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4255
        if err != nil {
3✔
4256
                return models.ChannelEdgeInfo{}, err
×
4257
        }
×
4258
        proof.BitcoinSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4259
        if err != nil {
3✔
4260
                return models.ChannelEdgeInfo{}, err
×
4261
        }
×
4262
        proof.BitcoinSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4263
        if err != nil {
3✔
4264
                return models.ChannelEdgeInfo{}, err
×
4265
        }
×
4266

4267
        if !proof.IsEmpty() {
6✔
4268
                edgeInfo.AuthProof = proof
3✔
4269
        }
3✔
4270

4271
        edgeInfo.ChannelPoint = wire.OutPoint{}
3✔
4272
        if err := ReadOutpoint(r, &edgeInfo.ChannelPoint); err != nil {
3✔
4273
                return models.ChannelEdgeInfo{}, err
×
4274
        }
×
4275
        if err := binary.Read(r, byteOrder, &edgeInfo.Capacity); err != nil {
3✔
4276
                return models.ChannelEdgeInfo{}, err
×
4277
        }
×
4278
        if err := binary.Read(r, byteOrder, &edgeInfo.ChannelID); err != nil {
3✔
4279
                return models.ChannelEdgeInfo{}, err
×
4280
        }
×
4281

4282
        if _, err := io.ReadFull(r, edgeInfo.ChainHash[:]); err != nil {
3✔
4283
                return models.ChannelEdgeInfo{}, err
×
4284
        }
×
4285

4286
        // We'll try and see if there are any opaque bytes left, if not, then
4287
        // we'll ignore the EOF error and return the edge as is.
4288
        edgeInfo.ExtraOpaqueData, err = wire.ReadVarBytes(
3✔
4289
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
3✔
4290
        )
3✔
4291
        switch {
3✔
4292
        case errors.Is(err, io.ErrUnexpectedEOF):
×
4293
        case errors.Is(err, io.EOF):
×
4294
        case err != nil:
×
4295
                return models.ChannelEdgeInfo{}, err
×
4296
        }
4297

4298
        return edgeInfo, nil
3✔
4299
}
4300

4301
func putChanEdgePolicy(edges kvdb.RwBucket, edge *models.ChannelEdgePolicy,
4302
        from, to []byte) error {
3✔
4303

3✔
4304
        var edgeKey [33 + 8]byte
3✔
4305
        copy(edgeKey[:], from)
3✔
4306
        byteOrder.PutUint64(edgeKey[33:], edge.ChannelID)
3✔
4307

3✔
4308
        var b bytes.Buffer
3✔
4309
        if err := serializeChanEdgePolicy(&b, edge, to); err != nil {
3✔
4310
                return err
×
4311
        }
×
4312

4313
        // Before we write out the new edge, we'll create a new entry in the
4314
        // update index in order to keep it fresh.
4315
        updateUnix := uint64(edge.LastUpdate.Unix())
3✔
4316
        var indexKey [8 + 8]byte
3✔
4317
        byteOrder.PutUint64(indexKey[:8], updateUnix)
3✔
4318
        byteOrder.PutUint64(indexKey[8:], edge.ChannelID)
3✔
4319

3✔
4320
        updateIndex, err := edges.CreateBucketIfNotExists(edgeUpdateIndexBucket)
3✔
4321
        if err != nil {
3✔
4322
                return err
×
4323
        }
×
4324

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

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

×
4347
                        return err
×
4348
                }
×
4349

4350
                oldUpdateTime := uint64(oldEdgePolicy.LastUpdate.Unix())
3✔
4351

3✔
4352
                var oldIndexKey [8 + 8]byte
3✔
4353
                byteOrder.PutUint64(oldIndexKey[:8], oldUpdateTime)
3✔
4354
                byteOrder.PutUint64(oldIndexKey[8:], edge.ChannelID)
3✔
4355

3✔
4356
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
3✔
4357
                        return err
×
4358
                }
×
4359
        }
4360

4361
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
3✔
4362
                return err
×
4363
        }
×
4364

4365
        err = updateEdgePolicyDisabledIndex(
3✔
4366
                edges, edge.ChannelID,
3✔
4367
                edge.ChannelFlags&lnwire.ChanUpdateDirection > 0,
3✔
4368
                edge.IsDisabled(),
3✔
4369
        )
3✔
4370
        if err != nil {
3✔
4371
                return err
×
4372
        }
×
4373

4374
        return edges.Put(edgeKey[:], b.Bytes())
3✔
4375
}
4376

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

3✔
4389
        var disabledEdgeKey [8 + 1]byte
3✔
4390
        byteOrder.PutUint64(disabledEdgeKey[0:], chanID)
3✔
4391
        if direction {
6✔
4392
                disabledEdgeKey[8] = 1
3✔
4393
        }
3✔
4394

4395
        disabledEdgePolicyIndex, err := edges.CreateBucketIfNotExists(
3✔
4396
                disabledEdgePolicyBucket,
3✔
4397
        )
3✔
4398
        if err != nil {
3✔
4399
                return err
×
4400
        }
×
4401

4402
        if disabled {
6✔
4403
                return disabledEdgePolicyIndex.Put(disabledEdgeKey[:], []byte{})
3✔
4404
        }
3✔
4405

4406
        return disabledEdgePolicyIndex.Delete(disabledEdgeKey[:])
3✔
4407
}
4408

4409
// putChanEdgePolicyUnknown marks the edge policy as unknown
4410
// in the edges bucket.
4411
func putChanEdgePolicyUnknown(edges kvdb.RwBucket, channelID uint64,
4412
        from []byte) error {
3✔
4413

3✔
4414
        var edgeKey [33 + 8]byte
3✔
4415
        copy(edgeKey[:], from)
3✔
4416
        byteOrder.PutUint64(edgeKey[33:], channelID)
3✔
4417

3✔
4418
        if edges.Get(edgeKey[:]) != nil {
3✔
4419
                return fmt.Errorf("cannot write unknown policy for channel %v "+
×
4420
                        " when there is already a policy present", channelID)
×
4421
        }
×
4422

4423
        return edges.Put(edgeKey[:], unknownPolicy)
3✔
4424
}
4425

4426
func fetchChanEdgePolicy(edges kvdb.RBucket, chanID []byte,
4427
        nodePub []byte) (*models.ChannelEdgePolicy, error) {
3✔
4428

3✔
4429
        var edgeKey [33 + 8]byte
3✔
4430
        copy(edgeKey[:], nodePub)
3✔
4431
        copy(edgeKey[33:], chanID)
3✔
4432

3✔
4433
        edgeBytes := edges.Get(edgeKey[:])
3✔
4434
        if edgeBytes == nil {
3✔
4435
                return nil, ErrEdgeNotFound
×
4436
        }
×
4437

4438
        // No need to deserialize unknown policy.
4439
        if bytes.Equal(edgeBytes, unknownPolicy) {
6✔
4440
                return nil, nil
3✔
4441
        }
3✔
4442

4443
        edgeReader := bytes.NewReader(edgeBytes)
3✔
4444

3✔
4445
        ep, err := deserializeChanEdgePolicy(edgeReader)
3✔
4446
        switch {
3✔
4447
        // If the db policy was missing an expected optional field, we return
4448
        // nil as if the policy was unknown.
UNCOV
4449
        case errors.Is(err, ErrEdgePolicyOptionalFieldNotFound):
×
UNCOV
4450
                return nil, nil
×
4451

4452
        case err != nil:
×
4453
                return nil, err
×
4454
        }
4455

4456
        return ep, nil
3✔
4457
}
4458

4459
func fetchChanEdgePolicies(edgeIndex kvdb.RBucket, edges kvdb.RBucket,
4460
        chanID []byte) (*models.ChannelEdgePolicy, *models.ChannelEdgePolicy,
4461
        error) {
3✔
4462

3✔
4463
        edgeInfo := edgeIndex.Get(chanID)
3✔
4464
        if edgeInfo == nil {
3✔
4465
                return nil, nil, fmt.Errorf("%w: chanID=%x", ErrEdgeNotFound,
×
4466
                        chanID)
×
4467
        }
×
4468

4469
        // The first node is contained within the first half of the edge
4470
        // information. We only propagate the error here and below if it's
4471
        // something other than edge non-existence.
4472
        node1Pub := edgeInfo[:33]
3✔
4473
        edge1, err := fetchChanEdgePolicy(edges, chanID, node1Pub)
3✔
4474
        if err != nil {
3✔
4475
                return nil, nil, fmt.Errorf("%w: node1Pub=%x", ErrEdgeNotFound,
×
4476
                        node1Pub)
×
4477
        }
×
4478

4479
        // Similarly, the second node is contained within the latter
4480
        // half of the edge information.
4481
        node2Pub := edgeInfo[33:66]
3✔
4482
        edge2, err := fetchChanEdgePolicy(edges, chanID, node2Pub)
3✔
4483
        if err != nil {
3✔
4484
                return nil, nil, fmt.Errorf("%w: node2Pub=%x", ErrEdgeNotFound,
×
4485
                        node2Pub)
×
4486
        }
×
4487

4488
        return edge1, edge2, nil
3✔
4489
}
4490

4491
func serializeChanEdgePolicy(w io.Writer, edge *models.ChannelEdgePolicy,
4492
        to []byte) error {
3✔
4493

3✔
4494
        err := wire.WriteVarBytes(w, 0, edge.SigBytes)
3✔
4495
        if err != nil {
3✔
4496
                return err
×
4497
        }
×
4498

4499
        if err := binary.Write(w, byteOrder, edge.ChannelID); err != nil {
3✔
4500
                return err
×
4501
        }
×
4502

4503
        var scratch [8]byte
3✔
4504
        updateUnix := uint64(edge.LastUpdate.Unix())
3✔
4505
        byteOrder.PutUint64(scratch[:], updateUnix)
3✔
4506
        if _, err := w.Write(scratch[:]); err != nil {
3✔
4507
                return err
×
4508
        }
×
4509

4510
        if err := binary.Write(w, byteOrder, edge.MessageFlags); err != nil {
3✔
4511
                return err
×
4512
        }
×
4513
        if err := binary.Write(w, byteOrder, edge.ChannelFlags); err != nil {
3✔
4514
                return err
×
4515
        }
×
4516
        if err := binary.Write(w, byteOrder, edge.TimeLockDelta); err != nil {
3✔
4517
                return err
×
4518
        }
×
4519
        if err := binary.Write(w, byteOrder, uint64(edge.MinHTLC)); err != nil {
3✔
4520
                return err
×
4521
        }
×
4522
        err = binary.Write(w, byteOrder, uint64(edge.FeeBaseMSat))
3✔
4523
        if err != nil {
3✔
4524
                return err
×
4525
        }
×
4526
        err = binary.Write(
3✔
4527
                w, byteOrder, uint64(edge.FeeProportionalMillionths),
3✔
4528
        )
3✔
4529
        if err != nil {
3✔
4530
                return err
×
4531
        }
×
4532

4533
        if _, err := w.Write(to); err != nil {
3✔
4534
                return err
×
4535
        }
×
4536

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

4549
        if len(edge.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
3✔
4550
                return ErrTooManyExtraOpaqueBytes(len(edge.ExtraOpaqueData))
×
4551
        }
×
4552
        if _, err := opaqueBuf.Write(edge.ExtraOpaqueData); err != nil {
3✔
4553
                return err
×
4554
        }
×
4555

4556
        if err := wire.WriteVarBytes(w, 0, opaqueBuf.Bytes()); err != nil {
3✔
4557
                return err
×
4558
        }
×
4559

4560
        return nil
3✔
4561
}
4562

4563
func deserializeChanEdgePolicy(r io.Reader) (*models.ChannelEdgePolicy, error) {
3✔
4564
        // Deserialize the policy. Note that in case an optional field is not
3✔
4565
        // found, both an error and a populated policy object are returned.
3✔
4566
        edge, deserializeErr := deserializeChanEdgePolicyRaw(r)
3✔
4567
        if deserializeErr != nil &&
3✔
4568
                !errors.Is(deserializeErr, ErrEdgePolicyOptionalFieldNotFound) {
3✔
4569

×
4570
                return nil, deserializeErr
×
4571
        }
×
4572

4573
        return edge, deserializeErr
3✔
4574
}
4575

4576
func deserializeChanEdgePolicyRaw(r io.Reader) (*models.ChannelEdgePolicy,
4577
        error) {
3✔
4578

3✔
4579
        edge := &models.ChannelEdgePolicy{}
3✔
4580

3✔
4581
        var err error
3✔
4582
        edge.SigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
3✔
4583
        if err != nil {
3✔
4584
                return nil, err
×
4585
        }
×
4586

4587
        if err := binary.Read(r, byteOrder, &edge.ChannelID); err != nil {
3✔
4588
                return nil, err
×
4589
        }
×
4590

4591
        var scratch [8]byte
3✔
4592
        if _, err := r.Read(scratch[:]); err != nil {
3✔
4593
                return nil, err
×
4594
        }
×
4595
        unix := int64(byteOrder.Uint64(scratch[:]))
3✔
4596
        edge.LastUpdate = time.Unix(unix, 0)
3✔
4597

3✔
4598
        if err := binary.Read(r, byteOrder, &edge.MessageFlags); err != nil {
3✔
4599
                return nil, err
×
4600
        }
×
4601
        if err := binary.Read(r, byteOrder, &edge.ChannelFlags); err != nil {
3✔
4602
                return nil, err
×
4603
        }
×
4604
        if err := binary.Read(r, byteOrder, &edge.TimeLockDelta); err != nil {
3✔
4605
                return nil, err
×
4606
        }
×
4607

4608
        var n uint64
3✔
4609
        if err := binary.Read(r, byteOrder, &n); err != nil {
3✔
4610
                return nil, err
×
4611
        }
×
4612
        edge.MinHTLC = lnwire.MilliSatoshi(n)
3✔
4613

3✔
4614
        if err := binary.Read(r, byteOrder, &n); err != nil {
3✔
4615
                return nil, err
×
4616
        }
×
4617
        edge.FeeBaseMSat = lnwire.MilliSatoshi(n)
3✔
4618

3✔
4619
        if err := binary.Read(r, byteOrder, &n); err != nil {
3✔
4620
                return nil, err
×
4621
        }
×
4622
        edge.FeeProportionalMillionths = lnwire.MilliSatoshi(n)
3✔
4623

3✔
4624
        if _, err := r.Read(edge.ToNode[:]); err != nil {
3✔
4625
                return nil, err
×
4626
        }
×
4627

4628
        // We'll try and see if there are any opaque bytes left, if not, then
4629
        // we'll ignore the EOF error and return the edge as is.
4630
        edge.ExtraOpaqueData, err = wire.ReadVarBytes(
3✔
4631
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
3✔
4632
        )
3✔
4633
        switch {
3✔
4634
        case errors.Is(err, io.ErrUnexpectedEOF):
×
UNCOV
4635
        case errors.Is(err, io.EOF):
×
4636
        case err != nil:
×
4637
                return nil, err
×
4638
        }
4639

4640
        // See if optional fields are present.
4641
        if edge.MessageFlags.HasMaxHtlc() {
6✔
4642
                // The max_htlc field should be at the beginning of the opaque
3✔
4643
                // bytes.
3✔
4644
                opq := edge.ExtraOpaqueData
3✔
4645

3✔
4646
                // If the max_htlc field is not present, it might be old data
3✔
4647
                // stored before this field was validated. We'll return the
3✔
4648
                // edge along with an error.
3✔
4649
                if len(opq) < 8 {
3✔
UNCOV
4650
                        return edge, ErrEdgePolicyOptionalFieldNotFound
×
UNCOV
4651
                }
×
4652

4653
                maxHtlc := byteOrder.Uint64(opq[:8])
3✔
4654
                edge.MaxHTLC = lnwire.MilliSatoshi(maxHtlc)
3✔
4655

3✔
4656
                // Exclude the parsed field from the rest of the opaque data.
3✔
4657
                edge.ExtraOpaqueData = opq[8:]
3✔
4658
        }
4659

4660
        return edge, nil
3✔
4661
}
4662

4663
// chanGraphNodeTx is an implementation of the NodeRTx interface backed by the
4664
// KVStore and a kvdb.RTx.
4665
type chanGraphNodeTx struct {
4666
        tx   kvdb.RTx
4667
        db   *KVStore
4668
        node *models.LightningNode
4669
}
4670

4671
// A compile-time constraint to ensure chanGraphNodeTx implements the NodeRTx
4672
// interface.
4673
var _ NodeRTx = (*chanGraphNodeTx)(nil)
4674

4675
func newChanGraphNodeTx(tx kvdb.RTx, db *KVStore,
4676
        node *models.LightningNode) *chanGraphNodeTx {
3✔
4677

3✔
4678
        return &chanGraphNodeTx{
3✔
4679
                tx:   tx,
3✔
4680
                db:   db,
3✔
4681
                node: node,
3✔
4682
        }
3✔
4683
}
3✔
4684

4685
// Node returns the raw information of the node.
4686
//
4687
// NOTE: This is a part of the NodeRTx interface.
4688
func (c *chanGraphNodeTx) Node() *models.LightningNode {
3✔
4689
        return c.node
3✔
4690
}
3✔
4691

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

UNCOV
4703
        return newChanGraphNodeTx(c.tx, c.db, node), nil
×
4704
}
4705

4706
// ForEachChannel can be used to iterate over the node's channels under
4707
// the same transaction used to fetch the node.
4708
//
4709
// NOTE: This is a part of the NodeRTx interface.
4710
func (c *chanGraphNodeTx) ForEachChannel(f func(*models.ChannelEdgeInfo,
UNCOV
4711
        *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
×
UNCOV
4712

×
UNCOV
4713
        return c.db.forEachNodeChannelTx(c.tx, c.node.PubKeyBytes,
×
UNCOV
4714
                func(_ kvdb.RTx, info *models.ChannelEdgeInfo, policy1,
×
UNCOV
4715
                        policy2 *models.ChannelEdgePolicy) error {
×
UNCOV
4716

×
UNCOV
4717
                        return f(info, policy1, policy2)
×
UNCOV
4718
                },
×
4719
        )
4720
}
4721

4722
// MakeTestGraph creates a new instance of the ChannelGraph for testing
4723
// purposes.
4724
//
4725
// NOTE: this helper currently creates a ChannelGraph that is only ever backed
4726
// by the `KVStore` of the `V1Store` interface.
UNCOV
4727
func MakeTestGraph(t testing.TB, opts ...ChanGraphOption) *ChannelGraph {
×
UNCOV
4728
        t.Helper()
×
UNCOV
4729

×
UNCOV
4730
        // Next, create KVStore for the first time.
×
UNCOV
4731
        backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
×
UNCOV
4732
        t.Cleanup(backendCleanup)
×
UNCOV
4733
        require.NoError(t, err)
×
UNCOV
4734
        t.Cleanup(func() {
×
UNCOV
4735
                require.NoError(t, backend.Close())
×
UNCOV
4736
        })
×
4737

UNCOV
4738
        graphStore, err := NewKVStore(backend)
×
UNCOV
4739
        require.NoError(t, err)
×
UNCOV
4740

×
UNCOV
4741
        graph, err := NewChannelGraph(graphStore, opts...)
×
UNCOV
4742
        require.NoError(t, err)
×
UNCOV
4743
        require.NoError(t, graph.Start())
×
UNCOV
4744
        t.Cleanup(func() {
×
UNCOV
4745
                require.NoError(t, graph.Stop())
×
UNCOV
4746
        })
×
4747

UNCOV
4748
        return graph
×
4749
}
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