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

lightningnetwork / lnd / 12762813874

14 Jan 2025 07:14AM UTC coverage: 58.704% (-0.02%) from 58.719%
12762813874

Pull #9383

github

ziggie1984
localchans: bugfix so that we always use the correct chanID
Pull Request #9383: bugfix createmissingedge

5 of 7 new or added lines in 1 file covered. (71.43%)

61 existing lines in 13 files now uncovered.

135327 of 230526 relevant lines covered (58.7%)

19174.14 hits per line

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

94.86
/routing/unified_edges.go
1
package routing
2

3
import (
4
        "math"
5

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

14
// nodeEdgeUnifier holds all edge unifiers for connections towards a node.
15
type nodeEdgeUnifier struct {
16
        // edgeUnifiers contains an edge unifier for every from node.
17
        edgeUnifiers map[route.Vertex]*edgeUnifier
18

19
        // sourceNode is the sender of a payment. The rules to pick the final
20
        // policy are different for local channels.
21
        sourceNode route.Vertex
22

23
        // toNode is the node for which the edge unifiers are instantiated.
24
        toNode route.Vertex
25

26
        // useInboundFees indicates whether to take inbound fees into account.
27
        useInboundFees bool
28

29
        // outChanRestr is an optional outgoing channel restriction for the
30
        // local channel to use.
31
        outChanRestr map[uint64]struct{}
32
}
33

34
// newNodeEdgeUnifier instantiates a new nodeEdgeUnifier object. Channel
35
// policies can be added to this object.
36
func newNodeEdgeUnifier(sourceNode, toNode route.Vertex, useInboundFees bool,
37
        outChanRestr map[uint64]struct{}) *nodeEdgeUnifier {
717✔
38

717✔
39
        return &nodeEdgeUnifier{
717✔
40
                edgeUnifiers:   make(map[route.Vertex]*edgeUnifier),
717✔
41
                toNode:         toNode,
717✔
42
                useInboundFees: useInboundFees,
717✔
43
                sourceNode:     sourceNode,
717✔
44
                outChanRestr:   outChanRestr,
717✔
45
        }
717✔
46
}
717✔
47

48
// addPolicy adds a single channel policy. Capacity may be zero if unknown
49
// (light clients). We expect a non-nil payload size function and will request a
50
// graceful shutdown if it is not provided as this indicates that edges are
51
// incorrectly specified.
52
func (u *nodeEdgeUnifier) addPolicy(fromNode route.Vertex,
53
        edge *models.CachedEdgePolicy, inboundFee models.InboundFee,
54
        capacity btcutil.Amount, hopPayloadSizeFn PayloadSizeFunc,
55
        blindedPayment *BlindedPayment) {
1,677✔
56

1,677✔
57
        localChan := fromNode == u.sourceNode
1,677✔
58

1,677✔
59
        // Skip channels if there is an outgoing channel restriction.
1,677✔
60
        if localChan && u.outChanRestr != nil {
1,691✔
61
                if _, ok := u.outChanRestr[edge.ChannelID]; !ok {
22✔
62
                        return
8✔
63
                }
8✔
64
        }
65

66
        // Update the edgeUnifiers map.
67
        unifier, ok := u.edgeUnifiers[fromNode]
1,669✔
68
        if !ok {
3,321✔
69
                unifier = &edgeUnifier{
1,652✔
70
                        localChan: localChan,
1,652✔
71
                }
1,652✔
72
                u.edgeUnifiers[fromNode] = unifier
1,652✔
73
        }
1,652✔
74

75
        // In case no payload size function was provided a graceful shutdown
76
        // is requested, because this function is not used as intended.
77
        if hopPayloadSizeFn == nil {
1,669✔
78
                log.Criticalf("No payloadsize function was provided for the "+
×
79
                        "edge (chanid=%v) when adding it to the edge unifier "+
×
80
                        "of node: %v", edge.ChannelID, fromNode)
×
81

×
82
                return
×
83
        }
×
84

85
        // Zero inbound fee for exit hops.
86
        if !u.useInboundFees {
2,167✔
87
                inboundFee = models.InboundFee{}
498✔
88
        }
498✔
89

90
        unifier.edges = append(unifier.edges, newUnifiedEdge(
1,669✔
91
                edge, capacity, inboundFee, hopPayloadSizeFn, blindedPayment,
1,669✔
92
        ))
1,669✔
93
}
94

95
// addGraphPolicies adds all policies that are known for the toNode in the
96
// graph.
97
func (u *nodeEdgeUnifier) addGraphPolicies(g Graph) error {
711✔
98
        cb := func(channel *graphdb.DirectedChannel) error {
2,362✔
99
                // If there is no edge policy for this candidate node, skip.
1,651✔
100
                // Note that we are searching backwards so this node would have
1,651✔
101
                // come prior to the pivot node in the route.
1,651✔
102
                if channel.InPolicy == nil {
1,651✔
103
                        return nil
×
104
                }
×
105

106
                // Add this policy to the corresponding edgeUnifier. We default
107
                // to the clear hop payload size function because
108
                // `addGraphPolicies` is only used for cleartext intermediate
109
                // hops in a route.
110
                inboundFee := models.NewInboundFeeFromWire(
1,651✔
111
                        channel.InboundFee,
1,651✔
112
                )
1,651✔
113

1,651✔
114
                u.addPolicy(
1,651✔
115
                        channel.OtherNode, channel.InPolicy, inboundFee,
1,651✔
116
                        channel.Capacity, defaultHopPayloadSize, nil,
1,651✔
117
                )
1,651✔
118

1,651✔
119
                return nil
1,651✔
120
        }
121

122
        // Iterate over all channels of the to node.
123
        return g.ForEachNodeChannel(u.toNode, cb)
711✔
124
}
125

126
// unifiedEdge is the individual channel data that is kept inside an edgeUnifier
127
// object.
128
type unifiedEdge struct {
129
        policy      *models.CachedEdgePolicy
130
        capacity    btcutil.Amount
131
        inboundFees models.InboundFee
132

133
        // hopPayloadSize supplies an edge with the ability to calculate the
134
        // exact payload size if this edge would be included in a route. This
135
        // is needed because hops of a blinded path differ in their payload
136
        // structure compared to cleartext hops.
137
        hopPayloadSizeFn PayloadSizeFunc
138

139
        // blindedPayment if set, is the BlindedPayment that this edge was
140
        // derived from originally.
141
        blindedPayment *BlindedPayment
142
}
143

144
// newUnifiedEdge constructs a new unifiedEdge.
145
func newUnifiedEdge(policy *models.CachedEdgePolicy, capacity btcutil.Amount,
146
        inboundFees models.InboundFee, hopPayloadSizeFn PayloadSizeFunc,
147
        blindedPayment *BlindedPayment) *unifiedEdge {
4,089✔
148

4,089✔
149
        return &unifiedEdge{
4,089✔
150
                policy:           policy,
4,089✔
151
                capacity:         capacity,
4,089✔
152
                inboundFees:      inboundFees,
4,089✔
153
                hopPayloadSizeFn: hopPayloadSizeFn,
4,089✔
154
                blindedPayment:   blindedPayment,
4,089✔
155
        }
4,089✔
156
}
4,089✔
157

158
// amtInRange checks whether an amount falls within the valid range for a
159
// channel.
160
func (u *unifiedEdge) amtInRange(amt lnwire.MilliSatoshi) bool {
1,354✔
161
        // If the capacity is available (non-light clients), skip channels that
1,354✔
162
        // are too small.
1,354✔
163
        if u.capacity > 0 &&
1,354✔
164
                amt > lnwire.NewMSatFromSatoshis(u.capacity) {
1,368✔
165

14✔
166
                log.Tracef("Not enough capacity: amt=%v, capacity=%v",
14✔
167
                        amt, u.capacity)
14✔
168
                return false
14✔
169
        }
14✔
170

171
        // Skip channels for which this htlc is too large.
172
        if u.policy.MessageFlags.HasMaxHtlc() &&
1,340✔
173
                amt > u.policy.MaxHTLC {
1,350✔
174

10✔
175
                log.Tracef("Exceeds policy's MaxHTLC: amt=%v, MaxHTLC=%v",
10✔
176
                        amt, u.policy.MaxHTLC)
10✔
177
                return false
10✔
178
        }
10✔
179

180
        // Skip channels for which this htlc is too small.
181
        if amt < u.policy.MinHTLC {
1,346✔
182
                log.Tracef("below policy's MinHTLC: amt=%v, MinHTLC=%v",
13✔
183
                        amt, u.policy.MinHTLC)
13✔
184
                return false
13✔
185
        }
13✔
186

187
        return true
1,323✔
188
}
189

190
// edgeUnifier is an object that covers all channels between a pair of nodes.
191
type edgeUnifier struct {
192
        edges     []*unifiedEdge
193
        localChan bool
194
}
195

196
// getEdge returns the optimal unified edge to use for this connection given a
197
// specific amount to send. It differentiates between local and network
198
// channels.
199
func (u *edgeUnifier) getEdge(netAmtReceived lnwire.MilliSatoshi,
200
        bandwidthHints bandwidthHints,
201
        nextOutFee lnwire.MilliSatoshi) *unifiedEdge {
1,322✔
202

1,322✔
203
        if u.localChan {
1,497✔
204
                return u.getEdgeLocal(
175✔
205
                        netAmtReceived, bandwidthHints, nextOutFee,
175✔
206
                )
175✔
207
        }
175✔
208

209
        return u.getEdgeNetwork(netAmtReceived, nextOutFee)
1,150✔
210
}
211

212
// calcCappedInboundFee calculates the inbound fee for a channel, taking into
213
// account the total node fee for the "to" node.
214
func calcCappedInboundFee(edge *unifiedEdge, amt lnwire.MilliSatoshi,
215
        nextOutFee lnwire.MilliSatoshi) int64 {
1,347✔
216

1,347✔
217
        // Calculate the inbound fee charged for the amount that passes over the
1,347✔
218
        // channel.
1,347✔
219
        inboundFee := edge.inboundFees.CalcFee(amt)
1,347✔
220

1,347✔
221
        // Take into account that the total node fee cannot be negative.
1,347✔
222
        if inboundFee < -int64(nextOutFee) {
1,350✔
223
                inboundFee = -int64(nextOutFee)
3✔
224
        }
3✔
225

226
        return inboundFee
1,347✔
227
}
228

229
// getEdgeLocal returns the optimal unified edge to use for this local
230
// connection given a specific amount to send.
231
func (u *edgeUnifier) getEdgeLocal(netAmtReceived lnwire.MilliSatoshi,
232
        bandwidthHints bandwidthHints,
233
        nextOutFee lnwire.MilliSatoshi) *unifiedEdge {
175✔
234

175✔
235
        var (
175✔
236
                bestEdge     *unifiedEdge
175✔
237
                maxBandwidth lnwire.MilliSatoshi
175✔
238
        )
175✔
239

175✔
240
        for _, edge := range u.edges {
353✔
241
                // Calculate the inbound fee charged at the receiving node.
178✔
242
                inboundFee := calcCappedInboundFee(
178✔
243
                        edge, netAmtReceived, nextOutFee,
178✔
244
                )
178✔
245

178✔
246
                // Add inbound fee to get to the amount that is sent over the
178✔
247
                // local channel.
178✔
248
                amt := netAmtReceived + lnwire.MilliSatoshi(inboundFee)
178✔
249

178✔
250
                // Check valid amount range for the channel. We skip this test
178✔
251
                // for payments with custom HTLC data, as the amount sent on
178✔
252
                // the BTC layer may differ from the amount that is actually
178✔
253
                // forwarded in custom channels.
178✔
254
                if bandwidthHints.firstHopCustomBlob().IsNone() &&
178✔
255
                        !edge.amtInRange(amt) {
189✔
256

11✔
257
                        log.Debugf("Amount %v not in range for edge %v",
11✔
258
                                netAmtReceived, edge.policy.ChannelID)
11✔
259

11✔
260
                        continue
11✔
261
                }
262

263
                // For local channels, there is no fee to pay or an extra time
264
                // lock. We only consider the currently available bandwidth for
265
                // channel selection. The disabled flag is ignored for local
266
                // channels.
267

268
                // Retrieve bandwidth for this local channel. If not
269
                // available, assume this channel has enough bandwidth.
270
                //
271
                // TODO(joostjager): Possibly change to skipping this
272
                // channel. The bandwidth hint is expected to be
273
                // available.
274
                bandwidth, ok := bandwidthHints.availableChanBandwidth(
167✔
275
                        edge.policy.ChannelID, amt,
167✔
276
                )
167✔
277
                if !ok {
246✔
278
                        log.Debugf("Cannot get bandwidth for edge %v, use max "+
79✔
279
                                "instead", edge.policy.ChannelID)
79✔
280
                        bandwidth = lnwire.MaxMilliSatoshi
79✔
281
                }
79✔
282

283
                // TODO(yy): if the above `!ok` is chosen, we'd have
284
                // `bandwidth` to be the max value, which will end up having
285
                // the `maxBandwidth` to be have the largest value and this
286
                // edge will be the chosen one. This is wrong in two ways,
287
                // 1. we need to understand why `availableChanBandwidth` cannot
288
                // find bandwidth for this edge as something is wrong with this
289
                // channel, and,
290
                // 2. this edge is likely NOT the local channel with the
291
                // highest available bandwidth.
292
                //
293
                // Skip channels that can't carry the payment.
294
                if amt > bandwidth {
177✔
295
                        log.Debugf("Skipped edge %v: not enough bandwidth, "+
10✔
296
                                "bandwidth=%v, amt=%v", edge.policy.ChannelID,
10✔
297
                                bandwidth, amt)
10✔
298

10✔
299
                        continue
10✔
300
                }
301

302
                // We pick the local channel with the highest available
303
                // bandwidth, to maximize the success probability. It can be
304
                // that the channel state changes between querying the bandwidth
305
                // hints and sending out the htlc.
306
                if bandwidth < maxBandwidth {
160✔
UNCOV
307
                        log.Debugf("Skipped edge %v: not max bandwidth, "+
×
UNCOV
308
                                "bandwidth=%v, maxBandwidth=%v",
×
UNCOV
309
                                edge.policy.ChannelID, bandwidth, maxBandwidth)
×
UNCOV
310

×
UNCOV
311
                        continue
×
312
                }
313
                maxBandwidth = bandwidth
160✔
314

160✔
315
                // Update best edge.
160✔
316
                bestEdge = newUnifiedEdge(
160✔
317
                        edge.policy, edge.capacity, edge.inboundFees,
160✔
318
                        edge.hopPayloadSizeFn, edge.blindedPayment,
160✔
319
                )
160✔
320
        }
321

322
        return bestEdge
175✔
323
}
324

325
// getEdgeNetwork returns the optimal unified edge to use for this connection
326
// given a specific amount to send. The goal is to return a unified edge with a
327
// policy that maximizes the probability of a successful forward in a non-strict
328
// forwarding context.
329
func (u *edgeUnifier) getEdgeNetwork(netAmtReceived lnwire.MilliSatoshi,
330
        nextOutFee lnwire.MilliSatoshi) *unifiedEdge {
1,150✔
331

1,150✔
332
        var (
1,150✔
333
                bestPolicy       *unifiedEdge
1,150✔
334
                maxFee           int64 = math.MinInt64
1,150✔
335
                maxTimelock      uint16
1,150✔
336
                maxCapMsat       lnwire.MilliSatoshi
1,150✔
337
                hopPayloadSizeFn PayloadSizeFunc
1,150✔
338
        )
1,150✔
339

1,150✔
340
        for _, edge := range u.edges {
2,311✔
341
                // Calculate the inbound fee charged at the receiving node.
1,161✔
342
                inboundFee := calcCappedInboundFee(
1,161✔
343
                        edge, netAmtReceived, nextOutFee,
1,161✔
344
                )
1,161✔
345

1,161✔
346
                // Add inbound fee to get to the amount that is sent over the
1,161✔
347
                // channel.
1,161✔
348
                amt := netAmtReceived + lnwire.MilliSatoshi(inboundFee)
1,161✔
349

1,161✔
350
                // Check valid amount range for the channel.
1,161✔
351
                if !edge.amtInRange(amt) {
1,183✔
352
                        log.Debugf("Amount %v not in range for edge %v",
22✔
353
                                amt, edge.policy.ChannelID)
22✔
354
                        continue
22✔
355
                }
356

357
                // For network channels, skip the disabled ones.
358
                edgeFlags := edge.policy.ChannelFlags
1,142✔
359
                isDisabled := edgeFlags&lnwire.ChanUpdateDisabled != 0
1,142✔
360
                if isDisabled {
1,144✔
361
                        log.Debugf("Skipped edge %v due to it being disabled",
2✔
362
                                edge.policy.ChannelID)
2✔
363
                        continue
2✔
364
                }
365

366
                // Track the maximal capacity for usable channels. If we don't
367
                // know the capacity, we fall back to MaxHTLC.
368
                capMsat := lnwire.NewMSatFromSatoshis(edge.capacity)
1,140✔
369
                if capMsat == 0 && edge.policy.MessageFlags.HasMaxHtlc() {
1,142✔
370
                        log.Tracef("No capacity available for channel %v, "+
2✔
371
                                "using MaxHtlcMsat (%v) as a fallback.",
2✔
372
                                edge.policy.ChannelID, edge.policy.MaxHTLC)
2✔
373

2✔
374
                        capMsat = edge.policy.MaxHTLC
2✔
375
                }
2✔
376
                maxCapMsat = lntypes.Max(capMsat, maxCapMsat)
1,140✔
377

1,140✔
378
                // Track the maximum time lock of all channels that are
1,140✔
379
                // candidate for non-strict forwarding at the routing node.
1,140✔
380
                maxTimelock = lntypes.Max(
1,140✔
381
                        maxTimelock, edge.policy.TimeLockDelta,
1,140✔
382
                )
1,140✔
383

1,140✔
384
                outboundFee := int64(edge.policy.ComputeFee(amt))
1,140✔
385
                fee := outboundFee + inboundFee
1,140✔
386

1,140✔
387
                // Use the policy that results in the highest fee for this
1,140✔
388
                // specific amount.
1,140✔
389
                if fee < maxFee {
1,143✔
390
                        log.Debugf("Skipped edge %v due to it produces less "+
3✔
391
                                "fee: fee=%v, maxFee=%v",
3✔
392
                                edge.policy.ChannelID, fee, maxFee)
3✔
393

3✔
394
                        continue
3✔
395
                }
396
                maxFee = fee
1,137✔
397

1,137✔
398
                bestPolicy = newUnifiedEdge(
1,137✔
399
                        edge.policy, 0, edge.inboundFees, nil,
1,137✔
400
                        edge.blindedPayment,
1,137✔
401
                )
1,137✔
402

1,137✔
403
                // The payload size function for edges to a connected peer is
1,137✔
404
                // always the same hence there is not need to find the maximum.
1,137✔
405
                // This also counts for blinded edges where we only have one
1,137✔
406
                // edge to a blinded peer.
1,137✔
407
                hopPayloadSizeFn = edge.hopPayloadSizeFn
1,137✔
408
        }
409

410
        // Return early if no channel matches.
411
        if bestPolicy == nil {
1,171✔
412
                return nil
21✔
413
        }
21✔
414

415
        // We have already picked the highest fee that could be required for
416
        // non-strict forwarding. To also cover the case where a lower fee
417
        // channel requires a longer time lock, we modify the policy by setting
418
        // the maximum encountered time lock. Note that this results in a
419
        // synthetic policy that is not actually present on the routing node.
420
        //
421
        // The reason we do this, is that we try to maximize the chance that we
422
        // get forwarded. Because we penalize pair-wise, there won't be a second
423
        // chance for this node pair. But this is all only needed for nodes that
424
        // have distinct policies for channels to the same peer.
425
        policyCopy := *bestPolicy.policy
1,132✔
426
        policyCopy.TimeLockDelta = maxTimelock
1,132✔
427
        modifiedEdge := newUnifiedEdge(
1,132✔
428
                &policyCopy, maxCapMsat.ToSatoshis(), bestPolicy.inboundFees,
1,132✔
429
                hopPayloadSizeFn, bestPolicy.blindedPayment,
1,132✔
430
        )
1,132✔
431

1,132✔
432
        return modifiedEdge
1,132✔
433
}
434

435
// minAmt returns the minimum amount that can be forwarded on this connection.
436
func (u *edgeUnifier) minAmt() lnwire.MilliSatoshi {
11✔
437
        min := lnwire.MaxMilliSatoshi
11✔
438
        for _, edge := range u.edges {
26✔
439
                min = lntypes.Min(min, edge.policy.MinHTLC)
15✔
440
        }
15✔
441

442
        return min
11✔
443
}
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