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

lightningnetwork / lnd / 13416360603

19 Feb 2025 03:34PM UTC coverage: 58.686% (-0.1%) from 58.794%
13416360603

Pull #9529

github

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

2732 of 3486 new or added lines in 9 files covered. (78.37%)

358 existing lines in 29 files now uncovered.

135967 of 231686 relevant lines covered (58.69%)

19334.46 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

195
        chanScheduler batch.Scheduler
196
        nodeScheduler batch.Scheduler
197
}
198

199
// NewKVStore allocates a new KVStore backed by a DB instance. The
200
// returned instance has its own unique reject cache and channel cache.
201
func NewKVStore(db kvdb.Backend, options ...KVStoreOptionModifier) (*KVStore,
202
        error) {
176✔
203

176✔
204
        opts := DefaultOptions()
176✔
205
        for _, o := range options {
178✔
206
                o(opts)
2✔
207
        }
2✔
208

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

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

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

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

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

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

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

587✔
252
                        return nil
587✔
253
                }
587✔
254

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

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

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

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

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

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

284
                channelMap[key] = edge
992✔
285

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

292
        return channelMap, nil
147✔
293
}
294

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

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

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

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

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

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

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

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

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

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

374
        return nil
176✔
375
}
376

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

517
                return cb(directedChannel)
497✔
518
        }
519

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

692
        return disabledChanIDs, nil
6✔
693
}
694

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

818
        return source, nil
232✔
819
}
820

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

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

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

840
        return &node, nil
494✔
841
}
842

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

948
        return alias, nil
3✔
949
}
950

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1054
                f(r)
2✔
1055
        }
1056

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1225
                        return nil
89✔
1226
                }
1227

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

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

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

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

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

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

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

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

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

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

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

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

1293
                edge.AuthProof = proof
3✔
1294

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1422
        return chansClosed, nil
241✔
1423
}
1424

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

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

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

1456
        return prunedNodes, err
264✔
1457
}
1458

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

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

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

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

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

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

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

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

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

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

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

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

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

NEW
1545
                        return nil, err
×
1546
                }
1547

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

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

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

1559
        return pruned, err
264✔
1560
}
1561

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1689
        return removedChans, nil
155✔
1690
}
1691

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

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

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

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

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

37✔
1726
                return nil
37✔
1727
        }, func() {})
55✔
1728
        if err != nil {
75✔
1729
                return nil, 0, err
20✔
1730
        }
20✔
1731

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

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

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

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

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

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

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

1790
                return nil
82✔
1791
        }, func() {
142✔
1792
                infos = nil
142✔
1793
        })
142✔
1794
        if err != nil {
202✔
1795
                return nil, err
60✔
1796
        }
60✔
1797

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

1803
        return infos, nil
82✔
1804
}
1805

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

1821
        return chanID, nil
3✔
1822
}
1823

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

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

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

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

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

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

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

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

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

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

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

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

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

1892
        return cid, nil
5✔
1893
}
1894

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

11✔
1990
                                continue
11✔
1991
                        }
1992

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2131
        return nodesInHorizon, nil
10✔
2132
}
2133

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

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

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

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

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

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

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

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

89✔
2184
                                if isZombie {
134✔
2185
                                        knownZombies = append(
45✔
2186
                                                knownZombies, info,
45✔
2187
                                        )
45✔
2188

45✔
2189
                                        continue
45✔
2190
                                }
2191
                        }
2192

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

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

NEW
2210
                return ogChanIDs, nil, nil
×
2211

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

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

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

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

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

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

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

220✔
2248
        if node1Timestamp.IsZero() {
430✔
2249
                chanInfo.Node1UpdateTimestamp = time.Unix(0, 0)
210✔
2250
        }
210✔
2251

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

2256
        return chanInfo
220✔
2257
}
2258

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

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

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

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

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

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

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

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

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

2325
                        if edgeInfo.AuthProof == nil {
48✔
2326
                                continue
2✔
2327
                        }
