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

lightningnetwork / lnd / 13536249039

26 Feb 2025 03:42AM UTC coverage: 57.462% (-1.4%) from 58.835%
13536249039

Pull #8453

github

Roasbeef
peer: update chooseDeliveryScript to gen script if needed

In this commit, we update `chooseDeliveryScript` to generate a new
script if needed. This allows us to fold in a few other lines that
always followed this function into this expanded function.

The tests have been updated accordingly.
Pull Request #8453: [4/4] - multi: integrate new rbf coop close FSM into the existing peer flow

275 of 1318 new or added lines in 22 files covered. (20.86%)

19521 existing lines in 257 files now uncovered.

103858 of 180741 relevant lines covered (57.46%)

24750.23 hits per line

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

81.82
/graph/db/graph_cache.go
1
package graphdb
2

3
import (
4
        "fmt"
5
        "sync"
6

7
        "github.com/btcsuite/btcd/btcutil"
8
        "github.com/lightningnetwork/lnd/graph/db/models"
9
        "github.com/lightningnetwork/lnd/lnwire"
10
        "github.com/lightningnetwork/lnd/routing/route"
11
)
12

13
// DirectedChannel is a type that stores the channel information as seen from
14
// one side of the channel.
15
type DirectedChannel struct {
16
        // ChannelID is the unique identifier of this channel.
17
        ChannelID uint64
18

19
        // IsNode1 indicates if this is the node with the smaller public key.
20
        IsNode1 bool
21

22
        // OtherNode is the public key of the node on the other end of this
23
        // channel.
24
        OtherNode route.Vertex
25

26
        // Capacity is the announced capacity of this channel in satoshis.
27
        Capacity btcutil.Amount
28

29
        // OutPolicySet is a boolean that indicates whether the node has an
30
        // outgoing policy set. For pathfinding only the existence of the policy
31
        // is important to know, not the actual content.
32
        OutPolicySet bool
33

34
        // InPolicy is the incoming policy *from* the other node to this node.
35
        // In path finding, we're walking backward from the destination to the
36
        // source, so we're always interested in the edge that arrives to us
37
        // from the other node.
38
        InPolicy *models.CachedEdgePolicy
39

40
        // Inbound fees of this node.
41
        InboundFee lnwire.Fee
42
}
43

44
// DeepCopy creates a deep copy of the channel, including the incoming policy.
45
func (c *DirectedChannel) DeepCopy() *DirectedChannel {
1,336✔
46
        channelCopy := *c
1,336✔
47

1,336✔
48
        if channelCopy.InPolicy != nil {
2,646✔
49
                inPolicyCopy := *channelCopy.InPolicy
1,310✔
50
                channelCopy.InPolicy = &inPolicyCopy
1,310✔
51

1,310✔
52
                // The fields for the ToNode can be overwritten by the path
1,310✔
53
                // finding algorithm, which is why we need a deep copy in the
1,310✔
54
                // first place. So we always start out with nil values, just to
1,310✔
55
                // be sure they don't contain any old data.
1,310✔
56
                channelCopy.InPolicy.ToNodePubKey = nil
1,310✔
57
                channelCopy.InPolicy.ToNodeFeatures = nil
1,310✔
58
        }
1,310✔
59

60
        return &channelCopy
1,336✔
61
}
62

63
// GraphCache is a type that holds a minimal set of information of the public
64
// channel graph that can be used for pathfinding.
65
type GraphCache struct {
66
        nodeChannels map[route.Vertex]map[uint64]*DirectedChannel
67
        nodeFeatures map[route.Vertex]*lnwire.FeatureVector
68

69
        mtx sync.RWMutex
70
}
71

72
// NewGraphCache creates a new graphCache.
73
func NewGraphCache(preAllocNumNodes int) *GraphCache {
142✔
74
        return &GraphCache{
142✔
75
                nodeChannels: make(
142✔
76
                        map[route.Vertex]map[uint64]*DirectedChannel,
142✔
77
                        // A channel connects two nodes, so we can look it up
142✔
78
                        // from both sides, meaning we get double the number of
142✔
79
                        // entries.
142✔
80
                        preAllocNumNodes*2,
142✔
81
                ),
142✔
82
                nodeFeatures: make(
142✔
83
                        map[route.Vertex]*lnwire.FeatureVector,
142✔
84
                        preAllocNumNodes,
142✔
85
                ),
142✔
86
        }
142✔
87
}
142✔
88

89
// Stats returns statistics about the current cache size.
90
func (c *GraphCache) Stats() string {
381✔
91
        c.mtx.RLock()
381✔
92
        defer c.mtx.RUnlock()
381✔
93

381✔
94
        numChannels := 0
381✔
95
        for node := range c.nodeChannels {
776✔
96
                numChannels += len(c.nodeChannels[node])
395✔
97
        }
395✔
98
        return fmt.Sprintf("num_node_features=%d, num_nodes=%d, "+
381✔
99
                "num_channels=%d", len(c.nodeFeatures), len(c.nodeChannels),
381✔
100
                numChannels)
381✔
101
}
102

