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

lightningnetwork / lnd / 15871518718

25 Jun 2025 08:37AM UTC coverage: 55.595% (-12.2%) from 67.8%
15871518718

Pull #9991

github

web-flow
Merge 43edfeb10 into 33e6f2854
Pull Request #9991: routerrpc: add log line for probing the invoice request

2 of 12 new or added lines in 1 file covered. (16.67%)

23714 existing lines in 287 files now uncovered.

108233 of 194681 relevant lines covered (55.6%)

22511.42 hits per line

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

77.68
/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/fn/v2"
26
        "github.com/lightningnetwork/lnd/graph/db/models"
27
        "github.com/lightningnetwork/lnd/input"
28
        "github.com/lightningnetwork/lnd/kvdb"
29
        "github.com/lightningnetwork/lnd/lnwire"
30
        "github.com/lightningnetwork/lnd/routing/route"
31
        "github.com/stretchr/testify/require"
32
)
33

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

169✔
210
        opts := DefaultOptions()
169✔
211
        for _, o := range options {
169✔
UNCOV
212
                o(opts)
×
UNCOV
213
        }
×
214

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

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

169✔
235
        return g, nil
169✔
236
}
237

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

244
// String returns a human-readable representation of the key.
245
func (c channelMapKey) String() string {
×
246
        return fmt.Sprintf("node=%v, chanID=%x", c.nodeKey, c.chanID)
×
247
}
×
248

249
// getChannelMap loads all channel edge policies from the database and stores
250
// them in a map.
251
func (c *KVStore) getChannelMap(edges kvdb.RBucket) (
252
        map[channelMapKey]*models.ChannelEdgePolicy, error) {
141✔
253

141✔
254
        // Create a map to store all channel edge policies.
141✔
255
        channelMap := make(map[channelMapKey]*models.ChannelEdgePolicy)
141✔
256

141✔
257
        err := kvdb.ForAll(edges, func(k, edgeBytes []byte) error {
1,700✔
258
                // Skip embedded buckets.
1,559✔
259
                if bytes.Equal(k, edgeIndexBucket) ||
1,559✔
260
                        bytes.Equal(k, edgeUpdateIndexBucket) ||
1,559✔
261
                        bytes.Equal(k, zombieBucket) ||
1,559✔
262
                        bytes.Equal(k, disabledEdgePolicyBucket) ||
1,559✔
263
                        bytes.Equal(k, channelPointBucket) {
2,128✔
264

569✔
265
                        return nil
569✔
266
                }
569✔
267

268
                // Validate key length.
269
                if len(k) != 33+8 {
990✔
270
                        return fmt.Errorf("invalid edge key %x encountered", k)
×
271
                }
×
272

273
                var key channelMapKey
990✔
274
                copy(key.nodeKey[:], k[:33])
990✔
275
                copy(key.chanID[:], k[33:])
990✔
276

990✔
277
                // No need to deserialize unknown policy.
990✔
278
                if bytes.Equal(edgeBytes, unknownPolicy) {
990✔
279
                        return nil
×
280
                }
×
281

282
                edgeReader := bytes.NewReader(edgeBytes)
990✔
283
                edge, err := deserializeChanEdgePolicyRaw(
990✔
284
                        edgeReader,
990✔
285
                )
990✔
286

990✔
287
                switch {
990✔
288
                // If the db policy was missing an expected optional field, we
289
                // return nil as if the policy was unknown.
290
                case errors.Is(err, ErrEdgePolicyOptionalFieldNotFound):
×
291
                        return nil
×
292

293
                // We don't want a single policy with bad TLV data to stop us
294
                // from loading the rest of the data, so we just skip this
295
                // policy. This is for backwards compatibility since we did not
296
                // use to validate TLV data in the past before persisting it.
297
                case errors.Is(err, ErrParsingExtraTLVBytes):
×
298
                        return nil
×
299

300
                case err != nil:
×
301
                        return err
×
302
                }
303

304
                channelMap[key] = edge
990✔
305

990✔
306
                return nil
990✔
307
        })
308
        if err != nil {
141✔
309
                return nil, err
×
310
        }
×
311

312
        return channelMap, nil
141✔
313
}
314

315
var graphTopLevelBuckets = [][]byte{
316
        nodeBucket,
317
        edgeBucket,
318
        graphMetaBucket,
319
        closedScidBucket,
320
}
321

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

334
                nodes := tx.ReadWriteBucket(nodeBucket)
169✔
335
                _, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
169✔
336
                if err != nil {
169✔
337
                        return err
×
338
                }
×
339
                _, err = nodes.CreateBucketIfNotExists(nodeUpdateIndexBucket)
169✔
340
                if err != nil {
169✔
341
                        return err
×
342
                }
×
343

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

362
                graphMeta := tx.ReadWriteBucket(graphMetaBucket)
169✔
363
                _, err = graphMeta.CreateBucketIfNotExists(pruneLogBucket)
169✔
364

169✔
365
                return err
169✔
366
        }, func() {})
169✔
367
        if err != nil {
169✔
368
                return fmt.Errorf("unable to create new channel graph: %w", err)
×
369
        }
×
370

371
        return nil
169✔
372
}
373

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

3✔
382
        pubKey, err := route.NewVertexFromBytes(nodePub.SerializeCompressed())
3✔
383
        if err != nil {
3✔
384
                return false, nil, err
×
385
        }
×
386

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

393
        case errors.Is(err, ErrGraphNodeNotFound):
1✔
394
                return false, nil, nil
1✔
395
        }
396

397
        return true, node.Addresses, nil
2✔
398
}
399

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

4✔
412
        return c.db.View(func(tx kvdb.RTx) error {
8✔
413
                edges := tx.ReadBucket(edgeBucket)
4✔
414
                if edges == nil {
4✔
415
                        return ErrGraphNoEdgesFound
×
416
                }
×
417

418
                // First, load all edges in memory indexed by node and channel
419
                // id.
420
                channelMap, err := c.getChannelMap(edges)
4✔
421
                if err != nil {
4✔
422
                        return err
×
423
                }
×
424

425
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
4✔
426
                if edgeIndex == nil {
4✔
427
                        return ErrGraphNoEdgesFound
×
428
                }
×
429

430
                // Load edge index, recombine each channel with the policies
431
                // loaded above and invoke the callback.
432
                return kvdb.ForAll(
4✔
433
                        edgeIndex, func(k, edgeInfoBytes []byte) error {
103✔
434
                                var chanID [8]byte
99✔
435
                                copy(chanID[:], k)
99✔
436

99✔
437
                                edgeInfoReader := bytes.NewReader(edgeInfoBytes)
99✔
438
                                info, err := deserializeChanEdgeInfo(
99✔
439
                                        edgeInfoReader,
99✔
440
                                )
99✔
441
                                if err != nil {
99✔
442
                                        return err
×
443
                                }
×
444

445
                                policy1 := channelMap[channelMapKey{
99✔
446
                                        nodeKey: info.NodeKey1Bytes,
99✔
447
                                        chanID:  chanID,
99✔
448
                                }]
99✔
449

99✔
450
                                policy2 := channelMap[channelMapKey{
99✔
451
                                        nodeKey: info.NodeKey2Bytes,
99✔
452
                                        chanID:  chanID,
99✔
453
                                }]
99✔
454

99✔
455
                                return cb(&info, policy1, policy2)
99✔
456
                        },
457
                )
458
        }, func() {})
4✔
459
}
460

461
// ForEachChannelCacheable iterates through all the channel edges stored within
462
// the graph and invokes the passed callback for each edge. The callback takes
463
// two edges as since this is a directed graph, both the in/out edges are
464
// visited. If the callback returns an error, then the transaction is aborted
465
// and the iteration stops early.
466
//
467
// NOTE: If an edge can't be found, or wasn't advertised, then a nil pointer
468
// for that particular channel edge routing policy will be passed into the
469
// callback.
470
//
471
// NOTE: this method is like ForEachChannel but fetches only the data required
472
// for the graph cache.
473
func (c *KVStore) ForEachChannelCacheable(cb func(*models.CachedEdgeInfo,
474
        *models.CachedEdgePolicy, *models.CachedEdgePolicy) error) error {
137✔
475

137✔
476
        return c.db.View(func(tx kvdb.RTx) error {
274✔
477
                edges := tx.ReadBucket(edgeBucket)
137✔
478
                if edges == nil {
137✔
479
                        return ErrGraphNoEdgesFound
×
480
                }
×
481

482
                // First, load all edges in memory indexed by node and channel
483
                // id.
484
                channelMap, err := c.getChannelMap(edges)
137✔
485
                if err != nil {
137✔
486
                        return err
×
487
                }
×
488

489
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
137✔
490
                if edgeIndex == nil {
137✔
491
                        return ErrGraphNoEdgesFound
×
492
                }
×
493

494
                // Load edge index, recombine each channel with the policies
495
                // loaded above and invoke the callback.
496
                return kvdb.ForAll(
137✔
497
                        edgeIndex, func(k, edgeInfoBytes []byte) error {
533✔
498
                                var chanID [8]byte
396✔
499
                                copy(chanID[:], k)
396✔
500

396✔
501
                                edgeInfoReader := bytes.NewReader(edgeInfoBytes)
396✔
502
                                info, err := deserializeChanEdgeInfo(
396✔
503
                                        edgeInfoReader,
396✔
504
                                )
396✔
505
                                if err != nil {
396✔
506
                                        return err
×
507
                                }
×
508

509
                                key1 := channelMapKey{
396✔
510
                                        nodeKey: info.NodeKey1Bytes,
396✔
511
                                        chanID:  chanID,
396✔
512
                                }
396✔
513
                                policy1 := channelMap[key1]
396✔
514

396✔
515
                                key2 := channelMapKey{
396✔
516
                                        nodeKey: info.NodeKey2Bytes,
396✔
517
                                        chanID:  chanID,
396✔
518
                                }
396✔
519
                                policy2 := channelMap[key2]
396✔
520

396✔
521
                                // We now create the cached edge policies, but
396✔
522
                                // only when the above policies are found in the
396✔
523
                                // `channelMap`.
396✔
524
                                var (
396✔
525
                                        cachedPolicy1 *models.CachedEdgePolicy
396✔
526
                                        cachedPolicy2 *models.CachedEdgePolicy
396✔
527
                                )
396✔
528

396✔
529
                                if policy1 != nil {
792✔
530
                                        cachedPolicy1 = models.NewCachedPolicy(
396✔
531
                                                policy1,
396✔
532
                                        )
396✔
533
                                }
396✔
534

535
                                if policy2 != nil {
792✔
536
                                        cachedPolicy2 = models.NewCachedPolicy(
396✔
537
                                                policy2,
396✔
538
                                        )
396✔
539
                                }
396✔
540

541
                                return cb(
396✔
542
                                        models.NewCachedEdge(&info),
396✔
543
                                        cachedPolicy1, cachedPolicy2,
396✔
544
                                )
396✔
545
                        },
546
                )
547
        }, func() {})
137✔
548
}
549

550
// forEachNodeDirectedChannel iterates through all channels of a given node,
551
// executing the passed callback on the directed edge representing the channel
552
// and its incoming policy. If the callback returns an error, then the iteration
553
// is halted with the error propagated back up to the caller. An optional read
554
// transaction may be provided. If none is provided, a new one will be created.
555
//
556
// Unknown policies are passed into the callback as nil values.
557
func (c *KVStore) forEachNodeDirectedChannel(tx kvdb.RTx,
558
        node route.Vertex, cb func(channel *DirectedChannel) error) error {
262✔
559

262✔
560
        // Fallback that uses the database.
262✔
561
        toNodeCallback := func() route.Vertex {
394✔
562
                return node
132✔
563
        }
132✔
564
        toNodeFeatures, err := c.fetchNodeFeatures(tx, node)
262✔
565
        if err != nil {
262✔
566
                return err
×
567
        }
×
568

569
        dbCallback := func(tx kvdb.RTx, e *models.ChannelEdgeInfo, p1,
262✔
570
                p2 *models.ChannelEdgePolicy) error {
948✔
571

686✔
572
                var cachedInPolicy *models.CachedEdgePolicy
686✔
573
                if p2 != nil {
1,369✔
574
                        cachedInPolicy = models.NewCachedPolicy(p2)
683✔
575
                        cachedInPolicy.ToNodePubKey = toNodeCallback
683✔
576
                        cachedInPolicy.ToNodeFeatures = toNodeFeatures
683✔
577
                }
683✔
578

579
                directedChannel := &DirectedChannel{
686✔
580
                        ChannelID:    e.ChannelID,
686✔
581
                        IsNode1:      node == e.NodeKey1Bytes,
686✔
582
                        OtherNode:    e.NodeKey2Bytes,
686✔
583
                        Capacity:     e.Capacity,
686✔
584
                        OutPolicySet: p1 != nil,
686✔
585
                        InPolicy:     cachedInPolicy,
686✔
586
                }
686✔
587

686✔
588
                if p1 != nil {
1,371✔
589
                        p1.InboundFee.WhenSome(func(fee lnwire.Fee) {
1,021✔
590
                                directedChannel.InboundFee = fee
336✔
591
                        })
336✔
592
                }
593

594
                if node == e.NodeKey2Bytes {
1,029✔
595
                        directedChannel.OtherNode = e.NodeKey1Bytes
343✔
596
                }
343✔
597

598
                return cb(directedChannel)
686✔
599
        }
600

601
        return nodeTraversal(tx, node[:], c.db, dbCallback)
262✔
602
}
603

604
// fetchNodeFeatures returns the features of a given node. If no features are
605
// known for the node, an empty feature vector is returned. An optional read
606
// transaction may be provided. If none is provided, a new one will be created.
607
func (c *KVStore) fetchNodeFeatures(tx kvdb.RTx,
608
        node route.Vertex) (*lnwire.FeatureVector, error) {
707✔
609

707✔
610
        // Fallback that uses the database.
707✔
611
        targetNode, err := c.FetchLightningNodeTx(tx, node)
707✔
612
        switch {
707✔
613
        // If the node exists and has features, return them directly.
614
        case err == nil:
696✔
615
                return targetNode.Features, nil
696✔
616

617
        // If we couldn't find a node announcement, populate a blank feature
618
        // vector.
619
        case errors.Is(err, ErrGraphNodeNotFound):
11✔
620
                return lnwire.EmptyFeatureVector(), nil
11✔
621

622
        // Otherwise, bubble the error up.
623
        default:
×
624
                return nil, err
×
625
        }
626
}
627

628
// ForEachNodeDirectedChannel iterates through all channels of a given node,
629
// executing the passed callback on the directed edge representing the channel
630
// and its incoming policy. If the callback returns an error, then the iteration
631
// is halted with the error propagated back up to the caller.
632
//
633
// Unknown policies are passed into the callback as nil values.
634
//
635
// NOTE: this is part of the graphdb.NodeTraverser interface.
636
func (c *KVStore) ForEachNodeDirectedChannel(nodePub route.Vertex,
637
        cb func(channel *DirectedChannel) error) error {
23✔
638

23✔
639
        return c.forEachNodeDirectedChannel(nil, nodePub, cb)
23✔
640
}
23✔
641

642
// FetchNodeFeatures returns the features of the given node. If no features are
643
// known for the node, an empty feature vector is returned.
644
//
645
// NOTE: this is part of the graphdb.NodeTraverser interface.
646
func (c *KVStore) FetchNodeFeatures(nodePub route.Vertex) (
647
        *lnwire.FeatureVector, error) {
1✔
648

1✔
649
        return c.fetchNodeFeatures(nil, nodePub)
1✔
650
}
1✔
651

652
// ForEachNodeCached is similar to forEachNode, but it returns DirectedChannel
653
// data to the call-back.
654
//
655
// NOTE: The callback contents MUST not be modified.
656
func (c *KVStore) ForEachNodeCached(cb func(node route.Vertex,
657
        chans map[uint64]*DirectedChannel) error) error {
1✔
658

1✔
659
        // Otherwise call back to a version that uses the database directly.
1✔
660
        // We'll iterate over each node, then the set of channels for each
1✔
661
        // node, and construct a similar callback functiopn signature as the
1✔
662
        // main funcotin expects.
1✔
663
        return c.forEachNode(func(tx kvdb.RTx,
1✔
664
                node *models.LightningNode) error {
21✔
665

20✔
666
                channels := make(map[uint64]*DirectedChannel)
20✔
667

20✔
668
                err := c.forEachNodeChannelTx(tx, node.PubKeyBytes,
20✔
669
                        func(tx kvdb.RTx, e *models.ChannelEdgeInfo,
20✔
670
                                p1 *models.ChannelEdgePolicy,
20✔
671
                                p2 *models.ChannelEdgePolicy) error {
210✔
672

190✔
673
                                toNodeCallback := func() route.Vertex {
190✔
674
                                        return node.PubKeyBytes
×
675
                                }
×
676
                                toNodeFeatures, err := c.fetchNodeFeatures(
190✔
677
                                        tx, node.PubKeyBytes,
190✔
678
                                )
190✔
679
                                if err != nil {
190✔
680
                                        return err
×
681
                                }
×
682

683
                                var cachedInPolicy *models.CachedEdgePolicy
190✔
684
                                if p2 != nil {
380✔
685
                                        cachedInPolicy =
190✔
686
                                                models.NewCachedPolicy(p2)
190✔
687
                                        cachedInPolicy.ToNodePubKey =
190✔
688
                                                toNodeCallback
190✔
689
                                        cachedInPolicy.ToNodeFeatures =
190✔
690
                                                toNodeFeatures
190✔
691
                                }
190✔
692

693
                                directedChannel := &DirectedChannel{
190✔
694
                                        ChannelID: e.ChannelID,
190✔
695
                                        IsNode1: node.PubKeyBytes ==
190✔
696
                                                e.NodeKey1Bytes,
190✔
697
                                        OtherNode:    e.NodeKey2Bytes,
190✔
698
                                        Capacity:     e.Capacity,
190✔
699
                                        OutPolicySet: p1 != nil,
190✔
700
                                        InPolicy:     cachedInPolicy,
190✔
701
                                }
190✔
702

190✔
703
                                if node.PubKeyBytes == e.NodeKey2Bytes {
285✔
704
                                        directedChannel.OtherNode =
95✔
705
                                                e.NodeKey1Bytes
95✔
706
                                }
95✔
707

708
                                channels[e.ChannelID] = directedChannel
190✔
709

190✔
710
                                return nil
190✔
711
                        })
712
                if err != nil {
20✔
713
                        return err
×
714
                }
×
715

716
                return cb(node.PubKeyBytes, channels)
20✔
717
        })
718
}
719

720
// DisabledChannelIDs returns the channel ids of disabled channels.
721
// A channel is disabled when two of the associated ChanelEdgePolicies
722
// have their disabled bit on.
723
func (c *KVStore) DisabledChannelIDs() ([]uint64, error) {
6✔
724
        var disabledChanIDs []uint64
6✔
725
        var chanEdgeFound map[uint64]struct{}
6✔
726

6✔
727
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
12✔
728
                edges := tx.ReadBucket(edgeBucket)
6✔
729
                if edges == nil {
6✔
730
                        return ErrGraphNoEdgesFound
×
731
                }
×
732

733
                disabledEdgePolicyIndex := edges.NestedReadBucket(
6✔
734
                        disabledEdgePolicyBucket,
6✔
735
                )
6✔
736
                if disabledEdgePolicyIndex == nil {
7✔
737
                        return nil
1✔
738
                }
1✔
739

740
                // We iterate over all disabled policies and we add each channel
741
                // that has more than one disabled policy to disabledChanIDs
742
                // array.
743
                return disabledEdgePolicyIndex.ForEach(
5✔
744
                        func(k, v []byte) error {
16✔
745
                                chanID := byteOrder.Uint64(k[:8])
11✔
746
                                _, edgeFound := chanEdgeFound[chanID]
11✔
747
                                if edgeFound {
15✔
748
                                        delete(chanEdgeFound, chanID)
4✔
749
                                        disabledChanIDs = append(
4✔
750
                                                disabledChanIDs, chanID,
4✔
751
                                        )
4✔
752

4✔
753
                                        return nil
4✔
754
                                }
4✔
755

756
                                chanEdgeFound[chanID] = struct{}{}
7✔
757

7✔
758
                                return nil
7✔
759
                        },
760
                )
761
        }, func() {
6✔
762
                disabledChanIDs = nil
6✔
763
                chanEdgeFound = make(map[uint64]struct{})
6✔
764
        })
6✔
765
        if err != nil {
6✔
766
                return nil, err
×
767
        }
×
768

769
        return disabledChanIDs, nil
6✔
770
}
771

772
// ForEachNode iterates through all the stored vertices/nodes in the graph,
773
// executing the passed callback with each node encountered. If the callback
774
// returns an error, then the transaction is aborted and the iteration stops
775
// early. Any operations performed on the NodeTx passed to the call-back are
776
// executed under the same read transaction and so, methods on the NodeTx object
777
// _MUST_ only be called from within the call-back.
778
func (c *KVStore) ForEachNode(cb func(tx NodeRTx) error) error {
128✔
779
        return c.forEachNode(func(tx kvdb.RTx,
128✔
780
                node *models.LightningNode) error {
1,286✔
781

1,158✔
782
                return cb(newChanGraphNodeTx(tx, c, node))
1,158✔
783
        })
1,158✔
784
}
785

786
// forEachNode iterates through all the stored vertices/nodes in the graph,
787
// executing the passed callback with each node encountered. If the callback
788
// returns an error, then the transaction is aborted and the iteration stops
789
// early.
790
//
791
// TODO(roasbeef): add iterator interface to allow for memory efficient graph
792
// traversal when graph gets mega.
793
func (c *KVStore) forEachNode(
794
        cb func(kvdb.RTx, *models.LightningNode) error) error {
129✔
795

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

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

812
                        nodeReader := bytes.NewReader(nodeBytes)
1,178✔
813
                        node, err := deserializeLightningNode(nodeReader)
1,178✔
814
                        if err != nil {
1,178✔
815
                                return err
×
816
                        }
×
817

818
                        // Execute the callback, the transaction will abort if
819
                        // this returns an error.
820
                        return cb(tx, &node)
1,178✔
821
                })
