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

lightningnetwork / lnd / 14358372723

09 Apr 2025 01:26PM UTC coverage: 56.696% (-12.3%) from 69.037%
14358372723

Pull #9696

github

web-flow
Merge e2837e400 into 867d27d68
Pull Request #9696: Add `development_guidelines.md` for both human and machine

107055 of 188823 relevant lines covered (56.7%)

22721.56 hits per line

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

74.08
/routing/router.go
1
package routing
2

3
import (
4
        "context"
5
        "fmt"
6
        "math"
7
        "math/big"
8
        "sort"
9
        "sync"
10
        "sync/atomic"
11
        "time"
12

13
        "github.com/btcsuite/btcd/btcec/v2"
14
        "github.com/btcsuite/btcd/btcutil"
15
        "github.com/davecgh/go-spew/spew"
16
        "github.com/go-errors/errors"
17
        "github.com/lightningnetwork/lnd/amp"
18
        "github.com/lightningnetwork/lnd/channeldb"
19
        "github.com/lightningnetwork/lnd/clock"
20
        "github.com/lightningnetwork/lnd/fn/v2"
21
        "github.com/lightningnetwork/lnd/graph/db/models"
22
        "github.com/lightningnetwork/lnd/htlcswitch"
23
        "github.com/lightningnetwork/lnd/lntypes"
24
        "github.com/lightningnetwork/lnd/lnutils"
25
        "github.com/lightningnetwork/lnd/lnwallet"
26
        "github.com/lightningnetwork/lnd/lnwire"
27
        "github.com/lightningnetwork/lnd/record"
28
        "github.com/lightningnetwork/lnd/routing/route"
29
        "github.com/lightningnetwork/lnd/routing/shards"
30
        "github.com/lightningnetwork/lnd/tlv"
31
        "github.com/lightningnetwork/lnd/zpay32"
32
)
33

34
const (
35
        // DefaultPayAttemptTimeout is the default payment attempt timeout. The
36
        // payment attempt timeout defines the duration after which we stop
37
        // trying more routes for a payment.
38
        DefaultPayAttemptTimeout = time.Second * 60
39

40
        // MinCLTVDelta is the minimum CLTV value accepted by LND for all
41
        // timelock deltas. This includes both forwarding CLTV deltas set on
42
        // channel updates, as well as final CLTV deltas used to create BOLT 11
43
        // payment requests.
44
        //
45
        // NOTE: For payment requests, BOLT 11 stipulates that a final CLTV
46
        // delta of 9 should be used when no value is decoded. This however
47
        // leads to inflexibility in upgrading this default parameter, since it
48
        // can create inconsistencies around the assumed value between sender
49
        // and receiver. Specifically, if the receiver assumes a higher value
50
        // than the sender, the receiver will always see the received HTLCs as
51
        // invalid due to their timelock not meeting the required delta.
52
        //
53
        // We skirt this by always setting an explicit CLTV delta when creating
54
        // invoices. This allows LND nodes to freely update the minimum without
55
        // creating incompatibilities during the upgrade process. For some time
56
        // LND has used an explicit default final CLTV delta of 40 blocks for
57
        // bitcoin, though we now clamp the lower end of this
58
        // range for user-chosen deltas to 18 blocks to be conservative.
59
        MinCLTVDelta = 18
60

61
        // MaxCLTVDelta is the maximum CLTV value accepted by LND for all
62
        // timelock deltas.
63
        MaxCLTVDelta = math.MaxUint16
64
)
65

66
var (
67
        // ErrRouterShuttingDown is returned if the router is in the process of
68
        // shutting down.
69
        ErrRouterShuttingDown = fmt.Errorf("router shutting down")
70

71
        // ErrSelfIntro is a failure returned when the source node of a
72
        // route request is also the introduction node. This is not yet
73
        // supported because LND does not support blinded forwardingg.
74
        ErrSelfIntro = errors.New("introduction point as own node not " +
75
                "supported")
76

77
        // ErrHintsAndBlinded is returned if a route request has both
78
        // bolt 11 route hints and a blinded path set.
79
        ErrHintsAndBlinded = errors.New("bolt 11 route hints and blinded " +
80
                "paths are mutually exclusive")
81

82
        // ErrExpiryAndBlinded is returned if a final cltv and a blinded path
83
        // are provided, as the cltv should be provided within the blinded
84
        // path.
85
        ErrExpiryAndBlinded = errors.New("final cltv delta and blinded " +
86
                "paths are mutually exclusive")
87

88
        // ErrTargetAndBlinded is returned is a target destination and a
89
        // blinded path are both set (as the target is inferred from the
90
        // blinded path).
91
        ErrTargetAndBlinded = errors.New("target node and blinded paths " +
92
                "are mutually exclusive")
93

94
        // ErrNoTarget is returned when the target node for a route is not
95
        // provided by either a blinded route or a cleartext pubkey.
96
        ErrNoTarget = errors.New("destination not set in target or blinded " +
97
                "path")
98

99
        // ErrSkipTempErr is returned when a non-MPP is made yet the
100
        // skipTempErr flag is set.
101
        ErrSkipTempErr = errors.New("cannot skip temp error for non-MPP")
102
)
103

104
// PaymentAttemptDispatcher is used by the router to send payment attempts onto
105
// the network, and receive their results.
106
type PaymentAttemptDispatcher interface {
107
        // SendHTLC is a function that directs a link-layer switch to
108
        // forward a fully encoded payment to the first hop in the route
109
        // denoted by its public key. A non-nil error is to be returned if the
110
        // payment was unsuccessful.
111
        SendHTLC(firstHop lnwire.ShortChannelID,
112
                attemptID uint64,
113
                htlcAdd *lnwire.UpdateAddHTLC) error
114

115
        // GetAttemptResult returns the result of the payment attempt with
116
        // the given attemptID. The paymentHash should be set to the payment's
117
        // overall hash, or in case of AMP payments the payment's unique
118
        // identifier.
119
        //
120
        // The method returns a channel where the payment result will be sent
121
        // when available, or an error is encountered during forwarding. When a
122
        // result is received on the channel, the HTLC is guaranteed to no
123
        // longer be in flight.  The switch shutting down is signaled by
124
        // closing the channel. If the attemptID is unknown,
125
        // ErrPaymentIDNotFound will be returned.
126
        GetAttemptResult(attemptID uint64, paymentHash lntypes.Hash,
127
                deobfuscator htlcswitch.ErrorDecrypter) (
128
                <-chan *htlcswitch.PaymentResult, error)
129

130
        // CleanStore calls the underlying result store, telling it is safe to
131
        // delete all entries except the ones in the keepPids map. This should
132
        // be called periodically to let the switch clean up payment results
133
        // that we have handled.
134
        // NOTE: New payment attempts MUST NOT be made after the keepPids map
135
        // has been created and this method has returned.
136
        CleanStore(keepPids map[uint64]struct{}) error
137

138
        // HasAttemptResult reads the network result store to fetch the
139
        // specified attempt. Returns true if the attempt result exists.
140
        //
141
        // NOTE: This method is used and should only be used by the router to
142
        // resume payments during startup. It can be viewed as a subset of
143
        // `GetAttemptResult` in terms of its functionality, and can be removed
144
        // once we move the construction of `UpdateAddHTLC` and
145
        // `ErrorDecrypter` into `htlcswitch`.
146
        HasAttemptResult(attemptID uint64) (bool, error)
147
}
148

149
// PaymentSessionSource is an interface that defines a source for the router to
150
// retrieve new payment sessions.
151
type PaymentSessionSource interface {
152
        // NewPaymentSession creates a new payment session that will produce
153
        // routes to the given target. An optional set of routing hints can be
154
        // provided in order to populate additional edges to explore when
155
        // finding a path to the payment's destination.
156
        NewPaymentSession(p *LightningPayment,
157
                firstHopBlob fn.Option[tlv.Blob],
158
                ts fn.Option[htlcswitch.AuxTrafficShaper]) (PaymentSession,
159
                error)
160

161
        // NewPaymentSessionEmpty creates a new paymentSession instance that is
162
        // empty, and will be exhausted immediately. Used for failure reporting
163
        // to missioncontrol for resumed payment we don't want to make more
164
        // attempts for.
165
        NewPaymentSessionEmpty() PaymentSession
166
}
167

168
// MissionControlQuerier is an interface that exposes failure reporting and
169
// probability estimation.
170
type MissionControlQuerier interface {
171
        // ReportPaymentFail reports a failed payment to mission control as
172
        // input for future probability estimates. It returns a bool indicating
173
        // whether this error is a final error and no further payment attempts
174
        // need to be made.
175
        ReportPaymentFail(attemptID uint64, rt *route.Route,
176
                failureSourceIdx *int, failure lnwire.FailureMessage) (
177
                *channeldb.FailureReason, error)
178

179
        // ReportPaymentSuccess reports a successful payment to mission control
180
        // as input for future probability estimates.
181
        ReportPaymentSuccess(attemptID uint64, rt *route.Route) error
182

183
        // GetProbability is expected to return the success probability of a
184
        // payment from fromNode along edge.
185
        GetProbability(fromNode, toNode route.Vertex,
186
                amt lnwire.MilliSatoshi, capacity btcutil.Amount) float64
187
}
188

189
// FeeSchema is the set fee configuration for a Lightning Node on the network.
190
// Using the coefficients described within the schema, the required fee to
191
// forward outgoing payments can be derived.
192
type FeeSchema struct {
193
        // BaseFee is the base amount of milli-satoshis that will be chained
194
        // for ANY payment forwarded.
195
        BaseFee lnwire.MilliSatoshi
196

197
        // FeeRate is the rate that will be charged for forwarding payments.
198
        // This value should be interpreted as the numerator for a fraction
199
        // (fixed point arithmetic) whose denominator is 1 million. As a result
200
        // the effective fee rate charged per mSAT will be: (amount *
201
        // FeeRate/1,000,000).
202
        FeeRate uint32
203

204
        // InboundFee is the inbound fee schedule that applies to forwards
205
        // coming in through a channel to which this FeeSchema pertains.
206
        InboundFee fn.Option[models.InboundFee]
207
}
208

209
// ChannelPolicy holds the parameters that determine the policy we enforce
210
// when forwarding payments on a channel. These parameters are communicated
211
// to the rest of the network in ChannelUpdate messages.
212
type ChannelPolicy struct {
213
        // FeeSchema holds the fee configuration for a channel.
214
        FeeSchema
215

216
        // TimeLockDelta is the required HTLC timelock delta to be used
217
        // when forwarding payments.
218
        TimeLockDelta uint32
219

220
        // MaxHTLC is the maximum HTLC size including fees we are allowed to
221
        // forward over this channel.
222
        MaxHTLC lnwire.MilliSatoshi
223

224
        // MinHTLC is the minimum HTLC size including fees we are allowed to
225
        // forward over this channel.
226
        MinHTLC *lnwire.MilliSatoshi
227
}
228

