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

lightningnetwork / lnd / 13035292482

29 Jan 2025 03:59PM UTC coverage: 49.3% (-9.5%) from 58.777%
13035292482

Pull #9456

github

mohamedawnallah
docs: update release-notes-0.19.0.md

In this commit, we warn users about the removal
of RPCs `SendToRoute`, `SendToRouteSync`, `SendPayment`,
and `SendPaymentSync` in the next release 0.20.
Pull Request #9456: lnrpc+docs: deprecate warning `SendToRoute`, `SendToRouteSync`, `SendPayment`, and `SendPaymentSync` in Release 0.19

100634 of 204126 relevant lines covered (49.3%)

1.54 hits per line

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

68.13
/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/lightningnetwork/lnd/aliasmgr"
22
        "github.com/lightningnetwork/lnd/batch"
23
        "github.com/lightningnetwork/lnd/graph/db/models"
24
        "github.com/lightningnetwork/lnd/input"
25
        "github.com/lightningnetwork/lnd/kvdb"
26
        "github.com/lightningnetwork/lnd/lnwire"
27
        "github.com/lightningnetwork/lnd/routing/route"
28
)
29

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3✔
203
        opts := DefaultOptions()
3✔
204
        for _, o := range options {
6✔
205
                o(opts)
3✔
206
        }
3✔
207

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

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

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

3✔
234
                err := g.ForEachNodeCacheable(
3✔
235
                        func(tx kvdb.RTx, node GraphCacheNode) error {
6✔
236
                                g.graphCache.AddNodeFeatures(node)
3✔
237

3✔
238
                                return nil
3✔
239
                        },
3✔
240
                )
241
                if err != nil {
3✔
242
                        return nil, err
×
243
                }
×
244

245
                err = g.ForEachChannel(func(info *models.ChannelEdgeInfo,
3✔
246
                        policy1, policy2 *models.ChannelEdgePolicy) error {
6✔
247

3✔
248
                        g.graphCache.AddChannel(info, policy1, policy2)
3✔
249

3✔
250
                        return nil
3✔
251
                })
3✔
252
                if err != nil {
3✔
253
                        return nil, err
×
254
                }
×
255

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

260
        return g, nil
3✔
261
}
262

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

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

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

3✔
277
        err := kvdb.ForAll(edges, func(k, edgeBytes []byte) error {
6✔
278
                // Skip embedded buckets.
3✔
279
                if bytes.Equal(k, edgeIndexBucket) ||
3✔
280
                        bytes.Equal(k, edgeUpdateIndexBucket) ||
3✔
281
                        bytes.Equal(k, zombieBucket) ||
3✔
282
                        bytes.Equal(k, disabledEdgePolicyBucket) ||
3✔
283
                        bytes.Equal(k, channelPointBucket) {
6✔
284

3✔
285
                        return nil
3✔
286
                }
3✔
287

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

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

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

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

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

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

317
                channelMap[key] = edge
3✔
318

3✔
319
                return nil
3✔
320
        })
321
        if err != nil {
3✔
322
                return nil, err
×
323
        }
×
324

325
        return channelMap, nil
3✔
326
}
327

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

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

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

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

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

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

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

403
        return nil
3✔
404
}
405

406
// NewPathFindTx returns a new read transaction that can be used for a single
407
// path finding session. Will return nil if the graph cache is enabled.
408
func (c *ChannelGraph) NewPathFindTx() (kvdb.RTx, error) {
3✔
409
        if c.graphCache != nil {
6✔
410
                return nil, nil
3✔
411
        }
3✔
412

413
        return c.db.BeginReadTx()
×
414
}
415

416
// AddrsForNode returns all known addresses for the target node public key that
417
// the graph DB is aware of. The returned boolean indicates if the given node is
418
// unknown to the graph DB or not.
419
//
420
// NOTE: this is part of the channeldb.AddrSource interface.
421
func (c *ChannelGraph) AddrsForNode(nodePub *btcec.PublicKey) (bool, []net.Addr,
422
        error) {
3✔
423

3✔
424
        pubKey, err := route.NewVertexFromBytes(nodePub.SerializeCompressed())
3✔
425
        if err != nil {
3✔
426
                return false, nil, err
×
427
        }
×
428

429
        node, err := c.FetchLightningNode(pubKey)
3✔
430
        // We don't consider it an error if the graph is unaware of the node.
3✔
431
        switch {
3✔
432
        case err != nil && !errors.Is(err, ErrGraphNodeNotFound):
×
433
                return false, nil, err
×
434

435
        case errors.Is(err, ErrGraphNodeNotFound):
3✔
436
                return false, nil, nil
3✔
437
        }
438

439
        return true, node.Addresses, nil
3✔
440
}
441

442
// ForEachChannel iterates through all the channel edges stored within the
443
// graph and invokes the passed callback for each edge. The callback takes two
444
// edges as since this is a directed graph, both the in/out edges are visited.
445
// If the callback returns an error, then the transaction is aborted and the
446
// iteration stops early.
447
//
448
// NOTE: If an edge can't be found, or wasn't advertised, then a nil pointer
449
// for that particular channel edge routing policy will be passed into the
450
// callback.
451
func (c *ChannelGraph) ForEachChannel(cb func(*models.ChannelEdgeInfo,
452
        *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
3✔
453

3✔
454
        return c.db.View(func(tx kvdb.RTx) error {
6✔
455
                edges := tx.ReadBucket(edgeBucket)
3✔
456
                if edges == nil {
3✔
457
                        return ErrGraphNoEdgesFound
×
458
                }
×
459

460
                // First, load all edges in memory indexed by node and channel
461
                // id.
462
                channelMap, err := c.getChannelMap(edges)
3✔
463
                if err != nil {
3✔
464
                        return err
×
465
                }
×
466

467
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
468
                if edgeIndex == nil {
3✔
469
                        return ErrGraphNoEdgesFound
×
470
                }
×
471

472
                // Load edge index, recombine each channel with the policies
473
                // loaded above and invoke the callback.
474
                return kvdb.ForAll(
3✔
475
                        edgeIndex, func(k, edgeInfoBytes []byte) error {
6✔
476
                                var chanID [8]byte
3✔
477
                                copy(chanID[:], k)
3✔
478

3✔
479
                                edgeInfoReader := bytes.NewReader(edgeInfoBytes)
3✔
480
                                info, err := deserializeChanEdgeInfo(
3✔
481
                                        edgeInfoReader,
3✔
482
                                )
3✔
483
                                if err != nil {
3✔
484
                                        return err
×
485
                                }
×
486

487
                                policy1 := channelMap[channelMapKey{
3✔
488
                                        nodeKey: info.NodeKey1Bytes,
3✔
489
                                        chanID:  chanID,
3✔
490
                                }]
3✔
491

3✔
492
                                policy2 := channelMap[channelMapKey{
3✔
493
                                        nodeKey: info.NodeKey2Bytes,
3✔
494
                                        chanID:  chanID,
3✔
495
                                }]
3✔
496

3✔
497
                                return cb(&info, policy1, policy2)
3✔
498
                        },
499
                )
500
        }, func() {})
3✔
501
}
502

503
// ForEachNodeDirectedChannel iterates through all channels of a given node,
504
// executing the passed callback on the directed edge representing the channel
505
// and its incoming policy. If the callback returns an error, then the iteration
506
// is halted with the error propagated back up to the caller.
507
//
508
// Unknown policies are passed into the callback as nil values.
509
func (c *ChannelGraph) ForEachNodeDirectedChannel(tx kvdb.RTx,
510
        node route.Vertex, cb func(channel *DirectedChannel) error) error {
3✔
511

3✔
512
        if c.graphCache != nil {
6✔
513
                return c.graphCache.ForEachChannel(node, cb)
3✔
514
        }
3✔
515

516
        // Fallback that uses the database.
517
        toNodeCallback := func() route.Vertex {
6✔
518
                return node
3✔
519
        }
3✔
520
        toNodeFeatures, err := c.FetchNodeFeatures(node)
3✔
521
        if err != nil {
3✔
522
                return err
×
523
        }
×
524

525
        dbCallback := func(tx kvdb.RTx, e *models.ChannelEdgeInfo, p1,
3✔
526
                p2 *models.ChannelEdgePolicy) error {
6✔
527

3✔
528
                var cachedInPolicy *models.CachedEdgePolicy
3✔
529
                if p2 != nil {
6✔
530
                        cachedInPolicy = models.NewCachedPolicy(p2)
3✔
531
                        cachedInPolicy.ToNodePubKey = toNodeCallback
3✔
532
                        cachedInPolicy.ToNodeFeatures = toNodeFeatures
3✔
533
                }
3✔
534

535
                var inboundFee lnwire.Fee
3✔
536
                if p1 != nil {
6✔
537
                        // Extract inbound fee. If there is a decoding error,
3✔
538
                        // skip this edge.
3✔
539
                        _, err := p1.ExtraOpaqueData.ExtractRecords(&inboundFee)
3✔
540
                        if err != nil {
3✔
541
                                return nil
×
542
                        }
×
543
                }
544

545
                directedChannel := &DirectedChannel{
3✔
546
                        ChannelID:    e.ChannelID,
3✔
547
                        IsNode1:      node == e.NodeKey1Bytes,
3✔
548
                        OtherNode:    e.NodeKey2Bytes,
3✔
549
                        Capacity:     e.Capacity,
3✔
550
                        OutPolicySet: p1 != nil,
3✔
551
                        InPolicy:     cachedInPolicy,
3✔
552
                        InboundFee:   inboundFee,
3✔
553
                }
3✔
554

3✔
555
                if node == e.NodeKey2Bytes {
6✔
556
                        directedChannel.OtherNode = e.NodeKey1Bytes
3✔
557
                }
3✔
558

559
                return cb(directedChannel)
3✔
560
        }
561
        return nodeTraversal(tx, node[:], c.db, dbCallback)
3✔
562
}
563

564
// FetchNodeFeatures returns the features of a given node. If no features are
565
// known for the node, an empty feature vector is returned.
566
func (c *ChannelGraph) FetchNodeFeatures(
567
        node route.Vertex) (*lnwire.FeatureVector, error) {
3✔
568

3✔
569
        if c.graphCache != nil {
6✔
570
                return c.graphCache.GetFeatures(node), nil
3✔
571
        }
3✔
572

573
        // Fallback that uses the database.
574
        targetNode, err := c.FetchLightningNode(node)
3✔
575
        switch err {
3✔
576
        // If the node exists and has features, return them directly.
577
        case nil:
3✔
578
                return targetNode.Features, nil
3✔
579

580
        // If we couldn't find a node announcement, populate a blank feature
581
        // vector.
582
        case ErrGraphNodeNotFound:
×
583
                return lnwire.EmptyFeatureVector(), nil
×
584

585
        // Otherwise, bubble the error up.
586
        default:
×
587
                return nil, err
×
588
        }
589
}
590

591
// ForEachNodeCached is similar to ForEachNode, but it utilizes the channel
592
// graph cache instead. Note that this doesn't return all the information the
593
// regular ForEachNode method does.
594
//
595
// NOTE: The callback contents MUST not be modified.
596
func (c *ChannelGraph) ForEachNodeCached(cb func(node route.Vertex,
597
        chans map[uint64]*DirectedChannel) error) error {
×
598

×
599
        if c.graphCache != nil {
×
600
                return c.graphCache.ForEachNode(cb)
×
601
        }
×
602

603
        // Otherwise call back to a version that uses the database directly.
604
        // We'll iterate over each node, then the set of channels for each
605
        // node, and construct a similar callback functiopn signature as the
606
        // main funcotin expects.
607
        return c.ForEachNode(func(tx kvdb.RTx,
×
608
                node *models.LightningNode) error {
×
609

×
610
                channels := make(map[uint64]*DirectedChannel)
×
611

×
612
                err := c.ForEachNodeChannelTx(tx, node.PubKeyBytes,
×
613
                        func(tx kvdb.RTx, e *models.ChannelEdgeInfo,
×
614
                                p1 *models.ChannelEdgePolicy,
×
615
                                p2 *models.ChannelEdgePolicy) error {
×
616

×
617
                                toNodeCallback := func() route.Vertex {
×
618
                                        return node.PubKeyBytes
×
619
                                }
×
620
                                toNodeFeatures, err := c.FetchNodeFeatures(
×
621
                                        node.PubKeyBytes,
×
622
                                )
×
623
                                if err != nil {
×
624
                                        return err
×
625
                                }
×
626

627
                                var cachedInPolicy *models.CachedEdgePolicy
×
628
                                if p2 != nil {
×
629
                                        cachedInPolicy =
×
630
                                                models.NewCachedPolicy(p2)
×
631
                                        cachedInPolicy.ToNodePubKey =
×
632
                                                toNodeCallback
×
633
                                        cachedInPolicy.ToNodeFeatures =
×
634
                                                toNodeFeatures
×
635
                                }
×
636

637
                                directedChannel := &DirectedChannel{
×
638
                                        ChannelID: e.ChannelID,
×
639
                                        IsNode1: node.PubKeyBytes ==
×
640
                                                e.NodeKey1Bytes,
×
641
                                        OtherNode:    e.NodeKey2Bytes,
×
642
                                        Capacity:     e.Capacity,
×
643
                                        OutPolicySet: p1 != nil,
×
644
                                        InPolicy:     cachedInPolicy,
×
645
                                }
×
646

×
647
                                if node.PubKeyBytes == e.NodeKey2Bytes {
×
648
                                        directedChannel.OtherNode =
×
649
                                                e.NodeKey1Bytes
×
650
                                }
×
651

652
                                channels[e.ChannelID] = directedChannel
×
653

×
654
                                return nil
×
655
                        })
656
                if err != nil {
×
657
                        return err
×
658
                }
×
659

660
                return cb(node.PubKeyBytes, channels)
×
661
        })
662
}
663

664
// DisabledChannelIDs returns the channel ids of disabled channels.
665
// A channel is disabled when two of the associated ChanelEdgePolicies
666
// have their disabled bit on.
667
func (c *ChannelGraph) DisabledChannelIDs() ([]uint64, error) {
×
668
        var disabledChanIDs []uint64
×
669
        var chanEdgeFound map[uint64]struct{}
×
670

×
671
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
×
672
                edges := tx.ReadBucket(edgeBucket)
×
673
                if edges == nil {
×
674
                        return ErrGraphNoEdgesFound
×
675
                }
×
676

677
                disabledEdgePolicyIndex := edges.NestedReadBucket(
×
678
                        disabledEdgePolicyBucket,
×
679
                )
×
680
                if disabledEdgePolicyIndex == nil {
×
681
                        return nil
×
682
                }
×
683

684
                // We iterate over all disabled policies and we add each channel
685
                // that has more than one disabled policy to disabledChanIDs
686
                // array.
687
                return disabledEdgePolicyIndex.ForEach(
×
688
                        func(k, v []byte) error {
×
689
                                chanID := byteOrder.Uint64(k[:8])
×
690
                                _, edgeFound := chanEdgeFound[chanID]
×
691
                                if edgeFound {
×
692
                                        delete(chanEdgeFound, chanID)
×
693
                                        disabledChanIDs = append(
×
694
                                                disabledChanIDs, chanID,
×
695
                                        )
×
696

×
697
                                        return nil
×
698
                                }
×
699

700
                                chanEdgeFound[chanID] = struct{}{}
×
701

×
702
                                return nil
×
703
                        },
704
                )
705
        }, func() {
×
706
                disabledChanIDs = nil
×
707
                chanEdgeFound = make(map[uint64]struct{})
×
708
        })
×
709
        if err != nil {
×
710
                return nil, err
×
711
        }
×
712

713
        return disabledChanIDs, nil
×
714
}
715

716
// ForEachNode iterates through all the stored vertices/nodes in the graph,
717
// executing the passed callback with each node encountered. If the callback
718
// returns an error, then the transaction is aborted and the iteration stops
719
// early.
720
//
721
// TODO(roasbeef): add iterator interface to allow for memory efficient graph
722
// traversal when graph gets mega
723
func (c *ChannelGraph) ForEachNode(
724
        cb func(kvdb.RTx, *models.LightningNode) error) error {
3✔
725

3✔
726
        traversal := func(tx kvdb.RTx) error {
6✔
727
                // First grab the nodes bucket which stores the mapping from
3✔
728
                // pubKey to node information.
3✔
729
                nodes := tx.ReadBucket(nodeBucket)
3✔
730
                if nodes == nil {
3✔
731
                        return ErrGraphNotFound
×
732
                }
×
733

734
                return nodes.ForEach(func(pubKey, nodeBytes []byte) error {
6✔
735
                        // If this is the source key, then we skip this
3✔
736
                        // iteration as the value for this key is a pubKey
3✔
737
                        // rather than raw node information.
3✔
738
                        if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
6✔
739
                                return nil
3✔
740
                        }
3✔
741

742
                        nodeReader := bytes.NewReader(nodeBytes)
3✔
743
                        node, err := deserializeLightningNode(nodeReader)
3✔
744
                        if err != nil {
3✔
745
                                return err
×
746
                        }
×
747

748
                        // Execute the callback, the transaction will abort if
749
                        // this returns an error.
750
                        return cb(tx, &node)
3✔
751
                })
752
        }
753

754
        return kvdb.View(c.db, traversal, func() {})
6✔
755
}
756

757
// ForEachNodeCacheable iterates through all the stored vertices/nodes in the
758
// graph, executing the passed callback with each node encountered. If the
759
// callback returns an error, then the transaction is aborted and the iteration
760
// stops early.
761
func (c *ChannelGraph) ForEachNodeCacheable(cb func(kvdb.RTx,
762
        GraphCacheNode) error) error {
3✔
763

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

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

780
                        nodeReader := bytes.NewReader(nodeBytes)
3✔
781
                        cacheableNode, err := deserializeLightningNodeCacheable(
3✔
782
                                nodeReader,
3✔
783
                        )
3✔
784
                        if err != nil {
3✔
785
                                return err
×
786
                        }
×
787

788
                        // Execute the callback, the transaction will abort if
789
                        // this returns an error.
790
                        return cb(tx, cacheableNode)
3✔
791
                })
792
        }
793

794
        return kvdb.View(c.db, traversal, func() {})
6✔
795
}
796

797
// SourceNode returns the source node of the graph. The source node is treated
798
// as the center node within a star-graph. This method may be used to kick off
799
// a path finding algorithm in order to explore the reachability of another
800
// node based off the source node.
801
func (c *ChannelGraph) SourceNode() (*models.LightningNode, error) {
3✔
802
        var source *models.LightningNode
3✔
803
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
804
                // First grab the nodes bucket which stores the mapping from
3✔
805
                // pubKey to node information.
3✔
806
                nodes := tx.ReadBucket(nodeBucket)
3✔
807
                if nodes == nil {
3✔
808
                        return ErrGraphNotFound
×
809
                }
×
810

811
                node, err := c.sourceNode(nodes)
3✔
812
                if err != nil {
3✔
813
                        return err
×
814
                }
×
815
                source = node
3✔
816

3✔
817
                return nil
3✔
818
        }, func() {
3✔
819
                source = nil
3✔
820
        })
3✔
821
        if err != nil {
3✔
822
                return nil, err
×
823
        }
×
824

825
        return source, nil
3✔
826
}
827

828
// sourceNode uses an existing database transaction and returns the source node
829
// of the graph. The source node is treated as the center node within a
830
// star-graph. This method may be used to kick off a path finding algorithm in
831
// order to explore the reachability of another node based off the source node.
832
func (c *ChannelGraph) sourceNode(nodes kvdb.RBucket) (*models.LightningNode,
833
        error) {
3✔
834

3✔
835
        selfPub := nodes.Get(sourceKey)
3✔
836
        if selfPub == nil {
3✔
837
                return nil, ErrSourceNodeNotSet
×
838
        }
×
839

840
        // With the pubKey of the source node retrieved, we're able to
841
        // fetch the full node information.
842
        node, err := fetchLightningNode(nodes, selfPub)
3✔
843
        if err != nil {
3✔
844
                return nil, err
×
845
        }
×
846

847
        return &node, nil
3✔
848
}
849

850
// SetSourceNode sets the source node within the graph database. The source
851
// node is to be used as the center of a star-graph within path finding
852
// algorithms.
853
func (c *ChannelGraph) SetSourceNode(node *models.LightningNode) error {
3✔
854
        nodePubBytes := node.PubKeyBytes[:]
3✔
855

3✔
856
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
857
                // First grab the nodes bucket which stores the mapping from
3✔
858
                // pubKey to node information.
3✔
859
                nodes, err := tx.CreateTopLevelBucket(nodeBucket)
3✔
860
                if err != nil {
3✔
861
                        return err
×
862
                }
×
863

864
                // Next we create the mapping from source to the targeted
865
                // public key.
866
                if err := nodes.Put(sourceKey, nodePubBytes); err != nil {
3✔
867
                        return err
×
868
                }
×
869

870
                // Finally, we commit the information of the lightning node
871
                // itself.
872
                return addLightningNode(tx, node)
3✔
873
        }, func() {})
3✔
874
}
875

876
// AddLightningNode adds a vertex/node to the graph database. If the node is not
877
// in the database from before, this will add a new, unconnected one to the
878
// graph. If it is present from before, this will update that node's
879
// information. Note that this method is expected to only be called to update an
880
// already present node from a node announcement, or to insert a node found in a
881
// channel update.
882
//
883
// TODO(roasbeef): also need sig of announcement
884
func (c *ChannelGraph) AddLightningNode(node *models.LightningNode,
885
        op ...batch.SchedulerOption) error {
3✔
886

3✔
887
        r := &batch.Request{
3✔
888
                Update: func(tx kvdb.RwTx) error {
6✔
889
                        if c.graphCache != nil {
6✔
890
                                cNode := newGraphCacheNode(
3✔
891
                                        node.PubKeyBytes, node.Features,
3✔
892
                                )
3✔
893
                                err := c.graphCache.AddNode(tx, cNode)
3✔
894
                                if err != nil {
3✔
895
                                        return err
×
896
                                }
×
897
                        }
898

899
                        return addLightningNode(tx, node)
3✔
900
                },
901
        }
902

903
        for _, f := range op {
6✔
904
                f(r)
3✔
905
        }
3✔
906

907
        return c.nodeScheduler.Execute(r)
3✔
908
}
909

910
func addLightningNode(tx kvdb.RwTx, node *models.LightningNode) error {
3✔
911
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
3✔
912
        if err != nil {
3✔
913
                return err
×
914
        }
×
915

916
        aliases, err := nodes.CreateBucketIfNotExists(aliasIndexBucket)
3✔
917
        if err != nil {
3✔
918
                return err
×
919
        }
×
920

921
        updateIndex, err := nodes.CreateBucketIfNotExists(
3✔
922
                nodeUpdateIndexBucket,
3✔
923
        )
3✔
924
        if err != nil {
3✔
925
                return err
×
926
        }
×
927

928
        return putLightningNode(nodes, aliases, updateIndex, node)
3✔
929
}
930