822
        }
823

824
        return kvdb.View(c.db, traversal, func() {})
258✔
825
}
826

827
// ForEachNodeCacheable iterates through all the stored vertices/nodes in the
828
// graph, executing the passed callback with each node encountered. If the
829
// callback returns an error, then the transaction is aborted and the iteration
830
// stops early.
831
func (c *KVStore) ForEachNodeCacheable(cb func(route.Vertex,
832
        *lnwire.FeatureVector) error) error {
138✔
833

138✔
834
        traversal := func(tx kvdb.RTx) error {
276✔
835
                // First grab the nodes bucket which stores the mapping from
138✔
836
                // pubKey to node information.
138✔
837
                nodes := tx.ReadBucket(nodeBucket)
138✔
838
                if nodes == nil {
138✔
839
                        return ErrGraphNotFound
×
840
                }
×
841

842
                return nodes.ForEach(func(pubKey, nodeBytes []byte) error {
534✔
843
                        // If this is the source key, then we skip this
396✔
844
                        // iteration as the value for this key is a pubKey
396✔
845
                        // rather than raw node information.
396✔
846
                        if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
672✔
847
                                return nil
276✔
848
                        }
276✔
849

850
                        nodeReader := bytes.NewReader(nodeBytes)
120✔
851
                        node, features, err := deserializeLightningNodeCacheable( //nolint:ll
120✔
852
                                nodeReader,
120✔
853
                        )
120✔
854
                        if err != nil {
120✔
855
                                return err
×
856
                        }
×
857

858
                        // Execute the callback, the transaction will abort if
859
                        // this returns an error.
860
                        return cb(node, features)
120✔
861
                })
862
        }
863

864
        return kvdb.View(c.db, traversal, func() {})
276✔
865
}
866

867
// SourceNode returns the source node of the graph. The source node is treated
868
// as the center node within a star-graph. This method may be used to kick off
869
// a path finding algorithm in order to explore the reachability of another
870
// node based off the source node.
871
func (c *KVStore) SourceNode(_ context.Context) (*models.LightningNode,
872
        error) {
237✔
873

237✔
874
        var source *models.LightningNode
237✔
875
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
474✔
876
                // First grab the nodes bucket which stores the mapping from
237✔
877
                // pubKey to node information.
237✔
878
                nodes := tx.ReadBucket(nodeBucket)
237✔
879
                if nodes == nil {
237✔
880
                        return ErrGraphNotFound
×
881
                }
×
882

883
                node, err := c.sourceNode(nodes)
237✔
884
                if err != nil {
238✔
885
                        return err
1✔
886
                }
1✔
887
                source = node
236✔
888

236✔
889
                return nil
236✔
890
        }, func() {
237✔
891
                source = nil
237✔
892
        })
237✔
893
        if err != nil {
238✔
894
                return nil, err
1✔
895
        }
1✔
896

897
        return source, nil
236✔
898
}
899

900
// sourceNode uses an existing database transaction and returns the source node
901
// of the graph. The source node is treated as the center node within a
902
// star-graph. This method may be used to kick off a path finding algorithm in
903
// order to explore the reachability of another node based off the source node.
904
func (c *KVStore) sourceNode(nodes kvdb.RBucket) (*models.LightningNode,
905
        error) {
503✔
906

503✔
907
        selfPub := nodes.Get(sourceKey)
503✔
908
        if selfPub == nil {
504✔
909
                return nil, ErrSourceNodeNotSet
1✔
910
        }
1✔
911

912
        // With the pubKey of the source node retrieved, we're able to
913
        // fetch the full node information.
914
        node, err := fetchLightningNode(nodes, selfPub)
502✔
915
        if err != nil {
502✔
916
                return nil, err
×
917
        }
×
918

919
        return &node, nil
502✔
920
}
921

922
// SetSourceNode sets the source node within the graph database. The source
923
// node is to be used as the center of a star-graph within path finding
924
// algorithms.
925
func (c *KVStore) SetSourceNode(_ context.Context,
926
        node *models.LightningNode) error {
114✔
927

114✔
928
        nodePubBytes := node.PubKeyBytes[:]
114✔
929

114✔
930
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
228✔
931
                // First grab the nodes bucket which stores the mapping from
114✔
932
                // pubKey to node information.
114✔
933
                nodes, err := tx.CreateTopLevelBucket(nodeBucket)
114✔
934
                if err != nil {
114✔
935
                        return err
×
936
                }
×
937

938
                // Next we create the mapping from source to the targeted
939
                // public key.
940
                if err := nodes.Put(sourceKey, nodePubBytes); err != nil {
114✔
941
                        return err
×
942
                }
×
943

944
                // Finally, we commit the information of the lightning node
945
                // itself.
946
                return addLightningNode(tx, node)
114✔
947
        }, func() {})
114✔
948
}
949

950
// AddLightningNode adds a vertex/node to the graph database. If the node is not
951
// in the database from before, this will add a new, unconnected one to the
952
// graph. If it is present from before, this will update that node's
953
// information. Note that this method is expected to only be called to update an
954
// already present node from a node announcement, or to insert a node found in a
955
// channel update.
956
//
957
// TODO(roasbeef): also need sig of announcement.
958
func (c *KVStore) AddLightningNode(ctx context.Context,
959
        node *models.LightningNode, opts ...batch.SchedulerOption) error {
803✔
960

803✔
961
        r := &batch.Request[kvdb.RwTx]{
803✔
962
                Opts: batch.NewSchedulerOptions(opts...),
803✔
963
                Do: func(tx kvdb.RwTx) error {
1,606✔
964
                        return addLightningNode(tx, node)
803✔
965
                },
803✔
966
        }
967

968
        return c.nodeScheduler.Execute(ctx, r)
803✔
969
}
970

971
func addLightningNode(tx kvdb.RwTx, node *models.LightningNode) error {
998✔
972
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
998✔
973
        if err != nil {
998✔
974
                return err
×
975
        }
×
976

977
        aliases, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
998✔
978
        if err != nil {
998✔
979
                return err
×
980
        }
×
981

982
        updateIndex, err := nodes.CreateBucketIfNotExists(
998✔
983
                nodeUpdateIndexBucket,
998✔
984
        )
998✔
985
        if err != nil {
998✔
986
                return err
×
987
        }
×
988

989
        return putLightningNode(nodes, aliases, updateIndex, node)
998✔
990
}
991

992
// LookupAlias attempts to return the alias as advertised by the target node.
993
// TODO(roasbeef): currently assumes that aliases are unique...
994
func (c *KVStore) LookupAlias(_ context.Context,
995
        pub *btcec.PublicKey) (string, error) {
2✔
996

2✔
997
        var alias string
2✔
998

2✔
999
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
4✔
1000
                nodes := tx.ReadBucket(nodeBucket)
2✔
1001
                if nodes == nil {
2✔
1002
                        return ErrGraphNodesNotFound
×
1003
                }
×
1004

1005
                aliases := nodes.NestedReadBucket(aliasIndexBucket)
2✔
1006
                if aliases == nil {
2✔
1007
                        return ErrGraphNodesNotFound
×
1008
                }
×
1009

1010
                nodePub := pub.SerializeCompressed()
2✔
1011
                a := aliases.Get(nodePub)
2✔
1012
                if a == nil {
3✔
1013
                        return ErrNodeAliasNotFound
1✔
1014
                }
1✔
1015

1016
                // TODO(roasbeef): should actually be using the utf-8
1017
                // package...
1018
                alias = string(a)
1✔
1019

1✔
1020
                return nil
1✔
1021
        }, func() {
2✔
1022
                alias = ""
2✔
1023
        })
2✔
1024
        if err != nil {
3✔
1025
                return "", err
1✔
1026
        }
1✔
1027

1028
        return alias, nil
1✔
1029
}
1030

1031
// DeleteLightningNode starts a new database transaction to remove a vertex/node
1032
// from the database according to the node's public key.
1033
func (c *KVStore) DeleteLightningNode(_ context.Context,
1034
        nodePub route.Vertex) error {
4✔
1035

4✔
1036
        // TODO(roasbeef): ensure dangling edges are removed...
4✔
1037
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
8✔
1038
                nodes := tx.ReadWriteBucket(nodeBucket)
4✔
1039
                if nodes == nil {
4✔
1040
                        return ErrGraphNodeNotFound
×
1041
                }
×
1042

1043
                return c.deleteLightningNode(nodes, nodePub[:])
4✔
1044
        }, func() {})
4✔
1045
}
1046

1047
// deleteLightningNode uses an existing database transaction to remove a
1048
// vertex/node from the database according to the node's public key.
1049
func (c *KVStore) deleteLightningNode(nodes kvdb.RwBucket,
1050
        compressedPubKey []byte) error {
67✔
1051

67✔
1052
        aliases := nodes.NestedReadWriteBucket(aliasIndexBucket)
67✔
1053
        if aliases == nil {
67✔
1054
                return ErrGraphNodesNotFound
×
1055
        }
×
1056

1057
        if err := aliases.Delete(compressedPubKey); err != nil {
67✔
1058
                return err
×
1059
        }
×
1060

1061
        // Before we delete the node, we'll fetch its current state so we can
1062
        // determine when its last update was to clear out the node update
1063
        // index.
1064
        node, err := fetchLightningNode(nodes, compressedPubKey)
67✔
1065
        if err != nil {
68✔
1066
                return err
1✔
1067
        }
1✔
1068

1069
        if err := nodes.Delete(compressedPubKey); err != nil {
66✔
1070
                return err
×
1071
        }
×
1072

1073
        // Finally, we'll delete the index entry for the node within the
1074
        // nodeUpdateIndexBucket as this node is no longer active, so we don't
1075
        // need to track its last update.
1076
        nodeUpdateIndex := nodes.NestedReadWriteBucket(nodeUpdateIndexBucket)
66✔
1077
        if nodeUpdateIndex == nil {
66✔
1078
                return ErrGraphNodesNotFound
×
1079
        }
×
1080

1081
        // In order to delete the entry, we'll need to reconstruct the key for
1082
        // its last update.
1083
        updateUnix := uint64(node.LastUpdate.Unix())
66✔
1084
        var indexKey [8 + 33]byte
66✔
1085
        byteOrder.PutUint64(indexKey[:8], updateUnix)
66✔
1086
        copy(indexKey[8:], compressedPubKey)
66✔
1087

66✔
1088
        return nodeUpdateIndex.Delete(indexKey[:])
66✔
1089
}
1090

1091
// AddChannelEdge adds a new (undirected, blank) edge to the graph database. An
1092
// undirected edge from the two target nodes are created. The information stored
1093
// denotes the static attributes of the channel, such as the channelID, the keys
1094
// involved in creation of the channel, and the set of features that the channel
1095
// supports. The chanPoint and chanID are used to uniquely identify the edge
1096
// globally within the database.
1097
func (c *KVStore) AddChannelEdge(ctx context.Context,
1098
        edge *models.ChannelEdgeInfo, opts ...batch.SchedulerOption) error {
1,715✔
1099

1,715✔
1100
        var alreadyExists bool
1,715✔
1101
        r := &batch.Request[kvdb.RwTx]{
1,715✔
1102
                Opts: batch.NewSchedulerOptions(opts...),
1,715✔
1103
                Reset: func() {
3,430✔
1104
                        alreadyExists = false
1,715✔
1105
                },
1,715✔
1106
                Do: func(tx kvdb.RwTx) error {
1,715✔
1107
                        err := c.addChannelEdge(tx, edge)
1,715✔
1108

1,715✔
1109
                        // Silence ErrEdgeAlreadyExist so that the batch can
1,715✔
1110
                        // succeed, but propagate the error via local state.
1,715✔
1111
                        if errors.Is(err, ErrEdgeAlreadyExist) {
1,952✔
1112
                                alreadyExists = true
237✔
1113
                                return nil
237✔
1114
                        }
237✔
1115

1116
                        return err
1,478✔
1117
                },
1118
                OnCommit: func(err error) error {
1,715✔
1119
                        switch {
1,715✔
1120
                        case err != nil:
×
1121
                                return err
×
1122
                        case alreadyExists:
237✔
1123
                                return ErrEdgeAlreadyExist
237✔
1124
                        default:
1,478✔
1125
                                c.rejectCache.remove(edge.ChannelID)
1,478✔
1126
                                c.chanCache.remove(edge.ChannelID)
1,478✔
1127
                                return nil
1,478✔
1128
                        }
1129
                },
1130
        }
1131

1132
        return c.chanScheduler.Execute(ctx, r)
1,715✔
1133
}
1134

1135
// addChannelEdge is the private form of AddChannelEdge that allows callers to
1136
// utilize an existing db transaction.
1137
func (c *KVStore) addChannelEdge(tx kvdb.RwTx,
1138
        edge *models.ChannelEdgeInfo) error {
1,715✔
1139

1,715✔
1140
        // Construct the channel's primary key which is the 8-byte channel ID.
1,715✔
1141
        var chanKey [8]byte
1,715✔
1142
        binary.BigEndian.PutUint64(chanKey[:], edge.ChannelID)
1,715✔
1143

1,715✔
1144
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
1,715✔
1145
        if err != nil {
1,715✔
1146
                return err
×
1147
        }
×
1148
        edges, err := tx.CreateTopLevelBucket(edgeBucket)
1,715✔
1149
        if err != nil {
1,715✔
1150
                return err
×
1151
        }
×
1152
        edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
1,715✔
1153
        if err != nil {
1,715✔
1154
                return err
×
1155
        }
×
1156
        chanIndex, err := edges.CreateBucketIfNotExists(channelPointBucket)
1,715✔
1157
        if err != nil {
1,715✔
1158
                return err
×
1159
        }
×
1160

1161
        // First, attempt to check if this edge has already been created. If
1162
        // so, then we can exit early as this method is meant to be idempotent.
1163
        if edgeInfo := edgeIndex.Get(chanKey[:]); edgeInfo != nil {
1,952✔
1164
                return ErrEdgeAlreadyExist
237✔
1165
        }
237✔
1166

1167
        // Before we insert the channel into the database, we'll ensure that
1168
        // both nodes already exist in the channel graph. If either node
1169
        // doesn't, then we'll insert a "shell" node that just includes its
1170
        // public key, so subsequent validation and queries can work properly.
1171
        _, node1Err := fetchLightningNode(nodes, edge.NodeKey1Bytes[:])
1,478✔
1172
        switch {
1,478✔
1173
        case errors.Is(node1Err, ErrGraphNodeNotFound):
21✔
1174
                node1Shell := models.LightningNode{
21✔
1175
                        PubKeyBytes:          edge.NodeKey1Bytes,
21✔
1176
                        HaveNodeAnnouncement: false,
21✔
1177
                }
21✔
1178
                err := addLightningNode(tx, &node1Shell)
21✔
1179
                if err != nil {
21✔
1180
                        return fmt.Errorf("unable to create shell node "+
×
1181
                                "for: %x: %w", edge.NodeKey1Bytes, err)
×
1182
                }
×
1183
        case node1Err != nil:
×
1184
                return node1Err
×
1185
        }
1186

1187
        _, node2Err := fetchLightningNode(nodes, edge.NodeKey2Bytes[:])
1,478✔
1188
        switch {
1,478✔
1189
        case errors.Is(node2Err, ErrGraphNodeNotFound):
60✔
1190
                node2Shell := models.LightningNode{
60✔
1191
                        PubKeyBytes:          edge.NodeKey2Bytes,
60✔
1192
                        HaveNodeAnnouncement: false,
60✔
1193
                }
60✔
1194
                err := addLightningNode(tx, &node2Shell)
60✔
1195
                if err != nil {
60✔
1196
                        return fmt.Errorf("unable to create shell node "+
×
1197
                                "for: %x: %w", edge.NodeKey2Bytes, err)
×
1198
                }
×
1199
        case node2Err != nil:
×
1200
                return node2Err
×
1201
        }
1202

1203
        // If the edge hasn't been created yet, then we'll first add it to the
1204
        // edge index in order to associate the edge between two nodes and also
1205
        // store the static components of the channel.
1206
        if err := putChanEdgeInfo(edgeIndex, edge, chanKey); err != nil {
1,478✔
1207
                return err
×
1208
        }
×
1209

1210
        // Mark edge policies for both sides as unknown. This is to enable
1211
        // efficient incoming channel lookup for a node.
1212
        keys := []*[33]byte{
1,478✔
1213
                &edge.NodeKey1Bytes,
1,478✔
1214
                &edge.NodeKey2Bytes,
1,478✔
1215
        }
1,478✔
1216
        for _, key := range keys {
4,434✔
1217
                err := putChanEdgePolicyUnknown(edges, edge.ChannelID, key[:])
2,956✔
1218
                if err != nil {
2,956✔
1219
                        return err
×
1220
                }
×
1221
        }
1222

1223
        // Finally we add it to the channel index which maps channel points
1224
        // (outpoints) to the shorter channel ID's.
1225
        var b bytes.Buffer
1,478✔
1226
        if err := WriteOutpoint(&b, &edge.ChannelPoint); err != nil {
1,478✔
1227
                return err
×
1228
        }
×
1229

1230
        return chanIndex.Put(b.Bytes(), chanKey[:])
1,478✔
1231
}
1232

1233
// HasChannelEdge returns true if the database knows of a channel edge with the
1234
// passed channel ID, and false otherwise. If an edge with that ID is found
1235
// within the graph, then two time stamps representing the last time the edge
1236
// was updated for both directed edges are returned along with the boolean. If
1237
// it is not found, then the zombie index is checked and its result is returned
1238
// as the second boolean.
1239
func (c *KVStore) HasChannelEdge(
1240
        chanID uint64) (time.Time, time.Time, bool, bool, error) {
206✔
1241

206✔
1242
        var (
206✔
1243
                upd1Time time.Time
206✔
1244
                upd2Time time.Time
206✔
1245
                exists   bool
206✔
1246
                isZombie bool
206✔
1247
        )
206✔
1248

206✔
1249
        // We'll query the cache with the shared lock held to allow multiple
206✔
1250
        // readers to access values in the cache concurrently if they exist.
206✔
1251
        c.cacheMu.RLock()
206✔
1252
        if entry, ok := c.rejectCache.get(chanID); ok {
273✔
1253
                c.cacheMu.RUnlock()
67✔
1254
                upd1Time = time.Unix(entry.upd1Time, 0)
67✔
1255
                upd2Time = time.Unix(entry.upd2Time, 0)
67✔
1256
                exists, isZombie = entry.flags.unpack()
67✔
1257

67✔
1258
                return upd1Time, upd2Time, exists, isZombie, nil
67✔
1259
        }
67✔
1260
        c.cacheMu.RUnlock()
139✔
1261

139✔
1262
        c.cacheMu.Lock()
139✔
1263
        defer c.cacheMu.Unlock()
139✔
1264

139✔
1265
        // The item was not found with the shared lock, so we'll acquire the
139✔
1266
        // exclusive lock and check the cache again in case another method added
139✔
1267
        // the entry to the cache while no lock was held.
139✔
1268
        if entry, ok := c.rejectCache.get(chanID); ok {
144✔
1269
                upd1Time = time.Unix(entry.upd1Time, 0)
5✔
1270
                upd2Time = time.Unix(entry.upd2Time, 0)
5✔
1271
                exists, isZombie = entry.flags.unpack()
5✔
1272

5✔
1273
                return upd1Time, upd2Time, exists, isZombie, nil
5✔
1274
        }
5✔
1275

1276
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
268✔
1277
                edges := tx.ReadBucket(edgeBucket)
134✔
1278
                if edges == nil {
134✔
1279
                        return ErrGraphNoEdgesFound
×
1280
                }
×
1281
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
134✔
1282
                if edgeIndex == nil {
134✔
1283
                        return ErrGraphNoEdgesFound
×
1284
                }
×
1285

1286
                var channelID [8]byte
134✔
1287
                byteOrder.PutUint64(channelID[:], chanID)
134✔
1288

134✔
1289
                // If the edge doesn't exist, then we'll also check our zombie
134✔
1290
                // index.
134✔
1291
                if edgeIndex.Get(channelID[:]) == nil {
215✔
1292
                        exists = false
81✔
1293
                        zombieIndex := edges.NestedReadBucket(zombieBucket)
81✔
1294
                        if zombieIndex != nil {
162✔
1295
                                isZombie, _, _ = isZombieEdge(
81✔
1296
                                        zombieIndex, chanID,
81✔
1297
                                )
81✔
1298
                        }
81✔
1299

1300
                        return nil
81✔
1301
                }
1302

1303
                exists = true
53✔
1304
                isZombie = false
53✔
1305

53✔
1306
                // If the channel has been found in the graph, then retrieve
53✔
1307
                // the edges itself so we can return the last updated
53✔
1308
                // timestamps.
53✔
1309
                nodes := tx.ReadBucket(nodeBucket)
53✔
1310
                if nodes == nil {
53✔
1311
                        return ErrGraphNodeNotFound
×
1312
                }
×
1313

1314
                e1, e2, err := fetchChanEdgePolicies(
53✔
1315
                        edgeIndex, edges, channelID[:],
53✔
1316
                )
53✔
1317
                if err != nil {
53✔
1318
                        return err
×
1319
                }
×
1320

1321
                // As we may have only one of the edges populated, only set the
1322
                // update time if the edge was found in the database.
1323
                if e1 != nil {
71✔
1324
                        upd1Time = e1.LastUpdate
18✔
1325
                }
18✔
1326
                if e2 != nil {
69✔
1327
                        upd2Time = e2.LastUpdate
16✔
1328
                }
16✔
1329

1330
                return nil
53✔
1331
        }, func() {}); err != nil {
134✔
1332
                return time.Time{}, time.Time{}, exists, isZombie, err
×
1333
        }