229
// Config defines the configuration for the ChannelRouter. ALL elements within
230
// the configuration MUST be non-nil for the ChannelRouter to carry out its
231
// duties.
232
type Config struct {
233
        // SelfNode is the public key of the node that this channel router
234
        // belongs to.
235
        SelfNode route.Vertex
236

237
        // RoutingGraph is a graph source that will be used for pathfinding.
238
        RoutingGraph Graph
239

240
        // Chain is the router's source to the most up-to-date blockchain data.
241
        // All incoming advertised channels will be checked against the chain
242
        // to ensure that the channels advertised are still open.
243
        Chain lnwallet.BlockChainIO
244

245
        // Payer is an instance of a PaymentAttemptDispatcher and is used by
246
        // the router to send payment attempts onto the network, and receive
247
        // their results.
248
        Payer PaymentAttemptDispatcher
249

250
        // Control keeps track of the status of ongoing payments, ensuring we
251
        // can properly resume them across restarts.
252
        Control ControlTower
253

254
        // MissionControl is a shared memory of sorts that executions of
255
        // payment path finding use in order to remember which vertexes/edges
256
        // were pruned from prior attempts. During SendPayment execution,
257
        // errors sent by nodes are mapped into a vertex or edge to be pruned.
258
        // Each run will then take into account this set of pruned
259
        // vertexes/edges to reduce route failure and pass on graph information
260
        // gained to the next execution.
261
        MissionControl MissionControlQuerier
262

263
        // SessionSource defines a source for the router to retrieve new payment
264
        // sessions.
265
        SessionSource PaymentSessionSource
266

267
        // GetLink is a method that allows the router to query the lower link
268
        // layer to determine the up-to-date available bandwidth at a
269
        // prospective link to be traversed. If the link isn't available, then
270
        // a value of zero should be returned. Otherwise, the current up-to-
271
        // date knowledge of the available bandwidth of the link should be
272
        // returned.
273
        GetLink getLinkQuery
274

275
        // NextPaymentID is a method that guarantees to return a new, unique ID
276
        // each time it is called. This is used by the router to generate a
277
        // unique payment ID for each payment it attempts to send, such that
278
        // the switch can properly handle the HTLC.
279
        NextPaymentID func() (uint64, error)
280

281
        // PathFindingConfig defines global path finding parameters.
282
        PathFindingConfig PathFindingConfig
283

284
        // Clock is mockable time provider.
285
        Clock clock.Clock
286

287
        // ApplyChannelUpdate can be called to apply a new channel update to the
288
        // graph that we received from a payment failure.
289
        ApplyChannelUpdate func(msg *lnwire.ChannelUpdate1) bool
290

291
        // ClosedSCIDs is used by the router to fetch closed channels.
292
        //
293
        // TODO(yy): remove it once the root cause of stuck payments is found.
294
        ClosedSCIDs map[lnwire.ShortChannelID]struct{}
295

296
        // TrafficShaper is an optional traffic shaper that can be used to
297
        // control the outgoing channel of a payment.
298
        TrafficShaper fn.Option[htlcswitch.AuxTrafficShaper]
299
}
300

301
// EdgeLocator is a struct used to identify a specific edge.
302
type EdgeLocator struct {
303
        // ChannelID is the channel of this edge.
304
        ChannelID uint64
305

306
        // Direction takes the value of 0 or 1 and is identical in definition to
307
        // the channel direction flag. A value of 0 means the direction from the
308
        // lower node pubkey to the higher.
309
        Direction uint8
310
}
311

312
// String returns a human-readable version of the edgeLocator values.
313
func (e *EdgeLocator) String() string {
×
314
        return fmt.Sprintf("%v:%v", e.ChannelID, e.Direction)
×
315
}
×
316

317
// ChannelRouter is the layer 3 router within the Lightning stack. Below the
318
// ChannelRouter is the HtlcSwitch, and below that is the Bitcoin blockchain
319
// itself. The primary role of the ChannelRouter is to respond to queries for
320
// potential routes that can support a payment amount, and also general graph
321
// reachability questions. The router will prune the channel graph
322
// automatically as new blocks are discovered which spend certain known funding
323
// outpoints, thereby closing their respective channels.
324
type ChannelRouter struct {
325
        started uint32 // To be used atomically.
326
        stopped uint32 // To be used atomically.
327

328
        // cfg is a copy of the configuration struct that the ChannelRouter was
329
        // initialized with.
330
        cfg *Config
331

332
        quit chan struct{}
333
        wg   sync.WaitGroup
334
}
335

336
// New creates a new instance of the ChannelRouter with the specified
337
// configuration parameters. As part of initialization, if the router detects
338
// that the channel graph isn't fully in sync with the latest UTXO (since the
339
// channel graph is a subset of the UTXO set) set, then the router will proceed
340
// to fully sync to the latest state of the UTXO set.
341
func New(cfg Config) (*ChannelRouter, error) {
17✔
342
        return &ChannelRouter{
17✔
343
                cfg:  &cfg,
17✔
344
                quit: make(chan struct{}),
17✔
345
        }, nil
17✔
346
}
17✔
347

348
// Start launches all the goroutines the ChannelRouter requires to carry out
349
// its duties. If the router has already been started, then this method is a
350
// noop.
351
func (r *ChannelRouter) Start() error {
17✔
352
        if !atomic.CompareAndSwapUint32(&r.started, 0, 1) {
17✔
353
                return nil
×
354
        }
×
355

356
        log.Info("Channel Router starting")
17✔
357

17✔
358
        // If any payments are still in flight, we resume, to make sure their
17✔
359
        // results are properly handled.
17✔
360
        if err := r.resumePayments(); err != nil {
17✔
361
                log.Error("Failed to resume payments during startup")
×
362
        }
×
363

364
        return nil
17✔
365
}
366

367
// Stop signals the ChannelRouter to gracefully halt all routines. This method
368
// will *block* until all goroutines have excited. If the channel router has
369
// already stopped then this method will return immediately.
370
func (r *ChannelRouter) Stop() error {
17✔
371
        if !atomic.CompareAndSwapUint32(&r.stopped, 0, 1) {
17✔
372
                return nil
×
373
        }
×
374

375
        log.Info("Channel Router shutting down...")
17✔
376
        defer log.Debug("Channel Router shutdown complete")
17✔
377

17✔
378
        close(r.quit)
17✔
379
        r.wg.Wait()
17✔
380

17✔
381
        return nil
17✔
382
}
383

384
// RouteRequest contains the parameters for a pathfinding request. It may
385
// describe a request to make a regular payment or one to a blinded path
386
// (incdicated by a non-nil BlindedPayment field).
387
type RouteRequest struct {
388
        // Source is the node that the path originates from.
389
        Source route.Vertex
390

391
        // Target is the node that the path terminates at. If the route
392
        // includes a blinded path, target will be the blinded node id of the
393
        // final hop in the blinded route.
394
        Target route.Vertex
395

396
        // Amount is the Amount in millisatoshis to be delivered to the target
397
        // node.
398
        Amount lnwire.MilliSatoshi
399

400
        // TimePreference expresses the caller's time preference for
401
        // pathfinding.
402
        TimePreference float64
403

404
        // Restrictions provides a set of additional Restrictions that the
405
        // route must adhere to.
406
        Restrictions *RestrictParams
407

408
        // CustomRecords is a set of custom tlv records to include for the
409
        // final hop.
410
        CustomRecords record.CustomSet
411

412
        // RouteHints contains an additional set of edges to include in our
413
        // view of the graph. This may either be a set of hints for private
414
        // channels or a "virtual" hop hint that represents a blinded route.
415
        RouteHints RouteHints
416

417
        // FinalExpiry is the cltv delta for the final hop. If paying to a
418
        // blinded path, this value is a duplicate of the delta provided
419
        // in blinded payment.
420
        FinalExpiry uint16
421

422
        // BlindedPathSet contains a set of optional blinded paths and
423
        // parameters used to reach a target node blinded paths. This field is
424
        // mutually exclusive with the Target field.
425
        BlindedPathSet *BlindedPaymentPathSet
426
}
427

428
// RouteHints is an alias type for a set of route hints, with the source node
429
// as the map's key and the details of the hint(s) in the edge policy.
430
type RouteHints map[route.Vertex][]AdditionalEdge
431

432
// NewRouteRequest produces a new route request for a regular payment or one
433
// to a blinded route, validating that the target, routeHints and finalExpiry
434
// parameters are mutually exclusive with the blindedPayment parameter (which
435
// contains these values for blinded payments).
436
func NewRouteRequest(source route.Vertex, target *route.Vertex,
437
        amount lnwire.MilliSatoshi, timePref float64,
438
        restrictions *RestrictParams, customRecords record.CustomSet,
439
        routeHints RouteHints, blindedPathSet *BlindedPaymentPathSet,
440
        finalExpiry uint16) (*RouteRequest, error) {
15✔
441

15✔
442
        var (
15✔
443
                // Assume that we're starting off with a regular payment.
15✔
444
                requestHints  = routeHints
15✔
445
                requestExpiry = finalExpiry
15✔
446
                err           error
15✔
447
        )
15✔
448

15✔
449
        if blindedPathSet != nil {
21✔
450
                if blindedPathSet.IsIntroNode(source) {
7✔
451
                        return nil, ErrSelfIntro
1✔
452
                }
1✔
453

454
                // Check that the values for a clear path have not been set,
455
                // as this is an ambiguous signal from the caller.
456
                if routeHints != nil {
6✔
457
                        return nil, ErrHintsAndBlinded
1✔
458
                }
1✔
459

460
                if finalExpiry != 0 {
5✔
461
                        return nil, ErrExpiryAndBlinded
1✔
462
                }
1✔
463

464
                requestExpiry = blindedPathSet.FinalCLTVDelta()
3✔
465

3✔
466
                requestHints, err = blindedPathSet.ToRouteHints()
3✔
467
                if err != nil {
3✔
468
                        return nil, err
×
469
                }
×
470
        }
471

472
        requestTarget, err := getTargetNode(target, blindedPathSet)
12✔
473
        if err != nil {
13✔
474
                return nil, err
1✔
475
        }
1✔
476

477
        return &RouteRequest{
11✔
478
                Source:         source,
11✔
479
                Target:         requestTarget,
11✔
480
                Amount:         amount,
11✔
481
                TimePreference: timePref,
11✔
482
                Restrictions:   restrictions,
11✔
483
                CustomRecords:  customRecords,
11✔
484
                RouteHints:     requestHints,
11✔
485
                FinalExpiry:    requestExpiry,
11✔
486
                BlindedPathSet: blindedPathSet,
11✔
487
        }, nil
11✔
488
}
489

490
func getTargetNode(target *route.Vertex,
491
        blindedPathSet *BlindedPaymentPathSet) (route.Vertex, error) {
12✔
492

12✔
493
        var (
12✔
494
                blinded   = blindedPathSet != nil
12✔
495
                targetSet = target != nil
12✔
496
        )
12✔
497

12✔
498
        switch {
12✔
499
        case blinded && targetSet:
1✔
500
                return route.Vertex{}, ErrTargetAndBlinded
1✔
501

502
        case blinded:
2✔
503
                return route.NewVertex(blindedPathSet.TargetPubKey()), nil
2✔
504

505
        case targetSet:
9✔
506
                return *target, nil
9✔
507

508
        default:
×
509
                return route.Vertex{}, ErrNoTarget
×
510
        }
511
}
512