103
// AddNodeFeatures adds a graph node and its features to the cache.
104
func (c *GraphCache) AddNodeFeatures(node route.Vertex,
105
        features *lnwire.FeatureVector) {
715✔
106

715✔
107
        c.mtx.Lock()
715✔
108
        defer c.mtx.Unlock()
715✔
109

715✔
110
        c.nodeFeatures[node] = features
715✔
111
}
715✔
112

113
// AddChannel adds a non-directed channel, meaning that the order of policy 1
114
// and policy 2 does not matter, the directionality is extracted from the info
115
// and policy flags automatically. The policy will be set as the outgoing policy
116
// on one node and the incoming policy on the peer's side.
117
func (c *GraphCache) AddChannel(info *models.ChannelEdgeInfo,
118
        policy1 *models.ChannelEdgePolicy, policy2 *models.ChannelEdgePolicy) {
1,693✔
119

1,693✔
120
        if info == nil {
1,693✔
121
                return
×
122
        }
×
123

124
        if policy1 != nil && policy1.IsDisabled() &&
1,693✔
125
                policy2 != nil && policy2.IsDisabled() {
1,693✔
UNCOV
126

×
UNCOV
127
                return
×
UNCOV
128
        }
×
129

130
        // Create the edge entry for both nodes.
131
        c.mtx.Lock()
1,693✔
132
        c.updateOrAddEdge(info.NodeKey1Bytes, &DirectedChannel{
1,693✔
133
                ChannelID: info.ChannelID,
1,693✔
134
                IsNode1:   true,
1,693✔
135
                OtherNode: info.NodeKey2Bytes,
1,693✔
136
                Capacity:  info.Capacity,
1,693✔
137
        })
1,693✔
138
        c.updateOrAddEdge(info.NodeKey2Bytes, &DirectedChannel{
1,693✔
139
                ChannelID: info.ChannelID,
1,693✔
140
                IsNode1:   false,
1,693✔
141
                OtherNode: info.NodeKey1Bytes,
1,693✔
142
                Capacity:  info.Capacity,
1,693✔
143
        })
1,693✔
144
        c.mtx.Unlock()
1,693✔
145

1,693✔
146
        // The policy's node is always the to_node. So if policy 1 has to_node
1,693✔
147
        // of node 2 then we have the policy 1 as seen from node 1.
1,693✔
148
        if policy1 != nil {
2,091✔
149
                fromNode, toNode := info.NodeKey1Bytes, info.NodeKey2Bytes
398✔
150
                if policy1.ToNode != info.NodeKey2Bytes {
399✔
151
                        fromNode, toNode = toNode, fromNode
1✔
152
                }
1✔
153
                isEdge1 := policy1.ChannelFlags&lnwire.ChanUpdateDirection == 0
398✔
154
                c.UpdatePolicy(policy1, fromNode, toNode, isEdge1)
398✔
155
        }
156
        if policy2 != nil {
2,091✔
157
                fromNode, toNode := info.NodeKey2Bytes, info.NodeKey1Bytes
398✔
158
                if policy2.ToNode != info.NodeKey1Bytes {
399✔
159
                        fromNode, toNode = toNode, fromNode
1✔
160
                }
1✔
161
                isEdge1 := policy2.ChannelFlags&lnwire.ChanUpdateDirection == 0
398✔
162
                c.UpdatePolicy(policy2, fromNode, toNode, isEdge1)
398✔
163
        }
164
}
165

166
// updateOrAddEdge makes sure the edge information for a node is either updated
167
// if it already exists or is added to that node's list of channels.
168
func (c *GraphCache) updateOrAddEdge(node route.Vertex, edge *DirectedChannel) {
3,386✔
169
        if len(c.nodeChannels[node]) == 0 {
4,243✔
170
                c.nodeChannels[node] = make(map[uint64]*DirectedChannel)
857✔
171
        }
857✔
172

173
        c.nodeChannels[node][edge.ChannelID] = edge
3,386✔
174
}
175