×
1334

1335
        c.rejectCache.insert(chanID, rejectCacheEntry{
134✔
1336
                upd1Time: upd1Time.Unix(),
134✔
1337
                upd2Time: upd2Time.Unix(),
134✔
1338
                flags:    packRejectFlags(exists, isZombie),
134✔
1339
        })
134✔
1340

134✔
1341
        return upd1Time, upd2Time, exists, isZombie, nil
134✔
1342
}
1343

1344
// AddEdgeProof sets the proof of an existing edge in the graph database.
1345
func (c *KVStore) AddEdgeProof(chanID lnwire.ShortChannelID,
1346
        proof *models.ChannelAuthProof) error {
2✔
1347

2✔
1348
        // Construct the channel's primary key which is the 8-byte channel ID.
2✔
1349
        var chanKey [8]byte
2✔
1350
        binary.BigEndian.PutUint64(chanKey[:], chanID.ToUint64())
2✔
1351

2✔
1352
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
4✔
1353
                edges := tx.ReadWriteBucket(edgeBucket)
2✔
1354
                if edges == nil {
2✔
1355
                        return ErrEdgeNotFound
×
1356
                }
×
1357

1358
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
2✔
1359
                if edgeIndex == nil {
2✔
1360
                        return ErrEdgeNotFound
×
1361
                }
×
1362

1363
                edge, err := fetchChanEdgeInfo(edgeIndex, chanKey[:])
2✔
1364
                if err != nil {
2✔
1365
                        return err
×
1366
                }
×
1367

1368
                edge.AuthProof = proof
2✔
1369

2✔
1370
                return putChanEdgeInfo(edgeIndex, &edge, chanKey)
2✔
1371
        }, func() {})
2✔
1372
}
1373

1374
const (
1375
        // pruneTipBytes is the total size of the value which stores a prune
1376
        // entry of the graph in the prune log. The "prune tip" is the last
1377
        // entry in the prune log, and indicates if the channel graph is in
1378
        // sync with the current UTXO state. The structure of the value
1379
        // is: blockHash, taking 32 bytes total.
1380
        pruneTipBytes = 32
1381
)
1382

1383
// PruneGraph prunes newly closed channels from the channel graph in response
1384
// to a new block being solved on the network. Any transactions which spend the
1385
// funding output of any known channels within he graph will be deleted.
1386
// Additionally, the "prune tip", or the last block which has been used to
1387
// prune the graph is stored so callers can ensure the graph is fully in sync
1388
// with the current UTXO state. A slice of channels that have been closed by
1389
// the target block along with any pruned nodes are returned if the function
1390
// succeeds without error.
1391
func (c *KVStore) PruneGraph(spentOutputs []*wire.OutPoint,
1392
        blockHash *chainhash.Hash, blockHeight uint32) (
1393
        []*models.ChannelEdgeInfo, []route.Vertex, error) {
242✔
1394

242✔
1395
        c.cacheMu.Lock()
242✔
1396
        defer c.cacheMu.Unlock()
242✔
1397

242✔
1398
        var (
242✔
1399
                chansClosed []*models.ChannelEdgeInfo
242✔
1400
                prunedNodes []route.Vertex
242✔
1401
        )
242✔
1402

242✔
1403
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
484✔
1404
                // First grab the edges bucket which houses the information
242✔
1405
                // we'd like to delete
242✔
1406
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
242✔
1407
                if err != nil {
242✔
1408
                        return err
×
1409
                }
×
1410

1411
                // Next grab the two edge indexes which will also need to be
1412
                // updated.
1413
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
242✔
1414
                if err != nil {
242✔
1415
                        return err
×
1416
                }
×
1417
                chanIndex, err := edges.CreateBucketIfNotExists(
242✔
1418
                        channelPointBucket,
242✔
1419
                )
242✔
1420
                if err != nil {
242✔
1421
                        return err
×
1422
                }
×
1423
                nodes := tx.ReadWriteBucket(nodeBucket)
242✔
1424
                if nodes == nil {
242✔
1425
                        return ErrSourceNodeNotSet
×
1426
                }
×
1427
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
242✔
1428
                if err != nil {
242✔
1429
                        return err
×
1430
                }
×
1431

1432
                // For each of the outpoints that have been spent within the
1433
                // block, we attempt to delete them from the graph as if that
1434
                // outpoint was a channel, then it has now been closed.
1435
                for _, chanPoint := range spentOutputs {
372✔
1436
                        // TODO(roasbeef): load channel bloom filter, continue
130✔
1437
                        // if NOT if filter
130✔
1438

130✔
1439
                        var opBytes bytes.Buffer
130✔
1440
                        err := WriteOutpoint(&opBytes, chanPoint)
130✔
1441
                        if err != nil {
130✔
1442
                                return err
×
1443
                        }
×
1444

1445
                        // First attempt to see if the channel exists within
1446
                        // the database, if not, then we can exit early.
1447
                        chanID := chanIndex.Get(opBytes.Bytes())
130✔
1448
                        if chanID == nil {
236✔
1449
                                continue
106✔
1450
                        }
1451

1452
                        // Attempt to delete the channel, an ErrEdgeNotFound
1453
                        // will be returned if that outpoint isn't known to be
1454
                        // a channel. If no error is returned, then a channel
1455
                        // was successfully pruned.
1456
                        edgeInfo, err := c.delChannelEdgeUnsafe(
24✔
1457
                                edges, edgeIndex, chanIndex, zombieIndex,
24✔
1458
                                chanID, false, false,
24✔
1459
                        )
24✔
1460
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
24✔
1461
                                return err
×
1462
                        }
×
1463

1464
                        chansClosed = append(chansClosed, edgeInfo)
24✔
1465
                }
1466

1467
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
242✔
1468
                if err != nil {
242✔
1469
                        return err
×
1470
                }
×
1471

1472
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
242✔
1473
                        pruneLogBucket,
242✔
1474
                )
242✔
1475
                if err != nil {
242✔
1476
                        return err
×
1477
                }
×
1478

1479
                // With the graph pruned, add a new entry to the prune log,
1480
                // which can be used to check if the graph is fully synced with
1481
                // the current UTXO state.
1482
                var blockHeightBytes [4]byte
242✔
1483
                byteOrder.PutUint32(blockHeightBytes[:], blockHeight)
242✔
1484

242✔
1485
                var newTip [pruneTipBytes]byte
242✔
1486
                copy(newTip[:], blockHash[:])
242✔
1487

242✔
1488
                err = pruneBucket.Put(blockHeightBytes[:], newTip[:])
242✔
1489
                if err != nil {
242✔
1490
                        return err
×
1491
                }
×
1492

1493
                // Now that the graph has been pruned, we'll also attempt to
1494
                // prune any nodes that have had a channel closed within the
1495
                // latest block.
1496
                prunedNodes, err = c.pruneGraphNodes(nodes, edgeIndex)
242✔
1497

242✔
1498
                return err
242✔
1499
        }, func() {
242✔
1500
                chansClosed = nil
242✔
1501
                prunedNodes = nil
242✔
1502
        })
242✔
1503
        if err != nil {
242✔
1504
                return nil, nil, err
×
1505
        }
×
1506

1507
        for _, channel := range chansClosed {
266✔
1508
                c.rejectCache.remove(channel.ChannelID)
24✔
1509
                c.chanCache.remove(channel.ChannelID)
24✔
1510
        }
24✔
1511

1512
        return chansClosed, prunedNodes, nil
242✔
1513
}
1514

1515
// PruneGraphNodes is a garbage collection method which attempts to prune out
1516
// any nodes from the channel graph that are currently unconnected. This ensure
1517
// that we only maintain a graph of reachable nodes. In the event that a pruned
1518
// node gains more channels, it will be re-added back to the graph.
1519
func (c *KVStore) PruneGraphNodes() ([]route.Vertex, error) {
23✔
1520
        var prunedNodes []route.Vertex
23✔
1521
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
46✔
1522
                nodes := tx.ReadWriteBucket(nodeBucket)
23✔
1523
                if nodes == nil {
23✔
1524
                        return ErrGraphNodesNotFound
×
1525
                }
×
1526
                edges := tx.ReadWriteBucket(edgeBucket)
23✔
1527
                if edges == nil {
23✔
1528
                        return ErrGraphNotFound
×
1529
                }
×
1530
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
23✔
1531
                if edgeIndex == nil {
23✔
1532
                        return ErrGraphNoEdgesFound
×
1533
                }
×
1534

1535
                var err error
23✔
1536
                prunedNodes, err = c.pruneGraphNodes(nodes, edgeIndex)
23✔
1537
                if err != nil {
23✔
1538
                        return err
×
1539
                }
×
1540

1541
                return nil
23✔
1542
        }, func() {
23✔
1543
                prunedNodes = nil
23✔
1544
        })
23✔
1545

1546
        return prunedNodes, err
23✔
1547
}
1548

1549
// pruneGraphNodes attempts to remove any nodes from the graph who have had a
1550
// channel closed within the current block. If the node still has existing
1551
// channels in the graph, this will act as a no-op.
1552
func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket,
1553
        edgeIndex kvdb.RwBucket) ([]route.Vertex, error) {
265✔
1554

265✔
1555
        log.Trace("Pruning nodes from graph with no open channels")
265✔
1556

265✔
1557
        // We'll retrieve the graph's source node to ensure we don't remove it
265✔
1558
        // even if it no longer has any open channels.
265✔
1559
        sourceNode, err := c.sourceNode(nodes)
265✔
1560
        if err != nil {
265✔
1561
                return nil, err
×
1562
        }
×
1563

1564
        // We'll use this map to keep count the number of references to a node
1565
        // in the graph. A node should only be removed once it has no more
1566
        // references in the graph.
1567
        nodeRefCounts := make(map[[33]byte]int)
265✔
1568
        err = nodes.ForEach(func(pubKey, nodeBytes []byte) error {
1,565✔
1569
                // If this is the source key, then we skip this
1,300✔
1570
                // iteration as the value for this key is a pubKey
1,300✔
1571
                // rather than raw node information.
1,300✔
1572
                if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
2,095✔
1573
                        return nil
795✔
1574
                }
795✔
1575

1576
                var nodePub [33]byte
505✔
1577
                copy(nodePub[:], pubKey)
505✔
1578
                nodeRefCounts[nodePub] = 0
505✔
1579

505✔
1580
                return nil
505✔
1581
        })
1582
        if err != nil {
265✔
1583
                return nil, err
×
1584
        }
×
1585

1586
        // To ensure we never delete the source node, we'll start off by
1587
        // bumping its ref count to 1.
1588
        nodeRefCounts[sourceNode.PubKeyBytes] = 1
265✔
1589

265✔
1590
        // Next, we'll run through the edgeIndex which maps a channel ID to the
265✔
1591
        // edge info. We'll use this scan to populate our reference count map
265✔
1592
        // above.
265✔
1593
        err = edgeIndex.ForEach(func(chanID, edgeInfoBytes []byte) error {
452✔
1594
                // The first 66 bytes of the edge info contain the pubkeys of
187✔
1595
                // the nodes that this edge attaches. We'll extract them, and
187✔
1596
                // add them to the ref count map.
187✔
1597
                var node1, node2 [33]byte
187✔
1598
                copy(node1[:], edgeInfoBytes[:33])
187✔
1599
                copy(node2[:], edgeInfoBytes[33:])
187✔
1600

187✔
1601
                // With the nodes extracted, we'll increase the ref count of
187✔
1602
                // each of the nodes.
187✔
1603
                nodeRefCounts[node1]++
187✔
1604
                nodeRefCounts[node2]++
187✔
1605

187✔
1606
                return nil
187✔
1607
        })
187✔
1608
        if err != nil {
265✔
1609
                return nil, err
×
1610
        }
×
1611

1612
        // Finally, we'll make a second pass over the set of nodes, and delete
1613
        // any nodes that have a ref count of zero.
1614
        var pruned []route.Vertex
265✔
1615
        for nodePubKey, refCount := range nodeRefCounts {
770✔
1616
                // If the ref count of the node isn't zero, then we can safely
505✔
1617
                // skip it as it still has edges to or from it within the
505✔
1618
                // graph.
505✔
1619
                if refCount != 0 {
947✔
1620
                        continue
442✔
1621
                }
1622

1623
                // If we reach this point, then there are no longer any edges
1624
                // that connect this node, so we can delete it.
1625
                err := c.deleteLightningNode(nodes, nodePubKey[:])
63✔
1626
                if err != nil {
63✔
1627
                        if errors.Is(err, ErrGraphNodeNotFound) ||
×
1628
                                errors.Is(err, ErrGraphNodesNotFound) {
×
1629

×
1630
                                log.Warnf("Unable to prune node %x from the "+
×
1631
                                        "graph: %v", nodePubKey, err)
×
1632
                                continue
×
1633
                        }
1634

1635
                        return nil, err
×
1636
                }
1637

1638
                log.Infof("Pruned unconnected node %x from channel graph",
63✔
1639
                        nodePubKey[:])
63✔
1640

63✔
1641
                pruned = append(pruned, nodePubKey)
63✔
1642
        }
1643

1644
        if len(pruned) > 0 {
312✔
1645
                log.Infof("Pruned %v unconnected nodes from the channel graph",
47✔
1646
                        len(pruned))
47✔
1647
        }
47✔
1648

1649
        return pruned, err
265✔
1650
}
1651

1652
// DisconnectBlockAtHeight is used to indicate that the block specified
1653
// by the passed height has been disconnected from the main chain. This
1654
// will "rewind" the graph back to the height below, deleting channels
1655
// that are no longer confirmed from the graph. The prune log will be
1656
// set to the last prune height valid for the remaining chain.
1657
// Channels that were removed from the graph resulting from the
1658
// disconnected block are returned.
1659
func (c *KVStore) DisconnectBlockAtHeight(height uint32) (
1660
        []*models.ChannelEdgeInfo, error) {
160✔
1661

160✔
1662
        // Every channel having a ShortChannelID starting at 'height'
160✔
1663
        // will no longer be confirmed.
160✔
1664
        startShortChanID := lnwire.ShortChannelID{
160✔
1665
                BlockHeight: height,
160✔
1666
        }
160✔
1667

160✔
1668
        // Delete everything after this height from the db up until the
160✔
1669
        // SCID alias range.
160✔
1670
        endShortChanID := aliasmgr.StartingAlias
160✔
1671

160✔
1672
        // The block height will be the 3 first bytes of the channel IDs.
160✔
1673
        var chanIDStart [8]byte
160✔
1674
        byteOrder.PutUint64(chanIDStart[:], startShortChanID.ToUint64())
160✔
1675
        var chanIDEnd [8]byte
160✔
1676
        byteOrder.PutUint64(chanIDEnd[:], endShortChanID.ToUint64())
160✔
1677

160✔
1678
        c.cacheMu.Lock()
160✔
1679
        defer c.cacheMu.Unlock()
160✔
1680

160✔
1681
        // Keep track of the channels that are removed from the graph.
160✔
1682
        var removedChans []*models.ChannelEdgeInfo
160✔
1683

160✔
1684
        if err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
320✔
1685
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
160✔
1686
                if err != nil {
160✔
1687
                        return err
×
1688
                }
×
1689
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
160✔
1690
                if err != nil {
160✔
1691
                        return err
×
1692
                }
×
1693
                chanIndex, err := edges.CreateBucketIfNotExists(
160✔
1694
                        channelPointBucket,
160✔
1695
                )
160✔
1696
                if err != nil {
160✔
1697
                        return err
×
1698
                }
×
1699
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
160✔
1700
                if err != nil {
160✔
1701
                        return err
×
1702
                }
×
1703

1704
                // Scan from chanIDStart to chanIDEnd, deleting every
1705
                // found edge.
1706
                // NOTE: we must delete the edges after the cursor loop, since
1707
                // modifying the bucket while traversing is not safe.
1708
                // NOTE: We use a < comparison in bytes.Compare instead of <=
1709
                // so that the StartingAlias itself isn't deleted.
1710
                var keys [][]byte
160✔
1711
                cursor := edgeIndex.ReadWriteCursor()
160✔
1712

160✔
1713
                //nolint:ll
160✔
1714
                for k, _ := cursor.Seek(chanIDStart[:]); k != nil &&
160✔
1715
                        bytes.Compare(k, chanIDEnd[:]) < 0; k, _ = cursor.Next() {
242✔
1716
                        keys = append(keys, k)
82✔
1717
                }
82✔
1718

1719
                for _, k := range keys {
242✔
1720
                        edgeInfo, err := c.delChannelEdgeUnsafe(
82✔
1721
                                edges, edgeIndex, chanIndex, zombieIndex,
82✔
1722
                                k, false, false,
82✔
1723
                        )
82✔
1724
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
82✔
1725
                                return err
×
1726
                        }
×
1727

1728
                        removedChans = append(removedChans, edgeInfo)
82✔
1729
                }
1730

1731
                // Delete all the entries in the prune log having a height
1732
                // greater or equal to the block disconnected.
1733
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
160✔
1734
                if err != nil {
160✔
1735
                        return err
×
1736
                }
×
1737

1738
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
160✔
1739
                        pruneLogBucket,
160✔
1740
                )
160✔
1741
                if err != nil {
160✔
1742
                        return err
×
1743
                }
×
1744

1745
                var pruneKeyStart [4]byte
160✔
1746
                byteOrder.PutUint32(pruneKeyStart[:], height)
160✔
1747

160✔
1748
                var pruneKeyEnd [4]byte
160✔
1749
                byteOrder.PutUint32(pruneKeyEnd[:], math.MaxUint32)
160✔
1750

160✔
1751
                // To avoid modifying the bucket while traversing, we delete
160✔
1752
                // the keys in a second loop.
160✔
1753
                var pruneKeys [][]byte
160✔
1754
                pruneCursor := pruneBucket.ReadWriteCursor()
160✔
1755
                //nolint:ll
160✔
1756
                for k, _ := pruneCursor.Seek(pruneKeyStart[:]); k != nil &&
160✔
1757
                        bytes.Compare(k, pruneKeyEnd[:]) <= 0; k, _ = pruneCursor.Next() {
257✔
1758
                        pruneKeys = append(pruneKeys, k)
97✔
1759
                }
97✔
1760

1761
                for _, k := range pruneKeys {
257✔
1762
                        if err := pruneBucket.Delete(k); err != nil {
97✔
1763
                                return err
×
1764
                        }
×
1765
                }
1766

1767
                return nil
160✔
1768
        }, func() {
160✔
1769
                removedChans = nil
160✔
1770
        }); err != nil {
160✔
1771
                return nil, err
×
1772
        }
×
1773

1774
        for _, channel := range removedChans {
242✔
1775
                c.rejectCache.remove(channel.ChannelID)
82✔
1776
                c.chanCache.remove(channel.ChannelID)
82✔
1777
        }
82✔
1778

1779
        return removedChans, nil
160✔
1780
}
1781

1782
// PruneTip returns the block height and hash of the latest block that has been
1783
// used to prune channels in the graph. Knowing the "prune tip" allows callers
1784
// to tell if the graph is currently in sync with the current best known UTXO
1785
// state.
1786
func (c *KVStore) PruneTip() (*chainhash.Hash, uint32, error) {
53✔
1787
        var (
53✔
1788
                tipHash   chainhash.Hash
53✔
1789
                tipHeight uint32
53✔
1790
        )
53✔
1791

53✔
1792
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
106✔
1793
                graphMeta := tx.ReadBucket(graphMetaBucket)
53✔
1794
                if graphMeta == nil {
53✔
1795
                        return ErrGraphNotFound
×
1796
                }
×
1797
                pruneBucket := graphMeta.NestedReadBucket(pruneLogBucket)
53✔
1798
                if pruneBucket == nil {
53✔
1799
                        return ErrGraphNeverPruned
×
1800
                }
×
1801

1802
                pruneCursor := pruneBucket.ReadCursor()
53✔
1803

53✔
1804
                // The prune key with the largest block height will be our
53✔
1805
                // prune tip.
53✔
1806
                k, v := pruneCursor.Last()
53✔
1807
                if k == nil {
71✔
1808
                        return ErrGraphNeverPruned
18✔
1809
                }
18✔
1810

1811
                // Once we have the prune tip, the value will be the block hash,
1812
                // and the key the block height.
1813
                copy(tipHash[:], v)
35✔
1814
                tipHeight = byteOrder.Uint32(k)
35✔
1815

35✔
1816
                return nil
35✔
1817
        }, func() {})
53✔
1818
        if err != nil {
71✔
1819
                return nil, 0, err
18✔
1820
        }
18✔
1821

1822
        return &tipHash, tipHeight, nil
35✔
1823
}
1824

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

147✔
1836
        // TODO(roasbeef): possibly delete from node bucket if node has no more
147✔
1837
        // channels
147✔
1838
        // TODO(roasbeef): don't delete both edges?
147✔
1839

147✔
1840
        c.cacheMu.Lock()
147✔
1841
        defer c.cacheMu.Unlock()
147✔
1842

147✔
1843
        var infos []*models.ChannelEdgeInfo
147✔
1844
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
294✔
1845
                edges := tx.ReadWriteBucket(edgeBucket)
147✔
1846
                if edges == nil {
147✔
1847
                        return ErrEdgeNotFound
×
1848
                }
×
1849
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
147✔
1850
                if edgeIndex == nil {
147✔
1851
                        return ErrEdgeNotFound
×
1852
                }
×
1853
                chanIndex := edges.NestedReadWriteBucket(channelPointBucket)
147✔
1854
                if chanIndex == nil {
147✔
1855
                        return ErrEdgeNotFound
×
1856
                }
×
1857
                nodes := tx.ReadWriteBucket(nodeBucket)
147✔
1858
                if nodes == nil {
147✔
1859
                        return ErrGraphNodeNotFound
×
1860
                }
×
1861
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
147✔
1862
                if err != nil {
147✔
1863
                        return err
×
1864
                }