513
// FindRoute attempts to query the ChannelRouter for the optimum path to a
514
// particular target destination to which it is able to send `amt` after
515
// factoring in channel capacities and cumulative fees along the route.
516
func (r *ChannelRouter) FindRoute(req *RouteRequest) (*route.Route, float64,
517
        error) {
5✔
518

5✔
519
        log.Debugf("Searching for path to %v, sending %v", req.Target,
5✔
520
                req.Amount)
5✔
521

5✔
522
        // We'll attempt to obtain a set of bandwidth hints that can help us
5✔
523
        // eliminate certain routes early on in the path finding process.
5✔
524
        bandwidthHints, err := newBandwidthManager(
5✔
525
                r.cfg.RoutingGraph, r.cfg.SelfNode, r.cfg.GetLink,
5✔
526
                fn.None[tlv.Blob](), r.cfg.TrafficShaper,
5✔
527
        )
5✔
528
        if err != nil {
5✔
529
                return nil, 0, err
×
530
        }
×
531

532
        // We'll fetch the current block height, so we can properly calculate
533
        // the required HTLC time locks within the route.
534
        _, currentHeight, err := r.cfg.Chain.GetBestBlock()
5✔
535
        if err != nil {
5✔
536
                return nil, 0, err
×
537
        }
×
538

539
        // Now that we know the destination is reachable within the graph, we'll
540
        // execute our path finding algorithm.
541
        finalHtlcExpiry := currentHeight + int32(req.FinalExpiry)
5✔
542

5✔
543
        // Validate time preference.
5✔
544
        timePref := req.TimePreference
5✔
545
        if timePref < -1 || timePref > 1 {
5✔
546
                return nil, 0, errors.New("time preference out of range")
×
547
        }
×
548

549
        path, probability, err := findPath(
5✔
550
                &graphParams{
5✔
551
                        additionalEdges: req.RouteHints,
5✔
552
                        bandwidthHints:  bandwidthHints,
5✔
553
                        graph:           r.cfg.RoutingGraph,
5✔
554
                },
5✔
555
                req.Restrictions, &r.cfg.PathFindingConfig,
5✔
556
                r.cfg.SelfNode, req.Source, req.Target, req.Amount,
5✔
557
                req.TimePreference, finalHtlcExpiry,
5✔
558
        )
5✔
559
        if err != nil {
5✔
560
                return nil, 0, err
×
561
        }
×
562

563
        // Create the route with absolute time lock values.
564
        route, err := newRoute(
5✔
565
                req.Source, path, uint32(currentHeight),
5✔
566
                finalHopParams{
5✔
567
                        amt:       req.Amount,
5✔
568
                        totalAmt:  req.Amount,
5✔
569
                        cltvDelta: req.FinalExpiry,
5✔
570
                        records:   req.CustomRecords,
5✔
571
                }, req.BlindedPathSet,
5✔
572
        )
5✔
573
        if err != nil {
5✔
574
                return nil, 0, err
×
575
        }
×
576

577
        go log.Tracef("Obtained path to send %v to %x: %v",
5✔
578
                req.Amount, req.Target, lnutils.SpewLogClosure(route))
5✔
579

5✔
580
        return route, probability, nil
5✔
581
}
582

583
// probabilitySource defines the signature of a function that can be used to
584
// query the success probability of sending a given amount between the two
585
// given vertices.
586
type probabilitySource func(route.Vertex, route.Vertex, lnwire.MilliSatoshi,
587
        btcutil.Amount) float64
588

589
// BlindedPathRestrictions are a set of constraints to adhere to when
590
// choosing a set of blinded paths to this node.
591
type BlindedPathRestrictions struct {
592
        // MinDistanceFromIntroNode is the minimum number of _real_ (non-dummy)
593
        // hops to include in a blinded path. Since we post-fix dummy hops, this
594
        // is the minimum distance between our node and the introduction node
595
        // of the path. This doesn't include our node, so if the minimum is 1,
596
        // then the path will contain at minimum our node along with an
597
        // introduction node hop.
598
        MinDistanceFromIntroNode uint8
599

600
        // NumHops is the number of hops that each blinded path should consist
601
        // of. If paths are found with a number of hops less that NumHops, then
602
        // dummy hops will be padded on to the route. This value doesn't
603
        // include our node, so if the maximum is 1, then the path will contain
604
        // our node along with an introduction node hop.
605
        NumHops uint8
606

607
        // MaxNumPaths is the maximum number of blinded paths to select.
608
        MaxNumPaths uint8
609

610
        // NodeOmissionSet is a set of nodes that should not be used within any
611
        // of the blinded paths that we generate.
612
        NodeOmissionSet fn.Set[route.Vertex]
613
}
614

615
// FindBlindedPaths finds a selection of paths to the destination node that can
616
// be used in blinded payment paths.
617
func (r *ChannelRouter) FindBlindedPaths(destination route.Vertex,
618
        amt lnwire.MilliSatoshi, probabilitySrc probabilitySource,
619
        restrictions *BlindedPathRestrictions) ([]*route.Route, error) {
7✔
620

7✔
621
        // First, find a set of candidate paths given the destination node and
7✔
622
        // path length restrictions.
7✔
623
        paths, err := findBlindedPaths(
7✔
624
                r.cfg.RoutingGraph, destination, &blindedPathRestrictions{
7✔
625
                        minNumHops:      restrictions.MinDistanceFromIntroNode,
7✔
626
                        maxNumHops:      restrictions.NumHops,
7✔
627
                        nodeOmissionSet: restrictions.NodeOmissionSet,
7✔
628
                },
7✔
629
        )
7✔
630
        if err != nil {
7✔
631
                return nil, err
×
632
        }
×
633

634
        // routeWithProbability groups a route with the probability of a
635
        // payment of the given amount succeeding on that path.
636
        type routeWithProbability struct {
7✔
637
                route       *route.Route
7✔
638
                probability float64
7✔
639
        }
7✔
640

7✔
641
        // Iterate over all the candidate paths and determine the success
7✔
642
        // probability of each path given the data we have about forwards
7✔
643
        // between any two nodes on a path.
7✔
644
        routes := make([]*routeWithProbability, 0, len(paths))
7✔
645
        for _, path := range paths {
25✔
646
                if len(path) < 1 {
18✔
647
                        return nil, fmt.Errorf("a blinded path must have at " +
×
648
                                "least one hop")
×
649
                }
×
650

651
                var (
18✔
652
                        introNode = path[0].vertex
18✔
653
                        prevNode  = introNode
18✔
654
                        hops      = make(
18✔
655
                                []*route.Hop, 0, len(path)-1,
18✔
656
                        )
18✔
657
                        totalRouteProbability = float64(1)
18✔
658
                )
18✔
659

18✔
660
                // For each set of hops on the path, get the success probability
18✔
661
                // of a forward between those two vertices and use that to
18✔
662
                // update the overall route probability.
18✔
663
                for j := 1; j < len(path); j++ {
52✔
664
                        probability := probabilitySrc(
34✔
665
                                prevNode, path[j].vertex, amt,
34✔
666
                                path[j-1].edgeCapacity,
34✔
667
                        )
34✔
668

34✔
669
                        totalRouteProbability *= probability
34✔
670

34✔
671
                        hops = append(hops, &route.Hop{
34✔
672
                                PubKeyBytes: path[j].vertex,
34✔
673
                                ChannelID:   path[j-1].channelID,
34✔
674
                        })
34✔
675

34✔
676
                        prevNode = path[j].vertex
34✔
677
                }
34✔
678

679
                // Don't bother adding a route if its success probability less
680
                // minimum that can be assigned to any single pair.
681
                if totalRouteProbability <= DefaultMinRouteProbability {
20✔
682
                        continue
2✔
683
                }
684

685
                routes = append(routes, &routeWithProbability{
16✔
686
                        route: &route.Route{
16✔
687
                                SourcePubKey: introNode,
16✔
688
                                Hops:         hops,
16✔
689
                        },
16✔
690
                        probability: totalRouteProbability,
16✔
691
                })
16✔
692
        }
693

694
        // Sort the routes based on probability.
695
        sort.Slice(routes, func(i, j int) bool {
18✔
696
                return routes[i].probability > routes[j].probability
11✔
697
        })
11✔
698

699
        // Now just choose the best paths up until the maximum number of allowed
700
        // paths.
701
        bestRoutes := make([]*route.Route, 0, restrictions.MaxNumPaths)
7✔
702
        for _, route := range routes {
22✔
703
                if len(bestRoutes) >= int(restrictions.MaxNumPaths) {
16✔
704
                        break
1✔
705
                }
706

707
                bestRoutes = append(bestRoutes, route.route)
14✔
708
        }
709

710
        return bestRoutes, nil
7✔
711
}
712

713
// generateNewSessionKey generates a new ephemeral private key to be used for a
714
// payment attempt.
715
func generateNewSessionKey() (*btcec.PrivateKey, error) {
36✔
716
        // Generate a new random session key to ensure that we don't trigger
36✔
717
        // any replay.
36✔
718
        //
36✔
719
        // TODO(roasbeef): add more sources of randomness?
36✔
720
        return btcec.NewPrivateKey()
36✔
721
}
36✔
722

723
// LightningPayment describes a payment to be sent through the network to the
724
// final destination.
725
type LightningPayment struct {
726
        // Target is the node in which the payment should be routed towards.
727
        Target route.Vertex
728

729
        // Amount is the value of the payment to send through the network in
730
        // milli-satoshis.
731
        Amount lnwire.MilliSatoshi
732

733
        // FeeLimit is the maximum fee in millisatoshis that the payment should
734
        // accept when sending it through the network. The payment will fail
735
        // if there isn't a route with lower fees than this limit.
736
        FeeLimit lnwire.MilliSatoshi
737

738
        // CltvLimit is the maximum time lock that is allowed for attempts to
739
        // complete this payment.
740
        CltvLimit uint32
741

742
        // paymentHash is the r-hash value to use within the HTLC extended to
743
        // the first hop. This won't be set for AMP payments.
744
        paymentHash *lntypes.Hash
745

746
        // amp is an optional field that is set if and only if this is am AMP
747
        // payment.
748
        amp *AMPOptions
749

750
        // FinalCLTVDelta is the CTLV expiry delta to use for the _final_ hop
751
        // in the route. This means that the final hop will have a CLTV delta
752
        // of at least: currentHeight + FinalCLTVDelta.
753
        FinalCLTVDelta uint16
754

755
        // PayAttemptTimeout is a timeout value that we'll use to determine
756
        // when we should should abandon the payment attempt after consecutive
757
        // payment failure. This prevents us from attempting to send a payment
758
        // indefinitely. A zero value means the payment will never time out.
759
        //
760
        // TODO(halseth): make wallclock time to allow resume after startup.
761
        PayAttemptTimeout time.Duration
762

763
        // RouteHints represents the different routing hints that can be used to
764
        // assist a payment in reaching its destination successfully. These
765
        // hints will act as intermediate hops along the route.
766
        //
767
        // NOTE: This is optional unless required by the payment. When providing
768
        // multiple routes, ensure the hop hints within each route are chained
769
        // together and sorted in forward order in order to reach the
770
        // destination successfully. This is mutually exclusive to the
771
        // BlindedPayment field.
772
        RouteHints [][]zpay32.HopHint
773

774
        // BlindedPathSet holds the information about a set of blinded paths to
775
        // the payment recipient. This is mutually exclusive to the RouteHints
776
        // field.
777
        BlindedPathSet *BlindedPaymentPathSet
778

779
        // OutgoingChannelIDs is the list of channels that are allowed for the
780
        // first hop. If nil, any channel may be used.
781
        OutgoingChannelIDs []uint64
782

783
        // LastHop is the pubkey of the last node before the final destination
784
        // is reached. If nil, any node may be used.
785
        LastHop *route.Vertex
786

787
        // DestFeatures specifies the set of features we assume the final node
788
        // has for pathfinding. Typically, these will be taken directly from an
789
        // invoice, but they can also be manually supplied or assumed by the
790
        // sender. If a nil feature vector is provided, the router will try to
791
        // fall back to the graph in order to load a feature vector for a node
792
        // in the public graph.
793
        DestFeatures *lnwire.FeatureVector
794

795
        // PaymentAddr is the payment address specified by the receiver. This
796
        // field should be a random 32-byte nonce presented in the receiver's
797
        // invoice to prevent probing of the destination.
798
        PaymentAddr fn.Option[[32]byte]
799

800
        // PaymentRequest is an optional payment request that this payment is
801
        // attempting to complete.
802
        PaymentRequest []byte
803

804
        // DestCustomRecords are TLV records that are to be sent to the final
805
        // hop in the new onion payload format. If the destination does not
806
        // understand this new onion payload format, then the payment will
807
        // fail.
808
        DestCustomRecords record.CustomSet
809

810
        // FirstHopCustomRecords are the TLV records that are to be sent to the
811
        // first hop of this payment. These records will be transmitted via the
812
        // wire message and therefore do not affect the onion payload size.
813
        FirstHopCustomRecords lnwire.CustomRecords
814

815
        // MaxParts is the maximum number of partial payments that may be used
816
        // to complete the full amount.
817
        MaxParts uint32
818

819
        // MaxShardAmt is the largest shard that we'll attempt to split using.
820
        // If this field is set, and we need to split, rather than attempting
821
        // half of the original payment amount, we'll use this value if half
822
        // the payment amount is greater than it.
823
        //
824
        // NOTE: This field is _optional_.
825
        MaxShardAmt *lnwire.MilliSatoshi
826

827
        // TimePref is the time preference for this payment. Set to -1 to
828
        // optimize for fees only, to 1 to optimize for reliability only or a
829
        // value in between for a mix.
830
        TimePref float64
831

832
        // Metadata is additional data that is sent along with the payment to
833
        // the payee.
834
        Metadata []byte
835
}
836

