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

lightningnetwork / lnd / 13558005087

27 Feb 2025 03:04AM UTC coverage: 58.834% (-0.001%) from 58.835%
13558005087

Pull #8453

github

Roasbeef
lnwallet/chancloser: increase test coverage of state machine
Pull Request #8453: [4/4] - multi: integrate new rbf coop close FSM into the existing peer flow

1079 of 1370 new or added lines in 23 files covered. (78.76%)

578 existing lines in 40 files now uncovered.

137063 of 232965 relevant lines covered (58.83%)

19205.84 hits per line

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

77.41
/graph/db/graph.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
)
30

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

174✔
204
        opts := DefaultOptions()
174✔
205
        for _, o := range options {
277✔
206
                o(opts)
103✔
207
        }
103✔
208

209
        if !opts.NoMigration {
348✔
210
                if err := initChannelGraph(db); err != nil {
174✔
211
                        return nil, err
×
212
                }
×
213
        }
214

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

174✔
227
        // The graph cache can be turned off (e.g. for mobile users) for a
174✔
228
        // speed/memory usage tradeoff.
174✔
229
        if opts.UseGraphCache {
315✔
230
                g.graphCache = NewGraphCache(opts.PreAllocCacheNumNodes)
141✔
231
                startTime := time.Now()
141✔
232
                log.Debugf("Populating in-memory channel graph, this might " +
141✔
233
                        "take a while...")
141✔
234

141✔
235
                err := g.ForEachNodeCacheable(func(node route.Vertex,
141✔
236
                        features *lnwire.FeatureVector) error {
242✔
237

101✔
238
                        g.graphCache.AddNodeFeatures(node, features)
101✔
239

101✔
240
                        return nil
101✔
241
                })
101✔
242
                if err != nil {
141✔
243
                        return nil, err
×
244
                }
×
245

246
                err = g.ForEachChannel(func(info *models.ChannelEdgeInfo,
141✔
247
                        policy1, policy2 *models.ChannelEdgePolicy) error {
538✔
248

397✔
249
                        g.graphCache.AddChannel(info, policy1, policy2)
397✔
250

397✔
251
                        return nil
397✔
252
                })
397✔
253
                if err != nil {
141✔
254
                        return nil, err
×
255
                }
×
256

257
                log.Debugf("Finished populating in-memory channel graph (took "+
141✔
258
                        "%v, %s)", time.Since(startTime), g.graphCache.Stats())
141✔
259
        }
260

261
        return g, nil
174✔
262
}
263

264
// channelMapKey is the key structure used for storing channel edge policies.
265
type channelMapKey struct {
266
        nodeKey route.Vertex
267
        chanID  [8]byte
268
}
269

270
// getChannelMap loads all channel edge policies from the database and stores
271
// them in a map.
272
func (c *ChannelGraph) getChannelMap(edges kvdb.RBucket) (
273
        map[channelMapKey]*models.ChannelEdgePolicy, error) {
145✔
274

145✔
275
        // Create a map to store all channel edge policies.
145✔
276
        channelMap := make(map[channelMapKey]*models.ChannelEdgePolicy)
145✔
277

145✔
278
        err := kvdb.ForAll(edges, func(k, edgeBytes []byte) error {
1,717✔
279
                // Skip embedded buckets.
1,572✔
280
                if bytes.Equal(k, edgeIndexBucket) ||
1,572✔
281
                        bytes.Equal(k, edgeUpdateIndexBucket) ||
1,572✔
282
                        bytes.Equal(k, zombieBucket) ||
1,572✔
283
                        bytes.Equal(k, disabledEdgePolicyBucket) ||
1,572✔
284
                        bytes.Equal(k, channelPointBucket) {
2,154✔
285

582✔
286
                        return nil
582✔
287
                }
582✔
288

289
                // Validate key length.
290
                if len(k) != 33+8 {
991✔
291
                        return fmt.Errorf("invalid edge key %x encountered", k)
×
292
                }
×
293

294
                var key channelMapKey
991✔
295
                copy(key.nodeKey[:], k[:33])
991✔
296
                copy(key.chanID[:], k[33:])
991✔
297

991✔
298
                // No need to deserialize unknown policy.
991✔
299
                if bytes.Equal(edgeBytes, unknownPolicy) {
991✔
300
                        return nil
×
301
                }
×
302

303
                edgeReader := bytes.NewReader(edgeBytes)
991✔
304
                edge, err := deserializeChanEdgePolicyRaw(
991✔
305
                        edgeReader,
991✔
306
                )
991✔
307

991✔
308
                switch {
991✔
309
                // If the db policy was missing an expected optional field, we
310
                // return nil as if the policy was unknown.
311
                case err == ErrEdgePolicyOptionalFieldNotFound:
×
312
                        return nil
×
313

314
                case err != nil:
×
315
                        return err
×
316
                }
317

318
                channelMap[key] = edge
991✔
319

991✔
320
                return nil
991✔
321
        })
322
        if err != nil {
145✔
323
                return nil, err
×
324
        }
×
325

326
        return channelMap, nil
145✔
327
}
328

329
var graphTopLevelBuckets = [][]byte{
330
        nodeBucket,
331
        edgeBucket,
332
        graphMetaBucket,
333
        closedScidBucket,
334
}
335

336
// Wipe completely deletes all saved state within all used buckets within the
337
// database. The deletion is done in a single transaction, therefore this
338
// operation is fully atomic.
339
func (c *ChannelGraph) Wipe() error {
×
340
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
×
341
                for _, tlb := range graphTopLevelBuckets {
×
342
                        err := tx.DeleteTopLevelBucket(tlb)
×
343
                        if err != nil && err != kvdb.ErrBucketNotFound {
×
344
                                return err
×
345
                        }
×
346
                }
347
                return nil
×
348
        }, func() {})
×
349
        if err != nil {
×
350
                return err
×
351
        }
×
352

353
        return initChannelGraph(c.db)
×
354
}
355

356
// createChannelDB creates and initializes a fresh version of  In
357
// the case that the target path has not yet been created or doesn't yet exist,
358
// then the path is created. Additionally, all required top-level buckets used
359
// within the database are created.
360
func initChannelGraph(db kvdb.Backend) error {
174✔
361
        err := kvdb.Update(db, func(tx kvdb.RwTx) error {
348✔
362
                for _, tlb := range graphTopLevelBuckets {
867✔
363
                        if _, err := tx.CreateTopLevelBucket(tlb); err != nil {
693✔
364
                                return err
×
365
                        }
×
366
                }
367

368
                nodes := tx.ReadWriteBucket(nodeBucket)
174✔
369
                _, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
174✔
370
                if err != nil {
174✔
371
                        return err
×
372
                }
×
373
                _, err = nodes.CreateBucketIfNotExists(nodeUpdateIndexBucket)
174✔
374
                if err != nil {
174✔
375
                        return err
×
376
                }
×
377

378
                edges := tx.ReadWriteBucket(edgeBucket)
174✔
379
                _, err = edges.CreateBucketIfNotExists(edgeIndexBucket)
174✔
380
                if err != nil {
174✔
381
                        return err
×
382
                }
×
383
                _, err = edges.CreateBucketIfNotExists(edgeUpdateIndexBucket)
174✔
384
                if err != nil {
174✔
385
                        return err
×
386
                }
×
387
                _, err = edges.CreateBucketIfNotExists(channelPointBucket)
174✔
388
                if err != nil {
174✔
389
                        return err
×
390
                }
×
391
                _, err = edges.CreateBucketIfNotExists(zombieBucket)
174✔
392
                if err != nil {
174✔
393
                        return err
×
394
                }
×
395

396
                graphMeta := tx.ReadWriteBucket(graphMetaBucket)
174✔
397
                _, err = graphMeta.CreateBucketIfNotExists(pruneLogBucket)
174✔
398
                return err
174✔
399
        }, func() {})
174✔
400
        if err != nil {
174✔
401
                return fmt.Errorf("unable to create new channel graph: %w", err)
×
402
        }
×
403

404
        return nil
174✔
405
}
406

407
// AddrsForNode returns all known addresses for the target node public key that
408
// the graph DB is aware of. The returned boolean indicates if the given node is
409
// unknown to the graph DB or not.
410
//
411
// NOTE: this is part of the channeldb.AddrSource interface.
412
func (c *ChannelGraph) AddrsForNode(nodePub *btcec.PublicKey) (bool, []net.Addr,
413
        error) {
2✔
414

2✔
415
        pubKey, err := route.NewVertexFromBytes(nodePub.SerializeCompressed())
2✔
416
        if err != nil {
2✔
417
                return false, nil, err
×
418
        }
×
419

420
        node, err := c.FetchLightningNode(pubKey)
2✔
421
        // We don't consider it an error if the graph is unaware of the node.
2✔
422
        switch {
2✔
423
        case err != nil && !errors.Is(err, ErrGraphNodeNotFound):
×
424
                return false, nil, err
×
425

426
        case errors.Is(err, ErrGraphNodeNotFound):
1✔
427
                return false, nil, nil
1✔
428
        }
429

430
        return true, node.Addresses, nil
2✔
431
}
432

433
// ForEachChannel iterates through all the channel edges stored within the
434
// graph and invokes the passed callback for each edge. The callback takes two
435
// edges as since this is a directed graph, both the in/out edges are visited.
436
// If the callback returns an error, then the transaction is aborted and the
437
// iteration stops early.
438
//
439
// NOTE: If an edge can't be found, or wasn't advertised, then a nil pointer
440
// for that particular channel edge routing policy will be passed into the
441
// callback.
442
func (c *ChannelGraph) ForEachChannel(cb func(*models.ChannelEdgeInfo,
443
        *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
145✔
444

145✔
445
        return c.db.View(func(tx kvdb.RTx) error {
290✔
446
                edges := tx.ReadBucket(edgeBucket)
145✔
447
                if edges == nil {
145✔
448
                        return ErrGraphNoEdgesFound
×
449
                }
×
450

451
                // First, load all edges in memory indexed by node and channel
452
                // id.
453
                channelMap, err := c.getChannelMap(edges)
145✔
454
                if err != nil {
145✔
455
                        return err
×
456
                }
×
457

458
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
145✔
459
                if edgeIndex == nil {
145✔
460
                        return ErrGraphNoEdgesFound
×
461
                }
×
462

463
                // Load edge index, recombine each channel with the policies
464
                // loaded above and invoke the callback.
465
                return kvdb.ForAll(
145✔
466
                        edgeIndex, func(k, edgeInfoBytes []byte) error {
641✔
467
                                var chanID [8]byte
496✔
468
                                copy(chanID[:], k)
496✔
469

496✔
470
                                edgeInfoReader := bytes.NewReader(edgeInfoBytes)
496✔
471
                                info, err := deserializeChanEdgeInfo(
496✔
472
                                        edgeInfoReader,
496✔
473
                                )
496✔
474
                                if err != nil {
496✔
475
                                        return err
×
476
                                }
×
477

478
                                policy1 := channelMap[channelMapKey{
496✔
479
                                        nodeKey: info.NodeKey1Bytes,
496✔
480
                                        chanID:  chanID,
496✔
481
                                }]
496✔
482

496✔
483
                                policy2 := channelMap[channelMapKey{
496✔
484
                                        nodeKey: info.NodeKey2Bytes,
496✔
485
                                        chanID:  chanID,
496✔
486
                                }]
496✔
487

496✔
488
                                return cb(&info, policy1, policy2)
496✔
489
                        },
490
                )
491
        }, func() {})
145✔
492
}
493

494
// forEachNodeDirectedChannel iterates through all channels of a given node,
495
// executing the passed callback on the directed edge representing the channel
496
// and its incoming policy. If the callback returns an error, then the iteration
497
// is halted with the error propagated back up to the caller. An optional read
498
// transaction may be provided. If none is provided, a new one will be created.
499
//
500
// Unknown policies are passed into the callback as nil values.
501
func (c *ChannelGraph) forEachNodeDirectedChannel(tx kvdb.RTx,
502
        node route.Vertex, cb func(channel *DirectedChannel) error) error {
704✔
503

704✔
504
        if c.graphCache != nil {
1,166✔
505
                return c.graphCache.ForEachChannel(node, cb)
462✔
506
        }
462✔
507

508
        // Fallback that uses the database.
509
        toNodeCallback := func() route.Vertex {
376✔
510
                return node
133✔
511
        }
133✔
512
        toNodeFeatures, err := c.fetchNodeFeatures(tx, node)
243✔
513
        if err != nil {
243✔
514
                return err
×
515
        }
×
516

517
        dbCallback := func(tx kvdb.RTx, e *models.ChannelEdgeInfo, p1,
243✔
518
                p2 *models.ChannelEdgePolicy) error {
740✔
519

497✔
520
                var cachedInPolicy *models.CachedEdgePolicy
497✔
521
                if p2 != nil {
991✔
522
                        cachedInPolicy = models.NewCachedPolicy(p2)
494✔
523
                        cachedInPolicy.ToNodePubKey = toNodeCallback
494✔
524
                        cachedInPolicy.ToNodeFeatures = toNodeFeatures
494✔
525
                }
494✔
526

527
                var inboundFee lnwire.Fee
497✔
528
                if p1 != nil {
993✔
529
                        // Extract inbound fee. If there is a decoding error,
496✔
530
                        // skip this edge.
496✔
531
                        _, err := p1.ExtraOpaqueData.ExtractRecords(&inboundFee)
496✔
532
                        if err != nil {
497✔
533
                                return nil
1✔
534
                        }
1✔
535
                }
536

537
                directedChannel := &DirectedChannel{
496✔
538
                        ChannelID:    e.ChannelID,
496✔
539
                        IsNode1:      node == e.NodeKey1Bytes,
496✔
540
                        OtherNode:    e.NodeKey2Bytes,
496✔
541
                        Capacity:     e.Capacity,
496✔
542
                        OutPolicySet: p1 != nil,
496✔
543
                        InPolicy:     cachedInPolicy,
496✔
544
                        InboundFee:   inboundFee,
496✔
545
                }
496✔
546

496✔
547
                if node == e.NodeKey2Bytes {
745✔
548
                        directedChannel.OtherNode = e.NodeKey1Bytes
249✔
549
                }
249✔
550

551
                return cb(directedChannel)
496✔
552
        }
553
        return nodeTraversal(tx, node[:], c.db, dbCallback)
243✔
554
}
555

556
// fetchNodeFeatures returns the features of a given node. If no features are
557
// known for the node, an empty feature vector is returned. An optional read
558
// transaction may be provided. If none is provided, a new one will be created.
559
func (c *ChannelGraph) fetchNodeFeatures(tx kvdb.RTx,
560
        node route.Vertex) (*lnwire.FeatureVector, error) {
1,140✔
561

1,140✔
562
        if c.graphCache != nil {
1,594✔
563
                return c.graphCache.GetFeatures(node), nil
454✔
564
        }
454✔
565

566
        // Fallback that uses the database.
567
        targetNode, err := c.FetchLightningNodeTx(tx, node)
687✔
568
        switch err {
687✔
569
        // If the node exists and has features, return them directly.
570
        case nil:
676✔
571
                return targetNode.Features, nil
676✔
572

573
        // If we couldn't find a node announcement, populate a blank feature
574
        // vector.
575
        case ErrGraphNodeNotFound:
11✔
576
                return lnwire.EmptyFeatureVector(), nil
11✔
577

578
        // Otherwise, bubble the error up.
579
        default:
×
580
                return nil, err
×
581
        }
582
}
583

584
// ForEachNodeDirectedChannel iterates through all channels of a given node,
585
// executing the passed callback on the directed edge representing the channel
586
// and its incoming policy. If the callback returns an error, then the iteration
587
// is halted with the error propagated back up to the caller. If the graphCache
588
// is available, then it will be used to retrieve the node's channels instead
589
// of the database.
590
//
591
// Unknown policies are passed into the callback as nil values.
592
//
593
// NOTE: this is part of the graphdb.NodeTraverser interface.
594
func (c *ChannelGraph) ForEachNodeDirectedChannel(nodePub route.Vertex,
595
        cb func(channel *DirectedChannel) error) error {
112✔
596

112✔
597
        return c.forEachNodeDirectedChannel(nil, nodePub, cb)
112✔
598
}
112✔
599

600
// FetchNodeFeatures returns the features of the given node. If no features are
601
// known for the node, an empty feature vector is returned.
602
// If the graphCache is available, then it will be used to retrieve the node's
603
// features instead of the database.
604
//
605
// NOTE: this is part of the graphdb.NodeTraverser interface.
606
func (c *ChannelGraph) FetchNodeFeatures(nodePub route.Vertex) (
607
        *lnwire.FeatureVector, error) {
88✔
608

88✔
609
        return c.fetchNodeFeatures(nil, nodePub)
88✔
610
}
88✔
611

612
// ForEachNodeCached is similar to forEachNode, but it utilizes the channel
613
// graph cache instead. Note that this doesn't return all the information the
614
// regular forEachNode method does.
615
//
616
// NOTE: The callback contents MUST not be modified.
617
func (c *ChannelGraph) ForEachNodeCached(cb func(node route.Vertex,
618
        chans map[uint64]*DirectedChannel) error) error {
1✔
619

1✔
620
        if c.graphCache != nil {
1✔
621
                return c.graphCache.ForEachNode(cb)
×
622
        }
×
623

624
        // Otherwise call back to a version that uses the database directly.
625
        // We'll iterate over each node, then the set of channels for each
626
        // node, and construct a similar callback functiopn signature as the
627
        // main funcotin expects.
628
        return c.forEachNode(func(tx kvdb.RTx,
1✔
629
                node *models.LightningNode) error {
21✔
630

20✔
631
                channels := make(map[uint64]*DirectedChannel)
20✔
632

20✔
633
                err := c.ForEachNodeChannelTx(tx, node.PubKeyBytes,
20✔
634
                        func(tx kvdb.RTx, e *models.ChannelEdgeInfo,
20✔
635
                                p1 *models.ChannelEdgePolicy,
20✔
636
                                p2 *models.ChannelEdgePolicy) error {
210✔
637

190✔
638
                                toNodeCallback := func() route.Vertex {
190✔
639
                                        return node.PubKeyBytes
×
640
                                }
×
641
                                toNodeFeatures, err := c.fetchNodeFeatures(
190✔
642
                                        tx, node.PubKeyBytes,
190✔
643
                                )
190✔
644
                                if err != nil {
190✔
645
                                        return err
×
646
                                }
×
647

648
                                var cachedInPolicy *models.CachedEdgePolicy
190✔
649
                                if p2 != nil {
380✔
650
                                        cachedInPolicy =
190✔
651
                                                models.NewCachedPolicy(p2)
190✔
652
                                        cachedInPolicy.ToNodePubKey =
190✔
653
                                                toNodeCallback
190✔
654
                                        cachedInPolicy.ToNodeFeatures =
190✔
655
                                                toNodeFeatures
190✔
656
                                }
190✔
657

658
                                directedChannel := &DirectedChannel{
190✔
659
                                        ChannelID: e.ChannelID,
190✔
660
                                        IsNode1: node.PubKeyBytes ==
190✔
661
                                                e.NodeKey1Bytes,
190✔
662
                                        OtherNode:    e.NodeKey2Bytes,
190✔
663
                                        Capacity:     e.Capacity,
190✔
664
                                        OutPolicySet: p1 != nil,
190✔
665
                                        InPolicy:     cachedInPolicy,
190✔
666
                                }
190✔
667

190✔
668
                                if node.PubKeyBytes == e.NodeKey2Bytes {
285✔
669
                                        directedChannel.OtherNode =
95✔
670
                                                e.NodeKey1Bytes
95✔
671
                                }
95✔
672

673
                                channels[e.ChannelID] = directedChannel
190✔
674

190✔
675
                                return nil
190✔
676
                        })
677
                if err != nil {
20✔
678
                        return err
×
679
                }
×
680

681
                return cb(node.PubKeyBytes, channels)
20✔
682
        })
683
}
684

685
// DisabledChannelIDs returns the channel ids of disabled channels.
686
// A channel is disabled when two of the associated ChanelEdgePolicies
687
// have their disabled bit on.
688
func (c *ChannelGraph) DisabledChannelIDs() ([]uint64, error) {
6✔
689
        var disabledChanIDs []uint64
6✔
690
        var chanEdgeFound map[uint64]struct{}
6✔
691

6✔
692
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
12✔
693
                edges := tx.ReadBucket(edgeBucket)
6✔
694
                if edges == nil {
6✔
695
                        return ErrGraphNoEdgesFound
×
696
                }
×
697

698
                disabledEdgePolicyIndex := edges.NestedReadBucket(
6✔
699
                        disabledEdgePolicyBucket,
6✔
700
                )
6✔
701
                if disabledEdgePolicyIndex == nil {
7✔
702
                        return nil
1✔
703
                }
1✔
704

705
                // We iterate over all disabled policies and we add each channel
706
                // that has more than one disabled policy to disabledChanIDs
707
                // array.
708
                return disabledEdgePolicyIndex.ForEach(
5✔
709
                        func(k, v []byte) error {
16✔
710
                                chanID := byteOrder.Uint64(k[:8])
11✔
711
                                _, edgeFound := chanEdgeFound[chanID]
11✔
712
                                if edgeFound {
15✔
713
                                        delete(chanEdgeFound, chanID)
4✔
714
                                        disabledChanIDs = append(
4✔
715
                                                disabledChanIDs, chanID,
4✔
716
                                        )
4✔
717

4✔
718
                                        return nil
4✔
719
                                }
4✔
720

721
                                chanEdgeFound[chanID] = struct{}{}
7✔
722

7✔
723
                                return nil
7✔
724
                        },
725
                )
726
        }, func() {
6✔
727
                disabledChanIDs = nil
6✔
728
                chanEdgeFound = make(map[uint64]struct{})
6✔
729
        })