×
1865

1866
                var rawChanID [8]byte
147✔
1867
                for _, chanID := range chanIDs {
230✔
1868
                        byteOrder.PutUint64(rawChanID[:], chanID)
83✔
1869
                        edgeInfo, err := c.delChannelEdgeUnsafe(
83✔
1870
                                edges, edgeIndex, chanIndex, zombieIndex,
83✔
1871
                                rawChanID[:], markZombie, strictZombiePruning,
83✔
1872
                        )
83✔
1873
                        if err != nil {
142✔
1874
                                return err
59✔
1875
                        }
59✔
1876

1877
                        infos = append(infos, edgeInfo)
24✔
1878
                }
1879

1880
                return nil
88✔
1881
        }, func() {
147✔
1882
                infos = nil
147✔
1883
        })
147✔
1884
        if err != nil {
206✔
1885
                return nil, err
59✔
1886
        }
59✔
1887

1888
        for _, chanID := range chanIDs {
112✔
1889
                c.rejectCache.remove(chanID)
24✔
1890
                c.chanCache.remove(chanID)
24✔
1891
        }
24✔
1892

1893
        return infos, nil
88✔
1894
}
1895

1896
// ChannelID attempt to lookup the 8-byte compact channel ID which maps to the
1897
// passed channel point (outpoint). If the passed channel doesn't exist within
1898
// the database, then ErrEdgeNotFound is returned.
1899
func (c *KVStore) ChannelID(chanPoint *wire.OutPoint) (uint64, error) {
1✔
1900
        var chanID uint64
1✔
1901
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
2✔
1902
                var err error
1✔
1903
                chanID, err = getChanID(tx, chanPoint)
1✔
1904
                return err
1✔
1905
        }, func() {
2✔
1906
                chanID = 0
1✔
1907
        }); err != nil {
1✔
UNCOV
1908
                return 0, err
×
UNCOV
1909
        }
×
1910

1911
        return chanID, nil
1✔
1912
}
1913

1914
// getChanID returns the assigned channel ID for a given channel point.
1915
func getChanID(tx kvdb.RTx, chanPoint *wire.OutPoint) (uint64, error) {
1✔
1916
        var b bytes.Buffer
1✔
1917
        if err := WriteOutpoint(&b, chanPoint); err != nil {
1✔
1918
                return 0, err
×
1919
        }
×
1920

1921
        edges := tx.ReadBucket(edgeBucket)
1✔
1922
        if edges == nil {
1✔
1923
                return 0, ErrGraphNoEdgesFound
×
1924
        }
×
1925
        chanIndex := edges.NestedReadBucket(channelPointBucket)
1✔
1926
        if chanIndex == nil {
1✔
1927
                return 0, ErrGraphNoEdgesFound
×
1928
        }
×
1929

1930
        chanIDBytes := chanIndex.Get(b.Bytes())
1✔
1931
        if chanIDBytes == nil {
1✔
UNCOV
1932
                return 0, ErrEdgeNotFound
×
UNCOV
1933
        }
×
1934

1935
        chanID := byteOrder.Uint64(chanIDBytes)
1✔
1936

1✔
1937
        return chanID, nil
1✔
1938
}
1939

1940
// TODO(roasbeef): allow updates to use Batch?
1941

1942
// HighestChanID returns the "highest" known channel ID in the channel graph.
1943
// This represents the "newest" channel from the PoV of the chain. This method
1944
// can be used by peers to quickly determine if they're graphs are in sync.
1945
func (c *KVStore) HighestChanID(_ context.Context) (uint64, error) {
3✔
1946
        var cid uint64
3✔
1947

3✔
1948
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1949
                edges := tx.ReadBucket(edgeBucket)
3✔
1950
                if edges == nil {
3✔
1951
                        return ErrGraphNoEdgesFound
×
1952
                }
×
1953
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
1954
                if edgeIndex == nil {
3✔
1955
                        return ErrGraphNoEdgesFound
×
1956
                }
×
1957

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

3✔
1962
                lastChanID, _ := cidCursor.Last()
3✔
1963

3✔
1964
                // If there's no key, then this means that we don't actually
3✔
1965
                // know of any channels, so we'll return a predicable error.
3✔
1966
                if lastChanID == nil {
4✔
1967
                        return ErrGraphNoEdgesFound
1✔
1968
                }
1✔
1969

1970
                // Otherwise, we'll de serialize the channel ID and return it
1971
                // to the caller.
1972
                cid = byteOrder.Uint64(lastChanID)
2✔
1973

2✔
1974
                return nil
2✔
1975
        }, func() {
3✔
1976
                cid = 0
3✔
1977
        })
3✔
1978
        if err != nil && !errors.Is(err, ErrGraphNoEdgesFound) {
3✔
1979
                return 0, err
×
1980
        }
×
1981

1982
        return cid, nil
3✔
1983
}
1984

1985
// ChannelEdge represents the complete set of information for a channel edge in
1986
// the known channel graph. This struct couples the core information of the
1987
// edge as well as each of the known advertised edge policies.
1988
type ChannelEdge struct {
1989
        // Info contains all the static information describing the channel.
1990
        Info *models.ChannelEdgeInfo
1991

1992
        // Policy1 points to the "first" edge policy of the channel containing
1993
        // the dynamic information required to properly route through the edge.
1994
        Policy1 *models.ChannelEdgePolicy
1995

1996
        // Policy2 points to the "second" edge policy of the channel containing
1997
        // the dynamic information required to properly route through the edge.
1998
        Policy2 *models.ChannelEdgePolicy
1999

2000
        // Node1 is "node 1" in the channel. This is the node that would have
2001
        // produced Policy1 if it exists.
2002
        Node1 *models.LightningNode
2003

2004
        // Node2 is "node 2" in the channel. This is the node that would have
2005
        // produced Policy2 if it exists.
2006
        Node2 *models.LightningNode
2007
}
2008

2009
// ChanUpdatesInHorizon returns all the known channel edges which have at least
2010
// one edge that has an update timestamp within the specified horizon.
2011
func (c *KVStore) ChanUpdatesInHorizon(startTime,
2012
        endTime time.Time) ([]ChannelEdge, error) {
134✔
2013

134✔
2014
        // To ensure we don't return duplicate ChannelEdges, we'll use an
134✔
2015
        // additional map to keep track of the edges already seen to prevent
134✔
2016
        // re-adding it.
134✔
2017
        var edgesSeen map[uint64]struct{}
134✔
2018
        var edgesToCache map[uint64]ChannelEdge
134✔
2019
        var edgesInHorizon []ChannelEdge
134✔
2020

134✔
2021
        c.cacheMu.Lock()
134✔
2022
        defer c.cacheMu.Unlock()
134✔
2023

134✔
2024
        var hits int
134✔
2025
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
268✔
2026
                edges := tx.ReadBucket(edgeBucket)
134✔
2027
                if edges == nil {
134✔
2028
                        return ErrGraphNoEdgesFound
×
2029
                }
×
2030
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
134✔
2031
                if edgeIndex == nil {
134✔
2032
                        return ErrGraphNoEdgesFound
×
2033
                }
×
2034
                edgeUpdateIndex := edges.NestedReadBucket(edgeUpdateIndexBucket)
134✔
2035
                if edgeUpdateIndex == nil {
134✔
2036
                        return ErrGraphNoEdgesFound
×
2037
                }
×
2038

2039
                nodes := tx.ReadBucket(nodeBucket)
134✔
2040
                if nodes == nil {
134✔
2041
                        return ErrGraphNodesNotFound
×
2042
                }
×
2043

2044
                // We'll now obtain a cursor to perform a range query within
2045
                // the index to find all channels within the horizon.
2046
                updateCursor := edgeUpdateIndex.ReadCursor()
134✔
2047

134✔
2048
                var startTimeBytes, endTimeBytes [8 + 8]byte
134✔
2049
                byteOrder.PutUint64(
134✔
2050
                        startTimeBytes[:8], uint64(startTime.Unix()),
134✔
2051
                )
134✔
2052
                byteOrder.PutUint64(
134✔
2053
                        endTimeBytes[:8], uint64(endTime.Unix()),
134✔
2054
                )
134✔
2055

134✔
2056
                // With our start and end times constructed, we'll step through
134✔
2057
                // the index collecting the info and policy of each update of
134✔
2058
                // each channel that has a last update within the time range.
134✔
2059
                //
134✔
2060
                //nolint:ll
134✔
2061
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
134✔
2062
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
180✔
2063
                        // We have a new eligible entry, so we'll slice of the
46✔
2064
                        // chan ID so we can query it in the DB.
46✔
2065
                        chanID := indexKey[8:]
46✔
2066

46✔
2067
                        // If we've already retrieved the info and policies for
46✔
2068
                        // this edge, then we can skip it as we don't need to do
46✔
2069
                        // so again.
46✔
2070
                        chanIDInt := byteOrder.Uint64(chanID)
46✔
2071
                        if _, ok := edgesSeen[chanIDInt]; ok {
65✔
2072
                                continue
19✔
2073
                        }
2074

2075
                        if channel, ok := c.chanCache.get(chanIDInt); ok {
36✔
2076
                                hits++
9✔
2077
                                edgesSeen[chanIDInt] = struct{}{}
9✔
2078
                                edgesInHorizon = append(edgesInHorizon, channel)
9✔
2079

9✔
2080
                                continue
9✔
2081
                        }
2082

2083
                        // First, we'll fetch the static edge information.
2084
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
18✔
2085
                        if err != nil {
18✔
2086
                                chanID := byteOrder.Uint64(chanID)
×
2087
                                return fmt.Errorf("unable to fetch info for "+
×
2088
                                        "edge with chan_id=%v: %v", chanID, err)
×
2089
                        }
×
2090

2091
                        // With the static information obtained, we'll now
2092
                        // fetch the dynamic policy info.
2093
                        edge1, edge2, err := fetchChanEdgePolicies(
18✔
2094
                                edgeIndex, edges, chanID,
18✔
2095
                        )
18✔
2096
                        if err != nil {
18✔
2097
                                chanID := byteOrder.Uint64(chanID)
×
2098
                                return fmt.Errorf("unable to fetch policies "+
×
2099
                                        "for edge with chan_id=%v: %v", chanID,
×
2100
                                        err)
×
2101
                        }
×
2102

2103
                        node1, err := fetchLightningNode(
18✔
2104
                                nodes, edgeInfo.NodeKey1Bytes[:],
18✔
2105
                        )
18✔
2106
                        if err != nil {
18✔
2107
                                return err
×
2108
                        }
×
2109

2110
                        node2, err := fetchLightningNode(
18✔
2111
                                nodes, edgeInfo.NodeKey2Bytes[:],
18✔
2112
                        )
18✔
2113
                        if err != nil {
18✔
2114
                                return err
×
2115
                        }
×
2116

2117
                        // Finally, we'll collate this edge with the rest of
2118
                        // edges to be returned.
2119
                        edgesSeen[chanIDInt] = struct{}{}
18✔
2120
                        channel := ChannelEdge{
18✔
2121
                                Info:    &edgeInfo,
18✔
2122
                                Policy1: edge1,
18✔
2123
                                Policy2: edge2,
18✔
2124
                                Node1:   &node1,
18✔
2125
                                Node2:   &node2,
18✔
2126
                        }
18✔
2127
                        edgesInHorizon = append(edgesInHorizon, channel)
18✔
2128
                        edgesToCache[chanIDInt] = channel
18✔
2129
                }
2130

2131
                return nil
134✔
2132
        }, func() {
134✔
2133
                edgesSeen = make(map[uint64]struct{})
134✔
2134
                edgesToCache = make(map[uint64]ChannelEdge)
134✔
2135
                edgesInHorizon = nil
134✔
2136
        })
134✔
2137
        switch {
134✔
2138
        case errors.Is(err, ErrGraphNoEdgesFound):
×
2139
                fallthrough
×
2140
        case errors.Is(err, ErrGraphNodesNotFound):
×
2141
                break
×
2142

2143
        case err != nil:
×
2144
                return nil, err
×
2145
        }
2146

2147
        // Insert any edges loaded from disk into the cache.
2148
        for chanid, channel := range edgesToCache {
152✔
2149
                c.chanCache.insert(chanid, channel)
18✔
2150
        }
18✔
2151

2152
        if len(edgesInHorizon) > 0 {
139✔
2153
                log.Debugf("ChanUpdatesInHorizon hit percentage: %f (%d/%d)",
5✔
2154
                        float64(hits)/float64(len(edgesInHorizon)), hits,
5✔
2155
                        len(edgesInHorizon))
5✔
2156
        } else {
134✔
2157
                log.Debugf("ChanUpdatesInHorizon returned no edges in "+
129✔
2158
                        "horizon (%s, %s)", startTime, endTime)
129✔
2159
        }
129✔
2160

2161
        return edgesInHorizon, nil
134✔
2162
}
2163

2164
// NodeUpdatesInHorizon returns all the known lightning node which have an
2165
// update timestamp within the passed range. This method can be used by two
2166
// nodes to quickly determine if they have the same set of up to date node
2167
// announcements.
2168
func (c *KVStore) NodeUpdatesInHorizon(startTime,
2169
        endTime time.Time) ([]models.LightningNode, error) {
8✔
2170

8✔
2171
        var nodesInHorizon []models.LightningNode
8✔
2172

8✔
2173
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
16✔
2174
                nodes := tx.ReadBucket(nodeBucket)
8✔
2175
                if nodes == nil {
8✔
2176
                        return ErrGraphNodesNotFound
×
2177
                }
×
2178

2179
                nodeUpdateIndex := nodes.NestedReadBucket(nodeUpdateIndexBucket)
8✔
2180
                if nodeUpdateIndex == nil {
8✔
2181
                        return ErrGraphNodesNotFound
×
2182
                }
×
2183

2184
                // We'll now obtain a cursor to perform a range query within
2185
                // the index to find all node announcements within the horizon.
2186
                updateCursor := nodeUpdateIndex.ReadCursor()
8✔
2187

8✔
2188
                var startTimeBytes, endTimeBytes [8 + 33]byte
8✔
2189
                byteOrder.PutUint64(
8✔
2190
                        startTimeBytes[:8], uint64(startTime.Unix()),
8✔
2191
                )
8✔
2192
                byteOrder.PutUint64(
8✔
2193
                        endTimeBytes[:8], uint64(endTime.Unix()),
8✔
2194
                )
8✔
2195

8✔
2196
                // With our start and end times constructed, we'll step through
8✔
2197
                // the index collecting info for each node within the time
8✔
2198
                // range.
8✔
2199
                //
8✔
2200
                //nolint:ll
8✔
2201
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
8✔
2202
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
37✔
2203
                        nodePub := indexKey[8:]
29✔
2204
                        node, err := fetchLightningNode(nodes, nodePub)
29✔
2205
                        if err != nil {
29✔
2206
                                return err
×
2207
                        }
×
2208

2209
                        nodesInHorizon = append(nodesInHorizon, node)
29✔
2210
                }
2211

2212
                return nil
8✔
2213
        }, func() {
8✔
2214
                nodesInHorizon = nil
8✔
2215
        })
8✔
2216
        switch {
8✔
2217
        case errors.Is(err, ErrGraphNoEdgesFound):
×
2218
                fallthrough
×
2219
        case errors.Is(err, ErrGraphNodesNotFound):
×
2220
                break
×
2221

2222
        case err != nil:
×
2223
                return nil, err
×
2224
        }
2225

2226
        return nodesInHorizon, nil
8✔
2227
}
2228

2229
// FilterKnownChanIDs takes a set of channel IDs and return the subset of chan
2230
// ID's that we don't know and are not known zombies of the passed set. In other
2231
// words, we perform a set difference of our set of chan ID's and the ones
2232
// passed in. This method can be used by callers to determine the set of
2233
// channels another peer knows of that we don't. The ChannelUpdateInfos for the
2234
// known zombies is also returned.
2235
func (c *KVStore) FilterKnownChanIDs(chansInfo []ChannelUpdateInfo) ([]uint64,
2236
        []ChannelUpdateInfo, error) {
128✔
2237

128✔
2238
        var (
128✔
2239
                newChanIDs   []uint64
128✔
2240
                knownZombies []ChannelUpdateInfo
128✔
2241
        )
128✔
2242

128✔
2243
        c.cacheMu.Lock()
128✔
2244
        defer c.cacheMu.Unlock()
128✔
2245

128✔
2246
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
256✔
2247
                edges := tx.ReadBucket(edgeBucket)
128✔
2248
                if edges == nil {
128✔
2249
                        return ErrGraphNoEdgesFound
×
2250
                }
×
2251
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
128✔
2252
                if edgeIndex == nil {
128✔
2253
                        return ErrGraphNoEdgesFound
×
2254
                }
×
2255

2256
                // Fetch the zombie index, it may not exist if no edges have
2257
                // ever been marked as zombies. If the index has been
2258
                // initialized, we will use it later to skip known zombie edges.
2259
                zombieIndex := edges.NestedReadBucket(zombieBucket)
128✔
2260

128✔
2261
                // We'll run through the set of chanIDs and collate only the
128✔
2262
                // set of channel that are unable to be found within our db.
128✔
2263
                var cidBytes [8]byte
128✔
2264
                for _, info := range chansInfo {
242✔
2265
                        scid := info.ShortChannelID.ToUint64()
114✔
2266
                        byteOrder.PutUint64(cidBytes[:], scid)
114✔
2267

114✔
2268
                        // If the edge is already known, skip it.
114✔
2269
                        if v := edgeIndex.Get(cidBytes[:]); v != nil {
135✔
2270
                                continue
21✔
2271
                        }
2272

2273
                        // If the edge is a known zombie, skip it.
2274
                        if zombieIndex != nil {
186✔
2275
                                isZombie, _, _ := isZombieEdge(
93✔
2276
                                        zombieIndex, scid,
93✔
2277
                                )
93✔
2278

93✔
2279
                                if isZombie {
140✔
2280
                                        knownZombies = append(
47✔
2281
                                                knownZombies, info,
47✔
2282
                                        )
47✔
2283

47✔
2284
                                        continue
47✔
2285
                                }
2286
                        }
2287

2288
                        newChanIDs = append(newChanIDs, scid)
46✔
2289
                }
2290

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

2305
                return ogChanIDs, nil, nil
×
2306

2307
        case err != nil:
×
2308
                return nil, nil, err
×
2309
        }
2310

2311
        return newChanIDs, knownZombies, nil
128✔
2312
}
2313

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

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

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

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

196✔
2337
        chanInfo := ChannelUpdateInfo{
196✔
2338
                ShortChannelID:       scid,
196✔
2339
                Node1UpdateTimestamp: node1Timestamp,
196✔
2340
                Node2UpdateTimestamp: node2Timestamp,
196✔
2341
        }
196✔
2342

196✔
2343
        if node1Timestamp.IsZero() {
382✔
2344
                chanInfo.Node1UpdateTimestamp = time.Unix(0, 0)
186✔
2345
        }
186✔
2346

2347
        if node2Timestamp.IsZero() {
382✔
2348
                chanInfo.Node2UpdateTimestamp = time.Unix(0, 0)
186✔
2349
        }
186✔
2350

2351
        return chanInfo
196✔
2352
}
2353

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

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

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

11✔
2377
        startChanID := &lnwire.ShortChannelID{
11✔
2378
                BlockHeight: startHeight,
11✔
2379
        }
11✔
2380

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

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

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

2405
                cursor := edgeIndex.ReadCursor()
11✔
2406

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

2420
                        if edgeInfo.AuthProof == nil {
44✔
UNCOV
2421
                                continue
×
2422
                        }
2423

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

44✔
2429
                        chanInfo := NewChannelUpdateInfo(
44✔
2430
                                cid, time.Time{}, time.Time{},
44✔
2431
                        )
44✔
2432

44✔
2433
                        if !withTimestamps {
66✔
2434
                                channelsPerBlock[cid.BlockHeight] = append(
22✔
2435
                                        channelsPerBlock[cid.BlockHeight],
22✔
2436
                                        chanInfo,
22✔
2437
                                )
22✔
2438

22✔
2439
                                continue
22✔
2440
                        }
2441

2442
                        node1Key, node2Key := computeEdgePolicyKeys(&edgeInfo)
22✔
2443

22✔
2444
                        rawPolicy := edges.Get(node1Key)
22✔
2445
                        if len(rawPolicy) != 0 {
28✔
2446
                                r := bytes.NewReader(rawPolicy)
6✔
2447

6✔
2448
                                edge, err := deserializeChanEdgePolicyRaw(r)
6✔
2449
                                if err != nil && !errors.Is(
6✔
2450
                                        err, ErrEdgePolicyOptionalFieldNotFound,
6✔
2451
                                ) && !errors.Is(err, ErrParsingExtraTLVBytes) {
6✔
2452

×
2453
                                        return err
×
2454
                                }
×
2455

2456
                                chanInfo.Node1UpdateTimestamp = edge.LastUpdate
6✔
2457
                        }
2458

2459
                        rawPolicy = edges.Get(node2Key)
22✔
2460
                        if len(rawPolicy) != 0 {
33✔
2461
                                r := bytes.NewReader(rawPolicy)
11✔
2462

11✔
2463
                                edge, err := deserializeChanEdgePolicyRaw(r)
11✔
2464
                                if err != nil && !errors.Is(
11✔
2465
                                        err, ErrEdgePolicyOptionalFieldNotFound,
11✔
2466
                                ) && !errors.Is(err, ErrParsingExtraTLVBytes) {
11✔
2467

×
2468
                                        return err
×
2469
                                }
×
2470

2471
                                chanInfo.Node2UpdateTimestamp = edge.LastUpdate
11✔
2472
                        }
2473

2474
                        channelsPerBlock[cid.BlockHeight] = append(
22✔
2475
                                channelsPerBlock[cid.BlockHeight], chanInfo,
22✔
2476
                        )
22✔
2477
                }