837
// AMPOptions houses information that must be known in order to send an AMP
838
// payment.
839
type AMPOptions struct {
840
        SetID     [32]byte
841
        RootShare [32]byte
842
}
843

844
// SetPaymentHash sets the given hash as the payment's overall hash. This
845
// should only be used for non-AMP payments.
846
func (l *LightningPayment) SetPaymentHash(hash lntypes.Hash) error {
10✔
847
        if l.amp != nil {
10✔
848
                return fmt.Errorf("cannot set payment hash for AMP payment")
×
849
        }
×
850

851
        l.paymentHash = &hash
10✔
852
        return nil
10✔
853
}
854

855
// SetAMP sets the given AMP options for the payment.
856
func (l *LightningPayment) SetAMP(amp *AMPOptions) error {
×
857
        if l.paymentHash != nil {
×
858
                return fmt.Errorf("cannot set amp options for payment " +
×
859
                        "with payment hash")
×
860
        }
×
861

862
        l.amp = amp
×
863
        return nil
×
864
}
865

866
// Identifier returns a 32-byte slice that uniquely identifies this single
867
// payment. For non-AMP payments this will be the payment hash, for AMP
868
// payments this will be the used SetID.
869
func (l *LightningPayment) Identifier() [32]byte {
71✔
870
        if l.amp != nil {
71✔
871
                return l.amp.SetID
×
872
        }
×
873

874
        return *l.paymentHash
71✔
875
}
876

877
// SendPayment attempts to send a payment as described within the passed
878
// LightningPayment. This function is blocking and will return either: when the
879
// payment is successful, or all candidates routes have been attempted and
880
// resulted in a failed payment. If the payment succeeds, then a non-nil Route
881
// will be returned which describes the path the successful payment traversed
882
// within the network to reach the destination. Additionally, the payment
883
// preimage will also be returned.
884
func (r *ChannelRouter) SendPayment(payment *LightningPayment) ([32]byte,
885
        *route.Route, error) {
12✔
886

12✔
887
        paySession, shardTracker, err := r.PreparePayment(payment)
12✔
888
        if err != nil {
12✔
889
                return [32]byte{}, nil, err
×
890
        }
×
891

892
        log.Tracef("Dispatching SendPayment for lightning payment: %v",
12✔
893
                spewPayment(payment))
12✔
894

12✔
895
        return r.sendPayment(
12✔
896
                context.Background(), payment.FeeLimit, payment.Identifier(),
12✔
897
                payment.PayAttemptTimeout, paySession, shardTracker,
12✔
898
                payment.FirstHopCustomRecords,
12✔
899
        )
12✔
900
}
901

902
// SendPaymentAsync is the non-blocking version of SendPayment. The payment
903
// result needs to be retrieved via the control tower.
904
func (r *ChannelRouter) SendPaymentAsync(ctx context.Context,
905
        payment *LightningPayment, ps PaymentSession, st shards.ShardTracker) {
×
906

×
907
        // Since this is the first time this payment is being made, we pass nil
×
908
        // for the existing attempt.
×
909
        r.wg.Add(1)
×
910
        go func() {
×
911
                defer r.wg.Done()
×
912

×
913
                log.Tracef("Dispatching SendPayment for lightning payment: %v",
×
914
                        spewPayment(payment))
×
915

×
916
                _, _, err := r.sendPayment(
×
917
                        ctx, payment.FeeLimit, payment.Identifier(),
×
918
                        payment.PayAttemptTimeout, ps, st,
×
919
                        payment.FirstHopCustomRecords,
×
920
                )
×
921
                if err != nil {
×
922
                        log.Errorf("Payment %x failed: %v",
×
923
                                payment.Identifier(), err)
×
924
                }
×
925
        }()
926
}
927

928
// spewPayment returns a log closures that provides a spewed string
929
// representation of the passed payment.
930
func spewPayment(payment *LightningPayment) lnutils.LogClosure {
12✔
931
        return lnutils.NewLogClosure(func() string {
12✔
932
                // Make a copy of the payment with a nilled Curve
×
933
                // before spewing.
×
934
                var routeHints [][]zpay32.HopHint
×
935
                for _, routeHint := range payment.RouteHints {
×
936
                        var hopHints []zpay32.HopHint
×
937
                        for _, hopHint := range routeHint {
×
938
                                h := hopHint.Copy()
×
939
                                hopHints = append(hopHints, h)
×
940
                        }
×
941
                        routeHints = append(routeHints, hopHints)
×
942
                }
943
                p := *payment
×
944
                p.RouteHints = routeHints
×
945
                return spew.Sdump(p)
×
946
        })
947
}
948

949
// PreparePayment creates the payment session and registers the payment with the
950
// control tower.
951
func (r *ChannelRouter) PreparePayment(payment *LightningPayment) (
952
        PaymentSession, shards.ShardTracker, error) {
12✔
953

12✔
954
        // Assemble any custom data we want to send to the first hop only.
12✔
955
        var firstHopData fn.Option[tlv.Blob]
12✔
956
        if len(payment.FirstHopCustomRecords) > 0 {
12✔
957
                if err := payment.FirstHopCustomRecords.Validate(); err != nil {
×
958
                        return nil, nil, fmt.Errorf("invalid first hop custom "+
×
959
                                "records: %w", err)
×
960
                }
×
961

962
                firstHopBlob, err := payment.FirstHopCustomRecords.Serialize()
×
963
                if err != nil {
×
964
                        return nil, nil, fmt.Errorf("unable to serialize "+
×
965
                                "first hop custom records: %w", err)
×
966
                }
×
967

968
                firstHopData = fn.Some(firstHopBlob)
×
969
        }
970

971
        // Before starting the HTLC routing attempt, we'll create a fresh
972
        // payment session which will report our errors back to mission
973
        // control.
974
        paySession, err := r.cfg.SessionSource.NewPaymentSession(
12✔
975
                payment, firstHopData, r.cfg.TrafficShaper,
12✔
976
        )
12✔
977
        if err != nil {
12✔
978
                return nil, nil, err
×
979
        }
×
980

981
        // Record this payment hash with the ControlTower, ensuring it is not
982
        // already in-flight.
983
        //
984
        // TODO(roasbeef): store records as part of creation info?
985
        info := &channeldb.PaymentCreationInfo{
12✔
986
                PaymentIdentifier:     payment.Identifier(),
12✔
987
                Value:                 payment.Amount,
12✔
988
                CreationTime:          r.cfg.Clock.Now(),
12✔
989
                PaymentRequest:        payment.PaymentRequest,
12✔
990
                FirstHopCustomRecords: payment.FirstHopCustomRecords,
12✔
991
        }
12✔
992

12✔
993
        // Create a new ShardTracker that we'll use during the life cycle of
12✔
994
        // this payment.
12✔
995
        var shardTracker shards.ShardTracker
12✔
996
        switch {
12✔
997
        // If this is an AMP payment, we'll use the AMP shard tracker.
998
        case payment.amp != nil:
×
999
                addr := payment.PaymentAddr.UnwrapOr([32]byte{})
×
1000
                shardTracker = amp.NewShardTracker(
×
1001
                        payment.amp.RootShare, payment.amp.SetID, addr,
×
1002
                        payment.Amount,
×
1003
                )
×
1004

1005
        // Otherwise we'll use the simple tracker that will map each attempt to
1006
        // the same payment hash.
1007
        default:
12✔
1008
                shardTracker = shards.NewSimpleShardTracker(
12✔
1009
                        payment.Identifier(), nil,
12✔
1010
                )
12✔
1011
        }
1012

1013
        err = r.cfg.Control.InitPayment(payment.Identifier(), info)
12✔
1014
        if err != nil {
12✔
1015
                return nil, nil, err
×
1016
        }
×
1017

1018
        return paySession, shardTracker, nil
12✔
1019
}
1020

1021
// SendToRoute sends a payment using the provided route and fails the payment
1022
// when an error is returned from the attempt.
1023
func (r *ChannelRouter) SendToRoute(htlcHash lntypes.Hash, rt *route.Route,
1024
        firstHopCustomRecords lnwire.CustomRecords) (*channeldb.HTLCAttempt,
1025
        error) {
6✔
1026

6✔
1027
        return r.sendToRoute(htlcHash, rt, false, firstHopCustomRecords)
6✔
1028
}
6✔
1029

1030
// SendToRouteSkipTempErr sends a payment using the provided route and fails
1031
// the payment ONLY when a terminal error is returned from the attempt.
1032
func (r *ChannelRouter) SendToRouteSkipTempErr(htlcHash lntypes.Hash,
1033
        rt *route.Route,
1034
        firstHopCustomRecords lnwire.CustomRecords) (*channeldb.HTLCAttempt,
1035
        error) {
4✔
1036

4✔
1037
        return r.sendToRoute(htlcHash, rt, true, firstHopCustomRecords)
4✔
1038
}
4✔
1039