931
// LookupAlias attempts to return the alias as advertised by the target node.
932
// TODO(roasbeef): currently assumes that aliases are unique...
933
func (c *ChannelGraph) LookupAlias(pub *btcec.PublicKey) (string, error) {
3✔
934
        var alias string
3✔
935

3✔
936
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
937
                nodes := tx.ReadBucket(nodeBucket)
3✔
938
                if nodes == nil {
3✔
939
                        return ErrGraphNodesNotFound
×
940
                }
×
941

942
                aliases := nodes.NestedReadBucket(aliasIndexBucket)
3✔
943
                if aliases == nil {
3✔
944
                        return ErrGraphNodesNotFound
×
945
                }
×
946

947
                nodePub := pub.SerializeCompressed()
3✔
948
                a := aliases.Get(nodePub)
3✔
949
                if a == nil {
3✔
950
                        return ErrNodeAliasNotFound
×
951
                }
×
952

953
                // TODO(roasbeef): should actually be using the utf-8
954
                // package...
955
                alias = string(a)
3✔
956
                return nil
3✔
957
        }, func() {
3✔
958
                alias = ""
3✔
959
        })
3✔
960
        if err != nil {
3✔
961
                return "", err
×
962
        }
×
963

964
        return alias, nil
3✔
965
}
966

967
// DeleteLightningNode starts a new database transaction to remove a vertex/node
968
// from the database according to the node's public key.
969
func (c *ChannelGraph) DeleteLightningNode(nodePub route.Vertex) error {
×
970
        // TODO(roasbeef): ensure dangling edges are removed...
×
971
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
×
972
                nodes := tx.ReadWriteBucket(nodeBucket)
×
973
                if nodes == nil {
×
974
                        return ErrGraphNodeNotFound
×
975
                }
×
976

977
                if c.graphCache != nil {
×
978
                        c.graphCache.RemoveNode(nodePub)
×
979
                }
×
980

981
                return c.deleteLightningNode(nodes, nodePub[:])
×
982
        }, func() {})
×
983
}
984

985
// deleteLightningNode uses an existing database transaction to remove a
986
// vertex/node from the database according to the node's public key.
987
func (c *ChannelGraph) deleteLightningNode(nodes kvdb.RwBucket,
988
        compressedPubKey []byte) error {
3✔
989

3✔
990
        aliases := nodes.NestedReadWriteBucket(aliasIndexBucket)
3✔
991
        if aliases == nil {
3✔
992
                return ErrGraphNodesNotFound
×
993
        }
×
994

995
        if err := aliases.Delete(compressedPubKey); err != nil {
3✔
996
                return err
×
997
        }
×
998

999
        // Before we delete the node, we'll fetch its current state so we can
1000
        // determine when its last update was to clear out the node update
1001
        // index.
1002
        node, err := fetchLightningNode(nodes, compressedPubKey)
3✔
1003
        if err != nil {
3✔
1004
                return err
×
1005
        }
×
1006

1007
        if err := nodes.Delete(compressedPubKey); err != nil {
3✔
1008
                return err
×
1009
        }
×
1010

1011
        // Finally, we'll delete the index entry for the node within the
1012
        // nodeUpdateIndexBucket as this node is no longer active, so we don't
1013
        // need to track its last update.
1014
        nodeUpdateIndex := nodes.NestedReadWriteBucket(nodeUpdateIndexBucket)
3✔
1015
        if nodeUpdateIndex == nil {
3✔
1016
                return ErrGraphNodesNotFound
×
1017
        }
×
1018

1019
        // In order to delete the entry, we'll need to reconstruct the key for
1020
        // its last update.
1021
        updateUnix := uint64(node.LastUpdate.Unix())
3✔
1022
        var indexKey [8 + 33]byte
3✔
1023
        byteOrder.PutUint64(indexKey[:8], updateUnix)
3✔
1024
        copy(indexKey[8:], compressedPubKey)
3✔
1025

3✔
1026
        return nodeUpdateIndex.Delete(indexKey[:])
3✔
1027
}
1028

1029
// AddChannelEdge adds a new (undirected, blank) edge to the graph database. An
1030
// undirected edge from the two target nodes are created. The information stored
1031
// denotes the static attributes of the channel, such as the channelID, the keys
1032
// involved in creation of the channel, and the set of features that the channel
1033
// supports. The chanPoint and chanID are used to uniquely identify the edge
1034
// globally within the database.
1035
func (c *ChannelGraph) AddChannelEdge(edge *models.ChannelEdgeInfo,
1036
        op ...batch.SchedulerOption) error {
3✔
1037

3✔
1038
        var alreadyExists bool
3✔
1039
        r := &batch.Request{
3✔
1040
                Reset: func() {
6✔
1041
                        alreadyExists = false
3✔
1042
                },
3✔
1043
                Update: func(tx kvdb.RwTx) error {
3✔
1044
                        err := c.addChannelEdge(tx, edge)
3✔
1045

3✔
1046
                        // Silence ErrEdgeAlreadyExist so that the batch can
3✔
1047
                        // succeed, but propagate the error via local state.
3✔
1048
                        if err == ErrEdgeAlreadyExist {
3✔
1049
                                alreadyExists = true
×
1050
                                return nil
×
1051
                        }
×
1052

1053
                        return err
3✔
1054
                },
1055
                OnCommit: func(err error) error {
3✔
1056
                        switch {
3✔
1057
                        case err != nil:
×
1058
                                return err
×
1059
                        case alreadyExists:
×
1060
                                return ErrEdgeAlreadyExist
×
1061
                        default:
3✔
1062
                                c.rejectCache.remove(edge.ChannelID)
3✔
1063
                                c.chanCache.remove(edge.ChannelID)
3✔
1064
                                return nil
3✔
1065
                        }
1066
                },
1067
        }
1068

1069
        for _, f := range op {
6✔
1070
                if f == nil {
3✔
1071
                        return fmt.Errorf("nil scheduler option was used")
×
1072
                }
×
1073

1074
                f(r)
3✔
1075
        }
1076

1077
        return c.chanScheduler.Execute(r)
3✔
1078
}
1079

1080
// addChannelEdge is the private form of AddChannelEdge that allows callers to
1081
// utilize an existing db transaction.
1082
func (c *ChannelGraph) addChannelEdge(tx kvdb.RwTx,
1083
        edge *models.ChannelEdgeInfo) error {
3✔
1084

3✔
1085
        // Construct the channel's primary key which is the 8-byte channel ID.
3✔
1086
        var chanKey [8]byte
3✔
1087
        binary.BigEndian.PutUint64(chanKey[:], edge.ChannelID)
3✔
1088

3✔
1089
        nodes, err := tx.CreateTopLevelBucket(nodeBucket)
3✔
1090
        if err != nil {
3✔
1091
                return err
×
1092
        }
×
1093
        edges, err := tx.CreateTopLevelBucket(edgeBucket)
3✔
1094
        if err != nil {
3✔
1095
                return err
×
1096
        }
×
1097
        edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
3✔
1098
        if err != nil {
3✔
1099
                return err
×
1100
        }
×
1101
        chanIndex, err := edges.CreateBucketIfNotExists(channelPointBucket)
3✔
1102
        if err != nil {
3✔
1103
                return err
×
1104
        }
×
1105

1106
        // First, attempt to check if this edge has already been created. If
1107
        // so, then we can exit early as this method is meant to be idempotent.
1108
        if edgeInfo := edgeIndex.Get(chanKey[:]); edgeInfo != nil {
3✔
1109
                return ErrEdgeAlreadyExist
×
1110
        }
×
1111

1112
        if c.graphCache != nil {
6✔
1113
                c.graphCache.AddChannel(edge, nil, nil)
3✔
1114
        }
3✔
1115

1116
        // Before we insert the channel into the database, we'll ensure that
1117
        // both nodes already exist in the channel graph. If either node
1118
        // doesn't, then we'll insert a "shell" node that just includes its
1119
        // public key, so subsequent validation and queries can work properly.
1120
        _, node1Err := fetchLightningNode(nodes, edge.NodeKey1Bytes[:])
3✔
1121
        switch {
3✔
1122
        case node1Err == ErrGraphNodeNotFound:
3✔
1123
                node1Shell := models.LightningNode{
3✔
1124
                        PubKeyBytes:          edge.NodeKey1Bytes,
3✔
1125
                        HaveNodeAnnouncement: false,
3✔
1126
                }
3✔
1127
                err := addLightningNode(tx, &node1Shell)
3✔
1128
                if err != nil {
3✔
1129
                        return fmt.Errorf("unable to create shell node "+
×
1130
                                "for: %x: %w", edge.NodeKey1Bytes, err)
×
1131
                }
×
1132
        case node1Err != nil:
×
1133
                return node1Err
×
1134
        }
1135

1136
        _, node2Err := fetchLightningNode(nodes, edge.NodeKey2Bytes[:])
3✔
1137
        switch {
3✔
1138
        case node2Err == ErrGraphNodeNotFound:
3✔
1139
                node2Shell := models.LightningNode{
3✔
1140
                        PubKeyBytes:          edge.NodeKey2Bytes,
3✔
1141
                        HaveNodeAnnouncement: false,
3✔
1142
                }
3✔
1143
                err := addLightningNode(tx, &node2Shell)
3✔
1144
                if err != nil {
3✔
1145
                        return fmt.Errorf("unable to create shell node "+
×
1146
                                "for: %x: %w", edge.NodeKey2Bytes, err)
×
1147
                }
×
1148
        case node2Err != nil:
×
1149
                return node2Err
×
1150
        }
1151

1152
        // If the edge hasn't been created yet, then we'll first add it to the
1153
        // edge index in order to associate the edge between two nodes and also
1154
        // store the static components of the channel.
1155
        if err := putChanEdgeInfo(edgeIndex, edge, chanKey); err != nil {
3✔
1156
                return err
×
1157
        }
×
1158

1159
        // Mark edge policies for both sides as unknown. This is to enable
1160
        // efficient incoming channel lookup for a node.
1161
        keys := []*[33]byte{
3✔
1162
                &edge.NodeKey1Bytes,
3✔
1163
                &edge.NodeKey2Bytes,
3✔
1164
        }
3✔
1165
        for _, key := range keys {
6✔
1166
                err := putChanEdgePolicyUnknown(edges, edge.ChannelID, key[:])
3✔
1167
                if err != nil {
3✔
1168
                        return err
×
1169
                }
×
1170
        }
1171

1172
        // Finally we add it to the channel index which maps channel points
1173
        // (outpoints) to the shorter channel ID's.
1174
        var b bytes.Buffer
3✔
1175
        if err := WriteOutpoint(&b, &edge.ChannelPoint); err != nil {
3✔
1176
                return err
×
1177
        }
×
1178
        return chanIndex.Put(b.Bytes(), chanKey[:])
3✔
1179
}
1180

1181
// HasChannelEdge returns true if the database knows of a channel edge with the
1182
// passed channel ID, and false otherwise. If an edge with that ID is found
1183
// within the graph, then two time stamps representing the last time the edge
1184
// was updated for both directed edges are returned along with the boolean. If
1185
// it is not found, then the zombie index is checked and its result is returned
1186
// as the second boolean.
1187
func (c *ChannelGraph) HasChannelEdge(
1188
        chanID uint64) (time.Time, time.Time, bool, bool, error) {
3✔
1189

3✔
1190
        var (
3✔
1191
                upd1Time time.Time
3✔
1192
                upd2Time time.Time
3✔
1193
                exists   bool
3✔
1194
                isZombie bool
3✔
1195
        )
3✔
1196

3✔
1197
        // We'll query the cache with the shared lock held to allow multiple
3✔
1198
        // readers to access values in the cache concurrently if they exist.
3✔
1199
        c.cacheMu.RLock()
3✔
1200
        if entry, ok := c.rejectCache.get(chanID); ok {
6✔
1201
                c.cacheMu.RUnlock()
3✔
1202
                upd1Time = time.Unix(entry.upd1Time, 0)
3✔
1203
                upd2Time = time.Unix(entry.upd2Time, 0)
3✔
1204
                exists, isZombie = entry.flags.unpack()
3✔
1205
                return upd1Time, upd2Time, exists, isZombie, nil
3✔
1206
        }
3✔
1207
        c.cacheMu.RUnlock()
3✔
1208

3✔
1209
        c.cacheMu.Lock()
3✔
1210
        defer c.cacheMu.Unlock()
3✔
1211

3✔
1212
        // The item was not found with the shared lock, so we'll acquire the
3✔
1213
        // exclusive lock and check the cache again in case another method added
3✔
1214
        // the entry to the cache while no lock was held.
3✔
1215
        if entry, ok := c.rejectCache.get(chanID); ok {
6✔
1216
                upd1Time = time.Unix(entry.upd1Time, 0)
3✔
1217
                upd2Time = time.Unix(entry.upd2Time, 0)
3✔
1218
                exists, isZombie = entry.flags.unpack()
3✔
1219
                return upd1Time, upd2Time, exists, isZombie, nil
3✔
1220
        }
3✔
1221

1222
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1223
                edges := tx.ReadBucket(edgeBucket)
3✔
1224
                if edges == nil {
3✔
1225
                        return ErrGraphNoEdgesFound
×
1226
                }
×
1227
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
1228
                if edgeIndex == nil {
3✔
1229
                        return ErrGraphNoEdgesFound
×
1230
                }
×
1231

1232
                var channelID [8]byte
3✔
1233
                byteOrder.PutUint64(channelID[:], chanID)
3✔
1234

3✔
1235
                // If the edge doesn't exist, then we'll also check our zombie
3✔
1236
                // index.
3✔
1237
                if edgeIndex.Get(channelID[:]) == nil {
6✔
1238
                        exists = false
3✔
1239
                        zombieIndex := edges.NestedReadBucket(zombieBucket)
3✔
1240
                        if zombieIndex != nil {
6✔
1241
                                isZombie, _, _ = isZombieEdge(
3✔
1242
                                        zombieIndex, chanID,
3✔
1243
                                )
3✔
1244
                        }
3✔
1245

1246
                        return nil
3✔
1247
                }
1248

1249
                exists = true
3✔
1250
                isZombie = false
3✔
1251

3✔
1252
                // If the channel has been found in the graph, then retrieve
3✔
1253
                // the edges itself so we can return the last updated
3✔
1254
                // timestamps.
3✔
1255
                nodes := tx.ReadBucket(nodeBucket)
3✔
1256
                if nodes == nil {
3✔
1257
                        return ErrGraphNodeNotFound
×
1258
                }
×
1259

1260
                e1, e2, err := fetchChanEdgePolicies(
3✔
1261
                        edgeIndex, edges, channelID[:],
3✔
1262
                )
3✔
1263
                if err != nil {
3✔
1264
                        return err
×
1265
                }
×
1266

1267
                // As we may have only one of the edges populated, only set the
1268
                // update time if the edge was found in the database.
1269
                if e1 != nil {
6✔
1270
                        upd1Time = e1.LastUpdate
3✔
1271
                }
3✔
1272
                if e2 != nil {
6✔
1273
                        upd2Time = e2.LastUpdate
3✔
1274
                }
3✔
1275

1276
                return nil
3✔
1277
        }, func() {}); err != nil {
3✔
1278
                return time.Time{}, time.Time{}, exists, isZombie, err
×
1279
        }
×
1280

1281
        c.rejectCache.insert(chanID, rejectCacheEntry{
3✔
1282
                upd1Time: upd1Time.Unix(),
3✔
1283
                upd2Time: upd2Time.Unix(),
3✔
1284
                flags:    packRejectFlags(exists, isZombie),
3✔
1285
        })
3✔
1286

3✔
1287
        return upd1Time, upd2Time, exists, isZombie, nil
3✔
1288
}
1289

1290
// UpdateChannelEdge retrieves and update edge of the graph database. Method
1291
// only reserved for updating an edge info after its already been created.
1292
// In order to maintain this constraints, we return an error in the scenario
1293
// that an edge info hasn't yet been created yet, but someone attempts to update
1294
// it.
1295
func (c *ChannelGraph) UpdateChannelEdge(edge *models.ChannelEdgeInfo) error {
3✔
1296
        // Construct the channel's primary key which is the 8-byte channel ID.
3✔
1297
        var chanKey [8]byte
3✔
1298
        binary.BigEndian.PutUint64(chanKey[:], edge.ChannelID)
3✔
1299

3✔
1300
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1301
                edges := tx.ReadWriteBucket(edgeBucket)
3✔
1302
                if edge == nil {
3✔
1303
                        return ErrEdgeNotFound
×
1304
                }
×
1305

1306
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
1307
                if edgeIndex == nil {
3✔
1308
                        return ErrEdgeNotFound
×
1309
                }
×
1310

1311
                if edgeInfo := edgeIndex.Get(chanKey[:]); edgeInfo == nil {
3✔
1312
                        return ErrEdgeNotFound
×
1313
                }
×
1314

1315
                if c.graphCache != nil {
6✔
1316
                        c.graphCache.UpdateChannel(edge)
3✔
1317
                }
3✔
1318

1319
                return putChanEdgeInfo(edgeIndex, edge, chanKey)
3✔
1320
        }, func() {})
3✔
1321
}
1322

1323
const (
1324
        // pruneTipBytes is the total size of the value which stores a prune
1325
        // entry of the graph in the prune log. The "prune tip" is the last
1326
        // entry in the prune log, and indicates if the channel graph is in
1327
        // sync with the current UTXO state. The structure of the value
1328
        // is: blockHash, taking 32 bytes total.
1329
        pruneTipBytes = 32
1330
)
1331

1332
// PruneGraph prunes newly closed channels from the channel graph in response
1333
// to a new block being solved on the network. Any transactions which spend the
1334
// funding output of any known channels within he graph will be deleted.
1335
// Additionally, the "prune tip", or the last block which has been used to
1336
// prune the graph is stored so callers can ensure the graph is fully in sync
1337
// with the current UTXO state. A slice of channels that have been closed by
1338
// the target block are returned if the function succeeds without error.
1339
func (c *ChannelGraph) PruneGraph(spentOutputs []*wire.OutPoint,
1340
        blockHash *chainhash.Hash, blockHeight uint32) (
1341
        []*models.ChannelEdgeInfo, error) {
3✔
1342

3✔
1343
        c.cacheMu.Lock()
3✔
1344
        defer c.cacheMu.Unlock()
3✔
1345

3✔
1346
        var chansClosed []*models.ChannelEdgeInfo
3✔
1347

3✔
1348
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1349
                // First grab the edges bucket which houses the information
3✔
1350
                // we'd like to delete
3✔
1351
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
3✔
1352
                if err != nil {
3✔
1353
                        return err
×
1354
                }
×
1355

1356
                // Next grab the two edge indexes which will also need to be
1357
                // updated.
1358
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
3✔
1359
                if err != nil {
3✔
1360
                        return err
×
1361
                }
×
1362
                chanIndex, err := edges.CreateBucketIfNotExists(
3✔
1363
                        channelPointBucket,
3✔
1364
                )
3✔
1365
                if err != nil {
3✔
1366
                        return err
×
1367
                }
×
1368
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
1369
                if nodes == nil {
3✔
1370
                        return ErrSourceNodeNotSet
×
1371
                }
×
1372
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
3✔
1373
                if err != nil {
3✔
1374
                        return err
×
1375
                }
×
1376

1377
                // For each of the outpoints that have been spent within the
1378
                // block, we attempt to delete them from the graph as if that
1379
                // outpoint was a channel, then it has now been closed.
1380
                for _, chanPoint := range spentOutputs {
6✔
1381
                        // TODO(roasbeef): load channel bloom filter, continue
3✔
1382
                        // if NOT if filter
3✔
1383

3✔
1384
                        var opBytes bytes.Buffer
3✔
1385
                        err := WriteOutpoint(&opBytes, chanPoint)
3✔
1386
                        if err != nil {
3✔
1387
                                return err
×
1388
                        }
×
1389

1390
                        // First attempt to see if the channel exists within
1391
                        // the database, if not, then we can exit early.
1392
                        chanID := chanIndex.Get(opBytes.Bytes())
3✔
1393
                        if chanID == nil {
3✔
1394
                                continue
×
1395
                        }
1396

1397
                        // However, if it does, then we'll read out the full
1398
                        // version so we can add it to the set of deleted
1399
                        // channels.
1400
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
1401
                        if err != nil {
3✔
1402
                                return err
×
1403
                        }
×
1404

1405
                        // Attempt to delete the channel, an ErrEdgeNotFound
1406
                        // will be returned if that outpoint isn't known to be
1407
                        // a channel. If no error is returned, then a channel
1408
                        // was successfully pruned.
1409
                        err = c.delChannelEdgeUnsafe(
3✔
1410
                                edges, edgeIndex, chanIndex, zombieIndex,
3✔
1411
                                chanID, false, false,
3✔
1412
                        )
3✔
1413
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
3✔
1414
                                return err
×
1415
                        }
×
1416

1417
                        chansClosed = append(chansClosed, &edgeInfo)
3✔
1418
                }
1419

1420
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
3✔
1421
                if err != nil {
3✔
1422
                        return err
×
1423
                }
×
1424

1425
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
3✔
1426
                        pruneLogBucket,
3✔
1427
                )
3✔
1428
                if err != nil {
3✔
1429
                        return err
×
1430
                }
×
1431

1432
                // With the graph pruned, add a new entry to the prune log,
1433
                // which can be used to check if the graph is fully synced with
1434
                // the current UTXO state.
1435
                var blockHeightBytes [4]byte
3✔
1436
                byteOrder.PutUint32(blockHeightBytes[:], blockHeight)
3✔
1437

3✔
1438
                var newTip [pruneTipBytes]byte
3✔
1439
                copy(newTip[:], blockHash[:])
3✔
1440

3✔
1441
                err = pruneBucket.Put(blockHeightBytes[:], newTip[:])
3✔
1442
                if err != nil {
3✔
1443
                        return err
×
1444
                }
×
1445

1446
                // Now that the graph has been pruned, we'll also attempt to
1447
                // prune any nodes that have had a channel closed within the
1448
                // latest block.
1449
                return c.pruneGraphNodes(nodes, edgeIndex)
3✔
1450
        }, func() {
3✔
1451
                chansClosed = nil
3✔
1452
        })
3✔
1453
        if err != nil {
3✔
1454
                return nil, err
×
1455
        }
×
1456

1457
        for _, channel := range chansClosed {
6✔
1458
                c.rejectCache.remove(channel.ChannelID)
3✔
1459
                c.chanCache.remove(channel.ChannelID)
3✔
1460
        }
3✔
1461

1462
        if c.graphCache != nil {
6✔
1463
                log.Debugf("Pruned graph, cache now has %s",
3✔
1464
                        c.graphCache.Stats())
3✔
1465
        }
3✔
1466

1467
        return chansClosed, nil
3✔
1468
}
1469

1470
// PruneGraphNodes is a garbage collection method which attempts to prune out
1471
// any nodes from the channel graph that are currently unconnected. This ensure
1472
// that we only maintain a graph of reachable nodes. In the event that a pruned
1473
// node gains more channels, it will be re-added back to the graph.
1474
func (c *ChannelGraph) PruneGraphNodes() error {
3✔
1475
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1476
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
1477
                if nodes == nil {
3✔
1478
                        return ErrGraphNodesNotFound
×
1479
                }
×
1480
                edges := tx.ReadWriteBucket(edgeBucket)
3✔
1481
                if edges == nil {
3✔
1482
                        return ErrGraphNotFound
×
1483
                }
×
1484
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
1485
                if edgeIndex == nil {
3✔
1486
                        return ErrGraphNoEdgesFound
×
1487
                }
×
1488

1489
                return c.pruneGraphNodes(nodes, edgeIndex)
3✔
1490
        }, func() {})