2478

2479
                return nil
11✔
2480
        }, func() {
11✔
2481
                channelsPerBlock = make(map[uint32][]ChannelUpdateInfo)
11✔
2482
        })
11✔
2483

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

2490
        case err != nil:
×
2491
                return nil, err
×
2492
        }
2493

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

2503
        channelRanges := make([]BlockChannelRange, 0, len(channelsPerBlock))
8✔
2504
        for _, block := range blocks {
30✔
2505
                channelRanges = append(channelRanges, BlockChannelRange{
22✔
2506
                        Height:   block,
22✔
2507
                        Channels: channelsPerBlock[block],
22✔
2508
                })
22✔
2509
        }
22✔
2510

2511
        return channelRanges, nil
8✔
2512
}
2513

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

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

4✔
2535
        var (
4✔
2536
                chanEdges []ChannelEdge
4✔
2537
                cidBytes  [8]byte
4✔
2538
        )
4✔
2539

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

2554
                for _, cid := range chanIDs {
15✔
2555
                        byteOrder.PutUint64(cidBytes[:], cid)
11✔
2556

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

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

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

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

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

2602
                return nil
4✔
2603
        }
2604

2605
        if tx == nil {
8✔
2606
                err := kvdb.View(c.db, fetchChanInfos, func() {
8✔
2607
                        chanEdges = nil
4✔
2608
                })
4✔
2609
                if err != nil {
4✔
2610
                        return nil, err
×
2611
                }
×
2612

2613
                return chanEdges, nil
4✔
2614
        }
2615

2616
        err := fetchChanInfos(tx)
×
2617
        if err != nil {
×
2618
                return nil, err
×
2619
        }
×
2620

2621
        return chanEdges, nil
×
2622
}
2623

2624
func delEdgeUpdateIndexEntry(edgesBucket kvdb.RwBucket, chanID uint64,
2625
        edge1, edge2 *models.ChannelEdgePolicy) error {
130✔
2626

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

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

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

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

2663
        return nil
130✔
2664
}
2665

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

189✔
2677
        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
189✔
2678
        if err != nil {
248✔
2679
                return nil, err
59✔
2680
        }
59✔
2681

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

2695
        // The edge key is of the format pubKey || chanID. First we construct
2696
        // the latter half, populating the channel ID.
2697
        var edgeKey [33 + 8]byte
130✔
2698
        copy(edgeKey[33:], chanID)
130✔
2699

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

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

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

2741
        // Finally, we'll mark the edge as a zombie within our index if it's
2742
        // being removed due to the channel becoming a zombie. We do this to
2743
        // ensure we don't store unnecessary data for spent channels.
2744
        if !isZombie {
237✔
2745
                return &edgeInfo, nil
107✔
2746
        }
107✔
2747

2748
        nodeKey1, nodeKey2 := edgeInfo.NodeKey1Bytes, edgeInfo.NodeKey2Bytes
23✔
2749
        if strictZombie {
26✔
2750
                var e1UpdateTime, e2UpdateTime *time.Time
3✔
2751
                if edge1 != nil {
5✔
2752
                        e1UpdateTime = &edge1.LastUpdate
2✔
2753
                }
2✔
2754
                if edge2 != nil {
6✔
2755
                        e2UpdateTime = &edge2.LastUpdate
3✔
2756
                }
3✔
2757

2758
                nodeKey1, nodeKey2 = makeZombiePubkeys(
3✔
2759
                        &edgeInfo, e1UpdateTime, e2UpdateTime,
3✔
2760
                )
3✔
2761
        }
2762

2763
        return &edgeInfo, markEdgeZombie(
23✔
2764
                zombieIndex, byteOrder.Uint64(chanID), nodeKey1, nodeKey2,
23✔
2765
        )
23✔
2766
}
2767

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

3✔
2787
        switch {
3✔
2788
        // If we don't have either edge policy, we'll return both pubkeys so
2789
        // that the channel can be resurrected by either party.
UNCOV
2790
        case e1 == nil && e2 == nil:
×
UNCOV
2791
                return info.NodeKey1Bytes, info.NodeKey2Bytes
×
2792

2793
        // If we're missing edge1, or if both edges are present but edge1 is
2794
        // older, we'll return edge1's pubkey and a blank pubkey for edge2. This
2795
        // means that only an update from edge1 will be able to resurrect the
2796
        // channel.
2797
        case e1 == nil || (e2 != nil && e1.Before(*e2)):
1✔
2798
                return info.NodeKey1Bytes, [33]byte{}
1✔
2799

2800
        // Otherwise, we're missing edge2 or edge2 is the older side, so we
2801
        // return a blank pubkey for edge1. In this case, only an update from
2802
        // edge2 can resurect the channel.
2803
        default:
2✔
2804
                return [33]byte{}, info.NodeKey2Bytes
2✔
2805
        }
2806
}
2807

2808
// UpdateEdgePolicy updates the edge routing policy for a single directed edge
2809
// within the database for the referenced channel. The `flags` attribute within
2810
// the ChannelEdgePolicy determines which of the directed edges are being
2811
// updated. If the flag is 1, then the first node's information is being
2812
// updated, otherwise it's the second node's information. The node ordering is
2813
// determined by the lexicographical ordering of the identity public keys of the
2814
// nodes on either side of the channel.
2815
func (c *KVStore) UpdateEdgePolicy(ctx context.Context,
2816
        edge *models.ChannelEdgePolicy,
2817
        opts ...batch.SchedulerOption) (route.Vertex, route.Vertex, error) {
2,665✔
2818

2,665✔
2819
        var (
2,665✔
2820
                isUpdate1    bool
2,665✔
2821
                edgeNotFound bool
2,665✔
2822
                from, to     route.Vertex
2,665✔
2823
        )
2,665✔
2824

2,665✔
2825
        r := &batch.Request[kvdb.RwTx]{
2,665✔
2826
                Opts: batch.NewSchedulerOptions(opts...),
2,665✔
2827
                Reset: func() {
5,331✔
2828
                        isUpdate1 = false
2,666✔
2829
                        edgeNotFound = false
2,666✔
2830
                },
2,666✔
2831
                Do: func(tx kvdb.RwTx) error {
2,666✔
2832
                        var err error
2,666✔
2833
                        from, to, isUpdate1, err = updateEdgePolicy(tx, edge)
2,666✔
2834
                        if err != nil {
2,671✔
2835
                                log.Errorf("UpdateEdgePolicy faild: %v", err)
5✔
2836
                        }
5✔
2837

2838
                        // Silence ErrEdgeNotFound so that the batch can
2839
                        // succeed, but propagate the error via local state.
2840
                        if errors.Is(err, ErrEdgeNotFound) {
2,669✔
2841
                                edgeNotFound = true
3✔
2842
                                return nil
3✔
2843
                        }
3✔
2844

2845
                        return err
2,663✔
2846
                },
2847
                OnCommit: func(err error) error {
2,665✔
2848
                        switch {
2,665✔
2849
                        case err != nil:
1✔
2850
                                return err
1✔
2851
                        case edgeNotFound:
3✔
2852
                                return ErrEdgeNotFound
3✔
2853
                        default:
2,661✔
2854
                                c.updateEdgeCache(edge, isUpdate1)
2,661✔
2855
                                return nil
2,661✔
2856
                        }
2857
                },
2858
        }
2859

2860
        err := c.chanScheduler.Execute(ctx, r)
2,665✔
2861

2,665✔
2862
        return from, to, err
2,665✔
2863
}
2864

2865
func (c *KVStore) updateEdgeCache(e *models.ChannelEdgePolicy,
2866
        isUpdate1 bool) {
2,661✔
2867

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

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

2895
// updateEdgePolicy attempts to update an edge's policy within the relevant
2896
// buckets using an existing database transaction. The returned boolean will be
2897
// true if the updated policy belongs to node1, and false if the policy belonged
2898
// to node2.
2899
func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy) (
2900
        route.Vertex, route.Vertex, bool, error) {
2,666✔
2901

2,666✔
2902
        var noVertex route.Vertex
2,666✔
2903

2,666✔
2904
        edges := tx.ReadWriteBucket(edgeBucket)
2,666✔
2905
        if edges == nil {
2,666✔
2906
                return noVertex, noVertex, false, ErrEdgeNotFound
×
2907
        }
×
2908
        edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
2,666✔
2909
        if edgeIndex == nil {
2,666✔
2910
                return noVertex, noVertex, false, ErrEdgeNotFound
×
2911
        }
×
2912

2913
        // Create the channelID key be converting the channel ID
2914
        // integer into a byte slice.
2915
        var chanID [8]byte
2,666✔
2916
        byteOrder.PutUint64(chanID[:], edge.ChannelID)
2,666✔
2917

2,666✔
2918
        // With the channel ID, we then fetch the value storing the two
2,666✔
2919
        // nodes which connect this channel edge.
2,666✔
2920
        nodeInfo := edgeIndex.Get(chanID[:])
2,666✔
2921
        if nodeInfo == nil {
2,669✔
2922
                return noVertex, noVertex, false, ErrEdgeNotFound
3✔
2923
        }
3✔
2924

2925
        // Depending on the flags value passed above, either the first
2926
        // or second edge policy is being updated.
2927
        var fromNode, toNode []byte
2,663✔
2928
        var isUpdate1 bool
2,663✔
2929
        if edge.ChannelFlags&lnwire.ChanUpdateDirection == 0 {
3,998✔
2930
                fromNode = nodeInfo[:33]
1,335✔
2931
                toNode = nodeInfo[33:66]
1,335✔
2932
                isUpdate1 = true
1,335✔
2933
        } else {
2,663✔
2934
                fromNode = nodeInfo[33:66]
1,328✔
2935
                toNode = nodeInfo[:33]
1,328✔
2936
                isUpdate1 = false
1,328✔
2937
        }
1,328✔
2938

2939
        // Finally, with the direction of the edge being updated
2940
        // identified, we update the on-disk edge representation.
2941
        err := putChanEdgePolicy(edges, edge, fromNode, toNode)
2,663✔
2942
        if err != nil {
2,665✔
2943
                return noVertex, noVertex, false, err
2✔
2944
        }
2✔
2945

2946
        var (
2,661✔
2947
                fromNodePubKey route.Vertex
2,661✔
2948
                toNodePubKey   route.Vertex
2,661✔
2949
        )
2,661✔
2950
        copy(fromNodePubKey[:], fromNode)
2,661✔
2951
        copy(toNodePubKey[:], toNode)
2,661✔
2952

2,661✔
2953
        return fromNodePubKey, toNodePubKey, isUpdate1, nil
2,661✔
2954
}
2955

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

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

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

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

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

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

2997
        return nodeIsPublic, nil
13✔
2998
}
2999

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

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

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

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

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

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

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

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

3050
                node = &n
3,795✔
3051

3,795✔
3052
                return nil
3,795✔
3053
        }
3054

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

3065
                return node, nil
179✔
3066
        }
3067

3068
        err := fetch(tx)
3,627✔
3069
        if err != nil {
3,638✔
3070
                return nil, err
11✔
3071
        }
11✔
3072

3073
        return node, nil
3,616✔
3074
}
3075

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

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

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

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

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

3114
                exists = true
14✔
3115
                updateTime = node.LastUpdate
14✔
3116

14✔
3117
                return nil
14✔
3118
        }, func() {
17✔
3119
                updateTime = time.Time{}
17✔
3120
                exists = false
17✔
3121
        })
17✔
3122
        if err != nil {
17✔
3123
                return time.Time{}, exists, err
×
3124
        }
×
3125

3126
        return updateTime, exists, nil
17✔
3127
}
3128

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

1,267✔
3135
        traversal := func(tx kvdb.RTx) error {
2,534✔
3136
                edges := tx.ReadBucket(edgeBucket)
1,267✔
3137
                if edges == nil {
1,267✔
3138
                        return ErrGraphNotFound
×
3139
                }
×
3140
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
1,267✔
3141
                if edgeIndex == nil {
1,267✔
3142
                        return ErrGraphNoEdgesFound
×
3143
                }
×
3144

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

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

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

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

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

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

3198
                return nil
1,258✔
3199
        }
3200

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

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

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

6✔
3224
        return nodeTraversal(nil, nodePub[:], c.db, func(_ kvdb.RTx,
6✔
3225
                info *models.ChannelEdgeInfo, policy,
6✔
3226
                policy2 *models.ChannelEdgePolicy) error {
16✔
3227

10✔
3228
                return cb(info, policy, policy2)
10✔
3229
        })
10✔
3230
}
3231

3232
// ForEachSourceNodeChannel iterates through all channels of the source node,
3233
// executing the passed callback on each. The callback is provided with the
3234
// channel's outpoint, whether we have a policy for the channel and the channel
3235
// peer's node information.
3236
func (c *KVStore) ForEachSourceNodeChannel(cb func(chanPoint wire.OutPoint,
3237
        havePolicy bool, otherNode *models.LightningNode) error) error {
1✔
3238

1✔
3239
        return kvdb.View(c.db, func(tx kvdb.RTx) error {
2✔
3240
                nodes := tx.ReadBucket(nodeBucket)
1✔
3241
                if nodes == nil {
1✔
3242
                        return ErrGraphNotFound
×
3243
                }
×
3244

3245
                node, err := c.sourceNode(nodes)
1✔
3246
                if err != nil {
1✔
3247
                        return err
×
3248
                }
×
3249

3250
                return nodeTraversal(
1✔
3251
                        tx, node.PubKeyBytes[:], c.db, func(tx kvdb.RTx,
1✔
3252
                                info *models.ChannelEdgeInfo,
1✔
3253
                                policy, _ *models.ChannelEdgePolicy) error {
3✔
3254

2✔
3255
                                peer, err := c.fetchOtherNode(
2✔
3256
                                        tx, info, node.PubKeyBytes[:],
2✔
3257
                                )
2✔
3258
                                if err != nil {
2✔
3259
                                        return err
×
3260
                                }
×
3261

3262
                                return cb(
2✔
3263
                                        info.ChannelPoint, policy != nil, peer,
2✔
3264
                                )
2✔
3265
                        },
3266
                )
3267
        }, func() {})
1✔
3268
}
3269

3270
// forEachNodeChannelTx iterates through all channels of the given node,
3271
// executing the passed callback with an edge info structure and the policies
3272
// of each end of the channel. The first edge policy is the outgoing edge *to*
3273
// the connecting node, while the second is the incoming edge *from* the
3274
// connecting node. If the callback returns an error, then the iteration is
3275
// halted with the error propagated back up to the caller.
3276
//
3277
// Unknown policies are passed into the callback as nil values.
3278
//
3279
// If the caller wishes to re-use an existing boltdb transaction, then it
3280
// should be passed as the first argument.  Otherwise, the first argument should
3281
// be nil and a fresh transaction will be created to execute the graph
3282
// traversal.
3283
func (c *KVStore) forEachNodeChannelTx(tx kvdb.RTx,
3284
        nodePub route.Vertex, cb func(kvdb.RTx, *models.ChannelEdgeInfo,
3285
                *models.ChannelEdgePolicy,
3286
                *models.ChannelEdgePolicy) error) error {
998✔
3287

998✔
3288
        return nodeTraversal(tx, nodePub[:], c.db, cb)
998✔
3289
}
998✔
3290

3291
// fetchOtherNode attempts to fetch the full LightningNode that's opposite of
3292
// the target node in the channel. This is useful when one knows the pubkey of
3293
// one of the nodes, and wishes to obtain the full LightningNode for the other
3294
// end of the channel.
3295
func (c *KVStore) fetchOtherNode(tx kvdb.RTx,
3296
        channel *models.ChannelEdgeInfo, thisNodeKey []byte) (
3297
        *models.LightningNode, error) {
2✔
3298

2✔
3299
        // Ensure that the node passed in is actually a member of the channel.
2✔
3300
        var targetNodeBytes [33]byte
2✔
3301
        switch {
2✔
3302
        case bytes.Equal(channel.NodeKey1Bytes[:], thisNodeKey):
1✔
3303
                targetNodeBytes = channel.NodeKey2Bytes
1✔
3304
        case bytes.Equal(channel.NodeKey2Bytes[:], thisNodeKey):
1✔
3305
                targetNodeBytes = channel.NodeKey1Bytes
1✔
3306
        default:
×
3307
                return nil, fmt.Errorf("node not participating in this channel")
×
3308
        }
3309

3310
        var targetNode *models.LightningNode
2✔
3311
        fetchNodeFunc := func(tx kvdb.RTx) error {
4✔
3312
                // First grab the nodes bucket which stores the mapping from
2✔
3313
                // pubKey to node information.
2✔
3314
                nodes := tx.ReadBucket(nodeBucket)
2✔
3315
                if nodes == nil {
2✔
3316
                        return ErrGraphNotFound
×
3317
                }
×
3318

3319
                node, err := fetchLightningNode(nodes, targetNodeBytes[:])
2✔
3320
                if err != nil {
2✔
3321
                        return err
×
3322
                }
×
3323

3324
                targetNode = &node
2✔
3325

2✔
3326
                return nil
2✔
3327
        }
3328

3329
        // If the transaction is nil, then we'll need to create a new one,
3330
        // otherwise we can use the existing db transaction.
3331
        var err error
2✔
3332
        if tx == nil {
2✔
3333
                err = kvdb.View(c.db, fetchNodeFunc, func() {
×
3334
                        targetNode = nil
×
3335
                })
×
3336
        } else {
2✔
3337
                err = fetchNodeFunc(tx)
2✔
3338
        }
2✔
3339

3340
        return targetNode, err
2✔
3341
}
3342

3343
// computeEdgePolicyKeys is a helper function that can be used to compute the
3344
// keys used to index the channel edge policy info for the two nodes of the
3345
// edge. The keys for node 1 and node 2 are returned respectively.
3346
func computeEdgePolicyKeys(info *models.ChannelEdgeInfo) ([]byte, []byte) {
22✔
3347
        var (
22✔
3348
                node1Key [33 + 8]byte
22✔
3349
                node2Key [33 + 8]byte
22✔
3350
        )
22✔
3351

22✔
3352
        copy(node1Key[:], info.NodeKey1Bytes[:])
22✔
3353
        copy(node2Key[:], info.NodeKey2Bytes[:])
22✔
3354

22✔
3355
        byteOrder.PutUint64(node1Key[33:], info.ChannelID)
22✔
3356
        byteOrder.PutUint64(node2Key[33:], info.ChannelID)
22✔
3357

22✔
3358
        return node1Key[:], node2Key[:]
22✔
3359
}
22✔
3360

3361
// FetchChannelEdgesByOutpoint attempts to lookup the two directed edges for
3362
// the channel identified by the funding outpoint. If the channel can't be
3363
// found, then ErrEdgeNotFound is returned. A struct which houses the general
3364
// information for the channel itself is returned as well as two structs that
3365
// contain the routing policies for the channel in either direction.
3366
func (c *KVStore) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
3367
        *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3368
        *models.ChannelEdgePolicy, error) {
11✔
3369

11✔
3370
        var (
11✔
3371
                edgeInfo *models.ChannelEdgeInfo
11✔
3372
                policy1  *models.ChannelEdgePolicy
11✔
3373
                policy2  *models.ChannelEdgePolicy
11✔
3374
        )
11✔
3375

11✔
3376
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
22✔
3377
                // First, grab the node bucket. This will be used to populate
11✔
3378
                // the Node pointers in each edge read from disk.
11✔
3379
                nodes := tx.ReadBucket(nodeBucket)
11✔
3380
                if nodes == nil {
11✔
3381
                        return ErrGraphNotFound
×
3382
                }
×
3383

3384
                // Next, grab the edge bucket which stores the edges, and also
3385
                // the index itself so we can group the directed edges together
3386
                // logically.
3387
                edges := tx.ReadBucket(edgeBucket)
11✔
3388
                if edges == nil {
11✔
3389
                        return ErrGraphNoEdgesFound
×
3390
                }
×
3391
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
11✔
3392
                if edgeIndex == nil {
11✔
3393
                        return ErrGraphNoEdgesFound
×
3394
                }
×
3395

3396
                // If the channel's outpoint doesn't exist within the outpoint
3397
                // index, then the edge does not exist.
3398
                chanIndex := edges.NestedReadBucket(channelPointBucket)
11✔
3399
                if chanIndex == nil {
11✔
3400
                        return ErrGraphNoEdgesFound
×
3401
                }
×
3402
                var b bytes.Buffer
11✔
3403
                if err := WriteOutpoint(&b, op); err != nil {
11✔
3404
                        return err
×
3405
                }
×
3406
                chanID := chanIndex.Get(b.Bytes())
11✔
3407
                if chanID == nil {
21✔
3408
                        return fmt.Errorf("%w: op=%v", ErrEdgeNotFound, op)
10✔
3409
                }
10✔
3410

3411
                // If the channel is found to exists, then we'll first retrieve
3412
                // the general information for the channel.
3413
                edge, err := fetchChanEdgeInfo(edgeIndex, chanID)
1✔
3414
                if err != nil {
1✔
3415
                        return fmt.Errorf("%w: chanID=%x", err, chanID)
×
3416
                }
×
3417
                edgeInfo = &edge
1✔
3418

1✔
3419
                // Once we have the information about the channels' parameters,
1✔
3420
                // we'll fetch the routing policies for each for the directed
1✔
3421
                // edges.
1✔
3422
                e1, e2, err := fetchChanEdgePolicies(edgeIndex, edges, chanID)
1✔
3423
                if err != nil {
1✔
3424
                        return fmt.Errorf("failed to find policy: %w", err)
×
3425
                }
×
3426

3427
                policy1 = e1
1✔
3428
                policy2 = e2
1✔
3429

1✔
3430
                return nil
1✔
3431
        }, func() {
11✔
3432
                edgeInfo = nil
11✔
3433
                policy1 = nil
11✔
3434
                policy2 = nil
11✔
3435
        })
11✔
3436
        if err != nil {
21✔
3437
                return nil, nil, nil, err
10✔
3438
        }