1040
// sendToRoute attempts to send a payment with the given hash through the
1041
// provided route. This function is blocking and will return the attempt
1042
// information as it is stored in the database. For a successful htlc, this
1043
// information will contain the preimage. If an error occurs after the attempt
1044
// was initiated, both return values will be non-nil. If skipTempErr is true,
1045
// the payment won't be failed unless a terminal error has occurred.
1046
func (r *ChannelRouter) sendToRoute(htlcHash lntypes.Hash, rt *route.Route,
1047
        skipTempErr bool,
1048
        firstHopCustomRecords lnwire.CustomRecords) (*channeldb.HTLCAttempt,
1049
        error) {
10✔
1050

10✔
1051
        // Helper function to fail a payment. It makes sure the payment is only
10✔
1052
        // failed once so that the failure reason is not overwritten.
10✔
1053
        failPayment := func(paymentIdentifier lntypes.Hash,
10✔
1054
                reason channeldb.FailureReason) error {
15✔
1055

5✔
1056
                payment, fetchErr := r.cfg.Control.FetchPayment(
5✔
1057
                        paymentIdentifier,
5✔
1058
                )
5✔
1059
                if fetchErr != nil {
5✔
1060
                        return fetchErr
×
1061
                }
×
1062

1063
                // NOTE: We cannot rely on the payment status to be failed here
1064
                // because it can still be in-flight although the payment is
1065
                // already failed.
1066
                _, failedReason := payment.TerminalInfo()
5✔
1067
                if failedReason != nil {
6✔
1068
                        return nil
1✔
1069
                }
1✔
1070

1071
                return r.cfg.Control.FailPayment(paymentIdentifier, reason)
4✔
1072
        }
1073

1074
        log.Debugf("SendToRoute for payment %v with skipTempErr=%v",
10✔
1075
                htlcHash, skipTempErr)
10✔
1076

10✔
1077
        // Calculate amount paid to receiver.
10✔
1078
        amt := rt.ReceiverAmt()
10✔
1079

10✔
1080
        // If this is meant as an MP payment shard, we set the amount for the
10✔
1081
        // creating info to the total amount of the payment.
10✔
1082
        finalHop := rt.Hops[len(rt.Hops)-1]
10✔
1083
        mpp := finalHop.MPP
10✔
1084
        if mpp != nil {
14✔
1085
                amt = mpp.TotalMsat()
4✔
1086
        }
4✔
1087

1088
        // For non-MPP, there's no such thing as temp error as there's only one
1089
        // HTLC attempt being made. When this HTLC is failed, the payment is
1090
        // failed hence cannot be retried.
1091
        if skipTempErr && mpp == nil {
11✔
1092
                return nil, ErrSkipTempErr
1✔
1093
        }
1✔
1094

1095
        // For non-AMP payments the overall payment identifier will be the same
1096
        // hash as used for this HTLC.
1097
        paymentIdentifier := htlcHash
9✔
1098

9✔
1099
        // For AMP-payments, we'll use the setID as the unique ID for the
9✔
1100
        // overall payment.
9✔
1101
        amp := finalHop.AMP
9✔
1102
        if amp != nil {
9✔
1103
                paymentIdentifier = amp.SetID()
×
1104
        }
×
1105

1106
        // Record this payment hash with the ControlTower, ensuring it is not
1107
        // already in-flight.
1108
        info := &channeldb.PaymentCreationInfo{
9✔
1109
                PaymentIdentifier:     paymentIdentifier,
9✔
1110
                Value:                 amt,
9✔
1111
                CreationTime:          r.cfg.Clock.Now(),
9✔
1112
                PaymentRequest:        nil,
9✔
1113
                FirstHopCustomRecords: firstHopCustomRecords,
9✔
1114
        }
9✔
1115

9✔
1116
        err := r.cfg.Control.InitPayment(paymentIdentifier, info)
9✔
1117
        switch {
9✔
1118
        // If this is an MPP attempt and the hash is already registered with
1119
        // the database, we can go on to launch the shard.
1120
        case mpp != nil && errors.Is(err, channeldb.ErrPaymentInFlight):
×
1121
        case mpp != nil && errors.Is(err, channeldb.ErrPaymentExists):
×
1122

1123
        // Any other error is not tolerated.
1124
        case err != nil:
×
1125
                return nil, err
×
1126
        }
1127

1128
        log.Tracef("Dispatching SendToRoute for HTLC hash %v: %v", htlcHash,
9✔
1129
                lnutils.SpewLogClosure(rt))
9✔
1130

9✔
1131
        // Since the HTLC hashes and preimages are specified manually over the
9✔
1132
        // RPC for SendToRoute requests, we don't have to worry about creating
9✔
1133
        // a ShardTracker that can generate hashes for AMP payments. Instead, we
9✔
1134
        // create a simple tracker that can just return the hash for the single
9✔
1135
        // shard we'll now launch.
9✔
1136
        shardTracker := shards.NewSimpleShardTracker(htlcHash, nil)
9✔
1137

9✔
1138
        // Create a payment lifecycle using the given route with,
9✔
1139
        // - zero fee limit as we are not requesting routes.
9✔
1140
        // - nil payment session (since we already have a route).
9✔
1141
        // - no payment timeout.
9✔
1142
        // - no current block height.
9✔
1143
        p := newPaymentLifecycle(
9✔
1144
                r, 0, paymentIdentifier, nil, shardTracker, 0,
9✔
1145
                firstHopCustomRecords,
9✔
1146
        )
9✔
1147

9✔
1148
        // Allow the traffic shaper to add custom records to the outgoing HTLC
9✔
1149
        // and also adjust the amount if needed.
9✔
1150
        err = p.amendFirstHopData(rt)
9✔
1151
        if err != nil {
9✔
1152
                return nil, err
×
1153
        }
×
1154

1155
        // We found a route to try, create a new HTLC attempt to try.
1156
        //
1157
        // NOTE: we use zero `remainingAmt` here to simulate the same effect of
1158
        // setting the lastShard to be false, which is used by previous
1159
        // implementation.
1160
        attempt, err := p.registerAttempt(rt, 0)
9✔
1161
        if err != nil {
10✔
1162
                return nil, err
1✔
1163
        }
1✔
1164

1165
        // Once the attempt is created, send it to the htlcswitch. Notice that
1166
        // the `err` returned here has already been processed by
1167
        // `handleSwitchErr`, which means if there's a terminal failure, the
1168
        // payment has been failed.
1169
        result, err := p.sendAttempt(attempt)
8✔
1170
        if err != nil {
8✔
1171
                return nil, err
×
1172
        }
×
1173

1174
        // Since for SendToRoute we won't retry in case the shard fails, we'll
1175
        // mark the payment failed with the control tower immediately if the
1176
        // skipTempErr is false.
1177
        reason := channeldb.FailureReasonError
8✔
1178

8✔
1179
        // If we failed to send the HTLC, we need to further decide if we want
8✔
1180
        // to fail the payment.
8✔
1181
        if result.err != nil {
11✔
1182
                // If skipTempErr, we'll return the attempt and the temp error.
3✔
1183
                if skipTempErr {
5✔
1184
                        return result.attempt, result.err
2✔
1185
                }
2✔
1186

1187
                err := failPayment(paymentIdentifier, reason)
1✔
1188
                if err != nil {
1✔
1189
                        return nil, err
×
1190
                }
×
1191

1192
                return result.attempt, result.err
1✔
1193
        }
1194

1195
        // The attempt was successfully sent, wait for the result to be
1196
        // available.
1197
        result, err = p.collectAndHandleResult(attempt)
5✔
1198
        if err != nil {
5✔
1199
                return nil, err
×
1200
        }
×
1201

1202
        // We got a successful result.
1203
        if result.err == nil {
6✔
1204
                return result.attempt, nil
1✔
1205
        }
1✔
1206

1207
        // An error returned from collecting the result, we'll mark the payment
1208
        // as failed if we don't skip temp error.
1209
        if !skipTempErr {
8✔
1210
                err := failPayment(paymentIdentifier, reason)
4✔
1211
                if err != nil {
4✔
1212
                        return nil, err
×
1213
                }
×
1214
        }
1215

1216
        return result.attempt, result.err
4✔
1217
}
1218

1219
// sendPayment attempts to send a payment to the passed payment hash. This
1220
// function is blocking and will return either: when the payment is successful,
1221
// or all candidates routes have been attempted and resulted in a failed
1222
// payment. If the payment succeeds, then a non-nil Route will be returned
1223
// which describes the path the successful payment traversed within the network
1224
// to reach the destination. Additionally, the payment preimage will also be
1225
// returned.
1226
//
1227
// This method relies on the ControlTower's internal payment state machine to
1228
// carry out its execution. After restarts, it is safe, and assumed, that the
1229
// router will call this method for every payment still in-flight according to
1230
// the ControlTower.
1231
func (r *ChannelRouter) sendPayment(ctx context.Context,
1232
        feeLimit lnwire.MilliSatoshi, identifier lntypes.Hash,
1233
        paymentAttemptTimeout time.Duration, paySession PaymentSession,
1234
        shardTracker shards.ShardTracker,
1235
        firstHopCustomRecords lnwire.CustomRecords) ([32]byte, *route.Route,
1236
        error) {
12✔
1237

12✔
1238
        // If the user provides a timeout, we will additionally wrap the context
12✔
1239
        // in a deadline.
12✔
1240
        cancel := func() {}
24✔
1241
        if paymentAttemptTimeout > 0 {
12✔
1242
                ctx, cancel = context.WithTimeout(ctx, paymentAttemptTimeout)
×
1243
        }
×
1244

1245
        // Since resumePayment is a blocking call, we'll cancel this
1246
        // context if the payment completes before the optional
1247
        // deadline.
1248
        defer cancel()
12✔
1249

12✔
1250
        // We'll also fetch the current block height, so we can properly
12✔
1251
        // calculate the required HTLC time locks within the route.
12✔
1252
        _, currentHeight, err := r.cfg.Chain.GetBestBlock()
12✔
1253
        if err != nil {
12✔
1254
                return [32]byte{}, nil, err
×
1255
        }
×
1256

1257
        // Validate the custom records before we attempt to send the payment.
1258
        // TODO(ziggie): Move this check before registering the payment in the
1259
        // db (InitPayment).
1260
        if err := firstHopCustomRecords.Validate(); err != nil {
12✔
1261
                return [32]byte{}, nil, err
×
1262
        }
×
1263

1264
        // Now set up a paymentLifecycle struct with these params, such that we
1265
        // can resume the payment from the current state.
1266
        p := newPaymentLifecycle(
12✔
1267
                r, feeLimit, identifier, paySession, shardTracker,
12✔
1268
                currentHeight, firstHopCustomRecords,
12✔
1269
        )
12✔
1270

12✔
1271
        return p.resumePayment(ctx)
12✔
1272
}
1273

1274
// extractChannelUpdate examines the error and extracts the channel update.
1275
func (r *ChannelRouter) extractChannelUpdate(
1276
        failure lnwire.FailureMessage) *lnwire.ChannelUpdate1 {
17✔
1277

17✔
1278
        var update *lnwire.ChannelUpdate1
17✔
1279
        switch onionErr := failure.(type) {
17✔
1280
        case *lnwire.FailExpiryTooSoon:
1✔
1281
                update = &onionErr.Update
1✔
1282
        case *lnwire.FailAmountBelowMinimum:
×
1283
                update = &onionErr.Update
×
1284
        case *lnwire.FailFeeInsufficient:
7✔
1285
                update = &onionErr.Update
7✔
1286
        case *lnwire.FailIncorrectCltvExpiry:
×
1287
                update = &onionErr.Update
×
1288
        case *lnwire.FailChannelDisabled:
×
1289
                update = &onionErr.Update
×
1290
        case *lnwire.FailTemporaryChannelFailure:
5✔
1291
                update = onionErr.Update
5✔
1292
        }
1293

1294
        return update
17✔
1295
}
1296

1297
// ErrNoChannel is returned when a route cannot be built because there are no
1298
// channels that satisfy all requirements.
1299
type ErrNoChannel struct {
1300
        position int
1301
}
1302

1303
// Error returns a human-readable string describing the error.
1304
func (e ErrNoChannel) Error() string {
1✔
1305
        return fmt.Sprintf("no matching outgoing channel available for "+
1✔
1306
                "node index %v", e.position)
1✔
1307
}
1✔
1308