3✔
1491
}
1492

1493
// pruneGraphNodes attempts to remove any nodes from the graph who have had a
1494
// channel closed within the current block. If the node still has existing
1495
// channels in the graph, this will act as a no-op.
1496
func (c *ChannelGraph) pruneGraphNodes(nodes kvdb.RwBucket,
1497
        edgeIndex kvdb.RwBucket) error {
3✔
1498

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

3✔
1501
        // We'll retrieve the graph's source node to ensure we don't remove it
3✔
1502
        // even if it no longer has any open channels.
3✔
1503
        sourceNode, err := c.sourceNode(nodes)
3✔
1504
        if err != nil {
3✔
1505
                return err
×
1506
        }
×
1507

1508
        // We'll use this map to keep count the number of references to a node
1509
        // in the graph. A node should only be removed once it has no more
1510
        // references in the graph.
1511
        nodeRefCounts := make(map[[33]byte]int)
3✔
1512
        err = nodes.ForEach(func(pubKey, nodeBytes []byte) error {
6✔
1513
                // If this is the source key, then we skip this
3✔
1514
                // iteration as the value for this key is a pubKey
3✔
1515
                // rather than raw node information.
3✔
1516
                if bytes.Equal(pubKey, sourceKey) || len(pubKey) != 33 {
6✔
1517
                        return nil
3✔
1518
                }
3✔
1519

1520
                var nodePub [33]byte
3✔
1521
                copy(nodePub[:], pubKey)
3✔
1522
                nodeRefCounts[nodePub] = 0
3✔
1523

3✔
1524
                return nil
3✔
1525
        })
1526
        if err != nil {
3✔
1527
                return err
×
1528
        }
×
1529

1530
        // To ensure we never delete the source node, we'll start off by
1531
        // bumping its ref count to 1.
1532
        nodeRefCounts[sourceNode.PubKeyBytes] = 1
3✔
1533

3✔
1534
        // Next, we'll run through the edgeIndex which maps a channel ID to the
3✔
1535
        // edge info. We'll use this scan to populate our reference count map
3✔
1536
        // above.
3✔
1537
        err = edgeIndex.ForEach(func(chanID, edgeInfoBytes []byte) error {
6✔
1538
                // The first 66 bytes of the edge info contain the pubkeys of
3✔
1539
                // the nodes that this edge attaches. We'll extract them, and
3✔
1540
                // add them to the ref count map.
3✔
1541
                var node1, node2 [33]byte
3✔
1542
                copy(node1[:], edgeInfoBytes[:33])
3✔
1543
                copy(node2[:], edgeInfoBytes[33:])
3✔
1544

3✔
1545
                // With the nodes extracted, we'll increase the ref count of
3✔
1546
                // each of the nodes.
3✔
1547
                nodeRefCounts[node1]++
3✔
1548
                nodeRefCounts[node2]++
3✔
1549

3✔
1550
                return nil
3✔
1551
        })
3✔
1552
        if err != nil {
3✔
1553
                return err
×
1554
        }
×
1555

1556
        // Finally, we'll make a second pass over the set of nodes, and delete
1557
        // any nodes that have a ref count of zero.
1558
        var numNodesPruned int
3✔
1559
        for nodePubKey, refCount := range nodeRefCounts {
6✔
1560
                // If the ref count of the node isn't zero, then we can safely
3✔
1561
                // skip it as it still has edges to or from it within the
3✔
1562
                // graph.
3✔
1563
                if refCount != 0 {
6✔
1564
                        continue
3✔
1565
                }
1566

1567
                if c.graphCache != nil {
6✔
1568
                        c.graphCache.RemoveNode(nodePubKey)
3✔
1569
                }
3✔
1570

1571
                // If we reach this point, then there are no longer any edges
1572
                // that connect this node, so we can delete it.
1573
                err := c.deleteLightningNode(nodes, nodePubKey[:])
3✔
1574
                if err != nil {
3✔
1575
                        if errors.Is(err, ErrGraphNodeNotFound) ||
×
1576
                                errors.Is(err, ErrGraphNodesNotFound) {
×
1577

×
1578
                                log.Warnf("Unable to prune node %x from the "+
×
1579
                                        "graph: %v", nodePubKey, err)
×
1580
                                continue
×
1581
                        }
1582

1583
                        return err
×
1584
                }
1585

1586
                log.Infof("Pruned unconnected node %x from channel graph",
3✔
1587
                        nodePubKey[:])
3✔
1588

3✔
1589
                numNodesPruned++
3✔
1590
        }
1591

1592
        if numNodesPruned > 0 {
6✔
1593
                log.Infof("Pruned %v unconnected nodes from the channel graph",
3✔
1594
                        numNodesPruned)
3✔
1595
        }
3✔
1596

1597
        return nil
3✔
1598
}
1599

1600
// DisconnectBlockAtHeight is used to indicate that the block specified
1601
// by the passed height has been disconnected from the main chain. This
1602
// will "rewind" the graph back to the height below, deleting channels
1603
// that are no longer confirmed from the graph. The prune log will be
1604
// set to the last prune height valid for the remaining chain.
1605
// Channels that were removed from the graph resulting from the
1606
// disconnected block are returned.
1607
func (c *ChannelGraph) DisconnectBlockAtHeight(height uint32) (
1608
        []*models.ChannelEdgeInfo, error) {
2✔
1609

2✔
1610
        // Every channel having a ShortChannelID starting at 'height'
2✔
1611
        // will no longer be confirmed.
2✔
1612
        startShortChanID := lnwire.ShortChannelID{
2✔
1613
                BlockHeight: height,
2✔
1614
        }
2✔
1615

2✔
1616
        // Delete everything after this height from the db up until the
2✔
1617
        // SCID alias range.
2✔
1618
        endShortChanID := aliasmgr.StartingAlias
2✔
1619

2✔
1620
        // The block height will be the 3 first bytes of the channel IDs.
2✔
1621
        var chanIDStart [8]byte
2✔
1622
        byteOrder.PutUint64(chanIDStart[:], startShortChanID.ToUint64())
2✔
1623
        var chanIDEnd [8]byte
2✔
1624
        byteOrder.PutUint64(chanIDEnd[:], endShortChanID.ToUint64())
2✔
1625

2✔
1626
        c.cacheMu.Lock()
2✔
1627
        defer c.cacheMu.Unlock()
2✔
1628

2✔
1629
        // Keep track of the channels that are removed from the graph.
2✔
1630
        var removedChans []*models.ChannelEdgeInfo
2✔
1631

2✔
1632
        if err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
4✔
1633
                edges, err := tx.CreateTopLevelBucket(edgeBucket)
2✔
1634
                if err != nil {
2✔
1635
                        return err
×
1636
                }
×
1637
                edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
2✔
1638
                if err != nil {
2✔
1639
                        return err
×
1640
                }
×
1641
                chanIndex, err := edges.CreateBucketIfNotExists(
2✔
1642
                        channelPointBucket,
2✔
1643
                )
2✔
1644
                if err != nil {
2✔
1645
                        return err
×
1646
                }
×
1647
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
2✔
1648
                if err != nil {
2✔
1649
                        return err
×
1650
                }
×
1651

1652
                // Scan from chanIDStart to chanIDEnd, deleting every
1653
                // found edge.
1654
                // NOTE: we must delete the edges after the cursor loop, since
1655
                // modifying the bucket while traversing is not safe.
1656
                // NOTE: We use a < comparison in bytes.Compare instead of <=
1657
                // so that the StartingAlias itself isn't deleted.
1658
                var keys [][]byte
2✔
1659
                cursor := edgeIndex.ReadWriteCursor()
2✔
1660

2✔
1661
                //nolint:ll
2✔
1662
                for k, v := cursor.Seek(chanIDStart[:]); k != nil &&
2✔
1663
                        bytes.Compare(k, chanIDEnd[:]) < 0; k, v = cursor.Next() {
4✔
1664
                        edgeInfoReader := bytes.NewReader(v)
2✔
1665
                        edgeInfo, err := deserializeChanEdgeInfo(edgeInfoReader)
2✔
1666
                        if err != nil {
2✔
1667
                                return err
×
1668
                        }
×
1669

1670
                        keys = append(keys, k)
2✔
1671
                        removedChans = append(removedChans, &edgeInfo)
2✔
1672
                }
1673

1674
                for _, k := range keys {
4✔
1675
                        err = c.delChannelEdgeUnsafe(
2✔
1676
                                edges, edgeIndex, chanIndex, zombieIndex,
2✔
1677
                                k, false, false,
2✔
1678
                        )
2✔
1679
                        if err != nil && !errors.Is(err, ErrEdgeNotFound) {
2✔
1680
                                return err
×
1681
                        }
×
1682
                }
1683

1684
                // Delete all the entries in the prune log having a height
1685
                // greater or equal to the block disconnected.
1686
                metaBucket, err := tx.CreateTopLevelBucket(graphMetaBucket)
2✔
1687
                if err != nil {
2✔
1688
                        return err
×
1689
                }
×
1690

1691
                pruneBucket, err := metaBucket.CreateBucketIfNotExists(
2✔
1692
                        pruneLogBucket,
2✔
1693
                )
2✔
1694
                if err != nil {
2✔
1695
                        return err
×
1696
                }
×
1697

1698
                var pruneKeyStart [4]byte
2✔
1699
                byteOrder.PutUint32(pruneKeyStart[:], height)
2✔
1700

2✔
1701
                var pruneKeyEnd [4]byte
2✔
1702
                byteOrder.PutUint32(pruneKeyEnd[:], math.MaxUint32)
2✔
1703

2✔
1704
                // To avoid modifying the bucket while traversing, we delete
2✔
1705
                // the keys in a second loop.
2✔
1706
                var pruneKeys [][]byte
2✔
1707
                pruneCursor := pruneBucket.ReadWriteCursor()
2✔
1708
                //nolint:ll
2✔
1709
                for k, _ := pruneCursor.Seek(pruneKeyStart[:]); k != nil &&
2✔
1710
                        bytes.Compare(k, pruneKeyEnd[:]) <= 0; k, _ = pruneCursor.Next() {
4✔
1711
                        pruneKeys = append(pruneKeys, k)
2✔
1712
                }
2✔
1713

1714
                for _, k := range pruneKeys {
4✔
1715
                        if err := pruneBucket.Delete(k); err != nil {
2✔
1716
                                return err
×
1717
                        }
×
1718
                }
1719

1720
                return nil
2✔
1721
        }, func() {
2✔
1722
                removedChans = nil
2✔
1723
        }); err != nil {
2✔
1724
                return nil, err
×
1725
        }
×
1726

1727
        for _, channel := range removedChans {
4✔
1728
                c.rejectCache.remove(channel.ChannelID)
2✔
1729
                c.chanCache.remove(channel.ChannelID)
2✔
1730
        }
2✔
1731

1732
        return removedChans, nil
2✔
1733
}
1734

1735
// PruneTip returns the block height and hash of the latest block that has been
1736
// used to prune channels in the graph. Knowing the "prune tip" allows callers
1737
// to tell if the graph is currently in sync with the current best known UTXO
1738
// state.
1739
func (c *ChannelGraph) PruneTip() (*chainhash.Hash, uint32, error) {
3✔
1740
        var (
3✔
1741
                tipHash   chainhash.Hash
3✔
1742
                tipHeight uint32
3✔
1743
        )
3✔
1744

3✔
1745
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1746
                graphMeta := tx.ReadBucket(graphMetaBucket)
3✔
1747
                if graphMeta == nil {
3✔
1748
                        return ErrGraphNotFound
×
1749
                }
×
1750
                pruneBucket := graphMeta.NestedReadBucket(pruneLogBucket)
3✔
1751
                if pruneBucket == nil {
3✔
1752
                        return ErrGraphNeverPruned
×
1753
                }
×
1754

1755
                pruneCursor := pruneBucket.ReadCursor()
3✔
1756

3✔
1757
                // The prune key with the largest block height will be our
3✔
1758
                // prune tip.
3✔
1759
                k, v := pruneCursor.Last()
3✔
1760
                if k == nil {
6✔
1761
                        return ErrGraphNeverPruned
3✔
1762
                }
3✔
1763

1764
                // Once we have the prune tip, the value will be the block hash,
1765
                // and the key the block height.
1766
                copy(tipHash[:], v[:])
3✔
1767
                tipHeight = byteOrder.Uint32(k[:])
3✔
1768

3✔
1769
                return nil
3✔
1770
        }, func() {})
3✔
1771
        if err != nil {
6✔
1772
                return nil, 0, err
3✔
1773
        }
3✔
1774

1775
        return &tipHash, tipHeight, nil
3✔
1776
}
1777

1778
// DeleteChannelEdges removes edges with the given channel IDs from the
1779
// database and marks them as zombies. This ensures that we're unable to re-add
1780
// it to our database once again. If an edge does not exist within the
1781
// database, then ErrEdgeNotFound will be returned. If strictZombiePruning is
1782
// true, then when we mark these edges as zombies, we'll set up the keys such
1783
// that we require the node that failed to send the fresh update to be the one
1784
// that resurrects the channel from its zombie state. The markZombie bool
1785
// denotes whether or not to mark the channel as a zombie.
1786
func (c *ChannelGraph) DeleteChannelEdges(strictZombiePruning, markZombie bool,
1787
        chanIDs ...uint64) error {
3✔
1788

3✔
1789
        // TODO(roasbeef): possibly delete from node bucket if node has no more
3✔
1790
        // channels
3✔
1791
        // TODO(roasbeef): don't delete both edges?
3✔
1792

3✔
1793
        c.cacheMu.Lock()
3✔
1794
        defer c.cacheMu.Unlock()
3✔
1795

3✔
1796
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
1797
                edges := tx.ReadWriteBucket(edgeBucket)
3✔
1798
                if edges == nil {
3✔
1799
                        return ErrEdgeNotFound
×
1800
                }
×
1801
                edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
1802
                if edgeIndex == nil {
3✔
1803
                        return ErrEdgeNotFound
×
1804
                }
×
1805
                chanIndex := edges.NestedReadWriteBucket(channelPointBucket)
3✔
1806
                if chanIndex == nil {
3✔
1807
                        return ErrEdgeNotFound
×
1808
                }
×
1809
                nodes := tx.ReadWriteBucket(nodeBucket)
3✔
1810
                if nodes == nil {
3✔
1811
                        return ErrGraphNodeNotFound
×
1812
                }
×
1813
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
3✔
1814
                if err != nil {
3✔
1815
                        return err
×
1816
                }
×
1817

1818
                var rawChanID [8]byte
3✔
1819
                for _, chanID := range chanIDs {
6✔
1820
                        byteOrder.PutUint64(rawChanID[:], chanID)
3✔
1821
                        err := c.delChannelEdgeUnsafe(
3✔
1822
                                edges, edgeIndex, chanIndex, zombieIndex,
3✔
1823
                                rawChanID[:], markZombie, strictZombiePruning,
3✔
1824
                        )
3✔
1825
                        if err != nil {
3✔
1826
                                return err
×
1827
                        }
×
1828
                }
1829

1830
                return nil
3✔
1831
        }, func() {})
3✔
1832
        if err != nil {
3✔
1833
                return err
×
1834
        }
×
1835

1836
        for _, chanID := range chanIDs {
6✔
1837
                c.rejectCache.remove(chanID)
3✔
1838
                c.chanCache.remove(chanID)
3✔
1839
        }
3✔
1840

1841
        return nil
3✔
1842
}
1843

1844
// ChannelID attempt to lookup the 8-byte compact channel ID which maps to the
1845
// passed channel point (outpoint). If the passed channel doesn't exist within
1846
// the database, then ErrEdgeNotFound is returned.
1847
func (c *ChannelGraph) ChannelID(chanPoint *wire.OutPoint) (uint64, error) {
3✔
1848
        var chanID uint64
3✔
1849
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1850
                var err error
3✔
1851
                chanID, err = getChanID(tx, chanPoint)
3✔
1852
                return err
3✔
1853
        }, func() {
6✔
1854
                chanID = 0
3✔
1855
        }); err != nil {
6✔
1856
                return 0, err
3✔
1857
        }
3✔
1858

1859
        return chanID, nil
3✔
1860
}
1861

1862
// getChanID returns the assigned channel ID for a given channel point.
1863
func getChanID(tx kvdb.RTx, chanPoint *wire.OutPoint) (uint64, error) {
3✔
1864
        var b bytes.Buffer
3✔
1865
        if err := WriteOutpoint(&b, chanPoint); err != nil {
3✔
1866
                return 0, err
×
1867
        }
×
1868

1869
        edges := tx.ReadBucket(edgeBucket)
3✔
1870
        if edges == nil {
3✔
1871
                return 0, ErrGraphNoEdgesFound
×
1872
        }
×
1873
        chanIndex := edges.NestedReadBucket(channelPointBucket)
3✔
1874
        if chanIndex == nil {
3✔
1875
                return 0, ErrGraphNoEdgesFound
×
1876
        }
×
1877

1878
        chanIDBytes := chanIndex.Get(b.Bytes())
3✔
1879
        if chanIDBytes == nil {
6✔
1880
                return 0, ErrEdgeNotFound
3✔
1881
        }
3✔
1882

1883
        chanID := byteOrder.Uint64(chanIDBytes)
3✔
1884

3✔
1885
        return chanID, nil
3✔
1886
}
1887

1888
// TODO(roasbeef): allow updates to use Batch?
1889

1890
// HighestChanID returns the "highest" known channel ID in the channel graph.
1891
// This represents the "newest" channel from the PoV of the chain. This method
1892
// can be used by peers to quickly determine if they're graphs are in sync.
1893
func (c *ChannelGraph) HighestChanID() (uint64, error) {
3✔
1894
        var cid uint64
3✔
1895

3✔
1896
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1897
                edges := tx.ReadBucket(edgeBucket)
3✔
1898
                if edges == nil {
3✔
1899
                        return ErrGraphNoEdgesFound
×
1900
                }
×
1901
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
1902
                if edgeIndex == nil {
3✔
1903
                        return ErrGraphNoEdgesFound
×
1904
                }
×
1905

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

3✔
1910
                lastChanID, _ := cidCursor.Last()
3✔
1911

3✔
1912
                // If there's no key, then this means that we don't actually
3✔
1913
                // know of any channels, so we'll return a predicable error.
3✔
1914
                if lastChanID == nil {
6✔
1915
                        return ErrGraphNoEdgesFound
3✔
1916
                }
3✔
1917

1918
                // Otherwise, we'll de serialize the channel ID and return it
1919
                // to the caller.
1920
                cid = byteOrder.Uint64(lastChanID)
3✔
1921
                return nil
3✔
1922
        }, func() {
3✔
1923
                cid = 0
3✔
1924
        })
3✔
1925
        if err != nil && err != ErrGraphNoEdgesFound {
3✔
1926
                return 0, err
×
1927
        }
×
1928

1929
        return cid, nil
3✔
1930
}
1931

1932
// ChannelEdge represents the complete set of information for a channel edge in
1933
// the known channel graph. This struct couples the core information of the
1934
// edge as well as each of the known advertised edge policies.
1935
type ChannelEdge struct {
1936
        // Info contains all the static information describing the channel.
1937
        Info *models.ChannelEdgeInfo
1938

1939
        // Policy1 points to the "first" edge policy of the channel containing
1940
        // the dynamic information required to properly route through the edge.
1941
        Policy1 *models.ChannelEdgePolicy
1942

1943
        // Policy2 points to the "second" edge policy of the channel containing
1944
        // the dynamic information required to properly route through the edge.
1945
        Policy2 *models.ChannelEdgePolicy
1946

1947
        // Node1 is "node 1" in the channel. This is the node that would have
1948
        // produced Policy1 if it exists.
1949
        Node1 *models.LightningNode
1950

1951
        // Node2 is "node 2" in the channel. This is the node that would have
1952
        // produced Policy2 if it exists.
1953
        Node2 *models.LightningNode
1954
}
1955

1956
// ChanUpdatesInHorizon returns all the known channel edges which have at least
1957
// one edge that has an update timestamp within the specified horizon.
1958
func (c *ChannelGraph) ChanUpdatesInHorizon(startTime,
1959
        endTime time.Time) ([]ChannelEdge, error) {
3✔
1960

3✔
1961
        // To ensure we don't return duplicate ChannelEdges, we'll use an
3✔
1962
        // additional map to keep track of the edges already seen to prevent
3✔
1963
        // re-adding it.
3✔
1964
        var edgesSeen map[uint64]struct{}
3✔
1965
        var edgesToCache map[uint64]ChannelEdge
3✔
1966
        var edgesInHorizon []ChannelEdge
3✔
1967

3✔
1968
        c.cacheMu.Lock()
3✔
1969
        defer c.cacheMu.Unlock()
3✔
1970

3✔
1971
        var hits int
3✔
1972
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
1973
                edges := tx.ReadBucket(edgeBucket)
3✔
1974
                if edges == nil {
3✔
1975
                        return ErrGraphNoEdgesFound
×
1976
                }
×
1977
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
1978
                if edgeIndex == nil {
3✔
1979
                        return ErrGraphNoEdgesFound
×
1980
                }
×
1981
                edgeUpdateIndex := edges.NestedReadBucket(edgeUpdateIndexBucket)
3✔
1982
                if edgeUpdateIndex == nil {
3✔
1983
                        return ErrGraphNoEdgesFound
×
1984
                }
×
1985

1986
                nodes := tx.ReadBucket(nodeBucket)
3✔
1987
                if nodes == nil {
3✔
1988
                        return ErrGraphNodesNotFound
×
1989
                }
×
1990

1991
                // We'll now obtain a cursor to perform a range query within
1992
                // the index to find all channels within the horizon.
1993
                updateCursor := edgeUpdateIndex.ReadCursor()
3✔
1994

3✔
1995
                var startTimeBytes, endTimeBytes [8 + 8]byte
3✔
1996
                byteOrder.PutUint64(
3✔
1997
                        startTimeBytes[:8], uint64(startTime.Unix()),
3✔
1998
                )
3✔
1999
                byteOrder.PutUint64(
3✔
2000
                        endTimeBytes[:8], uint64(endTime.Unix()),
3✔
2001
                )
3✔
2002

3✔
2003
                // With our start and end times constructed, we'll step through
3✔
2004
                // the index collecting the info and policy of each update of
3✔
2005
                // each channel that has a last update within the time range.
3✔
2006
                //
3✔
2007
                //nolint:ll
3✔
2008
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
3✔
2009
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
6✔
2010

3✔
2011
                        // We have a new eligible entry, so we'll slice of the
3✔
2012
                        // chan ID so we can query it in the DB.
3✔
2013
                        chanID := indexKey[8:]
3✔
2014

3✔
2015
                        // If we've already retrieved the info and policies for
3✔
2016
                        // this edge, then we can skip it as we don't need to do
3✔
2017
                        // so again.
3✔
2018
                        chanIDInt := byteOrder.Uint64(chanID)
3✔
2019
                        if _, ok := edgesSeen[chanIDInt]; ok {
3✔
2020
                                continue
×
2021
                        }
2022

2023
                        if channel, ok := c.chanCache.get(chanIDInt); ok {
6✔
2024
                                hits++
3✔
2025
                                edgesSeen[chanIDInt] = struct{}{}
3✔
2026
                                edgesInHorizon = append(edgesInHorizon, channel)
3✔
2027
                                continue
3✔
2028
                        }
2029

2030
                        // First, we'll fetch the static edge information.
2031
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
2032
                        if err != nil {
3✔
2033
                                chanID := byteOrder.Uint64(chanID)
×
2034
                                return fmt.Errorf("unable to fetch info for "+
×
2035
                                        "edge with chan_id=%v: %v", chanID, err)
×
2036
                        }
×
2037

2038
                        // With the static information obtained, we'll now
2039
                        // fetch the dynamic policy info.
2040
                        edge1, edge2, err := fetchChanEdgePolicies(
3✔
2041
                                edgeIndex, edges, chanID,
3✔
2042
                        )
3✔
2043
                        if err != nil {
3✔
2044
                                chanID := byteOrder.Uint64(chanID)
×
2045
                                return fmt.Errorf("unable to fetch policies "+
×
2046
                                        "for edge with chan_id=%v: %v", chanID,
×
2047
                                        err)
×
2048
                        }
×
2049

2050
                        node1, err := fetchLightningNode(
3✔
2051
                                nodes, edgeInfo.NodeKey1Bytes[:],
3✔
2052
                        )
3✔
2053
                        if err != nil {
3✔
2054
                                return err
×
2055
                        }
×
2056

2057
                        node2, err := fetchLightningNode(
3✔
2058
                                nodes, edgeInfo.NodeKey2Bytes[:],
3✔
2059
                        )
3✔
2060
                        if err != nil {
3✔
2061
                                return err
×
2062
                        }
×
2063

2064
                        // Finally, we'll collate this edge with the rest of
2065
                        // edges to be returned.
2066
                        edgesSeen[chanIDInt] = struct{}{}
3✔
2067
                        channel := ChannelEdge{
3✔
2068
                                Info:    &edgeInfo,
3✔
2069
                                Policy1: edge1,
3✔
2070
                                Policy2: edge2,
3✔
2071
                                Node1:   &node1,
3✔
2072
                                Node2:   &node2,
3✔
2073
                        }
3✔
2074
                        edgesInHorizon = append(edgesInHorizon, channel)
3✔
2075
                        edgesToCache[chanIDInt] = channel
3✔
2076
                }