6✔
730
        if err != nil {
6✔
731
                return nil, err
×
732
        }
×
733

734
        return disabledChanIDs, nil
6✔
735
}
736

737
// ForEachNode iterates through all the stored vertices/nodes in the graph,
738
// executing the passed callback with each node encountered. If the callback
739
// returns an error, then the transaction is aborted and the iteration stops
740
// early. Any operations performed on the NodeTx passed to the call-back are
741
// executed under the same read transaction and so, methods on the NodeTx object
742
// _MUST_ only be called from within the call-back.
743
func (c *ChannelGraph) ForEachNode(cb func(tx NodeRTx) error) error {
121✔
744
        return c.forEachNode(func(tx kvdb.RTx,
121✔
745
                node *models.LightningNode) error {
1,092✔
746

971✔
747
                return cb(newChanGraphNodeTx(tx, c, node))
971✔
748
        })
971✔
749
}
750

751
// forEachNode iterates through all the stored vertices/nodes in the graph,
752
// executing the passed callback with each node encountered. If the callback
753
// returns an error, then the transaction is aborted and the iteration stops
754
// early.
755
//
756
// TODO(roasbeef): add iterator interface to allow for memory efficient graph
757
// traversal when graph gets mega.
758
func (c *ChannelGraph) forEachNode(
759
        cb func(kvdb.RTx, *models.LightningNode) error) error {
130✔
760

130✔
761
        traversal := func(tx kvdb.RTx) error {
260✔
762
                // First grab the nodes bucket which stores the mapping from
130✔
763
                // pubKey to node information.
130✔
764
                nodes := tx.ReadBucket(nodeBucket)
130✔
765
                if nodes == nil {
130✔
766
                        return ErrGraphNotFound
×
767
                }
×
768

769
                return nodes.ForEach(func(pubKey, nodeBytes []byte) error {
1,570✔
770
                        // If this is the source key, then we skip this
1,440✔
771
                        // iteration as the value for this key is a pubKey
1,440✔
772
                        // rather than raw node information.
1,440✔
773
                        if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
1,702✔
774
                                return nil
262✔
775
                        }
262✔
776

777
                        nodeReader := bytes.NewReader(nodeBytes)
1,179✔
778
                        node, err := deserializeLightningNode(nodeReader)
1,179✔
779
                        if err != nil {
1,179✔
780
                                return err
×
781
                        }
×
782

783
                        // Execute the callback, the transaction will abort if
784
                        // this returns an error.
785
                        return cb(tx, &node)
1,179✔
786
                })
787
        }
788

789
        return kvdb.View(c.db, traversal, func() {})
260✔
790
}
791

792
// ForEachNodeCacheable iterates through all the stored vertices/nodes in the
793
// graph, executing the passed callback with each node encountered. If the
794
// callback returns an error, then the transaction is aborted and the iteration
795
// stops early.
796
func (c *ChannelGraph) ForEachNodeCacheable(cb func(route.Vertex,
797
        *lnwire.FeatureVector) error) error {
142✔
798

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

807
                return nodes.ForEach(func(pubKey, nodeBytes []byte) error {
545✔
808
                        // If this is the source key, then we skip this
403✔
809
                        // iteration as the value for this key is a pubKey
403✔
810
                        // rather than raw node information.
403✔
811
                        if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
686✔
812
                                return nil
283✔
813
                        }
283✔
814

815
                        nodeReader := bytes.NewReader(nodeBytes)
121✔
816
                        node, features, err := deserializeLightningNodeCacheable( //nolint:ll
121✔
817
                                nodeReader,
121✔
818
                        )
121✔
819
                        if err != nil {
121✔
820
                                return err
×
821
                        }
×
822

823
                        // Execute the callback, the transaction will abort if
824
                        // this returns an error.
825
                        return cb(node, features)
121✔
826
                })
827
        }
828

829
        return kvdb.View(c.db, traversal, func() {})
284✔
830
}
831

832
// SourceNode returns the source node of the graph. The source node is treated
833
// as the center node within a star-graph. This method may be used to kick off
834
// a path finding algorithm in order to explore the reachability of another
835
// node based off the source node.
836
func (c *ChannelGraph) SourceNode() (*models.LightningNode, error) {
232✔
837
        var source *models.LightningNode
232✔
838
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
464✔
839
                // First grab the nodes bucket which stores the mapping from
232✔
840
                // pubKey to node information.
232✔
841
                nodes := tx.ReadBucket(nodeBucket)
232✔
842
                if nodes == nil {
232✔
843
                        return ErrGraphNotFound
×
844
                }
×
845

846
                node, err := c.sourceNode(nodes)
232✔
847
                if err != nil {
233✔
848
                        return err
1✔
849
                }
1✔
850
                source = node
231✔
851

231✔
852
                return nil
231✔
853
        }, func() {
232✔
854
                source = nil
232✔
855
        })
232✔
856
        if err != nil {
233✔
857
                return nil, err
1✔
858
        }
1✔
859

860
        return source, nil
231✔
861
}
862

863
// sourceNode uses an existing database transaction and returns the source node
864
// of the graph. The source node is treated as the center node within a
865
// star-graph. This method may be used to kick off a path finding algorithm in
866
// order to explore the reachability of another node based off the source node.
867
func (c *ChannelGraph) sourceNode(nodes kvdb.RBucket) (*models.LightningNode,
868
        error) {
497✔
869

497✔
870
        selfPub := nodes.Get(sourceKey)
497✔
871
        if selfPub == nil {
498✔
872
                return nil, ErrSourceNodeNotSet
1✔
873
        }
1✔
874

875
        // With the pubKey of the source node retrieved, we're able to
876
        // fetch the full node information.
877
        node, err := fetchLightningNode(nodes, selfPub)
496✔
878
        if err != nil {
496✔
879
                return nil, err
×
880
        }
×
881

882
        return &node, nil
496✔
883
}
884

885
// SetSourceNode sets the source node within the graph database. The source
886
// node is to be used as the center of a star-graph within path finding
887
// algorithms.
888
func (c *ChannelGraph) SetSourceNode(node *models.LightningNode) error {
118✔
889
        nodePubBytes := node.PubKeyBytes[:]
118✔
890

118✔
891
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
236✔
892
                // First grab the nodes bucket which stores the mapping from
118✔
893
                // pubKey to node information.
118✔
894
                nodes, err := tx.CreateTopLevelBucket(nodeBucket)
118✔
895
                if err != nil {
118✔
896
                        return err
×
897
                }
×
898

899
                // Next we create the mapping from source to the targeted
900
                // public key.
901
                if err := nodes.Put(sourceKey, nodePubBytes); err != nil {
118✔
902
                        return err
×
903
                }
×
904

905
                // Finally, we commit the information of the lightning node
906
                // itself.
907
                return addLightningNode(tx, node)
118✔
908
        }, func() {})
118✔
909
}
910

911
// AddLightningNode adds a vertex/node to the graph database. If the node is not
912
// in the database from before, this will add a new, unconnected one to the
913
// graph. If it is present from before, this will update that node's
914
// information. Note that this method is expected to only be called to update an
915
// already present node from a node announcement, or to insert a node found in a
916
// channel update.
917
//
918
// TODO(roasbeef): also need sig of announcement
919
func (c *ChannelGraph) AddLightningNode(node *models.LightningNode,
920
        op ...batch.SchedulerOption) error {
801✔
921

801✔
922
        r := &batch.Request{
801✔
923
                Update: func(tx kvdb.RwTx) error {
1,602✔
924
                        if c.graphCache != nil {
1,415✔
925
                                c.graphCache.AddNodeFeatures(
614✔
926
                                        node.PubKeyBytes, node.Features,
614✔
927
                                )
614✔
928
                        }
614✔
929

930
                        return addLightningNode(tx, node)
801✔
931
                },
932
        }
933

934
        for _, f := range op {
802✔
935
                f(r)
1✔
936
        }
1✔
937

938
        return c.nodeScheduler.Execute(r)
801✔
939
}
940

941
func addLightningNode(tx kvdb.RwTx, node *models.LightningNode) error {
998✔
942
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
998✔
943
        if err != nil {
998✔
944
                return err
×
945
        }
×
946

947
        aliases, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
998✔
948
        if err != nil {
998✔
949
                return err
×
950
        }
×
951

952
        updateIndex, err := nodes.CreateBucketIfNotExists(
998✔
953
                nodeUpdateIndexBucket,
998✔
954
        )
998✔
955
        if err != nil {
998✔
956
                return err
×
957
        }
×
958

959
        return putLightningNode(nodes, aliases, updateIndex, node)
998✔
960
}
961

962
// LookupAlias attempts to return the alias as advertised by the target node.
963
// TODO(roasbeef): currently assumes that aliases are unique...
964
func (c *ChannelGraph) LookupAlias(pub *btcec.PublicKey) (string, error) {
3✔
965
        var alias string
3✔
966

3✔
967
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
968
                nodes := tx.ReadBucket(nodeBucket)
3✔
969
                if nodes == nil {
3✔
970
                        return ErrGraphNodesNotFound
×
971
                }
×
972

973
                aliases := nodes.NestedReadBucket(aliasIndexBucket)
3✔
974
                if aliases == nil {
3✔
975
                        return ErrGraphNodesNotFound
×
976
                }
×
977

978
                nodePub := pub.SerializeCompressed()
3✔
979
                a := aliases.Get(nodePub)
3✔
980
                if a == nil {
4✔
981
                        return ErrNodeAliasNotFound
1✔
982
                }
1✔
983

984
                // TODO(roasbeef): should actually be using the utf-8
985
                // package...
986
                alias = string(a)
2✔
987
                return nil
2✔
988
        }, func() {
3✔
989
                alias = ""
3✔
990
        })
3✔
991
        if err != nil {
4✔
992
                return "", err
1✔
993
        }
1✔
994

995
        return alias, nil
2✔
996
}
997

998
// DeleteLightningNode starts a new database transaction to remove a vertex/node
999
// from the database according to the node's public key.
1000
func (c *ChannelGraph) DeleteLightningNode(nodePub route.Vertex) error {
3✔
1001
        // TODO(roasbeef): ensure dangling edges are removed...
3✔
1002
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1003
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
1004
                if nodes == nil {
3✔
1005
                        return ErrGraphNodeNotFound
×
1006
                }
×
1007

1008
                if c.graphCache != nil {
6✔
1009
                        c.graphCache.RemoveNode(nodePub)
3✔
1010
                }
3✔
1011

1012
                return c.deleteLightningNode(nodes, nodePub[:])
3✔
1013
        }, func() {})
3✔
1014
}
1015

1016
// deleteLightningNode uses an existing database transaction to remove a
1017
// vertex/node from the database according to the node's public key.
1018
func (c *ChannelGraph) deleteLightningNode(nodes kvdb.RwBucket,
1019
        compressedPubKey []byte) error {
73✔
1020

73✔
1021
        aliases := nodes.NestedReadWriteBucket(aliasIndexBucket)
73✔
1022
        if aliases == nil {
73✔
1023
                return ErrGraphNodesNotFound
×
1024
        }
×
1025

1026
        if err := aliases.Delete(compressedPubKey); err != nil {
73✔
1027
                return err
×
1028
        }
×
1029

1030
        // Before we delete the node, we'll fetch its current state so we can
1031
        // determine when its last update was to clear out the node update
1032
        // index.
1033
        node, err := fetchLightningNode(nodes, compressedPubKey)
73✔
1034
        if err != nil {
73✔
1035
                return err
×
1036
        }
×
1037

1038
        if err := nodes.Delete(compressedPubKey); err != nil {
73✔
1039
                return err
×
1040
        }
×
1041

1042
        // Finally, we'll delete the index entry for the node within the
1043
        // nodeUpdateIndexBucket as this node is no longer active, so we don't
1044
        // need to track its last update.
1045
        nodeUpdateIndex := nodes.NestedReadWriteBucket(nodeUpdateIndexBucket)
73✔
1046
        if nodeUpdateIndex == nil {
73✔
1047
                return ErrGraphNodesNotFound
×
1048
        }
×
1049

1050
        // In order to delete the entry, we'll need to reconstruct the key for
1051
        // its last update.
1052
        updateUnix := uint64(node.LastUpdate.Unix())
73✔
1053
        var indexKey [8 + 33]byte
73✔
1054
        byteOrder.PutUint64(indexKey[:8], updateUnix)
73✔
1055
        copy(indexKey[8:], compressedPubKey)
73✔
1056

73✔
1057
        return nodeUpdateIndex.Delete(indexKey[:])
73✔
1058
}
1059

1060
// AddChannelEdge adds a new (undirected, blank) edge to the graph database. An
1061
// undirected edge from the two target nodes are created. The information stored
1062
// denotes the static attributes of the channel, such as the channelID, the keys
1063
// involved in creation of the channel, and the set of features that the channel
1064
// supports. The chanPoint and chanID are used to uniquely identify the edge
1065
// globally within the database.
1066
func (c *ChannelGraph) AddChannelEdge(edge *models.ChannelEdgeInfo,
1067
        op ...batch.SchedulerOption) error {
1,723✔
1068

1,723✔
1069
        var alreadyExists bool
1,723✔
1070
        r := &batch.Request{
1,723✔
1071
                Reset: func() {
3,446✔
1072
                        alreadyExists = false
1,723✔
1073
                },
1,723✔
1074
                Update: func(tx kvdb.RwTx) error {
1,723✔
1075
                        err := c.addChannelEdge(tx, edge)
1,723✔
1076

1,723✔
1077
                        // Silence ErrEdgeAlreadyExist so that the batch can
1,723✔
1078
                        // succeed, but propagate the error via local state.
1,723✔
1079
                        if err == ErrEdgeAlreadyExist {
1,957✔
1080
                                alreadyExists = true
234✔
1081
                                return nil
234✔
1082
                        }
234✔
1083

1084
                        return err
1,489✔
1085
                },
1086
                OnCommit: func(err error) error {
1,723✔
1087
                        switch {
1,723✔
1088
                        case err != nil:
×
1089
                                return err
×
1090
                        case alreadyExists:
234✔
1091
                                return ErrEdgeAlreadyExist
234✔
1092
                        default:
1,489✔
1093
                                c.rejectCache.remove(edge.ChannelID)
1,489✔
1094
                                c.chanCache.remove(edge.ChannelID)
1,489✔
1095
                                return nil
1,489✔
1096
                        }
1097
                },
1098
        }
1099

1100
        for _, f := range op {
1,724✔
1101
                if f == nil {
1✔
1102
                        return fmt.Errorf("nil scheduler option was used")
×
1103
                }
×
1104

1105
                f(r)
1✔
1106
        }
1107

1108
        return c.chanScheduler.Execute(r)
1,723✔
1109
}
1110

1111
// addChannelEdge is the private form of AddChannelEdge that allows callers to
1112
// utilize an existing db transaction.
1113
func (c *ChannelGraph) addChannelEdge(tx kvdb.RwTx,
1114
        edge *models.ChannelEdgeInfo) error {
1,723✔
1115

1,723✔
1116
        // Construct the channel's primary key which is the 8-byte channel ID.
1,723✔
1117
        var chanKey [8]byte
1,723✔
1118
        binary.BigEndian.PutUint64(chanKey[:], edge.ChannelID)
1,723✔
1119

1,723✔
1120
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
1,723✔
1121
        if err != nil {
1,723✔
1122
                return err
×
1123
        }
×
1124
        edges, err := tx.CreateTopLevelBucket(edgeBucket)
1,723✔
1125
        if err != nil {
1,723✔
1126
                return err
×
1127
        }
×
1128
        edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
1,723✔
1129
        if err != nil {
1,723✔
1130
                return err
×
1131
        }
×
1132
        chanIndex, err := edges.CreateBucketIfNotExists(channelPointBucket)
1,723✔
1133
        if err != nil {
1,723✔
1134
                return err
×
1135
        }
×
1136

1137
        // First, attempt to check if this edge has already been created. If
1138
        // so, then we can exit early as this method is meant to be idempotent.
1139
        if edgeInfo := edgeIndex.Get(chanKey[:]); edgeInfo != nil {
1,957✔
1140
                return ErrEdgeAlreadyExist
234✔
1141
        }
234✔
1142

1143
        if c.graphCache != nil {
2,788✔
1144
                c.graphCache.AddChannel(edge, nil, nil)
1,299✔
1145
        }
1,299✔
1146

1147
        // Before we insert the channel into the database, we'll ensure that
1148
        // both nodes already exist in the channel graph. If either node
1149
        // doesn't, then we'll insert a "shell" node that just includes its
1150
        // public key, so subsequent validation and queries can work properly.
1151
        _, node1Err := fetchLightningNode(nodes, edge.NodeKey1Bytes[:])
1,489✔
1152
        switch {
1,489✔
1153
        case node1Err == ErrGraphNodeNotFound:
19✔
1154
                node1Shell := models.LightningNode{
19✔
1155
                        PubKeyBytes:          edge.NodeKey1Bytes,
19✔
1156
                        HaveNodeAnnouncement: false,
19✔
1157
                }
19✔
1158
                err := addLightningNode(tx, &node1Shell)
19✔
1159
                if err != nil {
19✔
1160
                        return fmt.Errorf("unable to create shell node "+
×
1161
                                "for: %x: %w", edge.NodeKey1Bytes, err)
×
1162
                }
×
1163
        case node1Err != nil:
×
1164
                return node1Err
×
1165
        }
1166

1167
        _, node2Err := fetchLightningNode(nodes, edge.NodeKey2Bytes[:])
1,489✔
1168
        switch {
1,489✔
1169
        case node2Err == ErrGraphNodeNotFound:
63✔
1170
                node2Shell := models.LightningNode{
63✔
1171
                        PubKeyBytes:          edge.NodeKey2Bytes,
63✔
1172
                        HaveNodeAnnouncement: false,
63✔
1173
                }
63✔
1174
                err := addLightningNode(tx, &node2Shell)
63✔
1175
                if err != nil {
63✔
1176
                        return fmt.Errorf("unable to create shell node "+
×
1177
                                "for: %x: %w", edge.NodeKey2Bytes, err)
×
1178
                }
×
1179
        case node2Err != nil:
×
1180
                return node2Err
×
1181
        }
1182

1183
        // If the edge hasn't been created yet, then we'll first add it to the
1184
        // edge index in order to associate the edge between two nodes and also
1185
        // store the static components of the channel.
1186
        if err := putChanEdgeInfo(edgeIndex, edge, chanKey); err != nil {
1,489✔
1187
                return err
×
1188
        }
×
1189

1190
        // Mark edge policies for both sides as unknown. This is to enable
1191
        // efficient incoming channel lookup for a node.
1192
        keys := []*[33]byte{
1,489✔
1193
                &edge.NodeKey1Bytes,
1,489✔
1194
                &edge.NodeKey2Bytes,
1,489✔
1195
        }
1,489✔
1196
        for _, key := range keys {
4,466✔
1197
                err := putChanEdgePolicyUnknown(edges, edge.ChannelID, key[:])
2,977✔
1198
                if err != nil {
2,977✔
1199
                        return err
×
1200
                }
×
1201
        }
1202

1203
        // Finally we add it to the channel index which maps channel points
1204
        // (outpoints) to the shorter channel ID's.
1205
        var b bytes.Buffer
1,489✔
1206
        if err := WriteOutpoint(&b, &edge.ChannelPoint); err != nil {
1,489✔
1207
                return err
×
1208
        }
×
1209
        return chanIndex.Put(b.Bytes(), chanKey[:])
1,489✔
1210
}
1211