1309
// BuildRoute returns a fully specified route based on a list of pubkeys. If
1310
// amount is nil, the minimum routable amount is used. To force a specific
1311
// outgoing channel, use the outgoingChan parameter.
1312
func (r *ChannelRouter) BuildRoute(amt fn.Option[lnwire.MilliSatoshi],
1313
        hops []route.Vertex, outgoingChan *uint64, finalCltvDelta int32,
1314
        payAddr fn.Option[[32]byte], firstHopBlob fn.Option[[]byte]) (
1315
        *route.Route, error) {
8✔
1316

8✔
1317
        log.Tracef("BuildRoute called: hopsCount=%v, amt=%v", len(hops), amt)
8✔
1318

8✔
1319
        var outgoingChans map[uint64]struct{}
8✔
1320
        if outgoingChan != nil {
8✔
1321
                outgoingChans = map[uint64]struct{}{
×
1322
                        *outgoingChan: {},
×
1323
                }
×
1324
        }
×
1325

1326
        // We'll attempt to obtain a set of bandwidth hints that helps us select
1327
        // the best outgoing channel to use in case no outgoing channel is set.
1328
        bandwidthHints, err := newBandwidthManager(
8✔
1329
                r.cfg.RoutingGraph, r.cfg.SelfNode, r.cfg.GetLink, firstHopBlob,
8✔
1330
                r.cfg.TrafficShaper,
8✔
1331
        )
8✔
1332
        if err != nil {
8✔
1333
                return nil, err
×
1334
        }
×
1335

1336
        sourceNode := r.cfg.SelfNode
8✔
1337

8✔
1338
        // We check that each node in the route has a connection to others that
8✔
1339
        // can forward in principle.
8✔
1340
        unifiers, err := getEdgeUnifiers(
8✔
1341
                r.cfg.SelfNode, hops, outgoingChans, r.cfg.RoutingGraph,
8✔
1342
        )
8✔
1343
        if err != nil {
9✔
1344
                return nil, err
1✔
1345
        }
1✔
1346

1347
        var (
7✔
1348
                receiverAmt lnwire.MilliSatoshi
7✔
1349
                senderAmt   lnwire.MilliSatoshi
7✔
1350
                pathEdges   []*unifiedEdge
7✔
1351
        )
7✔
1352

7✔
1353
        // We determine the edges compatible with the requested amount, as well
7✔
1354
        // as the amount to send, which can be used to determine the final
7✔
1355
        // receiver amount, if a minimal amount was requested.
7✔
1356
        pathEdges, senderAmt, err = senderAmtBackwardPass(
7✔
1357
                unifiers, amt, bandwidthHints,
7✔
1358
        )
7✔
1359
        if err != nil {
9✔
1360
                return nil, err
2✔
1361
        }
2✔
1362

1363
        // For the minimal amount search, we need to do a forward pass to find a
1364
        // larger receiver amount due to possible min HTLC bumps, otherwise we
1365
        // just use the requested amount.
1366
        receiverAmt, err = fn.ElimOption(
5✔
1367
                amt,
5✔
1368
                func() fn.Result[lnwire.MilliSatoshi] {
8✔
1369
                        return fn.NewResult(
3✔
1370
                                receiverAmtForwardPass(senderAmt, pathEdges),
3✔
1371
                        )
3✔
1372
                },
3✔
1373
                fn.Ok[lnwire.MilliSatoshi],
1374
        ).Unpack()
1375
        if err != nil {
5✔
1376
                return nil, err
×
1377
        }
×
1378

1379
        // Fetch the current block height outside the routing transaction, to
1380
        // prevent the rpc call blocking the database.
1381
        _, height, err := r.cfg.Chain.GetBestBlock()
5✔
1382
        if err != nil {
5✔
1383
                return nil, err
×
1384
        }
×
1385

1386
        // Build and return the final route.
1387
        return newRoute(
5✔
1388
                sourceNode, pathEdges, uint32(height),
5✔
1389
                finalHopParams{
5✔
1390
                        amt:         receiverAmt,
5✔
1391
                        totalAmt:    receiverAmt,
5✔
1392
                        cltvDelta:   uint16(finalCltvDelta),
5✔
1393
                        records:     nil,
5✔
1394
                        paymentAddr: payAddr,
5✔
1395
                }, nil,
5✔
1396
        )
5✔
1397
}
1398

1399
// resumePayments fetches inflight payments and resumes their payment
1400
// lifecycles.
1401
func (r *ChannelRouter) resumePayments() error {
17✔
1402
        // Get all payments that are inflight.
17✔
1403
        payments, err := r.cfg.Control.FetchInFlightPayments()
17✔
1404
        if err != nil {
17✔
1405
                return err
×
1406
        }
×
1407

1408
        // Before we restart existing payments and start accepting more
1409
        // payments to be made, we clean the network result store of the
1410
        // Switch. We do this here at startup to ensure no more payments can be
1411
        // made concurrently, so we know the toKeep map will be up-to-date
1412
        // until the cleaning has finished.
1413
        toKeep := make(map[uint64]struct{})
17✔
1414
        for _, p := range payments {
17✔
1415
                for _, a := range p.HTLCs {
×
1416
                        toKeep[a.AttemptID] = struct{}{}
×
1417

×
1418
                        // Try to fail the attempt if the route contains a dead
×
1419
                        // channel.
×
1420
                        r.failStaleAttempt(a, p.Info.PaymentIdentifier)
×
1421
                }
×
1422
        }
1423

1424
        log.Debugf("Cleaning network result store.")
17✔
1425
        if err := r.cfg.Payer.CleanStore(toKeep); err != nil {
17✔
1426
                return err
×
1427
        }
×
1428

1429
        // launchPayment is a helper closure that handles resuming the payment.
1430
        launchPayment := func(payment *channeldb.MPPayment) {
17✔
1431
                defer r.wg.Done()
×
1432

×
1433
                // Get the hashes used for the outstanding HTLCs.
×
1434
                htlcs := make(map[uint64]lntypes.Hash)
×
1435
                for _, a := range payment.HTLCs {
×
1436
                        a := a
×
1437

×
1438
                        // We check whether the individual attempts have their
×
1439
                        // HTLC hash set, if not we'll fall back to the overall
×
1440
                        // payment hash.
×
1441
                        hash := payment.Info.PaymentIdentifier
×
1442
                        if a.Hash != nil {
×
1443
                                hash = *a.Hash
×
1444
                        }
×
1445

1446
                        htlcs[a.AttemptID] = hash
×
1447
                }
1448

1449
                payHash := payment.Info.PaymentIdentifier
×
1450

×
1451
                // Since we are not supporting creating more shards after a
×
1452
                // restart (only receiving the result of the shards already
×
1453
                // outstanding), we create a simple shard tracker that will map
×
1454
                // the attempt IDs to hashes used for the HTLCs. This will be
×
1455
                // enough also for AMP payments, since we only need the hashes
×
1456
                // for the individual HTLCs to regenerate the circuits, and we
×
1457
                // don't currently persist the root share necessary to
×
1458
                // re-derive them.
×
1459
                shardTracker := shards.NewSimpleShardTracker(payHash, htlcs)
×
1460

×
1461
                // We create a dummy, empty payment session such that we won't
×
1462
                // make another payment attempt when the result for the
×
1463
                // in-flight attempt is received.
×
1464
                paySession := r.cfg.SessionSource.NewPaymentSessionEmpty()
×
1465

×
1466
                // We pass in a non-timeout context, to indicate we don't need
×
1467
                // it to timeout. It will stop immediately after the existing
×
1468
                // attempt has finished anyway. We also set a zero fee limit,
×
1469
                // as no more routes should be tried.
×
1470
                noTimeout := time.Duration(0)
×
1471
                _, _, err := r.sendPayment(
×
1472
                        context.Background(), 0, payHash, noTimeout, paySession,
×
1473
                        shardTracker, payment.Info.FirstHopCustomRecords,
×
1474
                )
×
1475
                if err != nil {
×
1476
                        log.Errorf("Resuming payment %v failed: %v", payHash,
×
1477
                                err)
×
1478

×
1479
                        return
×
1480
                }
×
1481

1482
                log.Infof("Resumed payment %v completed", payHash)
×
1483
        }
1484

1485
        for _, payment := range payments {
17✔
1486
                log.Infof("Resuming payment %v", payment.Info.PaymentIdentifier)
×
1487

×
1488
                r.wg.Add(1)
×
1489
                go launchPayment(payment)
×
1490
        }
×
1491

1492
        return nil
17✔
1493
}
1494

1495
// failStaleAttempt will fail an HTLC attempt if it's using an unknown channel
1496
// in its route. It first consults the switch to see if there's already a
1497
// network result stored for this attempt. If not, it will check whether the
1498
// first hop of this attempt is using an active channel known to us. If
1499
// inactive, this attempt will be failed.
1500
//
1501
// NOTE: there's an unknown bug that caused the network result for a particular
1502
// attempt to NOT be saved, resulting a payment being stuck forever. More info:
1503
// - https://github.com/lightningnetwork/lnd/issues/8146
1504
// - https://github.com/lightningnetwork/lnd/pull/8174
1505
func (r *ChannelRouter) failStaleAttempt(a channeldb.HTLCAttempt,
1506
        payHash lntypes.Hash) {
×
1507

×
1508
        // We can only fail inflight HTLCs so we skip the settled/failed ones.
×
1509
        if a.Failure != nil || a.Settle != nil {
×
1510
                return
×
1511
        }
×
1512

1513
        // First, check if we've already had a network result for this attempt.
1514
        // If no result is found, we'll check whether the reference link is
1515
        // still known to us.
1516
        ok, err := r.cfg.Payer.HasAttemptResult(a.AttemptID)
×
1517
        if err != nil {
×
1518
                log.Errorf("Failed to fetch network result for attempt=%v",
×
1519
                        a.AttemptID)
×
1520
                return
×
1521
        }
×
1522

1523
        // There's already a network result, no need to fail it here as the
1524
        // payment lifecycle will take care of it, so we can exit early.
1525
        if ok {
×
1526
                log.Debugf("Already have network result for attempt=%v",
×
1527
                        a.AttemptID)
×
1528
                return
×
1529
        }
×
1530

1531
        // We now need to decide whether this attempt should be failed here.
1532
        // For very old payments, it's possible that the network results were
1533
        // never saved, causing the payments to be stuck inflight. We now check
1534
        // whether the first hop is referencing an active channel ID and, if
1535
        // not, we will fail the attempt as it has no way to be retried again.
1536
        var shouldFail bool
×
1537

×
1538
        // Validate that the attempt has hop info. If this attempt has no hop
×
1539
        // info it indicates an error in our db.
×
1540
        if len(a.Route.Hops) == 0 {
×
1541
                log.Errorf("Found empty hop for attempt=%v", a.AttemptID)
×
1542

×
1543
                shouldFail = true
×
1544
        } else {
×
1545
                // Get the short channel ID.
×
1546
                chanID := a.Route.Hops[0].ChannelID
×
1547
                scid := lnwire.NewShortChanIDFromInt(chanID)
×
1548

×
1549
                // Check whether this link is active. If so, we won't fail the
×
1550
                // attempt but keep waiting for its result.
×
1551
                _, err := r.cfg.GetLink(scid)
×
1552
                if err == nil {
×
1553
                        return
×
1554
                }
×
1555

1556
                // We should get the link not found err. If not, we will log an
1557
                // error and skip failing this attempt since an unknown error
1558
                // occurred.
1559
                if !errors.Is(err, htlcswitch.ErrChannelLinkNotFound) {
×
1560
                        log.Errorf("Failed to get link for attempt=%v for "+
×
1561
                                "payment=%v: %v", a.AttemptID, payHash, err)
×
1562

×
1563
                        return
×
1564
                }
×
1565

1566
                // The channel link is not active, we now check whether this
1567
                // channel is already closed. If so, we fail the HTLC attempt
1568
                // as there's no need to wait for its network result because
1569
                // there's no link. If the channel is still pending, we'll keep
1570
                // waiting for the result as we may get a contract resolution
1571
                // for this HTLC.
1572
                if _, ok := r.cfg.ClosedSCIDs[scid]; ok {
×
1573
                        shouldFail = true
×
1574
                }
×
1575
        }
1576

1577
        // Exit if there's no need to fail.
1578
        if !shouldFail {
×
1579
                return
×
1580
        }
×
1581

1582
        log.Errorf("Failing stale attempt=%v for payment=%v", a.AttemptID,
×
1583
                payHash)
×
1584

×
1585
        // Fail the attempt in db. If there's an error, there's nothing we can
×
1586
        // do here but logging it.
×
1587
        failInfo := &channeldb.HTLCFailInfo{
×
1588
                Reason:   channeldb.HTLCFailUnknown,
×
1589
                FailTime: r.cfg.Clock.Now(),
×
1590
        }
×
1591
        _, err = r.cfg.Control.FailAttempt(payHash, a.AttemptID, failInfo)
×
1592
        if err != nil {
×
1593
                log.Errorf("Fail attempt=%v got error: %v", a.AttemptID, err)
×
1594
        }
×
1595
}
1596