10✔
3439

3440
        return edgeInfo, policy1, policy2, nil
1✔
3441
}
3442

3443
// FetchChannelEdgesByID attempts to lookup the two directed edges for the
3444
// channel identified by the channel ID. If the channel can't be found, then
3445
// ErrEdgeNotFound is returned. A struct which houses the general information
3446
// for the channel itself is returned as well as two structs that contain the
3447
// routing policies for the channel in either direction.
3448
//
3449
// ErrZombieEdge an be returned if the edge is currently marked as a zombie
3450
// within the database. In this case, the ChannelEdgePolicy's will be nil, and
3451
// the ChannelEdgeInfo will only include the public keys of each node.
3452
func (c *KVStore) FetchChannelEdgesByID(chanID uint64) (
3453
        *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3454
        *models.ChannelEdgePolicy, error) {
2,683✔
3455

2,683✔
3456
        var (
2,683✔
3457
                edgeInfo  *models.ChannelEdgeInfo
2,683✔
3458
                policy1   *models.ChannelEdgePolicy
2,683✔
3459
                policy2   *models.ChannelEdgePolicy
2,683✔
3460
                channelID [8]byte
2,683✔
3461
        )
2,683✔
3462

2,683✔
3463
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
5,366✔
3464
                // First, grab the node bucket. This will be used to populate
2,683✔
3465
                // the Node pointers in each edge read from disk.
2,683✔
3466
                nodes := tx.ReadBucket(nodeBucket)
2,683✔
3467
                if nodes == nil {
2,683✔
3468
                        return ErrGraphNotFound
×
3469
                }
×
3470

3471
                // Next, grab the edge bucket which stores the edges, and also
3472
                // the index itself so we can group the directed edges together
3473
                // logically.
3474
                edges := tx.ReadBucket(edgeBucket)
2,683✔
3475
                if edges == nil {
2,683✔
3476
                        return ErrGraphNoEdgesFound
×
3477
                }
×
3478
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
2,683✔
3479
                if edgeIndex == nil {
2,683✔
3480
                        return ErrGraphNoEdgesFound
×
3481
                }
×
3482

3483
                byteOrder.PutUint64(channelID[:], chanID)
2,683✔
3484

2,683✔
3485
                // Now, attempt to fetch edge.
2,683✔
3486
                edge, err := fetchChanEdgeInfo(edgeIndex, channelID[:])
2,683✔
3487

2,683✔
3488
                // If it doesn't exist, we'll quickly check our zombie index to
2,683✔
3489
                // see if we've previously marked it as so.
2,683✔
3490
                if errors.Is(err, ErrEdgeNotFound) {
2,684✔
3491
                        // If the zombie index doesn't exist, or the edge is not
1✔
3492
                        // marked as a zombie within it, then we'll return the
1✔
3493
                        // original ErrEdgeNotFound error.
1✔
3494
                        zombieIndex := edges.NestedReadBucket(zombieBucket)
1✔
3495
                        if zombieIndex == nil {
1✔
3496
                                return ErrEdgeNotFound
×
3497
                        }
×
3498

3499
                        isZombie, pubKey1, pubKey2 := isZombieEdge(
1✔
3500
                                zombieIndex, chanID,
1✔
3501
                        )
1✔
3502
                        if !isZombie {
1✔
UNCOV
3503
                                return ErrEdgeNotFound
×
UNCOV
3504
                        }
×
3505

3506
                        // Otherwise, the edge is marked as a zombie, so we'll
3507
                        // populate the edge info with the public keys of each
3508
                        // party as this is the only information we have about
3509
                        // it and return an error signaling so.
3510
                        edgeInfo = &models.ChannelEdgeInfo{
1✔
3511
                                NodeKey1Bytes: pubKey1,
1✔
3512
                                NodeKey2Bytes: pubKey2,
1✔
3513
                        }
1✔
3514

1✔
3515
                        return ErrZombieEdge
1✔
3516
                }
3517

3518
                // Otherwise, we'll just return the error if any.
3519
                if err != nil {
2,682✔
3520
                        return err
×
3521
                }
×
3522

3523
                edgeInfo = &edge
2,682✔
3524

2,682✔
3525
                // Then we'll attempt to fetch the accompanying policies of this
2,682✔
3526
                // edge.
2,682✔
3527
                e1, e2, err := fetchChanEdgePolicies(
2,682✔
3528
                        edgeIndex, edges, channelID[:],
2,682✔
3529
                )
2,682✔
3530
                if err != nil {
2,682✔
3531
                        return err
×
3532
                }
×
3533

3534
                policy1 = e1
2,682✔
3535
                policy2 = e2
2,682✔
3536

2,682✔
3537
                return nil
2,682✔
3538
        }, func() {
2,683✔
3539
                edgeInfo = nil
2,683✔
3540
                policy1 = nil
2,683✔
3541
                policy2 = nil
2,683✔
3542
        })
2,683✔
3543
        if errors.Is(err, ErrZombieEdge) {
2,684✔
3544
                return edgeInfo, nil, nil, err
1✔
3545
        }
1✔
3546
        if err != nil {
2,682✔
UNCOV
3547
                return nil, nil, nil, err
×
UNCOV
3548
        }
×
3549

3550
        return edgeInfo, policy1, policy2, nil
2,682✔
3551
}
3552

3553
// IsPublicNode is a helper method that determines whether the node with the
3554
// given public key is seen as a public node in the graph from the graph's
3555
// source node's point of view.
3556
func (c *KVStore) IsPublicNode(pubKey [33]byte) (bool, error) {
13✔
3557
        var nodeIsPublic bool
13✔
3558
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
26✔
3559
                nodes := tx.ReadBucket(nodeBucket)
13✔
3560
                if nodes == nil {
13✔
3561
                        return ErrGraphNodesNotFound
×
3562
                }
×
3563
                ourPubKey := nodes.Get(sourceKey)
13✔
3564
                if ourPubKey == nil {
13✔
3565
                        return ErrSourceNodeNotSet
×
3566
                }
×
3567
                node, err := fetchLightningNode(nodes, pubKey[:])
13✔
3568
                if err != nil {
13✔
3569
                        return err
×
3570
                }
×
3571

3572
                nodeIsPublic, err = c.isPublic(tx, node.PubKeyBytes, ourPubKey)
13✔
3573

13✔
3574
                return err
13✔
3575
        }, func() {
13✔
3576
                nodeIsPublic = false
13✔
3577
        })
13✔
3578
        if err != nil {
13✔
3579
                return false, err
×
3580
        }
×
3581

3582
        return nodeIsPublic, nil
13✔
3583
}
3584

3585
// genMultiSigP2WSH generates the p2wsh'd multisig script for 2 of 2 pubkeys.
3586
func genMultiSigP2WSH(aPub, bPub []byte) ([]byte, error) {
46✔
3587
        witnessScript, err := input.GenMultiSigScript(aPub, bPub)
46✔
3588
        if err != nil {
46✔
3589
                return nil, err
×
3590
        }
×
3591

3592
        // With the witness script generated, we'll now turn it into a p2wsh
3593
        // script:
3594
        //  * OP_0 <sha256(script)>
3595
        bldr := txscript.NewScriptBuilder(
46✔
3596
                txscript.WithScriptAllocSize(input.P2WSHSize),
46✔
3597
        )
46✔
3598
        bldr.AddOp(txscript.OP_0)
46✔
3599
        scriptHash := sha256.Sum256(witnessScript)
46✔
3600
        bldr.AddData(scriptHash[:])
46✔
3601

46✔
3602
        return bldr.Script()
46✔
3603
}
3604

3605
// EdgePoint couples the outpoint of a channel with the funding script that it
3606
// creates. The FilteredChainView will use this to watch for spends of this
3607
// edge point on chain. We require both of these values as depending on the
3608
// concrete implementation, either the pkScript, or the out point will be used.
3609
type EdgePoint struct {
3610
        // FundingPkScript is the p2wsh multi-sig script of the target channel.
3611
        FundingPkScript []byte
3612

3613
        // OutPoint is the outpoint of the target channel.
3614
        OutPoint wire.OutPoint
3615
}
3616

3617
// String returns a human readable version of the target EdgePoint. We return
3618
// the outpoint directly as it is enough to uniquely identify the edge point.
3619
func (e *EdgePoint) String() string {
×
3620
        return e.OutPoint.String()
×
3621
}
×
3622

3623
// ChannelView returns the verifiable edge information for each active channel
3624
// within the known channel graph. The set of UTXO's (along with their scripts)
3625
// returned are the ones that need to be watched on chain to detect channel
3626
// closes on the resident blockchain.
3627
func (c *KVStore) ChannelView() ([]EdgePoint, error) {
22✔
3628
        var edgePoints []EdgePoint
22✔
3629
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
44✔
3630
                // We're going to iterate over the entire channel index, so
22✔
3631
                // we'll need to fetch the edgeBucket to get to the index as
22✔
3632
                // it's a sub-bucket.
22✔
3633
                edges := tx.ReadBucket(edgeBucket)
22✔
3634
                if edges == nil {
22✔
3635
                        return ErrGraphNoEdgesFound
×
3636
                }
×
3637
                chanIndex := edges.NestedReadBucket(channelPointBucket)
22✔
3638
                if chanIndex == nil {
22✔
3639
                        return ErrGraphNoEdgesFound
×
3640
                }
×
3641
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
22✔
3642
                if edgeIndex == nil {
22✔
3643
                        return ErrGraphNoEdgesFound
×
3644
                }
×
3645

3646
                // Once we have the proper bucket, we'll range over each key
3647
                // (which is the channel point for the channel) and decode it,
3648
                // accumulating each entry.
3649
                return chanIndex.ForEach(
22✔
3650
                        func(chanPointBytes, chanID []byte) error {
64✔
3651
                                chanPointReader := bytes.NewReader(
42✔
3652
                                        chanPointBytes,
42✔
3653
                                )
42✔
3654

42✔
3655
                                var chanPoint wire.OutPoint
42✔
3656
                                err := ReadOutpoint(chanPointReader, &chanPoint)
42✔
3657
                                if err != nil {
42✔
3658
                                        return err
×
3659
                                }
×
3660

3661
                                edgeInfo, err := fetchChanEdgeInfo(
42✔
3662
                                        edgeIndex, chanID,
42✔
3663
                                )
42✔
3664
                                if err != nil {
42✔
3665
                                        return err
×
3666
                                }
×
3667

3668
                                pkScript, err := genMultiSigP2WSH(
42✔
3669
                                        edgeInfo.BitcoinKey1Bytes[:],
42✔
3670
                                        edgeInfo.BitcoinKey2Bytes[:],
42✔
3671
                                )
42✔
3672
                                if err != nil {
42✔
3673
                                        return err
×
3674
                                }
×
3675

3676
                                edgePoints = append(edgePoints, EdgePoint{
42✔
3677
                                        FundingPkScript: pkScript,
42✔
3678
                                        OutPoint:        chanPoint,
42✔
3679
                                })
42✔
3680

42✔
3681
                                return nil
42✔
3682
                        },
3683
                )
3684
        }, func() {
22✔
3685
                edgePoints = nil
22✔
3686
        }); err != nil {
22✔
3687
                return nil, err
×
3688
        }
×
3689

3690
        return edgePoints, nil
22✔
3691
}
3692

3693
// MarkEdgeZombie attempts to mark a channel identified by its channel ID as a
3694
// zombie. This method is used on an ad-hoc basis, when channels need to be
3695
// marked as zombies outside the normal pruning cycle.
3696
func (c *KVStore) MarkEdgeZombie(chanID uint64,
3697
        pubKey1, pubKey2 [33]byte) error {
131✔
3698

131✔
3699
        c.cacheMu.Lock()
131✔
3700
        defer c.cacheMu.Unlock()
131✔
3701

131✔
3702
        err := kvdb.Batch(c.db, func(tx kvdb.RwTx) error {
262✔
3703
                edges := tx.ReadWriteBucket(edgeBucket)
131✔
3704
                if edges == nil {
131✔
3705
                        return ErrGraphNoEdgesFound
×
3706
                }
×
3707
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
131✔
3708
                if err != nil {
131✔
3709
                        return fmt.Errorf("unable to create zombie "+
×
3710
                                "bucket: %w", err)
×
3711
                }
×
3712

3713
                return markEdgeZombie(zombieIndex, chanID, pubKey1, pubKey2)
131✔
3714
        })
3715
        if err != nil {
131✔
3716
                return err
×
3717
        }
×
3718

3719
        c.rejectCache.remove(chanID)
131✔
3720
        c.chanCache.remove(chanID)
131✔
3721

131✔
3722
        return nil
131✔
3723
}
3724

3725
// markEdgeZombie marks an edge as a zombie within our zombie index. The public
3726
// keys should represent the node public keys of the two parties involved in the
3727
// edge.
3728
func markEdgeZombie(zombieIndex kvdb.RwBucket, chanID uint64, pubKey1,
3729
        pubKey2 [33]byte) error {
154✔
3730

154✔
3731
        var k [8]byte
154✔
3732
        byteOrder.PutUint64(k[:], chanID)
154✔
3733

154✔
3734
        var v [66]byte
154✔
3735
        copy(v[:33], pubKey1[:])
154✔
3736
        copy(v[33:], pubKey2[:])
154✔
3737

154✔
3738
        return zombieIndex.Put(k[:], v[:])
154✔
3739
}
154✔
3740

3741
// MarkEdgeLive clears an edge from our zombie index, deeming it as live.
3742
func (c *KVStore) MarkEdgeLive(chanID uint64) error {
21✔
3743
        c.cacheMu.Lock()
21✔
3744
        defer c.cacheMu.Unlock()
21✔
3745

21✔
3746
        return c.markEdgeLiveUnsafe(nil, chanID)
21✔
3747
}
21✔
3748

3749
// markEdgeLiveUnsafe clears an edge from the zombie index. This method can be
3750
// called with an existing kvdb.RwTx or the argument can be set to nil in which
3751
// case a new transaction will be created.
3752
//
3753
// NOTE: this method MUST only be called if the cacheMu has already been
3754
// acquired.
3755
func (c *KVStore) markEdgeLiveUnsafe(tx kvdb.RwTx, chanID uint64) error {
21✔
3756
        dbFn := func(tx kvdb.RwTx) error {
42✔
3757
                edges := tx.ReadWriteBucket(edgeBucket)
21✔
3758
                if edges == nil {
21✔
3759
                        return ErrGraphNoEdgesFound
×
3760
                }
×
3761
                zombieIndex := edges.NestedReadWriteBucket(zombieBucket)
21✔
3762
                if zombieIndex == nil {
21✔
3763
                        return nil
×
3764
                }
×
3765

3766
                var k [8]byte
21✔
3767
                byteOrder.PutUint64(k[:], chanID)
21✔
3768

21✔
3769
                if len(zombieIndex.Get(k[:])) == 0 {
22✔
3770
                        return ErrZombieEdgeNotFound
1✔
3771
                }
1✔
3772

3773
                return zombieIndex.Delete(k[:])
20✔
3774
        }
3775

3776
        // If the transaction is nil, we'll create a new one. Otherwise, we use
3777
        // the existing transaction
3778
        var err error
21✔
3779
        if tx == nil {
42✔
3780
                err = kvdb.Update(c.db, dbFn, func() {})
42✔
3781
        } else {
×
3782
                err = dbFn(tx)
×
3783
        }
×
3784
        if err != nil {
22✔
3785
                return err
1✔
3786
        }
1✔
3787

3788
        c.rejectCache.remove(chanID)
20✔
3789
        c.chanCache.remove(chanID)
20✔
3790

20✔
3791
        return nil
20✔
3792
}
3793

3794
// IsZombieEdge returns whether the edge is considered zombie. If it is a
3795
// zombie, then the two node public keys corresponding to this edge are also
3796
// returned.
3797
func (c *KVStore) IsZombieEdge(chanID uint64) (bool, [33]byte, [33]byte) {
14✔
3798
        var (
14✔
3799
                isZombie         bool
14✔
3800
                pubKey1, pubKey2 [33]byte
14✔
3801
        )
14✔
3802

14✔
3803
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
28✔
3804
                edges := tx.ReadBucket(edgeBucket)
14✔
3805
                if edges == nil {
14✔
3806
                        return ErrGraphNoEdgesFound
×
3807
                }
×
3808
                zombieIndex := edges.NestedReadBucket(zombieBucket)
14✔
3809
                if zombieIndex == nil {
14✔
3810
                        return nil
×
3811
                }
×
3812

3813
                isZombie, pubKey1, pubKey2 = isZombieEdge(zombieIndex, chanID)
14✔
3814

14✔
3815
                return nil
14✔
3816
        }, func() {
14✔
3817
                isZombie = false
14✔
3818
                pubKey1 = [33]byte{}
14✔
3819
                pubKey2 = [33]byte{}
14✔
3820
        })
14✔
3821
        if err != nil {
14✔
3822
                return false, [33]byte{}, [33]byte{}
×
3823
        }
×
3824

3825
        return isZombie, pubKey1, pubKey2
14✔
3826
}
3827

3828
// isZombieEdge returns whether an entry exists for the given channel in the
3829
// zombie index. If an entry exists, then the two node public keys corresponding
3830
// to this edge are also returned.
3831
func isZombieEdge(zombieIndex kvdb.RBucket,
3832
        chanID uint64) (bool, [33]byte, [33]byte) {
189✔
3833

189✔
3834
        var k [8]byte
189✔
3835
        byteOrder.PutUint64(k[:], chanID)
189✔
3836

189✔
3837
        v := zombieIndex.Get(k[:])
189✔
3838
        if v == nil {
290✔
3839
                return false, [33]byte{}, [33]byte{}
101✔
3840
        }
101✔
3841

3842
        var pubKey1, pubKey2 [33]byte
88✔
3843
        copy(pubKey1[:], v[:33])
88✔
3844
        copy(pubKey2[:], v[33:])
88✔
3845

88✔
3846
        return true, pubKey1, pubKey2
88✔
3847
}
3848

3849
// NumZombies returns the current number of zombie channels in the graph.
3850
func (c *KVStore) NumZombies() (uint64, error) {
4✔
3851
        var numZombies uint64
4✔
3852
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
8✔
3853
                edges := tx.ReadBucket(edgeBucket)
4✔
3854
                if edges == nil {
4✔
3855
                        return nil
×
3856
                }
×
3857
                zombieIndex := edges.NestedReadBucket(zombieBucket)
4✔
3858
                if zombieIndex == nil {
4✔
3859
                        return nil
×
3860
                }
×
3861

3862
                return zombieIndex.ForEach(func(_, _ []byte) error {
6✔
3863
                        numZombies++
2✔
3864
                        return nil
2✔
3865
                })
2✔
3866
        }, func() {
4✔
3867
                numZombies = 0
4✔
3868
        })
4✔
3869
        if err != nil {
4✔
3870
                return 0, err
×
3871
        }
×
3872

3873
        return numZombies, nil
4✔
3874
}
3875

3876
// PutClosedScid stores a SCID for a closed channel in the database. This is so
3877
// that we can ignore channel announcements that we know to be closed without
3878
// having to validate them and fetch a block.
3879
func (c *KVStore) PutClosedScid(scid lnwire.ShortChannelID) error {
1✔
3880
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
2✔
3881
                closedScids, err := tx.CreateTopLevelBucket(closedScidBucket)
1✔
3882
                if err != nil {
1✔
3883
                        return err
×
3884
                }
×
3885

3886
                var k [8]byte
1✔
3887
                byteOrder.PutUint64(k[:], scid.ToUint64())
1✔
3888

1✔
3889
                return closedScids.Put(k[:], []byte{})
1✔
3890
        }, func() {})
1✔
3891
}
3892

3893
// IsClosedScid checks whether a channel identified by the passed in scid is
3894
// closed. This helps avoid having to perform expensive validation checks.
3895
// TODO: Add an LRU cache to cut down on disc reads.
3896
func (c *KVStore) IsClosedScid(scid lnwire.ShortChannelID) (bool, error) {
2✔
3897
        var isClosed bool
2✔
3898
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
4✔
3899
                closedScids := tx.ReadBucket(closedScidBucket)
2✔
3900
                if closedScids == nil {
2✔
3901
                        return ErrClosedScidsNotFound
×
3902
                }
×
3903

3904
                var k [8]byte
2✔
3905
                byteOrder.PutUint64(k[:], scid.ToUint64())
2✔
3906

2✔
3907
                if closedScids.Get(k[:]) != nil {
3✔
3908
                        isClosed = true
1✔
3909
                        return nil
1✔
3910
                }
1✔
3911

3912
                return nil
1✔
3913
        }, func() {
2✔
3914
                isClosed = false
2✔
3915
        })
2✔
3916
        if err != nil {
2✔
3917
                return false, err
×
3918
        }
×
3919

3920
        return isClosed, nil
2✔
3921
}
3922

3923
// GraphSession will provide the call-back with access to a NodeTraverser
3924
// instance which can be used to perform queries against the channel graph.
3925
func (c *KVStore) GraphSession(cb func(graph NodeTraverser) error) error {
54✔
3926
        return c.db.View(func(tx walletdb.ReadTx) error {
108✔
3927
                return cb(&nodeTraverserSession{
54✔
3928
                        db: c,
54✔
3929
                        tx: tx,
54✔
3930
                })
54✔
3931
        }, func() {})
108✔
3932
}
3933

3934
// nodeTraverserSession implements the NodeTraverser interface but with a
3935
// backing read only transaction for a consistent view of the graph.
3936
type nodeTraverserSession struct {
3937
        tx kvdb.RTx
3938
        db *KVStore
3939
}
3940

3941
// ForEachNodeDirectedChannel calls the callback for every channel of the given
3942
// node.
3943
//
3944
// NOTE: Part of the NodeTraverser interface.
3945
func (c *nodeTraverserSession) ForEachNodeDirectedChannel(nodePub route.Vertex,
3946
        cb func(channel *DirectedChannel) error) error {
239✔
3947

239✔
3948
        return c.db.forEachNodeDirectedChannel(c.tx, nodePub, cb)
239✔
3949
}
239✔
3950