1212
// HasChannelEdge returns true if the database knows of a channel edge with the
1213
// passed channel ID, and false otherwise. If an edge with that ID is found
1214
// within the graph, then two time stamps representing the last time the edge
1215
// was updated for both directed edges are returned along with the boolean. If
1216
// it is not found, then the zombie index is checked and its result is returned
1217
// as the second boolean.
1218
func (c *ChannelGraph) HasChannelEdge(
1219
        chanID uint64) (time.Time, time.Time, bool, bool, error) {
213✔
1220

213✔
1221
        var (
213✔
1222
                upd1Time time.Time
213✔
1223
                upd2Time time.Time
213✔
1224
                exists   bool
213✔
1225
                isZombie bool
213✔
1226
        )
213✔
1227

213✔
1228
        // We'll query the cache with the shared lock held to allow multiple
213✔
1229
        // readers to access values in the cache concurrently if they exist.
213✔
1230
        c.cacheMu.RLock()
213✔
1231
        if entry, ok := c.rejectCache.get(chanID); ok {
282✔
1232
                c.cacheMu.RUnlock()
69✔
1233
                upd1Time = time.Unix(entry.upd1Time, 0)
69✔
1234
                upd2Time = time.Unix(entry.upd2Time, 0)
69✔
1235
                exists, isZombie = entry.flags.unpack()
69✔
1236
                return upd1Time, upd2Time, exists, isZombie, nil
69✔
1237
        }
69✔
1238
        c.cacheMu.RUnlock()
145✔
1239

145✔
1240
        c.cacheMu.Lock()
145✔
1241
        defer c.cacheMu.Unlock()
145✔
1242

145✔
1243
        // The item was not found with the shared lock, so we'll acquire the
145✔
1244
        // exclusive lock and check the cache again in case another method added
145✔
1245
        // the entry to the cache while no lock was held.
145✔
1246
        if entry, ok := c.rejectCache.get(chanID); ok {
148✔
1247
                upd1Time = time.Unix(entry.upd1Time, 0)
3✔
1248
                upd2Time = time.Unix(entry.upd2Time, 0)
3✔
1249
                exists, isZombie = entry.flags.unpack()
3✔
1250
                return upd1Time, upd2Time, exists, isZombie, nil
3✔
1251
        }
3✔
1252

1253
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
286✔
1254
                edges := tx.ReadBucket(edgeBucket)
143✔
1255
                if edges == nil {
143✔
1256
                        return ErrGraphNoEdgesFound
×
1257
                }
×
1258
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
143✔
1259
                if edgeIndex == nil {
143✔
1260
                        return ErrGraphNoEdgesFound
×
1261
                }
×
1262

1263
                var channelID [8]byte
143✔
1264
                byteOrder.PutUint64(channelID[:], chanID)
143✔
1265

143✔
1266
                // If the edge doesn't exist, then we'll also check our zombie
143✔
1267
                // index.
143✔
1268
                if edgeIndex.Get(channelID[:]) == nil {
237✔
1269
                        exists = false
94✔
1270
                        zombieIndex := edges.NestedReadBucket(zombieBucket)
94✔
1271
                        if zombieIndex != nil {
188✔
1272
                                isZombie, _, _ = isZombieEdge(
94✔
1273
                                        zombieIndex, chanID,
94✔
1274
                                )
94✔
1275
                        }
94✔
1276

1277
                        return nil
94✔
1278
                }
1279

1280
                exists = true
50✔
1281
                isZombie = false
50✔
1282

50✔
1283
                // If the channel has been found in the graph, then retrieve
50✔
1284
                // the edges itself so we can return the last updated
50✔
1285
                // timestamps.
50✔
1286
                nodes := tx.ReadBucket(nodeBucket)
50✔
1287
                if nodes == nil {
50✔
1288
                        return ErrGraphNodeNotFound
×
1289
                }
×
1290

1291
                e1, e2, err := fetchChanEdgePolicies(
50✔
1292
                        edgeIndex, edges, channelID[:],
50✔
1293
                )
50✔
1294
                if err != nil {
50✔
1295
                        return err
×
1296
                }
×
1297

1298
                // As we may have only one of the edges populated, only set the
1299
                // update time if the edge was found in the database.
1300
                if e1 != nil {
69✔
1301
                        upd1Time = e1.LastUpdate
19✔
1302
                }
19✔
1303
                if e2 != nil {
67✔
1304
                        upd2Time = e2.LastUpdate
17✔
1305
                }
17✔
1306

1307
                return nil
50✔
1308
        }, func() {}); err != nil {
143✔
1309
                return time.Time{}, time.Time{}, exists, isZombie, err
×
1310
        }
×
1311

1312
        c.rejectCache.insert(chanID, rejectCacheEntry{
143✔
1313
                upd1Time: upd1Time.Unix(),
143✔
1314
                upd2Time: upd2Time.Unix(),
143✔
1315
                flags:    packRejectFlags(exists, isZombie),
143✔
1316
        })
143✔
1317

143✔
1318
        return upd1Time, upd2Time, exists, isZombie, nil
143✔
1319
}
1320

1321
// AddEdgeProof sets the proof of an existing edge in the graph database.
1322
func (c *ChannelGraph) AddEdgeProof(chanID lnwire.ShortChannelID,
1323
        proof *models.ChannelAuthProof) error {
2✔
1324

2✔
1325
        // Construct the channel's primary key which is the 8-byte channel ID.
2✔
1326
        var chanKey [8]byte
2✔
1327
        binary.BigEndian.PutUint64(chanKey[:], chanID.ToUint64())
2✔
1328

2✔
1329
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
4✔
1330
                edges := tx.ReadWriteBucket(edgeBucket)
2✔
1331
                if edges == nil {
2✔
1332
                        return ErrEdgeNotFound
×
1333
                }
×
1334

1335
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
2✔
1336
                if edgeIndex == nil {
2✔
1337
                        return ErrEdgeNotFound
×
1338
                }
×
1339

1340
                edge, err := fetchChanEdgeInfo(edgeIndex, chanKey[:])
2✔
1341
                if err != nil {
2✔
1342
                        return err
×
1343
                }
×
1344

1345
                edge.AuthProof = proof
2✔
1346

2✔
1347
                return putChanEdgeInfo(edgeIndex, &edge, chanKey)
2✔
1348
        }, func() {})
2✔
1349
}
1350

1351
const (
1352
        // pruneTipBytes is the total size of the value which stores a prune
1353
        // entry of the graph in the prune log. The "prune tip" is the last
1354
        // entry in the prune log, and indicates if the channel graph is in
1355
        // sync with the current UTXO state. The structure of the value
1356
        // is: blockHash, taking 32 bytes total.
1357
        pruneTipBytes = 32
1358
)
1359

1360
// PruneGraph prunes newly closed channels from the channel graph in response
1361
// to a new block being solved on the network. Any transactions which spend the
1362
// funding output of any known channels within he graph will be deleted.
1363
// Additionally, the "prune tip", or the last block which has been used to
1364
// prune the graph is stored so callers can ensure the graph is fully in sync
1365
// with the current UTXO state. A slice of channels that have been closed by
1366
// the target block are returned if the function succeeds without error.
1367
func (c *ChannelGraph) PruneGraph(spentOutputs []*wire.OutPoint,
1368
        blockHash *chainhash.Hash, blockHeight uint32) (
1369
        []*models.ChannelEdgeInfo, error) {
243✔
1370

243✔
1371
        c.cacheMu.Lock()
243✔
1372
        defer c.cacheMu.Unlock()
243✔
1373

243✔
1374
        var chansClosed []*models.ChannelEdgeInfo
243✔
1375

243✔
1376
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
486✔
1377
                // First grab the edges bucket which houses the information
243✔
1378
                // we'd like to delete
243✔
1379
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
243✔
1380
                if err != nil {
243✔
1381
                        return err
×
1382
                }
×
1383

1384
                // Next grab the two edge indexes which will also need to be
1385
                // updated.
1386
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
243✔
1387
                if err != nil {
243✔
1388
                        return err
×
1389
                }
×
1390
                chanIndex, err := edges.CreateBucketIfNotExists(
243✔
1391
                        channelPointBucket,
243✔
1392
                )
243✔
1393
                if err != nil {
243✔
1394
                        return err
×
1395
                }
×
1396
                nodes := tx.ReadWriteBucket(nodeBucket)
243✔
1397
                if nodes == nil {
243✔
1398
                        return ErrSourceNodeNotSet
×
1399
                }
×
1400
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
243✔
1401
                if err != nil {
243✔
1402
                        return err
×
1403
                }
×
1404

1405
                // For each of the outpoints that have been spent within the
1406
                // block, we attempt to delete them from the graph as if that
1407
                // outpoint was a channel, then it has now been closed.
1408
                for _, chanPoint := range spentOutputs {
417✔
1409
                        // TODO(roasbeef): load channel bloom filter, continue
174✔
1410
                        // if NOT if filter
174✔
1411

174✔
1412
                        var opBytes bytes.Buffer
174✔
1413
                        err := WriteOutpoint(&opBytes, chanPoint)
174✔
1414
                        if err != nil {
174✔
1415
                                return err
×
1416
                        }
×
1417

1418
                        // First attempt to see if the channel exists within
1419
                        // the database, if not, then we can exit early.
1420
                        chanID := chanIndex.Get(opBytes.Bytes())
174✔
1421
                        if chanID == nil {
321✔
1422
                                continue
147✔
1423
                        }
1424

1425
                        // However, if it does, then we'll read out the full
1426
                        // version so we can add it to the set of deleted
1427
                        // channels.
1428
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
27✔
1429
                        if err != nil {
27✔
1430
                                return err
×
1431
                        }
×
1432

1433
                        // Attempt to delete the channel, an ErrEdgeNotFound
1434
                        // will be returned if that outpoint isn't known to be
1435
                        // a channel. If no error is returned, then a channel
1436
                        // was successfully pruned.
1437
                        err = c.delChannelEdgeUnsafe(
27✔
1438
                                edges, edgeIndex, chanIndex, zombieIndex,
27✔
1439
                                chanID, false, false,
27✔
1440
                        )
27✔
1441
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
27✔
1442
                                return err
×
1443
                        }
×
1444

1445
                        chansClosed = append(chansClosed, &edgeInfo)
27✔
1446
                }
1447

1448
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
243✔
1449
                if err != nil {
243✔
1450
                        return err
×
1451
                }
×
1452

1453
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
243✔
1454
                        pruneLogBucket,
243✔
1455
                )
243✔
1456
                if err != nil {
243✔
1457
                        return err
×
1458
                }
×
1459

1460
                // With the graph pruned, add a new entry to the prune log,
1461
                // which can be used to check if the graph is fully synced with
1462
                // the current UTXO state.
1463
                var blockHeightBytes [4]byte
243✔
1464
                byteOrder.PutUint32(blockHeightBytes[:], blockHeight)
243✔
1465

243✔
1466
                var newTip [pruneTipBytes]byte
243✔
1467
                copy(newTip[:], blockHash[:])
243✔
1468

243✔
1469
                err = pruneBucket.Put(blockHeightBytes[:], newTip[:])
243✔
1470
                if err != nil {
243✔
1471
                        return err
×
1472
                }
×
1473

1474
                // Now that the graph has been pruned, we'll also attempt to
1475
                // prune any nodes that have had a channel closed within the
1476
                // latest block.
1477
                return c.pruneGraphNodes(nodes, edgeIndex)
243✔
1478
        }, func() {
243✔
1479
                chansClosed = nil
243✔
1480
        })
243✔
1481
        if err != nil {
243✔
1482
                return nil, err
×
1483
        }
×
1484

1485
        for _, channel := range chansClosed {
270✔
1486
                c.rejectCache.remove(channel.ChannelID)
27✔
1487
                c.chanCache.remove(channel.ChannelID)
27✔
1488
        }
27✔
1489

1490
        if c.graphCache != nil {
486✔
1491
                log.Debugf("Pruned graph, cache now has %s",
243✔
1492
                        c.graphCache.Stats())
243✔
1493
        }
243✔
1494

1495
        return chansClosed, nil
243✔
1496
}
1497

1498
// PruneGraphNodes is a garbage collection method which attempts to prune out
1499
// any nodes from the channel graph that are currently unconnected. This ensure
1500
// that we only maintain a graph of reachable nodes. In the event that a pruned
1501
// node gains more channels, it will be re-added back to the graph.
1502
func (c *ChannelGraph) PruneGraphNodes() error {
24✔
1503
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
48✔
1504
                nodes := tx.ReadWriteBucket(nodeBucket)
24✔
1505
                if nodes == nil {
24✔
1506
                        return ErrGraphNodesNotFound
×
1507
                }
×
1508
                edges := tx.ReadWriteBucket(edgeBucket)
24✔
1509
                if edges == nil {
24✔
1510
                        return ErrGraphNotFound
×
1511
                }
×
1512
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
24✔
1513
                if edgeIndex == nil {
24✔
1514
                        return ErrGraphNoEdgesFound
×
1515
                }
×
1516

1517
                return c.pruneGraphNodes(nodes, edgeIndex)
24✔
1518
        }, func() {})
24✔
1519
}
1520

1521
// pruneGraphNodes attempts to remove any nodes from the graph who have had a
1522
// channel closed within the current block. If the node still has existing
1523
// channels in the graph, this will act as a no-op.
1524
func (c *ChannelGraph) pruneGraphNodes(nodes kvdb.RwBucket,
1525
        edgeIndex kvdb.RwBucket) error {
266✔
1526

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

266✔
1529
        // We'll retrieve the graph's source node to ensure we don't remove it
266✔
1530
        // even if it no longer has any open channels.
266✔
1531
        sourceNode, err := c.sourceNode(nodes)
266✔
1532
        if err != nil {
266✔
1533
                return err
×
1534
        }
×
1535

1536
        // We'll use this map to keep count the number of references to a node
1537
        // in the graph. A node should only be removed once it has no more
1538
        // references in the graph.
1539
        nodeRefCounts := make(map[[33]byte]int)
266✔
1540
        err = nodes.ForEach(func(pubKey, nodeBytes []byte) error {
1,568✔
1541
                // If this is the source key, then we skip this
1,302✔
1542
                // iteration as the value for this key is a pubKey
1,302✔
1543
                // rather than raw node information.
1,302✔
1544
                if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
2,098✔
1545
                        return nil
796✔
1546
                }
796✔
1547

1548
                var nodePub [33]byte
507✔
1549
                copy(nodePub[:], pubKey)
507✔
1550
                nodeRefCounts[nodePub] = 0
507✔
1551

507✔
1552
                return nil
507✔
1553
        })
1554
        if err != nil {
266✔
1555
                return err
×
1556
        }
×
1557

1558
        // To ensure we never delete the source node, we'll start off by
1559
        // bumping its ref count to 1.
1560
        nodeRefCounts[sourceNode.PubKeyBytes] = 1
266✔
1561

266✔
1562
        // Next, we'll run through the edgeIndex which maps a channel ID to the
266✔
1563
        // edge info. We'll use this scan to populate our reference count map
266✔
1564
        // above.
266✔
1565
        err = edgeIndex.ForEach(func(chanID, edgeInfoBytes []byte) error {
451✔
1566
                // The first 66 bytes of the edge info contain the pubkeys of
185✔
1567
                // the nodes that this edge attaches. We'll extract them, and
185✔
1568
                // add them to the ref count map.
185✔
1569
                var node1, node2 [33]byte
185✔
1570
                copy(node1[:], edgeInfoBytes[:33])
185✔
1571
                copy(node2[:], edgeInfoBytes[33:])
185✔
1572

185✔
1573
                // With the nodes extracted, we'll increase the ref count of
185✔
1574
                // each of the nodes.
185✔
1575
                nodeRefCounts[node1]++
185✔
1576
                nodeRefCounts[node2]++
185✔
1577

185✔
1578
                return nil
185✔
1579
        })
185✔
1580
        if err != nil {
266✔
1581
                return err
×
1582
        }
×
1583

1584
        // Finally, we'll make a second pass over the set of nodes, and delete
1585
        // any nodes that have a ref count of zero.
1586
        var numNodesPruned int
266✔
1587
        for nodePubKey, refCount := range nodeRefCounts {
773✔
1588
                // If the ref count of the node isn't zero, then we can safely
507✔
1589
                // skip it as it still has edges to or from it within the
507✔
1590
                // graph.
507✔
1591
                if refCount != 0 {
945✔
1592
                        continue
438✔
1593
                }
1594

1595
                if c.graphCache != nil {
140✔
1596
                        c.graphCache.RemoveNode(nodePubKey)
70✔
1597
                }
70✔
1598

1599
                // If we reach this point, then there are no longer any edges
1600
                // that connect this node, so we can delete it.
1601
                err := c.deleteLightningNode(nodes, nodePubKey[:])
70✔
1602
                if err != nil {
70✔
1603
                        if errors.Is(err, ErrGraphNodeNotFound) ||
×
1604
                                errors.Is(err, ErrGraphNodesNotFound) {
×
1605

×
1606
                                log.Warnf("Unable to prune node %x from the "+
×
1607
                                        "graph: %v", nodePubKey, err)
×
1608
                                continue
×
1609
                        }
1610

1611
                        return err
×
1612
                }
1613

1614
                log.Infof("Pruned unconnected node %x from channel graph",
70✔
1615
                        nodePubKey[:])
70✔
1616

70✔
1617
                numNodesPruned++
70✔
1618
        }
1619

1620
        if numNodesPruned > 0 {
320✔
1621
                log.Infof("Pruned %v unconnected nodes from the channel graph",
54✔
1622
                        numNodesPruned)
54✔
1623
        }
54✔
1624

1625
        return nil
266✔
1626
}
1627

1628
// DisconnectBlockAtHeight is used to indicate that the block specified
1629
// by the passed height has been disconnected from the main chain. This
1630
// will "rewind" the graph back to the height below, deleting channels
1631
// that are no longer confirmed from the graph. The prune log will be
1632
// set to the last prune height valid for the remaining chain.
1633
// Channels that were removed from the graph resulting from the
1634
// disconnected block are returned.
1635
func (c *ChannelGraph) DisconnectBlockAtHeight(height uint32) (
1636
        []*models.ChannelEdgeInfo, error) {
158✔
1637

158✔
1638
        // Every channel having a ShortChannelID starting at 'height'
158✔
1639
        // will no longer be confirmed.
158✔
1640
        startShortChanID := lnwire.ShortChannelID{
158✔
1641
                BlockHeight: height,
158✔
1642
        }
158✔
1643

158✔
1644
        // Delete everything after this height from the db up until the
158✔
1645
        // SCID alias range.
158✔
1646
        endShortChanID := aliasmgr.StartingAlias
158✔
1647

158✔
1648
        // The block height will be the 3 first bytes of the channel IDs.
158✔
1649
        var chanIDStart [8]byte
158✔
1650
        byteOrder.PutUint64(chanIDStart[:], startShortChanID.ToUint64())
158✔
1651
        var chanIDEnd [8]byte
158✔
1652
        byteOrder.PutUint64(chanIDEnd[:], endShortChanID.ToUint64())
158✔
1653

158✔
1654
        c.cacheMu.Lock()
158✔
1655
        defer c.cacheMu.Unlock()
158✔
1656

158✔
1657
        // Keep track of the channels that are removed from the graph.
158✔
1658
        var removedChans []*models.ChannelEdgeInfo
158✔
1659

158✔
1660
        if err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
316✔
1661
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
158✔
1662
                if err != nil {
158✔
1663
                        return err
×
1664
                }
×
1665
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
158✔
1666
                if err != nil {
158✔
1667
                        return err
×
1668
                }
×
1669
                chanIndex, err := edges.CreateBucketIfNotExists(
158✔
1670
                        channelPointBucket,
158✔
1671
                )
158✔
1672
                if err != nil {
158✔
1673
                        return err
×
1674
                }
×
1675
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
158✔
1676
                if err != nil {
158✔
1677
                        return err
×
1678
                }
×
1679

1680
                // Scan from chanIDStart to chanIDEnd, deleting every
1681
                // found edge.
1682
                // NOTE: we must delete the edges after the cursor loop, since
1683
                // modifying the bucket while traversing is not safe.
1684
                // NOTE: We use a < comparison in bytes.Compare instead of <=
1685
                // so that the StartingAlias itself isn't deleted.
1686
                var keys [][]byte
158✔
1687
                cursor := edgeIndex.ReadWriteCursor()
158✔
1688

158✔
1689
                //nolint:ll
158✔
1690
                for k, v := cursor.Seek(chanIDStart[:]); k != nil &&
158✔
1691
                        bytes.Compare(k, chanIDEnd[:]) < 0; k, v = cursor.Next() {
253✔
1692
                        edgeInfoReader := bytes.NewReader(v)
95✔
1693
                        edgeInfo, err := deserializeChanEdgeInfo(edgeInfoReader)
95✔
1694
                        if err != nil {
95✔
1695
                                return err
×
1696
                        }
×
1697

1698
                        keys = append(keys, k)
95✔
1699
                        removedChans = append(removedChans, &edgeInfo)
95✔
1700
                }
1701

1702
                for _, k := range keys {
253✔
1703
                        err = c.delChannelEdgeUnsafe(
95✔
1704
                                edges, edgeIndex, chanIndex, zombieIndex,
95✔
1705
                                k, false, false,
95✔
1706
                        )
95✔
1707
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
95✔
1708
                                return err
×
1709
                        }
×
1710
                }
1711

1712
                // Delete all the entries in the prune log having a height
1713
                // greater or equal to the block disconnected.
1714
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
158✔
1715
                if err != nil {
158✔
1716
                        return err
×
1717
                }
×
1718

1719
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
158✔
1720
                        pruneLogBucket,
158✔
1721
                )
158✔
1722
                if err != nil {
158✔
1723
                        return err
×
1724
                }
×
1725

1726
                var pruneKeyStart [4]byte
158✔
1727
                byteOrder.PutUint32(pruneKeyStart[:], height)
158✔
1728

158✔
1729
                var pruneKeyEnd [4]byte
158✔
1730
                byteOrder.PutUint32(pruneKeyEnd[:], math.MaxUint32)
158✔
1731

158✔
1732
                // To avoid modifying the bucket while traversing, we delete
158✔
1733
                // the keys in a second loop.
158✔
1734
                var pruneKeys [][]byte
158✔
1735
                pruneCursor := pruneBucket.ReadWriteCursor()
158✔
1736
                //nolint:ll
158✔
1737
                for k, _ := pruneCursor.Seek(pruneKeyStart[:]); k != nil &&
158✔
1738
                        bytes.Compare(k, pruneKeyEnd[:]) <= 0; k, _ = pruneCursor.Next() {
256✔
1739
                        pruneKeys = append(pruneKeys, k)
98✔
1740
                }
98✔
1741

1742
                for _, k := range pruneKeys {
256✔
1743
                        if err := pruneBucket.Delete(k); err != nil {
98✔
1744
                                return err
×
1745
                        }
×
1746
                }
1747

1748
                return nil
158✔
1749
        }, func() {
158✔
1750
                removedChans = nil
158✔
1751
        }); err != nil {
158✔
1752
                return nil, err
×
1753
        }
×
1754

1755
        for _, channel := range removedChans {
253✔
1756
                c.rejectCache.remove(channel.ChannelID)
95✔
1757
                c.chanCache.remove(channel.ChannelID)
95✔
1758
        }
95✔
1759

1760
        return removedChans, nil
158✔
1761
}
1762