2328

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

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

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

22✔
2344
                                continue
22✔
2345
                        }
2346

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

24✔
2349
                        rawPolicy := edges.Get(node1Key)
24✔
2350
                        if len(rawPolicy) != 0 {
32✔
2351
                                r := bytes.NewReader(rawPolicy)
8✔
2352

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

×
NEW
2358
                                        return err
×
NEW
2359
                                }
×
2360

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

2364
                        rawPolicy = edges.Get(node2Key)
24✔
2365
                        if len(rawPolicy) != 0 {
37✔
2366
                                r := bytes.NewReader(rawPolicy)
13✔
2367

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

×
NEW
2373
                                        return err
×
NEW
2374
                                }
×
2375

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

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

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

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

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

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

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

2416
        return channelRanges, nil
10✔
2417
}
2418

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

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

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

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

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

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

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

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

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

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

2507
                return nil
6✔
2508
        }
2509

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

2518
                return chanEdges, nil
6✔
2519
        }
2520

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

NEW
2526
        return chanEdges, nil
×
2527
}
2528

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

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

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

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

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

2568
        return nil
141✔
2569
}
2570

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

201✔
2582
        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
201✔
2583
        if err != nil {
261✔
2584
                return nil, err
60✔
2585
        }
60✔
2586

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2894
        return nodeIsPublic, nil
15✔
2895
}
2896

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

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

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

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

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

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

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

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

2947
                node = &n
3,770✔
2948

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

2952
        if tx == nil {
3,941✔
2953
                err := kvdb.View(
157✔
2954
                        c.db, fetch, func() {
314✔
2955
                                node = nil
157✔
2956
                        },
157✔
2957
                )
2958
                if err != nil {
162✔
2959
                        return nil, err
5✔
2960
                }
5✔
2961

2962
                return node, nil
154✔
2963
        }
2964

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3095
                return nil
1,259✔
3096
        }
3097

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

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

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

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

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

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

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

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

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

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

3178
                targetNode = &node
2✔
3179

2✔
3180
                return nil
2✔
3181
        }
3182

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

3194
        return targetNode, err
2✔
3195
}
3196

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3✔
3369
                        return ErrZombieEdge
3✔
3370
                }
3371

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

3377
                edgeInfo = &edge
2,675✔
3378

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

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

2,675✔
3391
                return nil
2,675✔
3392
        }, func() {
2,681✔
3393
                edgeInfo = nil
2,681✔
3394
                policy1 = nil
2,681✔
3395
                policy2 = nil
2,681✔
3396
        })
2,681✔
3397
        if errors.Is(err, ErrZombieEdge) {
2,684✔
3398
                return edgeInfo, nil, nil, err
3✔
3399
        }
3✔
3400
        if err != nil {
2,687✔
3401
                return nil, nil, nil, err
7✔
3402
        }
7✔
3403

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

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

3426
                nodeIsPublic, err = c.isPublic(tx, node.PubKeyBytes, ourPubKey)
15✔
3427

15✔
3428
                return err
15✔
3429
        }, func() {
15✔
3430
                nodeIsPublic = false
15✔
3431
        })
15✔
3432
        if err != nil {
15✔
NEW
3433
                return false, err
×
NEW
3434
        }
×
3435

3436
        return nodeIsPublic, nil
15✔
3437
}
3438

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

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

48✔
3456
        return bldr.Script()
48✔
3457
}
3458

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

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

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

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

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

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

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

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

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

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

3544
        return edgePoints, nil
24✔
3545
}
3546

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

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

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

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

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

133✔
3576
        return nil
133✔
3577
}
3578

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

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

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

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

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

22✔
3600
        return c.markEdgeLiveUnsafe(nil, chanID)
22✔
3601
}
22✔
3602

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

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

22✔
3623
                if len(zombieIndex.Get(k[:])) == 0 {
23✔
3624
                        return ErrZombieEdgeNotFound
1✔
3625
                }