3951
// FetchNodeFeatures returns the features of the given node. If the node is
3952
// unknown, assume no additional features are supported.
3953
//
3954
// NOTE: Part of the NodeTraverser interface.
3955
func (c *nodeTraverserSession) FetchNodeFeatures(nodePub route.Vertex) (
3956
        *lnwire.FeatureVector, error) {
254✔
3957

254✔
3958
        return c.db.fetchNodeFeatures(c.tx, nodePub)
254✔
3959
}
254✔
3960

3961
func putLightningNode(nodeBucket, aliasBucket, updateIndex kvdb.RwBucket,
3962
        node *models.LightningNode) error {
998✔
3963

998✔
3964
        var (
998✔
3965
                scratch [16]byte
998✔
3966
                b       bytes.Buffer
998✔
3967
        )
998✔
3968

998✔
3969
        pub, err := node.PubKey()
998✔
3970
        if err != nil {
998✔
3971
                return err
×
3972
        }
×
3973
        nodePub := pub.SerializeCompressed()
998✔
3974

998✔
3975
        // If the node has the update time set, write it, else write 0.
998✔
3976
        updateUnix := uint64(0)
998✔
3977
        if node.LastUpdate.Unix() > 0 {
1,861✔
3978
                updateUnix = uint64(node.LastUpdate.Unix())
863✔
3979
        }
863✔
3980

3981
        byteOrder.PutUint64(scratch[:8], updateUnix)
998✔
3982
        if _, err := b.Write(scratch[:8]); err != nil {
998✔
3983
                return err
×
3984
        }
×
3985

3986
        if _, err := b.Write(nodePub); err != nil {
998✔
3987
                return err
×
3988
        }
×
3989

3990
        // If we got a node announcement for this node, we will have the rest
3991
        // of the data available. If not we don't have more data to write.
3992
        if !node.HaveNodeAnnouncement {
1,080✔
3993
                // Write HaveNodeAnnouncement=0.
82✔
3994
                byteOrder.PutUint16(scratch[:2], 0)
82✔
3995
                if _, err := b.Write(scratch[:2]); err != nil {
82✔
3996
                        return err
×
3997
                }
×
3998

3999
                return nodeBucket.Put(nodePub, b.Bytes())
82✔
4000
        }
4001

4002
        // Write HaveNodeAnnouncement=1.
4003
        byteOrder.PutUint16(scratch[:2], 1)
916✔
4004
        if _, err := b.Write(scratch[:2]); err != nil {
916✔
4005
                return err
×
4006
        }
×
4007

4008
        if err := binary.Write(&b, byteOrder, node.Color.R); err != nil {
916✔
4009
                return err
×
4010
        }
×
4011
        if err := binary.Write(&b, byteOrder, node.Color.G); err != nil {
916✔
4012
                return err
×
4013
        }
×
4014
        if err := binary.Write(&b, byteOrder, node.Color.B); err != nil {
916✔
4015
                return err
×
4016
        }
×
4017

4018
        if err := wire.WriteVarString(&b, 0, node.Alias); err != nil {
916✔
4019
                return err
×
4020
        }
×
4021

4022
        if err := node.Features.Encode(&b); err != nil {
916✔
4023
                return err
×
4024
        }
×
4025

4026
        numAddresses := uint16(len(node.Addresses))
916✔
4027
        byteOrder.PutUint16(scratch[:2], numAddresses)
916✔
4028
        if _, err := b.Write(scratch[:2]); err != nil {
916✔
4029
                return err
×
4030
        }
×
4031

4032
        for _, address := range node.Addresses {
2,069✔
4033
                if err := SerializeAddr(&b, address); err != nil {
1,153✔
4034
                        return err
×
4035
                }
×
4036
        }
4037

4038
        sigLen := len(node.AuthSigBytes)
916✔
4039
        if sigLen > 80 {
916✔
4040
                return fmt.Errorf("max sig len allowed is 80, had %v",
×
4041
                        sigLen)
×
4042
        }
×
4043

4044
        err = wire.WriteVarBytes(&b, 0, node.AuthSigBytes)
916✔
4045
        if err != nil {
916✔
4046
                return err
×
4047
        }
×
4048

4049
        if len(node.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
916✔
4050
                return ErrTooManyExtraOpaqueBytes(len(node.ExtraOpaqueData))
×
4051
        }
×
4052
        err = wire.WriteVarBytes(&b, 0, node.ExtraOpaqueData)
916✔
4053
        if err != nil {
916✔
4054
                return err
×
4055
        }
×
4056

4057
        if err := aliasBucket.Put(nodePub, []byte(node.Alias)); err != nil {
916✔
4058
                return err
×
4059
        }
×
4060

4061
        // With the alias bucket updated, we'll now update the index that
4062
        // tracks the time series of node updates.
4063
        var indexKey [8 + 33]byte
916✔
4064
        byteOrder.PutUint64(indexKey[:8], updateUnix)
916✔
4065
        copy(indexKey[8:], nodePub)
916✔
4066

916✔
4067
        // If there was already an old index entry for this node, then we'll
916✔
4068
        // delete the old one before we write the new entry.
916✔
4069
        if nodeBytes := nodeBucket.Get(nodePub); nodeBytes != nil {
1,023✔
4070
                // Extract out the old update time to we can reconstruct the
107✔
4071
                // prior index key to delete it from the index.
107✔
4072
                oldUpdateTime := nodeBytes[:8]
107✔
4073

107✔
4074
                var oldIndexKey [8 + 33]byte
107✔
4075
                copy(oldIndexKey[:8], oldUpdateTime)
107✔
4076
                copy(oldIndexKey[8:], nodePub)
107✔
4077

107✔
4078
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
107✔
4079
                        return err
×
4080
                }
×
4081
        }
4082

4083
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
916✔
4084
                return err
×
4085
        }
×
4086

4087
        return nodeBucket.Put(nodePub, b.Bytes())
916✔
4088
}
4089

4090
func fetchLightningNode(nodeBucket kvdb.RBucket,
4091
        nodePub []byte) (models.LightningNode, error) {
3,621✔
4092

3,621✔
4093
        nodeBytes := nodeBucket.Get(nodePub)
3,621✔
4094
        if nodeBytes == nil {
3,703✔
4095
                return models.LightningNode{}, ErrGraphNodeNotFound
82✔
4096
        }
82✔
4097

4098
        nodeReader := bytes.NewReader(nodeBytes)
3,539✔
4099

3,539✔
4100
        return deserializeLightningNode(nodeReader)
3,539✔
4101
}
4102

4103
func deserializeLightningNodeCacheable(r io.Reader) (route.Vertex,
4104
        *lnwire.FeatureVector, error) {
120✔
4105

120✔
4106
        var (
120✔
4107
                pubKey      route.Vertex
120✔
4108
                features    = lnwire.EmptyFeatureVector()
120✔
4109
                nodeScratch [8]byte
120✔
4110
        )
120✔
4111

120✔
4112
        // Skip ahead:
120✔
4113
        // - LastUpdate (8 bytes)
120✔
4114
        if _, err := r.Read(nodeScratch[:]); err != nil {
120✔
4115
                return pubKey, nil, err
×
4116
        }
×
4117

4118
        if _, err := io.ReadFull(r, pubKey[:]); err != nil {
120✔
4119
                return pubKey, nil, err
×
4120
        }
×
4121

4122
        // Read the node announcement flag.
4123
        if _, err := r.Read(nodeScratch[:2]); err != nil {
120✔
4124
                return pubKey, nil, err
×
4125
        }
×
4126
        hasNodeAnn := byteOrder.Uint16(nodeScratch[:2])
120✔
4127

120✔
4128
        // The rest of the data is optional, and will only be there if we got a
120✔
4129
        // node announcement for this node.
120✔
4130
        if hasNodeAnn == 0 {
120✔
UNCOV
4131
                return pubKey, features, nil
×
UNCOV
4132
        }
×
4133

4134
        // We did get a node announcement for this node, so we'll have the rest
4135
        // of the data available.
4136
        var rgb uint8
120✔
4137
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
120✔
4138
                return pubKey, nil, err
×
4139
        }
×
4140
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
120✔
4141
                return pubKey, nil, err
×
4142
        }
×
4143
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
120✔
4144
                return pubKey, nil, err
×
4145
        }
×
4146

4147
        if _, err := wire.ReadVarString(r, 0); err != nil {
120✔
4148
                return pubKey, nil, err
×
4149
        }
×
4150

4151
        if err := features.Decode(r); err != nil {
120✔
4152
                return pubKey, nil, err
×
4153
        }
×
4154

4155
        return pubKey, features, nil
120✔
4156
}
4157

4158
func deserializeLightningNode(r io.Reader) (models.LightningNode, error) {
8,526✔
4159
        var (
8,526✔
4160
                node    models.LightningNode
8,526✔
4161
                scratch [8]byte
8,526✔
4162
                err     error
8,526✔
4163
        )
8,526✔
4164

8,526✔
4165
        // Always populate a feature vector, even if we don't have a node
8,526✔
4166
        // announcement and short circuit below.
8,526✔
4167
        node.Features = lnwire.EmptyFeatureVector()
8,526✔
4168

8,526✔
4169
        if _, err := r.Read(scratch[:]); err != nil {
8,526✔
4170
                return models.LightningNode{}, err
×
4171
        }
×
4172

4173
        unix := int64(byteOrder.Uint64(scratch[:]))
8,526✔
4174
        node.LastUpdate = time.Unix(unix, 0)
8,526✔
4175

8,526✔
4176
        if _, err := io.ReadFull(r, node.PubKeyBytes[:]); err != nil {
8,526✔
4177
                return models.LightningNode{}, err
×
4178
        }
×
4179

4180
        if _, err := r.Read(scratch[:2]); err != nil {
8,526✔
4181
                return models.LightningNode{}, err
×
4182
        }
×
4183

4184
        hasNodeAnn := byteOrder.Uint16(scratch[:2])
8,526✔
4185
        if hasNodeAnn == 1 {
16,914✔
4186
                node.HaveNodeAnnouncement = true
8,388✔
4187
        } else {
8,526✔
4188
                node.HaveNodeAnnouncement = false
138✔
4189
        }
138✔
4190

4191
        // The rest of the data is optional, and will only be there if we got a
4192
        // node announcement for this node.
4193
        if !node.HaveNodeAnnouncement {
8,664✔
4194
                return node, nil
138✔
4195
        }
138✔
4196

4197
        // We did get a node announcement for this node, so we'll have the rest
4198
        // of the data available.
4199
        if err := binary.Read(r, byteOrder, &node.Color.R); err != nil {
8,388✔
4200
                return models.LightningNode{}, err
×
4201
        }
×
4202
        if err := binary.Read(r, byteOrder, &node.Color.G); err != nil {
8,388✔
4203
                return models.LightningNode{}, err
×
4204
        }
×
4205
        if err := binary.Read(r, byteOrder, &node.Color.B); err != nil {
8,388✔
4206
                return models.LightningNode{}, err
×
4207
        }
×
4208

4209
        node.Alias, err = wire.ReadVarString(r, 0)
8,388✔
4210
        if err != nil {
8,388✔
4211
                return models.LightningNode{}, err
×
4212
        }
×
4213

4214
        err = node.Features.Decode(r)
8,388✔
4215
        if err != nil {
8,388✔
4216
                return models.LightningNode{}, err
×
4217
        }
×
4218

4219
        if _, err := r.Read(scratch[:2]); err != nil {
8,388✔
4220
                return models.LightningNode{}, err
×
4221
        }
×
4222
        numAddresses := int(byteOrder.Uint16(scratch[:2]))
8,388✔
4223

8,388✔
4224
        var addresses []net.Addr
8,388✔
4225
        for i := 0; i < numAddresses; i++ {
19,039✔
4226
                address, err := DeserializeAddr(r)
10,651✔
4227
                if err != nil {
10,651✔
4228
                        return models.LightningNode{}, err
×
4229
                }
×
4230
                addresses = append(addresses, address)
10,651✔
4231
        }
4232
        node.Addresses = addresses
8,388✔
4233

8,388✔
4234
        node.AuthSigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
8,388✔
4235
        if err != nil {
8,388✔
4236
                return models.LightningNode{}, err
×
4237
        }
×
4238

4239
        // We'll try and see if there are any opaque bytes left, if not, then
4240
        // we'll ignore the EOF error and return the node as is.
4241
        extraBytes, err := wire.ReadVarBytes(
8,388✔
4242
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
8,388✔
4243
        )
8,388✔
4244
        switch {
8,388✔
4245
        case errors.Is(err, io.ErrUnexpectedEOF):
×
4246
        case errors.Is(err, io.EOF):
×
4247
        case err != nil:
×
4248
                return models.LightningNode{}, err
×
4249
        }
4250

4251
        if len(extraBytes) > 0 {
8,398✔
4252
                node.ExtraOpaqueData = extraBytes
10✔
4253
        }
10✔
4254

4255
        return node, nil
8,388✔
4256
}
4257

4258
func putChanEdgeInfo(edgeIndex kvdb.RwBucket,
4259
        edgeInfo *models.ChannelEdgeInfo, chanID [8]byte) error {
1,480✔
4260

1,480✔
4261
        var b bytes.Buffer
1,480✔
4262

1,480✔
4263
        if _, err := b.Write(edgeInfo.NodeKey1Bytes[:]); err != nil {
1,480✔
4264
                return err
×
4265
        }
×
4266
        if _, err := b.Write(edgeInfo.NodeKey2Bytes[:]); err != nil {
1,480✔
4267
                return err
×
4268
        }
×
4269
        if _, err := b.Write(edgeInfo.BitcoinKey1Bytes[:]); err != nil {
1,480✔
4270
                return err
×
4271
        }
×
4272
        if _, err := b.Write(edgeInfo.BitcoinKey2Bytes[:]); err != nil {
1,480✔
4273
                return err
×
4274
        }
×
4275

4276
        if err := wire.WriteVarBytes(&b, 0, edgeInfo.Features); err != nil {
1,480✔
4277
                return err
×
4278
        }
×
4279

4280
        authProof := edgeInfo.AuthProof
1,480✔
4281
        var nodeSig1, nodeSig2, bitcoinSig1, bitcoinSig2 []byte
1,480✔
4282
        if authProof != nil {
2,876✔
4283
                nodeSig1 = authProof.NodeSig1Bytes
1,396✔
4284
                nodeSig2 = authProof.NodeSig2Bytes
1,396✔
4285
                bitcoinSig1 = authProof.BitcoinSig1Bytes
1,396✔
4286
                bitcoinSig2 = authProof.BitcoinSig2Bytes
1,396✔
4287
        }
1,396✔
4288

4289
        if err := wire.WriteVarBytes(&b, 0, nodeSig1); err != nil {
1,480✔
4290
                return err
×
4291
        }
×
4292
        if err := wire.WriteVarBytes(&b, 0, nodeSig2); err != nil {
1,480✔
4293
                return err
×
4294
        }
×
4295
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig1); err != nil {
1,480✔
4296
                return err
×
4297
        }
×
4298
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig2); err != nil {
1,480✔
4299
                return err
×
4300
        }
×
4301

4302
        if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
1,480✔
4303
                return err
×
4304
        }
×
4305
        err := binary.Write(&b, byteOrder, uint64(edgeInfo.Capacity))
1,480✔
4306
        if err != nil {
1,480✔
4307
                return err
×
4308
        }
×
4309
        if _, err := b.Write(chanID[:]); err != nil {
1,480✔
4310
                return err
×
4311
        }
×
4312
        if _, err := b.Write(edgeInfo.ChainHash[:]); err != nil {
1,480✔
4313
                return err
×
4314
        }
×
4315

4316
        if len(edgeInfo.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
1,480✔
4317
                return ErrTooManyExtraOpaqueBytes(len(edgeInfo.ExtraOpaqueData))
×
4318
        }
×
4319
        err = wire.WriteVarBytes(&b, 0, edgeInfo.ExtraOpaqueData)
1,480✔
4320
        if err != nil {
1,480✔
4321
                return err
×
4322
        }
×
4323

4324
        return edgeIndex.Put(chanID[:], b.Bytes())
1,480✔
4325
}
4326

4327
func fetchChanEdgeInfo(edgeIndex kvdb.RBucket,
4328
        chanID []byte) (models.ChannelEdgeInfo, error) {
6,788✔
4329

6,788✔
4330
        edgeInfoBytes := edgeIndex.Get(chanID)
6,788✔
4331
        if edgeInfoBytes == nil {
6,851✔
4332
                return models.ChannelEdgeInfo{}, ErrEdgeNotFound
63✔
4333
        }
63✔
4334

4335
        edgeInfoReader := bytes.NewReader(edgeInfoBytes)
6,725✔
4336

6,725✔
4337
        return deserializeChanEdgeInfo(edgeInfoReader)
6,725✔
4338
}
4339

4340
func deserializeChanEdgeInfo(r io.Reader) (models.ChannelEdgeInfo, error) {
7,264✔
4341
        var (
7,264✔
4342
                err      error
7,264✔
4343
                edgeInfo models.ChannelEdgeInfo
7,264✔
4344
        )
7,264✔
4345

7,264✔
4346
        if _, err := io.ReadFull(r, edgeInfo.NodeKey1Bytes[:]); err != nil {
7,264✔
4347
                return models.ChannelEdgeInfo{}, err
×
4348
        }
×
4349
        if _, err := io.ReadFull(r, edgeInfo.NodeKey2Bytes[:]); err != nil {
7,264✔
4350
                return models.ChannelEdgeInfo{}, err
×
4351
        }
×
4352
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey1Bytes[:]); err != nil {
7,264✔
4353
                return models.ChannelEdgeInfo{}, err
×
4354
        }
×
4355
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey2Bytes[:]); err != nil {
7,264✔
4356
                return models.ChannelEdgeInfo{}, err
×
4357
        }
×
4358

4359
        edgeInfo.Features, err = wire.ReadVarBytes(r, 0, 900, "features")
7,264✔
4360
        if err != nil {
7,264✔
4361
                return models.ChannelEdgeInfo{}, err
×
4362
        }
×
4363

4364
        proof := &models.ChannelAuthProof{}
7,264✔
4365

7,264✔
4366
        proof.NodeSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
7,264✔
4367
        if err != nil {
7,264✔
4368
                return models.ChannelEdgeInfo{}, err
×
4369
        }
×
4370
        proof.NodeSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
7,264✔
4371
        if err != nil {
7,264✔
4372
                return models.ChannelEdgeInfo{}, err
×
4373
        }
×
4374
        proof.BitcoinSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
7,264✔
4375
        if err != nil {
7,264✔
4376
                return models.ChannelEdgeInfo{}, err
×
4377
        }
×
4378
        proof.BitcoinSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
7,264✔
4379
        if err != nil {
7,264✔
4380
                return models.ChannelEdgeInfo{}, err
×
4381
        }
×
4382

4383
        if !proof.IsEmpty() {
11,425✔
4384
                edgeInfo.AuthProof = proof
4,161✔
4385
        }
4,161✔
4386

4387
        edgeInfo.ChannelPoint = wire.OutPoint{}
7,264✔
4388
        if err := ReadOutpoint(r, &edgeInfo.ChannelPoint); err != nil {
7,264✔
4389
                return models.ChannelEdgeInfo{}, err
×
4390
        }
×
4391
        if err := binary.Read(r, byteOrder, &edgeInfo.Capacity); err != nil {
7,264✔
4392
                return models.ChannelEdgeInfo{}, err
×
4393
        }
×
4394
        if err := binary.Read(r, byteOrder, &edgeInfo.ChannelID); err != nil {
7,264✔
4395
                return models.ChannelEdgeInfo{}, err
×
4396
        }
×
4397

4398
        if _, err := io.ReadFull(r, edgeInfo.ChainHash[:]); err != nil {
7,264✔
4399
                return models.ChannelEdgeInfo{}, err
×
4400
        }
×
4401

4402
        // We'll try and see if there are any opaque bytes left, if not, then
4403
        // we'll ignore the EOF error and return the edge as is.
4404
        edgeInfo.ExtraOpaqueData, err = wire.ReadVarBytes(
7,264✔
4405
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
7,264✔
4406
        )
7,264✔
4407
        switch {
7,264✔
4408
        case errors.Is(err, io.ErrUnexpectedEOF):
×
4409
        case errors.Is(err, io.EOF):
×
4410
        case err != nil:
×
4411
                return models.ChannelEdgeInfo{}, err
×
4412
        }
4413

4414
        return edgeInfo, nil
7,264✔
4415
}
4416