1763
// PruneTip returns the block height and hash of the latest block that has been
1764
// used to prune channels in the graph. Knowing the "prune tip" allows callers
1765
// to tell if the graph is currently in sync with the current best known UTXO
1766
// state.
1767
func (c *ChannelGraph) PruneTip() (*chainhash.Hash, uint32, error) {
54✔
1768
        var (
54✔
1769
                tipHash   chainhash.Hash
54✔
1770
                tipHeight uint32
54✔
1771
        )
54✔
1772

54✔
1773
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
108✔
1774
                graphMeta := tx.ReadBucket(graphMetaBucket)
54✔
1775
                if graphMeta == nil {
54✔
1776
                        return ErrGraphNotFound
×
1777
                }
×
1778
                pruneBucket := graphMeta.NestedReadBucket(pruneLogBucket)
54✔
1779
                if pruneBucket == nil {
54✔
1780
                        return ErrGraphNeverPruned
×
1781
                }
×
1782

1783
                pruneCursor := pruneBucket.ReadCursor()
54✔
1784

54✔
1785
                // The prune key with the largest block height will be our
54✔
1786
                // prune tip.
54✔
1787
                k, v := pruneCursor.Last()
54✔
1788
                if k == nil {
73✔
1789
                        return ErrGraphNeverPruned
19✔
1790
                }
19✔
1791

1792
                // Once we have the prune tip, the value will be the block hash,
1793
                // and the key the block height.
1794
                copy(tipHash[:], v[:])
36✔
1795
                tipHeight = byteOrder.Uint32(k[:])
36✔
1796

36✔
1797
                return nil
36✔
1798
        }, func() {})
54✔
1799
        if err != nil {
73✔
1800
                return nil, 0, err
19✔
1801
        }
19✔
1802

1803
        return &tipHash, tipHeight, nil
36✔
1804
}
1805

1806
// DeleteChannelEdges removes edges with the given channel IDs from the
1807
// database and marks them as zombies. This ensures that we're unable to re-add
1808
// it to our database once again. If an edge does not exist within the
1809
// database, then ErrEdgeNotFound will be returned. If strictZombiePruning is
1810
// true, then when we mark these edges as zombies, we'll set up the keys such
1811
// that we require the node that failed to send the fresh update to be the one
1812
// that resurrects the channel from its zombie state. The markZombie bool
1813
// denotes whether or not to mark the channel as a zombie.
1814
func (c *ChannelGraph) DeleteChannelEdges(strictZombiePruning, markZombie bool,
1815
        chanIDs ...uint64) error {
136✔
1816

136✔
1817
        // TODO(roasbeef): possibly delete from node bucket if node has no more
136✔
1818
        // channels
136✔
1819
        // TODO(roasbeef): don't delete both edges?
136✔
1820

136✔
1821
        c.cacheMu.Lock()
136✔
1822
        defer c.cacheMu.Unlock()
136✔
1823

136✔
1824
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
272✔
1825
                edges := tx.ReadWriteBucket(edgeBucket)
136✔
1826
                if edges == nil {
136✔
1827
                        return ErrEdgeNotFound
×
1828
                }
×
1829
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
136✔
1830
                if edgeIndex == nil {
136✔
1831
                        return ErrEdgeNotFound
×
1832
                }
×
1833
                chanIndex := edges.NestedReadWriteBucket(channelPointBucket)
136✔
1834
                if chanIndex == nil {
136✔
1835
                        return ErrEdgeNotFound
×
1836
                }
×
1837
                nodes := tx.ReadWriteBucket(nodeBucket)
136✔
1838
                if nodes == nil {
136✔
1839
                        return ErrGraphNodeNotFound
×
1840
                }
×
1841
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
136✔
1842
                if err != nil {
136✔
1843
                        return err
×
1844
                }
×
1845

1846
                var rawChanID [8]byte
136✔
1847
                for _, chanID := range chanIDs {
223✔
1848
                        byteOrder.PutUint64(rawChanID[:], chanID)
87✔
1849
                        err := c.delChannelEdgeUnsafe(
87✔
1850
                                edges, edgeIndex, chanIndex, zombieIndex,
87✔
1851
                                rawChanID[:], markZombie, strictZombiePruning,
87✔
1852
                        )
87✔
1853
                        if err != nil {
146✔
1854
                                return err
59✔
1855
                        }
59✔
1856
                }
1857

1858
                return nil
77✔
1859
        }, func() {})
136✔
1860
        if err != nil {
195✔
1861
                return err
59✔
1862
        }
59✔
1863

1864
        for _, chanID := range chanIDs {
105✔
1865
                c.rejectCache.remove(chanID)
28✔
1866
                c.chanCache.remove(chanID)
28✔
1867
        }
28✔
1868

1869
        return nil
77✔
1870
}
1871

1872
// ChannelID attempt to lookup the 8-byte compact channel ID which maps to the
1873
// passed channel point (outpoint). If the passed channel doesn't exist within
1874
// the database, then ErrEdgeNotFound is returned.
1875
func (c *ChannelGraph) ChannelID(chanPoint *wire.OutPoint) (uint64, error) {
2✔
1876
        var chanID uint64
2✔
1877
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
4✔
1878
                var err error
2✔
1879
                chanID, err = getChanID(tx, chanPoint)
2✔
1880
                return err
2✔
1881
        }, func() {
4✔
1882
                chanID = 0
2✔
1883
        }); err != nil {
3✔
1884
                return 0, err
1✔
1885
        }
1✔
1886

1887
        return chanID, nil
2✔
1888
}
1889

1890
// getChanID returns the assigned channel ID for a given channel point.
1891
func getChanID(tx kvdb.RTx, chanPoint *wire.OutPoint) (uint64, error) {
2✔
1892
        var b bytes.Buffer
2✔
1893
        if err := WriteOutpoint(&b, chanPoint); err != nil {
2✔
1894
                return 0, err
×
1895
        }
×
1896

1897
        edges := tx.ReadBucket(edgeBucket)
2✔
1898
        if edges == nil {
2✔
1899
                return 0, ErrGraphNoEdgesFound
×
1900
        }
×
1901
        chanIndex := edges.NestedReadBucket(channelPointBucket)
2✔
1902
        if chanIndex == nil {
2✔
1903
                return 0, ErrGraphNoEdgesFound
×
1904
        }
×
1905

1906
        chanIDBytes := chanIndex.Get(b.Bytes())
2✔
1907
        if chanIDBytes == nil {
3✔
1908
                return 0, ErrEdgeNotFound
1✔
1909
        }
1✔
1910

1911
        chanID := byteOrder.Uint64(chanIDBytes)
2✔
1912

2✔
1913
        return chanID, nil
2✔
1914
}
1915

1916
// TODO(roasbeef): allow updates to use Batch?
1917

1918
// HighestChanID returns the "highest" known channel ID in the channel graph.
1919
// This represents the "newest" channel from the PoV of the chain. This method
1920
// can be used by peers to quickly determine if they're graphs are in sync.
1921
func (c *ChannelGraph) HighestChanID() (uint64, error) {
4✔
1922
        var cid uint64
4✔
1923

4✔
1924
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
8✔
1925
                edges := tx.ReadBucket(edgeBucket)
4✔
1926
                if edges == nil {
4✔
1927
                        return ErrGraphNoEdgesFound
×
1928
                }
×
1929
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
4✔
1930
                if edgeIndex == nil {
4✔
1931
                        return ErrGraphNoEdgesFound
×
1932
                }
×
1933

1934
                // In order to find the highest chan ID, we'll fetch a cursor
1935
                // and use that to seek to the "end" of our known rage.
1936
                cidCursor := edgeIndex.ReadCursor()
4✔
1937

4✔
1938
                lastChanID, _ := cidCursor.Last()
4✔
1939

4✔
1940
                // If there's no key, then this means that we don't actually
4✔
1941
                // know of any channels, so we'll return a predicable error.
4✔
1942
                if lastChanID == nil {
6✔
1943
                        return ErrGraphNoEdgesFound
2✔
1944
                }
2✔
1945

1946
                // Otherwise, we'll de serialize the channel ID and return it
1947
                // to the caller.
1948
                cid = byteOrder.Uint64(lastChanID)
3✔
1949
                return nil
3✔
1950
        }, func() {
4✔
1951
                cid = 0
4✔
1952
        })
4✔
1953
        if err != nil && err != ErrGraphNoEdgesFound {
4✔
1954
                return 0, err
×
1955
        }
×
1956

1957
        return cid, nil
4✔
1958
}
1959

1960
// ChannelEdge represents the complete set of information for a channel edge in
1961
// the known channel graph. This struct couples the core information of the
1962
// edge as well as each of the known advertised edge policies.
1963
type ChannelEdge struct {
1964
        // Info contains all the static information describing the channel.
1965
        Info *models.ChannelEdgeInfo
1966

1967
        // Policy1 points to the "first" edge policy of the channel containing
1968
        // the dynamic information required to properly route through the edge.
1969
        Policy1 *models.ChannelEdgePolicy
1970

1971
        // Policy2 points to the "second" edge policy of the channel containing
1972
        // the dynamic information required to properly route through the edge.
1973
        Policy2 *models.ChannelEdgePolicy
1974

1975
        // Node1 is "node 1" in the channel. This is the node that would have
1976
        // produced Policy1 if it exists.
1977
        Node1 *models.LightningNode
1978

1979
        // Node2 is "node 2" in the channel. This is the node that would have
1980
        // produced Policy2 if it exists.
1981
        Node2 *models.LightningNode
1982
}
1983

1984
// ChanUpdatesInHorizon returns all the known channel edges which have at least
1985
// one edge that has an update timestamp within the specified horizon.
1986
func (c *ChannelGraph) ChanUpdatesInHorizon(startTime,
1987
        endTime time.Time) ([]ChannelEdge, error) {
134✔
1988

134✔
1989
        // To ensure we don't return duplicate ChannelEdges, we'll use an
134✔
1990
        // additional map to keep track of the edges already seen to prevent
134✔
1991
        // re-adding it.
134✔
1992
        var edgesSeen map[uint64]struct{}
134✔
1993
        var edgesToCache map[uint64]ChannelEdge
134✔
1994
        var edgesInHorizon []ChannelEdge
134✔
1995

134✔
1996
        c.cacheMu.Lock()
134✔
1997
        defer c.cacheMu.Unlock()
134✔
1998

134✔
1999
        var hits int
134✔
2000
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
268✔
2001
                edges := tx.ReadBucket(edgeBucket)
134✔
2002
                if edges == nil {
134✔
2003
                        return ErrGraphNoEdgesFound
×
2004
                }
×
2005
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
134✔
2006
                if edgeIndex == nil {
134✔
2007
                        return ErrGraphNoEdgesFound
×
2008
                }
×
2009
                edgeUpdateIndex := edges.NestedReadBucket(edgeUpdateIndexBucket)
134✔
2010
                if edgeUpdateIndex == nil {
134✔
2011
                        return ErrGraphNoEdgesFound
×
2012
                }
×
2013

2014
                nodes := tx.ReadBucket(nodeBucket)
134✔
2015
                if nodes == nil {
134✔
2016
                        return ErrGraphNodesNotFound
×
2017
                }
×
2018

2019
                // We'll now obtain a cursor to perform a range query within
2020
                // the index to find all channels within the horizon.
2021
                updateCursor := edgeUpdateIndex.ReadCursor()
134✔
2022

134✔
2023
                var startTimeBytes, endTimeBytes [8 + 8]byte
134✔
2024
                byteOrder.PutUint64(
134✔
2025
                        startTimeBytes[:8], uint64(startTime.Unix()),
134✔
2026
                )
134✔
2027
                byteOrder.PutUint64(
134✔
2028
                        endTimeBytes[:8], uint64(endTime.Unix()),
134✔
2029
                )
134✔
2030

134✔
2031
                // With our start and end times constructed, we'll step through
134✔
2032
                // the index collecting the info and policy of each update of
134✔
2033
                // each channel that has a last update within the time range.
134✔
2034
                //
134✔
2035
                //nolint:ll
134✔
2036
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
134✔
2037
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
181✔
2038

47✔
2039
                        // We have a new eligible entry, so we'll slice of the
47✔
2040
                        // chan ID so we can query it in the DB.
47✔
2041
                        chanID := indexKey[8:]
47✔
2042

47✔
2043
                        // If we've already retrieved the info and policies for
47✔
2044
                        // this edge, then we can skip it as we don't need to do
47✔
2045
                        // so again.
47✔
2046
                        chanIDInt := byteOrder.Uint64(chanID)
47✔
2047
                        if _, ok := edgesSeen[chanIDInt]; ok {
66✔
2048
                                continue
19✔
2049
                        }
2050

2051
                        if channel, ok := c.chanCache.get(chanIDInt); ok {
37✔
2052
                                hits++
9✔
2053
                                edgesSeen[chanIDInt] = struct{}{}
9✔
2054
                                edgesInHorizon = append(edgesInHorizon, channel)
9✔
2055
                                continue
9✔
2056
                        }
2057

2058
                        // First, we'll fetch the static edge information.
2059
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
19✔
2060
                        if err != nil {
19✔
2061
                                chanID := byteOrder.Uint64(chanID)
×
2062
                                return fmt.Errorf("unable to fetch info for "+
×
2063
                                        "edge with chan_id=%v: %v", chanID, err)
×
2064
                        }
×
2065

2066
                        // With the static information obtained, we'll now
2067
                        // fetch the dynamic policy info.
2068
                        edge1, edge2, err := fetchChanEdgePolicies(
19✔
2069
                                edgeIndex, edges, chanID,
19✔
2070
                        )
19✔
2071
                        if err != nil {
19✔
2072
                                chanID := byteOrder.Uint64(chanID)
×
2073
                                return fmt.Errorf("unable to fetch policies "+
×
2074
                                        "for edge with chan_id=%v: %v", chanID,
×
2075
                                        err)
×
2076
                        }
×
2077

2078
                        node1, err := fetchLightningNode(
19✔
2079
                                nodes, edgeInfo.NodeKey1Bytes[:],
19✔
2080
                        )
19✔
2081
                        if err != nil {
19✔
2082
                                return err
×
2083
                        }
×
2084

2085
                        node2, err := fetchLightningNode(
19✔
2086
                                nodes, edgeInfo.NodeKey2Bytes[:],
19✔
2087
                        )
19✔
2088
                        if err != nil {
19✔
2089
                                return err
×
2090
                        }
×
2091

2092
                        // Finally, we'll collate this edge with the rest of
2093
                        // edges to be returned.
2094
                        edgesSeen[chanIDInt] = struct{}{}
19✔
2095
                        channel := ChannelEdge{
19✔
2096
                                Info:    &edgeInfo,
19✔
2097
                                Policy1: edge1,
19✔
2098
                                Policy2: edge2,
19✔
2099
                                Node1:   &node1,
19✔
2100
                                Node2:   &node2,
19✔
2101
                        }
19✔
2102
                        edgesInHorizon = append(edgesInHorizon, channel)
19✔
2103
                        edgesToCache[chanIDInt] = channel
19✔
2104
                }
2105

2106
                return nil
134✔
2107
        }, func() {
134✔
2108
                edgesSeen = make(map[uint64]struct{})
134✔
2109
                edgesToCache = make(map[uint64]ChannelEdge)
134✔
2110
                edgesInHorizon = nil
134✔
2111
        })
134✔
2112
        switch {
134✔
2113
        case err == ErrGraphNoEdgesFound:
×
2114
                fallthrough
×
2115
        case err == ErrGraphNodesNotFound:
×
2116
                break
×
2117

2118
        case err != nil:
×
2119
                return nil, err
×
2120
        }
2121

2122
        // Insert any edges loaded from disk into the cache.
2123
        for chanid, channel := range edgesToCache {
153✔
2124
                c.chanCache.insert(chanid, channel)
19✔
2125
        }
19✔
2126

2127
        log.Debugf("ChanUpdatesInHorizon hit percentage: %f (%d/%d)",
134✔
2128
                float64(hits)/float64(len(edgesInHorizon)), hits,
134✔
2129
                len(edgesInHorizon))
134✔
2130

134✔
2131
        return edgesInHorizon, nil
134✔
2132
}
2133

2134
// NodeUpdatesInHorizon returns all the known lightning node which have an
2135
// update timestamp within the passed range. This method can be used by two
2136
// nodes to quickly determine if they have the same set of up to date node
2137
// announcements.
2138
func (c *ChannelGraph) NodeUpdatesInHorizon(startTime,
2139
        endTime time.Time) ([]models.LightningNode, error) {
9✔
2140

9✔
2141
        var nodesInHorizon []models.LightningNode
9✔
2142

9✔
2143
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
18✔
2144
                nodes := tx.ReadBucket(nodeBucket)
9✔
2145
                if nodes == nil {
9✔
2146
                        return ErrGraphNodesNotFound
×
2147
                }
×
2148

2149
                nodeUpdateIndex := nodes.NestedReadBucket(nodeUpdateIndexBucket)
9✔
2150
                if nodeUpdateIndex == nil {
9✔
2151
                        return ErrGraphNodesNotFound
×
2152
                }
×
2153

2154
                // We'll now obtain a cursor to perform a range query within
2155
                // the index to find all node announcements within the horizon.
2156
                updateCursor := nodeUpdateIndex.ReadCursor()
9✔
2157

9✔
2158
                var startTimeBytes, endTimeBytes [8 + 33]byte
9✔
2159
                byteOrder.PutUint64(
9✔
2160
                        startTimeBytes[:8], uint64(startTime.Unix()),
9✔
2161
                )
9✔
2162
                byteOrder.PutUint64(
9✔
2163
                        endTimeBytes[:8], uint64(endTime.Unix()),
9✔
2164
                )
9✔
2165

9✔
2166
                // With our start and end times constructed, we'll step through
9✔
2167
                // the index collecting info for each node within the time
9✔
2168
                // range.
9✔
2169
                //
9✔
2170
                //nolint:ll
9✔
2171
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
9✔
2172
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
39✔
2173

30✔
2174
                        nodePub := indexKey[8:]
30✔
2175
                        node, err := fetchLightningNode(nodes, nodePub)
30✔
2176
                        if err != nil {
30✔
2177
                                return err
×
2178
                        }
×
2179

2180
                        nodesInHorizon = append(nodesInHorizon, node)
30✔
2181
                }
2182

2183
                return nil
9✔
2184
        }, func() {
9✔
2185
                nodesInHorizon = nil
9✔
2186
        })
9✔
2187
        switch {
9✔
2188
        case err == ErrGraphNoEdgesFound:
×
2189
                fallthrough
×
2190
        case err == ErrGraphNodesNotFound:
×
2191
                break
×
2192

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

2197
        return nodesInHorizon, nil
9✔
2198
}
2199

2200
// FilterKnownChanIDs takes a set of channel IDs and return the subset of chan
2201
// ID's that we don't know and are not known zombies of the passed set. In other
2202
// words, we perform a set difference of our set of chan ID's and the ones
2203
// passed in. This method can be used by callers to determine the set of
2204
// channels another peer knows of that we don't.
2205
func (c *ChannelGraph) FilterKnownChanIDs(chansInfo []ChannelUpdateInfo,
2206
        isZombieChan func(time.Time, time.Time) bool) ([]uint64, error) {
125✔
2207

125✔
2208
        var newChanIDs []uint64
125✔
2209

125✔
2210
        c.cacheMu.Lock()
125✔
2211
        defer c.cacheMu.Unlock()
125✔
2212

125✔
2213
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
250✔
2214
                edges := tx.ReadBucket(edgeBucket)
125✔
2215
                if edges == nil {
125✔
2216
                        return ErrGraphNoEdgesFound
×
2217
                }
×
2218
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
125✔
2219
                if edgeIndex == nil {
125✔
2220
                        return ErrGraphNoEdgesFound
×
2221
                }
×
2222

2223
                // Fetch the zombie index, it may not exist if no edges have
2224
                // ever been marked as zombies. If the index has been
2225
                // initialized, we will use it later to skip known zombie edges.
2226
                zombieIndex := edges.NestedReadBucket(zombieBucket)
125✔
2227

125✔
2228
                // We'll run through the set of chanIDs and collate only the
125✔
2229
                // set of channel that are unable to be found within our db.
125✔
2230
                var cidBytes [8]byte
125✔
2231
                for _, info := range chansInfo {
262✔
2232
                        scid := info.ShortChannelID.ToUint64()
137✔
2233
                        byteOrder.PutUint64(cidBytes[:], scid)
137✔
2234

137✔
2235
                        // If the edge is already known, skip it.
137✔
2236
                        if v := edgeIndex.Get(cidBytes[:]); v != nil {
155✔
2237
                                continue
18✔
2238
                        }
2239

2240
                        // If the edge is a known zombie, skip it.
2241
                        if zombieIndex != nil {
240✔
2242
                                isZombie, _, _ := isZombieEdge(
120✔
2243
                                        zombieIndex, scid,
120✔
2244
                                )
120✔
2245

120✔
2246
                                // TODO(ziggie): Make sure that for the strict
120✔
2247
                                // pruning case we compare the pubkeys and
120✔
2248
                                // whether the right timestamp is not older than
120✔
2249
                                // the `ChannelPruneExpiry`.
120✔
2250
                                //
120✔
2251
                                // NOTE: The timestamp data has no verification
120✔
2252
                                // attached to it in the `ReplyChannelRange` msg
120✔
2253
                                // so we are trusting this data at this point.
120✔
2254
                                // However it is not critical because we are
120✔
2255
                                // just removing the channel from the db when
120✔
2256
                                // the timestamps are more recent. During the
120✔
2257
                                // querying of the gossip msg verification
120✔
2258
                                // happens as usual.
120✔
2259
                                // However we should start punishing peers when
120✔
2260
                                // they don't provide us honest data ?
120✔
2261
                                isStillZombie := isZombieChan(
120✔
2262
                                        info.Node1UpdateTimestamp,
120✔
2263
                                        info.Node2UpdateTimestamp,
120✔
2264
                                )
120✔
2265

120✔
2266
                                switch {
120✔
2267
                                // If the edge is a known zombie and if we
2268
                                // would still consider it a zombie given the
2269
                                // latest update timestamps, then we skip this
2270
                                // channel.
2271
                                case isZombie && isStillZombie:
30✔
2272
                                        continue
30✔
2273

2274
                                // Otherwise, if we have marked it as a zombie
2275
                                // but the latest update timestamps could bring
2276
                                // it back from the dead, then we mark it alive,
2277
                                // and we let it be added to the set of IDs to
2278
                                // query our peer for.
2279
                                case isZombie && !isStillZombie:
26✔
2280
                                        err := c.markEdgeLiveUnsafe(tx, scid)
26✔
2281
                                        if err != nil {
26✔
2282
                                                return err
×
2283
                                        }
×
2284
                                }
2285
                        }
2286

2287
                        newChanIDs = append(newChanIDs, scid)
90✔
2288
                }
2289

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

2303
                return ogChanIDs, nil
×
2304

2305
        case err != nil:
×
2306
                return nil, err
×
2307
        }