1✔
3626

3627
                return zombieIndex.Delete(k[:])
21✔
3628
        }
3629

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

3642
        c.rejectCache.remove(chanID)
21✔
3643
        c.chanCache.remove(chanID)
21✔
3644

21✔
3645
        return nil
21✔
3646
}
3647

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

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

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

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

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

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

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

191✔
3691
        v := zombieIndex.Get(k[:])
191✔
3692
        if v == nil {
294✔
3693
                return false, [33]byte{}, [33]byte{}
103✔
3694
        }
103✔
3695

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

90✔
3700
        return true, pubKey1, pubKey2
90✔
3701
}
3702

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

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

3727
        return numZombies, nil
4✔
3728
}
3729

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

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

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

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

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

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

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

3774
        return isClosed, nil
4✔
3775
}
3776

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

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

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

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

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

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

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

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

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

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

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

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

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

3856
                return nodeBucket.Put(nodePub, b.Bytes())
73✔
3857
        }
3858

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3944
        return nodeBucket.Put(nodePub, b.Bytes())
918✔
3945
}
3946

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

3,611✔
3950
        nodeBytes := nodeBucket.Get(nodePub)
3,611✔
3951
        if nodeBytes == nil {
3,683✔
3952
                return models.LightningNode{}, ErrGraphNodeNotFound
72✔
3953
        }
72✔
3954

3955
        nodeReader := bytes.NewReader(nodeBytes)
3,541✔
3956

3,541✔
3957
        return deserializeLightningNode(nodeReader)
3,541✔
3958
}
3959

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

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

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

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

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

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

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

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

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

4012
        return pubKey, features, nil
122✔
4013
}
4014

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

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

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

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

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

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

4041
        hasNodeAnn := byteOrder.Uint16(scratch[:2])
8,500✔
4042
        if hasNodeAnn == 1 {
16,860✔
4043
                node.HaveNodeAnnouncement = true
8,360✔
4044
        } else {
8,502✔
4045
                node.HaveNodeAnnouncement = false
142✔
4046
        }
142✔
4047

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

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

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

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

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

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

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

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

4108
        return node, nil
8,360✔
4109
}
4110

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

1,483✔
4114
        var b bytes.Buffer
1,483✔
4115

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

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

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

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

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

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

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

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

6,788✔
4183
        edgeInfoBytes := edgeIndex.Get(chanID)
6,788✔
4184
        if edgeInfoBytes == nil {
6,854✔
4185
                return models.ChannelEdgeInfo{}, ErrEdgeNotFound
66✔
4186
        }
66✔
4187

4188
        edgeInfoReader := bytes.NewReader(edgeInfoBytes)
6,724✔
4189

6,724✔
4190
        return deserializeChanEdgeInfo(edgeInfoReader)
6,724✔
4191
}
4192

4193
func deserializeChanEdgeInfo(r io.Reader) (models.ChannelEdgeInfo, error) {
7,263✔
4194
        var (
7,263✔
4195
                err      error
7,263✔
4196
                edgeInfo models.ChannelEdgeInfo
7,263✔
4197
        )
7,263✔
4198

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

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

4217
        proof := &models.ChannelAuthProof{}
7,263✔
4218

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

4236
        if !proof.IsEmpty() {
11,425✔
4237
                edgeInfo.AuthProof = proof
4,162✔
4238
        }
4,162✔
4239

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

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

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

4267
        return edgeInfo, nil
7,263✔
4268
}
4269

4270
func putChanEdgePolicy(edges kvdb.RwBucket, edge *models.ChannelEdgePolicy,
4271
        from, to []byte) error {
2,662✔
4272

2,662✔
4273
        var edgeKey [33 + 8]byte
2,662✔
4274
        copy(edgeKey[:], from)
2,662✔
4275
        byteOrder.PutUint64(edgeKey[33:], edge.ChannelID)
2,662✔
4276

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

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

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

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

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

×
NEW
4316
                        return err
×
NEW
4317
                }