1597
// getEdgeUnifiers returns a list of edge unifiers for the given route.
1598
func getEdgeUnifiers(source route.Vertex, hops []route.Vertex,
1599
        outgoingChans map[uint64]struct{},
1600
        graph Graph) ([]*edgeUnifier, error) {
8✔
1601

8✔
1602
        // Allocate a list that will contain the edge unifiers for this route.
8✔
1603
        unifiers := make([]*edgeUnifier, len(hops))
8✔
1604

8✔
1605
        // Traverse hops backwards to accumulate fees in the running amounts.
8✔
1606
        for i := len(hops) - 1; i >= 0; i-- {
21✔
1607
                toNode := hops[i]
13✔
1608

13✔
1609
                var fromNode route.Vertex
13✔
1610
                if i == 0 {
19✔
1611
                        fromNode = source
6✔
1612
                } else {
13✔
1613
                        fromNode = hops[i-1]
7✔
1614
                }
7✔
1615

1616
                // Build unified policies for this hop based on the channels
1617
                // known in the graph. Inbound fees are only active if the edge
1618
                // is not the last hop.
1619
                isExitHop := i == len(hops)-1
13✔
1620
                u := newNodeEdgeUnifier(
13✔
1621
                        source, toNode, !isExitHop, outgoingChans,
13✔
1622
                )
13✔
1623

13✔
1624
                err := u.addGraphPolicies(graph)
13✔
1625
                if err != nil {
13✔
1626
                        return nil, err
×
1627
                }
×
1628

1629
                // Exit if there are no channels.
1630
                edgeUnifier, ok := u.edgeUnifiers[fromNode]
13✔
1631
                if !ok {
14✔
1632
                        log.Errorf("Cannot find policy for node %v", fromNode)
1✔
1633
                        return nil, ErrNoChannel{position: i}
1✔
1634
                }
1✔
1635

1636
                unifiers[i] = edgeUnifier
12✔
1637
        }
1638

1639
        return unifiers, nil
7✔
1640
}
1641

1642
// senderAmtBackwardPass returns a list of unified edges for the given route and
1643
// determines the amount that should be sent to fulfill min HTLC requirements.
1644
// The minimal sender amount can be searched for by using amt=None.
1645
func senderAmtBackwardPass(unifiers []*edgeUnifier,
1646
        amt fn.Option[lnwire.MilliSatoshi],
1647
        bandwidthHints bandwidthHints) ([]*unifiedEdge, lnwire.MilliSatoshi,
1648
        error) {
11✔
1649

11✔
1650
        if len(unifiers) == 0 {
12✔
1651
                return nil, 0, fmt.Errorf("no unifiers provided")
1✔
1652
        }
1✔
1653

1654
        var unifiedEdges = make([]*unifiedEdge, len(unifiers))
10✔
1655

10✔
1656
        // We traverse the route backwards and handle the last hop separately.
10✔
1657
        edgeUnifier := unifiers[len(unifiers)-1]
10✔
1658

10✔
1659
        // incomingAmt tracks the amount that is forwarded on the edges of a
10✔
1660
        // route. The last hop only forwards the amount that the receiver should
10✔
1661
        // receive, as there are no fees paid to the last node.
10✔
1662
        // For minimum amount routes, aim to deliver at least 1 msat to
10✔
1663
        // the destination. There are nodes in the wild that have a
10✔
1664
        // min_htlc channel policy of zero, which could lead to a zero
10✔
1665
        // amount payment being made.
10✔
1666
        incomingAmt := amt.UnwrapOr(1)
10✔
1667

10✔
1668
        // If using min amt, increase the amount if needed to fulfill min HTLC
10✔
1669
        // requirements.
10✔
1670
        if amt.IsNone() {
15✔
1671
                min := edgeUnifier.minAmt()
5✔
1672
                if min > incomingAmt {
10✔
1673
                        incomingAmt = min
5✔
1674
                }
5✔
1675
        }
1676

1677
        // Get an edge for the specific amount that we want to forward.
1678
        edge := edgeUnifier.getEdge(incomingAmt, bandwidthHints, 0)
10✔
1679
        if edge == nil {
11✔
1680
                log.Errorf("Cannot find policy with amt=%v "+
1✔
1681
                        "for hop %v", incomingAmt, len(unifiers)-1)
1✔
1682

1✔
1683
                return nil, 0, ErrNoChannel{position: len(unifiers) - 1}
1✔
1684
        }
1✔
1685

1686
        unifiedEdges[len(unifiers)-1] = edge
9✔
1687

9✔
1688
        // Handle the rest of the route except the last hop.
9✔
1689
        for i := len(unifiers) - 2; i >= 0; i-- {
21✔
1690
                edgeUnifier = unifiers[i]
12✔
1691

12✔
1692
                // If using min amt, increase the amount if needed to fulfill
12✔
1693
                // min HTLC requirements.
12✔
1694
                if amt.IsNone() {
18✔
1695
                        min := edgeUnifier.minAmt()
6✔
1696
                        if min > incomingAmt {
6✔
1697
                                incomingAmt = min
×
1698
                        }
×
1699
                }
1700

1701
                // A --current hop-- B --next hop: incomingAmt-- C
1702
                // The outbound fee paid to the current end node B is based on
1703
                // the amount that the next hop forwards and B's policy for that
1704
                // hop.
1705
                outboundFee := unifiedEdges[i+1].policy.ComputeFee(
12✔
1706
                        incomingAmt,
12✔
1707
                )
12✔
1708

12✔
1709
                netAmount := incomingAmt + outboundFee
12✔
1710

12✔
1711
                // We need to select an edge that can forward the requested
12✔
1712
                // amount.
12✔
1713
                edge = edgeUnifier.getEdge(
12✔
1714
                        netAmount, bandwidthHints, outboundFee,
12✔
1715
                )
12✔
1716
                if edge == nil {
13✔
1717
                        return nil, 0, ErrNoChannel{position: i}
1✔
1718
                }
1✔
1719

1720
                // The fee paid to B depends on the current hop's inbound fee
1721
                // policy and on the outbound fee for the next hop as any
1722
                // inbound fee discount is capped by the outbound fee such that
1723
                // the total fee for B can't become negative.
1724
                inboundFee := calcCappedInboundFee(edge, netAmount, outboundFee)
11✔
1725

11✔
1726
                fee := lnwire.MilliSatoshi(int64(outboundFee) + inboundFee)
11✔
1727

11✔
1728
                log.Tracef("Select channel %v at position %v",
11✔
1729
                        edge.policy.ChannelID, i)
11✔
1730

11✔
1731
                // Finally, we update the amount that needs to flow into node B
11✔
1732
                // from A, which is the next hop's forwarding amount plus the
11✔
1733
                // fee for B: A --current hop: incomingAmt-- B --next hop-- C
11✔
1734
                incomingAmt += fee
11✔
1735

11✔
1736
                unifiedEdges[i] = edge
11✔
1737
        }
1738

1739
        return unifiedEdges, incomingAmt, nil
8✔
1740
}
1741

1742
// receiverAmtForwardPass returns the amount that a receiver will receive after
1743
// deducting all fees from the sender amount.
1744
func receiverAmtForwardPass(runningAmt lnwire.MilliSatoshi,
1745
        unifiedEdges []*unifiedEdge) (lnwire.MilliSatoshi, error) {
10✔
1746

10✔
1747
        if len(unifiedEdges) == 0 {
11✔
1748
                return 0, fmt.Errorf("no edges to forward through")
1✔
1749
        }
1✔
1750

1751
        inEdge := unifiedEdges[0]
9✔
1752
        if !inEdge.amtInRange(runningAmt) {
10✔
1753
                log.Errorf("Amount %v not in range for hop index %v",
1✔
1754
                        runningAmt, 0)
1✔
1755

1✔
1756
                return 0, ErrNoChannel{position: 0}
1✔
1757
        }
1✔
1758

1759
        // Now that we arrived at the start of the route and found out the route
1760
        // total amount, we make a forward pass. Because the amount may have
1761
        // been increased in the backward pass, fees need to be recalculated and
1762
        // amount ranges re-checked.
1763
        for i := 1; i < len(unifiedEdges); i++ {
17✔
1764
                inEdge := unifiedEdges[i-1]
9✔
1765
                outEdge := unifiedEdges[i]
9✔
1766

9✔
1767
                // Decrease the amount to send while going forward.
9✔
1768
                runningAmt = outgoingFromIncoming(runningAmt, inEdge, outEdge)
9✔
1769

9✔
1770
                if !outEdge.amtInRange(runningAmt) {
9✔
1771
                        log.Errorf("Amount %v not in range for hop index %v",
×
1772
                                runningAmt, i)
×
1773

×
1774
                        return 0, ErrNoChannel{position: i}
×
1775
                }
×
1776
        }
1777

1778
        return runningAmt, nil
8✔
1779
}
1780

1781
// incomingFromOutgoing computes the incoming amount based on the outgoing
1782
// amount by adding fees to the outgoing amount, replicating the path finding
1783
// and routing process, see also CheckHtlcForward.
1784
func incomingFromOutgoing(outgoingAmt lnwire.MilliSatoshi,
1785
        incoming, outgoing *unifiedEdge) lnwire.MilliSatoshi {
31✔
1786

31✔
1787
        outgoingFee := outgoing.policy.ComputeFee(outgoingAmt)
31✔
1788

31✔
1789
        // Net amount is the amount the inbound fees are calculated with.
31✔
1790
        netAmount := outgoingAmt + outgoingFee
31✔
1791

31✔
1792
        inboundFee := incoming.inboundFees.CalcFee(netAmount)
31✔
1793

31✔
1794
        // The inbound fee is not allowed to reduce the incoming amount below
31✔
1795
        // the outgoing amount.
31✔
1796
        if int64(outgoingFee)+inboundFee < 0 {
40✔
1797
                return outgoingAmt
9✔
1798
        }
9✔
1799

1800
        return netAmount + lnwire.MilliSatoshi(inboundFee)
22✔
1801
}
1802