2308

2309
        return newChanIDs, nil
125✔
2310
}
2311

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

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

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

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

219✔
2335
        chanInfo := ChannelUpdateInfo{
219✔
2336
                ShortChannelID:       scid,
219✔
2337
                Node1UpdateTimestamp: node1Timestamp,
219✔
2338
                Node2UpdateTimestamp: node2Timestamp,
219✔
2339
        }
219✔
2340

219✔
2341
        if node1Timestamp.IsZero() {
428✔
2342
                chanInfo.Node1UpdateTimestamp = time.Unix(0, 0)
209✔
2343
        }
209✔
2344

2345
        if node2Timestamp.IsZero() {
428✔
2346
                chanInfo.Node2UpdateTimestamp = time.Unix(0, 0)
209✔
2347
        }
209✔
2348

2349
        return chanInfo
219✔
2350
}
2351

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

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

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

12✔
2375
        startChanID := &lnwire.ShortChannelID{
12✔
2376
                BlockHeight: startHeight,
12✔
2377
        }
12✔
2378

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

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

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

2403
                cursor := edgeIndex.ReadCursor()
12✔
2404

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

2418
                        if edgeInfo.AuthProof == nil {
46✔
2419
                                continue
1✔
2420
                        }
2421

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

45✔
2427
                        chanInfo := NewChannelUpdateInfo(
45✔
2428
                                cid, time.Time{}, time.Time{},
45✔
2429
                        )
45✔
2430

45✔
2431
                        if !withTimestamps {
67✔
2432
                                channelsPerBlock[cid.BlockHeight] = append(
22✔
2433
                                        channelsPerBlock[cid.BlockHeight],
22✔
2434
                                        chanInfo,
22✔
2435
                                )
22✔
2436

22✔
2437
                                continue
22✔
2438
                        }
2439

2440
                        node1Key, node2Key := computeEdgePolicyKeys(&edgeInfo)
23✔
2441

23✔
2442
                        rawPolicy := edges.Get(node1Key)
23✔
2443
                        if len(rawPolicy) != 0 {
30✔
2444
                                r := bytes.NewReader(rawPolicy)
7✔
2445

7✔
2446
                                edge, err := deserializeChanEdgePolicyRaw(r)
7✔
2447
                                if err != nil && !errors.Is(
7✔
2448
                                        err, ErrEdgePolicyOptionalFieldNotFound,
7✔
2449
                                ) {
7✔
2450

×
2451
                                        return err
×
2452
                                }
×
2453

2454
                                chanInfo.Node1UpdateTimestamp = edge.LastUpdate
7✔
2455
                        }
2456

2457
                        rawPolicy = edges.Get(node2Key)
23✔
2458
                        if len(rawPolicy) != 0 {
35✔
2459
                                r := bytes.NewReader(rawPolicy)
12✔
2460

12✔
2461
                                edge, err := deserializeChanEdgePolicyRaw(r)
12✔
2462
                                if err != nil && !errors.Is(
12✔
2463
                                        err, ErrEdgePolicyOptionalFieldNotFound,
12✔
2464
                                ) {
12✔
2465

×
2466
                                        return err
×
2467
                                }
×
2468

2469
                                chanInfo.Node2UpdateTimestamp = edge.LastUpdate
12✔
2470
                        }
2471

2472
                        channelsPerBlock[cid.BlockHeight] = append(
23✔
2473
                                channelsPerBlock[cid.BlockHeight], chanInfo,
23✔
2474
                        )
23✔
2475
                }
2476

2477
                return nil
12✔
2478
        }, func() {
12✔
2479
                channelsPerBlock = make(map[uint32][]ChannelUpdateInfo)
12✔
2480
        })
12✔
2481

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

2488
        case err != nil:
×
2489
                return nil, err
×
2490
        }
2491

2492
        // Return the channel ranges in ascending block height order.
2493
        blocks := make([]uint32, 0, len(channelsPerBlock))
9✔
2494
        for block := range channelsPerBlock {
32✔
2495
                blocks = append(blocks, block)
23✔
2496
        }
23✔
2497
        sort.Slice(blocks, func(i, j int) bool {
35✔
2498
                return blocks[i] < blocks[j]
26✔
2499
        })
26✔
2500

2501
        channelRanges := make([]BlockChannelRange, 0, len(channelsPerBlock))
9✔
2502
        for _, block := range blocks {
32✔
2503
                channelRanges = append(channelRanges, BlockChannelRange{
23✔
2504
                        Height:   block,
23✔
2505
                        Channels: channelsPerBlock[block],
23✔
2506
                })
23✔
2507
        }
23✔
2508

2509
        return channelRanges, nil
9✔
2510
}
2511

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

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

31✔
2533
        var (
31✔
2534
                chanEdges []ChannelEdge
31✔
2535
                cidBytes  [8]byte
31✔
2536
        )
31✔
2537

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

2552
                for _, cid := range chanIDs {
69✔
2553
                        byteOrder.PutUint64(cidBytes[:], cid)
38✔
2554

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

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

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

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

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

2602
        if tx == nil {
36✔
2603
                err := kvdb.View(c.db, fetchChanInfos, func() {
10✔
2604
                        chanEdges = nil
5✔
2605
                })
5✔
2606
                if err != nil {
5✔
2607
                        return nil, err
×
2608
                }
×
2609

2610
                return chanEdges, nil
5✔
2611
        }
2612

2613
        err := fetchChanInfos(tx)
26✔
2614
        if err != nil {
26✔
2615
                return nil, err
×
2616
        }
×
2617

2618
        return chanEdges, nil
26✔
2619
}
2620

2621
func delEdgeUpdateIndexEntry(edgesBucket kvdb.RwBucket, chanID uint64,
2622
        edge1, edge2 *models.ChannelEdgePolicy) error {
148✔
2623

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

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

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

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

2660
        return nil
148✔
2661
}
2662

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

207✔
2674
        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
207✔
2675
        if err != nil {
266✔
2676
                return err
59✔
2677
        }
59✔
2678

2679
        if c.graphCache != nil {
296✔
2680
                c.graphCache.RemoveChannel(
148✔
2681
                        edgeInfo.NodeKey1Bytes, edgeInfo.NodeKey2Bytes,
148✔
2682
                        edgeInfo.ChannelID,
148✔
2683
                )
148✔
2684
        }
148✔
2685

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

2699
        // The edge key is of the format pubKey || chanID. First we construct
2700
        // the latter half, populating the channel ID.
2701
        var edgeKey [33 + 8]byte
148✔
2702
        copy(edgeKey[33:], chanID)
148✔
2703

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

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

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

2745
        // Finally, we'll mark the edge as a zombie within our index if it's
2746
        // being removed due to the channel becoming a zombie. We do this to
2747
        // ensure we don't store unnecessary data for spent channels.
2748
        if !isZombie {
273✔
2749
                return nil
125✔
2750
        }
125✔
2751

2752
        nodeKey1, nodeKey2 := edgeInfo.NodeKey1Bytes, edgeInfo.NodeKey2Bytes
24✔
2753
        if strictZombie {
27✔
2754
                nodeKey1, nodeKey2 = makeZombiePubkeys(&edgeInfo, edge1, edge2)
3✔
2755
        }
3✔
2756

2757
        return markEdgeZombie(
24✔
2758
                zombieIndex, byteOrder.Uint64(chanID), nodeKey1, nodeKey2,
24✔
2759
        )
24✔
2760
}
2761

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

3✔
2781
        switch {
3✔
2782
        // If we don't have either edge policy, we'll return both pubkeys so
2783
        // that the channel can be resurrected by either party.
UNCOV
2784
        case e1 == nil && e2 == nil:
×
UNCOV
2785
                return info.NodeKey1Bytes, info.NodeKey2Bytes
×
2786

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

2794
        // Otherwise, we're missing edge2 or edge2 is the older side, so we
2795
        // return a blank pubkey for edge1. In this case, only an update from
2796
        // edge2 can resurect the channel.
2797
        default:
2✔
2798
                return [33]byte{}, info.NodeKey2Bytes
2✔
2799
        }
2800
}
2801

2802
// UpdateEdgePolicy updates the edge routing policy for a single directed edge
2803
// within the database for the referenced channel. The `flags` attribute within
2804
// the ChannelEdgePolicy determines which of the directed edges are being
2805
// updated. If the flag is 1, then the first node's information is being
2806
// updated, otherwise it's the second node's information. The node ordering is
2807
// determined by the lexicographical ordering of the identity public keys of the
2808
// nodes on either side of the channel.
2809
func (c *ChannelGraph) UpdateEdgePolicy(edge *models.ChannelEdgePolicy,
2810
        op ...batch.SchedulerOption) error {
2,664✔
2811

2,664✔
2812
        var (
2,664✔
2813
                isUpdate1    bool
2,664✔
2814
                edgeNotFound bool
2,664✔
2815
        )
2,664✔
2816

2,664✔
2817
        r := &batch.Request{
2,664✔
2818
                Reset: func() {
5,328✔
2819
                        isUpdate1 = false
2,664✔
2820
                        edgeNotFound = false
2,664✔
2821
                },
2,664✔
2822
                Update: func(tx kvdb.RwTx) error {
2,664✔
2823
                        var err error
2,664✔
2824
                        isUpdate1, err = updateEdgePolicy(
2,664✔
2825
                                tx, edge, c.graphCache,
2,664✔
2826
                        )
2,664✔
2827

2,664✔
2828
                        if err != nil {
2,667✔
2829
                                log.Errorf("UpdateEdgePolicy faild: %v", err)
3✔
2830
                        }
3✔
2831

2832
                        // Silence ErrEdgeNotFound so that the batch can
2833
                        // succeed, but propagate the error via local state.
2834
                        if errors.Is(err, ErrEdgeNotFound) {
2,667✔
2835
                                edgeNotFound = true
3✔
2836
                                return nil
3✔
2837
                        }
3✔
2838

2839
                        return err
2,661✔
2840
                },
2841
                OnCommit: func(err error) error {
2,664✔
2842
                        switch {
2,664✔
2843
                        case err != nil:
×
2844
                                return err
×
2845
                        case edgeNotFound:
3✔
2846
                                return ErrEdgeNotFound
3✔
2847
                        default:
2,661✔
2848
                                c.updateEdgeCache(edge, isUpdate1)
2,661✔
2849
                                return nil
2,661✔
2850
                        }
2851
                },
2852
        }
2853

2854
        for _, f := range op {
2,665✔
2855
                f(r)
1✔
2856
        }
1✔
2857

2858
        return c.chanScheduler.Execute(r)
2,664✔
2859
}
2860

2861
func (c *ChannelGraph) updateEdgeCache(e *models.ChannelEdgePolicy,
2862
        isUpdate1 bool) {
2,661✔
2863

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

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

2891
// updateEdgePolicy attempts to update an edge's policy within the relevant
2892
// buckets using an existing database transaction. The returned boolean will be
2893
// true if the updated policy belongs to node1, and false if the policy belonged
2894
// to node2.
2895
func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy,
2896
        graphCache *GraphCache) (bool, error) {
2,664✔
2897

2,664✔
2898
        edges := tx.ReadWriteBucket(edgeBucket)
2,664✔
2899
        if edges == nil {
2,664✔
2900
                return false, ErrEdgeNotFound
×
2901
        }
×
2902
        edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
2,664✔
2903
        if edgeIndex == nil {
2,664✔
2904
                return false, ErrEdgeNotFound
×
2905
        }
×
2906

2907
        // Create the channelID key be converting the channel ID
2908
        // integer into a byte slice.
2909
        var chanID [8]byte
2,664✔
2910
        byteOrder.PutUint64(chanID[:], edge.ChannelID)
2,664✔
2911

2,664✔
2912
        // With the channel ID, we then fetch the value storing the two
2,664✔
2913
        // nodes which connect this channel edge.
2,664✔
2914
        nodeInfo := edgeIndex.Get(chanID[:])
2,664✔
2915
        if nodeInfo == nil {
2,667✔
2916
                return false, ErrEdgeNotFound
3✔
2917
        }
3✔
2918

2919
        // Depending on the flags value passed above, either the first
2920
        // or second edge policy is being updated.
2921
        var fromNode, toNode []byte
2,661✔
2922
        var isUpdate1 bool
2,661✔
2923
        if edge.ChannelFlags&lnwire.ChanUpdateDirection == 0 {
3,996✔
2924
                fromNode = nodeInfo[:33]
1,335✔
2925
                toNode = nodeInfo[33:66]
1,335✔
2926
                isUpdate1 = true
1,335✔
2927
        } else {
2,662✔
2928
                fromNode = nodeInfo[33:66]
1,327✔
2929
                toNode = nodeInfo[:33]
1,327✔
2930
                isUpdate1 = false
1,327✔
2931
        }
1,327✔
2932

2933
        // Finally, with the direction of the edge being updated
2934
        // identified, we update the on-disk edge representation.
2935
        err := putChanEdgePolicy(edges, edge, fromNode, toNode)
2,661✔
2936
        if err != nil {
2,661✔
2937
                return false, err
×
2938
        }
×
2939

2940
        var (
2,661✔
2941
                fromNodePubKey route.Vertex
2,661✔
2942
                toNodePubKey   route.Vertex
2,661✔
2943
        )
2,661✔
2944
        copy(fromNodePubKey[:], fromNode)
2,661✔
2945
        copy(toNodePubKey[:], toNode)
2,661✔
2946

2,661✔
2947
        if graphCache != nil {
4,936✔
2948
                graphCache.UpdatePolicy(
2,275✔
2949
                        edge, fromNodePubKey, toNodePubKey, isUpdate1,
2,275✔
2950
                )
2,275✔
2951
        }
2,275✔
2952

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

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

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

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

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

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

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

2997
        return nodeIsPublic, nil
14✔
2998
}
2999

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

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

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

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

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

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

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

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

3050
                node = &n
3,769✔
3051

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

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

3065
                return node, nil
153✔
3066
        }
3067

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3197
                return nil
1,258✔
3198
        }
3199

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

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

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

7✔
3223
        return nodeTraversal(nil, nodePub[:], c.db, cb)
7✔
3224
}
7✔
3225

3226
// ForEachNodeChannelTx iterates through all channels of the given node,
3227
// executing the passed callback with an edge info structure and the policies
3228
// of each end of the channel. The first edge policy is the outgoing edge *to*
3229
// the connecting node, while the second is the incoming edge *from* the
3230
// connecting node. If the callback returns an error, then the iteration is
3231
// halted with the error propagated back up to the caller.
3232
//
3233
// Unknown policies are passed into the callback as nil values.
3234
//
3235
// If the caller wishes to re-use an existing boltdb transaction, then it
3236
// should be passed as the first argument.  Otherwise, the first argument should
3237
// be nil and a fresh transaction will be created to execute the graph
3238
// traversal.
3239
func (c *ChannelGraph) ForEachNodeChannelTx(tx kvdb.RTx,
3240
        nodePub route.Vertex, cb func(kvdb.RTx, *models.ChannelEdgeInfo,
3241
                *models.ChannelEdgePolicy,
3242
                *models.ChannelEdgePolicy) error) error {
1,019✔
3243

1,019✔
3244
        return nodeTraversal(tx, nodePub[:], c.db, cb)
1,019✔
3245
}
1,019✔
3246

3247
// FetchOtherNode attempts to fetch the full LightningNode that's opposite of
3248
// the target node in the channel. This is useful when one knows the pubkey of
3249
// one of the nodes, and wishes to obtain the full LightningNode for the other
3250
// end of the channel.
3251
func (c *ChannelGraph) FetchOtherNode(tx kvdb.RTx,
3252
        channel *models.ChannelEdgeInfo, thisNodeKey []byte) (
3253
        *models.LightningNode, error) {
1✔
3254

1✔
3255
        // Ensure that the node passed in is actually a member of the channel.
1✔
3256
        var targetNodeBytes [33]byte
1✔
3257
        switch {
1✔
3258
        case bytes.Equal(channel.NodeKey1Bytes[:], thisNodeKey):
1✔
3259
                targetNodeBytes = channel.NodeKey2Bytes
1✔
3260
        case bytes.Equal(channel.NodeKey2Bytes[:], thisNodeKey):
1✔
3261
                targetNodeBytes = channel.NodeKey1Bytes
1✔
3262
        default:
×
3263
                return nil, fmt.Errorf("node not participating in this channel")
×
3264
        }
3265

3266
        var targetNode *models.LightningNode
1✔
3267
        fetchNodeFunc := func(tx kvdb.RTx) error {
2✔
3268
                // First grab the nodes bucket which stores the mapping from
1✔
3269
                // pubKey to node information.
1✔
3270
                nodes := tx.ReadBucket(nodeBucket)
1✔
3271
                if nodes == nil {
1✔
3272
                        return ErrGraphNotFound
×
3273
                }
×
3274

3275
                node, err := fetchLightningNode(nodes, targetNodeBytes[:])
1✔
3276
                if err != nil {
1✔
3277
                        return err
×
3278
                }
×
3279

3280
                targetNode = &node
1✔
3281

1✔
3282
                return nil
1✔
3283
        }
3284

3285
        // If the transaction is nil, then we'll need to create a new one,
3286
        // otherwise we can use the existing db transaction.
3287
        var err error
1✔
3288
        if tx == nil {
1✔
3289
                err = kvdb.View(c.db, fetchNodeFunc, func() {
×
3290
                        targetNode = nil
×
3291
                })
×
3292
        } else {
1✔
3293
                err = fetchNodeFunc(tx)
1✔
3294
        }
1✔
3295

3296
        return targetNode, err
1✔
3297
}
3298

3299
// computeEdgePolicyKeys is a helper function that can be used to compute the
3300
// keys used to index the channel edge policy info for the two nodes of the
3301
// edge. The keys for node 1 and node 2 are returned respectively.
3302
func computeEdgePolicyKeys(info *models.ChannelEdgeInfo) ([]byte, []byte) {
23✔
3303
        var (
23✔
3304
                node1Key [33 + 8]byte
23✔
3305
                node2Key [33 + 8]byte
23✔
3306
        )
23✔
3307

23✔
3308
        copy(node1Key[:], info.NodeKey1Bytes[:])
23✔
3309
        copy(node2Key[:], info.NodeKey2Bytes[:])
23✔
3310

23✔
3311
        byteOrder.PutUint64(node1Key[33:], info.ChannelID)
23✔
3312
        byteOrder.PutUint64(node2Key[33:], info.ChannelID)
23✔
3313

23✔
3314
        return node1Key[:], node2Key[:]
23✔
3315
}
23✔
3316

3317
// FetchChannelEdgesByOutpoint attempts to lookup the two directed edges for
3318
// the channel identified by the funding outpoint. If the channel can't be
3319
// found, then ErrEdgeNotFound is returned. A struct which houses the general
3320
// information for the channel itself is returned as well as two structs that
3321
// contain the routing policies for the channel in either direction.
3322
func (c *ChannelGraph) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
3323
        *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3324
        *models.ChannelEdgePolicy, error) {
12✔
3325

12✔
3326
        var (
12✔
3327
                edgeInfo *models.ChannelEdgeInfo
12✔
3328
                policy1  *models.ChannelEdgePolicy
12✔
3329
                policy2  *models.ChannelEdgePolicy
12✔
3330
        )
12✔
3331

12✔
3332
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
24✔
3333
                // First, grab the node bucket. This will be used to populate
12✔
3334
                // the Node pointers in each edge read from disk.
12✔
3335
                nodes := tx.ReadBucket(nodeBucket)
12✔
3336
                if nodes == nil {
12✔
3337
                        return ErrGraphNotFound
×
3338
                }
×
3339

3340
                // Next, grab the edge bucket which stores the edges, and also
3341
                // the index itself so we can group the directed edges together
3342
                // logically.
3343
                edges := tx.ReadBucket(edgeBucket)
12✔
3344
                if edges == nil {
12✔
3345
                        return ErrGraphNoEdgesFound
×
3346
                }
×
3347
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
12✔
3348
                if edgeIndex == nil {
12✔
3349
                        return ErrGraphNoEdgesFound
×
3350
                }
×
3351

3352
                // If the channel's outpoint doesn't exist within the outpoint
3353
                // index, then the edge does not exist.
3354
                chanIndex := edges.NestedReadBucket(channelPointBucket)
12✔
3355
                if chanIndex == nil {
12✔
3356
                        return ErrGraphNoEdgesFound
×
3357
                }
×
3358
                var b bytes.Buffer
12✔
3359
                if err := WriteOutpoint(&b, op); err != nil {
12✔
3360
                        return err
×
3361
                }
×
3362
                chanID := chanIndex.Get(b.Bytes())
12✔
3363
                if chanID == nil {
23✔
3364
                        return fmt.Errorf("%w: op=%v", ErrEdgeNotFound, op)
11✔
3365
                }
11✔
3366

3367
                // If the channel is found to exists, then we'll first retrieve
3368
                // the general information for the channel.
3369
                edge, err := fetchChanEdgeInfo(edgeIndex, chanID)
2✔
3370
                if err != nil {
2✔
3371
                        return fmt.Errorf("%w: chanID=%x", err, chanID)
×
3372
                }
×
3373
                edgeInfo = &edge
2✔
3374

2✔
3375
                // Once we have the information about the channels' parameters,
2✔
3376
                // we'll fetch the routing policies for each for the directed
2✔
3377
                // edges.
2✔
3378
                e1, e2, err := fetchChanEdgePolicies(edgeIndex, edges, chanID)
2✔
3379
                if err != nil {
2✔
3380
                        return fmt.Errorf("failed to find policy: %w", err)
×
3381
                }
×
3382

3383
                policy1 = e1
2✔
3384
                policy2 = e2
2✔
3385
                return nil
2✔
3386
        }, func() {
12✔
3387
                edgeInfo = nil
12✔
3388
                policy1 = nil
12✔
3389
                policy2 = nil
12✔
3390
        })
