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

lightningnetwork / lnd / 15561477203

10 Jun 2025 01:54PM UTC coverage: 58.351% (-10.1%) from 68.487%
15561477203

Pull #9356

github

web-flow
Merge 6440b25db into c6d6d4c0b
Pull Request #9356: lnrpc: add incoming/outgoing channel ids filter to forwarding history request

33 of 36 new or added lines in 2 files covered. (91.67%)

28366 existing lines in 455 files now uncovered.

97715 of 167461 relevant lines covered (58.35%)

1.81 hits per line

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

60.32
/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) {
3✔
342
        return &ChannelRouter{
3✔
343
                cfg:  &cfg,
3✔
344
                quit: make(chan struct{}),
3✔
345
        }, nil
3✔
346
}
3✔
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 {
3✔
352
        if !atomic.CompareAndSwapUint32(&r.started, 0, 1) {
3✔
353
                return nil
×
354
        }
×
355

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

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

364
        return nil
3✔
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 {
3✔
371
        if !atomic.CompareAndSwapUint32(&r.stopped, 0, 1) {
3✔
372
                return nil
×
373
        }
×
374

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

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

3✔
381
        return nil
3✔
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) {
3✔
441

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

3✔
449
        if blindedPathSet != nil {
6✔
450
                if blindedPathSet.IsIntroNode(source) {
3✔
UNCOV
451
                        return nil, ErrSelfIntro
×
UNCOV
452
                }
×
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 {
3✔
UNCOV
457
                        return nil, ErrHintsAndBlinded
×
UNCOV
458
                }
×
459

460
                if finalExpiry != 0 {
3✔
UNCOV
461
                        return nil, ErrExpiryAndBlinded
×
UNCOV
462
                }
×
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)
3✔
473
        if err != nil {
3✔
UNCOV
474
                return nil, err
×
UNCOV
475
        }
×
476

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

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

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

3✔
498
        switch {
3✔
UNCOV
499
        case blinded && targetSet:
×
UNCOV
500
                return route.Vertex{}, ErrTargetAndBlinded
×
501

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

505
        case targetSet:
3✔
506
                return *target, nil
3✔
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) {
3✔
518

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

3✔
522
        // We'll attempt to obtain a set of bandwidth hints that can help us
3✔
523
        // eliminate certain routes early on in the path finding process.
3✔
524
        bandwidthHints, err := newBandwidthManager(
3✔
525
                r.cfg.RoutingGraph, r.cfg.SelfNode, r.cfg.GetLink,
3✔
526
                fn.None[tlv.Blob](), r.cfg.TrafficShaper,
3✔
527
        )
3✔
528
        if err != nil {
3✔
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()
3✔
535
        if err != nil {
3✔
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)
3✔
542

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

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

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

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

3✔
580
        return route, probability, nil
3✔
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
        // IncomingChainedChannels holds the chained channels list (specified
615
        // via channel id) starting from a channel which points to the receiver
616
        // node.
617
        IncomingChainedChannels []uint64
618
}
619

620
// FindBlindedPaths finds a selection of paths to the destination node that can
621
// be used in blinded payment paths.
622
func (r *ChannelRouter) FindBlindedPaths(destination route.Vertex,
623
        amt lnwire.MilliSatoshi, probabilitySrc probabilitySource,
624
        restrictions *BlindedPathRestrictions) ([]*route.Route, error) {
3✔
625

3✔
626
        // First, find a set of candidate paths given the destination node and
3✔
627
        // path length restrictions.
3✔
628
        incomingChainedChannels := restrictions.IncomingChainedChannels
3✔
629
        minDistanceFromIntroNode := restrictions.MinDistanceFromIntroNode
3✔
630
        paths, err := findBlindedPaths(
3✔
631
                r.cfg.RoutingGraph, destination, &blindedPathRestrictions{
3✔
632
                        minNumHops:              minDistanceFromIntroNode,
3✔
633
                        maxNumHops:              restrictions.NumHops,
3✔
634
                        nodeOmissionSet:         restrictions.NodeOmissionSet,
3✔
635
                        incomingChainedChannels: incomingChainedChannels,
3✔
636
                },
3✔
637
        )
3✔
638
        if err != nil {
6✔
639
                return nil, err
3✔
640
        }
3✔
641

642
        // routeWithProbability groups a route with the probability of a
643
        // payment of the given amount succeeding on that path.
644
        type routeWithProbability struct {
3✔
645
                route       *route.Route
3✔
646
                probability float64
3✔
647
        }
3✔
648

3✔
649
        // Iterate over all the candidate paths and determine the success
3✔
650
        // probability of each path given the data we have about forwards
3✔
651
        // between any two nodes on a path.
3✔
652
        routes := make([]*routeWithProbability, 0, len(paths))
3✔
653
        for _, path := range paths {
6✔
654
                if len(path) < 1 {
3✔
655
                        return nil, fmt.Errorf("a blinded path must have at " +
×
656
                                "least one hop")
×
657
                }
×
658

659
                var (
3✔
660
                        introNode = path[0].vertex
3✔
661
                        prevNode  = introNode
3✔
662
                        hops      = make(
3✔
663
                                []*route.Hop, 0, len(path)-1,
3✔
664
                        )
3✔
665
                        totalRouteProbability = float64(1)
3✔
666
                )
3✔
667

3✔
668
                // For each set of hops on the path, get the success probability
3✔
669
                // of a forward between those two vertices and use that to
3✔
670
                // update the overall route probability.
3✔
671
                for j := 1; j < len(path); j++ {
6✔
672
                        probability := probabilitySrc(
3✔
673
                                prevNode, path[j].vertex, amt,
3✔
674
                                path[j-1].edgeCapacity,
3✔
675
                        )
3✔
676

3✔
677
                        totalRouteProbability *= probability
3✔
678

3✔
679
                        hops = append(hops, &route.Hop{
3✔
680
                                PubKeyBytes: path[j].vertex,
3✔
681
                                ChannelID:   path[j-1].channelID,
3✔
682
                        })
3✔
683

3✔
684
                        prevNode = path[j].vertex
3✔
685
                }
3✔
686

687
                routeWithProbability := &routeWithProbability{
3✔
688
                        route: &route.Route{
3✔
689
                                SourcePubKey: introNode,
3✔
690
                                Hops:         hops,
3✔
691
                        },
3✔
692
                        probability: totalRouteProbability,
3✔
693
                }
3✔
694

3✔
695
                // Don't bother adding a route if its success probability less
3✔
696
                // minimum that can be assigned to any single pair.
3✔
697
                if totalRouteProbability <= DefaultMinRouteProbability {
3✔
UNCOV
698
                        log.Debugf("Not using route (%v) as a blinded "+
×
UNCOV
699
                                "path since it resulted in an low "+
×
UNCOV
700
                                "probability path(%.3f)",
×
UNCOV
701
                                route.ChanIDString(routeWithProbability.route),
×
UNCOV
702
                                routeWithProbability.probability,
×
UNCOV
703
                        )
×
UNCOV
704
                        continue
×
705
                }
706

707
                routes = append(routes, routeWithProbability)
3✔
708
        }
709

710
        // Sort the routes based on probability.
711
        sort.Slice(routes, func(i, j int) bool {
6✔
712
                return routes[i].probability > routes[j].probability
3✔
713
        })
3✔
714

715
        // Now just choose the best paths up until the maximum number of allowed
716
        // paths.
717
        bestRoutes := make([]*route.Route, 0, restrictions.MaxNumPaths)
3✔
718
        for _, route := range routes {
6✔
719
                if len(bestRoutes) >= int(restrictions.MaxNumPaths) {
3✔
UNCOV
720
                        break
×
721
                }
722

723
                bestRoutes = append(bestRoutes, route.route)
3✔
724
        }
725

726
        return bestRoutes, nil
3✔
727
}
728

729
// generateNewSessionKey generates a new ephemeral private key to be used for a
730
// payment attempt.
731
func generateNewSessionKey() (*btcec.PrivateKey, error) {
3✔
732
        // Generate a new random session key to ensure that we don't trigger
3✔
733
        // any replay.
3✔
734
        //
3✔
735
        // TODO(roasbeef): add more sources of randomness?
3✔
736
        return btcec.NewPrivateKey()
3✔
737
}
3✔
738

739
// LightningPayment describes a payment to be sent through the network to the
740
// final destination.
741
type LightningPayment struct {
742
        // Target is the node in which the payment should be routed towards.
743
        Target route.Vertex
744

745
        // Amount is the value of the payment to send through the network in
746
        // milli-satoshis.
747
        Amount lnwire.MilliSatoshi
748

749
        // FeeLimit is the maximum fee in millisatoshis that the payment should
750
        // accept when sending it through the network. The payment will fail
751
        // if there isn't a route with lower fees than this limit.
752
        FeeLimit lnwire.MilliSatoshi
753

754
        // CltvLimit is the maximum time lock that is allowed for attempts to
755
        // complete this payment.
756
        CltvLimit uint32
757

758
        // paymentHash is the r-hash value to use within the HTLC extended to
759
        // the first hop. This won't be set for AMP payments.
760
        paymentHash *lntypes.Hash
761

762
        // amp is an optional field that is set if and only if this is am AMP
763
        // payment.
764
        amp *AMPOptions
765

766
        // FinalCLTVDelta is the CTLV expiry delta to use for the _final_ hop
767
        // in the route. This means that the final hop will have a CLTV delta
768
        // of at least: currentHeight + FinalCLTVDelta.
769
        FinalCLTVDelta uint16
770

771
        // PayAttemptTimeout is a timeout value that we'll use to determine
772
        // when we should should abandon the payment attempt after consecutive
773
        // payment failure. This prevents us from attempting to send a payment
774
        // indefinitely. A zero value means the payment will never time out.
775
        //
776
        // TODO(halseth): make wallclock time to allow resume after startup.
777
        PayAttemptTimeout time.Duration
778

779
        // RouteHints represents the different routing hints that can be used to
780
        // assist a payment in reaching its destination successfully. These
781
        // hints will act as intermediate hops along the route.
782
        //
783
        // NOTE: This is optional unless required by the payment. When providing
784
        // multiple routes, ensure the hop hints within each route are chained
785
        // together and sorted in forward order in order to reach the
786
        // destination successfully. This is mutually exclusive to the
787
        // BlindedPayment field.
788
        RouteHints [][]zpay32.HopHint
789

790
        // BlindedPathSet holds the information about a set of blinded paths to
791
        // the payment recipient. This is mutually exclusive to the RouteHints
792
        // field.
793
        BlindedPathSet *BlindedPaymentPathSet
794

795
        // OutgoingChannelIDs is the list of channels that are allowed for the
796
        // first hop. If nil, any channel may be used.
797
        OutgoingChannelIDs []uint64
798

799
        // LastHop is the pubkey of the last node before the final destination
800
        // is reached. If nil, any node may be used.
801
        LastHop *route.Vertex
802

803
        // DestFeatures specifies the set of features we assume the final node
804
        // has for pathfinding. Typically, these will be taken directly from an
805
        // invoice, but they can also be manually supplied or assumed by the
806
        // sender. If a nil feature vector is provided, the router will try to
807
        // fall back to the graph in order to load a feature vector for a node
808
        // in the public graph.
809
        DestFeatures *lnwire.FeatureVector
810

811
        // PaymentAddr is the payment address specified by the receiver. This
812
        // field should be a random 32-byte nonce presented in the receiver's
813
        // invoice to prevent probing of the destination.
814
        PaymentAddr fn.Option[[32]byte]
815

816
        // PaymentRequest is an optional payment request that this payment is
817
        // attempting to complete.
818
        PaymentRequest []byte
819

820
        // DestCustomRecords are TLV records that are to be sent to the final
821
        // hop in the new onion payload format. If the destination does not
822
        // understand this new onion payload format, then the payment will
823
        // fail.
824
        DestCustomRecords record.CustomSet
825

826
        // FirstHopCustomRecords are the TLV records that are to be sent to the
827
        // first hop of this payment. These records will be transmitted via the
828
        // wire message and therefore do not affect the onion payload size.
829
        FirstHopCustomRecords lnwire.CustomRecords
830

831
        // MaxParts is the maximum number of partial payments that may be used
832
        // to complete the full amount.
833
        MaxParts uint32
834

835
        // MaxShardAmt is the largest shard that we'll attempt to split using.
836
        // If this field is set, and we need to split, rather than attempting
837
        // half of the original payment amount, we'll use this value if half
838
        // the payment amount is greater than it.
839
        //
840
        // NOTE: This field is _optional_.
841
        MaxShardAmt *lnwire.MilliSatoshi
842

843
        // TimePref is the time preference for this payment. Set to -1 to
844
        // optimize for fees only, to 1 to optimize for reliability only or a
845
        // value in between for a mix.
846
        TimePref float64
847

848
        // Metadata is additional data that is sent along with the payment to
849
        // the payee.
850
        Metadata []byte
851
}
852

853
// AMPOptions houses information that must be known in order to send an AMP
854
// payment.
855
type AMPOptions struct {
856
        SetID     [32]byte
857
        RootShare [32]byte
858
}
859

860
// SetPaymentHash sets the given hash as the payment's overall hash. This
861
// should only be used for non-AMP payments.
862
func (l *LightningPayment) SetPaymentHash(hash lntypes.Hash) error {
3✔
863
        if l.amp != nil {
3✔
864
                return fmt.Errorf("cannot set payment hash for AMP payment")
×
865
        }
×
866

867
        l.paymentHash = &hash
3✔
868
        return nil
3✔
869
}
870

871
// SetAMP sets the given AMP options for the payment.
872
func (l *LightningPayment) SetAMP(amp *AMPOptions) error {
3✔
873
        if l.paymentHash != nil {
3✔
874
                return fmt.Errorf("cannot set amp options for payment " +
×
875
                        "with payment hash")
×
876
        }
×
877

878
        l.amp = amp
3✔
879
        return nil
3✔
880
}
881

882
// Identifier returns a 32-byte slice that uniquely identifies this single
883
// payment. For non-AMP payments this will be the payment hash, for AMP
884
// payments this will be the used SetID.
885
func (l *LightningPayment) Identifier() [32]byte {
3✔
886
        if l.amp != nil {
6✔
887
                return l.amp.SetID
3✔
888
        }
3✔
889

890
        return *l.paymentHash
3✔
891
}
892

893
// SendPayment attempts to send a payment as described within the passed
894
// LightningPayment. This function is blocking and will return either: when the
895
// payment is successful, or all candidates routes have been attempted and
896
// resulted in a failed payment. If the payment succeeds, then a non-nil Route
897
// will be returned which describes the path the successful payment traversed
898
// within the network to reach the destination. Additionally, the payment
899
// preimage will also be returned.
900
func (r *ChannelRouter) SendPayment(payment *LightningPayment) ([32]byte,
UNCOV
901
        *route.Route, error) {
×
UNCOV
902

×
UNCOV
903
        paySession, shardTracker, err := r.PreparePayment(payment)
×
UNCOV
904
        if err != nil {
×
905
                return [32]byte{}, nil, err
×
906
        }
×
907

UNCOV
908
        log.Tracef("Dispatching SendPayment for lightning payment: %v",
×
UNCOV
909
                spewPayment(payment))
×
UNCOV
910

×
UNCOV
911
        return r.sendPayment(
×
UNCOV
912
                context.Background(), payment.FeeLimit, payment.Identifier(),
×
UNCOV
913
                payment.PayAttemptTimeout, paySession, shardTracker,
×
UNCOV
914
                payment.FirstHopCustomRecords,
×
UNCOV
915
        )
×
916
}
917

918
// SendPaymentAsync is the non-blocking version of SendPayment. The payment
919
// result needs to be retrieved via the control tower.
920
func (r *ChannelRouter) SendPaymentAsync(ctx context.Context,
921
        payment *LightningPayment, ps PaymentSession, st shards.ShardTracker) {
3✔
922

3✔
923
        // Since this is the first time this payment is being made, we pass nil
3✔
924
        // for the existing attempt.
3✔
925
        r.wg.Add(1)
3✔
926
        go func() {
6✔
927
                defer r.wg.Done()
3✔
928

3✔
929
                log.Tracef("Dispatching SendPayment for lightning payment: %v",
3✔
930
                        spewPayment(payment))
3✔
931

3✔
932
                _, _, err := r.sendPayment(
3✔
933
                        ctx, payment.FeeLimit, payment.Identifier(),
3✔
934
                        payment.PayAttemptTimeout, ps, st,
3✔
935
                        payment.FirstHopCustomRecords,
3✔
936
                )
3✔
937
                if err != nil {
6✔
938
                        log.Errorf("Payment %x failed: %v",
3✔
939
                                payment.Identifier(), err)
3✔
940
                }
3✔
941
        }()
942
}
943

944
// spewPayment returns a log closures that provides a spewed string
945
// representation of the passed payment.
946
func spewPayment(payment *LightningPayment) lnutils.LogClosure {
3✔
947
        return lnutils.NewLogClosure(func() string {
3✔
948
                // Make a copy of the payment with a nilled Curve
×
949
                // before spewing.
×
950
                var routeHints [][]zpay32.HopHint
×
951
                for _, routeHint := range payment.RouteHints {
×
952
                        var hopHints []zpay32.HopHint
×
953
                        for _, hopHint := range routeHint {
×
954
                                h := hopHint.Copy()
×
955
                                hopHints = append(hopHints, h)
×
956
                        }
×
957
                        routeHints = append(routeHints, hopHints)
×
958
                }
959
                p := *payment
×
960
                p.RouteHints = routeHints
×
961
                return spew.Sdump(p)
×
962
        })
963
}
964

965
// PreparePayment creates the payment session and registers the payment with the
966
// control tower.
967
func (r *ChannelRouter) PreparePayment(payment *LightningPayment) (
968
        PaymentSession, shards.ShardTracker, error) {
3✔
969

3✔
970
        // Assemble any custom data we want to send to the first hop only.
3✔
971
        var firstHopData fn.Option[tlv.Blob]
3✔
972
        if len(payment.FirstHopCustomRecords) > 0 {
6✔
973
                if err := payment.FirstHopCustomRecords.Validate(); err != nil {
3✔
974
                        return nil, nil, fmt.Errorf("invalid first hop custom "+
×
975
                                "records: %w", err)
×
976
                }
×
977

978
                firstHopBlob, err := payment.FirstHopCustomRecords.Serialize()
3✔
979
                if err != nil {
3✔
980
                        return nil, nil, fmt.Errorf("unable to serialize "+
×
981
                                "first hop custom records: %w", err)
×
982
                }
×
983

984
                firstHopData = fn.Some(firstHopBlob)
3✔
985
        }
986

987
        // Before starting the HTLC routing attempt, we'll create a fresh
988
        // payment session which will report our errors back to mission
989
        // control.
990
        paySession, err := r.cfg.SessionSource.NewPaymentSession(
3✔
991
                payment, firstHopData, r.cfg.TrafficShaper,
3✔
992
        )
3✔
993
        if err != nil {
3✔
994
                return nil, nil, err
×
995
        }
×
996

997
        // Record this payment hash with the ControlTower, ensuring it is not
998
        // already in-flight.
999
        //
1000
        // TODO(roasbeef): store records as part of creation info?
1001
        info := &channeldb.PaymentCreationInfo{
3✔
1002
                PaymentIdentifier:     payment.Identifier(),
3✔
1003
                Value:                 payment.Amount,
3✔
1004
                CreationTime:          r.cfg.Clock.Now(),
3✔
1005
                PaymentRequest:        payment.PaymentRequest,
3✔
1006
                FirstHopCustomRecords: payment.FirstHopCustomRecords,
3✔
1007
        }
3✔
1008

3✔
1009
        // Create a new ShardTracker that we'll use during the life cycle of
3✔
1010
        // this payment.
3✔
1011
        var shardTracker shards.ShardTracker
3✔
1012
        switch {
3✔
1013
        // If this is an AMP payment, we'll use the AMP shard tracker.
1014
        case payment.amp != nil:
3✔
1015
                addr := payment.PaymentAddr.UnwrapOr([32]byte{})
3✔
1016
                shardTracker = amp.NewShardTracker(
3✔
1017
                        payment.amp.RootShare, payment.amp.SetID, addr,
3✔
1018
                        payment.Amount,
3✔
1019
                )
3✔
1020

1021
        // Otherwise we'll use the simple tracker that will map each attempt to
1022
        // the same payment hash.
1023
        default:
3✔
1024
                shardTracker = shards.NewSimpleShardTracker(
3✔
1025
                        payment.Identifier(), nil,
3✔
1026
                )
3✔
1027
        }
1028

1029
        err = r.cfg.Control.InitPayment(payment.Identifier(), info)
3✔
1030
        if err != nil {
3✔
1031
                return nil, nil, err
×
1032
        }
×
1033

1034
        return paySession, shardTracker, nil
3✔
1035
}
1036

1037
// SendToRoute sends a payment using the provided route and fails the payment
1038
// when an error is returned from the attempt.
1039
func (r *ChannelRouter) SendToRoute(htlcHash lntypes.Hash, rt *route.Route,
1040
        firstHopCustomRecords lnwire.CustomRecords) (*channeldb.HTLCAttempt,
1041
        error) {
3✔
1042

3✔
1043
        return r.sendToRoute(htlcHash, rt, false, firstHopCustomRecords)
3✔
1044
}
3✔
1045

1046
// SendToRouteSkipTempErr sends a payment using the provided route and fails
1047
// the payment ONLY when a terminal error is returned from the attempt.
1048
func (r *ChannelRouter) SendToRouteSkipTempErr(htlcHash lntypes.Hash,
1049
        rt *route.Route,
1050
        firstHopCustomRecords lnwire.CustomRecords) (*channeldb.HTLCAttempt,
UNCOV
1051
        error) {
×
UNCOV
1052

×
UNCOV
1053
        return r.sendToRoute(htlcHash, rt, true, firstHopCustomRecords)
×
UNCOV
1054
}
×
1055

1056
// sendToRoute attempts to send a payment with the given hash through the
1057
// provided route. This function is blocking and will return the attempt
1058
// information as it is stored in the database. For a successful htlc, this
1059
// information will contain the preimage. If an error occurs after the attempt
1060
// was initiated, both return values will be non-nil. If skipTempErr is true,
1061
// the payment won't be failed unless a terminal error has occurred.
1062
func (r *ChannelRouter) sendToRoute(htlcHash lntypes.Hash, rt *route.Route,
1063
        skipTempErr bool,
1064
        firstHopCustomRecords lnwire.CustomRecords) (*channeldb.HTLCAttempt,
1065
        error) {
3✔
1066

3✔
1067
        // Helper function to fail a payment. It makes sure the payment is only
3✔
1068
        // failed once so that the failure reason is not overwritten.
3✔
1069
        failPayment := func(paymentIdentifier lntypes.Hash,
3✔
1070
                reason channeldb.FailureReason) error {
6✔
1071

3✔
1072
                payment, fetchErr := r.cfg.Control.FetchPayment(
3✔
1073
                        paymentIdentifier,
3✔
1074
                )
3✔
1075
                if fetchErr != nil {
3✔
1076
                        return fetchErr
×
1077
                }
×
1078

1079
                // NOTE: We cannot rely on the payment status to be failed here
1080
                // because it can still be in-flight although the payment is
1081
                // already failed.
1082
                _, failedReason := payment.TerminalInfo()
3✔
1083
                if failedReason != nil {
6✔
1084
                        return nil
3✔
1085
                }
3✔
1086

1087
                return r.cfg.Control.FailPayment(paymentIdentifier, reason)
3✔
1088
        }
1089

1090
        log.Debugf("SendToRoute for payment %v with skipTempErr=%v",
3✔
1091
                htlcHash, skipTempErr)
3✔
1092

3✔
1093
        // Calculate amount paid to receiver.
3✔
1094
        amt := rt.ReceiverAmt()
3✔
1095

3✔
1096
        // If this is meant as an MP payment shard, we set the amount for the
3✔
1097
        // creating info to the total amount of the payment.
3✔
1098
        finalHop := rt.Hops[len(rt.Hops)-1]
3✔
1099
        mpp := finalHop.MPP
3✔
1100
        if mpp != nil {
6✔
1101
                amt = mpp.TotalMsat()
3✔
1102
        }
3✔
1103

1104
        // For non-MPP, there's no such thing as temp error as there's only one
1105
        // HTLC attempt being made. When this HTLC is failed, the payment is
1106
        // failed hence cannot be retried.
1107
        if skipTempErr && mpp == nil {
3✔
UNCOV
1108
                return nil, ErrSkipTempErr
×
UNCOV
1109
        }
×
1110

1111
        // For non-AMP payments the overall payment identifier will be the same
1112
        // hash as used for this HTLC.
1113
        paymentIdentifier := htlcHash
3✔
1114

3✔
1115
        // For AMP-payments, we'll use the setID as the unique ID for the
3✔
1116
        // overall payment.
3✔
1117
        amp := finalHop.AMP
3✔
1118
        if amp != nil {
6✔
1119
                paymentIdentifier = amp.SetID()
3✔
1120
        }
3✔
1121

1122
        // Record this payment hash with the ControlTower, ensuring it is not
1123
        // already in-flight.
1124
        info := &channeldb.PaymentCreationInfo{
3✔
1125
                PaymentIdentifier:     paymentIdentifier,
3✔
1126
                Value:                 amt,
3✔
1127
                CreationTime:          r.cfg.Clock.Now(),
3✔
1128
                PaymentRequest:        nil,
3✔
1129
                FirstHopCustomRecords: firstHopCustomRecords,
3✔
1130
        }
3✔
1131

3✔
1132
        err := r.cfg.Control.InitPayment(paymentIdentifier, info)
3✔
1133
        switch {
3✔
1134
        // If this is an MPP attempt and the hash is already registered with
1135
        // the database, we can go on to launch the shard.
1136
        case mpp != nil && errors.Is(err, channeldb.ErrPaymentInFlight):
3✔
1137
        case mpp != nil && errors.Is(err, channeldb.ErrPaymentExists):
×
1138

1139
        // Any other error is not tolerated.
1140
        case err != nil:
×
1141
                return nil, err
×
1142
        }
1143

1144
        log.Tracef("Dispatching SendToRoute for HTLC hash %v: %v", htlcHash,
3✔
1145
                lnutils.SpewLogClosure(rt))
3✔
1146

3✔
1147
        // Since the HTLC hashes and preimages are specified manually over the
3✔
1148
        // RPC for SendToRoute requests, we don't have to worry about creating
3✔
1149
        // a ShardTracker that can generate hashes for AMP payments. Instead, we
3✔
1150
        // create a simple tracker that can just return the hash for the single
3✔
1151
        // shard we'll now launch.
3✔
1152
        shardTracker := shards.NewSimpleShardTracker(htlcHash, nil)
3✔
1153

3✔
1154
        // Create a payment lifecycle using the given route with,
3✔
1155
        // - zero fee limit as we are not requesting routes.
3✔
1156
        // - nil payment session (since we already have a route).
3✔
1157
        // - no payment timeout.
3✔
1158
        // - no current block height.
3✔
1159
        p := newPaymentLifecycle(
3✔
1160
                r, 0, paymentIdentifier, nil, shardTracker, 0,
3✔
1161
                firstHopCustomRecords,
3✔
1162
        )
3✔
1163

3✔
1164
        // Allow the traffic shaper to add custom records to the outgoing HTLC
3✔
1165
        // and also adjust the amount if needed.
3✔
1166
        err = p.amendFirstHopData(rt)
3✔
1167
        if err != nil {
3✔
1168
                return nil, err
×
1169
        }
×
1170

1171
        // We found a route to try, create a new HTLC attempt to try.
1172
        //
1173
        // NOTE: we use zero `remainingAmt` here to simulate the same effect of
1174
        // setting the lastShard to be false, which is used by previous
1175
        // implementation.
1176
        attempt, err := p.registerAttempt(rt, 0)
3✔
1177
        if err != nil {
3✔
UNCOV
1178
                return nil, err
×
UNCOV
1179
        }
×
1180

1181
        // Once the attempt is created, send it to the htlcswitch. Notice that
1182
        // the `err` returned here has already been processed by
1183
        // `handleSwitchErr`, which means if there's a terminal failure, the
1184
        // payment has been failed.
1185
        result, err := p.sendAttempt(attempt)
3✔
1186
        if err != nil {
3✔
1187
                return nil, err
×
1188
        }
×
1189

1190
        // Since for SendToRoute we won't retry in case the shard fails, we'll
1191
        // mark the payment failed with the control tower immediately if the
1192
        // skipTempErr is false.
1193
        reason := channeldb.FailureReasonError
3✔
1194

3✔
1195
        // If we failed to send the HTLC, we need to further decide if we want
3✔
1196
        // to fail the payment.
3✔
1197
        if result.err != nil {
6✔
1198
                // If skipTempErr, we'll return the attempt and the temp error.
3✔
1199
                if skipTempErr {
3✔
UNCOV
1200
                        return result.attempt, result.err
×
UNCOV
1201
                }
×
1202

1203
                err := failPayment(paymentIdentifier, reason)
3✔
1204
                if err != nil {
3✔
1205
                        return nil, err
×
1206
                }
×
1207

1208
                return result.attempt, result.err
3✔
1209
        }
1210

1211
        // The attempt was successfully sent, wait for the result to be
1212
        // available.
1213
        result, err = p.collectAndHandleResult(attempt)
3✔
1214
        if err != nil {
3✔
1215
                return nil, err
×
1216
        }
×
1217

1218
        // We got a successful result.
1219
        if result.err == nil {
6✔
1220
                return result.attempt, nil
3✔
1221
        }
3✔
1222

1223
        // An error returned from collecting the result, we'll mark the payment
1224
        // as failed if we don't skip temp error.
1225
        if !skipTempErr {
6✔
1226
                err := failPayment(paymentIdentifier, reason)
3✔
1227
                if err != nil {
3✔
1228
                        return nil, err
×
1229
                }
×
1230
        }
1231

1232
        return result.attempt, result.err
3✔
1233
}
1234

1235
// sendPayment attempts to send a payment to the passed payment hash. This
1236
// function is blocking and will return either: when the payment is successful,
1237
// or all candidates routes have been attempted and resulted in a failed
1238
// payment. If the payment succeeds, then a non-nil Route will be returned
1239
// which describes the path the successful payment traversed within the network
1240
// to reach the destination. Additionally, the payment preimage will also be
1241
// returned.
1242
//
1243
// This method relies on the ControlTower's internal payment state machine to
1244
// carry out its execution. After restarts, it is safe, and assumed, that the
1245
// router will call this method for every payment still in-flight according to
1246
// the ControlTower.
1247
func (r *ChannelRouter) sendPayment(ctx context.Context,
1248
        feeLimit lnwire.MilliSatoshi, identifier lntypes.Hash,
1249
        paymentAttemptTimeout time.Duration, paySession PaymentSession,
1250
        shardTracker shards.ShardTracker,
1251
        firstHopCustomRecords lnwire.CustomRecords) ([32]byte, *route.Route,
1252
        error) {
3✔
1253

3✔
1254
        // If the user provides a timeout, we will additionally wrap the context
3✔
1255
        // in a deadline.
3✔
1256
        cancel := func() {}
6✔
1257
        if paymentAttemptTimeout > 0 {
6✔
1258
                ctx, cancel = context.WithTimeout(ctx, paymentAttemptTimeout)
3✔
1259
        }
3✔
1260

1261
        // Since resumePayment is a blocking call, we'll cancel this
1262
        // context if the payment completes before the optional
1263
        // deadline.
1264
        defer cancel()
3✔
1265

3✔
1266
        // We'll also fetch the current block height, so we can properly
3✔
1267
        // calculate the required HTLC time locks within the route.
3✔
1268
        _, currentHeight, err := r.cfg.Chain.GetBestBlock()
3✔
1269
        if err != nil {
3✔
1270
                return [32]byte{}, nil, err
×
1271
        }
×
1272

1273
        // Validate the custom records before we attempt to send the payment.
1274
        // TODO(ziggie): Move this check before registering the payment in the
1275
        // db (InitPayment).
1276
        if err := firstHopCustomRecords.Validate(); err != nil {
3✔
1277
                return [32]byte{}, nil, err
×
1278
        }
×
1279

1280
        // Now set up a paymentLifecycle struct with these params, such that we
1281
        // can resume the payment from the current state.
1282
        p := newPaymentLifecycle(
3✔
1283
                r, feeLimit, identifier, paySession, shardTracker,
3✔
1284
                currentHeight, firstHopCustomRecords,
3✔
1285
        )
3✔
1286

3✔
1287
        return p.resumePayment(ctx)
3✔
1288
}
1289

1290
// extractChannelUpdate examines the error and extracts the channel update.
1291
func (r *ChannelRouter) extractChannelUpdate(
1292
        failure lnwire.FailureMessage) *lnwire.ChannelUpdate1 {
3✔
1293

3✔
1294
        var update *lnwire.ChannelUpdate1
3✔
1295
        switch onionErr := failure.(type) {
3✔
UNCOV
1296
        case *lnwire.FailExpiryTooSoon:
×
UNCOV
1297
                update = &onionErr.Update
×
1298
        case *lnwire.FailAmountBelowMinimum:
3✔
1299
                update = &onionErr.Update
3✔
1300
        case *lnwire.FailFeeInsufficient:
3✔
1301
                update = &onionErr.Update
3✔
1302
        case *lnwire.FailIncorrectCltvExpiry:
×
1303
                update = &onionErr.Update
×
1304
        case *lnwire.FailChannelDisabled:
3✔
1305
                update = &onionErr.Update
3✔
1306
        case *lnwire.FailTemporaryChannelFailure:
3✔
1307
                update = onionErr.Update
3✔
1308
        }
1309

1310
        return update
3✔
1311
}
1312

1313
// ErrNoChannel is returned when a route cannot be built because there are no
1314
// channels that satisfy all requirements.
1315
type ErrNoChannel struct {
1316
        position int
1317
}
1318

1319
// Error returns a human-readable string describing the error.
UNCOV
1320
func (e ErrNoChannel) Error() string {
×
UNCOV
1321
        return fmt.Sprintf("no matching outgoing channel available for "+
×
UNCOV
1322
                "node index %v", e.position)
×
UNCOV
1323
}
×
1324

1325
// BuildRoute returns a fully specified route based on a list of pubkeys. If
1326
// amount is nil, the minimum routable amount is used. To force a specific
1327
// outgoing channel, use the outgoingChan parameter.
1328
func (r *ChannelRouter) BuildRoute(amt fn.Option[lnwire.MilliSatoshi],
1329
        hops []route.Vertex, outgoingChan *uint64, finalCltvDelta int32,
1330
        payAddr fn.Option[[32]byte], firstHopBlob fn.Option[[]byte]) (
1331
        *route.Route, error) {
3✔
1332

3✔
1333
        log.Tracef("BuildRoute called: hopsCount=%v, amt=%v", len(hops), amt)
3✔
1334

3✔
1335
        var outgoingChans map[uint64]struct{}
3✔
1336
        if outgoingChan != nil {
3✔
1337
                outgoingChans = map[uint64]struct{}{
×
1338
                        *outgoingChan: {},
×
1339
                }
×
1340
        }
×
1341

1342
        // We'll attempt to obtain a set of bandwidth hints that helps us select
1343
        // the best outgoing channel to use in case no outgoing channel is set.
1344
        bandwidthHints, err := newBandwidthManager(
3✔
1345
                r.cfg.RoutingGraph, r.cfg.SelfNode, r.cfg.GetLink, firstHopBlob,
3✔
1346
                r.cfg.TrafficShaper,
3✔
1347
        )
3✔
1348
        if err != nil {
3✔
1349
                return nil, err
×
1350
        }
×
1351

1352
        sourceNode := r.cfg.SelfNode
3✔
1353

3✔
1354
        // We check that each node in the route has a connection to others that
3✔
1355
        // can forward in principle.
3✔
1356
        unifiers, err := getEdgeUnifiers(
3✔
1357
                r.cfg.SelfNode, hops, outgoingChans, r.cfg.RoutingGraph,
3✔
1358
        )
3✔
1359
        if err != nil {
3✔
UNCOV
1360
                return nil, err
×
UNCOV
1361
        }
×
1362

1363
        var (
3✔
1364
                receiverAmt lnwire.MilliSatoshi
3✔
1365
                senderAmt   lnwire.MilliSatoshi
3✔
1366
                pathEdges   []*unifiedEdge
3✔
1367
        )
3✔
1368

3✔
1369
        // We determine the edges compatible with the requested amount, as well
3✔
1370
        // as the amount to send, which can be used to determine the final
3✔
1371
        // receiver amount, if a minimal amount was requested.
3✔
1372
        pathEdges, senderAmt, err = senderAmtBackwardPass(
3✔
1373
                unifiers, amt, bandwidthHints,
3✔
1374
        )
3✔
1375
        if err != nil {
3✔
UNCOV
1376
                return nil, err
×
UNCOV
1377
        }
×
1378

1379
        // For the minimal amount search, we need to do a forward pass to find a
1380
        // larger receiver amount due to possible min HTLC bumps, otherwise we
1381
        // just use the requested amount.
1382
        receiverAmt, err = fn.ElimOption(
3✔
1383
                amt,
3✔
1384
                func() fn.Result[lnwire.MilliSatoshi] {
3✔
UNCOV
1385
                        return fn.NewResult(
×
UNCOV
1386
                                receiverAmtForwardPass(senderAmt, pathEdges),
×
UNCOV
1387
                        )
×
UNCOV
1388
                },
×
1389
                fn.Ok[lnwire.MilliSatoshi],
1390
        ).Unpack()
1391
        if err != nil {
3✔
1392
                return nil, err
×
1393
        }
×
1394

1395
        // Fetch the current block height outside the routing transaction, to
1396
        // prevent the rpc call blocking the database.
1397
        _, height, err := r.cfg.Chain.GetBestBlock()
3✔
1398
        if err != nil {
3✔
1399
                return nil, err
×
1400
        }
×
1401

1402
        // Build and return the final route.
1403
        return newRoute(
3✔
1404
                sourceNode, pathEdges, uint32(height),
3✔
1405
                finalHopParams{
3✔
1406
                        amt:         receiverAmt,
3✔
1407
                        totalAmt:    receiverAmt,
3✔
1408
                        cltvDelta:   uint16(finalCltvDelta),
3✔
1409
                        records:     nil,
3✔
1410
                        paymentAddr: payAddr,
3✔
1411
                }, nil,
3✔
1412
        )
3✔
1413
}
1414

1415
// resumePayments fetches inflight payments and resumes their payment
1416
// lifecycles.
1417
func (r *ChannelRouter) resumePayments() error {
3✔
1418
        // Get all payments that are inflight.
3✔
1419
        log.Debugf("Scanning for inflight payments")
3✔
1420
        payments, err := r.cfg.Control.FetchInFlightPayments()
3✔
1421
        if err != nil {
3✔
1422
                return err
×
1423
        }
×
1424

1425
        log.Debugf("Scanning finished, found %d inflight payments",
3✔
1426
                len(payments))
3✔
1427

3✔
1428
        // Before we restart existing payments and start accepting more
3✔
1429
        // payments to be made, we clean the network result store of the
3✔
1430
        // Switch. We do this here at startup to ensure no more payments can be
3✔
1431
        // made concurrently, so we know the toKeep map will be up-to-date
3✔
1432
        // until the cleaning has finished.
3✔
1433
        toKeep := make(map[uint64]struct{})
3✔
1434
        for _, p := range payments {
6✔
1435
                for _, a := range p.HTLCs {
6✔
1436
                        toKeep[a.AttemptID] = struct{}{}
3✔
1437

3✔
1438
                        // Try to fail the attempt if the route contains a dead
3✔
1439
                        // channel.
3✔
1440
                        r.failStaleAttempt(a, p.Info.PaymentIdentifier)
3✔
1441
                }
3✔
1442
        }
1443

1444
        log.Debugf("Cleaning network result store.")
3✔
1445
        if err := r.cfg.Payer.CleanStore(toKeep); err != nil {
3✔
1446
                return err
×
1447
        }
×
1448

1449
        // launchPayment is a helper closure that handles resuming the payment.
1450
        launchPayment := func(payment *channeldb.MPPayment) {
6✔
1451
                defer r.wg.Done()
3✔
1452

3✔
1453
                // Get the hashes used for the outstanding HTLCs.
3✔
1454
                htlcs := make(map[uint64]lntypes.Hash)
3✔
1455
                for _, a := range payment.HTLCs {
6✔
1456
                        a := a
3✔
1457

3✔
1458
                        // We check whether the individual attempts have their
3✔
1459
                        // HTLC hash set, if not we'll fall back to the overall
3✔
1460
                        // payment hash.
3✔
1461
                        hash := payment.Info.PaymentIdentifier
3✔
1462
                        if a.Hash != nil {
6✔
1463
                                hash = *a.Hash
3✔
1464
                        }
3✔
1465

1466
                        htlcs[a.AttemptID] = hash
3✔
1467
                }
1468

1469
                payHash := payment.Info.PaymentIdentifier
3✔
1470

3✔
1471
                // Since we are not supporting creating more shards after a
3✔
1472
                // restart (only receiving the result of the shards already
3✔
1473
                // outstanding), we create a simple shard tracker that will map
3✔
1474
                // the attempt IDs to hashes used for the HTLCs. This will be
3✔
1475
                // enough also for AMP payments, since we only need the hashes
3✔
1476
                // for the individual HTLCs to regenerate the circuits, and we
3✔
1477
                // don't currently persist the root share necessary to
3✔
1478
                // re-derive them.
3✔
1479
                shardTracker := shards.NewSimpleShardTracker(payHash, htlcs)
3✔
1480

3✔
1481
                // We create a dummy, empty payment session such that we won't
3✔
1482
                // make another payment attempt when the result for the
3✔
1483
                // in-flight attempt is received.
3✔
1484
                paySession := r.cfg.SessionSource.NewPaymentSessionEmpty()
3✔
1485

3✔
1486
                // We pass in a non-timeout context, to indicate we don't need
3✔
1487
                // it to timeout. It will stop immediately after the existing
3✔
1488
                // attempt has finished anyway. We also set a zero fee limit,
3✔
1489
                // as no more routes should be tried.
3✔
1490
                noTimeout := time.Duration(0)
3✔
1491
                _, _, err := r.sendPayment(
3✔
1492
                        context.Background(), 0, payHash, noTimeout, paySession,
3✔
1493
                        shardTracker, payment.Info.FirstHopCustomRecords,
3✔
1494
                )
3✔
1495
                if err != nil {
6✔
1496
                        log.Errorf("Resuming payment %v failed: %v", payHash,
3✔
1497
                                err)
3✔
1498

3✔
1499
                        return
3✔
1500
                }
3✔
1501

1502
                log.Infof("Resumed payment %v completed", payHash)
3✔
1503
        }
1504

1505
        for _, payment := range payments {
6✔
1506
                log.Infof("Resuming payment %v", payment.Info.PaymentIdentifier)
3✔
1507

3✔
1508
                r.wg.Add(1)
3✔
1509
                go launchPayment(payment)
3✔
1510
        }
3✔
1511

1512
        return nil
3✔
1513
}
1514

1515
// failStaleAttempt will fail an HTLC attempt if it's using an unknown channel
1516
// in its route. It first consults the switch to see if there's already a
1517
// network result stored for this attempt. If not, it will check whether the
1518
// first hop of this attempt is using an active channel known to us. If
1519
// inactive, this attempt will be failed.
1520
//
1521
// NOTE: there's an unknown bug that caused the network result for a particular
1522
// attempt to NOT be saved, resulting a payment being stuck forever. More info:
1523
// - https://github.com/lightningnetwork/lnd/issues/8146
1524
// - https://github.com/lightningnetwork/lnd/pull/8174
1525
func (r *ChannelRouter) failStaleAttempt(a channeldb.HTLCAttempt,
1526
        payHash lntypes.Hash) {
3✔
1527

3✔
1528
        // We can only fail inflight HTLCs so we skip the settled/failed ones.
3✔
1529
        if a.Failure != nil || a.Settle != nil {
3✔
1530
                return
×
1531
        }
×
1532

1533
        // First, check if we've already had a network result for this attempt.
1534
        // If no result is found, we'll check whether the reference link is
1535
        // still known to us.
1536
        ok, err := r.cfg.Payer.HasAttemptResult(a.AttemptID)
3✔
1537
        if err != nil {
3✔
1538
                log.Errorf("Failed to fetch network result for attempt=%v",
×
1539
                        a.AttemptID)
×
1540
                return
×
1541
        }
×
1542

1543
        // There's already a network result, no need to fail it here as the
1544
        // payment lifecycle will take care of it, so we can exit early.
1545
        if ok {
3✔
1546
                log.Debugf("Already have network result for attempt=%v",
×
1547
                        a.AttemptID)
×
1548
                return
×
1549
        }
×
1550

1551
        // We now need to decide whether this attempt should be failed here.
1552
        // For very old payments, it's possible that the network results were
1553
        // never saved, causing the payments to be stuck inflight. We now check
1554
        // whether the first hop is referencing an active channel ID and, if
1555
        // not, we will fail the attempt as it has no way to be retried again.
1556
        var shouldFail bool
3✔
1557

3✔
1558
        // Validate that the attempt has hop info. If this attempt has no hop
3✔
1559
        // info it indicates an error in our db.
3✔
1560
        if len(a.Route.Hops) == 0 {
3✔
1561
                log.Errorf("Found empty hop for attempt=%v", a.AttemptID)
×
1562

×
1563
                shouldFail = true
×
1564
        } else {
3✔
1565
                // Get the short channel ID.
3✔
1566
                chanID := a.Route.Hops[0].ChannelID
3✔
1567
                scid := lnwire.NewShortChanIDFromInt(chanID)
3✔
1568

3✔
1569
                // Check whether this link is active. If so, we won't fail the
3✔
1570
                // attempt but keep waiting for its result.
3✔
1571
                _, err := r.cfg.GetLink(scid)
3✔
1572
                if err == nil {
3✔
1573
                        return
×
1574
                }
×
1575

1576
                // We should get the link not found err. If not, we will log an
1577
                // error and skip failing this attempt since an unknown error
1578
                // occurred.
1579
                if !errors.Is(err, htlcswitch.ErrChannelLinkNotFound) {
3✔
1580
                        log.Errorf("Failed to get link for attempt=%v for "+
×
1581
                                "payment=%v: %v", a.AttemptID, payHash, err)
×
1582

×
1583
                        return
×
1584
                }
×
1585

1586
                // The channel link is not active, we now check whether this
1587
                // channel is already closed. If so, we fail the HTLC attempt
1588
                // as there's no need to wait for its network result because
1589
                // there's no link. If the channel is still pending, we'll keep
1590
                // waiting for the result as we may get a contract resolution
1591
                // for this HTLC.
1592
                if _, ok := r.cfg.ClosedSCIDs[scid]; ok {
3✔
1593
                        shouldFail = true
×
1594
                }
×
1595
        }
1596

1597
        // Exit if there's no need to fail.
1598
        if !shouldFail {
6✔
1599
                return
3✔
1600
        }
3✔
1601

1602
        log.Errorf("Failing stale attempt=%v for payment=%v", a.AttemptID,
×
1603
                payHash)
×
1604

×
1605
        // Fail the attempt in db. If there's an error, there's nothing we can
×
1606
        // do here but logging it.
×
1607
        failInfo := &channeldb.HTLCFailInfo{
×
1608
                Reason:   channeldb.HTLCFailUnknown,
×
1609
                FailTime: r.cfg.Clock.Now(),
×
1610
        }
×
1611
        _, err = r.cfg.Control.FailAttempt(payHash, a.AttemptID, failInfo)
×
1612
        if err != nil {
×
1613
                log.Errorf("Fail attempt=%v got error: %v", a.AttemptID, err)
×
1614
        }
×
1615
}
1616

1617
// getEdgeUnifiers returns a list of edge unifiers for the given route.
1618
func getEdgeUnifiers(source route.Vertex, hops []route.Vertex,
1619
        outgoingChans map[uint64]struct{},
1620
        graph Graph) ([]*edgeUnifier, error) {
3✔
1621

3✔
1622
        // Allocate a list that will contain the edge unifiers for this route.
3✔
1623
        unifiers := make([]*edgeUnifier, len(hops))
3✔
1624

3✔
1625
        // Traverse hops backwards to accumulate fees in the running amounts.
3✔
1626
        for i := len(hops) - 1; i >= 0; i-- {
6✔
1627
                toNode := hops[i]
3✔
1628

3✔
1629
                var fromNode route.Vertex
3✔
1630
                if i == 0 {
6✔
1631
                        fromNode = source
3✔
1632
                } else {
6✔
1633
                        fromNode = hops[i-1]
3✔
1634
                }
3✔
1635

1636
                // Build unified policies for this hop based on the channels
1637
                // known in the graph. Inbound fees are only active if the edge
1638
                // is not the last hop.
1639
                isExitHop := i == len(hops)-1
3✔
1640
                u := newNodeEdgeUnifier(
3✔
1641
                        source, toNode, !isExitHop, outgoingChans,
3✔
1642
                )
3✔
1643

3✔
1644
                err := u.addGraphPolicies(graph)
3✔
1645
                if err != nil {
3✔
1646
                        return nil, err
×
1647
                }
×
1648

1649
                // Exit if there are no channels.
1650
                edgeUnifier, ok := u.edgeUnifiers[fromNode]
3✔
1651
                if !ok {
3✔
UNCOV
1652
                        log.Errorf("Cannot find policy for node %v", fromNode)
×
UNCOV
1653
                        return nil, ErrNoChannel{position: i}
×
UNCOV
1654
                }
×
1655

1656
                unifiers[i] = edgeUnifier
3✔
1657
        }
1658

1659
        return unifiers, nil
3✔
1660
}
1661

1662
// senderAmtBackwardPass returns a list of unified edges for the given route and
1663
// determines the amount that should be sent to fulfill min HTLC requirements.
1664
// The minimal sender amount can be searched for by using amt=None.
1665
func senderAmtBackwardPass(unifiers []*edgeUnifier,
1666
        amt fn.Option[lnwire.MilliSatoshi],
1667
        bandwidthHints bandwidthHints) ([]*unifiedEdge, lnwire.MilliSatoshi,
1668
        error) {
3✔
1669

3✔
1670
        if len(unifiers) == 0 {
3✔
UNCOV
1671
                return nil, 0, fmt.Errorf("no unifiers provided")
×
UNCOV
1672
        }
×
1673

1674
        var unifiedEdges = make([]*unifiedEdge, len(unifiers))
3✔
1675

3✔
1676
        // We traverse the route backwards and handle the last hop separately.
3✔
1677
        edgeUnifier := unifiers[len(unifiers)-1]
3✔
1678

3✔
1679
        // incomingAmt tracks the amount that is forwarded on the edges of a
3✔
1680
        // route. The last hop only forwards the amount that the receiver should
3✔
1681
        // receive, as there are no fees paid to the last node.
3✔
1682
        // For minimum amount routes, aim to deliver at least 1 msat to
3✔
1683
        // the destination. There are nodes in the wild that have a
3✔
1684
        // min_htlc channel policy of zero, which could lead to a zero
3✔
1685
        // amount payment being made.
3✔
1686
        incomingAmt := amt.UnwrapOr(1)
3✔
1687

3✔
1688
        // If using min amt, increase the amount if needed to fulfill min HTLC
3✔
1689
        // requirements.
3✔
1690
        if amt.IsNone() {
3✔
UNCOV
1691
                min := edgeUnifier.minAmt()
×
UNCOV
1692
                if min > incomingAmt {
×
UNCOV
1693
                        incomingAmt = min
×
UNCOV
1694
                }
×
1695
        }
1696

1697
        // Get an edge for the specific amount that we want to forward.
1698
        edge := edgeUnifier.getEdge(incomingAmt, bandwidthHints, 0)
3✔
1699
        if edge == nil {
3✔
UNCOV
1700
                log.Errorf("Cannot find policy with amt=%v "+
×
UNCOV
1701
                        "for hop %v", incomingAmt, len(unifiers)-1)
×
UNCOV
1702

×
UNCOV
1703
                return nil, 0, ErrNoChannel{position: len(unifiers) - 1}
×
UNCOV
1704
        }
×
1705

1706
        unifiedEdges[len(unifiers)-1] = edge
3✔
1707

3✔
1708
        // Handle the rest of the route except the last hop.
3✔
1709
        for i := len(unifiers) - 2; i >= 0; i-- {
6✔
1710
                edgeUnifier = unifiers[i]
3✔
1711

3✔
1712
                // If using min amt, increase the amount if needed to fulfill
3✔
1713
                // min HTLC requirements.
3✔
1714
                if amt.IsNone() {
3✔
UNCOV
1715
                        min := edgeUnifier.minAmt()
×
UNCOV
1716
                        if min > incomingAmt {
×
1717
                                incomingAmt = min
×
1718
                        }
×
1719
                }
1720

1721
                // A --current hop-- B --next hop: incomingAmt-- C
1722
                // The outbound fee paid to the current end node B is based on
1723
                // the amount that the next hop forwards and B's policy for that
1724
                // hop.
1725
                outboundFee := unifiedEdges[i+1].policy.ComputeFee(
3✔
1726
                        incomingAmt,
3✔
1727
                )
3✔
1728

3✔
1729
                netAmount := incomingAmt + outboundFee
3✔
1730

3✔
1731
                // We need to select an edge that can forward the requested
3✔
1732
                // amount.
3✔
1733
                edge = edgeUnifier.getEdge(
3✔
1734
                        netAmount, bandwidthHints, outboundFee,
3✔
1735
                )
3✔
1736
                if edge == nil {
3✔
UNCOV
1737
                        return nil, 0, ErrNoChannel{position: i}
×
UNCOV
1738
                }
×
1739

1740
                // The fee paid to B depends on the current hop's inbound fee
1741
                // policy and on the outbound fee for the next hop as any
1742
                // inbound fee discount is capped by the outbound fee such that
1743
                // the total fee for B can't become negative.
1744
                inboundFee := calcCappedInboundFee(edge, netAmount, outboundFee)
3✔
1745

3✔
1746
                fee := lnwire.MilliSatoshi(int64(outboundFee) + inboundFee)
3✔
1747

3✔
1748
                log.Tracef("Select channel %v at position %v",
3✔
1749
                        edge.policy.ChannelID, i)
3✔
1750

3✔
1751
                // Finally, we update the amount that needs to flow into node B
3✔
1752
                // from A, which is the next hop's forwarding amount plus the
3✔
1753
                // fee for B: A --current hop: incomingAmt-- B --next hop-- C
3✔
1754
                incomingAmt += fee
3✔
1755

3✔
1756
                unifiedEdges[i] = edge
3✔
1757
        }
1758

1759
        return unifiedEdges, incomingAmt, nil
3✔
1760
}
1761

1762
// receiverAmtForwardPass returns the amount that a receiver will receive after
1763
// deducting all fees from the sender amount.
1764
func receiverAmtForwardPass(runningAmt lnwire.MilliSatoshi,
UNCOV
1765
        unifiedEdges []*unifiedEdge) (lnwire.MilliSatoshi, error) {
×
UNCOV
1766

×
UNCOV
1767
        if len(unifiedEdges) == 0 {
×
UNCOV
1768
                return 0, fmt.Errorf("no edges to forward through")
×
UNCOV
1769
        }
×
1770

UNCOV
1771
        inEdge := unifiedEdges[0]
×
UNCOV
1772
        if !inEdge.amtInRange(runningAmt) {
×
UNCOV
1773
                log.Errorf("Amount %v not in range for hop index %v",
×
UNCOV
1774
                        runningAmt, 0)
×
UNCOV
1775

×
UNCOV
1776
                return 0, ErrNoChannel{position: 0}
×
UNCOV
1777
        }
×
1778

1779
        // Now that we arrived at the start of the route and found out the route
1780
        // total amount, we make a forward pass. Because the amount may have
1781
        // been increased in the backward pass, fees need to be recalculated and
1782
        // amount ranges re-checked.
UNCOV
1783
        for i := 1; i < len(unifiedEdges); i++ {
×
UNCOV
1784
                inEdge := unifiedEdges[i-1]
×
UNCOV
1785
                outEdge := unifiedEdges[i]
×
UNCOV
1786

×
UNCOV
1787
                // Decrease the amount to send while going forward.
×
UNCOV
1788
                runningAmt = outgoingFromIncoming(runningAmt, inEdge, outEdge)
×
UNCOV
1789

×
UNCOV
1790
                if !outEdge.amtInRange(runningAmt) {
×
1791
                        log.Errorf("Amount %v not in range for hop index %v",
×
1792
                                runningAmt, i)
×
1793

×
1794
                        return 0, ErrNoChannel{position: i}
×
1795
                }
×
1796
        }
1797

UNCOV
1798
        return runningAmt, nil
×
1799
}
1800

1801
// incomingFromOutgoing computes the incoming amount based on the outgoing
1802
// amount by adding fees to the outgoing amount, replicating the path finding
1803
// and routing process, see also CheckHtlcForward.
1804
func incomingFromOutgoing(outgoingAmt lnwire.MilliSatoshi,
UNCOV
1805
        incoming, outgoing *unifiedEdge) lnwire.MilliSatoshi {
×
UNCOV
1806

×
UNCOV
1807
        outgoingFee := outgoing.policy.ComputeFee(outgoingAmt)
×
UNCOV
1808

×
UNCOV
1809
        // Net amount is the amount the inbound fees are calculated with.
×
UNCOV
1810
        netAmount := outgoingAmt + outgoingFee
×
UNCOV
1811

×
UNCOV
1812
        inboundFee := incoming.inboundFees.CalcFee(netAmount)
×
UNCOV
1813

×
UNCOV
1814
        // The inbound fee is not allowed to reduce the incoming amount below
×
UNCOV
1815
        // the outgoing amount.
×
UNCOV
1816
        if int64(outgoingFee)+inboundFee < 0 {
×
UNCOV
1817
                return outgoingAmt
×
UNCOV
1818
        }
×
1819

UNCOV
1820
        return netAmount + lnwire.MilliSatoshi(inboundFee)
×
1821
}
1822

1823
// outgoingFromIncoming computes the outgoing amount based on the incoming
1824
// amount by subtracting fees from the incoming amount. Note that this is not
1825
// exactly the inverse of incomingFromOutgoing, because of some rounding.
1826
func outgoingFromIncoming(incomingAmt lnwire.MilliSatoshi,
UNCOV
1827
        incoming, outgoing *unifiedEdge) lnwire.MilliSatoshi {
×
UNCOV
1828

×
UNCOV
1829
        // Convert all quantities to big.Int to be able to hande negative
×
UNCOV
1830
        // values. The formulas to compute the outgoing amount involve terms
×
UNCOV
1831
        // with PPM*PPM*A, which can easily overflow an int64.
×
UNCOV
1832
        A := big.NewInt(int64(incomingAmt))
×
UNCOV
1833
        Ro := big.NewInt(int64(outgoing.policy.FeeProportionalMillionths))
×
UNCOV
1834
        Bo := big.NewInt(int64(outgoing.policy.FeeBaseMSat))
×
UNCOV
1835
        Ri := big.NewInt(int64(incoming.inboundFees.Rate))
×
UNCOV
1836
        Bi := big.NewInt(int64(incoming.inboundFees.Base))
×
UNCOV
1837
        PPM := big.NewInt(1_000_000)
×
UNCOV
1838

×
UNCOV
1839
        // The following discussion was contributed by user feelancer21, see
×
UNCOV
1840
        //nolint:ll
×
UNCOV
1841
        // https://github.com/feelancer21/lnd/commit/f6f05fa930985aac0d27c3f6681aada1b599162a.
×
UNCOV
1842

×
UNCOV
1843
        // The incoming amount Ai based on the outgoing amount Ao is computed by
×
UNCOV
1844
        // Ai = max(Ai(Ao), Ao), which caps the incoming amount such that the
×
UNCOV
1845
        // total node fee (Ai - Ao) is non-negative. This is commonly enforced
×
UNCOV
1846
        // by routing nodes.
×
UNCOV
1847

×
UNCOV
1848
        // The function Ai(Ao) is given by:
×
UNCOV
1849
        // Ai(Ao) = (Ao + Bo + Ro/PPM) + (Bi + (Ao + Ro/PPM + Bo)*Ri/PPM), where
×
UNCOV
1850
        // the first term is the net amount (the outgoing amount plus the
×
UNCOV
1851
        // outbound fee), and the second is the inbound fee computed based on
×
UNCOV
1852
        // the net amount.
×
UNCOV
1853

×
UNCOV
1854
        // Ai(Ao) can potentially become more negative in absolute value than
×
UNCOV
1855
        // Ao, which is why the above mentioned capping is needed. We can
×
UNCOV
1856
        // abbreviate Ai(Ao) with Ai(Ao) = m*Ao + n, where m and n are:
×
UNCOV
1857
        // m := (1 + Ro/PPM) * (1 + Ri/PPM)
×
UNCOV
1858
        // n := Bi + Bo*(1 + Ri/PPM)
×
UNCOV
1859

×
UNCOV
1860
        // If we know that m > 0, this is equivalent of Ri/PPM > -1, because Ri
×
UNCOV
1861
        // is the only factor that can become negative. A value or Ri/PPM = -1,
×
UNCOV
1862
        // means that the routing node is willing to give up on 100% of the
×
UNCOV
1863
        // net amount (based on the fee rate), which is likely to not happen in
×
UNCOV
1864
        // practice. This condition will be important for a later trick.
×
UNCOV
1865

×
UNCOV
1866
        // If we want to compute the incoming amount based on the outgoing
×
UNCOV
1867
        // amount, which is the reverse problem, we need to solve Ai =
×
UNCOV
1868
        // max(Ai(Ao), Ao) for Ao(Ai). Given an incoming amount A,
×
UNCOV
1869
        // we look for an Ao such that A = max(Ai(Ao), Ao).
×
UNCOV
1870

×
UNCOV
1871
        // The max function separates this into two cases. The case to take is
×
UNCOV
1872
        // not clear yet, because we don't know Ao, but later we see a trick
×
UNCOV
1873
        // how to determine which case is the one to take.
×
UNCOV
1874

×
UNCOV
1875
        // first case: Ai(Ao) <= Ao:
×
UNCOV
1876
        // Therefore, A = max(Ai(Ao), Ao) = Ao, we find Ao = A.
×
UNCOV
1877
        // This also leads to Ai(A) <= A by substitution into the condition.
×
UNCOV
1878

×
UNCOV
1879
        // second case: Ai(Ao) > Ao:
×
UNCOV
1880
        // Therefore, A = max(Ai(Ao), Ao) = Ai(Ao) = m*Ao + n. Solving for Ao
×
UNCOV
1881
        // gives Ao = (A - n)/m.
×
UNCOV
1882
        //
×
UNCOV
1883
        // We know
×
UNCOV
1884
        // Ai(Ao) > Ao  <=>  A = Ai(Ao) > Ao = (A - n)/m,
×
UNCOV
1885
        // so A > (A - n)/m.
×
UNCOV
1886
        //
×
UNCOV
1887
        // **Assuming m > 0**, by multiplying with m, we can transform this to
×
UNCOV
1888
        // A * m + n > A.
×
UNCOV
1889
        //
×
UNCOV
1890
        // We know Ai(A) = A*m + n, therefore Ai(A) > A.
×
UNCOV
1891
        //
×
UNCOV
1892
        // This means that if we apply the incoming amount calculation to the
×
UNCOV
1893
        // **incoming** amount, and this condition holds, then we know that we
×
UNCOV
1894
        // deal with the second case, being able to compute the outgoing amount
×
UNCOV
1895
        // based off the formula Ao = (A - n)/m, otherwise we will just return
×
UNCOV
1896
        // the incoming amount.
×
UNCOV
1897

×
UNCOV
1898
        // In case the inbound fee rate is less than -1 (-100%), we fail to
×
UNCOV
1899
        // compute the outbound amount and return the incoming amount. This also
×
UNCOV
1900
        // protects against zero division later.
×
UNCOV
1901

×
UNCOV
1902
        // We compute m in terms of big.Int to be safe from overflows and to be
×
UNCOV
1903
        // consistent with later calculations.
×
UNCOV
1904
        // m := (PPM*PPM + Ri*PPM + Ro*PPM + Ro*Ri)/(PPM*PPM)
×
UNCOV
1905

×
UNCOV
1906
        // Compute terms in (PPM*PPM + Ri*PPM + Ro*PPM + Ro*Ri).
×
UNCOV
1907
        m1 := new(big.Int).Mul(PPM, PPM)
×
UNCOV
1908
        m2 := new(big.Int).Mul(Ri, PPM)
×
UNCOV
1909
        m3 := new(big.Int).Mul(Ro, PPM)
×
UNCOV
1910
        m4 := new(big.Int).Mul(Ro, Ri)
×
UNCOV
1911

×
UNCOV
1912
        // Add up terms m1..m4.
×
UNCOV
1913
        m := big.NewInt(0)
×
UNCOV
1914
        m.Add(m, m1)
×
UNCOV
1915
        m.Add(m, m2)
×
UNCOV
1916
        m.Add(m, m3)
×
UNCOV
1917
        m.Add(m, m4)
×
UNCOV
1918

×
UNCOV
1919
        // Since we compare to 0, we can multiply by PPM*PPM to avoid the
×
UNCOV
1920
        // division.
×
UNCOV
1921
        if m.Int64() <= 0 {
×
UNCOV
1922
                return incomingAmt
×
UNCOV
1923
        }
×
1924

1925
        // In order to decide if the total fee is negative, we apply the fee
1926
        // to the *incoming* amount as mentioned before.
1927

1928
        // We compute the test amount in terms of big.Int to be safe from
1929
        // overflows and to be consistent later calculations.
1930
        // testAmtF := A*m + n =
1931
        // = A + Bo + Bi + (PPM*(A*Ri + A*Ro + Ro*Ri) + A*Ri*Ro)/(PPM*PPM)
1932

1933
        // Compute terms in (A*Ri + A*Ro + Ro*Ri).
UNCOV
1934
        t1 := new(big.Int).Mul(A, Ri)
×
UNCOV
1935
        t2 := new(big.Int).Mul(A, Ro)
×
UNCOV
1936
        t3 := new(big.Int).Mul(Ro, Ri)
×
UNCOV
1937

×
UNCOV
1938
        // Sum up terms t1-t3.
×
UNCOV
1939
        t4 := big.NewInt(0)
×
UNCOV
1940
        t4.Add(t4, t1)
×
UNCOV
1941
        t4.Add(t4, t2)
×
UNCOV
1942
        t4.Add(t4, t3)
×
UNCOV
1943

×
UNCOV
1944
        // Compute PPM*(A*Ri + A*Ro + Ro*Ri).
×
UNCOV
1945
        t6 := new(big.Int).Mul(PPM, t4)
×
UNCOV
1946

×
UNCOV
1947
        // Compute A*Ri*Ro.
×
UNCOV
1948
        t7 := new(big.Int).Mul(A, Ri)
×
UNCOV
1949
        t7.Mul(t7, Ro)
×
UNCOV
1950

×
UNCOV
1951
        // Compute (PPM*(A*Ri + A*Ro + Ro*Ri) + A*Ri*Ro)/(PPM*PPM).
×
UNCOV
1952
        num := new(big.Int).Add(t6, t7)
×
UNCOV
1953
        denom := new(big.Int).Mul(PPM, PPM)
×
UNCOV
1954
        fraction := new(big.Int).Div(num, denom)
×
UNCOV
1955

×
UNCOV
1956
        // Sum up all terms.
×
UNCOV
1957
        testAmt := big.NewInt(0)
×
UNCOV
1958
        testAmt.Add(testAmt, A)
×
UNCOV
1959
        testAmt.Add(testAmt, Bo)
×
UNCOV
1960
        testAmt.Add(testAmt, Bi)
×
UNCOV
1961
        testAmt.Add(testAmt, fraction)
×
UNCOV
1962

×
UNCOV
1963
        // Protect against negative values for the integer cast to Msat.
×
UNCOV
1964
        if testAmt.Int64() < 0 {
×
UNCOV
1965
                return incomingAmt
×
UNCOV
1966
        }
×
1967

1968
        // If the second case holds, we have to compute the outgoing amount.
UNCOV
1969
        if lnwire.MilliSatoshi(testAmt.Int64()) > incomingAmt {
×
UNCOV
1970
                // Compute the outgoing amount by integer ceiling division. This
×
UNCOV
1971
                // precision is needed because PPM*PPM*A and other terms can
×
UNCOV
1972
                // easily overflow with int64, which happens with about
×
UNCOV
1973
                // A = 10_000 sat.
×
UNCOV
1974

×
UNCOV
1975
                // out := (A - n) / m = numerator / denominator
×
UNCOV
1976
                // numerator := PPM*(PPM*(A - Bo - Bi) - Bo*Ri)
×
UNCOV
1977
                // denominator := PPM*(PPM + Ri + Ro) + Ri*Ro
×
UNCOV
1978

×
UNCOV
1979
                var numerator big.Int
×
UNCOV
1980

×
UNCOV
1981
                // Compute (A - Bo - Bi).
×
UNCOV
1982
                temp1 := new(big.Int).Sub(A, Bo)
×
UNCOV
1983
                temp2 := new(big.Int).Sub(temp1, Bi)
×
UNCOV
1984

×
UNCOV
1985
                // Compute terms in (PPM*(A - Bo - Bi) - Bo*Ri).
×
UNCOV
1986
                temp3 := new(big.Int).Mul(PPM, temp2)
×
UNCOV
1987
                temp4 := new(big.Int).Mul(Bo, Ri)
×
UNCOV
1988

×
UNCOV
1989
                // Compute PPM*(PPM*(A - Bo - Bi) - Bo*Ri)
×
UNCOV
1990
                temp5 := new(big.Int).Sub(temp3, temp4)
×
UNCOV
1991
                numerator.Mul(PPM, temp5)
×
UNCOV
1992

×
UNCOV
1993
                var denominator big.Int
×
UNCOV
1994

×
UNCOV
1995
                // Compute (PPM + Ri + Ro).
×
UNCOV
1996
                temp1 = new(big.Int).Add(PPM, Ri)
×
UNCOV
1997
                temp2 = new(big.Int).Add(temp1, Ro)
×
UNCOV
1998

×
UNCOV
1999
                // Compute PPM*(PPM + Ri + Ro) + Ri*Ro.
×
UNCOV
2000
                temp3 = new(big.Int).Mul(PPM, temp2)
×
UNCOV
2001
                temp4 = new(big.Int).Mul(Ri, Ro)
×
UNCOV
2002
                denominator.Add(temp3, temp4)
×
UNCOV
2003

×
UNCOV
2004
                // We overestimate the outgoing amount by taking the ceiling of
×
UNCOV
2005
                // the division. This means that we may round slightly up by a
×
UNCOV
2006
                // MilliSatoshi, but this helps to ensure that we don't hit min
×
UNCOV
2007
                // HTLC constrains in the context of finding the minimum amount
×
UNCOV
2008
                // of a route.
×
UNCOV
2009
                // ceil = floor((numerator + denominator - 1) / denominator)
×
UNCOV
2010
                ceil := new(big.Int).Add(&numerator, &denominator)
×
UNCOV
2011
                ceil.Sub(ceil, big.NewInt(1))
×
UNCOV
2012
                ceil.Div(ceil, &denominator)
×
UNCOV
2013

×
UNCOV
2014
                return lnwire.MilliSatoshi(ceil.Int64())
×
UNCOV
2015
        }
×
2016

2017
        // Otherwise the inbound fee made up for the outbound fee, which is why
2018
        // we just return the incoming amount.
UNCOV
2019
        return incomingAmt
×
2020
}
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