1803
// outgoingFromIncoming computes the outgoing amount based on the incoming
1804
// amount by subtracting fees from the incoming amount. Note that this is not
1805
// exactly the inverse of incomingFromOutgoing, because of some rounding.
1806
func outgoingFromIncoming(incomingAmt lnwire.MilliSatoshi,
1807
        incoming, outgoing *unifiedEdge) lnwire.MilliSatoshi {
40✔
1808

40✔
1809
        // Convert all quantities to big.Int to be able to hande negative
40✔
1810
        // values. The formulas to compute the outgoing amount involve terms
40✔
1811
        // with PPM*PPM*A, which can easily overflow an int64.
40✔
1812
        A := big.NewInt(int64(incomingAmt))
40✔
1813
        Ro := big.NewInt(int64(outgoing.policy.FeeProportionalMillionths))
40✔
1814
        Bo := big.NewInt(int64(outgoing.policy.FeeBaseMSat))
40✔
1815
        Ri := big.NewInt(int64(incoming.inboundFees.Rate))
40✔
1816
        Bi := big.NewInt(int64(incoming.inboundFees.Base))
40✔
1817
        PPM := big.NewInt(1_000_000)
40✔
1818

40✔
1819
        // The following discussion was contributed by user feelancer21, see
40✔
1820
        //nolint:ll
40✔
1821
        // https://github.com/feelancer21/lnd/commit/f6f05fa930985aac0d27c3f6681aada1b599162a.
40✔
1822

40✔
1823
        // The incoming amount Ai based on the outgoing amount Ao is computed by
40✔
1824
        // Ai = max(Ai(Ao), Ao), which caps the incoming amount such that the
40✔
1825
        // total node fee (Ai - Ao) is non-negative. This is commonly enforced
40✔
1826
        // by routing nodes.
40✔
1827

40✔
1828
        // The function Ai(Ao) is given by:
40✔
1829
        // Ai(Ao) = (Ao + Bo + Ro/PPM) + (Bi + (Ao + Ro/PPM + Bo)*Ri/PPM), where
40✔
1830
        // the first term is the net amount (the outgoing amount plus the
40✔
1831
        // outbound fee), and the second is the inbound fee computed based on
40✔
1832
        // the net amount.
40✔
1833

40✔
1834
        // Ai(Ao) can potentially become more negative in absolute value than
40✔
1835
        // Ao, which is why the above mentioned capping is needed. We can
40✔
1836
        // abbreviate Ai(Ao) with Ai(Ao) = m*Ao + n, where m and n are:
40✔
1837
        // m := (1 + Ro/PPM) * (1 + Ri/PPM)
40✔
1838
        // n := Bi + Bo*(1 + Ri/PPM)
40✔
1839

40✔
1840
        // If we know that m > 0, this is equivalent of Ri/PPM > -1, because Ri
40✔
1841
        // is the only factor that can become negative. A value or Ri/PPM = -1,
40✔
1842
        // means that the routing node is willing to give up on 100% of the
40✔
1843
        // net amount (based on the fee rate), which is likely to not happen in
40✔
1844
        // practice. This condition will be important for a later trick.
40✔
1845

40✔
1846
        // If we want to compute the incoming amount based on the outgoing
40✔
1847
        // amount, which is the reverse problem, we need to solve Ai =
40✔
1848
        // max(Ai(Ao), Ao) for Ao(Ai). Given an incoming amount A,
40✔
1849
        // we look for an Ao such that A = max(Ai(Ao), Ao).
40✔
1850

40✔
1851
        // The max function separates this into two cases. The case to take is
40✔
1852
        // not clear yet, because we don't know Ao, but later we see a trick
40✔
1853
        // how to determine which case is the one to take.
40✔
1854

40✔
1855
        // first case: Ai(Ao) <= Ao:
40✔
1856
        // Therefore, A = max(Ai(Ao), Ao) = Ao, we find Ao = A.
40✔
1857
        // This also leads to Ai(A) <= A by substitution into the condition.
40✔
1858

40✔
1859
        // second case: Ai(Ao) > Ao:
40✔
1860
        // Therefore, A = max(Ai(Ao), Ao) = Ai(Ao) = m*Ao + n. Solving for Ao
40✔
1861
        // gives Ao = (A - n)/m.
40✔
1862
        //
40✔
1863
        // We know
40✔
1864
        // Ai(Ao) > Ao  <=>  A = Ai(Ao) > Ao = (A - n)/m,
40✔
1865
        // so A > (A - n)/m.
40✔
1866
        //
40✔
1867
        // **Assuming m > 0**, by multiplying with m, we can transform this to
40✔
1868
        // A * m + n > A.
40✔
1869
        //
40✔
1870
        // We know Ai(A) = A*m + n, therefore Ai(A) > A.
40✔
1871
        //
40✔
1872
        // This means that if we apply the incoming amount calculation to the
40✔
1873
        // **incoming** amount, and this condition holds, then we know that we
40✔
1874
        // deal with the second case, being able to compute the outgoing amount
40✔
1875
        // based off the formula Ao = (A - n)/m, otherwise we will just return
40✔
1876
        // the incoming amount.
40✔
1877

40✔
1878
        // In case the inbound fee rate is less than -1 (-100%), we fail to
40✔
1879
        // compute the outbound amount and return the incoming amount. This also
40✔
1880
        // protects against zero division later.
40✔
1881

40✔
1882
        // We compute m in terms of big.Int to be safe from overflows and to be
40✔
1883
        // consistent with later calculations.
40✔
1884
        // m := (PPM*PPM + Ri*PPM + Ro*PPM + Ro*Ri)/(PPM*PPM)
40✔
1885

40✔
1886
        // Compute terms in (PPM*PPM + Ri*PPM + Ro*PPM + Ro*Ri).
40✔
1887
        m1 := new(big.Int).Mul(PPM, PPM)
40✔
1888
        m2 := new(big.Int).Mul(Ri, PPM)
40✔
1889
        m3 := new(big.Int).Mul(Ro, PPM)
40✔
1890
        m4 := new(big.Int).Mul(Ro, Ri)
40✔
1891

40✔
1892
        // Add up terms m1..m4.
40✔
1893
        m := big.NewInt(0)
40✔
1894
        m.Add(m, m1)
40✔
1895
        m.Add(m, m2)
40✔
1896
        m.Add(m, m3)
40✔
1897
        m.Add(m, m4)
40✔
1898

40✔
1899
        // Since we compare to 0, we can multiply by PPM*PPM to avoid the
40✔
1900
        // division.
40✔
1901
        if m.Int64() <= 0 {
42✔
1902
                return incomingAmt
2✔
1903
        }
2✔
1904

1905
        // In order to decide if the total fee is negative, we apply the fee
1906
        // to the *incoming* amount as mentioned before.
1907

1908
        // We compute the test amount in terms of big.Int to be safe from
1909
        // overflows and to be consistent later calculations.
1910
        // testAmtF := A*m + n =
1911
        // = A + Bo + Bi + (PPM*(A*Ri + A*Ro + Ro*Ri) + A*Ri*Ro)/(PPM*PPM)
1912

1913
        // Compute terms in (A*Ri + A*Ro + Ro*Ri).
1914
        t1 := new(big.Int).Mul(A, Ri)
38✔
1915
        t2 := new(big.Int).Mul(A, Ro)
38✔
1916
        t3 := new(big.Int).Mul(Ro, Ri)
38✔
1917

38✔
1918
        // Sum up terms t1-t3.
38✔
1919
        t4 := big.NewInt(0)
38✔
1920
        t4.Add(t4, t1)
38✔
1921
        t4.Add(t4, t2)
38✔
1922
        t4.Add(t4, t3)
38✔
1923

38✔
1924
        // Compute PPM*(A*Ri + A*Ro + Ro*Ri).
38✔
1925
        t6 := new(big.Int).Mul(PPM, t4)
38✔
1926

38✔
1927
        // Compute A*Ri*Ro.
38✔
1928
        t7 := new(big.Int).Mul(A, Ri)
38✔
1929
        t7.Mul(t7, Ro)
38✔
1930

38✔
1931
        // Compute (PPM*(A*Ri + A*Ro + Ro*Ri) + A*Ri*Ro)/(PPM*PPM).
38✔
1932
        num := new(big.Int).Add(t6, t7)
38✔
1933
        denom := new(big.Int).Mul(PPM, PPM)
38✔
1934
        fraction := new(big.Int).Div(num, denom)
38✔
1935

38✔
1936
        // Sum up all terms.
38✔
1937
        testAmt := big.NewInt(0)
38✔
1938
        testAmt.Add(testAmt, A)
38✔
1939
        testAmt.Add(testAmt, Bo)
38✔
1940
        testAmt.Add(testAmt, Bi)
38✔
1941
        testAmt.Add(testAmt, fraction)
38✔
1942

38✔
1943
        // Protect against negative values for the integer cast to Msat.
38✔
1944
        if testAmt.Int64() < 0 {
42✔
1945
                return incomingAmt
4✔
1946
        }
4✔
1947

1948
        // If the second case holds, we have to compute the outgoing amount.
1949
        if lnwire.MilliSatoshi(testAmt.Int64()) > incomingAmt {
62✔
1950
                // Compute the outgoing amount by integer ceiling division. This
28✔
1951
                // precision is needed because PPM*PPM*A and other terms can
28✔
1952
                // easily overflow with int64, which happens with about
28✔
1953
                // A = 10_000 sat.
28✔
1954

28✔
1955
                // out := (A - n) / m = numerator / denominator
28✔
1956
                // numerator := PPM*(PPM*(A - Bo - Bi) - Bo*Ri)
28✔
1957
                // denominator := PPM*(PPM + Ri + Ro) + Ri*Ro
28✔
1958

28✔
1959
                var numerator big.Int
28✔
1960

28✔
1961
                // Compute (A - Bo - Bi).
28✔
1962
                temp1 := new(big.Int).Sub(A, Bo)
28✔
1963
                temp2 := new(big.Int).Sub(temp1, Bi)
28✔
1964

28✔
1965
                // Compute terms in (PPM*(A - Bo - Bi) - Bo*Ri).
28✔
1966
                temp3 := new(big.Int).Mul(PPM, temp2)
28✔
1967
                temp4 := new(big.Int).Mul(Bo, Ri)
28✔
1968

28✔
1969
                // Compute PPM*(PPM*(A - Bo - Bi) - Bo*Ri)
28✔
1970
                temp5 := new(big.Int).Sub(temp3, temp4)
28✔
1971
                numerator.Mul(PPM, temp5)
28✔
1972

28✔
1973
                var denominator big.Int
28✔
1974

28✔
1975
                // Compute (PPM + Ri + Ro).
28✔
1976
                temp1 = new(big.Int).Add(PPM, Ri)
28✔
1977
                temp2 = new(big.Int).Add(temp1, Ro)
28✔
1978

28✔
1979
                // Compute PPM*(PPM + Ri + Ro) + Ri*Ro.
28✔
1980
                temp3 = new(big.Int).Mul(PPM, temp2)
28✔
1981
                temp4 = new(big.Int).Mul(Ri, Ro)
28✔
1982
                denominator.Add(temp3, temp4)
28✔
1983

28✔
1984
                // We overestimate the outgoing amount by taking the ceiling of
28✔
1985
                // the division. This means that we may round slightly up by a
28✔
1986
                // MilliSatoshi, but this helps to ensure that we don't hit min
28✔
1987
                // HTLC constrains in the context of finding the minimum amount
28✔
1988
                // of a route.
28✔
1989
                // ceil = floor((numerator + denominator - 1) / denominator)
28✔
1990
                ceil := new(big.Int).Add(&numerator, &denominator)
28✔
1991
                ceil.Sub(ceil, big.NewInt(1))
28✔
1992
                ceil.Div(ceil, &denominator)
28✔
1993

28✔
1994
                return lnwire.MilliSatoshi(ceil.Int64())
28✔
1995
        }
28✔
1996

1997
        // Otherwise the inbound fee made up for the outbound fee, which is why
1998
        // we just return the incoming amount.
1999
        return incomingAmt
6✔
2000
}
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