12✔
3391
        if err != nil {
23✔
3392
                return nil, nil, nil, err
11✔
3393
        }
11✔
3394

3395
        return edgeInfo, policy1, policy2, nil
2✔
3396
}
3397

3398
// FetchChannelEdgesByID attempts to lookup the two directed edges for the
3399
// channel identified by the channel ID. If the channel can't be found, then
3400
// ErrEdgeNotFound is returned. A struct which houses the general information
3401
// for the channel itself is returned as well as two structs that contain the
3402
// routing policies for the channel in either direction.
3403
//
3404
// ErrZombieEdge an be returned if the edge is currently marked as a zombie
3405
// within the database. In this case, the ChannelEdgePolicy's will be nil, and
3406
// the ChannelEdgeInfo will only include the public keys of each node.
3407
func (c *ChannelGraph) FetchChannelEdgesByID(chanID uint64) (
3408
        *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3409
        *models.ChannelEdgePolicy, error) {
25✔
3410

25✔
3411
        var (
25✔
3412
                edgeInfo  *models.ChannelEdgeInfo
25✔
3413
                policy1   *models.ChannelEdgePolicy
25✔
3414
                policy2   *models.ChannelEdgePolicy
25✔
3415
                channelID [8]byte
25✔
3416
        )
25✔
3417

25✔
3418
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
50✔
3419
                // First, grab the node bucket. This will be used to populate
25✔
3420
                // the Node pointers in each edge read from disk.
25✔
3421
                nodes := tx.ReadBucket(nodeBucket)
25✔
3422
                if nodes == nil {
25✔
3423
                        return ErrGraphNotFound
×
3424
                }
×
3425

3426
                // Next, grab the edge bucket which stores the edges, and also
3427
                // the index itself so we can group the directed edges together
3428
                // logically.
3429
                edges := tx.ReadBucket(edgeBucket)
25✔
3430
                if edges == nil {
25✔
3431
                        return ErrGraphNoEdgesFound
×
3432
                }
×
3433
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
25✔
3434
                if edgeIndex == nil {
25✔
3435
                        return ErrGraphNoEdgesFound
×
3436
                }
×
3437

3438
                byteOrder.PutUint64(channelID[:], chanID)
25✔
3439

25✔
3440
                // Now, attempt to fetch edge.
25✔
3441
                edge, err := fetchChanEdgeInfo(edgeIndex, channelID[:])
25✔
3442

25✔
3443
                // If it doesn't exist, we'll quickly check our zombie index to
25✔
3444
                // see if we've previously marked it as so.
25✔
3445
                if errors.Is(err, ErrEdgeNotFound) {
27✔
3446
                        // If the zombie index doesn't exist, or the edge is not
2✔
3447
                        // marked as a zombie within it, then we'll return the
2✔
3448
                        // original ErrEdgeNotFound error.
2✔
3449
                        zombieIndex := edges.NestedReadBucket(zombieBucket)
2✔
3450
                        if zombieIndex == nil {
2✔
3451
                                return ErrEdgeNotFound
×
3452
                        }
×
3453

3454
                        isZombie, pubKey1, pubKey2 := isZombieEdge(
2✔
3455
                                zombieIndex, chanID,
2✔
3456
                        )
2✔
3457
                        if !isZombie {
3✔
3458
                                return ErrEdgeNotFound
1✔
3459
                        }
1✔
3460

3461
                        // Otherwise, the edge is marked as a zombie, so we'll
3462
                        // populate the edge info with the public keys of each
3463
                        // party as this is the only information we have about
3464
                        // it and return an error signaling so.
3465
                        edgeInfo = &models.ChannelEdgeInfo{
2✔
3466
                                NodeKey1Bytes: pubKey1,
2✔
3467
                                NodeKey2Bytes: pubKey2,
2✔
3468
                        }
2✔
3469
                        return ErrZombieEdge
2✔
3470
                }
3471

3472
                // Otherwise, we'll just return the error if any.
3473
                if err != nil {
24✔
3474
                        return err
×
3475
                }
×
3476

3477
                edgeInfo = &edge
24✔
3478

24✔
3479
                // Then we'll attempt to fetch the accompanying policies of this
24✔
3480
                // edge.
24✔
3481
                e1, e2, err := fetchChanEdgePolicies(
24✔
3482
                        edgeIndex, edges, channelID[:],
24✔
3483
                )
24✔
3484
                if err != nil {
24✔
3485
                        return err
×
3486
                }
×
3487

3488
                policy1 = e1
24✔
3489
                policy2 = e2
24✔
3490
                return nil
24✔
3491
        }, func() {
25✔
3492
                edgeInfo = nil
25✔
3493
                policy1 = nil
25✔
3494
                policy2 = nil
25✔
3495
        })
25✔
3496
        if err == ErrZombieEdge {
27✔
3497
                return edgeInfo, nil, nil, err
2✔
3498
        }
2✔
3499
        if err != nil {
25✔
3500
                return nil, nil, nil, err
1✔
3501
        }
1✔
3502

3503
        return edgeInfo, policy1, policy2, nil
24✔
3504
}
3505

3506
// IsPublicNode is a helper method that determines whether the node with the
3507
// given public key is seen as a public node in the graph from the graph's
3508
// source node's point of view.
3509
func (c *ChannelGraph) IsPublicNode(pubKey [33]byte) (bool, error) {
14✔
3510
        var nodeIsPublic bool
14✔
3511
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
28✔
3512
                nodes := tx.ReadBucket(nodeBucket)
14✔
3513
                if nodes == nil {
14✔
3514
                        return ErrGraphNodesNotFound
×
3515
                }
×
3516
                ourPubKey := nodes.Get(sourceKey)
14✔
3517
                if ourPubKey == nil {
14✔
3518
                        return ErrSourceNodeNotSet
×
3519
                }
×
3520
                node, err := fetchLightningNode(nodes, pubKey[:])
14✔
3521
                if err != nil {
14✔
3522
                        return err
×
3523
                }
×
3524

3525
                nodeIsPublic, err = c.isPublic(tx, node.PubKeyBytes, ourPubKey)
14✔
3526
                return err
14✔
3527
        }, func() {
14✔
3528
                nodeIsPublic = false
14✔
3529
        })
14✔
3530
        if err != nil {
14✔
3531
                return false, err
×
3532
        }
×
3533

3534
        return nodeIsPublic, nil
14✔
3535
}
3536

3537
// genMultiSigP2WSH generates the p2wsh'd multisig script for 2 of 2 pubkeys.
3538
func genMultiSigP2WSH(aPub, bPub []byte) ([]byte, error) {
47✔
3539
        witnessScript, err := input.GenMultiSigScript(aPub, bPub)
47✔
3540
        if err != nil {
47✔
3541
                return nil, err
×
3542
        }
×
3543

3544
        // With the witness script generated, we'll now turn it into a p2wsh
3545
        // script:
3546
        //  * OP_0 <sha256(script)>
3547
        bldr := txscript.NewScriptBuilder(
47✔
3548
                txscript.WithScriptAllocSize(input.P2WSHSize),
47✔
3549
        )
47✔
3550
        bldr.AddOp(txscript.OP_0)
47✔
3551
        scriptHash := sha256.Sum256(witnessScript)
47✔
3552
        bldr.AddData(scriptHash[:])
47✔
3553

47✔
3554
        return bldr.Script()
47✔
3555
}
3556

3557
// EdgePoint couples the outpoint of a channel with the funding script that it
3558
// creates. The FilteredChainView will use this to watch for spends of this
3559
// edge point on chain. We require both of these values as depending on the
3560
// concrete implementation, either the pkScript, or the out point will be used.
3561
type EdgePoint struct {
3562
        // FundingPkScript is the p2wsh multi-sig script of the target channel.
3563
        FundingPkScript []byte
3564

3565
        // OutPoint is the outpoint of the target channel.
3566
        OutPoint wire.OutPoint
3567
}
3568

3569
// String returns a human readable version of the target EdgePoint. We return
3570
// the outpoint directly as it is enough to uniquely identify the edge point.
3571
func (e *EdgePoint) String() string {
×
3572
        return e.OutPoint.String()
×
3573
}
×
3574

3575
// ChannelView returns the verifiable edge information for each active channel
3576
// within the known channel graph. The set of UTXO's (along with their scripts)
3577
// returned are the ones that need to be watched on chain to detect channel
3578
// closes on the resident blockchain.
3579
func (c *ChannelGraph) ChannelView() ([]EdgePoint, error) {
23✔
3580
        var edgePoints []EdgePoint
23✔
3581
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
46✔
3582
                // We're going to iterate over the entire channel index, so
23✔
3583
                // we'll need to fetch the edgeBucket to get to the index as
23✔
3584
                // it's a sub-bucket.
23✔
3585
                edges := tx.ReadBucket(edgeBucket)
23✔
3586
                if edges == nil {
23✔
3587
                        return ErrGraphNoEdgesFound
×
3588
                }
×
3589
                chanIndex := edges.NestedReadBucket(channelPointBucket)
23✔
3590
                if chanIndex == nil {
23✔
3591
                        return ErrGraphNoEdgesFound
×
3592
                }
×
3593
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
23✔
3594
                if edgeIndex == nil {
23✔
3595
                        return ErrGraphNoEdgesFound
×
3596
                }
×
3597

3598
                // Once we have the proper bucket, we'll range over each key
3599
                // (which is the channel point for the channel) and decode it,
3600
                // accumulating each entry.
3601
                return chanIndex.ForEach(
23✔
3602
                        func(chanPointBytes, chanID []byte) error {
66✔
3603
                                chanPointReader := bytes.NewReader(
43✔
3604
                                        chanPointBytes,
43✔
3605
                                )
43✔
3606

43✔
3607
                                var chanPoint wire.OutPoint
43✔
3608
                                err := ReadOutpoint(chanPointReader, &chanPoint)
43✔
3609
                                if err != nil {
43✔
3610
                                        return err
×
3611
                                }
×
3612

3613
                                edgeInfo, err := fetchChanEdgeInfo(
43✔
3614
                                        edgeIndex, chanID,
43✔
3615
                                )
43✔
3616
                                if err != nil {
43✔
3617
                                        return err
×
3618
                                }
×
3619

3620
                                pkScript, err := genMultiSigP2WSH(
43✔
3621
                                        edgeInfo.BitcoinKey1Bytes[:],
43✔
3622
                                        edgeInfo.BitcoinKey2Bytes[:],
43✔
3623
                                )
43✔
3624
                                if err != nil {
43✔
3625
                                        return err
×
3626
                                }
×
3627

3628
                                edgePoints = append(edgePoints, EdgePoint{
43✔
3629
                                        FundingPkScript: pkScript,
43✔
3630
                                        OutPoint:        chanPoint,
43✔
3631
                                })
43✔
3632

43✔
3633
                                return nil
43✔
3634
                        },
3635
                )
3636
        }, func() {
23✔
3637
                edgePoints = nil
23✔
3638
        }); err != nil {
23✔
3639
                return nil, err
×
3640
        }
×
3641

3642
        return edgePoints, nil
23✔
3643
}
3644

3645
// MarkEdgeZombie attempts to mark a channel identified by its channel ID as a
3646
// zombie. This method is used on an ad-hoc basis, when channels need to be
3647
// marked as zombies outside the normal pruning cycle.
3648
func (c *ChannelGraph) MarkEdgeZombie(chanID uint64,
3649
        pubKey1, pubKey2 [33]byte) error {
126✔
3650

126✔
3651
        c.cacheMu.Lock()
126✔
3652
        defer c.cacheMu.Unlock()
126✔
3653

126✔
3654
        err := kvdb.Batch(c.db, func(tx kvdb.RwTx) error {
252✔
3655
                edges := tx.ReadWriteBucket(edgeBucket)
126✔
3656
                if edges == nil {
126✔
3657
                        return ErrGraphNoEdgesFound
×
3658
                }
×
3659
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
126✔
3660
                if err != nil {
126✔
3661
                        return fmt.Errorf("unable to create zombie "+
×
3662
                                "bucket: %w", err)
×
3663
                }
×
3664

3665
                if c.graphCache != nil {
252✔
3666
                        c.graphCache.RemoveChannel(pubKey1, pubKey2, chanID)
126✔
3667
                }
126✔
3668

3669
                return markEdgeZombie(zombieIndex, chanID, pubKey1, pubKey2)
126✔
3670
        })
3671
        if err != nil {
126✔
3672
                return err
×
3673
        }
×
3674

3675
        c.rejectCache.remove(chanID)
126✔
3676
        c.chanCache.remove(chanID)
126✔
3677

126✔
3678
        return nil
126✔
3679
}
3680

3681
// markEdgeZombie marks an edge as a zombie within our zombie index. The public
3682
// keys should represent the node public keys of the two parties involved in the
3683
// edge.
3684
func markEdgeZombie(zombieIndex kvdb.RwBucket, chanID uint64, pubKey1,
3685
        pubKey2 [33]byte) error {
150✔
3686

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

150✔
3690
        var v [66]byte
150✔
3691
        copy(v[:33], pubKey1[:])
150✔
3692
        copy(v[33:], pubKey2[:])
150✔
3693

150✔
3694
        return zombieIndex.Put(k[:], v[:])
150✔
3695
}
150✔
3696

3697
// MarkEdgeLive clears an edge from our zombie index, deeming it as live.
3698
func (c *ChannelGraph) MarkEdgeLive(chanID uint64) error {
2✔
3699
        c.cacheMu.Lock()
2✔
3700
        defer c.cacheMu.Unlock()
2✔
3701

2✔
3702
        return c.markEdgeLiveUnsafe(nil, chanID)
2✔
3703
}
2✔
3704

3705
// markEdgeLiveUnsafe clears an edge from the zombie index. This method can be
3706
// called with an existing kvdb.RwTx or the argument can be set to nil in which
3707
// case a new transaction will be created.
3708
//
3709
// NOTE: this method MUST only be called if the cacheMu has already been
3710
// acquired.
3711
func (c *ChannelGraph) markEdgeLiveUnsafe(tx kvdb.RwTx, chanID uint64) error {
28✔
3712
        dbFn := func(tx kvdb.RwTx) error {
56✔
3713
                edges := tx.ReadWriteBucket(edgeBucket)
28✔
3714
                if edges == nil {
28✔
3715
                        return ErrGraphNoEdgesFound
×
3716
                }
×
3717
                zombieIndex := edges.NestedReadWriteBucket(zombieBucket)
28✔
3718
                if zombieIndex == nil {
28✔
3719
                        return nil
×
3720
                }
×
3721

3722
                var k [8]byte
28✔
3723
                byteOrder.PutUint64(k[:], chanID)
28✔
3724

28✔
3725
                if len(zombieIndex.Get(k[:])) == 0 {
29✔
3726
                        return ErrZombieEdgeNotFound
1✔
3727
                }
1✔
3728

3729
                return zombieIndex.Delete(k[:])
27✔
3730
        }
3731

3732
        // If the transaction is nil, we'll create a new one. Otherwise, we use
3733
        // the existing transaction
3734
        var err error
28✔
3735
        if tx == nil {
30✔
3736
                err = kvdb.Update(c.db, dbFn, func() {})
4✔
3737
        } else {
26✔
3738
                err = dbFn(tx)
26✔
3739
        }
26✔
3740
        if err != nil {
29✔
3741
                return err
1✔
3742
        }
1✔
3743

3744
        c.rejectCache.remove(chanID)
27✔
3745
        c.chanCache.remove(chanID)
27✔
3746

27✔
3747
        // We need to add the channel back into our graph cache, otherwise we
27✔
3748
        // won't use it for path finding.
27✔
3749
        if c.graphCache != nil {
54✔
3750
                edgeInfos, err := c.fetchChanInfos(tx, []uint64{chanID})
27✔
3751
                if err != nil {
27✔
3752
                        return err
×
3753
                }
×
3754

3755
                for _, edgeInfo := range edgeInfos {
27✔
3756
                        c.graphCache.AddChannel(
×
3757
                                edgeInfo.Info, edgeInfo.Policy1,
×
3758
                                edgeInfo.Policy2,
×
3759
                        )
×
3760
                }
×
3761
        }
3762

3763
        return nil
27✔
3764
}
3765

3766
// IsZombieEdge returns whether the edge is considered zombie. If it is a
3767
// zombie, then the two node public keys corresponding to this edge are also
3768
// returned.
3769
func (c *ChannelGraph) IsZombieEdge(chanID uint64) (bool, [33]byte, [33]byte) {
5✔
3770
        var (
5✔
3771
                isZombie         bool
5✔
3772
                pubKey1, pubKey2 [33]byte
5✔
3773
        )
5✔
3774

5✔
3775
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
10✔
3776
                edges := tx.ReadBucket(edgeBucket)
5✔
3777
                if edges == nil {
5✔
3778
                        return ErrGraphNoEdgesFound
×
3779
                }
×
3780
                zombieIndex := edges.NestedReadBucket(zombieBucket)
5✔
3781
                if zombieIndex == nil {
5✔
3782
                        return nil
×
3783
                }
×
3784

3785
                isZombie, pubKey1, pubKey2 = isZombieEdge(zombieIndex, chanID)
5✔
3786
                return nil
5✔
3787
        }, func() {
5✔
3788
                isZombie = false
5✔
3789
                pubKey1 = [33]byte{}
5✔
3790
                pubKey2 = [33]byte{}
5✔
3791
        })
5✔
3792
        if err != nil {
5✔
3793
                return false, [33]byte{}, [33]byte{}
×
3794
        }
×
3795

3796
        return isZombie, pubKey1, pubKey2
5✔
3797
}
3798

3799
// isZombieEdge returns whether an entry exists for the given channel in the
3800
// zombie index. If an entry exists, then the two node public keys corresponding
3801
// to this edge are also returned.
3802
func isZombieEdge(zombieIndex kvdb.RBucket,
3803
        chanID uint64) (bool, [33]byte, [33]byte) {
219✔
3804

219✔
3805
        var k [8]byte
219✔
3806
        byteOrder.PutUint64(k[:], chanID)
219✔
3807

219✔
3808
        v := zombieIndex.Get(k[:])
219✔
3809
        if v == nil {
348✔
3810
                return false, [33]byte{}, [33]byte{}
129✔
3811
        }
129✔
3812

3813
        var pubKey1, pubKey2 [33]byte
91✔
3814
        copy(pubKey1[:], v[:33])
91✔
3815
        copy(pubKey2[:], v[33:])
91✔
3816

91✔
3817
        return true, pubKey1, pubKey2
91✔
3818
}
3819

3820
// NumZombies returns the current number of zombie channels in the graph.
3821
func (c *ChannelGraph) NumZombies() (uint64, error) {
4✔
3822
        var numZombies uint64
4✔
3823
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
8✔
3824
                edges := tx.ReadBucket(edgeBucket)
4✔
3825
                if edges == nil {
4✔
3826
                        return nil
×
3827
                }
×
3828
                zombieIndex := edges.NestedReadBucket(zombieBucket)
4✔
3829
                if zombieIndex == nil {
4✔
3830
                        return nil
×
3831
                }
×
3832

3833
                return zombieIndex.ForEach(func(_, _ []byte) error {
6✔
3834
                        numZombies++
2✔
3835
                        return nil
2✔
3836
                })
2✔
3837
        }, func() {
4✔
3838
                numZombies = 0
4✔
3839
        })
4✔
3840
        if err != nil {
4✔
3841
                return 0, err
×
3842
        }
×
3843

3844
        return numZombies, nil
4✔
3845
}
3846

3847
// PutClosedScid stores a SCID for a closed channel in the database. This is so
3848
// that we can ignore channel announcements that we know to be closed without
3849
// having to validate them and fetch a block.
3850
func (c *ChannelGraph) PutClosedScid(scid lnwire.ShortChannelID) error {
1✔
3851
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
2✔
3852
                closedScids, err := tx.CreateTopLevelBucket(closedScidBucket)
1✔
3853
                if err != nil {
1✔
3854
                        return err
×
3855
                }
×
3856

3857
                var k [8]byte
1✔
3858
                byteOrder.PutUint64(k[:], scid.ToUint64())
1✔
3859

1✔
3860
                return closedScids.Put(k[:], []byte{})
1✔
3861
        }, func() {})
1✔
3862
}
3863