2077

2078
                return nil
3✔
2079
        }, func() {
3✔
2080
                edgesSeen = make(map[uint64]struct{})
3✔
2081
                edgesToCache = make(map[uint64]ChannelEdge)
3✔
2082
                edgesInHorizon = nil
3✔
2083
        })
3✔
2084
        switch {
3✔
2085
        case err == ErrGraphNoEdgesFound:
×
2086
                fallthrough
×
2087
        case err == ErrGraphNodesNotFound:
×
2088
                break
×
2089

2090
        case err != nil:
×
2091
                return nil, err
×
2092
        }
2093

2094
        // Insert any edges loaded from disk into the cache.
2095
        for chanid, channel := range edgesToCache {
6✔
2096
                c.chanCache.insert(chanid, channel)
3✔
2097
        }
3✔
2098

2099
        log.Debugf("ChanUpdatesInHorizon hit percentage: %f (%d/%d)",
3✔
2100
                float64(hits)/float64(len(edgesInHorizon)), hits,
3✔
2101
                len(edgesInHorizon))
3✔
2102

3✔
2103
        return edgesInHorizon, nil
3✔
2104
}
2105

2106
// NodeUpdatesInHorizon returns all the known lightning node which have an
2107
// update timestamp within the passed range. This method can be used by two
2108
// nodes to quickly determine if they have the same set of up to date node
2109
// announcements.
2110
func (c *ChannelGraph) NodeUpdatesInHorizon(startTime,
2111
        endTime time.Time) ([]models.LightningNode, error) {
3✔
2112

3✔
2113
        var nodesInHorizon []models.LightningNode
3✔
2114

3✔
2115
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
2116
                nodes := tx.ReadBucket(nodeBucket)
3✔
2117
                if nodes == nil {
3✔
2118
                        return ErrGraphNodesNotFound
×
2119
                }
×
2120

2121
                nodeUpdateIndex := nodes.NestedReadBucket(nodeUpdateIndexBucket)
3✔
2122
                if nodeUpdateIndex == nil {
3✔
2123
                        return ErrGraphNodesNotFound
×
2124
                }
×
2125

2126
                // We'll now obtain a cursor to perform a range query within
2127
                // the index to find all node announcements within the horizon.
2128
                updateCursor := nodeUpdateIndex.ReadCursor()
3✔
2129

3✔
2130
                var startTimeBytes, endTimeBytes [8 + 33]byte
3✔
2131
                byteOrder.PutUint64(
3✔
2132
                        startTimeBytes[:8], uint64(startTime.Unix()),
3✔
2133
                )
3✔
2134
                byteOrder.PutUint64(
3✔
2135
                        endTimeBytes[:8], uint64(endTime.Unix()),
3✔
2136
                )
3✔
2137

3✔
2138
                // With our start and end times constructed, we'll step through
3✔
2139
                // the index collecting info for each node within the time
3✔
2140
                // range.
3✔
2141
                //
3✔
2142
                //nolint:ll
3✔
2143
                for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
3✔
2144
                        bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {
6✔
2145

3✔
2146
                        nodePub := indexKey[8:]
3✔
2147
                        node, err := fetchLightningNode(nodes, nodePub)
3✔
2148
                        if err != nil {
3✔
2149
                                return err
×
2150
                        }
×
2151

2152
                        nodesInHorizon = append(nodesInHorizon, node)
3✔
2153
                }
2154

2155
                return nil
3✔
2156
        }, func() {
3✔
2157
                nodesInHorizon = nil
3✔
2158
        })
3✔
2159
        switch {
3✔
2160
        case err == ErrGraphNoEdgesFound:
×
2161
                fallthrough
×
2162
        case err == ErrGraphNodesNotFound:
×
2163
                break
×
2164

2165
        case err != nil:
×
2166
                return nil, err
×
2167
        }
2168

2169
        return nodesInHorizon, nil
3✔
2170
}
2171

2172
// FilterKnownChanIDs takes a set of channel IDs and return the subset of chan
2173
// ID's that we don't know and are not known zombies of the passed set. In other
2174
// words, we perform a set difference of our set of chan ID's and the ones
2175
// passed in. This method can be used by callers to determine the set of
2176
// channels another peer knows of that we don't.
2177
func (c *ChannelGraph) FilterKnownChanIDs(chansInfo []ChannelUpdateInfo,
2178
        isZombieChan func(time.Time, time.Time) bool) ([]uint64, error) {
3✔
2179

3✔
2180
        var newChanIDs []uint64
3✔
2181

3✔
2182
        c.cacheMu.Lock()
3✔
2183
        defer c.cacheMu.Unlock()
3✔
2184

3✔
2185
        err := kvdb.Update(c.db, func(tx kvdb.RwTx) error {
6✔
2186
                edges := tx.ReadBucket(edgeBucket)
3✔
2187
                if edges == nil {
3✔
2188
                        return ErrGraphNoEdgesFound
×
2189
                }
×
2190
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
2191
                if edgeIndex == nil {
3✔
2192
                        return ErrGraphNoEdgesFound
×
2193
                }
×
2194

2195
                // Fetch the zombie index, it may not exist if no edges have
2196
                // ever been marked as zombies. If the index has been
2197
                // initialized, we will use it later to skip known zombie edges.
2198
                zombieIndex := edges.NestedReadBucket(zombieBucket)
3✔
2199

3✔
2200
                // We'll run through the set of chanIDs and collate only the
3✔
2201
                // set of channel that are unable to be found within our db.
3✔
2202
                var cidBytes [8]byte
3✔
2203
                for _, info := range chansInfo {
6✔
2204
                        scid := info.ShortChannelID.ToUint64()
3✔
2205
                        byteOrder.PutUint64(cidBytes[:], scid)
3✔
2206

3✔
2207
                        // If the edge is already known, skip it.
3✔
2208
                        if v := edgeIndex.Get(cidBytes[:]); v != nil {
6✔
2209
                                continue
3✔
2210
                        }
2211

2212
                        // If the edge is a known zombie, skip it.
2213
                        if zombieIndex != nil {
6✔
2214
                                isZombie, _, _ := isZombieEdge(
3✔
2215
                                        zombieIndex, scid,
3✔
2216
                                )
3✔
2217

3✔
2218
                                // TODO(ziggie): Make sure that for the strict
3✔
2219
                                // pruning case we compare the pubkeys and
3✔
2220
                                // whether the right timestamp is not older than
3✔
2221
                                // the `ChannelPruneExpiry`.
3✔
2222
                                //
3✔
2223
                                // NOTE: The timestamp data has no verification
3✔
2224
                                // attached to it in the `ReplyChannelRange` msg
3✔
2225
                                // so we are trusting this data at this point.
3✔
2226
                                // However it is not critical because we are
3✔
2227
                                // just removing the channel from the db when
3✔
2228
                                // the timestamps are more recent. During the
3✔
2229
                                // querying of the gossip msg verification
3✔
2230
                                // happens as usual.
3✔
2231
                                // However we should start punishing peers when
3✔
2232
                                // they don't provide us honest data ?
3✔
2233
                                isStillZombie := isZombieChan(
3✔
2234
                                        info.Node1UpdateTimestamp,
3✔
2235
                                        info.Node2UpdateTimestamp,
3✔
2236
                                )
3✔
2237

3✔
2238
                                switch {
3✔
2239
                                // If the edge is a known zombie and if we
2240
                                // would still consider it a zombie given the
2241
                                // latest update timestamps, then we skip this
2242
                                // channel.
2243
                                case isZombie && isStillZombie:
×
2244
                                        continue
×
2245

2246
                                // Otherwise, if we have marked it as a zombie
2247
                                // but the latest update timestamps could bring
2248
                                // it back from the dead, then we mark it alive,
2249
                                // and we let it be added to the set of IDs to
2250
                                // query our peer for.
2251
                                case isZombie && !isStillZombie:
×
2252
                                        err := c.markEdgeLiveUnsafe(tx, scid)
×
2253
                                        if err != nil {
×
2254
                                                return err
×
2255
                                        }
×
2256
                                }
2257
                        }
2258

2259
                        newChanIDs = append(newChanIDs, scid)
3✔
2260
                }
2261

2262
                return nil
3✔
2263
        }, func() {
3✔
2264
                newChanIDs = nil
3✔
2265
        })
3✔
2266
        switch {
3✔
2267
        // If we don't know of any edges yet, then we'll return the entire set
2268
        // of chan IDs specified.
2269
        case err == ErrGraphNoEdgesFound:
×
2270
                ogChanIDs := make([]uint64, len(chansInfo))
×
2271
                for i, info := range chansInfo {
×
2272
                        ogChanIDs[i] = info.ShortChannelID.ToUint64()
×
2273
                }
×
2274

2275
                return ogChanIDs, nil
×
2276

2277
        case err != nil:
×
2278
                return nil, err
×
2279
        }
2280

2281
        return newChanIDs, nil
3✔
2282
}
2283

2284
// ChannelUpdateInfo couples the SCID of a channel with the timestamps of the
2285
// latest received channel updates for the channel.
2286
type ChannelUpdateInfo struct {
2287
        // ShortChannelID is the SCID identifier of the channel.
2288
        ShortChannelID lnwire.ShortChannelID
2289

2290
        // Node1UpdateTimestamp is the timestamp of the latest received update
2291
        // from the node 1 channel peer. This will be set to zero time if no
2292
        // update has yet been received from this node.
2293
        Node1UpdateTimestamp time.Time
2294

2295
        // Node2UpdateTimestamp is the timestamp of the latest received update
2296
        // from the node 2 channel peer. This will be set to zero time if no
2297
        // update has yet been received from this node.
2298
        Node2UpdateTimestamp time.Time
2299
}
2300

2301
// NewChannelUpdateInfo is a constructor which makes sure we initialize the
2302
// timestamps with zero seconds unix timestamp which equals
2303
// `January 1, 1970, 00:00:00 UTC` in case the value is `time.Time{}`.
2304
func NewChannelUpdateInfo(scid lnwire.ShortChannelID, node1Timestamp,
2305
        node2Timestamp time.Time) ChannelUpdateInfo {
3✔
2306

3✔
2307
        chanInfo := ChannelUpdateInfo{
3✔
2308
                ShortChannelID:       scid,
3✔
2309
                Node1UpdateTimestamp: node1Timestamp,
3✔
2310
                Node2UpdateTimestamp: node2Timestamp,
3✔
2311
        }
3✔
2312

3✔
2313
        if node1Timestamp.IsZero() {
6✔
2314
                chanInfo.Node1UpdateTimestamp = time.Unix(0, 0)
3✔
2315
        }
3✔
2316

2317
        if node2Timestamp.IsZero() {
6✔
2318
                chanInfo.Node2UpdateTimestamp = time.Unix(0, 0)
3✔
2319
        }
3✔
2320

2321
        return chanInfo
3✔
2322
}
2323

2324
// BlockChannelRange represents a range of channels for a given block height.
2325
type BlockChannelRange struct {
2326
        // Height is the height of the block all of the channels below were
2327
        // included in.
2328
        Height uint32
2329

2330
        // Channels is the list of channels identified by their short ID
2331
        // representation known to us that were included in the block height
2332
        // above. The list may include channel update timestamp information if
2333
        // requested.
2334
        Channels []ChannelUpdateInfo
2335
}
2336

2337
// FilterChannelRange returns the channel ID's of all known channels which were
2338
// mined in a block height within the passed range. The channel IDs are grouped
2339
// by their common block height. This method can be used to quickly share with a
2340
// peer the set of channels we know of within a particular range to catch them
2341
// up after a period of time offline. If withTimestamps is true then the
2342
// timestamp info of the latest received channel update messages of the channel
2343
// will be included in the response.
2344
func (c *ChannelGraph) FilterChannelRange(startHeight,
2345
        endHeight uint32, withTimestamps bool) ([]BlockChannelRange, error) {
3✔
2346

3✔
2347
        startChanID := &lnwire.ShortChannelID{
3✔
2348
                BlockHeight: startHeight,
3✔
2349
        }
3✔
2350

3✔
2351
        endChanID := lnwire.ShortChannelID{
3✔
2352
                BlockHeight: endHeight,
3✔
2353
                TxIndex:     math.MaxUint32 & 0x00ffffff,
3✔
2354
                TxPosition:  math.MaxUint16,
3✔
2355
        }
3✔
2356

3✔
2357
        // As we need to perform a range scan, we'll convert the starting and
3✔
2358
        // ending height to their corresponding values when encoded using short
3✔
2359
        // channel ID's.
3✔
2360
        var chanIDStart, chanIDEnd [8]byte
3✔
2361
        byteOrder.PutUint64(chanIDStart[:], startChanID.ToUint64())
3✔
2362
        byteOrder.PutUint64(chanIDEnd[:], endChanID.ToUint64())
3✔
2363

3✔
2364
        var channelsPerBlock map[uint32][]ChannelUpdateInfo
3✔
2365
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
6✔
2366
                edges := tx.ReadBucket(edgeBucket)
3✔
2367
                if edges == nil {
3✔
2368
                        return ErrGraphNoEdgesFound
×
2369
                }
×
2370
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
2371
                if edgeIndex == nil {
3✔
2372
                        return ErrGraphNoEdgesFound
×
2373
                }
×
2374

2375
                cursor := edgeIndex.ReadCursor()
3✔
2376

3✔
2377
                // We'll now iterate through the database, and find each
3✔
2378
                // channel ID that resides within the specified range.
3✔
2379
                //
3✔
2380
                //nolint:ll
3✔
2381
                for k, v := cursor.Seek(chanIDStart[:]); k != nil &&
3✔
2382
                        bytes.Compare(k, chanIDEnd[:]) <= 0; k, v = cursor.Next() {
6✔
2383
                        // Don't send alias SCIDs during gossip sync.
3✔
2384
                        edgeReader := bytes.NewReader(v)
3✔
2385
                        edgeInfo, err := deserializeChanEdgeInfo(edgeReader)
3✔
2386
                        if err != nil {
3✔
2387
                                return err
×
2388
                        }
×
2389

2390
                        if edgeInfo.AuthProof == nil {
6✔
2391
                                continue
3✔
2392
                        }
2393

2394
                        // This channel ID rests within the target range, so
2395
                        // we'll add it to our returned set.
2396
                        rawCid := byteOrder.Uint64(k)
3✔
2397
                        cid := lnwire.NewShortChanIDFromInt(rawCid)
3✔
2398

3✔
2399
                        chanInfo := NewChannelUpdateInfo(
3✔
2400
                                cid, time.Time{}, time.Time{},
3✔
2401
                        )
3✔
2402

3✔
2403
                        if !withTimestamps {
3✔
2404
                                channelsPerBlock[cid.BlockHeight] = append(
×
2405
                                        channelsPerBlock[cid.BlockHeight],
×
2406
                                        chanInfo,
×
2407
                                )
×
2408

×
2409
                                continue
×
2410
                        }
2411

2412
                        node1Key, node2Key := computeEdgePolicyKeys(&edgeInfo)
3✔
2413

3✔
2414
                        rawPolicy := edges.Get(node1Key)
3✔
2415
                        if len(rawPolicy) != 0 {
6✔
2416
                                r := bytes.NewReader(rawPolicy)
3✔
2417

3✔
2418
                                edge, err := deserializeChanEdgePolicyRaw(r)
3✔
2419
                                if err != nil && !errors.Is(
3✔
2420
                                        err, ErrEdgePolicyOptionalFieldNotFound,
3✔
2421
                                ) {
3✔
2422

×
2423
                                        return err
×
2424
                                }
×
2425

2426
                                chanInfo.Node1UpdateTimestamp = edge.LastUpdate
3✔
2427
                        }
2428

2429
                        rawPolicy = edges.Get(node2Key)
3✔
2430
                        if len(rawPolicy) != 0 {
6✔
2431
                                r := bytes.NewReader(rawPolicy)
3✔
2432

3✔
2433
                                edge, err := deserializeChanEdgePolicyRaw(r)
3✔
2434
                                if err != nil && !errors.Is(
3✔
2435
                                        err, ErrEdgePolicyOptionalFieldNotFound,
3✔
2436
                                ) {
3✔
2437

×
2438
                                        return err
×
2439
                                }
×
2440

2441
                                chanInfo.Node2UpdateTimestamp = edge.LastUpdate
3✔
2442
                        }
2443

2444
                        channelsPerBlock[cid.BlockHeight] = append(
3✔
2445
                                channelsPerBlock[cid.BlockHeight], chanInfo,
3✔
2446
                        )
3✔
2447
                }
2448

2449
                return nil
3✔
2450
        }, func() {
3✔
2451
                channelsPerBlock = make(map[uint32][]ChannelUpdateInfo)
3✔
2452
        })
3✔
2453

2454
        switch {
3✔
2455
        // If we don't know of any channels yet, then there's nothing to
2456
        // filter, so we'll return an empty slice.
2457
        case err == ErrGraphNoEdgesFound || len(channelsPerBlock) == 0:
3✔
2458
                return nil, nil
3✔
2459

2460
        case err != nil:
×
2461
                return nil, err
×
2462
        }
2463

2464
        // Return the channel ranges in ascending block height order.
2465
        blocks := make([]uint32, 0, len(channelsPerBlock))
3✔
2466
        for block := range channelsPerBlock {
6✔
2467
                blocks = append(blocks, block)
3✔
2468
        }
3✔
2469
        sort.Slice(blocks, func(i, j int) bool {
6✔
2470
                return blocks[i] < blocks[j]
3✔
2471
        })
3✔
2472

2473
        channelRanges := make([]BlockChannelRange, 0, len(channelsPerBlock))
3✔
2474
        for _, block := range blocks {
6✔
2475
                channelRanges = append(channelRanges, BlockChannelRange{
3✔
2476
                        Height:   block,
3✔
2477
                        Channels: channelsPerBlock[block],
3✔
2478
                })
3✔
2479
        }
3✔
2480

2481
        return channelRanges, nil
3✔
2482
}
2483

2484
// FetchChanInfos returns the set of channel edges that correspond to the passed
2485
// channel ID's. If an edge is the query is unknown to the database, it will
2486
// skipped and the result will contain only those edges that exist at the time
2487
// of the query. This can be used to respond to peer queries that are seeking to
2488
// fill in gaps in their view of the channel graph.
2489
func (c *ChannelGraph) FetchChanInfos(chanIDs []uint64) ([]ChannelEdge, error) {
3✔
2490
        return c.fetchChanInfos(nil, chanIDs)
3✔
2491
}
3✔
2492

2493
// fetchChanInfos returns the set of channel edges that correspond to the passed
2494
// channel ID's. If an edge is the query is unknown to the database, it will
2495
// skipped and the result will contain only those edges that exist at the time
2496
// of the query. This can be used to respond to peer queries that are seeking to
2497
// fill in gaps in their view of the channel graph.
2498
//
2499
// NOTE: An optional transaction may be provided. If none is provided, then a
2500
// new one will be created.
2501
func (c *ChannelGraph) fetchChanInfos(tx kvdb.RTx, chanIDs []uint64) (
2502
        []ChannelEdge, error) {
3✔
2503
        // TODO(roasbeef): sort cids?
3✔
2504

3✔
2505
        var (
3✔
2506
                chanEdges []ChannelEdge
3✔
2507
                cidBytes  [8]byte
3✔
2508
        )
3✔
2509

3✔
2510
        fetchChanInfos := func(tx kvdb.RTx) error {
6✔
2511
                edges := tx.ReadBucket(edgeBucket)
3✔
2512
                if edges == nil {
3✔
2513
                        return ErrGraphNoEdgesFound
×
2514
                }
×
2515
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
2516
                if edgeIndex == nil {
3✔
2517
                        return ErrGraphNoEdgesFound
×
2518
                }
×
2519
                nodes := tx.ReadBucket(nodeBucket)
3✔
2520
                if nodes == nil {
3✔
2521
                        return ErrGraphNotFound
×
2522
                }
×
2523

2524
                for _, cid := range chanIDs {
6✔
2525
                        byteOrder.PutUint64(cidBytes[:], cid)
3✔
2526

3✔
2527
                        // First, we'll fetch the static edge information. If
3✔
2528
                        // the edge is unknown, we will skip the edge and
3✔
2529
                        // continue gathering all known edges.
3✔
2530
                        edgeInfo, err := fetchChanEdgeInfo(
3✔
2531
                                edgeIndex, cidBytes[:],
3✔
2532
                        )
3✔
2533
                        switch {
3✔
2534
                        case errors.Is(err, ErrEdgeNotFound):
×
2535
                                continue
×
2536
                        case err != nil:
×
2537
                                return err
×
2538
                        }
2539

2540
                        // With the static information obtained, we'll now
2541
                        // fetch the dynamic policy info.
2542
                        edge1, edge2, err := fetchChanEdgePolicies(
3✔
2543
                                edgeIndex, edges, cidBytes[:],
3✔
2544
                        )
3✔
2545
                        if err != nil {
3✔
2546
                                return err
×
2547
                        }
×
2548

2549
                        node1, err := fetchLightningNode(
3✔
2550
                                nodes, edgeInfo.NodeKey1Bytes[:],
3✔
2551
                        )
3✔
2552
                        if err != nil {
3✔
2553
                                return err
×
2554
                        }
×
2555

2556
                        node2, err := fetchLightningNode(
3✔
2557
                                nodes, edgeInfo.NodeKey2Bytes[:],
3✔
2558
                        )
3✔
2559
                        if err != nil {
3✔
2560
                                return err
×
2561
                        }
×
2562

2563
                        chanEdges = append(chanEdges, ChannelEdge{
3✔
2564
                                Info:    &edgeInfo,
3✔
2565
                                Policy1: edge1,
3✔
2566
                                Policy2: edge2,
3✔
2567
                                Node1:   &node1,
3✔
2568
                                Node2:   &node2,
3✔
2569
                        })
3✔
2570
                }
2571
                return nil
3✔
2572
        }
2573

2574
        if tx == nil {
6✔
2575
                err := kvdb.View(c.db, fetchChanInfos, func() {
6✔
2576
                        chanEdges = nil
3✔
2577
                })
3✔
2578
                if err != nil {
3✔
2579
                        return nil, err
×
2580
                }
×
2581

2582
                return chanEdges, nil
3✔
2583
        }
2584

2585
        err := fetchChanInfos(tx)
×
2586
        if err != nil {
×
2587
                return nil, err
×
2588
        }
×
2589

2590
        return chanEdges, nil
×
2591
}
2592