×
4318

4319
                oldUpdateTime := uint64(oldEdgePolicy.LastUpdate.Unix())
26✔
4320

26✔
4321
                var oldIndexKey [8 + 8]byte
26✔
4322
                byteOrder.PutUint64(oldIndexKey[:8], oldUpdateTime)
26✔
4323
                byteOrder.PutUint64(oldIndexKey[8:], edge.ChannelID)
26✔
4324

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

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

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

4343
        return edges.Put(edgeKey[:], b.Bytes())
2,662✔
4344
}
4345

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

2,940✔
4358
        var disabledEdgeKey [8 + 1]byte
2,940✔
4359
        byteOrder.PutUint64(disabledEdgeKey[0:], chanID)
2,940✔
4360
        if direction {
4,409✔
4361
                disabledEdgeKey[8] = 1
1,469✔
4362
        }
1,469✔
4363

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

4371
        if disabled {
2,968✔
4372
                return disabledEdgePolicyIndex.Put(disabledEdgeKey[:], []byte{})
28✔
4373
        }
28✔
4374

4375
        return disabledEdgePolicyIndex.Delete(disabledEdgeKey[:])
2,914✔
4376
}
4377

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

2,962✔
4383
        var edgeKey [33 + 8]byte
2,962✔
4384
        copy(edgeKey[:], from)
2,962✔
4385
        byteOrder.PutUint64(edgeKey[33:], channelID)
2,962✔
4386

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

4392
        return edges.Put(edgeKey[:], unknownPolicy)
2,962✔
4393
}
4394

4395
func fetchChanEdgePolicy(edges kvdb.RBucket, chanID []byte,
4396
        nodePub []byte) (*models.ChannelEdgePolicy, error) {
13,464✔
4397

13,464✔
4398
        var edgeKey [33 + 8]byte
13,464✔
4399
        copy(edgeKey[:], nodePub)
13,464✔
4400
        copy(edgeKey[33:], chanID)
13,464✔
4401

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

4407
        // No need to deserialize unknown policy.
4408
        if bytes.Equal(edgeBytes, unknownPolicy) {
14,902✔
4409
                return nil, nil
1,438✔
4410
        }
1,438✔
4411

4412
        edgeReader := bytes.NewReader(edgeBytes)
12,028✔
4413

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

NEW
4421
        case err != nil:
×
NEW
4422
                return nil, err
×
4423
        }
4424

4425
        return ep, nil
12,026✔
4426
}
4427

4428
func fetchChanEdgePolicies(edgeIndex kvdb.RBucket, edges kvdb.RBucket,
4429
        chanID []byte) (*models.ChannelEdgePolicy, *models.ChannelEdgePolicy,
4430
        error) {
2,893✔
4431

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

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

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

4457
        return edge1, edge2, nil
2,893✔
4458
}
4459

4460
func serializeChanEdgePolicy(w io.Writer, edge *models.ChannelEdgePolicy,
4461
        to []byte) error {
2,664✔
4462

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

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

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

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

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

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

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

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

4529
        return nil
2,664✔
4530
}
4531

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

×
NEW
4539
                return nil, deserializeErr
×
NEW
4540
        }
×
4541

4542
        return edge, deserializeErr
12,053✔
4543
}
4544