3864
// IsClosedScid checks whether a channel identified by the passed in scid is
3865
// closed. This helps avoid having to perform expensive validation checks.
3866
// TODO: Add an LRU cache to cut down on disc reads.
3867
func (c *ChannelGraph) IsClosedScid(scid lnwire.ShortChannelID) (bool, error) {
3✔
3868
        var isClosed bool
3✔
3869
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
3870
                closedScids := tx.ReadBucket(closedScidBucket)
3✔
3871
                if closedScids == nil {
3✔
3872
                        return ErrClosedScidsNotFound
×
3873
                }
×
3874

3875
                var k [8]byte
3✔
3876
                byteOrder.PutUint64(k[:], scid.ToUint64())
3✔
3877

3✔
3878
                if closedScids.Get(k[:]) != nil {
4✔
3879
                        isClosed = true
1✔
3880
                        return nil
1✔
3881
                }
1✔
3882

3883
                return nil
2✔
3884
        }, func() {
3✔
3885
                isClosed = false
3✔
3886
        })
3✔
3887
        if err != nil {
3✔
3888
                return false, err
×
3889
        }
×
3890

3891
        return isClosed, nil
3✔
3892
}
3893

3894
// GraphSession will provide the call-back with access to a NodeTraverser
3895
// instance which can be used to perform queries against the channel graph. If
3896
// the graph cache is not enabled, then the call-back will  be provided with
3897
// access to the graph via a consistent read-only transaction.
3898
func (c *ChannelGraph) GraphSession(cb func(graph NodeTraverser) error) error {
134✔
3899
        if c.graphCache != nil {
214✔
3900
                return cb(&nodeTraverserSession{db: c})
80✔
3901
        }
80✔
3902

3903
        return c.db.View(func(tx walletdb.ReadTx) error {
108✔
3904
                return cb(&nodeTraverserSession{
54✔
3905
                        db: c,
54✔
3906
                        tx: tx,
54✔
3907
                })
54✔
3908
        }, func() {})
108✔
3909
}
3910

3911
// nodeTraverserSession implements the NodeTraverser interface but with a
3912
// backing read only transaction for a consistent view of the graph in the case
3913
// where the graph Cache has not been enabled.
3914
type nodeTraverserSession struct {
3915
        tx kvdb.RTx
3916
        db *ChannelGraph
3917
}
3918

3919
// ForEachNodeDirectedChannel calls the callback for every channel of the given
3920
// node.
3921
//
3922
// NOTE: Part of the NodeTraverser interface.
3923
func (c *nodeTraverserSession) ForEachNodeDirectedChannel(nodePub route.Vertex,
3924
        cb func(channel *DirectedChannel) error) error {
590✔
3925

590✔
3926
        return c.db.forEachNodeDirectedChannel(c.tx, nodePub, cb)
590✔
3927
}
590✔
3928

3929
// FetchNodeFeatures returns the features of the given node. If the node is
3930
// unknown, assume no additional features are supported.
3931
//
3932
// NOTE: Part of the NodeTraverser interface.
3933
func (c *nodeTraverserSession) FetchNodeFeatures(nodePub route.Vertex) (
3934
        *lnwire.FeatureVector, error) {
621✔
3935

621✔
3936
        return c.db.fetchNodeFeatures(c.tx, nodePub)
621✔
3937
}
621✔
3938

3939
func putLightningNode(nodeBucket kvdb.RwBucket, aliasBucket kvdb.RwBucket, // nolint:dupl
3940
        updateIndex kvdb.RwBucket, node *models.LightningNode) error {
998✔
3941

998✔
3942
        var (
998✔
3943
                scratch [16]byte
998✔
3944
                b       bytes.Buffer
998✔
3945
        )
998✔
3946

998✔
3947
        pub, err := node.PubKey()
998✔
3948
        if err != nil {
998✔
3949
                return err
×
3950
        }
×
3951
        nodePub := pub.SerializeCompressed()
998✔
3952

998✔
3953
        // If the node has the update time set, write it, else write 0.
998✔
3954
        updateUnix := uint64(0)
998✔
3955
        if node.LastUpdate.Unix() > 0 {
1,862✔
3956
                updateUnix = uint64(node.LastUpdate.Unix())
864✔
3957
        }
864✔
3958

3959
        byteOrder.PutUint64(scratch[:8], updateUnix)
998✔
3960
        if _, err := b.Write(scratch[:8]); err != nil {
998✔
3961
                return err
×
3962
        }
×
3963

3964
        if _, err := b.Write(nodePub); err != nil {
998✔
3965
                return err
×
3966
        }
×
3967

3968
        // If we got a node announcement for this node, we will have the rest
3969
        // of the data available. If not we don't have more data to write.
3970
        if !node.HaveNodeAnnouncement {
1,080✔
3971
                // Write HaveNodeAnnouncement=0.
82✔
3972
                byteOrder.PutUint16(scratch[:2], 0)
82✔
3973
                if _, err := b.Write(scratch[:2]); err != nil {
82✔
3974
                        return err
×
3975
                }
×
3976

3977
                return nodeBucket.Put(nodePub, b.Bytes())
82✔
3978
        }
3979

3980
        // Write HaveNodeAnnouncement=1.
3981
        byteOrder.PutUint16(scratch[:2], 1)
917✔
3982
        if _, err := b.Write(scratch[:2]); err != nil {
917✔
3983
                return err
×
3984
        }
×
3985

3986
        if err := binary.Write(&b, byteOrder, node.Color.R); err != nil {
917✔
3987
                return err
×
3988
        }
×
3989
        if err := binary.Write(&b, byteOrder, node.Color.G); err != nil {
917✔
3990
                return err
×
3991
        }
×
3992
        if err := binary.Write(&b, byteOrder, node.Color.B); err != nil {
917✔
3993
                return err
×
3994
        }
×
3995

3996
        if err := wire.WriteVarString(&b, 0, node.Alias); err != nil {
917✔
3997
                return err
×
3998
        }
×
3999

4000
        if err := node.Features.Encode(&b); err != nil {
917✔
4001
                return err
×
4002
        }
×
4003

4004
        numAddresses := uint16(len(node.Addresses))
917✔
4005
        byteOrder.PutUint16(scratch[:2], numAddresses)
917✔
4006
        if _, err := b.Write(scratch[:2]); err != nil {
917✔
4007
                return err
×
4008
        }
×
4009

4010
        for _, address := range node.Addresses {
2,062✔
4011
                if err := SerializeAddr(&b, address); err != nil {
1,145✔
4012
                        return err
×
4013
                }
×
4014
        }
4015

4016
        sigLen := len(node.AuthSigBytes)
917✔
4017
        if sigLen > 80 {
917✔
4018
                return fmt.Errorf("max sig len allowed is 80, had %v",
×
4019
                        sigLen)
×
4020
        }
×
4021

4022
        err = wire.WriteVarBytes(&b, 0, node.AuthSigBytes)
917✔
4023
        if err != nil {
917✔
4024
                return err
×
4025
        }
×
4026

4027
        if len(node.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
917✔
4028
                return ErrTooManyExtraOpaqueBytes(len(node.ExtraOpaqueData))
×
4029
        }
×
4030
        err = wire.WriteVarBytes(&b, 0, node.ExtraOpaqueData)
917✔
4031
        if err != nil {
917✔
4032
                return err
×
4033
        }
×
4034

4035
        if err := aliasBucket.Put(nodePub, []byte(node.Alias)); err != nil {
917✔
4036
                return err
×
4037
        }
×
4038

4039
        // With the alias bucket updated, we'll now update the index that
4040
        // tracks the time series of node updates.
4041
        var indexKey [8 + 33]byte
917✔
4042
        byteOrder.PutUint64(indexKey[:8], updateUnix)
917✔
4043
        copy(indexKey[8:], nodePub)
917✔
4044

917✔
4045
        // If there was already an old index entry for this node, then we'll
917✔
4046
        // delete the old one before we write the new entry.
917✔
4047
        if nodeBytes := nodeBucket.Get(nodePub); nodeBytes != nil {
1,022✔
4048
                // Extract out the old update time to we can reconstruct the
105✔
4049
                // prior index key to delete it from the index.
105✔
4050
                oldUpdateTime := nodeBytes[:8]
105✔
4051

105✔
4052
                var oldIndexKey [8 + 33]byte
105✔
4053
                copy(oldIndexKey[:8], oldUpdateTime)
105✔
4054
                copy(oldIndexKey[8:], nodePub)
105✔
4055

105✔
4056
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
105✔
4057
                        return err
×
4058
                }
×
4059
        }
4060

4061
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
917✔
4062
                return err
×
4063
        }
×
4064

4065
        return nodeBucket.Put(nodePub, b.Bytes())
917✔
4066
}
4067

4068
func fetchLightningNode(nodeBucket kvdb.RBucket,
4069
        nodePub []byte) (models.LightningNode, error) {
3,638✔
4070

3,638✔
4071
        nodeBytes := nodeBucket.Get(nodePub)
3,638✔
4072
        if nodeBytes == nil {
3,719✔
4073
                return models.LightningNode{}, ErrGraphNodeNotFound
81✔
4074
        }
81✔
4075

4076
        nodeReader := bytes.NewReader(nodeBytes)
3,558✔
4077
        return deserializeLightningNode(nodeReader)
3,558✔
4078
}
4079

4080
func deserializeLightningNodeCacheable(r io.Reader) (route.Vertex,
4081
        *lnwire.FeatureVector, error) {
121✔
4082

121✔
4083
        var (
121✔
4084
                pubKey      route.Vertex
121✔
4085
                features    = lnwire.EmptyFeatureVector()
121✔
4086
                nodeScratch [8]byte
121✔
4087
        )
121✔
4088

121✔
4089
        // Skip ahead:
121✔
4090
        // - LastUpdate (8 bytes)
121✔
4091
        if _, err := r.Read(nodeScratch[:]); err != nil {
121✔
4092
                return pubKey, nil, err
×
4093
        }
×
4094

4095
        if _, err := io.ReadFull(r, pubKey[:]); err != nil {
121✔
4096
                return pubKey, nil, err
×
4097
        }
×
4098

4099
        // Read the node announcement flag.
4100
        if _, err := r.Read(nodeScratch[:2]); err != nil {
121✔
4101
                return pubKey, nil, err
×
4102
        }
×
4103
        hasNodeAnn := byteOrder.Uint16(nodeScratch[:2])
121✔
4104

121✔
4105
        // The rest of the data is optional, and will only be there if we got a
121✔
4106
        // node announcement for this node.
121✔
4107
        if hasNodeAnn == 0 {
122✔
4108
                return pubKey, features, nil
1✔
4109
        }
1✔
4110

4111
        // We did get a node announcement for this node, so we'll have the rest
4112
        // of the data available.
4113
        var rgb uint8
121✔
4114
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
121✔
4115
                return pubKey, nil, err
×
4116
        }
×
4117
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
121✔
4118
                return pubKey, nil, err
×
4119
        }
×
4120
        if err := binary.Read(r, byteOrder, &rgb); err != nil {
121✔
4121
                return pubKey, nil, err
×
4122
        }
×
4123

4124
        if _, err := wire.ReadVarString(r, 0); err != nil {
121✔
4125
                return pubKey, nil, err
×
4126
        }
×
4127

4128
        if err := features.Decode(r); err != nil {
121✔
4129
                return pubKey, nil, err
×
4130
        }
×
4131

4132
        return pubKey, features, nil
121✔
4133
}
4134

4135
func deserializeLightningNode(r io.Reader) (models.LightningNode, error) {
8,517✔
4136
        var (
8,517✔
4137
                node    models.LightningNode
8,517✔
4138
                scratch [8]byte
8,517✔
4139
                err     error
8,517✔
4140
        )
8,517✔
4141

8,517✔
4142
        // Always populate a feature vector, even if we don't have a node
8,517✔
4143
        // announcement and short circuit below.
8,517✔
4144
        node.Features = lnwire.EmptyFeatureVector()
8,517✔
4145

8,517✔
4146
        if _, err := r.Read(scratch[:]); err != nil {
8,517✔
4147
                return models.LightningNode{}, err
×
4148
        }
×
4149

4150
        unix := int64(byteOrder.Uint64(scratch[:]))
8,517✔
4151
        node.LastUpdate = time.Unix(unix, 0)
8,517✔
4152

8,517✔
4153
        if _, err := io.ReadFull(r, node.PubKeyBytes[:]); err != nil {
8,517✔
4154
                return models.LightningNode{}, err
×
4155
        }
×
4156

4157
        if _, err := r.Read(scratch[:2]); err != nil {
8,517✔
4158
                return models.LightningNode{}, err
×
4159
        }
×
4160

4161
        hasNodeAnn := byteOrder.Uint16(scratch[:2])
8,517✔
4162
        if hasNodeAnn == 1 {
16,887✔
4163
                node.HaveNodeAnnouncement = true
8,370✔
4164
        } else {
8,518✔
4165
                node.HaveNodeAnnouncement = false
148✔
4166
        }
148✔
4167

4168
        // The rest of the data is optional, and will only be there if we got a
4169
        // node announcement for this node.
4170
        if !node.HaveNodeAnnouncement {
8,665✔
4171
                return node, nil
148✔
4172
        }
148✔
4173

4174
        // We did get a node announcement for this node, so we'll have the rest
4175
        // of the data available.
4176
        if err := binary.Read(r, byteOrder, &node.Color.R); err != nil {
8,370✔
4177
                return models.LightningNode{}, err
×
4178
        }
×
4179
        if err := binary.Read(r, byteOrder, &node.Color.G); err != nil {
8,370✔
4180
                return models.LightningNode{}, err
×
4181
        }
×
4182
        if err := binary.Read(r, byteOrder, &node.Color.B); err != nil {
8,370✔
4183
                return models.LightningNode{}, err
×
4184
        }
×
4185

4186
        node.Alias, err = wire.ReadVarString(r, 0)
8,370✔
4187
        if err != nil {
8,370✔
4188
                return models.LightningNode{}, err
×
4189
        }
×
4190

4191
        err = node.Features.Decode(r)
8,370✔
4192
        if err != nil {
8,370✔
4193
                return models.LightningNode{}, err
×
4194
        }
×
4195

4196
        if _, err := r.Read(scratch[:2]); err != nil {
8,370✔
4197
                return models.LightningNode{}, err
×
4198
        }
×
4199
        numAddresses := int(byteOrder.Uint16(scratch[:2]))
8,370✔
4200

8,370✔
4201
        var addresses []net.Addr
8,370✔
4202
        for i := 0; i < numAddresses; i++ {
18,981✔
4203
                address, err := DeserializeAddr(r)
10,611✔
4204
                if err != nil {
10,611✔
4205
                        return models.LightningNode{}, err
×
4206
                }
×
4207
                addresses = append(addresses, address)
10,611✔
4208
        }
4209
        node.Addresses = addresses
8,370✔
4210

8,370✔
4211
        node.AuthSigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
8,370✔
4212
        if err != nil {
8,370✔
4213
                return models.LightningNode{}, err
×
4214
        }
×
4215

4216
        // We'll try and see if there are any opaque bytes left, if not, then
4217
        // we'll ignore the EOF error and return the node as is.
4218
        node.ExtraOpaqueData, err = wire.ReadVarBytes(
8,370✔
4219
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
8,370✔
4220
        )
8,370✔
4221
        switch {
8,370✔
4222
        case err == io.ErrUnexpectedEOF:
×
4223
        case err == io.EOF:
×
4224
        case err != nil:
×
4225
                return models.LightningNode{}, err
×
4226
        }
4227

4228
        return node, nil
8,370✔
4229
}
4230

4231
func putChanEdgeInfo(edgeIndex kvdb.RwBucket,
4232
        edgeInfo *models.ChannelEdgeInfo, chanID [8]byte) error {
1,490✔
4233

1,490✔
4234
        var b bytes.Buffer
1,490✔
4235

1,490✔
4236
        if _, err := b.Write(edgeInfo.NodeKey1Bytes[:]); err != nil {
1,490✔
4237
                return err
×
4238
        }
×
4239
        if _, err := b.Write(edgeInfo.NodeKey2Bytes[:]); err != nil {
1,490✔
4240
                return err
×
4241
        }
×
4242
        if _, err := b.Write(edgeInfo.BitcoinKey1Bytes[:]); err != nil {
1,490✔
4243
                return err
×
4244
        }
×
4245
        if _, err := b.Write(edgeInfo.BitcoinKey2Bytes[:]); err != nil {
1,490✔
4246
                return err
×
4247
        }
×
4248

4249
        if err := wire.WriteVarBytes(&b, 0, edgeInfo.Features); err != nil {
1,490✔
4250
                return err
×
4251
        }
×
4252

4253
        authProof := edgeInfo.AuthProof
1,490✔
4254
        var nodeSig1, nodeSig2, bitcoinSig1, bitcoinSig2 []byte
1,490✔
4255
        if authProof != nil {
2,897✔
4256
                nodeSig1 = authProof.NodeSig1Bytes
1,407✔
4257
                nodeSig2 = authProof.NodeSig2Bytes
1,407✔
4258
                bitcoinSig1 = authProof.BitcoinSig1Bytes
1,407✔
4259
                bitcoinSig2 = authProof.BitcoinSig2Bytes
1,407✔
4260
        }
1,407✔
4261

4262
        if err := wire.WriteVarBytes(&b, 0, nodeSig1); err != nil {
1,490✔
4263
                return err
×
4264
        }
×
4265
        if err := wire.WriteVarBytes(&b, 0, nodeSig2); err != nil {
1,490✔
4266
                return err
×
4267
        }
×
4268
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig1); err != nil {
1,490✔
4269
                return err
×
4270
        }
×
4271
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig2); err != nil {
1,490✔
4272
                return err
×
4273
        }
×
4274

4275
        if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
1,490✔
4276
                return err
×
4277
        }
×
4278
        err := binary.Write(&b, byteOrder, uint64(edgeInfo.Capacity))
1,490✔
4279
        if err != nil {
1,490✔
4280
                return err
×
4281
        }
×
4282
        if _, err := b.Write(chanID[:]); err != nil {
1,490✔
4283
                return err
×
4284
        }
×
4285
        if _, err := b.Write(edgeInfo.ChainHash[:]); err != nil {
1,490✔
4286
                return err
×
4287
        }
×
4288

4289
        if len(edgeInfo.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
1,490✔
4290
                return ErrTooManyExtraOpaqueBytes(len(edgeInfo.ExtraOpaqueData))
×
4291
        }
×
4292
        err = wire.WriteVarBytes(&b, 0, edgeInfo.ExtraOpaqueData)
1,490✔
4293
        if err != nil {
1,490✔
4294
                return err
×
4295
        }
×
4296

4297
        return edgeIndex.Put(chanID[:], b.Bytes())
1,490✔
4298
}
4299

4300
func fetchChanEdgeInfo(edgeIndex kvdb.RBucket,
4301
        chanID []byte) (models.ChannelEdgeInfo, error) {
4,196✔
4302

4,196✔
4303
        edgeInfoBytes := edgeIndex.Get(chanID)
4,196✔
4304
        if edgeInfoBytes == nil {
4,286✔
4305
                return models.ChannelEdgeInfo{}, ErrEdgeNotFound
90✔
4306
        }
90✔
4307

4308
        edgeInfoReader := bytes.NewReader(edgeInfoBytes)
4,107✔
4309
        return deserializeChanEdgeInfo(edgeInfoReader)
4,107✔
4310
}
4311

4312
func deserializeChanEdgeInfo(r io.Reader) (models.ChannelEdgeInfo, error) {
4,740✔
4313
        var (
4,740✔
4314
                err      error
4,740✔
4315
                edgeInfo models.ChannelEdgeInfo
4,740✔
4316
        )
4,740✔
4317

4,740✔
4318
        if _, err := io.ReadFull(r, edgeInfo.NodeKey1Bytes[:]); err != nil {
4,740✔
4319
                return models.ChannelEdgeInfo{}, err
×
4320
        }
×
4321
        if _, err := io.ReadFull(r, edgeInfo.NodeKey2Bytes[:]); err != nil {
4,740✔
4322
                return models.ChannelEdgeInfo{}, err
×
4323
        }
×
4324
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey1Bytes[:]); err != nil {
4,740✔
4325
                return models.ChannelEdgeInfo{}, err
×
4326
        }
×
4327
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey2Bytes[:]); err != nil {
4,740✔
4328
                return models.ChannelEdgeInfo{}, err
×
4329
        }
×
4330

4331
        edgeInfo.Features, err = wire.ReadVarBytes(r, 0, 900, "features")
4,740✔
4332
        if err != nil {
4,740✔
4333
                return models.ChannelEdgeInfo{}, err
×
4334
        }
×
4335

4336
        proof := &models.ChannelAuthProof{}
4,740✔
4337

4,740✔
4338
        proof.NodeSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
4,740✔
4339
        if err != nil {
4,740✔
4340
                return models.ChannelEdgeInfo{}, err
×
4341
        }
×
4342
        proof.NodeSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
4,740✔
4343
        if err != nil {
4,740✔
4344
                return models.ChannelEdgeInfo{}, err
×
4345
        }
×
4346
        proof.BitcoinSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
4,740✔
4347
        if err != nil {
4,740✔
4348
                return models.ChannelEdgeInfo{}, err
×
4349
        }
×
4350
        proof.BitcoinSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
4,740✔
4351
        if err != nil {
4,740✔
4352
                return models.ChannelEdgeInfo{}, err
×
4353
        }
×
4354

4355
        if !proof.IsEmpty() {
6,531✔
4356
                edgeInfo.AuthProof = proof
1,791✔
4357
        }
1,791✔
4358

4359
        edgeInfo.ChannelPoint = wire.OutPoint{}
4,740✔
4360
        if err := ReadOutpoint(r, &edgeInfo.ChannelPoint); err != nil {
4,740✔
4361
                return models.ChannelEdgeInfo{}, err
×
4362
        }