2593
func delEdgeUpdateIndexEntry(edgesBucket kvdb.RwBucket, chanID uint64,
2594
        edge1, edge2 *models.ChannelEdgePolicy) error {
3✔
2595

3✔
2596
        // First, we'll fetch the edge update index bucket which currently
3✔
2597
        // stores an entry for the channel we're about to delete.
3✔
2598
        updateIndex := edgesBucket.NestedReadWriteBucket(edgeUpdateIndexBucket)
3✔
2599
        if updateIndex == nil {
3✔
2600
                // No edges in bucket, return early.
×
2601
                return nil
×
2602
        }
×
2603

2604
        // Now that we have the bucket, we'll attempt to construct a template
2605
        // for the index key: updateTime || chanid.
2606
        var indexKey [8 + 8]byte
3✔
2607
        byteOrder.PutUint64(indexKey[8:], chanID)
3✔
2608

3✔
2609
        // With the template constructed, we'll attempt to delete an entry that
3✔
2610
        // would have been created by both edges: we'll alternate the update
3✔
2611
        // times, as one may had overridden the other.
3✔
2612
        if edge1 != nil {
6✔
2613
                byteOrder.PutUint64(
3✔
2614
                        indexKey[:8], uint64(edge1.LastUpdate.Unix()),
3✔
2615
                )
3✔
2616
                if err := updateIndex.Delete(indexKey[:]); err != nil {
3✔
2617
                        return err
×
2618
                }
×
2619
        }
2620

2621
        // We'll also attempt to delete the entry that may have been created by
2622
        // the second edge.
2623
        if edge2 != nil {
6✔
2624
                byteOrder.PutUint64(
3✔
2625
                        indexKey[:8], uint64(edge2.LastUpdate.Unix()),
3✔
2626
                )
3✔
2627
                if err := updateIndex.Delete(indexKey[:]); err != nil {
3✔
2628
                        return err
×
2629
                }
×
2630
        }
2631

2632
        return nil
3✔
2633
}
2634

2635
// delChannelEdgeUnsafe deletes the edge with the given chanID from the graph
2636
// cache. It then goes on to delete any policy info and edge info for this
2637
// channel from the DB and finally, if isZombie is true, it will add an entry
2638
// for this channel in the zombie index.
2639
//
2640
// NOTE: this method MUST only be called if the cacheMu has already been
2641
// acquired.
2642
func (c *ChannelGraph) delChannelEdgeUnsafe(edges, edgeIndex, chanIndex,
2643
        zombieIndex kvdb.RwBucket, chanID []byte, isZombie,
2644
        strictZombie bool) error {
3✔
2645

3✔
2646
        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
2647
        if err != nil {
3✔
2648
                return err
×
2649
        }
×
2650

2651
        if c.graphCache != nil {
6✔
2652
                c.graphCache.RemoveChannel(
3✔
2653
                        edgeInfo.NodeKey1Bytes, edgeInfo.NodeKey2Bytes,
3✔
2654
                        edgeInfo.ChannelID,
3✔
2655
                )
3✔
2656
        }
3✔
2657

2658
        // We'll also remove the entry in the edge update index bucket before
2659
        // we delete the edges themselves so we can access their last update
2660
        // times.
2661
        cid := byteOrder.Uint64(chanID)
3✔
2662
        edge1, edge2, err := fetchChanEdgePolicies(edgeIndex, edges, chanID)
3✔
2663
        if err != nil {
3✔
2664
                return err
×
2665
        }
×
2666
        err = delEdgeUpdateIndexEntry(edges, cid, edge1, edge2)
3✔
2667
        if err != nil {
3✔
2668
                return err
×
2669
        }
×
2670

2671
        // The edge key is of the format pubKey || chanID. First we construct
2672
        // the latter half, populating the channel ID.
2673
        var edgeKey [33 + 8]byte
3✔
2674
        copy(edgeKey[33:], chanID)
3✔
2675

3✔
2676
        // With the latter half constructed, copy over the first public key to
3✔
2677
        // delete the edge in this direction, then the second to delete the
3✔
2678
        // edge in the opposite direction.
3✔
2679
        copy(edgeKey[:33], edgeInfo.NodeKey1Bytes[:])
3✔
2680
        if edges.Get(edgeKey[:]) != nil {
6✔
2681
                if err := edges.Delete(edgeKey[:]); err != nil {
3✔
2682
                        return err
×
2683
                }
×
2684
        }
2685
        copy(edgeKey[:33], edgeInfo.NodeKey2Bytes[:])
3✔
2686
        if edges.Get(edgeKey[:]) != nil {
6✔
2687
                if err := edges.Delete(edgeKey[:]); err != nil {
3✔
2688
                        return err
×
2689
                }
×
2690
        }
2691

2692
        // As part of deleting the edge we also remove all disabled entries
2693
        // from the edgePolicyDisabledIndex bucket. We do that for both
2694
        // directions.
2695
        err = updateEdgePolicyDisabledIndex(edges, cid, false, false)
3✔
2696
        if err != nil {
3✔
2697
                return err
×
2698
        }
×
2699
        err = updateEdgePolicyDisabledIndex(edges, cid, true, false)
3✔
2700
        if err != nil {
3✔
2701
                return err
×
2702
        }
×
2703

2704
        // With the edge data deleted, we can purge the information from the two
2705
        // edge indexes.
2706
        if err := edgeIndex.Delete(chanID); err != nil {
3✔
2707
                return err
×
2708
        }
×
2709
        var b bytes.Buffer
3✔
2710
        if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
3✔
2711
                return err
×
2712
        }
×
2713
        if err := chanIndex.Delete(b.Bytes()); err != nil {
3✔
2714
                return err
×
2715
        }
×
2716

2717
        // Finally, we'll mark the edge as a zombie within our index if it's
2718
        // being removed due to the channel becoming a zombie. We do this to
2719
        // ensure we don't store unnecessary data for spent channels.
2720
        if !isZombie {
6✔
2721
                return nil
3✔
2722
        }
3✔
2723

2724
        nodeKey1, nodeKey2 := edgeInfo.NodeKey1Bytes, edgeInfo.NodeKey2Bytes
3✔
2725
        if strictZombie {
3✔
2726
                nodeKey1, nodeKey2 = makeZombiePubkeys(&edgeInfo, edge1, edge2)
×
2727
        }
×
2728

2729
        return markEdgeZombie(
3✔
2730
                zombieIndex, byteOrder.Uint64(chanID), nodeKey1, nodeKey2,
3✔
2731
        )
3✔
2732
}
2733

2734
// makeZombiePubkeys derives the node pubkeys to store in the zombie index for a
2735
// particular pair of channel policies. The return values are one of:
2736
//  1. (pubkey1, pubkey2)
2737
//  2. (pubkey1, blank)
2738
//  3. (blank, pubkey2)
2739
//
2740
// A blank pubkey means that corresponding node will be unable to resurrect a
2741
// channel on its own. For example, node1 may continue to publish recent
2742
// updates, but node2 has fallen way behind. After marking an edge as a zombie,
2743
// we don't want another fresh update from node1 to resurrect, as the edge can
2744
// only become live once node2 finally sends something recent.
2745
//
2746
// In the case where we have neither update, we allow either party to resurrect
2747
// the channel. If the channel were to be marked zombie again, it would be
2748
// marked with the correct lagging channel since we received an update from only
2749
// one side.
2750
func makeZombiePubkeys(info *models.ChannelEdgeInfo,
2751
        e1, e2 *models.ChannelEdgePolicy) ([33]byte, [33]byte) {
×
2752

×
2753
        switch {
×
2754
        // If we don't have either edge policy, we'll return both pubkeys so
2755
        // that the channel can be resurrected by either party.
2756
        case e1 == nil && e2 == nil:
×
2757
                return info.NodeKey1Bytes, info.NodeKey2Bytes
×
2758

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

2766
        // Otherwise, we're missing edge2 or edge2 is the older side, so we
2767
        // return a blank pubkey for edge1. In this case, only an update from
2768
        // edge2 can resurect the channel.
2769
        default:
×
2770
                return [33]byte{}, info.NodeKey2Bytes
×
2771
        }
2772
}
2773

2774
// UpdateEdgePolicy updates the edge routing policy for a single directed edge
2775
// within the database for the referenced channel. The `flags` attribute within
2776
// the ChannelEdgePolicy determines which of the directed edges are being
2777
// updated. If the flag is 1, then the first node's information is being
2778
// updated, otherwise it's the second node's information. The node ordering is
2779
// determined by the lexicographical ordering of the identity public keys of the
2780
// nodes on either side of the channel.
2781
func (c *ChannelGraph) UpdateEdgePolicy(edge *models.ChannelEdgePolicy,
2782
        op ...batch.SchedulerOption) error {
3✔
2783

3✔
2784
        var (
3✔
2785
                isUpdate1    bool
3✔
2786
                edgeNotFound bool
3✔
2787
        )
3✔
2788

3✔
2789
        r := &batch.Request{
3✔
2790
                Reset: func() {
6✔
2791
                        isUpdate1 = false
3✔
2792
                        edgeNotFound = false
3✔
2793
                },
3✔
2794
                Update: func(tx kvdb.RwTx) error {
3✔
2795
                        var err error
3✔
2796
                        isUpdate1, err = updateEdgePolicy(
3✔
2797
                                tx, edge, c.graphCache,
3✔
2798
                        )
3✔
2799

3✔
2800
                        // Silence ErrEdgeNotFound so that the batch can
3✔
2801
                        // succeed, but propagate the error via local state.
×
2802
                        if errors.Is(err, ErrEdgeNotFound) {
×
2803
                                edgeNotFound = true
2804
                                return nil
2805
                        }
2806

3✔
2807
                        return err
×
2808
                },
×
2809
                OnCommit: func(err error) error {
×
2810
                        switch {
2811
                        case err != nil:
3✔
2812
                                return err
2813
                        case edgeNotFound:
3✔
2814
                                return ErrEdgeNotFound
3✔
2815
                        default:
×
2816
                                c.updateEdgeCache(edge, isUpdate1)
×
2817
                                return nil
×
2818
                        }
×
2819
                },
3✔
2820
        }
3✔
2821

3✔
2822
        for _, f := range op {
2823
                f(r)
2824
        }
2825

2826
        return c.chanScheduler.Execute(r)
6✔
2827
}
3✔
2828

3✔
2829
func (c *ChannelGraph) updateEdgeCache(e *models.ChannelEdgePolicy,
2830
        isUpdate1 bool) {
3✔
2831

2832
        // If an entry for this channel is found in reject cache, we'll modify
2833
        // the entry with the updated timestamp for the direction that was just
2834
        // written. If the edge doesn't exist, we'll load the cache entry lazily
3✔
2835
        // during the next query for this edge.
3✔
2836
        if entry, ok := c.rejectCache.get(e.ChannelID); ok {
3✔
2837
                if isUpdate1 {
3✔
2838
                        entry.upd1Time = e.LastUpdate.Unix()
3✔
2839
                } else {
3✔
2840
                        entry.upd2Time = e.LastUpdate.Unix()
6✔
2841
                }
6✔
2842
                c.rejectCache.insert(e.ChannelID, entry)
3✔
2843
        }
6✔
2844

3✔
2845
        // If an entry for this channel is found in channel cache, we'll modify
3✔
2846
        // the entry with the updated policy for the direction that was just
3✔
2847
        // written. If the edge doesn't exist, we'll defer loading the info and
2848
        // policies and lazily read from disk during the next query.
2849
        if channel, ok := c.chanCache.get(e.ChannelID); ok {
2850
                if isUpdate1 {
2851
                        channel.Policy1 = e
2852
                } else {
2853
                        channel.Policy2 = e
6✔
2854
                }
6✔
2855
                c.chanCache.insert(e.ChannelID, channel)
3✔
2856
        }
6✔
2857
}
3✔
2858

3✔
2859
// updateEdgePolicy attempts to update an edge's policy within the relevant
3✔
2860
// buckets using an existing database transaction. The returned boolean will be
2861
// true if the updated policy belongs to node1, and false if the policy belonged
2862
// to node2.
2863
func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy,
2864
        graphCache *GraphCache) (bool, error) {
2865

2866
        edges := tx.ReadWriteBucket(edgeBucket)
2867
        if edges == nil {
2868
                return false, ErrEdgeNotFound
3✔
2869
        }
3✔
2870
        edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3✔
2871
        if edgeIndex == nil {
3✔
2872
                return false, ErrEdgeNotFound
×
2873
        }
×
2874

3✔
2875
        // Create the channelID key be converting the channel ID
3✔
2876
        // integer into a byte slice.
×
2877
        var chanID [8]byte
×
2878
        byteOrder.PutUint64(chanID[:], edge.ChannelID)
2879

2880
        // With the channel ID, we then fetch the value storing the two
2881
        // nodes which connect this channel edge.
3✔
2882
        nodeInfo := edgeIndex.Get(chanID[:])
3✔
2883
        if nodeInfo == nil {
3✔
2884
                return false, ErrEdgeNotFound
3✔
2885
        }
3✔
2886

3✔
2887
        // Depending on the flags value passed above, either the first
3✔
2888
        // or second edge policy is being updated.
×
2889
        var fromNode, toNode []byte
×
2890
        var isUpdate1 bool
2891
        if edge.ChannelFlags&lnwire.ChanUpdateDirection == 0 {
2892
                fromNode = nodeInfo[:33]
2893
                toNode = nodeInfo[33:66]
3✔
2894
                isUpdate1 = true
3✔
2895
        } else {
6✔
2896
                fromNode = nodeInfo[33:66]
3✔
2897
                toNode = nodeInfo[:33]
3✔
2898
                isUpdate1 = false
3✔
2899
        }
6✔
2900

3✔
2901
        // Finally, with the direction of the edge being updated
3✔
2902
        // identified, we update the on-disk edge representation.
3✔
2903
        err := putChanEdgePolicy(edges, edge, fromNode, toNode)
3✔
2904
        if err != nil {
2905
                return false, err
2906
        }
2907

3✔
2908
        var (
3✔
2909
                fromNodePubKey route.Vertex
×
2910
                toNodePubKey   route.Vertex
×
2911
        )
2912
        copy(fromNodePubKey[:], fromNode)
3✔
2913
        copy(toNodePubKey[:], toNode)
3✔
2914

3✔
2915
        if graphCache != nil {
3✔
2916
                graphCache.UpdatePolicy(
3✔
2917
                        edge, fromNodePubKey, toNodePubKey, isUpdate1,
3✔
2918
                )
3✔
2919
        }
6✔
2920

3✔
2921
        return isUpdate1, nil
3✔
2922
}
3✔
2923

3✔
2924
// isPublic determines whether the node is seen as public within the graph from
2925
// the source node's point of view. An existing database transaction can also be
3✔
2926
// specified.
2927
func (c *ChannelGraph) isPublic(tx kvdb.RTx, nodePub route.Vertex,
2928
        sourcePubKey []byte) (bool, error) {
2929

2930
        // In order to determine whether this node is publicly advertised within
2931
        // the graph, we'll need to look at all of its edges and check whether
2932
        // they extend to any other node than the source node. errDone will be
3✔
2933
        // used to terminate the check early.
3✔
2934
        nodeIsPublic := false
3✔
2935
        errDone := errors.New("done")
3✔
2936
        err := c.ForEachNodeChannelTx(tx, nodePub, func(tx kvdb.RTx,
3✔
2937
                info *models.ChannelEdgeInfo, _ *models.ChannelEdgePolicy,
3✔
2938
                _ *models.ChannelEdgePolicy) error {
3✔
2939

3✔
2940
                // If this edge doesn't extend to the source node, we'll
3✔
2941
                // terminate our search as we can now conclude that the node is
3✔
2942
                // publicly advertised within the graph due to the local node
6✔
2943
                // knowing of the current edge.
3✔
2944
                if !bytes.Equal(info.NodeKey1Bytes[:], sourcePubKey) &&
3✔
2945
                        !bytes.Equal(info.NodeKey2Bytes[:], sourcePubKey) {
3✔
2946

3✔
2947
                        nodeIsPublic = true
3✔
2948
                        return errDone
3✔
2949
                }
6✔
2950

3✔
2951
                // Since the edge _does_ extend to the source node, we'll also
3✔
2952
                // need to ensure that this is a public edge.
3✔
2953
                if info.AuthProof != nil {
3✔
2954
                        nodeIsPublic = true
2955
                        return errDone
2956
                }
2957

6✔
2958
                // Otherwise, we'll continue our search.
3✔
2959
                return nil
3✔
2960
        })
3✔
2961
        if err != nil && err != errDone {
2962
                return false, err
2963
        }
3✔
2964

2965
        return nodeIsPublic, nil
3✔
2966
}
×
2967

×
2968
// FetchLightningNodeTx attempts to look up a target node by its identity
2969
// public key. If the node isn't found in the database, then
3✔
2970
// ErrGraphNodeNotFound is returned. An optional transaction may be provided.
2971
// If none is provided, then a new one will be created.
2972
func (c *ChannelGraph) FetchLightningNodeTx(tx kvdb.RTx, nodePub route.Vertex) (
2973
        *models.LightningNode, error) {
2974

2975
        return c.fetchLightningNode(tx, nodePub)
2976
}
2977

×
2978
// FetchLightningNode attempts to look up a target node by its identity public
×
2979
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
×
2980
// returned.
×
2981
func (c *ChannelGraph) FetchLightningNode(nodePub route.Vertex) (
2982
        *models.LightningNode, error) {
2983

2984
        return c.fetchLightningNode(nil, nodePub)
2985
}
2986

3✔
2987
// fetchLightningNode attempts to look up a target node by its identity public
3✔
2988
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
3✔
2989
// returned. An optional transaction may be provided. If none is provided, then
3✔
2990
// a new one will be created.
2991
func (c *ChannelGraph) fetchLightningNode(tx kvdb.RTx,
2992
        nodePub route.Vertex) (*models.LightningNode, error) {
2993

2994
        var node *models.LightningNode
2995
        fetch := func(tx kvdb.RTx) error {
2996
                // First grab the nodes bucket which stores the mapping from
3✔
2997
                // pubKey to node information.
3✔
2998
                nodes := tx.ReadBucket(nodeBucket)
3✔
2999
                if nodes == nil {
6✔
3000
                        return ErrGraphNotFound
3✔
3001
                }
3✔
3002

3✔
3003
                // If a key for this serialized public key isn't found, then
3✔
3004
                // the target node doesn't exist within the database.
×
3005
                nodeBytes := nodes.Get(nodePub[:])
×
3006
                if nodeBytes == nil {
3007
                        return ErrGraphNodeNotFound
3008
                }
3009

3✔
3010
                // If the node is found, then we can de deserialize the node
6✔
3011
                // information to return to the user.
3✔
3012
                nodeReader := bytes.NewReader(nodeBytes)
3✔
3013
                n, err := deserializeLightningNode(nodeReader)
3014
                if err != nil {
3015
                        return err
3016
                }
3✔
3017

3✔
3018
                node = &n
3✔
3019

×
3020
                return nil
×
3021
        }
3022

3✔
3023
        if tx == nil {
3✔
3024
                err := kvdb.View(
3✔
3025
                        c.db, fetch, func() {
3026
                                node = nil
3027
                        },
6✔
3028
                )
3✔
3029
                if err != nil {
6✔
3030
                        return nil, err
3✔
3031
                }
3✔
3032

3033
                return node, nil
6✔
3034
        }
3✔
3035

3✔
3036
        err := fetch(tx)
3037
        if err != nil {
3✔
3038
                return nil, err
3039
        }
3040

×
3041
        return node, nil
×
3042
}
×
3043

×
3044
// graphCacheNode is a struct that wraps a LightningNode in a way that it can be
3045
// cached in the graph cache.
×
3046
type graphCacheNode struct {
3047
        pubKeyBytes route.Vertex
3048
        features    *lnwire.FeatureVector
3049
}
3050

3051
// newGraphCacheNode returns a new cache optimized node.
3052
func newGraphCacheNode(pubKey route.Vertex,
3053
        features *lnwire.FeatureVector) *graphCacheNode {
3054

3055
        return &graphCacheNode{
3056
                pubKeyBytes: pubKey,
3057
                features:    features,
3✔
3058
        }
3✔
3059
}
3✔
3060

3✔
3061
// PubKey returns the node's public identity key.
3✔
3062
func (n *graphCacheNode) PubKey() route.Vertex {
3✔
3063
        return n.pubKeyBytes
3✔
3064
}
3065

3066
// Features returns the node's features.
3✔
3067
func (n *graphCacheNode) Features() *lnwire.FeatureVector {
3✔
3068
        return n.features
3✔
3069
}
3070

3071
// ForEachChannel iterates through all channels of this node, executing the
3✔
3072
// passed callback with an edge info structure and the policies of each end
3✔
3073
// of the channel. The first edge policy is the outgoing edge *to* the
3✔
3074
// connecting node, while the second is the incoming edge *from* the
3075
// connecting node. If the callback returns an error, then the iteration is
3076
// halted with the error propagated back up to the caller.
3077
//
3078
// Unknown policies are passed into the callback as nil values.
3079
func (n *graphCacheNode) ForEachChannel(tx kvdb.RTx,
3080
        cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3081
                *models.ChannelEdgePolicy) error) error {
3082

3083
        return nodeTraversal(tx, n.pubKeyBytes[:], nil, cb)
3084
}
3085

3✔
3086
var _ GraphCacheNode = (*graphCacheNode)(nil)
3✔
3087