4545
func deserializeChanEdgePolicyRaw(r io.Reader) (*models.ChannelEdgePolicy,
4546
        error) {
13,060✔
4547

13,060✔
4548
        edge := &models.ChannelEdgePolicy{}
13,060✔
4549

13,060✔
4550
        var err error
13,060✔
4551
        edge.SigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
13,060✔
4552
        if err != nil {
13,060✔
NEW
4553
                return nil, err
×
NEW
4554
        }
×
4555

4556
        if err := binary.Read(r, byteOrder, &edge.ChannelID); err != nil {
13,060✔
NEW
4557
                return nil, err
×
NEW
4558
        }
×
4559

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

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

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

13,060✔
4583
        if err := binary.Read(r, byteOrder, &n); err != nil {
13,060✔
NEW
4584
                return nil, err
×
NEW
4585
        }
×
4586
        edge.FeeBaseMSat = lnwire.MilliSatoshi(n)
13,060✔
4587

13,060✔
4588
        if err := binary.Read(r, byteOrder, &n); err != nil {
13,060✔
NEW
4589
                return nil, err
×
NEW
4590
        }
×
4591
        edge.FeeProportionalMillionths = lnwire.MilliSatoshi(n)
13,060✔
4592

13,060✔
4593
        if _, err := r.Read(edge.ToNode[:]); err != nil {
13,060✔
NEW
4594
                return nil, err
×
NEW
4595
        }
×
4596

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

4609
        // See if optional fields are present.
4610
        if edge.MessageFlags.HasMaxHtlc() {
25,128✔
4611
                // The max_htlc field should be at the beginning of the opaque
12,068✔
4612
                // bytes.
12,068✔
4613
                opq := edge.ExtraOpaqueData
12,068✔
4614

12,068✔
4615
                // If the max_htlc field is not present, it might be old data
12,068✔
4616
                // stored before this field was validated. We'll return the
12,068✔
4617
                // edge along with an error.
12,068✔
4618
                if len(opq) < 8 {
12,072✔
4619
                        return edge, ErrEdgePolicyOptionalFieldNotFound
4✔
4620
                }
4✔
4621

4622
                maxHtlc := byteOrder.Uint64(opq[:8])
12,064✔
4623
                edge.MaxHTLC = lnwire.MilliSatoshi(maxHtlc)
12,064✔
4624

12,064✔
4625
                // Exclude the parsed field from the rest of the opaque data.
12,064✔
4626
                edge.ExtraOpaqueData = opq[8:]
12,064✔
4627
        }
4628

4629
        return edge, nil
13,056✔
4630
}
4631

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

4640
// A compile-time constraint to ensure chanGraphNodeTx implements the NodeRTx
4641
// interface.
4642
var _ NodeRTx = (*chanGraphNodeTx)(nil)
4643

4644
func newChanGraphNodeTx(tx kvdb.RTx, db *KVStore,
4645
        node *models.LightningNode) *chanGraphNodeTx {
3,916✔
4646

3,916✔
4647
        return &chanGraphNodeTx{
3,916✔
4648
                tx:   tx,
3,916✔
4649
                db:   db,
3,916✔
4650
                node: node,
3,916✔
4651
        }
3,916✔
4652
}
3,916✔
4653

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

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

4672
        return newChanGraphNodeTx(c.tx, c.db, node), nil
2,944✔
4673
}
4674

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

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

2,944✔
4686
                        return f(info, policy1, policy2)
2,944✔
4687
                },
2,944✔
4688
        )
4689
}
4690

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

41✔
4696
        opts := DefaultOptions()
41✔
4697
        for _, modifier := range modifiers {
41✔
NEW
4698
                modifier(opts)
×
NEW
4699
        }
×
4700

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

×
NEW
4706
                return nil, err
×
NEW
4707
        }
×
4708

4709
        graph, err := NewChannelGraph(&Config{
41✔
4710
                KVDB:        backend,
41✔
4711
                KVStoreOpts: modifiers,
41✔
4712
        })
41✔
4713
        if err != nil {
41✔
NEW
4714
                backendCleanup()
×
NEW
4715

×
NEW
4716
                return nil, err
×
NEW
4717
        }
×
4718
        require.NoError(t, graph.Start())
41✔
4719

41✔
4720
        t.Cleanup(func() {
82✔
4721
                _ = backend.Close()
41✔
4722
                backendCleanup()
41✔
4723
                require.NoError(t, graph.Stop())
41✔
4724
        })
41✔
4725

4726
        return graph, nil
41✔
4727
}
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