×
4363
        if err := binary.Read(r, byteOrder, &edgeInfo.Capacity); err != nil {
4,740✔
4364
                return models.ChannelEdgeInfo{}, err
×
4365
        }
×
4366
        if err := binary.Read(r, byteOrder, &edgeInfo.ChannelID); err != nil {
4,740✔
4367
                return models.ChannelEdgeInfo{}, err
×
4368
        }
×
4369

4370
        if _, err := io.ReadFull(r, edgeInfo.ChainHash[:]); err != nil {
4,740✔
4371
                return models.ChannelEdgeInfo{}, err
×
4372
        }
×
4373

4374
        // We'll try and see if there are any opaque bytes left, if not, then
4375
        // we'll ignore the EOF error and return the edge as is.
4376
        edgeInfo.ExtraOpaqueData, err = wire.ReadVarBytes(
4,740✔
4377
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
4,740✔
4378
        )
4,740✔
4379
        switch {
4,740✔
4380
        case err == io.ErrUnexpectedEOF:
×
4381
        case err == io.EOF:
×
4382
        case err != nil:
×
4383
                return models.ChannelEdgeInfo{}, err
×
4384
        }
4385

4386
        return edgeInfo, nil
4,740✔
4387
}
4388

4389
func putChanEdgePolicy(edges kvdb.RwBucket, edge *models.ChannelEdgePolicy,
4390
        from, to []byte) error {
2,661✔
4391

2,661✔
4392
        var edgeKey [33 + 8]byte
2,661✔
4393
        copy(edgeKey[:], from)
2,661✔
4394
        byteOrder.PutUint64(edgeKey[33:], edge.ChannelID)
2,661✔
4395

2,661✔
4396
        var b bytes.Buffer
2,661✔
4397
        if err := serializeChanEdgePolicy(&b, edge, to); err != nil {
2,661✔
4398
                return err
×
4399
        }
×
4400

4401
        // Before we write out the new edge, we'll create a new entry in the
4402
        // update index in order to keep it fresh.
4403
        updateUnix := uint64(edge.LastUpdate.Unix())
2,661✔
4404
        var indexKey [8 + 8]byte
2,661✔
4405
        byteOrder.PutUint64(indexKey[:8], updateUnix)
2,661✔
4406
        byteOrder.PutUint64(indexKey[8:], edge.ChannelID)
2,661✔
4407

2,661✔
4408
        updateIndex, err := edges.CreateBucketIfNotExists(edgeUpdateIndexBucket)
2,661✔
4409
        if err != nil {
2,661✔
4410
                return err
×
4411
        }
×
4412

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

25✔
4420
                // In order to delete the old entry, we'll need to obtain the
25✔
4421
                // *prior* update time in order to delete it. To do this, we'll
25✔
4422
                // need to deserialize the existing policy within the database
25✔
4423
                // (now outdated by the new one), and delete its corresponding
25✔
4424
                // entry within the update index. We'll ignore any
25✔
4425
                // ErrEdgePolicyOptionalFieldNotFound error, as we only need
25✔
4426
                // the channel ID and update time to delete the entry.
25✔
4427
                // TODO(halseth): get rid of these invalid policies in a
25✔
4428
                // migration.
25✔
4429
                oldEdgePolicy, err := deserializeChanEdgePolicy(
25✔
4430
                        bytes.NewReader(edgeBytes),
25✔
4431
                )
25✔
4432
                if err != nil && err != ErrEdgePolicyOptionalFieldNotFound {
25✔
4433
                        return err
×
4434
                }
×
4435

4436
                oldUpdateTime := uint64(oldEdgePolicy.LastUpdate.Unix())
25✔
4437

25✔
4438
                var oldIndexKey [8 + 8]byte
25✔
4439
                byteOrder.PutUint64(oldIndexKey[:8], oldUpdateTime)
25✔
4440
                byteOrder.PutUint64(oldIndexKey[8:], edge.ChannelID)
25✔
4441

25✔
4442
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
25✔
4443
                        return err
×
4444
                }
×
4445
        }
4446

4447
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
2,661✔
4448
                return err
×
4449
        }
×
4450

4451
        err = updateEdgePolicyDisabledIndex(
2,661✔
4452
                edges, edge.ChannelID,
2,661✔
4453
                edge.ChannelFlags&lnwire.ChanUpdateDirection > 0,
2,661✔
4454
                edge.IsDisabled(),
2,661✔
4455
        )
2,661✔
4456
        if err != nil {
2,661✔
4457
                return err
×
4458
        }
×
4459

4460
        return edges.Put(edgeKey[:], b.Bytes()[:])
2,661✔
4461
}
4462

4463
// updateEdgePolicyDisabledIndex is used to update the disabledEdgePolicyIndex
4464
// bucket by either add a new disabled ChannelEdgePolicy or remove an existing
4465
// one.
4466
// The direction represents the direction of the edge and disabled is used for
4467
// deciding whether to remove or add an entry to the bucket.
4468
// In general a channel is disabled if two entries for the same chanID exist
4469
// in this bucket.
4470
// Maintaining the bucket this way allows a fast retrieval of disabled
4471
// channels, for example when prune is needed.
4472
func updateEdgePolicyDisabledIndex(edges kvdb.RwBucket, chanID uint64,
4473
        direction bool, disabled bool) error {
2,955✔
4474

2,955✔
4475
        var disabledEdgeKey [8 + 1]byte
2,955✔
4476
        byteOrder.PutUint64(disabledEdgeKey[0:], chanID)
2,955✔
4477
        if direction {
4,429✔
4478
                disabledEdgeKey[8] = 1
1,474✔
4479
        }
1,474✔
4480

4481
        disabledEdgePolicyIndex, err := edges.CreateBucketIfNotExists(
2,955✔
4482
                disabledEdgePolicyBucket,
2,955✔
4483
        )
2,955✔
4484
        if err != nil {
2,955✔
4485
                return err
×
4486
        }
×
4487

4488
        if disabled {
2,982✔
4489
                return disabledEdgePolicyIndex.Put(disabledEdgeKey[:], []byte{})
27✔
4490
        }
27✔
4491

4492
        return disabledEdgePolicyIndex.Delete(disabledEdgeKey[:])
2,929✔
4493
}
4494

4495
// putChanEdgePolicyUnknown marks the edge policy as unknown
4496
// in the edges bucket.
4497
func putChanEdgePolicyUnknown(edges kvdb.RwBucket, channelID uint64,
4498
        from []byte) error {
2,977✔
4499

2,977✔
4500
        var edgeKey [33 + 8]byte
2,977✔
4501
        copy(edgeKey[:], from)
2,977✔
4502
        byteOrder.PutUint64(edgeKey[33:], channelID)
2,977✔
4503

2,977✔
4504
        if edges.Get(edgeKey[:]) != nil {
2,977✔
4505
                return fmt.Errorf("cannot write unknown policy for channel %v "+
×
4506
                        " when there is already a policy present", channelID)
×
4507
        }
×
4508

4509
        return edges.Put(edgeKey[:], unknownPolicy)
2,977✔
4510
}
4511

4512
func fetchChanEdgePolicy(edges kvdb.RBucket, chanID []byte,
4513
        nodePub []byte) (*models.ChannelEdgePolicy, error) {
8,173✔
4514

8,173✔
4515
        var edgeKey [33 + 8]byte
8,173✔
4516
        copy(edgeKey[:], nodePub)
8,173✔
4517
        copy(edgeKey[33:], chanID[:])
8,173✔
4518

8,173✔
4519
        edgeBytes := edges.Get(edgeKey[:])
8,173✔
4520
        if edgeBytes == nil {
8,173✔
4521
                return nil, ErrEdgeNotFound
×
4522
        }
×
4523

4524
        // No need to deserialize unknown policy.
4525
        if bytes.Equal(edgeBytes[:], unknownPolicy) {
8,549✔
4526
                return nil, nil
376✔
4527
        }
376✔
4528

4529
        edgeReader := bytes.NewReader(edgeBytes)
7,798✔
4530

7,798✔
4531
        ep, err := deserializeChanEdgePolicy(edgeReader)
7,798✔
4532
        switch {
7,798✔
4533
        // If the db policy was missing an expected optional field, we return
4534
        // nil as if the policy was unknown.
4535
        case err == ErrEdgePolicyOptionalFieldNotFound:
1✔
4536
                return nil, nil
1✔
4537

4538
        case err != nil:
×
4539
                return nil, err
×
4540
        }
4541

4542
        return ep, nil
7,797✔
4543
}
4544

4545
func fetchChanEdgePolicies(edgeIndex kvdb.RBucket, edges kvdb.RBucket,
4546
        chanID []byte) (*models.ChannelEdgePolicy, *models.ChannelEdgePolicy,
4547
        error) {
247✔
4548

247✔
4549
        edgeInfo := edgeIndex.Get(chanID)
247✔
4550
        if edgeInfo == nil {
247✔
4551
                return nil, nil, fmt.Errorf("%w: chanID=%x", ErrEdgeNotFound,
×
4552
                        chanID)
×
4553
        }
×
4554

4555
        // The first node is contained within the first half of the edge
4556
        // information. We only propagate the error here and below if it's
4557
        // something other than edge non-existence.
4558
        node1Pub := edgeInfo[:33]
247✔
4559
        edge1, err := fetchChanEdgePolicy(edges, chanID, node1Pub)
247✔
4560
        if err != nil {
247✔
4561
                return nil, nil, fmt.Errorf("%w: node1Pub=%x", ErrEdgeNotFound,
×
4562
                        node1Pub)
×
4563
        }
×
4564

4565
        // Similarly, the second node is contained within the latter
4566
        // half of the edge information.
4567
        node2Pub := edgeInfo[33:66]
247✔
4568
        edge2, err := fetchChanEdgePolicy(edges, chanID, node2Pub)
247✔
4569
        if err != nil {
247✔
4570
                return nil, nil, fmt.Errorf("%w: node2Pub=%x", ErrEdgeNotFound,
×
4571
                        node2Pub)
×
4572
        }
×
4573

4574
        return edge1, edge2, nil
247✔
4575
}
4576

4577
func serializeChanEdgePolicy(w io.Writer, edge *models.ChannelEdgePolicy,
4578
        to []byte) error {
2,663✔
4579

2,663✔
4580
        err := wire.WriteVarBytes(w, 0, edge.SigBytes)
2,663✔
4581
        if err != nil {
2,663✔
4582
                return err
×
4583
        }
×
4584

4585
        if err := binary.Write(w, byteOrder, edge.ChannelID); err != nil {
2,663✔
4586
                return err
×
4587
        }
×
4588

4589
        var scratch [8]byte
2,663✔
4590
        updateUnix := uint64(edge.LastUpdate.Unix())
2,663✔
4591
        byteOrder.PutUint64(scratch[:], updateUnix)
2,663✔
4592
        if _, err := w.Write(scratch[:]); err != nil {
2,663✔
4593
                return err
×
4594
        }
×
4595

4596
        if err := binary.Write(w, byteOrder, edge.MessageFlags); err != nil {
2,663✔
4597
                return err
×
4598
        }
×
4599
        if err := binary.Write(w, byteOrder, edge.ChannelFlags); err != nil {
2,663✔
4600
                return err
×
4601
        }
×
4602
        if err := binary.Write(w, byteOrder, edge.TimeLockDelta); err != nil {
2,663✔
4603
                return err
×
4604
        }
×
4605
        if err := binary.Write(w, byteOrder, uint64(edge.MinHTLC)); err != nil {
2,663✔
4606
                return err
×
4607
        }
×
4608
        err = binary.Write(w, byteOrder, uint64(edge.FeeBaseMSat))
2,663✔
4609
        if err != nil {
2,663✔
4610
                return err
×
4611
        }
×
4612
        err = binary.Write(
2,663✔
4613
                w, byteOrder, uint64(edge.FeeProportionalMillionths),
2,663✔
4614
        )
2,663✔
4615
        if err != nil {
2,663✔
4616
                return err
×
4617
        }
×
4618

4619
        if _, err := w.Write(to); err != nil {
2,663✔
4620
                return err
×
4621
        }
×
4622

4623
        // If the max_htlc field is present, we write it. To be compatible with
4624
        // older versions that wasn't aware of this field, we write it as part
4625
        // of the opaque data.
4626
        // TODO(halseth): clean up when moving to TLV.
4627
        var opaqueBuf bytes.Buffer
2,663✔
4628
        if edge.MessageFlags.HasMaxHtlc() {
4,942✔
4629
                err := binary.Write(&opaqueBuf, byteOrder, uint64(edge.MaxHTLC))
2,279✔
4630
                if err != nil {
2,279✔
4631
                        return err
×
4632
                }
×
4633
        }
4634

4635
        if len(edge.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
2,663✔
4636
                return ErrTooManyExtraOpaqueBytes(len(edge.ExtraOpaqueData))
×
4637
        }
×
4638
        if _, err := opaqueBuf.Write(edge.ExtraOpaqueData); err != nil {
2,663✔
4639
                return err
×
4640
        }
×
4641

4642
        if err := wire.WriteVarBytes(w, 0, opaqueBuf.Bytes()); err != nil {
2,663✔
4643
                return err
×
4644
        }
×
4645
        return nil
2,663✔
4646
}
4647

4648
func deserializeChanEdgePolicy(r io.Reader) (*models.ChannelEdgePolicy, error) {
7,823✔
4649
        // Deserialize the policy. Note that in case an optional field is not
7,823✔
4650
        // found, both an error and a populated policy object are returned.
7,823✔
4651
        edge, deserializeErr := deserializeChanEdgePolicyRaw(r)
7,823✔
4652
        if deserializeErr != nil &&
7,823✔
4653
                deserializeErr != ErrEdgePolicyOptionalFieldNotFound {
7,823✔
4654

×
4655
                return nil, deserializeErr
×
4656
        }
×
4657

4658
        return edge, deserializeErr
7,823✔
4659
}
4660

4661
func deserializeChanEdgePolicyRaw(r io.Reader) (*models.ChannelEdgePolicy,
4662
        error) {
8,830✔
4663

8,830✔
4664
        edge := &models.ChannelEdgePolicy{}
8,830✔
4665

8,830✔
4666
        var err error
8,830✔
4667
        edge.SigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
8,830✔
4668
        if err != nil {
8,830✔
4669
                return nil, err
×
4670
        }
×
4671

4672
        if err := binary.Read(r, byteOrder, &edge.ChannelID); err != nil {
8,830✔
4673
                return nil, err
×
4674
        }
×
4675

4676
        var scratch [8]byte
8,830✔
4677
        if _, err := r.Read(scratch[:]); err != nil {
8,830✔
4678
                return nil, err
×
4679
        }
×
4680
        unix := int64(byteOrder.Uint64(scratch[:]))
8,830✔
4681
        edge.LastUpdate = time.Unix(unix, 0)
8,830✔
4682

8,830✔
4683
        if err := binary.Read(r, byteOrder, &edge.MessageFlags); err != nil {
8,830✔
4684
                return nil, err
×
4685
        }
×
4686
        if err := binary.Read(r, byteOrder, &edge.ChannelFlags); err != nil {
8,830✔
4687
                return nil, err
×
4688
        }
×
4689
        if err := binary.Read(r, byteOrder, &edge.TimeLockDelta); err != nil {
8,830✔
4690
                return nil, err
×
4691
        }
×
4692

4693
        var n uint64
8,830✔
4694
        if err := binary.Read(r, byteOrder, &n); err != nil {
8,830✔
4695
                return nil, err
×
4696
        }
×
4697
        edge.MinHTLC = lnwire.MilliSatoshi(n)
8,830✔
4698

8,830✔
4699
        if err := binary.Read(r, byteOrder, &n); err != nil {
8,830✔
4700
                return nil, err
×
4701
        }
×
4702
        edge.FeeBaseMSat = lnwire.MilliSatoshi(n)
8,830✔
4703

8,830✔
4704
        if err := binary.Read(r, byteOrder, &n); err != nil {
8,830✔
4705
                return nil, err
×
4706
        }
×
4707
        edge.FeeProportionalMillionths = lnwire.MilliSatoshi(n)
8,830✔
4708

8,830✔
4709
        if _, err := r.Read(edge.ToNode[:]); err != nil {
8,830✔
4710
                return nil, err
×
4711
        }
×
4712

4713
        // We'll try and see if there are any opaque bytes left, if not, then
4714
        // we'll ignore the EOF error and return the edge as is.
4715
        edge.ExtraOpaqueData, err = wire.ReadVarBytes(
8,830✔
4716
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
8,830✔
4717
        )
8,830✔
4718
        switch {
8,830✔
4719
        case err == io.ErrUnexpectedEOF:
×
4720
        case err == io.EOF:
3✔
4721
        case err != nil:
×
4722
                return nil, err
×
4723
        }
4724

4725
        // See if optional fields are present.
4726
        if edge.MessageFlags.HasMaxHtlc() {
17,286✔
4727
                // The max_htlc field should be at the beginning of the opaque
8,456✔
4728
                // bytes.
8,456✔
4729
                opq := edge.ExtraOpaqueData
8,456✔
4730

8,456✔
4731
                // If the max_htlc field is not present, it might be old data
8,456✔
4732
                // stored before this field was validated. We'll return the
8,456✔
4733
                // edge along with an error.
8,456✔
4734
                if len(opq) < 8 {
8,459✔
4735
                        return edge, ErrEdgePolicyOptionalFieldNotFound
3✔
4736
                }
3✔
4737

4738
                maxHtlc := byteOrder.Uint64(opq[:8])
8,453✔
4739
                edge.MaxHTLC = lnwire.MilliSatoshi(maxHtlc)
8,453✔
4740

8,453✔
4741
                // Exclude the parsed field from the rest of the opaque data.
8,453✔
4742
                edge.ExtraOpaqueData = opq[8:]
8,453✔
4743
        }
4744

4745
        return edge, nil
8,827✔
4746
}
4747

4748
// chanGraphNodeTx is an implementation of the NodeRTx interface backed by the
4749
// ChannelGraph and a kvdb.RTx.
4750
type chanGraphNodeTx struct {
4751
        tx   kvdb.RTx
4752
        db   *ChannelGraph
4753
        node *models.LightningNode
4754
}
4755

4756
// A compile-time constraint to ensure chanGraphNodeTx implements the NodeRTx
4757
// interface.
4758
var _ NodeRTx = (*chanGraphNodeTx)(nil)
4759

4760
func newChanGraphNodeTx(tx kvdb.RTx, db *ChannelGraph,
4761
        node *models.LightningNode) *chanGraphNodeTx {
3,915✔
4762

3,915✔
4763
        return &chanGraphNodeTx{
3,915✔
4764
                tx:   tx,
3,915✔
4765
                db:   db,
3,915✔
4766
                node: node,
3,915✔
4767
        }
3,915✔
4768
}
3,915✔
4769

4770
// Node returns the raw information of the node.
4771
//
4772
// NOTE: This is a part of the NodeRTx interface.
4773
func (c *chanGraphNodeTx) Node() *models.LightningNode {
4,840✔
4774
        return c.node
4,840✔
4775
}
4,840✔
4776

4777
// FetchNode fetches the node with the given pub key under the same transaction
4778
// used to fetch the current node. The returned node is also a NodeRTx and any
4779
// operations on that NodeRTx will also be done under the same transaction.
4780
//
4781
// NOTE: This is a part of the NodeRTx interface.
4782
func (c *chanGraphNodeTx) FetchNode(nodePub route.Vertex) (NodeRTx, error) {
2,944✔
4783
        node, err := c.db.FetchLightningNodeTx(c.tx, nodePub)
2,944✔
4784
        if err != nil {
2,944✔
4785
                return nil, err
×
4786
        }
×
4787

4788
        return newChanGraphNodeTx(c.tx, c.db, node), nil
2,944✔
4789
}
4790

4791
// ForEachChannel can be used to iterate over the node's channels under
4792
// the same transaction used to fetch the node.
4793
//
4794
// NOTE: This is a part of the NodeRTx interface.
4795
func (c *chanGraphNodeTx) ForEachChannel(f func(*models.ChannelEdgeInfo,
4796
        *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
965✔
4797

965✔
4798
        return c.db.ForEachNodeChannelTx(c.tx, c.node.PubKeyBytes,
965✔
4799
                func(_ kvdb.RTx, info *models.ChannelEdgeInfo, policy1,
965✔
4800
                        policy2 *models.ChannelEdgePolicy) error {
3,909✔
4801

2,944✔
4802
                        return f(info, policy1, policy2)
2,944✔
4803
                },
2,944✔
4804
        )
4805
}
4806

4807
// MakeTestGraph creates a new instance of the ChannelGraph for testing
4808
// purposes.
4809
func MakeTestGraph(t testing.TB, modifiers ...OptionModifier) (*ChannelGraph,
4810
        error) {
40✔
4811

40✔
4812
        opts := DefaultOptions()
40✔
4813
        for _, modifier := range modifiers {
40✔
4814
                modifier(opts)
×
4815
        }
×
4816

4817
        // Next, create channelgraph for the first time.
4818
        backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
40✔
4819
        if err != nil {
40✔
4820
                backendCleanup()
×
4821
                return nil, err
×
4822
        }
×
4823

4824
        graph, err := NewChannelGraph(backend)
40✔
4825
        if err != nil {
40✔
4826
                backendCleanup()
×
4827
                return nil, err
×
4828
        }
×
4829

4830
        t.Cleanup(func() {
80✔
4831
                _ = backend.Close()
40✔
4832
                backendCleanup()
40✔
4833
        })
40✔
4834

4835
        return graph, nil
40✔
4836
}
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