4417
func putChanEdgePolicy(edges kvdb.RwBucket, edge *models.ChannelEdgePolicy,
4418
        from, to []byte) error {
2,663✔
4419

2,663✔
4420
        var edgeKey [33 + 8]byte
2,663✔
4421
        copy(edgeKey[:], from)
2,663✔
4422
        byteOrder.PutUint64(edgeKey[33:], edge.ChannelID)
2,663✔
4423

2,663✔
4424
        var b bytes.Buffer
2,663✔
4425
        if err := serializeChanEdgePolicy(&b, edge, to); err != nil {
2,665✔
4426
                return err
2✔
4427
        }
2✔
4428

4429
        // Before we write out the new edge, we'll create a new entry in the
4430
        // update index in order to keep it fresh.
4431
        updateUnix := uint64(edge.LastUpdate.Unix())
2,661✔
4432
        var indexKey [8 + 8]byte
2,661✔
4433
        byteOrder.PutUint64(indexKey[:8], updateUnix)
2,661✔
4434
        byteOrder.PutUint64(indexKey[8:], edge.ChannelID)
2,661✔
4435

2,661✔
4436
        updateIndex, err := edges.CreateBucketIfNotExists(edgeUpdateIndexBucket)
2,661✔
4437
        if err != nil {
2,661✔
4438
                return err
×
4439
        }
×
4440

4441
        // If there was already an entry for this edge, then we'll need to
4442
        // delete the old one to ensure we don't leave around any after-images.
4443
        // An unknown policy value does not have a update time recorded, so
4444
        // it also does not need to be removed.
4445
        if edgeBytes := edges.Get(edgeKey[:]); edgeBytes != nil &&
2,661✔
4446
                !bytes.Equal(edgeBytes, unknownPolicy) {
2,684✔
4447

23✔
4448
                // In order to delete the old entry, we'll need to obtain the
23✔
4449
                // *prior* update time in order to delete it. To do this, we'll
23✔
4450
                // need to deserialize the existing policy within the database
23✔
4451
                // (now outdated by the new one), and delete its corresponding
23✔
4452
                // entry within the update index. We'll ignore any
23✔
4453
                // ErrEdgePolicyOptionalFieldNotFound or ErrParsingExtraTLVBytes
23✔
4454
                // errors, as we only need the channel ID and update time to
23✔
4455
                // delete the entry.
23✔
4456
                //
23✔
4457
                // TODO(halseth): get rid of these invalid policies in a
23✔
4458
                // migration.
23✔
4459
                // TODO(elle): complete the above TODO in migration from kvdb
23✔
4460
                // to SQL.
23✔
4461
                oldEdgePolicy, err := deserializeChanEdgePolicy(
23✔
4462
                        bytes.NewReader(edgeBytes),
23✔
4463
                )
23✔
4464
                if err != nil &&
23✔
4465
                        !errors.Is(err, ErrEdgePolicyOptionalFieldNotFound) &&
23✔
4466
                        !errors.Is(err, ErrParsingExtraTLVBytes) {
23✔
4467

×
4468
                        return err
×
4469
                }
×
4470

4471
                oldUpdateTime := uint64(oldEdgePolicy.LastUpdate.Unix())
23✔
4472

23✔
4473
                var oldIndexKey [8 + 8]byte
23✔
4474
                byteOrder.PutUint64(oldIndexKey[:8], oldUpdateTime)
23✔
4475
                byteOrder.PutUint64(oldIndexKey[8:], edge.ChannelID)
23✔
4476

23✔
4477
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
23✔
4478
                        return err
×
4479
                }
×
4480
        }
4481

4482
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
2,661✔
4483
                return err
×
4484
        }
×
4485

4486
        err = updateEdgePolicyDisabledIndex(
2,661✔
4487
                edges, edge.ChannelID,
2,661✔
4488
                edge.ChannelFlags&lnwire.ChanUpdateDirection > 0,
2,661✔
4489
                edge.IsDisabled(),
2,661✔
4490
        )
2,661✔
4491
        if err != nil {
2,661✔
4492
                return err
×
4493
        }
×
4494

4495
        return edges.Put(edgeKey[:], b.Bytes())
2,661✔
4496
}
4497

4498
// updateEdgePolicyDisabledIndex is used to update the disabledEdgePolicyIndex
4499
// bucket by either add a new disabled ChannelEdgePolicy or remove an existing
4500
// one.
4501
// The direction represents the direction of the edge and disabled is used for
4502
// deciding whether to remove or add an entry to the bucket.
4503
// In general a channel is disabled if two entries for the same chanID exist
4504
// in this bucket.
4505
// Maintaining the bucket this way allows a fast retrieval of disabled
4506
// channels, for example when prune is needed.
4507
func updateEdgePolicyDisabledIndex(edges kvdb.RwBucket, chanID uint64,
4508
        direction bool, disabled bool) error {
2,921✔
4509

2,921✔
4510
        var disabledEdgeKey [8 + 1]byte
2,921✔
4511
        byteOrder.PutUint64(disabledEdgeKey[0:], chanID)
2,921✔
4512
        if direction {
4,379✔
4513
                disabledEdgeKey[8] = 1
1,458✔
4514
        }
1,458✔
4515

4516
        disabledEdgePolicyIndex, err := edges.CreateBucketIfNotExists(
2,921✔
4517
                disabledEdgePolicyBucket,
2,921✔
4518
        )
2,921✔
4519
        if err != nil {
2,921✔
4520
                return err
×
4521
        }
×
4522

4523
        if disabled {
2,947✔
4524
                return disabledEdgePolicyIndex.Put(disabledEdgeKey[:], []byte{})
26✔
4525
        }
26✔
4526

4527
        return disabledEdgePolicyIndex.Delete(disabledEdgeKey[:])
2,895✔
4528
}
4529

4530
// putChanEdgePolicyUnknown marks the edge policy as unknown
4531
// in the edges bucket.
4532
func putChanEdgePolicyUnknown(edges kvdb.RwBucket, channelID uint64,
4533
        from []byte) error {
2,956✔
4534

2,956✔
4535
        var edgeKey [33 + 8]byte
2,956✔
4536
        copy(edgeKey[:], from)
2,956✔
4537
        byteOrder.PutUint64(edgeKey[33:], channelID)
2,956✔
4538

2,956✔
4539
        if edges.Get(edgeKey[:]) != nil {
2,956✔
4540
                return fmt.Errorf("cannot write unknown policy for channel %v "+
×
4541
                        " when there is already a policy present", channelID)
×
4542
        }
×
4543

4544
        return edges.Put(edgeKey[:], unknownPolicy)
2,956✔
4545
}
4546

4547
func fetchChanEdgePolicy(edges kvdb.RBucket, chanID []byte,
4548
        nodePub []byte) (*models.ChannelEdgePolicy, error) {
13,468✔
4549

13,468✔
4550
        var edgeKey [33 + 8]byte
13,468✔
4551
        copy(edgeKey[:], nodePub)
13,468✔
4552
        copy(edgeKey[33:], chanID)
13,468✔
4553

13,468✔
4554
        edgeBytes := edges.Get(edgeKey[:])
13,468✔
4555
        if edgeBytes == nil {
13,468✔
4556
                return nil, ErrEdgeNotFound
×
4557
        }
×
4558

4559
        // No need to deserialize unknown policy.
4560
        if bytes.Equal(edgeBytes, unknownPolicy) {
14,826✔
4561
                return nil, nil
1,358✔
4562
        }
1,358✔
4563

4564
        edgeReader := bytes.NewReader(edgeBytes)
12,110✔
4565

12,110✔
4566
        ep, err := deserializeChanEdgePolicy(edgeReader)
12,110✔
4567
        switch {
12,110✔
4568
        // If the db policy was missing an expected optional field, we return
4569
        // nil as if the policy was unknown.
4570
        case errors.Is(err, ErrEdgePolicyOptionalFieldNotFound):
2✔
4571
                return nil, nil
2✔
4572

4573
        // If the policy contains invalid TLV bytes, we return nil as if
4574
        // the policy was unknown.
4575
        case errors.Is(err, ErrParsingExtraTLVBytes):
×
4576
                return nil, nil
×
4577

4578
        case err != nil:
×
4579
                return nil, err
×
4580
        }
4581

4582
        return ep, nil
12,108✔
4583
}
4584

4585
func fetchChanEdgePolicies(edgeIndex kvdb.RBucket, edges kvdb.RBucket,
4586
        chanID []byte) (*models.ChannelEdgePolicy, *models.ChannelEdgePolicy,
4587
        error) {
2,892✔
4588

2,892✔
4589
        edgeInfo := edgeIndex.Get(chanID)
2,892✔
4590
        if edgeInfo == nil {
2,892✔
4591
                return nil, nil, fmt.Errorf("%w: chanID=%x", ErrEdgeNotFound,
×
4592
                        chanID)
×
4593
        }
×
4594

4595
        // The first node is contained within the first half of the edge
4596
        // information. We only propagate the error here and below if it's
4597
        // something other than edge non-existence.
4598
        node1Pub := edgeInfo[:33]
2,892✔
4599
        edge1, err := fetchChanEdgePolicy(edges, chanID, node1Pub)
2,892✔
4600
        if err != nil {
2,892✔
4601
                return nil, nil, fmt.Errorf("%w: node1Pub=%x", ErrEdgeNotFound,
×
4602
                        node1Pub)
×
4603
        }
×
4604

4605
        // Similarly, the second node is contained within the latter
4606
        // half of the edge information.
4607
        node2Pub := edgeInfo[33:66]
2,892✔
4608
        edge2, err := fetchChanEdgePolicy(edges, chanID, node2Pub)
2,892✔
4609
        if err != nil {
2,892✔
4610
                return nil, nil, fmt.Errorf("%w: node2Pub=%x", ErrEdgeNotFound,
×
4611
                        node2Pub)
×
4612
        }
×
4613

4614
        return edge1, edge2, nil
2,892✔
4615
}
4616

4617
func serializeChanEdgePolicy(w io.Writer, edge *models.ChannelEdgePolicy,
4618
        to []byte) error {
2,665✔
4619

2,665✔
4620
        err := wire.WriteVarBytes(w, 0, edge.SigBytes)
2,665✔
4621
        if err != nil {
2,665✔
4622
                return err
×
4623
        }
×
4624

4625
        if err := binary.Write(w, byteOrder, edge.ChannelID); err != nil {
2,665✔
4626
                return err
×
4627
        }
×
4628

4629
        var scratch [8]byte
2,665✔
4630
        updateUnix := uint64(edge.LastUpdate.Unix())
2,665✔
4631
        byteOrder.PutUint64(scratch[:], updateUnix)
2,665✔
4632
        if _, err := w.Write(scratch[:]); err != nil {
2,665✔
4633
                return err
×
4634
        }
×
4635

4636
        if err := binary.Write(w, byteOrder, edge.MessageFlags); err != nil {
2,665✔
4637
                return err
×
4638
        }
×
4639
        if err := binary.Write(w, byteOrder, edge.ChannelFlags); err != nil {
2,665✔
4640
                return err
×
4641
        }
×
4642
        if err := binary.Write(w, byteOrder, edge.TimeLockDelta); err != nil {
2,665✔
4643
                return err
×
4644
        }
×
4645
        if err := binary.Write(w, byteOrder, uint64(edge.MinHTLC)); err != nil {
2,665✔
4646
                return err
×
4647
        }
×
4648
        err = binary.Write(w, byteOrder, uint64(edge.FeeBaseMSat))
2,665✔
4649
        if err != nil {
2,665✔
4650
                return err
×
4651
        }
×
4652
        err = binary.Write(
2,665✔
4653
                w, byteOrder, uint64(edge.FeeProportionalMillionths),
2,665✔
4654
        )
2,665✔
4655
        if err != nil {
2,665✔
4656
                return err
×
4657
        }
×
4658

4659
        if _, err := w.Write(to); err != nil {
2,665✔
4660
                return err
×
4661
        }
×
4662

4663
        // If the max_htlc field is present, we write it. To be compatible with
4664
        // older versions that wasn't aware of this field, we write it as part
4665
        // of the opaque data.
4666
        // TODO(halseth): clean up when moving to TLV.
4667
        var opaqueBuf bytes.Buffer
2,665✔
4668
        if edge.MessageFlags.HasMaxHtlc() {
4,946✔
4669
                err := binary.Write(&opaqueBuf, byteOrder, uint64(edge.MaxHTLC))
2,281✔
4670
                if err != nil {
2,281✔
4671
                        return err
×
4672
                }
×
4673
        }
4674

4675
        // Validate that the ExtraOpaqueData is in fact a valid TLV stream.
4676
        err = edge.ExtraOpaqueData.ValidateTLV()
2,665✔
4677
        if err != nil {
2,667✔
4678
                return fmt.Errorf("%w: %w", ErrParsingExtraTLVBytes, err)
2✔
4679
        }
2✔
4680

4681
        if len(edge.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
2,663✔
4682
                return ErrTooManyExtraOpaqueBytes(len(edge.ExtraOpaqueData))
×
4683
        }
×
4684
        if _, err := opaqueBuf.Write(edge.ExtraOpaqueData); err != nil {
2,663✔
4685
                return err
×
4686
        }
×
4687

4688
        if err := wire.WriteVarBytes(w, 0, opaqueBuf.Bytes()); err != nil {
2,663✔
4689
                return err
×
4690
        }
×
4691

4692
        return nil
2,663✔
4693
}
4694

4695
func deserializeChanEdgePolicy(r io.Reader) (*models.ChannelEdgePolicy, error) {
12,134✔
4696
        // Deserialize the policy. Note that in case an optional field is not
12,134✔
4697
        // found or if the edge has invalid TLV data, then both an error and a
12,134✔
4698
        // populated policy object are returned so that the caller can decide
12,134✔
4699
        // if it still wants to use the edge or not.
12,134✔
4700
        edge, err := deserializeChanEdgePolicyRaw(r)
12,134✔
4701
        if err != nil &&
12,134✔
4702
                !errors.Is(err, ErrEdgePolicyOptionalFieldNotFound) &&
12,134✔
4703
                !errors.Is(err, ErrParsingExtraTLVBytes) {
12,134✔
4704

×
4705
                return nil, err
×
4706
        }
×
4707

4708
        return edge, err
12,134✔
4709
}
4710

4711
func deserializeChanEdgePolicyRaw(r io.Reader) (*models.ChannelEdgePolicy,
4712
        error) {
13,141✔
4713

13,141✔
4714
        edge := &models.ChannelEdgePolicy{}
13,141✔
4715

13,141✔
4716
        var err error
13,141✔
4717
        edge.SigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
13,141✔
4718
        if err != nil {
13,141✔
4719
                return nil, err
×
4720
        }
×
4721

4722
        if err := binary.Read(r, byteOrder, &edge.ChannelID); err != nil {
13,141✔
4723
                return nil, err
×
4724
        }
×
4725

4726
        var scratch [8]byte
13,141✔
4727
        if _, err := r.Read(scratch[:]); err != nil {
13,141✔
4728
                return nil, err
×
4729
        }
×
4730
        unix := int64(byteOrder.Uint64(scratch[:]))
13,141✔
4731
        edge.LastUpdate = time.Unix(unix, 0)
13,141✔
4732

13,141✔
4733
        if err := binary.Read(r, byteOrder, &edge.MessageFlags); err != nil {
13,141✔
4734
                return nil, err
×
4735
        }
×
4736
        if err := binary.Read(r, byteOrder, &edge.ChannelFlags); err != nil {
13,141✔
4737
                return nil, err
×
4738
        }
×
4739
        if err := binary.Read(r, byteOrder, &edge.TimeLockDelta); err != nil {
13,141✔
4740
                return nil, err
×
4741
        }
×
4742

4743
        var n uint64
13,141✔
4744
        if err := binary.Read(r, byteOrder, &n); err != nil {
13,141✔
4745
                return nil, err
×
4746
        }
×
4747
        edge.MinHTLC = lnwire.MilliSatoshi(n)
13,141✔
4748

13,141✔
4749
        if err := binary.Read(r, byteOrder, &n); err != nil {
13,141✔
4750
                return nil, err
×
4751
        }
×
4752
        edge.FeeBaseMSat = lnwire.MilliSatoshi(n)
13,141✔
4753

13,141✔
4754
        if err := binary.Read(r, byteOrder, &n); err != nil {
13,141✔
4755
                return nil, err
×
4756
        }
×
4757
        edge.FeeProportionalMillionths = lnwire.MilliSatoshi(n)
13,141✔
4758

13,141✔
4759
        if _, err := r.Read(edge.ToNode[:]); err != nil {
13,141✔
4760
                return nil, err
×
4761
        }
×
4762

4763
        // We'll try and see if there are any opaque bytes left, if not, then
4764
        // we'll ignore the EOF error and return the edge as is.
4765
        edge.ExtraOpaqueData, err = wire.ReadVarBytes(
13,141✔
4766
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
13,141✔
4767
        )
13,141✔
4768
        switch {
13,141✔
4769
        case errors.Is(err, io.ErrUnexpectedEOF):
×
4770
        case errors.Is(err, io.EOF):
4✔
4771
        case err != nil:
×
4772
                return nil, err
×
4773
        }
4774

4775
        // See if optional fields are present.
4776
        if edge.MessageFlags.HasMaxHtlc() {
25,305✔
4777
                // The max_htlc field should be at the beginning of the opaque
12,164✔
4778
                // bytes.
12,164✔
4779
                opq := edge.ExtraOpaqueData
12,164✔
4780

12,164✔
4781
                // If the max_htlc field is not present, it might be old data
12,164✔
4782
                // stored before this field was validated. We'll return the
12,164✔
4783
                // edge along with an error.
12,164✔
4784
                if len(opq) < 8 {
12,168✔
4785
                        return edge, ErrEdgePolicyOptionalFieldNotFound
4✔
4786
                }
4✔
4787

4788
                maxHtlc := byteOrder.Uint64(opq[:8])
12,160✔
4789
                edge.MaxHTLC = lnwire.MilliSatoshi(maxHtlc)
12,160✔
4790

12,160✔
4791
                // Exclude the parsed field from the rest of the opaque data.
12,160✔
4792
                edge.ExtraOpaqueData = opq[8:]
12,160✔
4793
        }
4794

4795
        // Attempt to extract the inbound fee from the opaque data. If we fail
4796
        // to parse the TLV here, we return an error we also return the edge
4797
        // so that the caller can still use it. This is for backwards
4798
        // compatibility in case we have already persisted some policies that
4799
        // have invalid TLV data.
4800
        var inboundFee lnwire.Fee
13,137✔
4801
        typeMap, err := edge.ExtraOpaqueData.ExtractRecords(&inboundFee)
13,137✔
4802
        if err != nil {
13,137✔
4803
                return edge, fmt.Errorf("%w: %w", ErrParsingExtraTLVBytes, err)
×
4804
        }
×
4805

4806
        val, ok := typeMap[lnwire.FeeRecordType]
13,137✔
4807
        if ok && val == nil {
14,886✔
4808
                edge.InboundFee = fn.Some(inboundFee)
1,749✔
4809
        }
1,749✔
4810

4811
        return edge, nil
13,137✔
4812
}
4813

4814
// chanGraphNodeTx is an implementation of the NodeRTx interface backed by the
4815
// KVStore and a kvdb.RTx.
4816
type chanGraphNodeTx struct {
4817
        tx   kvdb.RTx
4818
        db   *KVStore
4819
        node *models.LightningNode
4820
}
4821

4822
// A compile-time constraint to ensure chanGraphNodeTx implements the NodeRTx
4823
// interface.
4824
var _ NodeRTx = (*chanGraphNodeTx)(nil)
4825

4826
func newChanGraphNodeTx(tx kvdb.RTx, db *KVStore,
4827
        node *models.LightningNode) *chanGraphNodeTx {
4,102✔
4828

4,102✔
4829
        return &chanGraphNodeTx{
4,102✔
4830
                tx:   tx,
4,102✔
4831
                db:   db,
4,102✔
4832
                node: node,
4,102✔
4833
        }
4,102✔
4834
}
4,102✔
4835

4836
// Node returns the raw information of the node.
4837
//
4838
// NOTE: This is a part of the NodeRTx interface.
4839
func (c *chanGraphNodeTx) Node() *models.LightningNode {
5,019✔
4840
        return c.node
5,019✔
4841
}
5,019✔
4842

4843
// FetchNode fetches the node with the given pub key under the same transaction
4844
// used to fetch the current node. The returned node is also a NodeRTx and any
4845
// operations on that NodeRTx will also be done under the same transaction.
4846
//
4847
// NOTE: This is a part of the NodeRTx interface.
4848
func (c *chanGraphNodeTx) FetchNode(nodePub route.Vertex) (NodeRTx, error) {
2,944✔
4849
        node, err := c.db.FetchLightningNodeTx(c.tx, nodePub)
2,944✔
4850
        if err != nil {
2,944✔
4851
                return nil, err
×
4852
        }
×
4853

4854
        return newChanGraphNodeTx(c.tx, c.db, node), nil
2,944✔
4855
}
4856

4857
// ForEachChannel can be used to iterate over the node's channels under
4858
// the same transaction used to fetch the node.
4859
//
4860
// NOTE: This is a part of the NodeRTx interface.
4861
func (c *chanGraphNodeTx) ForEachChannel(f func(*models.ChannelEdgeInfo,
4862
        *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
965✔
4863

965✔
4864
        return c.db.forEachNodeChannelTx(c.tx, c.node.PubKeyBytes,
965✔
4865
                func(_ kvdb.RTx, info *models.ChannelEdgeInfo, policy1,
965✔
4866
                        policy2 *models.ChannelEdgePolicy) error {
3,909✔
4867

2,944✔
4868
                        return f(info, policy1, policy2)
2,944✔
4869
                },
2,944✔
4870
        )
4871
}
4872

4873
// MakeTestGraph creates a new instance of the ChannelGraph for testing
4874
// purposes.
4875
//
4876
// NOTE: this helper currently creates a ChannelGraph that is only ever backed
4877
// by the `KVStore` of the `V1Store` interface.
4878
func MakeTestGraph(t testing.TB, opts ...ChanGraphOption) *ChannelGraph {
140✔
4879
        t.Helper()
140✔
4880

140✔
4881
        // Next, create KVStore for the first time.
140✔
4882
        backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
140✔
4883
        t.Cleanup(backendCleanup)
140✔
4884
        require.NoError(t, err)
140✔
4885
        t.Cleanup(func() {
280✔
4886
                require.NoError(t, backend.Close())
140✔
4887
        })
140✔
4888

4889
        graphStore, err := NewKVStore(backend)
140✔
4890
        require.NoError(t, err)
140✔
4891

140✔
4892
        graph, err := NewChannelGraph(graphStore, opts...)
140✔
4893
        require.NoError(t, err)
140✔
4894
        require.NoError(t, graph.Start())
140✔
4895
        t.Cleanup(func() {
280✔
4896
                require.NoError(t, graph.Stop())
140✔
4897
        })
140✔
4898

4899
        return graph
140✔
4900
}
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