3✔
3088
// HasLightningNode determines if the graph has a vertex identified by the
3✔
3089
// target node identity public key. If the node exists in the database, a
3090
// timestamp of when the data for the node was lasted updated is returned along
3091
// with a true boolean. Otherwise, an empty time.Time is returned with a false
3092
// boolean.
3093
func (c *ChannelGraph) HasLightningNode(nodePub [33]byte) (time.Time, bool,
3094
        error) {
3095

3096
        var (
3097
                updateTime time.Time
3098
                exists     bool
3✔
3099
        )
3✔
3100

3✔
3101
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
3✔
3102
                // First grab the nodes bucket which stores the mapping from
3✔
3103
                // pubKey to node information.
3✔
3104
                nodes := tx.ReadBucket(nodeBucket)
3✔
3105
                if nodes == nil {
6✔
3106
                        return ErrGraphNotFound
3✔
3107
                }
3✔
3108

3✔
3109
                // If a key for this serialized public key isn't found, we can
3✔
3110
                // exit early.
×
3111
                nodeBytes := nodes.Get(nodePub[:])
×
3112
                if nodeBytes == nil {
3113
                        exists = false
3114
                        return nil
3115
                }
3✔
3116

6✔
3117
                // Otherwise we continue on to obtain the time stamp
3✔
3118
                // representing the last time the data for this node was
3✔
3119
                // updated.
3✔
3120
                nodeReader := bytes.NewReader(nodeBytes)
3121
                node, err := deserializeLightningNode(nodeReader)
3122
                if err != nil {
3123
                        return err
3124
                }
3✔
3125

3✔
3126
                exists = true
3✔
3127
                updateTime = node.LastUpdate
×
3128
                return nil
×
3129
        }, func() {
3130
                updateTime = time.Time{}
3✔
3131
                exists = false
3✔
3132
        })
3✔
3133
        if err != nil {
3✔
3134
                return time.Time{}, exists, err
3✔
3135
        }
3✔
3136

3✔
3137
        return updateTime, exists, nil
3✔
3138
}
×
3139

×
3140
// nodeTraversal is used to traverse all channels of a node given by its
3141
// public key and passes channel information into the specified callback.
3✔
3142
func nodeTraversal(tx kvdb.RTx, nodePub []byte, db kvdb.Backend,
3143
        cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3144
                *models.ChannelEdgePolicy) error) error {
3145

3146
        traversal := func(tx kvdb.RTx) error {
3147
                edges := tx.ReadBucket(edgeBucket)
3148
                if edges == nil {
3✔
3149
                        return ErrGraphNotFound
3✔
3150
                }
6✔
3151
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
3152
                if edgeIndex == nil {
3✔
3153
                        return ErrGraphNoEdgesFound
×
3154
                }
×
3155

3✔
3156
                // In order to reach all the edges for this node, we take
3✔
3157
                // advantage of the construction of the key-space within the
×
3158
                // edge bucket. The keys are stored in the form: pubKey ||
×
3159
                // chanID. Therefore, starting from a chanID of zero, we can
3160
                // scan forward in the bucket, grabbing all the edges for the
3161
                // node. Once the prefix no longer matches, then we know we're
3162
                // done.
3163
                var nodeStart [33 + 8]byte
3164
                copy(nodeStart[:], nodePub)
3165
                copy(nodeStart[33:], chanStart[:])
3166

3167
                // Starting from the key pubKey || 0, we seek forward in the
3✔
3168
                // bucket until the retrieved key no longer has the public key
3✔
3169
                // as its prefix. This indicates that we've stepped over into
3✔
3170
                // another node's edges, so we can terminate our scan.
3✔
3171
                edgeCursor := edges.ReadCursor()
3✔
3172
                for nodeEdge, _ := edgeCursor.Seek(nodeStart[:]); bytes.HasPrefix(nodeEdge, nodePub); nodeEdge, _ = edgeCursor.Next() { //nolint:ll
3✔
3173
                        // If the prefix still matches, the channel id is
3✔
3174
                        // returned in nodeEdge. Channel id is used to lookup
3✔
3175
                        // the node at the other end of the channel and both
3✔
3176
                        // edge policies.
6✔
3177
                        chanID := nodeEdge[33:]
3✔
3178
                        edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
3179
                        if err != nil {
3✔
3180
                                return err
3✔
3181
                        }
3✔
3182

3✔
3183
                        outgoingPolicy, err := fetchChanEdgePolicy(
3✔
3184
                                edges, chanID, nodePub,
×
3185
                        )
×
3186
                        if err != nil {
3187
                                return err
3✔
3188
                        }
3✔
3189

3✔
3190
                        otherNode, err := edgeInfo.OtherNodeKeyBytes(nodePub)
3✔
3191
                        if err != nil {
×
3192
                                return err
×
3193
                        }
3194

3✔
3195
                        incomingPolicy, err := fetchChanEdgePolicy(
3✔
3196
                                edges, chanID, otherNode[:],
×
3197
                        )
×
3198
                        if err != nil {
3199
                                return err
3✔
3200
                        }
3✔
3201

3✔
3202
                        // Finally, we execute the callback.
3✔
3203
                        err = cb(tx, &edgeInfo, outgoingPolicy, incomingPolicy)
×
3204
                        if err != nil {
×
3205
                                return err
3206
                        }
3207
                }
3✔
3208

6✔
3209
                return nil
3✔
3210
        }
3✔
3211

3212
        // If no transaction was provided, then we'll create a new transaction
3213
        // to execute the transaction within.
3✔
3214
        if tx == nil {
3215
                return kvdb.View(db, traversal, func() {})
3216
        }
3217

3218
        // Otherwise, we re-use the existing transaction to execute the graph
6✔
3219
        // traversal.
6✔
3220
        return traversal(tx)
3221
}
3222

3223
// ForEachNodeChannel iterates through all channels of the given node,
3224
// executing the passed callback with an edge info structure and the policies
3✔
3225
// of each end of the channel. The first edge policy is the outgoing edge *to*
3226
// the connecting node, while the second is the incoming edge *from* the
3227
// connecting node. If the callback returns an error, then the iteration is
3228
// halted with the error propagated back up to the caller.
3229
//
3230
// Unknown policies are passed into the callback as nil values.
3231
func (c *ChannelGraph) ForEachNodeChannel(nodePub route.Vertex,
3232
        cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3233
                *models.ChannelEdgePolicy) error) error {
3234

3235
        return nodeTraversal(nil, nodePub[:], c.db, cb)
3236
}
3237

3✔
3238
// ForEachNodeChannelTx iterates through all channels of the given node,
3✔
3239
// executing the passed callback with an edge info structure and the policies
3✔
3240
// of each end of the channel. The first edge policy is the outgoing edge *to*
3✔
3241
// the connecting node, while the second is the incoming edge *from* the
3242
// connecting node. If the callback returns an error, then the iteration is
3243
// halted with the error propagated back up to the caller.
3244
//
3245
// Unknown policies are passed into the callback as nil values.
3246
//
3247
// If the caller wishes to re-use an existing boltdb transaction, then it
3248
// should be passed as the first argument.  Otherwise, the first argument should
3249
// be nil and a fresh transaction will be created to execute the graph
3250
// traversal.
3251
func (c *ChannelGraph) ForEachNodeChannelTx(tx kvdb.RTx,
3252
        nodePub route.Vertex, cb func(kvdb.RTx, *models.ChannelEdgeInfo,
3253
                *models.ChannelEdgePolicy,
3254
                *models.ChannelEdgePolicy) error) error {
3255

3256
        return nodeTraversal(tx, nodePub[:], c.db, cb)
3257
}
3258

3✔
3259
// FetchOtherNode attempts to fetch the full LightningNode that's opposite of
3✔
3260
// the target node in the channel. This is useful when one knows the pubkey of
3✔
3261
// one of the nodes, and wishes to obtain the full LightningNode for the other
3✔
3262
// end of the channel.
3263
func (c *ChannelGraph) FetchOtherNode(tx kvdb.RTx,
3264
        channel *models.ChannelEdgeInfo, thisNodeKey []byte) (
3265
        *models.LightningNode, error) {
3266

3267
        // Ensure that the node passed in is actually a member of the channel.
3268
        var targetNodeBytes [33]byte
3269
        switch {
3✔
3270
        case bytes.Equal(channel.NodeKey1Bytes[:], thisNodeKey):
3✔
3271
                targetNodeBytes = channel.NodeKey2Bytes
3✔
3272
        case bytes.Equal(channel.NodeKey2Bytes[:], thisNodeKey):
3✔
3273
                targetNodeBytes = channel.NodeKey1Bytes
3✔
3274
        default:
3✔
3275
                return nil, fmt.Errorf("node not participating in this channel")
3✔
3276
        }
3✔
3277

3✔
3278
        var targetNode *models.LightningNode
×
3279
        fetchNodeFunc := func(tx kvdb.RTx) error {
×
3280
                // First grab the nodes bucket which stores the mapping from
3281
                // pubKey to node information.
3282
                nodes := tx.ReadBucket(nodeBucket)
3✔
3283
                if nodes == nil {
6✔
3284
                        return ErrGraphNotFound
3✔
3285
                }
3✔
3286

3✔
3287
                node, err := fetchLightningNode(nodes, targetNodeBytes[:])
3✔
3288
                if err != nil {
×
3289
                        return err
×
3290
                }
3291

3✔
3292
                targetNode = &node
3✔
3293

×
3294
                return nil
×
3295
        }
3296

3✔
3297
        // If the transaction is nil, then we'll need to create a new one,
3✔
3298
        // otherwise we can use the existing db transaction.
3✔
3299
        var err error
3300
        if tx == nil {
3301
                err = kvdb.View(c.db, fetchNodeFunc, func() {
3302
                        targetNode = nil
3303
                })
3✔
3304
        } else {
3✔
3305
                err = fetchNodeFunc(tx)
×
3306
        }
×
3307

×
3308
        return targetNode, err
3✔
3309
}
3✔
3310

3✔
3311
// computeEdgePolicyKeys is a helper function that can be used to compute the
3312
// keys used to index the channel edge policy info for the two nodes of the
3✔
3313
// edge. The keys for node 1 and node 2 are returned respectively.
3314
func computeEdgePolicyKeys(info *models.ChannelEdgeInfo) ([]byte, []byte) {
3315
        var (
3316
                node1Key [33 + 8]byte
3317
                node2Key [33 + 8]byte
3318
        )
3✔
3319

3✔
3320
        copy(node1Key[:], info.NodeKey1Bytes[:])
3✔
3321
        copy(node2Key[:], info.NodeKey2Bytes[:])
3✔
3322

3✔
3323
        byteOrder.PutUint64(node1Key[33:], info.ChannelID)
3✔
3324
        byteOrder.PutUint64(node2Key[33:], info.ChannelID)
3✔
3325

3✔
3326
        return node1Key[:], node2Key[:]
3✔
3327
}
3✔
3328

3✔
3329
// FetchChannelEdgesByOutpoint attempts to lookup the two directed edges for
3✔
3330
// the channel identified by the funding outpoint. If the channel can't be
3✔
3331
// found, then ErrEdgeNotFound is returned. A struct which houses the general
3✔
3332
// information for the channel itself is returned as well as two structs that
3333
// contain the routing policies for the channel in either direction.
3334
func (c *ChannelGraph) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
3335
        *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3336
        *models.ChannelEdgePolicy, error) {
3337

3338
        var (
3339
                edgeInfo *models.ChannelEdgeInfo
3340
                policy1  *models.ChannelEdgePolicy
3✔
3341
                policy2  *models.ChannelEdgePolicy
3✔
3342
        )
3✔
3343

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

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

3✔
3364
                // If the channel's outpoint doesn't exist within the outpoint
3✔
3365
                // index, then the edge does not exist.
×
3366
                chanIndex := edges.NestedReadBucket(channelPointBucket)
×
3367
                if chanIndex == nil {
3368
                        return ErrGraphNoEdgesFound
3369
                }
3370
                var b bytes.Buffer
3✔
3371
                if err := WriteOutpoint(&b, op); err != nil {
3✔
3372
                        return err
×
3373
                }
×
3374
                chanID := chanIndex.Get(b.Bytes())
3✔
3375
                if chanID == nil {
3✔
3376
                        return fmt.Errorf("%w: op=%v", ErrEdgeNotFound, op)
×
3377
                }
×
3378

3✔
3379
                // If the channel is found to exists, then we'll first retrieve
6✔
3380
                // the general information for the channel.
3✔
3381
                edge, err := fetchChanEdgeInfo(edgeIndex, chanID)
3✔
3382
                if err != nil {
3383
                        return fmt.Errorf("%w: chanID=%x", err, chanID)
3384
                }
3385
                edgeInfo = &edge
3✔
3386

3✔
3387
                // Once we have the information about the channels' parameters,
×
3388
                // we'll fetch the routing policies for each for the directed
×
3389
                // edges.
3✔
3390
                e1, e2, err := fetchChanEdgePolicies(edgeIndex, edges, chanID)
3✔
3391
                if err != nil {
3✔
3392
                        return fmt.Errorf("failed to find policy: %w", err)
3✔
3393
                }
3✔
3394

3✔
3395
                policy1 = e1
3✔
3396
                policy2 = e2
×
3397
                return nil
×
3398
        }, func() {
3399
                edgeInfo = nil
3✔
3400
                policy1 = nil
3✔
3401
                policy2 = nil
3✔
3402
        })
3✔
3403
        if err != nil {
3✔
3404
                return nil, nil, nil, err
3✔
3405
        }
3✔
3406

3✔
3407
        return edgeInfo, policy1, policy2, nil
6✔
3408
}
3✔
3409

3✔
3410
// FetchChannelEdgesByID attempts to lookup the two directed edges for the
3411
// channel identified by the channel ID. If the channel can't be found, then
3✔
3412
// ErrEdgeNotFound is returned. A struct which houses the general information
3413
// for the channel itself is returned as well as two structs that contain the
3414
// routing policies for the channel in either direction.
3415
//
3416
// ErrZombieEdge an be returned if the edge is currently marked as a zombie
3417
// within the database. In this case, the ChannelEdgePolicy's will be nil, and
3418
// the ChannelEdgeInfo will only include the public keys of each node.
3419
func (c *ChannelGraph) FetchChannelEdgesByID(chanID uint64) (
3420
        *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3421
        *models.ChannelEdgePolicy, error) {
3422

3423
        var (
3424
                edgeInfo  *models.ChannelEdgeInfo
3425
                policy1   *models.ChannelEdgePolicy
3✔
3426
                policy2   *models.ChannelEdgePolicy
3✔
3427
                channelID [8]byte
3✔
3428
        )
3✔
3429

3✔
3430
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
3✔
3431
                // First, grab the node bucket. This will be used to populate
3✔
3432
                // the Node pointers in each edge read from disk.
3✔
3433
                nodes := tx.ReadBucket(nodeBucket)
3✔
3434
                if nodes == nil {
6✔
3435
                        return ErrGraphNotFound
3✔
3436
                }
3✔
3437

3✔
3438
                // Next, grab the edge bucket which stores the edges, and also
3✔
3439
                // the index itself so we can group the directed edges together
×
3440
                // logically.
×
3441
                edges := tx.ReadBucket(edgeBucket)
3442
                if edges == nil {
3443
                        return ErrGraphNoEdgesFound
3444
                }
3445
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
3446
                if edgeIndex == nil {
3✔
3447
                        return ErrGraphNoEdgesFound
×
3448
                }
×
3449

3✔
3450
                byteOrder.PutUint64(channelID[:], chanID)
3✔
3451

×
3452
                // Now, attempt to fetch edge.
×
3453
                edge, err := fetchChanEdgeInfo(edgeIndex, channelID[:])
3454

3✔
3455
                // If it doesn't exist, we'll quickly check our zombie index to
3✔
3456
                // see if we've previously marked it as so.
3✔
3457
                if errors.Is(err, ErrEdgeNotFound) {
3✔
3458
                        // If the zombie index doesn't exist, or the edge is not
3✔
3459
                        // marked as a zombie within it, then we'll return the
3✔
3460
                        // original ErrEdgeNotFound error.
3✔
3461
                        zombieIndex := edges.NestedReadBucket(zombieBucket)
6✔
3462
                        if zombieIndex == nil {
3✔
3463
                                return ErrEdgeNotFound
3✔
3464
                        }
3✔
3465

3✔
3466
                        isZombie, pubKey1, pubKey2 := isZombieEdge(
3✔
3467
                                zombieIndex, chanID,
×
3468
                        )
×
3469
                        if !isZombie {
3470
                                return ErrEdgeNotFound
3✔
3471
                        }
3✔
3472

3✔
3473
                        // Otherwise, the edge is marked as a zombie, so we'll
6✔
3474
                        // populate the edge info with the public keys of each
3✔
3475
                        // party as this is the only information we have about
3✔
3476
                        // it and return an error signaling so.
3477
                        edgeInfo = &models.ChannelEdgeInfo{
3478
                                NodeKey1Bytes: pubKey1,
3479
                                NodeKey2Bytes: pubKey2,
3480
                        }
3481
                        return ErrZombieEdge
3✔
3482
                }
3✔
3483

3✔
3484
                // Otherwise, we'll just return the error if any.
3✔
3485
                if err != nil {
3✔
3486
                        return err
3487
                }
3488

3489
                edgeInfo = &edge
3✔
3490

×
3491
                // Then we'll attempt to fetch the accompanying policies of this
×
3492
                // edge.
3493
                e1, e2, err := fetchChanEdgePolicies(
3✔
3494
                        edgeIndex, edges, channelID[:],
3✔
3495
                )
3✔
3496
                if err != nil {
3✔
3497
                        return err
3✔
3498
                }
3✔
3499

3✔
3500
                policy1 = e1
3✔
3501
                policy2 = e2
×
3502
                return nil
×
3503
        }, func() {
3504
                edgeInfo = nil
3✔
3505
                policy1 = nil
3✔
3506
                policy2 = nil
3✔
3507
        })
3✔
3508
        if err == ErrZombieEdge {
3✔
3509
                return edgeInfo, nil, nil, err
3✔
3510
        }
3✔
3511
        if err != nil {
3✔
3512
                return nil, nil, nil, err
6✔
3513
        }
3✔
3514

3✔
3515
        return edgeInfo, policy1, policy2, nil
6✔
3516
}
3✔
3517

3✔
3518
// IsPublicNode is a helper method that determines whether the node with the
3519
// given public key is seen as a public node in the graph from the graph's
3✔
3520
// source node's point of view.
3521
func (c *ChannelGraph) IsPublicNode(pubKey [33]byte) (bool, error) {
3522
        var nodeIsPublic bool
3523
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
3524
                nodes := tx.ReadBucket(nodeBucket)
3525
                if nodes == nil {
3✔
3526
                        return ErrGraphNodesNotFound
3✔
3527
                }
6✔
3528
                ourPubKey := nodes.Get(sourceKey)
3✔
3529
                if ourPubKey == nil {
3✔
3530
                        return ErrSourceNodeNotSet
×
3531
                }
×
3532
                node, err := fetchLightningNode(nodes, pubKey[:])
3✔
3533
                if err != nil {
3✔
3534
                        return err
×
3535
                }
×
3536

3✔
3537
                nodeIsPublic, err = c.isPublic(tx, node.PubKeyBytes, ourPubKey)
3✔
3538
                return err
×
3539
        }, func() {
×
3540
                nodeIsPublic = false
3541
        })
3✔
3542
        if err != nil {
3✔
3543
                return false, err
3✔
3544
        }
3✔
3545

3✔
3546
        return nodeIsPublic, nil
3✔
3547
}
×
3548

×
3549
// genMultiSigP2WSH generates the p2wsh'd multisig script for 2 of 2 pubkeys.
3550
func genMultiSigP2WSH(aPub, bPub []byte) ([]byte, error) {
3✔
3551
        witnessScript, err := input.GenMultiSigScript(aPub, bPub)
3552
        if err != nil {
3553
                return nil, err
3554
        }
3✔
3555

3✔
3556
        // With the witness script generated, we'll now turn it into a p2wsh
3✔
3557
        // script:
×
3558
        //  * OP_0 <sha256(script)>
×
3559
        bldr := txscript.NewScriptBuilder(
3560
                txscript.WithScriptAllocSize(input.P2WSHSize),
3561
        )
3562
        bldr.AddOp(txscript.OP_0)
3563
        scriptHash := sha256.Sum256(witnessScript)
3✔
3564
        bldr.AddData(scriptHash[:])
3✔
3565

3✔
3566
        return bldr.Script()
3✔
3567
}
3✔
3568

3✔
3569
// EdgePoint couples the outpoint of a channel with the funding script that it
3✔
3570
// creates. The FilteredChainView will use this to watch for spends of this
3✔
3571
// edge point on chain. We require both of these values as depending on the
3572
// concrete implementation, either the pkScript, or the out point will be used.
3573
type EdgePoint struct {
3574
        // FundingPkScript is the p2wsh multi-sig script of the target channel.
3575
        FundingPkScript []byte
3576

3577
        // OutPoint is the outpoint of the target channel.
3578
        OutPoint wire.OutPoint
3579
}
3580

3581
// String returns a human readable version of the target EdgePoint. We return
3582
// the outpoint directly as it is enough to uniquely identify the edge point.
3583
func (e *EdgePoint) String() string {
3584
        return e.OutPoint.String()
3585
}
3586

3587
// ChannelView returns the verifiable edge information for each active channel
×
3588
// within the known channel graph. The set of UTXO's (along with their scripts)
×
3589
// returned are the ones that need to be watched on chain to detect channel
×
3590
// closes on the resident blockchain.
3591
func (c *ChannelGraph) ChannelView() ([]EdgePoint, error) {
3592
        var edgePoints []EdgePoint
3593
        if err := kvdb.View(c.db, func(tx kvdb.RTx) error {
3594
                // We're going to iterate over the entire channel index, so
3595
                // we'll need to fetch the edgeBucket to get to the index as
3✔
3596
                // it's a sub-bucket.
3✔
3597
                edges := tx.ReadBucket(edgeBucket)
6✔
3598
                if edges == nil {
3✔
3599
                        return ErrGraphNoEdgesFound
3✔
3600
                }
3✔
3601
                chanIndex := edges.NestedReadBucket(channelPointBucket)
3✔
3602
                if chanIndex == nil {
3✔
3603
                        return ErrGraphNoEdgesFound
×
3604
                }
×
3605
                edgeIndex := edges.NestedReadBucket(edgeIndexBucket)
3✔
3606
                if edgeIndex == nil {
3✔
3607
                        return ErrGraphNoEdgesFound
×
3608
                }
×
3609

3✔
3610
                // Once we have the proper bucket, we'll range over each key
3✔
3611
                // (which is the channel point for the channel) and decode it,
×
3612
                // accumulating each entry.
×
3613
                return chanIndex.ForEach(
3614
                        func(chanPointBytes, chanID []byte) error {
3615
                                chanPointReader := bytes.NewReader(
3616
                                        chanPointBytes,
3617
                                )
3✔
3618

6✔
3619
                                var chanPoint wire.OutPoint
3✔
3620
                                err := ReadOutpoint(chanPointReader, &chanPoint)
3✔
3621
                                if err != nil {
3✔
3622
                                        return err
3✔
3623
                                }
3✔
3624

3✔
3625
                                edgeInfo, err := fetchChanEdgeInfo(
3✔
3626
                                        edgeIndex, chanID,
×
3627
                                )
×
3628
                                if err != nil {
3629
                                        return err
3✔
3630
                                }
3✔
3631

3✔
3632
                                pkScript, err := genMultiSigP2WSH(
3✔
3633
                                        edgeInfo.BitcoinKey1Bytes[:],
×
3634
                                        edgeInfo.BitcoinKey2Bytes[:],
×
3635
                                )
3636
                                if err != nil {
3✔
3637
                                        return err
3✔
3638
                                }
3✔
3639

3✔
3640
                                edgePoints = append(edgePoints, EdgePoint{
3✔
3641
                                        FundingPkScript: pkScript,
×
3642
                                        OutPoint:        chanPoint,
×
3643
                                })
3644

3✔
3645
                                return nil
3✔
3646
                        },
3✔
3647
                )
3✔
3648
        }, func() {
3✔
3649
                edgePoints = nil
3✔
3650
        }); err != nil {
3651
                return nil, err
3652
        }
3✔
3653

3✔
3654
        return edgePoints, nil
3✔
3655
}
×
3656