176
// UpdatePolicy updates a single policy on both the from and to node. The order
177
// of the from and to node is not strictly important. But we assume that a
178
// channel edge was added beforehand so that the directed channel struct already
179
// exists in the cache.
180
func (c *GraphCache) UpdatePolicy(policy *models.ChannelEdgePolicy, fromNode,
181
        toNode route.Vertex, edge1 bool) {
3,070✔
182

3,070✔
183
        // Extract inbound fee if possible and available. If there is a decoding
3,070✔
184
        // error, ignore this policy.
3,070✔
185
        var inboundFee lnwire.Fee
3,070✔
186
        _, err := policy.ExtraOpaqueData.ExtractRecords(&inboundFee)
3,070✔
187
        if err != nil {
3,070✔
188
                log.Errorf("Failed to extract records from edge policy %v: %v",
×
189
                        policy.ChannelID, err)
×
190

×
191
                return
×
192
        }
×
193

194
        c.mtx.Lock()
3,070✔
195
        defer c.mtx.Unlock()
3,070✔
196

3,070✔
197
        updatePolicy := func(nodeKey route.Vertex) {
9,210✔
198
                if len(c.nodeChannels[nodeKey]) == 0 {
6,140✔
199
                        log.Warnf("Node=%v not found in graph cache", nodeKey)
×
200

×
201
                        return
×
202
                }
×
203

204
                channel, ok := c.nodeChannels[nodeKey][policy.ChannelID]
6,140✔
205
                if !ok {
6,140✔
206
                        log.Warnf("Channel=%v not found in graph cache",
×
207
                                policy.ChannelID)
×
208

×
209
                        return
×
210
                }
×
211

212
                // Edge 1 is defined as the policy for the direction of node1 to
213
                // node2.
214
                switch {
6,140✔
215
                // This is node 1, and it is edge 1, so this is the outgoing
216
                // policy for node 1.
217
                case channel.IsNode1 && edge1:
1,537✔
218
                        channel.OutPolicySet = true
1,537✔
219
                        channel.InboundFee = inboundFee
1,537✔
220

221
                // This is node 2, and it is edge 2, so this is the outgoing
222
                // policy for node 2.
223
                case !channel.IsNode1 && !edge1:
1,533✔
224
                        channel.OutPolicySet = true
1,533✔
225
                        channel.InboundFee = inboundFee
1,533✔
226

227
                // The other two cases left mean it's the inbound policy for the
228
                // node.
229
                default:
3,070✔
230
                        channel.InPolicy = models.NewCachedPolicy(policy)
3,070✔
231
                }
232
        }
233

234
        updatePolicy(fromNode)
3,070✔
235
        updatePolicy(toNode)
3,070✔
236
}
237

238
// RemoveNode completely removes a node and all its channels (including the
239
// peer's side).
240
func (c *GraphCache) RemoveNode(node route.Vertex) {
68✔
241
        c.mtx.Lock()
68✔
242
        defer c.mtx.Unlock()
68✔
243

68✔
244
        delete(c.nodeFeatures, node)
68✔
245

68✔
246
        // First remove all channels from the other nodes' lists.
68✔
247
        for _, channel := range c.nodeChannels[node] {
68✔
248
                c.removeChannelIfFound(channel.OtherNode, channel.ChannelID)
×
249
        }
×
250

251
        // Then remove our whole node completely.
252
        delete(c.nodeChannels, node)
68✔
253
}
254

255
// RemoveChannel removes a single channel between two nodes.
256
func (c *GraphCache) RemoveChannel(node1, node2 route.Vertex, chanID uint64) {
262✔
257
        c.mtx.Lock()
262✔
258
        defer c.mtx.Unlock()
262✔
259

262✔
260
        // Remove that one channel from both sides.
262✔
261
        c.removeChannelIfFound(node1, chanID)
262✔
262
        c.removeChannelIfFound(node2, chanID)
262✔
263
}
262✔
264

265
// removeChannelIfFound removes a single channel from one side.
266
func (c *GraphCache) removeChannelIfFound(node route.Vertex, chanID uint64) {
524✔
267
        if len(c.nodeChannels[node]) == 0 {
686✔
268
                return
162✔
269
        }
162✔
270

271
        delete(c.nodeChannels[node], chanID)
362✔
272
}
273

274
// UpdateChannel updates the channel edge information for a specific edge. We
275
// expect the edge to already exist and be known. If it does not yet exist, this
276
// call is a no-op.
277
func (c *GraphCache) UpdateChannel(info *models.ChannelEdgeInfo) {
×
278
        c.mtx.Lock()
×
279
        defer c.mtx.Unlock()
×
280

×
281
        if len(c.nodeChannels[info.NodeKey1Bytes]) == 0 ||
×
282
                len(c.nodeChannels[info.NodeKey2Bytes]) == 0 {
×
283

×
284
                return
×
285
        }
×
286

287
        channel, ok := c.nodeChannels[info.NodeKey1Bytes][info.ChannelID]
×
288
        if ok {
×
289
                // We only expect to be called when the channel is already
×
290
                // known.
×
291
                channel.Capacity = info.Capacity
×
292
                channel.OtherNode = info.NodeKey2Bytes
×
293
        }
×
294

295
        channel, ok = c.nodeChannels[info.NodeKey2Bytes][info.ChannelID]
×
296
        if ok {
×
297
                channel.Capacity = info.Capacity
×
298
                channel.OtherNode = info.NodeKey1Bytes
×
299
        }
×
300
}
301

302
// getChannels returns a copy of the passed node's channels or nil if there
303
// isn't any.
304
func (c *GraphCache) getChannels(node route.Vertex) []*DirectedChannel {
501✔
305
        c.mtx.RLock()
501✔
306
        defer c.mtx.RUnlock()
501✔
307

501✔
308
        channels, ok := c.nodeChannels[node]
501✔
309
        if !ok {
508✔
310
                return nil
7✔
311
        }
7✔
312

313
        features, ok := c.nodeFeatures[node]
494✔
314
        if !ok {
508✔
315
                // If the features were set to nil explicitly, that's fine here.
14✔
316
                // The router will overwrite the features of the destination
14✔
317
                // node with those found in the invoice if necessary. But if we
14✔
318
                // didn't yet get a node announcement we want to mimic the
14✔
319
                // behavior of the old DB based code that would always set an
14✔
320
                // empty feature vector instead of leaving it nil.
14✔
321
                features = lnwire.EmptyFeatureVector()
14✔
322
        }
14✔
323

324
        toNodeCallback := func() route.Vertex {
942✔
325
                return node
448✔
326
        }
448✔
327

328
        i := 0
494✔
329
        channelsCopy := make([]*DirectedChannel, len(channels))
494✔
330
        for _, channel := range channels {
1,826✔
331
                // We need to copy the channel and policy to avoid it being
1,332✔
332
                // updated in the cache if the path finding algorithm sets
1,332✔
333
                // fields on it (currently only the ToNodeFeatures of the
1,332✔
334
                // policy).
1,332✔
335
                channelCopy := channel.DeepCopy()
1,332✔
336
                if channelCopy.InPolicy != nil {
2,639✔
337
                        channelCopy.InPolicy.ToNodePubKey = toNodeCallback
1,307✔
338
                        channelCopy.InPolicy.ToNodeFeatures = features
1,307✔
339
                }
1,307✔
340

341
                channelsCopy[i] = channelCopy
1,332✔
342
                i++
1,332✔
343
        }
344

345
        return channelsCopy
494✔
346
}
347

348
// ForEachChannel invokes the given callback for each channel of the given node.
349
func (c *GraphCache) ForEachChannel(node route.Vertex,
350
        cb func(channel *DirectedChannel) error) error {
501✔
351

501✔
352
        // Obtain a copy of the node's channels. We need do this in order to
501✔
353
        // avoid deadlocks caused by interaction with the graph cache, channel
501✔
354
        // state and the graph database from multiple goroutines. This snapshot
501✔
355
        // is only used for path finding where being stale is acceptable since
501✔
356
        // the real world graph and our representation may always become
501✔
357
        // slightly out of sync for a short time and the actual channel state
501✔
358
        // is stored separately.
501✔
359
        channels := c.getChannels(node)
501✔
360
        for _, channel := range channels {
1,833✔
361
                if err := cb(channel); err != nil {
1,332✔
362
                        return err
×
363
                }
×
364
        }
365

366
        return nil
501✔
367
}
368

369
// ForEachNode iterates over the adjacency list of the graph, executing the
370
// call back for each node and the set of channels that emanate from the given
371
// node.
372
//
373
// NOTE: This method should be considered _read only_, the channels or nodes
374
// passed in MUST NOT be modified.
375
func (c *GraphCache) ForEachNode(cb func(node route.Vertex,
376
        channels map[uint64]*DirectedChannel) error) error {
2✔
377

2✔
378
        c.mtx.RLock()
2✔
379
        defer c.mtx.RUnlock()
2✔
380

2✔
381
        for node, channels := range c.nodeChannels {
6✔
382
                // We don't make a copy here since this is a read-only RPC
4✔
383
                // call. We also don't need the node features either for this
4✔
384
                // call.
4✔
385
                if err := cb(node, channels); err != nil {
4✔
386
                        return err
×
387
                }
×
388
        }
389

390
        return nil
2✔
391
}
392

393
// GetFeatures returns the features of the node with the given ID. If no
394
// features are known for the node, an empty feature vector is returned.
395
func (c *GraphCache) GetFeatures(node route.Vertex) *lnwire.FeatureVector {
459✔
396
        c.mtx.RLock()
459✔
397
        defer c.mtx.RUnlock()
459✔
398

459✔
399
        features, ok := c.nodeFeatures[node]
459✔
400
        if !ok || features == nil {
469✔
401
                // The router expects the features to never be nil, so we return
10✔
402
                // an empty feature set instead.
10✔
403
                return lnwire.EmptyFeatureVector()
10✔
404
        }
10✔
405

406
        return features
449✔
407
}
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