×
3657
// MarkEdgeZombie attempts to mark a channel identified by its channel ID as a
3658
// zombie. This method is used on an ad-hoc basis, when channels need to be
3✔
3659
// marked as zombies outside the normal pruning cycle.
3660
func (c *ChannelGraph) MarkEdgeZombie(chanID uint64,
3661
        pubKey1, pubKey2 [33]byte) error {
3662

3663
        c.cacheMu.Lock()
3664
        defer c.cacheMu.Unlock()
3665

×
3666
        err := kvdb.Batch(c.db, func(tx kvdb.RwTx) error {
×
3667
                edges := tx.ReadWriteBucket(edgeBucket)
×
3668
                if edges == nil {
×
3669
                        return ErrGraphNoEdgesFound
×
3670
                }
×
3671
                zombieIndex, err := edges.CreateBucketIfNotExists(zombieBucket)
×
3672
                if err != nil {
×
3673
                        return fmt.Errorf("unable to create zombie "+
×
3674
                                "bucket: %w", err)
×
3675
                }
×
3676

×
3677
                if c.graphCache != nil {
×
3678
                        c.graphCache.RemoveChannel(pubKey1, pubKey2, chanID)
×
3679
                }
×
3680

3681
                return markEdgeZombie(zombieIndex, chanID, pubKey1, pubKey2)
×
3682
        })
×
3683
        if err != nil {
×
3684
                return err
3685
        }
×
3686

3687
        c.rejectCache.remove(chanID)
×
3688
        c.chanCache.remove(chanID)
×
3689

×
3690
        return nil
3691
}
×
3692

×
3693
// markEdgeZombie marks an edge as a zombie within our zombie index. The public
×
3694
// keys should represent the node public keys of the two parties involved in the
×
3695
// edge.
3696
func markEdgeZombie(zombieIndex kvdb.RwBucket, chanID uint64, pubKey1,
3697
        pubKey2 [33]byte) error {
3698

3699
        var k [8]byte
3700
        byteOrder.PutUint64(k[:], chanID)
3701

3✔
3702
        var v [66]byte
3✔
3703
        copy(v[:33], pubKey1[:])
3✔
3704
        copy(v[33:], pubKey2[:])
3✔
3705

3✔
3706
        return zombieIndex.Put(k[:], v[:])
3✔
3707
}
3✔
3708

3✔
3709
// MarkEdgeLive clears an edge from our zombie index, deeming it as live.
3✔
3710
func (c *ChannelGraph) MarkEdgeLive(chanID uint64) error {
3✔
3711
        c.cacheMu.Lock()
3✔
3712
        defer c.cacheMu.Unlock()
3713

3714
        return c.markEdgeLiveUnsafe(nil, chanID)
×
3715
}
×
3716

×
3717
// markEdgeLiveUnsafe clears an edge from the zombie index. This method can be
×
3718
// called with an existing kvdb.RwTx or the argument can be set to nil in which
×
3719
// case a new transaction will be created.
×
3720
//
3721
// NOTE: this method MUST only be called if the cacheMu has already been
3722
// acquired.
3723
func (c *ChannelGraph) markEdgeLiveUnsafe(tx kvdb.RwTx, chanID uint64) error {
3724
        dbFn := func(tx kvdb.RwTx) error {
3725
                edges := tx.ReadWriteBucket(edgeBucket)
3726
                if edges == nil {
3727
                        return ErrGraphNoEdgesFound
×
3728
                }
×
3729
                zombieIndex := edges.NestedReadWriteBucket(zombieBucket)
×
3730
                if zombieIndex == nil {
×
3731
                        return nil
×
3732
                }
×
3733

×
3734
                var k [8]byte
×
3735
                byteOrder.PutUint64(k[:], chanID)
×
3736

×
3737
                if len(zombieIndex.Get(k[:])) == 0 {
3738
                        return ErrZombieEdgeNotFound
×
3739
                }
×
3740

×
3741
                return zombieIndex.Delete(k[:])
×
3742
        }
×
3743

×
3744
        // If the transaction is nil, we'll create a new one. Otherwise, we use
3745
        // the existing transaction
×
3746
        var err error
3747
        if tx == nil {
3748
                err = kvdb.Update(c.db, dbFn, func() {})
3749
        } else {
3750
                err = dbFn(tx)
×
3751
        }
×
3752
        if err != nil {
×
3753
                return err
×
3754
        }
×
3755

×
3756
        c.rejectCache.remove(chanID)
×
3757
        c.chanCache.remove(chanID)
×
3758

×
3759
        // We need to add the channel back into our graph cache, otherwise we
3760
        // won't use it for path finding.
×
3761
        if c.graphCache != nil {
×
3762
                edgeInfos, err := c.fetchChanInfos(tx, []uint64{chanID})
×
3763
                if err != nil {
×
3764
                        return err
×
3765
                }
×
3766

×
3767
                for _, edgeInfo := range edgeInfos {
×
3768
                        c.graphCache.AddChannel(
×
3769
                                edgeInfo.Info, edgeInfo.Policy1,
×
3770
                                edgeInfo.Policy2,
3771
                        )
×
3772
                }
×
3773
        }
×
3774

×
3775
        return nil
×
3776
}
×
3777

3778
// IsZombieEdge returns whether the edge is considered zombie. If it is a
3779
// zombie, then the two node public keys corresponding to this edge are also
×
3780
// returned.
3781
func (c *ChannelGraph) IsZombieEdge(chanID uint64) (bool, [33]byte, [33]byte) {
3782
        var (
3783
                isZombie         bool
3784
                pubKey1, pubKey2 [33]byte
3785
        )
×
3786

×
3787
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
×
3788
                edges := tx.ReadBucket(edgeBucket)
×
3789
                if edges == nil {
×
3790
                        return ErrGraphNoEdgesFound
×
3791
                }
×
3792
                zombieIndex := edges.NestedReadBucket(zombieBucket)
×
3793
                if zombieIndex == nil {
×
3794
                        return nil
×
3795
                }
×
3796

×
3797
                isZombie, pubKey1, pubKey2 = isZombieEdge(zombieIndex, chanID)
×
3798
                return nil
×
3799
        }, func() {
×
3800
                isZombie = false
3801
                pubKey1 = [33]byte{}
×
3802
                pubKey2 = [33]byte{}
×
3803
        })
×
3804
        if err != nil {
×
3805
                return false, [33]byte{}, [33]byte{}
×
3806
        }
×
3807

×
3808
        return isZombie, pubKey1, pubKey2
×
3809
}
×
3810

×
3811
// isZombieEdge returns whether an entry exists for the given channel in the
3812
// zombie index. If an entry exists, then the two node public keys corresponding
×
3813
// to this edge are also returned.
3814
func isZombieEdge(zombieIndex kvdb.RBucket,
3815
        chanID uint64) (bool, [33]byte, [33]byte) {
3816

3817
        var k [8]byte
3818
        byteOrder.PutUint64(k[:], chanID)
3819

3✔
3820
        v := zombieIndex.Get(k[:])
3✔
3821
        if v == nil {
3✔
3822
                return false, [33]byte{}, [33]byte{}
3✔
3823
        }
3✔
3824

3✔
3825
        var pubKey1, pubKey2 [33]byte
6✔
3826
        copy(pubKey1[:], v[:33])
3✔
3827
        copy(pubKey2[:], v[33:])
3✔
3828

3829
        return true, pubKey1, pubKey2
3✔
3830
}
3✔
3831

3✔
3832
// NumZombies returns the current number of zombie channels in the graph.
3✔
3833
func (c *ChannelGraph) NumZombies() (uint64, error) {
3✔
3834
        var numZombies uint64
3835
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
3836
                edges := tx.ReadBucket(edgeBucket)
3837
                if edges == nil {
×
3838
                        return nil
×
3839
                }
×
3840
                zombieIndex := edges.NestedReadBucket(zombieBucket)
×
3841
                if zombieIndex == nil {
×
3842
                        return nil
×
3843
                }
×
3844

×
3845
                return zombieIndex.ForEach(func(_, _ []byte) error {
×
3846
                        numZombies++
×
3847
                        return nil
×
3848
                })
3849
        }, func() {
×
3850
                numZombies = 0
×
3851
        })
×
3852
        if err != nil {
×
3853
                return 0, err
×
3854
        }
×
3855

×
3856
        return numZombies, nil
×
3857
}
×
3858

×
3859
// PutClosedScid stores a SCID for a closed channel in the database. This is so
3860
// that we can ignore channel announcements that we know to be closed without
×
3861
// having to validate them and fetch a block.
3862
func (c *ChannelGraph) PutClosedScid(scid lnwire.ShortChannelID) error {
3863
        return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
3864
                closedScids, err := tx.CreateTopLevelBucket(closedScidBucket)
3865
                if err != nil {
3866
                        return err
×
3867
                }
×
3868

×
3869
                var k [8]byte
×
3870
                byteOrder.PutUint64(k[:], scid.ToUint64())
×
3871

×
3872
                return closedScids.Put(k[:], []byte{})
3873
        }, func() {})
×
3874
}
×
3875

×
3876
// IsClosedScid checks whether a channel identified by the passed in scid is
×
3877
// closed. This helps avoid having to perform expensive validation checks.
×
3878
// TODO: Add an LRU cache to cut down on disc reads.
3879
func (c *ChannelGraph) IsClosedScid(scid lnwire.ShortChannelID) (bool, error) {
3880
        var isClosed bool
3881
        err := kvdb.View(c.db, func(tx kvdb.RTx) error {
3882
                closedScids := tx.ReadBucket(closedScidBucket)
3883
                if closedScids == nil {
3✔
3884
                        return ErrClosedScidsNotFound
3✔
3885
                }
6✔
3886

3✔
3887
                var k [8]byte
3✔
3888
                byteOrder.PutUint64(k[:], scid.ToUint64())
×
3889

×
3890
                if closedScids.Get(k[:]) != nil {
3891
                        isClosed = true
3✔
3892
                        return nil
3✔
3893
                }
3✔
3894

3✔
3895
                return nil
×
3896
        }, func() {
×
3897
                isClosed = false
×
3898
        })
3899
        if err != nil {
3✔
3900
                return false, err
3✔
3901
        }
3✔
3902

3✔
3903
        return isClosed, nil
3✔
3904
}
×
3905

×
3906
func putLightningNode(nodeBucket kvdb.RwBucket, aliasBucket kvdb.RwBucket, // nolint:dupl
3907
        updateIndex kvdb.RwBucket, node *models.LightningNode) error {
3✔
3908

3909
        var (
3910
                scratch [16]byte
3911
                b       bytes.Buffer
3✔
3912
        )
3✔
3913

3✔
3914
        pub, err := node.PubKey()
3✔
3915
        if err != nil {
3✔
3916
                return err
3✔
3917
        }
3✔
3918
        nodePub := pub.SerializeCompressed()
3✔
3919

3✔
3920
        // If the node has the update time set, write it, else write 0.
×
3921
        updateUnix := uint64(0)
×
3922
        if node.LastUpdate.Unix() > 0 {
3✔
3923
                updateUnix = uint64(node.LastUpdate.Unix())
3✔
3924
        }
3✔
3925

3✔
3926
        byteOrder.PutUint64(scratch[:8], updateUnix)
6✔
3927
        if _, err := b.Write(scratch[:8]); err != nil {
3✔
3928
                return err
3✔
3929
        }
3930

3✔
3931
        if _, err := b.Write(nodePub); err != nil {
3✔
3932
                return err
×
3933
        }
×
3934

3935
        // If we got a node announcement for this node, we will have the rest
3✔
3936
        // of the data available. If not we don't have more data to write.
×
3937
        if !node.HaveNodeAnnouncement {
×
3938
                // Write HaveNodeAnnouncement=0.
3939
                byteOrder.PutUint16(scratch[:2], 0)
3940
                if _, err := b.Write(scratch[:2]); err != nil {
3941
                        return err
6✔
3942
                }
3✔
3943

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

×
3947
        // Write HaveNodeAnnouncement=1.
3948
        byteOrder.PutUint16(scratch[:2], 1)
3✔
3949
        if _, err := b.Write(scratch[:2]); err != nil {
3950
                return err
3951
        }
3952

3✔
3953
        if err := binary.Write(&b, byteOrder, node.Color.R); err != nil {
3✔
3954
                return err
×
3955
        }
×
3956
        if err := binary.Write(&b, byteOrder, node.Color.G); err != nil {
3957
                return err
3✔
3958
        }
×
3959
        if err := binary.Write(&b, byteOrder, node.Color.B); err != nil {
×
3960
                return err
3✔
3961
        }
×
3962

×
3963
        if err := wire.WriteVarString(&b, 0, node.Alias); err != nil {
3✔
3964
                return err
×
3965
        }
×
3966

3967
        if err := node.Features.Encode(&b); err != nil {
3✔
3968
                return err
×
3969
        }
×
3970

3971
        numAddresses := uint16(len(node.Addresses))
3✔
3972
        byteOrder.PutUint16(scratch[:2], numAddresses)
×
3973
        if _, err := b.Write(scratch[:2]); err != nil {
×
3974
                return err
3975
        }
3✔
3976

3✔
3977
        for _, address := range node.Addresses {
3✔
3978
                if err := SerializeAddr(&b, address); err != nil {
×
3979
                        return err
×
3980
                }
3981
        }
6✔
3982

3✔
3983
        sigLen := len(node.AuthSigBytes)
×
3984
        if sigLen > 80 {
×
3985
                return fmt.Errorf("max sig len allowed is 80, had %v",
3986
                        sigLen)
3987
        }
3✔
3988

3✔
3989
        err = wire.WriteVarBytes(&b, 0, node.AuthSigBytes)
×
3990
        if err != nil {
×
3991
                return err
×
3992
        }
3993

3✔
3994
        if len(node.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
3✔
3995
                return ErrTooManyExtraOpaqueBytes(len(node.ExtraOpaqueData))
×
3996
        }
×
3997
        err = wire.WriteVarBytes(&b, 0, node.ExtraOpaqueData)
3998
        if err != nil {
3✔
3999
                return err
×
4000
        }
×
4001

3✔
4002
        if err := aliasBucket.Put(nodePub, []byte(node.Alias)); err != nil {
3✔
4003
                return err
×
4004
        }
×
4005

4006
        // With the alias bucket updated, we'll now update the index that
3✔
4007
        // tracks the time series of node updates.
×
4008
        var indexKey [8 + 33]byte
×
4009
        byteOrder.PutUint64(indexKey[:8], updateUnix)
4010
        copy(indexKey[8:], nodePub)
4011

4012
        // If there was already an old index entry for this node, then we'll
3✔
4013
        // delete the old one before we write the new entry.
3✔
4014
        if nodeBytes := nodeBucket.Get(nodePub); nodeBytes != nil {
3✔
4015
                // Extract out the old update time to we can reconstruct the
3✔
4016
                // prior index key to delete it from the index.
3✔
4017
                oldUpdateTime := nodeBytes[:8]
3✔
4018

6✔
4019
                var oldIndexKey [8 + 33]byte
3✔
4020
                copy(oldIndexKey[:8], oldUpdateTime)
3✔
4021
                copy(oldIndexKey[8:], nodePub)
3✔
4022

3✔
4023
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
3✔
4024
                        return err
3✔
4025
                }
3✔
4026
        }
3✔
4027

3✔
4028
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
×
4029
                return err
×
4030
        }
4031

4032
        return nodeBucket.Put(nodePub, b.Bytes())
3✔
4033
}
×
4034

×
4035
func fetchLightningNode(nodeBucket kvdb.RBucket,
4036
        nodePub []byte) (models.LightningNode, error) {
3✔
4037

4038
        nodeBytes := nodeBucket.Get(nodePub)
4039
        if nodeBytes == nil {
4040
                return models.LightningNode{}, ErrGraphNodeNotFound
3✔
4041
        }
3✔
4042

3✔
4043
        nodeReader := bytes.NewReader(nodeBytes)
6✔
4044
        return deserializeLightningNode(nodeReader)
3✔
4045
}
3✔
4046

4047
func deserializeLightningNodeCacheable(r io.Reader) (*graphCacheNode, error) {
3✔
4048
        // Always populate a feature vector, even if we don't have a node
3✔
4049
        // announcement and short circuit below.
4050
        node := newGraphCacheNode(
4051
                route.Vertex{},
3✔
4052
                lnwire.EmptyFeatureVector(),
3✔
4053
        )
3✔
4054

3✔
4055
        var nodeScratch [8]byte
3✔
4056

3✔
4057
        // Skip ahead:
3✔
4058
        // - LastUpdate (8 bytes)
3✔
4059
        if _, err := r.Read(nodeScratch[:]); err != nil {
3✔
4060
                return nil, err
3✔
4061
        }
3✔
4062

3✔
4063
        if _, err := io.ReadFull(r, node.pubKeyBytes[:]); err != nil {
3✔
4064
                return nil, err
×
4065
        }
×
4066

4067
        // Read the node announcement flag.
3✔
4068
        if _, err := r.Read(nodeScratch[:2]); err != nil {
×
4069
                return nil, err
×
4070
        }
4071
        hasNodeAnn := byteOrder.Uint16(nodeScratch[:2])
4072

3✔
4073
        // The rest of the data is optional, and will only be there if we got a
×
4074
        // node announcement for this node.
×
4075
        if hasNodeAnn == 0 {
3✔
4076
                return node, nil
3✔
4077
        }
3✔
4078

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

×
4092
        if _, err := wire.ReadVarString(r, 0); err != nil {
3✔
4093
                return nil, err
×
4094
        }
×
4095

4096
        if err := node.features.Decode(r); err != nil {
3✔
4097
                return nil, err
×
4098
        }
×
4099

4100
        return node, nil
3✔
4101
}
×
4102

×
4103
func deserializeLightningNode(r io.Reader) (models.LightningNode, error) {
4104
        var (
3✔
4105
                node    models.LightningNode
4106
                scratch [8]byte
4107
                err     error
3✔
4108
        )
3✔
4109

3✔
4110
        // Always populate a feature vector, even if we don't have a node
3✔
4111
        // announcement and short circuit below.
3✔
4112
        node.Features = lnwire.EmptyFeatureVector()
3✔
4113

3✔
4114
        if _, err := r.Read(scratch[:]); err != nil {
3✔
4115
                return models.LightningNode{}, err
3✔
4116
        }
3✔
4117

3✔
4118
        unix := int64(byteOrder.Uint64(scratch[:]))
3✔
4119
        node.LastUpdate = time.Unix(unix, 0)
×
4120

×
4121
        if _, err := io.ReadFull(r, node.PubKeyBytes[:]); err != nil {
4122
                return models.LightningNode{}, err
3✔
4123
        }
3✔
4124

3✔
4125
        if _, err := r.Read(scratch[:2]); err != nil {
3✔
4126
                return models.LightningNode{}, err
×
4127
        }
×
4128

4129
        hasNodeAnn := byteOrder.Uint16(scratch[:2])
3✔
4130
        if hasNodeAnn == 1 {
×
4131
                node.HaveNodeAnnouncement = true
×
4132
        } else {
4133
                node.HaveNodeAnnouncement = false
3✔
4134
        }
6✔
4135

3✔
4136
        // The rest of the data is optional, and will only be there if we got a
6✔
4137
        // node announcement for this node.
3✔
4138
        if !node.HaveNodeAnnouncement {
3✔
4139
                return node, nil
4140
        }
4141

4142
        // We did get a node announcement for this node, so we'll have the rest
6✔
4143
        // of the data available.
3✔
4144
        if err := binary.Read(r, byteOrder, &node.Color.R); err != nil {
3✔
4145
                return models.LightningNode{}, err
4146
        }
4147
        if err := binary.Read(r, byteOrder, &node.Color.G); err != nil {
4148
                return models.LightningNode{}, err
3✔
4149
        }
×
4150
        if err := binary.Read(r, byteOrder, &node.Color.B); err != nil {
×
4151
                return models.LightningNode{}, err
3✔
4152
        }
×
4153

×
4154
        node.Alias, err = wire.ReadVarString(r, 0)
3✔
4155
        if err != nil {
×
4156
                return models.LightningNode{}, err
×
4157
        }
4158

3✔
4159
        err = node.Features.Decode(r)
3✔
4160
        if err != nil {
×
4161
                return models.LightningNode{}, err
×
4162
        }
4163

3✔
4164
        if _, err := r.Read(scratch[:2]); err != nil {
3✔
4165
                return models.LightningNode{}, err
×
4166
        }
×
4167
        numAddresses := int(byteOrder.Uint16(scratch[:2]))
4168

3✔
4169
        var addresses []net.Addr
×
4170
        for i := 0; i < numAddresses; i++ {
×
4171
                address, err := DeserializeAddr(r)
3✔
4172
                if err != nil {
3✔
4173
                        return models.LightningNode{}, err
3✔
4174
                }
6✔
4175
                addresses = append(addresses, address)
3✔
4176
        }
3✔
4177
        node.Addresses = addresses
×
4178

×
4179
        node.AuthSigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
3✔
4180
        if err != nil {
4181
                return models.LightningNode{}, err
3✔
4182
        }
3✔
4183

3✔
4184
        // We'll try and see if there are any opaque bytes left, if not, then
3✔
4185
        // we'll ignore the EOF error and return the node as is.
×
4186
        node.ExtraOpaqueData, err = wire.ReadVarBytes(
×
4187
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
4188
        )
4189
        switch {
4190
        case err == io.ErrUnexpectedEOF:
3✔
4191
        case err == io.EOF:
3✔
4192
        case err != nil:
3✔
4193
                return models.LightningNode{}, err
3✔
4194
        }
×
4195

×
4196
        return node, nil
×
4197
}
×
4198

4199
func putChanEdgeInfo(edgeIndex kvdb.RwBucket,
4200
        edgeInfo *models.ChannelEdgeInfo, chanID [8]byte) error {
3✔
4201

4202
        var b bytes.Buffer
4203

4204
        if _, err := b.Write(edgeInfo.NodeKey1Bytes[:]); err != nil {
3✔
4205
                return err
3✔
4206
        }
3✔
4207
        if _, err := b.Write(edgeInfo.NodeKey2Bytes[:]); err != nil {
3✔
4208
                return err
3✔
4209
        }
×
4210
        if _, err := b.Write(edgeInfo.BitcoinKey1Bytes[:]); err != nil {
×
4211
                return err
3✔
4212
        }
×
4213
        if _, err := b.Write(edgeInfo.BitcoinKey2Bytes[:]); err != nil {
×
4214
                return err
3✔
4215
        }
×
4216

×
4217
        if err := wire.WriteVarBytes(&b, 0, edgeInfo.Features); err != nil {
3✔
4218
                return err
×
4219
        }
×
4220

4221
        authProof := edgeInfo.AuthProof
3✔
4222
        var nodeSig1, nodeSig2, bitcoinSig1, bitcoinSig2 []byte
×
4223
        if authProof != nil {
×
4224
                nodeSig1 = authProof.NodeSig1Bytes
4225
                nodeSig2 = authProof.NodeSig2Bytes
3✔
4226
                bitcoinSig1 = authProof.BitcoinSig1Bytes
3✔
4227
                bitcoinSig2 = authProof.BitcoinSig2Bytes
6✔
4228
        }
3✔
4229

3✔
4230
        if err := wire.WriteVarBytes(&b, 0, nodeSig1); err != nil {
3✔
4231
                return err
3✔
4232
        }
3✔
4233
        if err := wire.WriteVarBytes(&b, 0, nodeSig2); err != nil {
4234
                return err
3✔
4235
        }
×
4236
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig1); err != nil {
×
4237
                return err
3✔
4238
        }
×
4239
        if err := wire.WriteVarBytes(&b, 0, bitcoinSig2); err != nil {
×
4240
                return err
3✔
4241
        }
×
4242

×
4243
        if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
3✔
4244
                return err
×
4245
        }
×
4246
        err := binary.Write(&b, byteOrder, uint64(edgeInfo.Capacity))
4247
        if err != nil {
3✔
4248
                return err
×
4249
        }
×
4250
        if _, err := b.Write(chanID[:]); err != nil {
3✔
4251
                return err
3✔
4252
        }
×
4253
        if _, err := b.Write(edgeInfo.ChainHash[:]); err != nil {
×
4254
                return err
3✔
4255
        }
×
4256

×
4257
        if len(edgeInfo.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
3✔
4258
                return ErrTooManyExtraOpaqueBytes(len(edgeInfo.ExtraOpaqueData))
×
4259
        }
×
4260
        err = wire.WriteVarBytes(&b, 0, edgeInfo.ExtraOpaqueData)
4261
        if err != nil {
3✔
4262
                return err
×
4263
        }
×
4264

3✔
4265
        return edgeIndex.Put(chanID[:], b.Bytes())
3✔
4266
}
×
4267

×
4268
func fetchChanEdgeInfo(edgeIndex kvdb.RBucket,
4269
        chanID []byte) (models.ChannelEdgeInfo, error) {
3✔
4270

4271
        edgeInfoBytes := edgeIndex.Get(chanID)
4272
        if edgeInfoBytes == nil {
4273
                return models.ChannelEdgeInfo{}, ErrEdgeNotFound
3✔
4274
        }
3✔
4275

3✔
4276
        edgeInfoReader := bytes.NewReader(edgeInfoBytes)
6✔
4277
        return deserializeChanEdgeInfo(edgeInfoReader)
3✔
4278
}
3✔
4279

4280
func deserializeChanEdgeInfo(r io.Reader) (models.ChannelEdgeInfo, error) {
3✔
4281
        var (
3✔
4282
                err      error
4283
                edgeInfo models.ChannelEdgeInfo
4284
        )
3✔
4285

3✔
4286
        if _, err := io.ReadFull(r, edgeInfo.NodeKey1Bytes[:]); err != nil {
3✔
4287
                return models.ChannelEdgeInfo{}, err
3✔
4288
        }
3✔
4289
        if _, err := io.ReadFull(r, edgeInfo.NodeKey2Bytes[:]); err != nil {
3✔
4290
                return models.ChannelEdgeInfo{}, err
3✔
4291
        }
×
4292
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey1Bytes[:]); err != nil {
×
4293
                return models.ChannelEdgeInfo{}, err
3✔
4294
        }
×
4295
        if _, err := io.ReadFull(r, edgeInfo.BitcoinKey2Bytes[:]); err != nil {
×
4296
                return models.ChannelEdgeInfo{}, err
3✔
4297
        }
×
4298

×
4299
        edgeInfo.Features, err = wire.ReadVarBytes(r, 0, 900, "features")
3✔
4300
        if err != nil {
×
4301
                return models.ChannelEdgeInfo{}, err
×
4302
        }
4303

3✔
4304
        proof := &models.ChannelAuthProof{}
3✔
4305

×
4306
        proof.NodeSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
×
4307
        if err != nil {
4308
                return models.ChannelEdgeInfo{}, err
3✔
4309
        }
3✔
4310
        proof.NodeSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4311
        if err != nil {
3✔
4312
                return models.ChannelEdgeInfo{}, err
×
4313
        }
×
4314
        proof.BitcoinSig1Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4315
        if err != nil {
3✔
4316
                return models.ChannelEdgeInfo{}, err
×
4317
        }
×
4318
        proof.BitcoinSig2Bytes, err = wire.ReadVarBytes(r, 0, 80, "sigs")
3✔
4319
        if err != nil {
3✔
4320
                return models.ChannelEdgeInfo{}, err
×
4321
        }
×
4322

3✔
4323
        if !proof.IsEmpty() {
3✔
4324
                edgeInfo.AuthProof = proof
×
4325
        }
×
4326

4327
        edgeInfo.ChannelPoint = wire.OutPoint{}
6✔
4328
        if err := ReadOutpoint(r, &edgeInfo.ChannelPoint); err != nil {
3✔
4329
                return models.ChannelEdgeInfo{}, err
3✔
4330
        }
4331
        if err := binary.Read(r, byteOrder, &edgeInfo.Capacity); err != nil {
3✔
4332
                return models.ChannelEdgeInfo{}, err
3✔
4333
        }
×
4334
        if err := binary.Read(r, byteOrder, &edgeInfo.ChannelID); err != nil {
×
4335
                return models.ChannelEdgeInfo{}, err
3✔
4336
        }
×
4337

×
4338
        if _, err := io.ReadFull(r, edgeInfo.ChainHash[:]); err != nil {
3✔
4339
                return models.ChannelEdgeInfo{}, err
×
4340
        }
×
4341

4342
        // We'll try and see if there are any opaque bytes left, if not, then
3✔
4343
        // we'll ignore the EOF error and return the edge as is.
×
4344
        edgeInfo.ExtraOpaqueData, err = wire.ReadVarBytes(
×
4345
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
4346
        )
4347
        switch {
4348
        case err == io.ErrUnexpectedEOF:
3✔
4349
        case err == io.EOF:
3✔
4350
        case err != nil:
3✔
4351
                return models.ChannelEdgeInfo{}, err
3✔
4352
        }
×
4353

×
4354
        return edgeInfo, nil
×
4355
}
×
4356

4357
func putChanEdgePolicy(edges kvdb.RwBucket, edge *models.ChannelEdgePolicy,
4358
        from, to []byte) error {
3✔
4359

4360
        var edgeKey [33 + 8]byte
4361
        copy(edgeKey[:], from)
4362
        byteOrder.PutUint64(edgeKey[33:], edge.ChannelID)
3✔
4363

3✔
4364
        var b bytes.Buffer
3✔
4365
        if err := serializeChanEdgePolicy(&b, edge, to); err != nil {
3✔
4366
                return err
3✔
4367
        }
3✔
4368

3✔
4369
        // Before we write out the new edge, we'll create a new entry in the
3✔
4370
        // update index in order to keep it fresh.
×
4371
        updateUnix := uint64(edge.LastUpdate.Unix())
×
4372
        var indexKey [8 + 8]byte
4373
        byteOrder.PutUint64(indexKey[:8], updateUnix)
4374
        byteOrder.PutUint64(indexKey[8:], edge.ChannelID)
4375

3✔
4376
        updateIndex, err := edges.CreateBucketIfNotExists(edgeUpdateIndexBucket)
3✔
4377
        if err != nil {
3✔
4378
                return err
3✔
4379
        }
3✔
4380

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

4388
                // In order to delete the old entry, we'll need to obtain the
4389
                // *prior* update time in order to delete it. To do this, we'll
3✔
4390
                // need to deserialize the existing policy within the database
6✔
4391
                // (now outdated by the new one), and delete its corresponding
3✔
4392
                // entry within the update index. We'll ignore any
3✔
4393
                // ErrEdgePolicyOptionalFieldNotFound error, as we only need
3✔
4394
                // the channel ID and update time to delete the entry.
3✔
4395
                // TODO(halseth): get rid of these invalid policies in a
3✔
4396
                // migration.
3✔
4397
                oldEdgePolicy, err := deserializeChanEdgePolicy(
3✔
4398
                        bytes.NewReader(edgeBytes),
3✔
4399
                )
3✔
4400
                if err != nil && err != ErrEdgePolicyOptionalFieldNotFound {
3✔
4401
                        return err
3✔
4402
                }
3✔
4403

3✔
4404
                oldUpdateTime := uint64(oldEdgePolicy.LastUpdate.Unix())
3✔
4405

×
4406
                var oldIndexKey [8 + 8]byte
×
4407
                byteOrder.PutUint64(oldIndexKey[:8], oldUpdateTime)
4408
                byteOrder.PutUint64(oldIndexKey[8:], edge.ChannelID)
3✔
4409

3✔
4410
                if err := updateIndex.Delete(oldIndexKey[:]); err != nil {
3✔
4411
                        return err
3✔
4412
                }
3✔
4413
        }
3✔
4414

3✔
4415
        if err := updateIndex.Put(indexKey[:], nil); err != nil {
×
4416
                return err
×
4417
        }
4418

4419
        err = updateEdgePolicyDisabledIndex(
3✔
4420
                edges, edge.ChannelID,
×
4421
                edge.ChannelFlags&lnwire.ChanUpdateDirection > 0,
×
4422
                edge.IsDisabled(),
4423
        )
3✔
4424
        if err != nil {
3✔
4425
                return err
3✔
4426
        }
3✔
4427

3✔
4428
        return edges.Put(edgeKey[:], b.Bytes()[:])
3✔
4429
}
×
4430

×
4431
// updateEdgePolicyDisabledIndex is used to update the disabledEdgePolicyIndex
4432
// bucket by either add a new disabled ChannelEdgePolicy or remove an existing
3✔
4433
// one.
4434
// The direction represents the direction of the edge and disabled is used for
4435
// deciding whether to remove or add an entry to the bucket.
4436
// In general a channel is disabled if two entries for the same chanID exist
4437
// in this bucket.
4438
// Maintaining the bucket this way allows a fast retrieval of disabled
4439
// channels, for example when prune is needed.
4440
func updateEdgePolicyDisabledIndex(edges kvdb.RwBucket, chanID uint64,
4441
        direction bool, disabled bool) error {
4442

4443
        var disabledEdgeKey [8 + 1]byte
4444
        byteOrder.PutUint64(disabledEdgeKey[0:], chanID)
4445
        if direction {
3✔
4446
                disabledEdgeKey[8] = 1
3✔
4447
        }
3✔
4448

3✔
4449
        disabledEdgePolicyIndex, err := edges.CreateBucketIfNotExists(
6✔
4450
                disabledEdgePolicyBucket,
3✔
4451
        )
3✔
4452
        if err != nil {
4453
                return err
3✔
4454
        }
3✔
4455

3✔
4456
        if disabled {
3✔
4457
                return disabledEdgePolicyIndex.Put(disabledEdgeKey[:], []byte{})
×
4458
        }
×
4459

4460
        return disabledEdgePolicyIndex.Delete(disabledEdgeKey[:])
6✔
4461
}
3✔
4462

3✔
4463
// putChanEdgePolicyUnknown marks the edge policy as unknown
4464
// in the edges bucket.
3✔
4465
func putChanEdgePolicyUnknown(edges kvdb.RwBucket, channelID uint64,
4466
        from []byte) error {
4467

4468
        var edgeKey [33 + 8]byte
4469
        copy(edgeKey[:], from)
4470
        byteOrder.PutUint64(edgeKey[33:], channelID)
3✔
4471

3✔
4472
        if edges.Get(edgeKey[:]) != nil {
3✔
4473
                return fmt.Errorf("cannot write unknown policy for channel %v "+
3✔
4474
                        " when there is already a policy present", channelID)
3✔
4475
        }
3✔
4476

3✔
4477
        return edges.Put(edgeKey[:], unknownPolicy)
×
4478
}
×
4479

×
4480
func fetchChanEdgePolicy(edges kvdb.RBucket, chanID []byte,
4481
        nodePub []byte) (*models.ChannelEdgePolicy, error) {
3✔
4482

4483
        var edgeKey [33 + 8]byte
4484
        copy(edgeKey[:], nodePub)
4485
        copy(edgeKey[33:], chanID[:])
3✔
4486

3✔
4487
        edgeBytes := edges.Get(edgeKey[:])
3✔
4488
        if edgeBytes == nil {
3✔
4489
                return nil, ErrEdgeNotFound
3✔
4490
        }
3✔
4491

3✔
4492
        // No need to deserialize unknown policy.
3✔
4493
        if bytes.Equal(edgeBytes[:], unknownPolicy) {
×
4494
                return nil, nil
×
4495
        }
4496

4497
        edgeReader := bytes.NewReader(edgeBytes)
6✔
4498

3✔
4499
        ep, err := deserializeChanEdgePolicy(edgeReader)
3✔
4500
        switch {
4501
        // If the db policy was missing an expected optional field, we return
3✔
4502
        // nil as if the policy was unknown.
3✔
4503
        case err == ErrEdgePolicyOptionalFieldNotFound:
3✔
4504
                return nil, nil
3✔
4505

4506
        case err != nil:
4507
                return nil, err
×
4508
        }
×
4509

4510
        return ep, nil
×
4511
}
×
4512

4513
func fetchChanEdgePolicies(edgeIndex kvdb.RBucket, edges kvdb.RBucket,
4514
        chanID []byte) (*models.ChannelEdgePolicy, *models.ChannelEdgePolicy,
3✔
4515
        error) {
4516

4517
        edgeInfo := edgeIndex.Get(chanID)
4518
        if edgeInfo == nil {
4519
                return nil, nil, fmt.Errorf("%w: chanID=%x", ErrEdgeNotFound,
3✔
4520
                        chanID)
3✔
4521
        }
3✔
4522

3✔
4523
        // The first node is contained within the first half of the edge
×
4524
        // information. We only propagate the error here and below if it's
×
4525
        // something other than edge non-existence.
×
4526
        node1Pub := edgeInfo[:33]
4527
        edge1, err := fetchChanEdgePolicy(edges, chanID, node1Pub)
4528
        if err != nil {
4529
                return nil, nil, fmt.Errorf("%w: node1Pub=%x", ErrEdgeNotFound,
4530
                        node1Pub)
3✔
4531
        }
3✔
4532

3✔
4533
        // Similarly, the second node is contained within the latter
×
4534
        // half of the edge information.
×
4535
        node2Pub := edgeInfo[33:66]
×
4536
        edge2, err := fetchChanEdgePolicy(edges, chanID, node2Pub)
4537
        if err != nil {
4538
                return nil, nil, fmt.Errorf("%w: node2Pub=%x", ErrEdgeNotFound,
4539
                        node2Pub)
3✔
4540
        }
3✔
4541

3✔
4542
        return edge1, edge2, nil
×
4543
}
×
4544

×
4545
func serializeChanEdgePolicy(w io.Writer, edge *models.ChannelEdgePolicy,
4546
        to []byte) error {
3✔
4547

4548
        err := wire.WriteVarBytes(w, 0, edge.SigBytes)
4549
        if err != nil {
4550
                return err
3✔
4551
        }
3✔
4552

3✔
4553
        if err := binary.Write(w, byteOrder, edge.ChannelID); err != nil {
3✔
4554
                return err
×
4555
        }
×
4556

4557
        var scratch [8]byte
3✔
4558
        updateUnix := uint64(edge.LastUpdate.Unix())
×
4559
        byteOrder.PutUint64(scratch[:], updateUnix)
×
4560
        if _, err := w.Write(scratch[:]); err != nil {
4561
                return err
3✔
4562
        }
3✔
4563

3✔
4564
        if err := binary.Write(w, byteOrder, edge.MessageFlags); err != nil {
3✔
4565
                return err
×
4566
        }
×
4567
        if err := binary.Write(w, byteOrder, edge.ChannelFlags); err != nil {
4568
                return err
3✔
4569
        }
×
4570
        if err := binary.Write(w, byteOrder, edge.TimeLockDelta); err != nil {
×
4571
                return err
3✔
4572
        }
×
4573
        if err := binary.Write(w, byteOrder, uint64(edge.MinHTLC)); err != nil {
×
4574
                return err
3✔
4575
        }
×
4576
        err = binary.Write(w, byteOrder, uint64(edge.FeeBaseMSat))
×
4577
        if err != nil {
3✔
4578
                return err
×
4579
        }
×
4580
        err = binary.Write(
3✔
4581
                w, byteOrder, uint64(edge.FeeProportionalMillionths),
3✔
4582
        )
×
4583
        if err != nil {
×
4584
                return err
3✔
4585
        }
3✔
4586

3✔
4587
        if _, err := w.Write(to); err != nil {
3✔
4588
                return err
×
4589
        }
×
4590

4591
        // If the max_htlc field is present, we write it. To be compatible with
3✔
4592
        // older versions that wasn't aware of this field, we write it as part
×
4593
        // of the opaque data.
×
4594
        // TODO(halseth): clean up when moving to TLV.
4595
        var opaqueBuf bytes.Buffer
4596
        if edge.MessageFlags.HasMaxHtlc() {
4597
                err := binary.Write(&opaqueBuf, byteOrder, uint64(edge.MaxHTLC))
4598
                if err != nil {
4599
                        return err
3✔
4600
                }
6✔
4601
        }
3✔
4602

3✔
4603
        if len(edge.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
×
4604
                return ErrTooManyExtraOpaqueBytes(len(edge.ExtraOpaqueData))
×
4605
        }
4606
        if _, err := opaqueBuf.Write(edge.ExtraOpaqueData); err != nil {
4607
                return err
3✔
4608
        }
×
4609

×
4610
        if err := wire.WriteVarBytes(w, 0, opaqueBuf.Bytes()); err != nil {
3✔
4611
                return err
×
4612
        }
×
4613
        return nil
4614
}
3✔
4615

×
4616
func deserializeChanEdgePolicy(r io.Reader) (*models.ChannelEdgePolicy, error) {
×
4617
        // Deserialize the policy. Note that in case an optional field is not
3✔
4618
        // found, both an error and a populated policy object are returned.
4619
        edge, deserializeErr := deserializeChanEdgePolicyRaw(r)
4620
        if deserializeErr != nil &&
3✔
4621
                deserializeErr != ErrEdgePolicyOptionalFieldNotFound {
3✔
4622

3✔
4623
                return nil, deserializeErr
3✔
4624
        }
3✔
4625

3✔
4626
        return edge, deserializeErr
×
4627
}
×
4628

×
4629
func deserializeChanEdgePolicyRaw(r io.Reader) (*models.ChannelEdgePolicy,
4630
        error) {
3✔
4631

4632
        edge := &models.ChannelEdgePolicy{}
4633

4634
        var err error
3✔
4635
        edge.SigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
3✔
4636
        if err != nil {
3✔
4637
                return nil, err
3✔
4638
        }
3✔
4639

3✔
4640
        if err := binary.Read(r, byteOrder, &edge.ChannelID); err != nil {
3✔
4641
                return nil, err
×
4642
        }
×
4643

4644
        var scratch [8]byte
3✔
4645
        if _, err := r.Read(scratch[:]); err != nil {
×
4646
                return nil, err
×
4647
        }
4648
        unix := int64(byteOrder.Uint64(scratch[:]))
3✔
4649
        edge.LastUpdate = time.Unix(unix, 0)
3✔
4650

×
4651
        if err := binary.Read(r, byteOrder, &edge.MessageFlags); err != nil {
×
4652
                return nil, err
3✔
4653
        }
3✔
4654
        if err := binary.Read(r, byteOrder, &edge.ChannelFlags); err != nil {
3✔
4655
                return nil, err
3✔
4656
        }
×
4657
        if err := binary.Read(r, byteOrder, &edge.TimeLockDelta); err != nil {
×
4658
                return nil, err
3✔
4659
        }
×
4660

×
4661
        var n uint64
3✔
4662
        if err := binary.Read(r, byteOrder, &n); err != nil {
×
4663
                return nil, err
×
4664
        }
4665
        edge.MinHTLC = lnwire.MilliSatoshi(n)
3✔
4666

3✔
4667
        if err := binary.Read(r, byteOrder, &n); err != nil {
×
4668
                return nil, err
×
4669
        }
3✔
4670
        edge.FeeBaseMSat = lnwire.MilliSatoshi(n)
3✔
4671

3✔
4672
        if err := binary.Read(r, byteOrder, &n); err != nil {
×
4673
                return nil, err
×
4674
        }
3✔
4675
        edge.FeeProportionalMillionths = lnwire.MilliSatoshi(n)
3✔
4676

3✔
4677
        if _, err := r.Read(edge.ToNode[:]); err != nil {
×
4678
                return nil, err
×
4679
        }
3✔
4680

3✔
4681
        // We'll try and see if there are any opaque bytes left, if not, then
3✔
4682
        // we'll ignore the EOF error and return the edge as is.
×
4683
        edge.ExtraOpaqueData, err = wire.ReadVarBytes(
×
4684
                r, 0, MaxAllowedExtraOpaqueBytes, "blob",
4685
        )
4686
        switch {
4687
        case err == io.ErrUnexpectedEOF:
3✔
4688
        case err == io.EOF:
3✔
4689
        case err != nil:
3✔
4690
                return nil, err
3✔
4691
        }
×
4692

×
4693
        // See if optional fields are present.
×
4694
        if edge.MessageFlags.HasMaxHtlc() {
×
4695
                // The max_htlc field should be at the beginning of the opaque
4696
                // bytes.
4697
                opq := edge.ExtraOpaqueData
4698

6✔
4699
                // If the max_htlc field is not present, it might be old data
3✔
4700
                // stored before this field was validated. We'll return the
3✔
4701
                // edge along with an error.
3✔
4702
                if len(opq) < 8 {
3✔
4703
                        return edge, ErrEdgePolicyOptionalFieldNotFound
3✔
4704
                }
3✔
4705

3✔
4706
                maxHtlc := byteOrder.Uint64(opq[:8])
3✔
4707
                edge.MaxHTLC = lnwire.MilliSatoshi(maxHtlc)
×
4708

×
4709
                // Exclude the parsed field from the rest of the opaque data.
4710
                edge.ExtraOpaqueData = opq[8:]
3✔
4711
        }
3✔
4712

3✔
4713
        return edge, nil
3✔
4714
}
3✔
4715

4716
// MakeTestGraph creates a new instance of the ChannelGraph for testing
4717
// purposes.
3✔
4718
func MakeTestGraph(t testing.TB, modifiers ...OptionModifier) (*ChannelGraph,
4719
        error) {
4720

4721
        opts := DefaultOptions()
4722
        for _, modifier := range modifiers {
4723
                modifier(opts)
×
4724
        }
×
4725

×
4726
        // Next, create channelgraph for the first time.
×
4727
        backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
×
4728
        if err != nil {
×
4729
                backendCleanup()
4730
                return nil, err
4731
        }
×
4732

×
4733
        graph, err := NewChannelGraph(backend)
×
4734
        if err != nil {
×
4735
                backendCleanup()
×
4736
                return nil, err
4737
        }
×
4738

×
4739
        t.Cleanup(func() {
×
4740
                _ = backend.Close()
×
4741
                backendCleanup()
×
4742
        })
4743

×
4744
        return graph, nil
×
4745
}
×
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