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

lightningnetwork / lnd / 13423774416

19 Feb 2025 10:39PM UTC coverage: 49.338% (-9.5%) from 58.794%
13423774416

Pull #9484

github

Abdulkbk
lnutils: add createdir util function

This utility function replaces repetitive logic patterns
throughout LND.
Pull Request #9484: lnutils: add createDir util function

0 of 13 new or added lines in 1 file covered. (0.0%)

27149 existing lines in 431 files now uncovered.

100732 of 204168 relevant lines covered (49.34%)

1.54 hits per line

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

59.36
/routing/router.go
1
package routing
2

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

239
        // RoutingGraph is a graph source that will be used for pathfinding.
240
        RoutingGraph Graph
241

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

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

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

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

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

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

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

283
        // PathFindingConfig defines global path finding parameters.
284
        PathFindingConfig PathFindingConfig
285

286
        // Clock is mockable time provider.
287
        Clock clock.Clock
288

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

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

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

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

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

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

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

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

334
        quit chan struct{}
335
        wg   sync.WaitGroup
336
}
337

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

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

3✔
358
        log.Info("Channel Router starting")
3✔
359

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

366
        return nil
367
}
368

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

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

3✔
380
        close(r.quit)
3✔
381
        r.wg.Wait()
3✔
382

383
        return nil
384
}
385

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

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

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

402
        // TimePreference expresses the caller's time preference for
403
        // pathfinding.
404
        TimePreference float64
405

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

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

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

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

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

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

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

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

3✔
UNCOV
451
        if blindedPathSet != nil {
×
UNCOV
452
                if blindedPathSet.IsIntroNode(source) {
×
453
                        return nil, ErrSelfIntro
454
                }
455

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

×
UNCOV
462
                if finalExpiry != 0 {
×
463
                        return nil, ErrExpiryAndBlinded
464
                }
3✔
465

3✔
466
                requestExpiry = blindedPathSet.FinalCLTVDelta()
3✔
467

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

3✔
UNCOV
474
        requestTarget, err := getTargetNode(target, blindedPathSet)
×
UNCOV
475
        if err != nil {
×
476
                return nil, err
477
        }
3✔
478

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

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

3✔
495
        var (
3✔
496
                blinded   = blindedPathSet != nil
3✔
497
                targetSet = target != nil
3✔
498
        )
3✔
UNCOV
499

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

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

3✔
507
        case targetSet:
508
                return *target, nil
×
509

×
510
        default:
511
                return route.Vertex{}, ErrNoTarget
512
        }
513
}
514

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

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

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

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

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

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

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

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

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

582
        return route, probability, nil
583
}
584

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

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

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

609
        // MaxNumPaths is the maximum number of blinded paths to select.
610
        MaxNumPaths uint8
611

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

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

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

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

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

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

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

3✔
671
                        totalRouteProbability *= probability
3✔
672

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

3✔
678
                        prevNode = path[j].vertex
679
                }
680

681
                // Don't bother adding a route if its success probability less
3✔
UNCOV
682
                // minimum that can be assigned to any single pair.
×
683
                if totalRouteProbability <= DefaultMinRouteProbability {
684
                        continue
685
                }
3✔
686

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

6✔
696
        // Sort the routes based on probability.
3✔
697
        sort.Slice(routes, func(i, j int) bool {
3✔
698
                return routes[i].probability > routes[j].probability
699
        })
700

701
        // Now just choose the best paths up until the maximum number of allowed
3✔
702
        // paths.
6✔
703
        bestRoutes := make([]*route.Route, 0, restrictions.MaxNumPaths)
3✔
UNCOV
704
        for _, route := range routes {
×
705
                if len(bestRoutes) >= int(restrictions.MaxNumPaths) {
706
                        break
707
                }
3✔
708

709
                bestRoutes = append(bestRoutes, route.route)
710
        }
3✔
711

712
        return bestRoutes, nil
713
}
714

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

725
// generateSphinxPacket generates then encodes a sphinx packet which encodes
726
// the onion route specified by the passed layer 3 route. The blob returned
727
// from this function can immediately be included within an HTLC add packet to
728
// be sent to the first hop within the route.
729
func generateSphinxPacket(rt *route.Route, paymentHash []byte,
730
        sessionKey *btcec.PrivateKey) ([]byte, *sphinx.Circuit, error) {
731

732
        // Now that we know we have an actual route, we'll map the route into a
733
        // sphinx payment path which includes per-hop payloads for each hop
734
        // that give each node within the route the necessary information
735
        // (fees, CLTV value, etc.) to properly forward the payment.
736
        sphinxPath, err := rt.ToSphinxPath()
737
        if err != nil {
738
                return nil, nil, err
739
        }
740

741
        log.Tracef("Constructed per-hop payloads for payment_hash=%x: %v",
742
                paymentHash, lnutils.NewLogClosure(func() string {
743
                        path := make(
744
                                []sphinx.OnionHop, sphinxPath.TrueRouteLength(),
745
                        )
746
                        for i := range path {
747
                                hopCopy := sphinxPath[i]
748
                                path[i] = hopCopy
749
                        }
750

751
                        return spew.Sdump(path)
752
                }),
753
        )
754

755
        // Next generate the onion routing packet which allows us to perform
756
        // privacy preserving source routing across the network.
757
        sphinxPacket, err := sphinx.NewOnionPacket(
758
                sphinxPath, sessionKey, paymentHash,
759
                sphinx.DeterministicPacketFiller,
760
        )
761
        if err != nil {
762
                return nil, nil, err
763
        }
764

765
        // Finally, encode Sphinx packet using its wire representation to be
766
        // included within the HTLC add packet.
767
        var onionBlob bytes.Buffer
768
        if err := sphinxPacket.Encode(&onionBlob); err != nil {
769
                return nil, nil, err
770
        }
771

772
        log.Tracef("Generated sphinx packet: %v",
773
                lnutils.NewLogClosure(func() string {
774
                        // We make a copy of the ephemeral key and unset the
775
                        // internal curve here in order to keep the logs from
776
                        // getting noisy.
777
                        key := *sphinxPacket.EphemeralKey
778
                        packetCopy := *sphinxPacket
779
                        packetCopy.EphemeralKey = &key
780
                        return spew.Sdump(packetCopy)
781
                }),
782
        )
783

784
        return onionBlob.Bytes(), &sphinx.Circuit{
785
                SessionKey:  sessionKey,
786
                PaymentPath: sphinxPath.NodeKeys(),
787
        }, nil
788
}
789

790
// LightningPayment describes a payment to be sent through the network to the
791
// final destination.
792
type LightningPayment struct {
793
        // Target is the node in which the payment should be routed towards.
794
        Target route.Vertex
795

796
        // Amount is the value of the payment to send through the network in
797
        // milli-satoshis.
798
        Amount lnwire.MilliSatoshi
799

800
        // FeeLimit is the maximum fee in millisatoshis that the payment should
801
        // accept when sending it through the network. The payment will fail
802
        // if there isn't a route with lower fees than this limit.
803
        FeeLimit lnwire.MilliSatoshi
804

805
        // CltvLimit is the maximum time lock that is allowed for attempts to
806
        // complete this payment.
807
        CltvLimit uint32
808

809
        // paymentHash is the r-hash value to use within the HTLC extended to
810
        // the first hop. This won't be set for AMP payments.
811
        paymentHash *lntypes.Hash
812

813
        // amp is an optional field that is set if and only if this is am AMP
814
        // payment.
815
        amp *AMPOptions
816

817
        // FinalCLTVDelta is the CTLV expiry delta to use for the _final_ hop
818
        // in the route. This means that the final hop will have a CLTV delta
819
        // of at least: currentHeight + FinalCLTVDelta.
820
        FinalCLTVDelta uint16
821

822
        // PayAttemptTimeout is a timeout value that we'll use to determine
823
        // when we should should abandon the payment attempt after consecutive
824
        // payment failure. This prevents us from attempting to send a payment
825
        // indefinitely. A zero value means the payment will never time out.
826
        //
827
        // TODO(halseth): make wallclock time to allow resume after startup.
828
        PayAttemptTimeout time.Duration
829

830
        // RouteHints represents the different routing hints that can be used to
831
        // assist a payment in reaching its destination successfully. These
832
        // hints will act as intermediate hops along the route.
833
        //
834
        // NOTE: This is optional unless required by the payment. When providing
835
        // multiple routes, ensure the hop hints within each route are chained
836
        // together and sorted in forward order in order to reach the
837
        // destination successfully. This is mutually exclusive to the
838
        // BlindedPayment field.
839
        RouteHints [][]zpay32.HopHint
840

841
        // BlindedPathSet holds the information about a set of blinded paths to
842
        // the payment recipient. This is mutually exclusive to the RouteHints
843
        // field.
844
        BlindedPathSet *BlindedPaymentPathSet
845

846
        // OutgoingChannelIDs is the list of channels that are allowed for the
3✔
847
        // first hop. If nil, any channel may be used.
3✔
848
        OutgoingChannelIDs []uint64
×
849

×
850
        // LastHop is the pubkey of the last node before the final destination
851
        // is reached. If nil, any node may be used.
3✔
852
        LastHop *route.Vertex
3✔
853

854
        // DestFeatures specifies the set of features we assume the final node
855
        // has for pathfinding. Typically, these will be taken directly from an
856
        // invoice, but they can also be manually supplied or assumed by the
3✔
857
        // sender. If a nil feature vector is provided, the router will try to
3✔
858
        // fall back to the graph in order to load a feature vector for a node
×
859
        // in the public graph.
×
860
        DestFeatures *lnwire.FeatureVector
×
861

862
        // PaymentAddr is the payment address specified by the receiver. This
3✔
863
        // field should be a random 32-byte nonce presented in the receiver's
3✔
864
        // invoice to prevent probing of the destination.
865
        PaymentAddr fn.Option[[32]byte]
866

867
        // PaymentRequest is an optional payment request that this payment is
868
        // attempting to complete.
869
        PaymentRequest []byte
3✔
870

6✔
871
        // DestCustomRecords are TLV records that are to be sent to the final
3✔
872
        // hop in the new onion payload format. If the destination does not
3✔
873
        // understand this new onion payload format, then the payment will
874
        // fail.
3✔
875
        DestCustomRecords record.CustomSet
876

877
        // FirstHopCustomRecords are the TLV records that are to be sent to the
878
        // first hop of this payment. These records will be transmitted via the
879
        // wire message and therefore do not affect the onion payload size.
880
        FirstHopCustomRecords lnwire.CustomRecords
881

882
        // MaxParts is the maximum number of partial payments that may be used
883
        // to complete the full amount.
884
        MaxParts uint32
UNCOV
885

×
UNCOV
886
        // MaxShardAmt is the largest shard that we'll attempt to split using.
×
UNCOV
887
        // If this field is set, and we need to split, rather than attempting
×
UNCOV
888
        // half of the original payment amount, we'll use this value if half
×
889
        // the payment amount is greater than it.
×
890
        //
×
891
        // NOTE: This field is _optional_.
UNCOV
892
        MaxShardAmt *lnwire.MilliSatoshi
×
UNCOV
893

×
UNCOV
894
        // TimePref is the time preference for this payment. Set to -1 to
×
UNCOV
895
        // optimize for fees only, to 1 to optimize for reliability only or a
×
UNCOV
896
        // value in between for a mix.
×
UNCOV
897
        TimePref float64
×
UNCOV
898

×
UNCOV
899
        // Metadata is additional data that is sent along with the payment to
×
900
        // the payee.
901
        Metadata []byte
902
}
903

904
// AMPOptions houses information that must be known in order to send an AMP
905
// payment.
3✔
906
type AMPOptions struct {
3✔
907
        SetID     [32]byte
3✔
908
        RootShare [32]byte
3✔
909
}
3✔
910

6✔
911
// SetPaymentHash sets the given hash as the payment's overall hash. This
3✔
912
// should only be used for non-AMP payments.
3✔
913
func (l *LightningPayment) SetPaymentHash(hash lntypes.Hash) error {
3✔
914
        if l.amp != nil {
3✔
915
                return fmt.Errorf("cannot set payment hash for AMP payment")
3✔
916
        }
3✔
917

3✔
918
        l.paymentHash = &hash
3✔
919
        return nil
3✔
920
}
3✔
921

6✔
922
// SetAMP sets the given AMP options for the payment.
3✔
923
func (l *LightningPayment) SetAMP(amp *AMPOptions) error {
3✔
924
        if l.paymentHash != nil {
3✔
925
                return fmt.Errorf("cannot set amp options for payment " +
926
                        "with payment hash")
927
        }
928

929
        l.amp = amp
930
        return nil
3✔
931
}
3✔
932

×
933
// Identifier returns a 32-byte slice that uniquely identifies this single
×
934
// payment. For non-AMP payments this will be the payment hash, for AMP
×
935
// payments this will be the used SetID.
×
936
func (l *LightningPayment) Identifier() [32]byte {
×
937
        if l.amp != nil {
×
938
                return l.amp.SetID
×
939
        }
×
940

×
941
        return *l.paymentHash
×
942
}
943

×
944
// SendPayment attempts to send a payment as described within the passed
×
945
// LightningPayment. This function is blocking and will return either: when the
×
946
// payment is successful, or all candidates routes have been attempted and
947
// resulted in a failed payment. If the payment succeeds, then a non-nil Route
948
// will be returned which describes the path the successful payment traversed
949
// within the network to reach the destination. Additionally, the payment
950
// preimage will also be returned.
951
func (r *ChannelRouter) SendPayment(payment *LightningPayment) ([32]byte,
952
        *route.Route, error) {
3✔
953

3✔
954
        paySession, shardTracker, err := r.PreparePayment(payment)
3✔
955
        if err != nil {
3✔
956
                return [32]byte{}, nil, err
6✔
957
        }
3✔
958

×
959
        log.Tracef("Dispatching SendPayment for lightning payment: %v",
×
960
                spewPayment(payment))
×
961

962
        return r.sendPayment(
3✔
963
                context.Background(), payment.FeeLimit, payment.Identifier(),
3✔
964
                payment.PayAttemptTimeout, paySession, shardTracker,
×
965
                payment.FirstHopCustomRecords,
×
966
        )
×
967
}
968

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

974
        // Since this is the first time this payment is being made, we pass nil
3✔
975
        // for the existing attempt.
3✔
976
        r.wg.Add(1)
3✔
977
        go func() {
3✔
978
                defer r.wg.Done()
×
979

×
980
                log.Tracef("Dispatching SendPayment for lightning payment: %v",
981
                        spewPayment(payment))
982

983
                _, _, err := r.sendPayment(
984
                        ctx, payment.FeeLimit, payment.Identifier(),
985
                        payment.PayAttemptTimeout, ps, st,
3✔
986
                        payment.FirstHopCustomRecords,
3✔
987
                )
3✔
988
                if err != nil {
3✔
989
                        log.Errorf("Payment %x failed: %v",
3✔
990
                                payment.Identifier(), err)
3✔
991
                }
3✔
992
        }()
3✔
993
}
3✔
994

3✔
995
// spewPayment returns a log closures that provides a spewed string
3✔
996
// representation of the passed payment.
3✔
997
func spewPayment(payment *LightningPayment) lnutils.LogClosure {
998
        return lnutils.NewLogClosure(func() string {
3✔
999
                // Make a copy of the payment with a nilled Curve
3✔
1000
                // before spewing.
3✔
1001
                var routeHints [][]zpay32.HopHint
3✔
1002
                for _, routeHint := range payment.RouteHints {
3✔
1003
                        var hopHints []zpay32.HopHint
3✔
1004
                        for _, hopHint := range routeHint {
1005
                                h := hopHint.Copy()
1006
                                hopHints = append(hopHints, h)
1007
                        }
3✔
1008
                        routeHints = append(routeHints, hopHints)
3✔
1009
                }
3✔
1010
                p := *payment
3✔
1011
                p.RouteHints = routeHints
1012
                return spew.Sdump(p)
1013
        })
3✔
1014
}
3✔
1015

×
1016
// PreparePayment creates the payment session and registers the payment with the
×
1017
// control tower.
1018
func (r *ChannelRouter) PreparePayment(payment *LightningPayment) (
3✔
1019
        PaymentSession, shards.ShardTracker, error) {
1020

1021
        // Assemble any custom data we want to send to the first hop only.
1022
        var firstHopData fn.Option[tlv.Blob]
1023
        if len(payment.FirstHopCustomRecords) > 0 {
1024
                if err := payment.FirstHopCustomRecords.Validate(); err != nil {
1025
                        return nil, nil, fmt.Errorf("invalid first hop custom "+
3✔
1026
                                "records: %w", err)
3✔
1027
                }
3✔
1028

3✔
1029
                firstHopBlob, err := payment.FirstHopCustomRecords.Serialize()
1030
                if err != nil {
1031
                        return nil, nil, fmt.Errorf("unable to serialize "+
1032
                                "first hop custom records: %w", err)
1033
                }
1034

UNCOV
1035
                firstHopData = fn.Some(firstHopBlob)
×
UNCOV
1036
        }
×
UNCOV
1037

×
UNCOV
1038
        // Before starting the HTLC routing attempt, we'll create a fresh
×
1039
        // payment session which will report our errors back to mission
1040
        // control.
1041
        paySession, err := r.cfg.SessionSource.NewPaymentSession(
1042
                payment, firstHopData, r.cfg.TrafficShaper,
1043
        )
1044
        if err != nil {
1045
                return nil, nil, err
1046
        }
1047

1048
        // Record this payment hash with the ControlTower, ensuring it is not
1049
        // already in-flight.
3✔
1050
        //
3✔
1051
        // TODO(roasbeef): store records as part of creation info?
3✔
1052
        info := &channeldb.PaymentCreationInfo{
3✔
1053
                PaymentIdentifier:     payment.Identifier(),
3✔
1054
                Value:                 payment.Amount,
3✔
1055
                CreationTime:          r.cfg.Clock.Now(),
3✔
1056
                PaymentRequest:        payment.PaymentRequest,
3✔
1057
                FirstHopCustomRecords: payment.FirstHopCustomRecords,
3✔
1058
        }
3✔
1059

3✔
1060
        // Create a new ShardTracker that we'll use during the life cycle of
3✔
1061
        // this payment.
6✔
1062
        var shardTracker shards.ShardTracker
3✔
1063
        switch {
3✔
1064
        // If this is an AMP payment, we'll use the AMP shard tracker.
1065
        case payment.amp != nil:
1066
                addr := payment.PaymentAddr.UnwrapOr([32]byte{})
1067
                shardTracker = amp.NewShardTracker(
1068
                        payment.amp.RootShare, payment.amp.SetID, addr,
3✔
UNCOV
1069
                        payment.Amount,
×
UNCOV
1070
                )
×
1071

1072
        // Otherwise we'll use the simple tracker that will map each attempt to
1073
        // the same payment hash.
1074
        default:
3✔
1075
                shardTracker = shards.NewSimpleShardTracker(
3✔
1076
                        payment.Identifier(), nil,
3✔
1077
                )
3✔
1078
        }
3✔
1079

6✔
1080
        err = r.cfg.Control.InitPayment(payment.Identifier(), info)
3✔
1081
        if err != nil {
3✔
1082
                return nil, nil, err
1083
        }
1084

1085
        return paySession, shardTracker, nil
3✔
1086
}
3✔
1087

3✔
1088
// SendToRoute sends a payment using the provided route and fails the payment
3✔
1089
// when an error is returned from the attempt.
3✔
1090
func (r *ChannelRouter) SendToRoute(htlcHash lntypes.Hash, rt *route.Route,
3✔
1091
        firstHopCustomRecords lnwire.CustomRecords) (*channeldb.HTLCAttempt,
3✔
1092
        error) {
3✔
1093

3✔
1094
        return r.sendToRoute(htlcHash, rt, false, firstHopCustomRecords)
3✔
1095
}
1096

1097
// SendToRouteSkipTempErr sends a payment using the provided route and fails
3✔
1098
// the payment ONLY when a terminal error is returned from the attempt.
×
1099
func (r *ChannelRouter) SendToRouteSkipTempErr(htlcHash lntypes.Hash,
1100
        rt *route.Route,
1101
        firstHopCustomRecords lnwire.CustomRecords) (*channeldb.HTLCAttempt,
×
1102
        error) {
×
1103

1104
        return r.sendToRoute(htlcHash, rt, true, firstHopCustomRecords)
1105
}
3✔
1106

3✔
1107
// sendToRoute attempts to send a payment with the given hash through the
3✔
1108
// provided route. This function is blocking and will return the attempt
3✔
1109
// information as it is stored in the database. For a successful htlc, this
3✔
1110
// information will contain the preimage. If an error occurs after the attempt
3✔
1111
// was initiated, both return values will be non-nil. If skipTempErr is true,
3✔
1112
// the payment won't be failed unless a terminal error has occurred.
3✔
1113
func (r *ChannelRouter) sendToRoute(htlcHash lntypes.Hash, rt *route.Route,
3✔
1114
        skipTempErr bool,
3✔
1115
        firstHopCustomRecords lnwire.CustomRecords) (*channeldb.HTLCAttempt,
3✔
1116
        error) {
3✔
1117

3✔
1118
        log.Debugf("SendToRoute for payment %v with skipTempErr=%v",
3✔
1119
                htlcHash, skipTempErr)
3✔
1120

3✔
1121
        // Calculate amount paid to receiver.
3✔
1122
        amt := rt.ReceiverAmt()
3✔
1123

3✔
1124
        // If this is meant as an MP payment shard, we set the amount for the
3✔
1125
        // creating info to the total amount of the payment.
3✔
1126
        finalHop := rt.Hops[len(rt.Hops)-1]
3✔
1127
        mpp := finalHop.MPP
3✔
1128
        if mpp != nil {
3✔
1129
                amt = mpp.TotalMsat()
×
1130
        }
×
1131

1132
        // For non-MPP, there's no such thing as temp error as there's only one
1133
        // HTLC attempt being made. When this HTLC is failed, the payment is
1134
        // failed hence cannot be retried.
1135
        if skipTempErr && mpp == nil {
1136
                return nil, ErrSkipTempErr
1137
        }
3✔
1138

3✔
UNCOV
1139
        // For non-AMP payments the overall payment identifier will be the same
×
UNCOV
1140
        // hash as used for this HTLC.
×
1141
        paymentIdentifier := htlcHash
1142

1143
        // For AMP-payments, we'll use the setID as the unique ID for the
1144
        // overall payment.
1145
        amp := finalHop.AMP
1146
        if amp != nil {
3✔
1147
                paymentIdentifier = amp.SetID()
3✔
1148
        }
×
1149

×
1150
        // Record this payment hash with the ControlTower, ensuring it is not
1151
        // already in-flight.
1152
        info := &channeldb.PaymentCreationInfo{
3✔
1153
                PaymentIdentifier:     paymentIdentifier,
3✔
1154
                Value:                 amt,
×
1155
                CreationTime:          r.cfg.Clock.Now(),
×
1156
                PaymentRequest:        nil,
1157
                FirstHopCustomRecords: firstHopCustomRecords,
1158
        }
1159

1160
        err := r.cfg.Control.InitPayment(paymentIdentifier, info)
3✔
1161
        switch {
3✔
UNCOV
1162
        // If this is an MPP attempt and the hash is already registered with
×
UNCOV
1163
        // the database, we can go on to launch the shard.
×
1164
        case mpp != nil && errors.Is(err, channeldb.ErrPaymentInFlight):
1165
        case mpp != nil && errors.Is(err, channeldb.ErrPaymentExists):
1166

1167
        // Any other error is not tolerated.
1168
        case err != nil:
3✔
1169
                return nil, err
3✔
1170
        }
3✔
1171

3✔
1172
        log.Tracef("Dispatching SendToRoute for HTLC hash %v: %v", htlcHash,
6✔
1173
                lnutils.SpewLogClosure(rt))
3✔
1174

3✔
UNCOV
1175
        // Since the HTLC hashes and preimages are specified manually over the
×
UNCOV
1176
        // RPC for SendToRoute requests, we don't have to worry about creating
×
1177
        // a ShardTracker that can generate hashes for AMP payments. Instead, we
1178
        // create a simple tracker that can just return the hash for the single
1179
        // shard we'll now launch.
3✔
1180
        shardTracker := shards.NewSimpleShardTracker(htlcHash, nil)
3✔
1181

×
1182
        // Create a payment lifecycle using the given route with,
×
1183
        // - zero fee limit as we are not requesting routes.
1184
        // - nil payment session (since we already have a route).
3✔
1185
        // - no payment timeout.
1186
        // - no current block height.
1187
        p := newPaymentLifecycle(
1188
                r, 0, paymentIdentifier, nil, shardTracker, 0,
1189
                firstHopCustomRecords,
3✔
1190
        )
3✔
1191

×
1192
        // Allow the traffic shaper to add custom records to the outgoing HTLC
×
1193
        // and also adjust the amount if needed.
1194
        err = p.amendFirstHopData(rt)
1195
        if err != nil {
6✔
1196
                return nil, err
3✔
1197
        }
3✔
1198

1199
        // We found a route to try, create a new HTLC attempt to try.
1200
        //
1201
        // NOTE: we use zero `remainingAmt` here to simulate the same effect of
6✔
1202
        // setting the lastShard to be false, which is used by previous
3✔
1203
        // implementation.
3✔
1204
        attempt, err := p.registerAttempt(rt, 0)
×
1205
        if err != nil {
×
1206
                return nil, err
1207
        }
1208

3✔
1209
        // Once the attempt is created, send it to the htlcswitch. Notice that
1210
        // the `err` returned here has already been processed by
1211
        // `handleSwitchErr`, which means if there's a terminal failure, the
1212
        // payment has been failed.
1213
        result, err := p.sendAttempt(attempt)
1214
        if err != nil {
1215
                return nil, err
1216
        }
1217

1218
        // We now look up the payment to see if it's already failed.
1219
        payment, err := p.router.cfg.Control.FetchPayment(p.identifier)
1220
        if err != nil {
1221
                return result.attempt, err
1222
        }
1223

1224
        // Exit if the above error has caused the payment to be failed, we also
1225
        // return the error from sending attempt to mimic the old behavior of
1226
        // this method.
1227
        _, failedReason := payment.TerminalInfo()
1228
        if failedReason != nil {
3✔
1229
                return result.attempt, result.err
3✔
1230
        }
3✔
1231

3✔
1232
        // Since for SendToRoute we won't retry in case the shard fails, we'll
6✔
1233
        // mark the payment failed with the control tower immediately if the
6✔
1234
        // skipTempErr is false.
3✔
1235
        reason := channeldb.FailureReasonError
3✔
1236

1237
        // If we failed to send the HTLC, we need to further decide if we want
1238
        // to fail the payment.
1239
        if result.err != nil {
1240
                // If skipTempErr, we'll return the attempt and the temp error.
3✔
1241
                if skipTempErr {
3✔
1242
                        return result.attempt, result.err
3✔
1243
                }
3✔
1244

3✔
1245
                // Otherwise we need to fail the payment.
3✔
1246
                err := r.cfg.Control.FailPayment(paymentIdentifier, reason)
×
1247
                if err != nil {
×
1248
                        return nil, err
1249
                }
1250

3✔
1251
                return result.attempt, result.err
×
1252
        }
×
1253

1254
        // The attempt was successfully sent, wait for the result to be
1255
        // available.
1256
        result, err = p.collectResult(attempt)
3✔
1257
        if err != nil {
3✔
1258
                return nil, err
3✔
1259
        }
3✔
1260

3✔
1261
        // We got a successful result.
3✔
1262
        if result.err == nil {
1263
                return result.attempt, nil
1264
        }
1265

1266
        // An error returned from collecting the result, we'll mark the payment
3✔
1267
        // as failed if we don't skip temp error.
3✔
1268
        if !skipTempErr {
3✔
1269
                err := r.cfg.Control.FailPayment(paymentIdentifier, reason)
3✔
UNCOV
1270
                if err != nil {
×
UNCOV
1271
                        return nil, err
×
1272
                }
3✔
1273
        }
3✔
1274

3✔
1275
        return result.attempt, result.err
3✔
1276
}
×
1277

×
1278
// sendPayment attempts to send a payment to the passed payment hash. This
3✔
1279
// function is blocking and will return either: when the payment is successful,
3✔
1280
// or all candidates routes have been attempted and resulted in a failed
3✔
1281
// payment. If the payment succeeds, then a non-nil Route will be returned
3✔
1282
// which describes the path the successful payment traversed within the network
1283
// to reach the destination. Additionally, the payment preimage will also be
1284
// returned.
3✔
1285
//
1286
// This method relies on the ControlTower's internal payment state machine to
1287
// carry out its execution. After restarts, it is safe, and assumed, that the
1288
// router will call this method for every payment still in-flight according to
1289
// the ControlTower.
1290
func (r *ChannelRouter) sendPayment(ctx context.Context,
1291
        feeLimit lnwire.MilliSatoshi, identifier lntypes.Hash,
1292
        paymentAttemptTimeout time.Duration, paySession PaymentSession,
1293
        shardTracker shards.ShardTracker,
UNCOV
1294
        firstHopCustomRecords lnwire.CustomRecords) ([32]byte, *route.Route,
×
UNCOV
1295
        error) {
×
UNCOV
1296

×
UNCOV
1297
        // If the user provides a timeout, we will additionally wrap the context
×
1298
        // in a deadline.
1299
        cancel := func() {}
1300
        if paymentAttemptTimeout > 0 {
1301
                ctx, cancel = context.WithTimeout(ctx, paymentAttemptTimeout)
1302
        }
1303

1304
        // Since resumePayment is a blocking call, we'll cancel this
1305
        // context if the payment completes before the optional
3✔
1306
        // deadline.
3✔
1307
        defer cancel()
3✔
1308

3✔
1309
        // We'll also fetch the current block height, so we can properly
3✔
1310
        // calculate the required HTLC time locks within the route.
3✔
1311
        _, currentHeight, err := r.cfg.Chain.GetBestBlock()
×
1312
        if err != nil {
×
1313
                return [32]byte{}, nil, err
×
1314
        }
×
1315

1316
        // Validate the custom records before we attempt to send the payment.
1317
        if err := firstHopCustomRecords.Validate(); err != nil {
1318
                return [32]byte{}, nil, err
3✔
1319
        }
3✔
1320

3✔
1321
        // Now set up a paymentLifecycle struct with these params, such that we
3✔
1322
        // can resume the payment from the current state.
3✔
1323
        p := newPaymentLifecycle(
×
1324
                r, feeLimit, identifier, paySession, shardTracker,
×
1325
                currentHeight, firstHopCustomRecords,
1326
        )
3✔
1327

3✔
1328
        return p.resumePayment(ctx)
3✔
1329
}
3✔
1330

3✔
1331
// extractChannelUpdate examines the error and extracts the channel update.
3✔
1332
func (r *ChannelRouter) extractChannelUpdate(
3✔
1333
        failure lnwire.FailureMessage) *lnwire.ChannelUpdate1 {
3✔
UNCOV
1334

×
UNCOV
1335
        var update *lnwire.ChannelUpdate1
×
1336
        switch onionErr := failure.(type) {
1337
        case *lnwire.FailExpiryTooSoon:
3✔
1338
                update = &onionErr.Update
3✔
1339
        case *lnwire.FailAmountBelowMinimum:
3✔
1340
                update = &onionErr.Update
3✔
1341
        case *lnwire.FailFeeInsufficient:
3✔
1342
                update = &onionErr.Update
3✔
1343
        case *lnwire.FailIncorrectCltvExpiry:
3✔
1344
                update = &onionErr.Update
3✔
1345
        case *lnwire.FailChannelDisabled:
3✔
1346
                update = &onionErr.Update
3✔
1347
        case *lnwire.FailTemporaryChannelFailure:
3✔
1348
                update = onionErr.Update
3✔
1349
        }
3✔
UNCOV
1350

×
UNCOV
1351
        return update
×
1352
}
1353

1354
// ErrNoChannel is returned when a route cannot be built because there are no
1355
// channels that satisfy all requirements.
1356
type ErrNoChannel struct {
3✔
1357
        position int
3✔
1358
}
3✔
UNCOV
1359

×
UNCOV
1360
// Error returns a human-readable string describing the error.
×
UNCOV
1361
func (e ErrNoChannel) Error() string {
×
UNCOV
1362
        return fmt.Sprintf("no matching outgoing channel available for "+
×
1363
                "node index %v", e.position)
1364
}
1365

3✔
1366
// BuildRoute returns a fully specified route based on a list of pubkeys. If
×
1367
// amount is nil, the minimum routable amount is used. To force a specific
×
1368
// outgoing channel, use the outgoingChan parameter.
1369
func (r *ChannelRouter) BuildRoute(amt fn.Option[lnwire.MilliSatoshi],
1370
        hops []route.Vertex, outgoingChan *uint64, finalCltvDelta int32,
1371
        payAddr fn.Option[[32]byte], firstHopBlob fn.Option[[]byte]) (
3✔
1372
        *route.Route, error) {
3✔
1373

×
1374
        log.Tracef("BuildRoute called: hopsCount=%v, amt=%v", len(hops), amt)
×
1375

1376
        var outgoingChans map[uint64]struct{}
1377
        if outgoingChan != nil {
3✔
1378
                outgoingChans = map[uint64]struct{}{
3✔
1379
                        *outgoingChan: {},
3✔
1380
                }
3✔
1381
        }
3✔
1382

3✔
1383
        // We'll attempt to obtain a set of bandwidth hints that helps us select
3✔
1384
        // the best outgoing channel to use in case no outgoing channel is set.
3✔
1385
        bandwidthHints, err := newBandwidthManager(
3✔
1386
                r.cfg.RoutingGraph, r.cfg.SelfNode, r.cfg.GetLink, firstHopBlob,
3✔
1387
                r.cfg.TrafficShaper,
1388
        )
1389
        if err != nil {
1390
                return nil, err
1391
        }
3✔
1392

3✔
1393
        sourceNode := r.cfg.SelfNode
3✔
1394

3✔
1395
        // We check that each node in the route has a connection to others that
×
1396
        // can forward in principle.
×
1397
        unifiers, err := getEdgeUnifiers(
1398
                r.cfg.SelfNode, hops, outgoingChans, r.cfg.RoutingGraph,
1399
        )
1400
        if err != nil {
1401
                return nil, err
1402
        }
1403

3✔
1404
        var (
6✔
1405
                receiverAmt lnwire.MilliSatoshi
6✔
1406
                senderAmt   lnwire.MilliSatoshi
3✔
1407
                pathEdges   []*unifiedEdge
3✔
1408
        )
3✔
1409

3✔
1410
        // We determine the edges compatible with the requested amount, as well
3✔
1411
        // as the amount to send, which can be used to determine the final
3✔
1412
        // receiver amount, if a minimal amount was requested.
1413
        pathEdges, senderAmt, err = senderAmtBackwardPass(
1414
                unifiers, amt, bandwidthHints,
3✔
1415
        )
3✔
1416
        if err != nil {
×
1417
                return nil, err
×
1418
        }
1419

1420
        // For the minimal amount search, we need to do a forward pass to find a
6✔
1421
        // larger receiver amount due to possible min HTLC bumps, otherwise we
3✔
1422
        // just use the requested amount.
3✔
1423
        receiverAmt, err = fn.ElimOption(
3✔
1424
                amt,
3✔
1425
                func() fn.Result[lnwire.MilliSatoshi] {
6✔
1426
                        return fn.NewResult(
3✔
1427
                                receiverAmtForwardPass(senderAmt, pathEdges),
3✔
1428
                        )
3✔
1429
                },
3✔
1430
                fn.Ok[lnwire.MilliSatoshi],
3✔
1431
        ).Unpack()
3✔
1432
        if err != nil {
6✔
1433
                return nil, err
3✔
1434
        }
3✔
1435

1436
        // Fetch the current block height outside the routing transaction, to
3✔
1437
        // prevent the rpc call blocking the database.
1438
        _, height, err := r.cfg.Chain.GetBestBlock()
1439
        if err != nil {
3✔
1440
                return nil, err
3✔
1441
        }
3✔
1442

3✔
1443
        // Build and return the final route.
3✔
1444
        return newRoute(
3✔
1445
                sourceNode, pathEdges, uint32(height),
3✔
1446
                finalHopParams{
3✔
1447
                        amt:         receiverAmt,
3✔
1448
                        totalAmt:    receiverAmt,
3✔
1449
                        cltvDelta:   uint16(finalCltvDelta),
3✔
1450
                        records:     nil,
3✔
1451
                        paymentAddr: payAddr,
3✔
1452
                }, nil,
3✔
1453
        )
3✔
1454
}
3✔
1455

3✔
1456
// resumePayments fetches inflight payments and resumes their payment
3✔
1457
// lifecycles.
3✔
1458
func (r *ChannelRouter) resumePayments() error {
3✔
1459
        // Get all payments that are inflight.
3✔
1460
        payments, err := r.cfg.Control.FetchInFlightPayments()
3✔
1461
        if err != nil {
3✔
1462
                return err
3✔
1463
        }
3✔
1464

3✔
1465
        // Before we restart existing payments and start accepting more
6✔
1466
        // payments to be made, we clean the network result store of the
3✔
1467
        // Switch. We do this here at startup to ensure no more payments can be
3✔
1468
        // made concurrently, so we know the toKeep map will be up-to-date
3✔
1469
        // until the cleaning has finished.
3✔
1470
        toKeep := make(map[uint64]struct{})
3✔
1471
        for _, p := range payments {
1472
                for _, a := range p.HTLCs {
3✔
1473
                        toKeep[a.AttemptID] = struct{}{}
1474

1475
                        // Try to fail the attempt if the route contains a dead
6✔
1476
                        // channel.
3✔
1477
                        r.failStaleAttempt(a, p.Info.PaymentIdentifier)
3✔
1478
                }
3✔
1479
        }
3✔
1480

3✔
1481
        log.Debugf("Cleaning network result store.")
1482
        if err := r.cfg.Payer.CleanStore(toKeep); err != nil {
3✔
1483
                return err
1484
        }
1485

1486
        // launchPayment is a helper closure that handles resuming the payment.
1487
        launchPayment := func(payment *channeldb.MPPayment) {
1488
                defer r.wg.Done()
1489

1490
                // Get the hashes used for the outstanding HTLCs.
1491
                htlcs := make(map[uint64]lntypes.Hash)
1492
                for _, a := range payment.HTLCs {
1493
                        a := a
1494

1495
                        // We check whether the individual attempts have their
1496
                        // HTLC hash set, if not we'll fall back to the overall
3✔
1497
                        // payment hash.
3✔
1498
                        hash := payment.Info.PaymentIdentifier
3✔
1499
                        if a.Hash != nil {
3✔
1500
                                hash = *a.Hash
×
1501
                        }
×
1502

1503
                        htlcs[a.AttemptID] = hash
1504
                }
1505

1506
                payHash := payment.Info.PaymentIdentifier
3✔
1507

3✔
1508
                // Since we are not supporting creating more shards after a
×
1509
                // restart (only receiving the result of the shards already
×
1510
                // outstanding), we create a simple shard tracker that will map
×
1511
                // the attempt IDs to hashes used for the HTLCs. This will be
×
1512
                // enough also for AMP payments, since we only need the hashes
1513
                // for the individual HTLCs to regenerate the circuits, and we
1514
                // don't currently persist the root share necessary to
1515
                // re-derive them.
3✔
1516
                shardTracker := shards.NewSimpleShardTracker(payHash, htlcs)
×
1517

×
1518
                // We create a dummy, empty payment session such that we won't
×
1519
                // make another payment attempt when the result for the
×
1520
                // in-flight attempt is received.
1521
                paySession := r.cfg.SessionSource.NewPaymentSessionEmpty()
1522

1523
                // We pass in a non-timeout context, to indicate we don't need
1524
                // it to timeout. It will stop immediately after the existing
1525
                // attempt has finished anyway. We also set a zero fee limit,
1526
                // as no more routes should be tried.
3✔
1527
                noTimeout := time.Duration(0)
3✔
1528
                _, _, err := r.sendPayment(
3✔
1529
                        context.Background(), 0, payHash, noTimeout, paySession,
3✔
1530
                        shardTracker, payment.Info.FirstHopCustomRecords,
3✔
1531
                )
×
1532
                if err != nil {
×
1533
                        log.Errorf("Resuming payment %v failed: %v", payHash,
×
1534
                                err)
3✔
1535

3✔
1536
                        return
3✔
1537
                }
3✔
1538

3✔
1539
                log.Infof("Resumed payment %v completed", payHash)
3✔
1540
        }
3✔
1541

3✔
1542
        for _, payment := range payments {
3✔
1543
                log.Infof("Resuming payment %v", payment.Info.PaymentIdentifier)
×
1544

×
1545
                r.wg.Add(1)
1546
                go launchPayment(payment)
1547
        }
1548

1549
        return nil
3✔
1550
}
×
1551

×
1552
// failStaleAttempt will fail an HTLC attempt if it's using an unknown channel
×
1553
// in its route. It first consults the switch to see if there's already a
×
1554
// network result stored for this attempt. If not, it will check whether the
×
1555
// first hop of this attempt is using an active channel known to us. If
1556
// inactive, this attempt will be failed.
1557
//
1558
// NOTE: there's an unknown bug that caused the network result for a particular
1559
// attempt to NOT be saved, resulting a payment being stuck forever. More info:
1560
// - https://github.com/lightningnetwork/lnd/issues/8146
1561
// - https://github.com/lightningnetwork/lnd/pull/8174
1562
func (r *ChannelRouter) failStaleAttempt(a channeldb.HTLCAttempt,
3✔
1563
        payHash lntypes.Hash) {
×
1564

×
1565
        // We can only fail inflight HTLCs so we skip the settled/failed ones.
1566
        if a.Failure != nil || a.Settle != nil {
1567
                return
1568
        }
6✔
1569

3✔
1570
        // First, check if we've already had a network result for this attempt.
3✔
1571
        // If no result is found, we'll check whether the reference link is
1572
        // still known to us.
×
1573
        ok, err := r.cfg.Payer.HasAttemptResult(a.AttemptID)
×
1574
        if err != nil {
×
1575
                log.Errorf("Failed to fetch network result for attempt=%v",
×
1576
                        a.AttemptID)
×
1577
                return
×
1578
        }
×
1579

×
1580
        // There's already a network result, no need to fail it here as the
×
1581
        // payment lifecycle will take care of it, so we can exit early.
×
1582
        if ok {
×
1583
                log.Debugf("Already have network result for attempt=%v",
×
1584
                        a.AttemptID)
×
1585
                return
1586
        }
1587

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

3✔
1595
        // Validate that the attempt has hop info. If this attempt has no hop
3✔
1596
        // info it indicates an error in our db.
6✔
1597
        if len(a.Route.Hops) == 0 {
3✔
1598
                log.Errorf("Found empty hop for attempt=%v", a.AttemptID)
3✔
1599

3✔
1600
                shouldFail = true
6✔
1601
        } else {
3✔
1602
                // Get the short channel ID.
6✔
1603
                chanID := a.Route.Hops[0].ChannelID
3✔
1604
                scid := lnwire.NewShortChanIDFromInt(chanID)
3✔
1605

1606
                // Check whether this link is active. If so, we won't fail the
1607
                // attempt but keep waiting for its result.
1608
                _, err := r.cfg.GetLink(scid)
1609
                if err == nil {
3✔
1610
                        return
3✔
1611
                }
3✔
1612

3✔
1613
                // We should get the link not found err. If not, we will log an
3✔
1614
                // error and skip failing this attempt since an unknown error
3✔
1615
                // occurred.
3✔
1616
                if !errors.Is(err, htlcswitch.ErrChannelLinkNotFound) {
×
1617
                        log.Errorf("Failed to get link for attempt=%v for "+
×
1618
                                "payment=%v: %v", a.AttemptID, payHash, err)
1619

1620
                        return
3✔
1621
                }
3✔
UNCOV
1622

×
UNCOV
1623
                // The channel link is not active, we now check whether this
×
UNCOV
1624
                // channel is already closed. If so, we fail the HTLC attempt
×
1625
                // as there's no need to wait for its network result because
1626
                // there's no link. If the channel is still pending, we'll keep
3✔
1627
                // waiting for the result as we may get a contract resolution
1628
                // for this HTLC.
1629
                if _, ok := r.cfg.ClosedSCIDs[scid]; ok {
3✔
1630
                        shouldFail = true
1631
                }
1632
        }
1633

1634
        // Exit if there's no need to fail.
1635
        if !shouldFail {
1636
                return
1637
        }
1638

3✔
1639
        log.Errorf("Failing stale attempt=%v for payment=%v", a.AttemptID,
3✔
1640
                payHash)
3✔
UNCOV
1641

×
UNCOV
1642
        // Fail the attempt in db. If there's an error, there's nothing we can
×
1643
        // do here but logging it.
1644
        failInfo := &channeldb.HTLCFailInfo{
3✔
1645
                Reason:   channeldb.HTLCFailUnknown,
3✔
1646
                FailTime: r.cfg.Clock.Now(),
3✔
1647
        }
3✔
1648
        _, err = r.cfg.Control.FailAttempt(payHash, a.AttemptID, failInfo)
3✔
1649
        if err != nil {
3✔
1650
                log.Errorf("Fail attempt=%v got error: %v", a.AttemptID, err)
3✔
1651
        }
3✔
1652
}
3✔
1653

3✔
1654
// getEdgeUnifiers returns a list of edge unifiers for the given route.
3✔
1655
func getEdgeUnifiers(source route.Vertex, hops []route.Vertex,
3✔
1656
        outgoingChans map[uint64]struct{},
3✔
1657
        graph Graph) ([]*edgeUnifier, error) {
3✔
1658

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

×
UNCOV
1662
        // Traverse hops backwards to accumulate fees in the running amounts.
×
UNCOV
1663
        for i := len(hops) - 1; i >= 0; i-- {
×
UNCOV
1664
                toNode := hops[i]
×
1665

1666
                var fromNode route.Vertex
1667
                if i == 0 {
1668
                        fromNode = source
3✔
1669
                } else {
3✔
UNCOV
1670
                        fromNode = hops[i-1]
×
UNCOV
1671
                }
×
UNCOV
1672

×
UNCOV
1673
                // Build unified policies for this hop based on the channels
×
UNCOV
1674
                // known in the graph. Inbound fees are only active if the edge
×
1675
                // is not the last hop.
1676
                isExitHop := i == len(hops)-1
3✔
1677
                u := newNodeEdgeUnifier(
3✔
1678
                        source, toNode, !isExitHop, outgoingChans,
3✔
1679
                )
6✔
1680

3✔
1681
                err := u.addGraphPolicies(graph)
3✔
1682
                if err != nil {
3✔
1683
                        return nil, err
3✔
1684
                }
3✔
UNCOV
1685

×
UNCOV
1686
                // Exit if there are no channels.
×
1687
                edgeUnifier, ok := u.edgeUnifiers[fromNode]
×
1688
                if !ok {
×
1689
                        log.Errorf("Cannot find policy for node %v", fromNode)
1690
                        return nil, ErrNoChannel{position: i}
1691
                }
1692

1693
                unifiers[i] = edgeUnifier
1694
        }
1695

3✔
1696
        return unifiers, nil
3✔
1697
}
3✔
1698

3✔
1699
// senderAmtBackwardPass returns a list of unified edges for the given route and
3✔
1700
// determines the amount that should be sent to fulfill min HTLC requirements.
3✔
1701
// The minimal sender amount can be searched for by using amt=None.
3✔
1702
func senderAmtBackwardPass(unifiers []*edgeUnifier,
3✔
1703
        amt fn.Option[lnwire.MilliSatoshi],
3✔
1704
        bandwidthHints bandwidthHints) ([]*unifiedEdge, lnwire.MilliSatoshi,
3✔
1705
        error) {
3✔
1706

3✔
UNCOV
1707
        if len(unifiers) == 0 {
×
UNCOV
1708
                return nil, 0, fmt.Errorf("no unifiers provided")
×
1709
        }
1710

1711
        var unifiedEdges = make([]*unifiedEdge, len(unifiers))
1712

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

3✔
1716
        // incomingAmt tracks the amount that is forwarded on the edges of a
3✔
1717
        // route. The last hop only forwards the amount that the receiver should
3✔
1718
        // receive, as there are no fees paid to the last node.
3✔
1719
        // For minimum amount routes, aim to deliver at least 1 msat to
3✔
1720
        // the destination. There are nodes in the wild that have a
3✔
1721
        // min_htlc channel policy of zero, which could lead to a zero
3✔
1722
        // amount payment being made.
3✔
1723
        incomingAmt := amt.UnwrapOr(1)
3✔
1724

3✔
1725
        // If using min amt, increase the amount if needed to fulfill min HTLC
3✔
1726
        // requirements.
3✔
1727
        if amt.IsNone() {
1728
                min := edgeUnifier.minAmt()
1729
                if min > incomingAmt {
3✔
1730
                        incomingAmt = min
1731
                }
1732
        }
1733

1734
        // Get an edge for the specific amount that we want to forward.
UNCOV
1735
        edge := edgeUnifier.getEdge(incomingAmt, bandwidthHints, 0)
×
UNCOV
1736
        if edge == nil {
×
UNCOV
1737
                log.Errorf("Cannot find policy with amt=%v "+
×
UNCOV
1738
                        "for hop %v", incomingAmt, len(unifiers)-1)
×
UNCOV
1739

×
1740
                return nil, 0, ErrNoChannel{position: len(unifiers) - 1}
UNCOV
1741
        }
×
UNCOV
1742

×
UNCOV
1743
        unifiedEdges[len(unifiers)-1] = edge
×
UNCOV
1744

×
UNCOV
1745
        // Handle the rest of the route except the last hop.
×
UNCOV
1746
        for i := len(unifiers) - 2; i >= 0; i-- {
×
UNCOV
1747
                edgeUnifier = unifiers[i]
×
1748

1749
                // If using min amt, increase the amount if needed to fulfill
1750
                // min HTLC requirements.
1751
                if amt.IsNone() {
1752
                        min := edgeUnifier.minAmt()
UNCOV
1753
                        if min > incomingAmt {
×
UNCOV
1754
                                incomingAmt = min
×
UNCOV
1755
                        }
×
UNCOV
1756
                }
×
UNCOV
1757

×
UNCOV
1758
                // A --current hop-- B --next hop: incomingAmt-- C
×
UNCOV
1759
                // The outbound fee paid to the current end node B is based on
×
UNCOV
1760
                // the amount that the next hop forwards and B's policy for that
×
1761
                // hop.
×
1762
                outboundFee := unifiedEdges[i+1].policy.ComputeFee(
×
1763
                        incomingAmt,
×
1764
                )
×
1765

×
1766
                netAmount := incomingAmt + outboundFee
1767

UNCOV
1768
                // We need to select an edge that can forward the requested
×
1769
                // amount.
1770
                edge = edgeUnifier.getEdge(
1771
                        netAmount, bandwidthHints, outboundFee,
1772
                )
1773
                if edge == nil {
1774
                        return nil, 0, ErrNoChannel{position: i}
UNCOV
1775
                }
×
UNCOV
1776

×
UNCOV
1777
                // The fee paid to B depends on the current hop's inbound fee
×
UNCOV
1778
                // policy and on the outbound fee for the next hop as any
×
UNCOV
1779
                // inbound fee discount is capped by the outbound fee such that
×
UNCOV
1780
                // the total fee for B can't become negative.
×
UNCOV
1781
                inboundFee := calcCappedInboundFee(edge, netAmount, outboundFee)
×
UNCOV
1782

×
UNCOV
1783
                fee := lnwire.MilliSatoshi(int64(outboundFee) + inboundFee)
×
UNCOV
1784

×
UNCOV
1785
                log.Tracef("Select channel %v at position %v",
×
UNCOV
1786
                        edge.policy.ChannelID, i)
×
UNCOV
1787

×
UNCOV
1788
                // Finally, we update the amount that needs to flow into node B
×
1789
                // from A, which is the next hop's forwarding amount plus the
UNCOV
1790
                // fee for B: A --current hop: incomingAmt-- B --next hop-- C
×
1791
                incomingAmt += fee
1792

1793
                unifiedEdges[i] = edge
1794
        }
1795

1796
        return unifiedEdges, incomingAmt, nil
UNCOV
1797
}
×
UNCOV
1798

×
UNCOV
1799
// receiverAmtForwardPass returns the amount that a receiver will receive after
×
UNCOV
1800
// deducting all fees from the sender amount.
×
UNCOV
1801
func receiverAmtForwardPass(runningAmt lnwire.MilliSatoshi,
×
UNCOV
1802
        unifiedEdges []*unifiedEdge) (lnwire.MilliSatoshi, error) {
×
UNCOV
1803

×
UNCOV
1804
        if len(unifiedEdges) == 0 {
×
UNCOV
1805
                return 0, fmt.Errorf("no edges to forward through")
×
UNCOV
1806
        }
×
UNCOV
1807

×
UNCOV
1808
        inEdge := unifiedEdges[0]
×
UNCOV
1809
        if !inEdge.amtInRange(runningAmt) {
×
UNCOV
1810
                log.Errorf("Amount %v not in range for hop index %v",
×
UNCOV
1811
                        runningAmt, 0)
×
UNCOV
1812

×
UNCOV
1813
                return 0, ErrNoChannel{position: 0}
×
UNCOV
1814
        }
×
UNCOV
1815

×
UNCOV
1816
        // Now that we arrived at the start of the route and found out the route
×
UNCOV
1817
        // total amount, we make a forward pass. Because the amount may have
×
UNCOV
1818
        // been increased in the backward pass, fees need to be recalculated and
×
UNCOV
1819
        // amount ranges re-checked.
×
UNCOV
1820
        for i := 1; i < len(unifiedEdges); i++ {
×
UNCOV
1821
                inEdge := unifiedEdges[i-1]
×
UNCOV
1822
                outEdge := unifiedEdges[i]
×
UNCOV
1823

×
UNCOV
1824
                // Decrease the amount to send while going forward.
×
UNCOV
1825
                runningAmt = outgoingFromIncoming(runningAmt, inEdge, outEdge)
×
UNCOV
1826

×
UNCOV
1827
                if !outEdge.amtInRange(runningAmt) {
×
UNCOV
1828
                        log.Errorf("Amount %v not in range for hop index %v",
×
UNCOV
1829
                                runningAmt, i)
×
UNCOV
1830

×
UNCOV
1831
                        return 0, ErrNoChannel{position: i}
×
UNCOV
1832
                }
×
UNCOV
1833
        }
×
UNCOV
1834

×
UNCOV
1835
        return runningAmt, nil
×
UNCOV
1836
}
×
UNCOV
1837

×
UNCOV
1838
// incomingFromOutgoing computes the incoming amount based on the outgoing
×
UNCOV
1839
// amount by adding fees to the outgoing amount, replicating the path finding
×
UNCOV
1840
// and routing process, see also CheckHtlcForward.
×
UNCOV
1841
func incomingFromOutgoing(outgoingAmt lnwire.MilliSatoshi,
×
UNCOV
1842
        incoming, outgoing *unifiedEdge) lnwire.MilliSatoshi {
×
UNCOV
1843

×
UNCOV
1844
        outgoingFee := outgoing.policy.ComputeFee(outgoingAmt)
×
UNCOV
1845

×
UNCOV
1846
        // Net amount is the amount the inbound fees are calculated with.
×
UNCOV
1847
        netAmount := outgoingAmt + outgoingFee
×
UNCOV
1848

×
UNCOV
1849
        inboundFee := incoming.inboundFees.CalcFee(netAmount)
×
UNCOV
1850

×
UNCOV
1851
        // The inbound fee is not allowed to reduce the incoming amount below
×
UNCOV
1852
        // the outgoing amount.
×
UNCOV
1853
        if int64(outgoingFee)+inboundFee < 0 {
×
UNCOV
1854
                return outgoingAmt
×
UNCOV
1855
        }
×
UNCOV
1856

×
UNCOV
1857
        return netAmount + lnwire.MilliSatoshi(inboundFee)
×
UNCOV
1858
}
×
UNCOV
1859

×
UNCOV
1860
// outgoingFromIncoming computes the outgoing amount based on the incoming
×
UNCOV
1861
// amount by subtracting fees from the incoming amount. Note that this is not
×
UNCOV
1862
// exactly the inverse of incomingFromOutgoing, because of some rounding.
×
UNCOV
1863
func outgoingFromIncoming(incomingAmt lnwire.MilliSatoshi,
×
UNCOV
1864
        incoming, outgoing *unifiedEdge) lnwire.MilliSatoshi {
×
UNCOV
1865

×
UNCOV
1866
        // Convert all quantities to big.Int to be able to hande negative
×
UNCOV
1867
        // values. The formulas to compute the outgoing amount involve terms
×
UNCOV
1868
        // with PPM*PPM*A, which can easily overflow an int64.
×
UNCOV
1869
        A := big.NewInt(int64(incomingAmt))
×
UNCOV
1870
        Ro := big.NewInt(int64(outgoing.policy.FeeProportionalMillionths))
×
UNCOV
1871
        Bo := big.NewInt(int64(outgoing.policy.FeeBaseMSat))
×
UNCOV
1872
        Ri := big.NewInt(int64(incoming.inboundFees.Rate))
×
UNCOV
1873
        Bi := big.NewInt(int64(incoming.inboundFees.Base))
×
UNCOV
1874
        PPM := big.NewInt(1_000_000)
×
UNCOV
1875

×
UNCOV
1876
        // The following discussion was contributed by user feelancer21, see
×
UNCOV
1877
        //nolint:ll
×
UNCOV
1878
        // https://github.com/feelancer21/lnd/commit/f6f05fa930985aac0d27c3f6681aada1b599162a.
×
UNCOV
1879

×
UNCOV
1880
        // The incoming amount Ai based on the outgoing amount Ao is computed by
×
UNCOV
1881
        // Ai = max(Ai(Ao), Ao), which caps the incoming amount such that the
×
UNCOV
1882
        // total node fee (Ai - Ao) is non-negative. This is commonly enforced
×
UNCOV
1883
        // by routing nodes.
×
UNCOV
1884

×
UNCOV
1885
        // The function Ai(Ao) is given by:
×
UNCOV
1886
        // Ai(Ao) = (Ao + Bo + Ro/PPM) + (Bi + (Ao + Ro/PPM + Bo)*Ri/PPM), where
×
UNCOV
1887
        // the first term is the net amount (the outgoing amount plus the
×
UNCOV
1888
        // outbound fee), and the second is the inbound fee computed based on
×
UNCOV
1889
        // the net amount.
×
UNCOV
1890

×
UNCOV
1891
        // Ai(Ao) can potentially become more negative in absolute value than
×
UNCOV
1892
        // Ao, which is why the above mentioned capping is needed. We can
×
UNCOV
1893
        // abbreviate Ai(Ao) with Ai(Ao) = m*Ao + n, where m and n are:
×
1894
        // m := (1 + Ro/PPM) * (1 + Ri/PPM)
1895
        // n := Bi + Bo*(1 + Ri/PPM)
1896

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

1903
        // If we want to compute the incoming amount based on the outgoing
UNCOV
1904
        // amount, which is the reverse problem, we need to solve Ai =
×
UNCOV
1905
        // max(Ai(Ao), Ao) for Ao(Ai). Given an incoming amount A,
×
UNCOV
1906
        // we look for an Ao such that A = max(Ai(Ao), Ao).
×
UNCOV
1907

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

×
UNCOV
1912
        // first case: Ai(Ao) <= Ao:
×
UNCOV
1913
        // Therefore, A = max(Ai(Ao), Ao) = Ao, we find Ao = A.
×
UNCOV
1914
        // This also leads to Ai(A) <= A by substitution into the condition.
×
UNCOV
1915

×
UNCOV
1916
        // second case: Ai(Ao) > Ao:
×
UNCOV
1917
        // Therefore, A = max(Ai(Ao), Ao) = Ai(Ao) = m*Ao + n. Solving for Ao
×
UNCOV
1918
        // gives Ao = (A - n)/m.
×
UNCOV
1919
        //
×
UNCOV
1920
        // We know
×
UNCOV
1921
        // Ai(Ao) > Ao  <=>  A = Ai(Ao) > Ao = (A - n)/m,
×
UNCOV
1922
        // so A > (A - n)/m.
×
UNCOV
1923
        //
×
UNCOV
1924
        // **Assuming m > 0**, by multiplying with m, we can transform this to
×
UNCOV
1925
        // A * m + n > A.
×
UNCOV
1926
        //
×
UNCOV
1927
        // We know Ai(A) = A*m + n, therefore Ai(A) > A.
×
UNCOV
1928
        //
×
UNCOV
1929
        // This means that if we apply the incoming amount calculation to the
×
UNCOV
1930
        // **incoming** amount, and this condition holds, then we know that we
×
UNCOV
1931
        // deal with the second case, being able to compute the outgoing amount
×
UNCOV
1932
        // based off the formula Ao = (A - n)/m, otherwise we will just return
×
UNCOV
1933
        // the incoming amount.
×
UNCOV
1934

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

UNCOV
1939
        // We compute m in terms of big.Int to be safe from overflows and to be
×
UNCOV
1940
        // consistent with later calculations.
×
UNCOV
1941
        // m := (PPM*PPM + Ri*PPM + Ro*PPM + Ro*Ri)/(PPM*PPM)
×
UNCOV
1942

×
UNCOV
1943
        // Compute terms in (PPM*PPM + Ri*PPM + Ro*PPM + Ro*Ri).
×
UNCOV
1944
        m1 := new(big.Int).Mul(PPM, PPM)
×
UNCOV
1945
        m2 := new(big.Int).Mul(Ri, PPM)
×
UNCOV
1946
        m3 := new(big.Int).Mul(Ro, PPM)
×
UNCOV
1947
        m4 := new(big.Int).Mul(Ro, Ri)
×
UNCOV
1948

×
UNCOV
1949
        // Add up terms m1..m4.
×
UNCOV
1950
        m := big.NewInt(0)
×
UNCOV
1951
        m.Add(m, m1)
×
UNCOV
1952
        m.Add(m, m2)
×
UNCOV
1953
        m.Add(m, m3)
×
UNCOV
1954
        m.Add(m, m4)
×
UNCOV
1955

×
UNCOV
1956
        // Since we compare to 0, we can multiply by PPM*PPM to avoid the
×
UNCOV
1957
        // division.
×
UNCOV
1958
        if m.Int64() <= 0 {
×
UNCOV
1959
                return incomingAmt
×
UNCOV
1960
        }
×
UNCOV
1961

×
UNCOV
1962
        // In order to decide if the total fee is negative, we apply the fee
×
UNCOV
1963
        // to the *incoming* amount as mentioned before.
×
UNCOV
1964

×
UNCOV
1965
        // We compute the test amount in terms of big.Int to be safe from
×
UNCOV
1966
        // overflows and to be consistent later calculations.
×
UNCOV
1967
        // testAmtF := A*m + n =
×
UNCOV
1968
        // = A + Bo + Bi + (PPM*(A*Ri + A*Ro + Ro*Ri) + A*Ri*Ro)/(PPM*PPM)
×
UNCOV
1969

×
UNCOV
1970
        // Compute terms in (A*Ri + A*Ro + Ro*Ri).
×
UNCOV
1971
        t1 := new(big.Int).Mul(A, Ri)
×
UNCOV
1972
        t2 := new(big.Int).Mul(A, Ro)
×
UNCOV
1973
        t3 := new(big.Int).Mul(Ro, Ri)
×
UNCOV
1974

×
UNCOV
1975
        // Sum up terms t1-t3.
×
UNCOV
1976
        t4 := big.NewInt(0)
×
UNCOV
1977
        t4.Add(t4, t1)
×
UNCOV
1978
        t4.Add(t4, t2)
×
UNCOV
1979
        t4.Add(t4, t3)
×
UNCOV
1980

×
UNCOV
1981
        // Compute PPM*(A*Ri + A*Ro + Ro*Ri).
×
UNCOV
1982
        t6 := new(big.Int).Mul(PPM, t4)
×
UNCOV
1983

×
UNCOV
1984
        // Compute A*Ri*Ro.
×
UNCOV
1985
        t7 := new(big.Int).Mul(A, Ri)
×
1986
        t7.Mul(t7, Ro)
1987

1988
        // Compute (PPM*(A*Ri + A*Ro + Ro*Ri) + A*Ri*Ro)/(PPM*PPM).
UNCOV
1989
        num := new(big.Int).Add(t6, t7)
×
1990
        denom := new(big.Int).Mul(PPM, PPM)
1991
        fraction := new(big.Int).Div(num, denom)
1992

1993
        // Sum up all terms.
1994
        testAmt := big.NewInt(0)
1995
        testAmt.Add(testAmt, A)
1996
        testAmt.Add(testAmt, Bo)
1997
        testAmt.Add(testAmt, Bi)
1998
        testAmt.Add(testAmt, fraction)
1999

2000
        // Protect against negative values for the integer cast to Msat.
2001
        if testAmt.Int64() < 0 {
2002
                return incomingAmt
2003
        }
2004

2005
        // If the second case holds, we have to compute the outgoing amount.
2006
        if lnwire.MilliSatoshi(testAmt.Int64()) > incomingAmt {
2007
                // Compute the outgoing amount by integer ceiling division. This
2008
                // precision is needed because PPM*PPM*A and other terms can
2009
                // easily overflow with int64, which happens with about
2010
                // A = 10_000 sat.
2011

2012
                // out := (A - n) / m = numerator / denominator
2013
                // numerator := PPM*(PPM*(A - Bo - Bi) - Bo*Ri)
2014
                // denominator := PPM*(PPM + Ri + Ro) + Ri*Ro
2015

2016
                var numerator big.Int
2017

2018
                // Compute (A - Bo - Bi).
2019
                temp1 := new(big.Int).Sub(A, Bo)
2020
                temp2 := new(big.Int).Sub(temp1, Bi)
2021

2022
                // Compute terms in (PPM*(A - Bo - Bi) - Bo*Ri).
2023
                temp3 := new(big.Int).Mul(PPM, temp2)
2024
                temp4 := new(big.Int).Mul(Bo, Ri)
2025

2026
                // Compute PPM*(PPM*(A - Bo - Bi) - Bo*Ri)
2027
                temp5 := new(big.Int).Sub(temp3, temp4)
2028
                numerator.Mul(PPM, temp5)
2029

2030
                var denominator big.Int
2031

2032
                // Compute (PPM + Ri + Ro).
2033
                temp1 = new(big.Int).Add(PPM, Ri)
2034
                temp2 = new(big.Int).Add(temp1, Ro)
2035

2036
                // Compute PPM*(PPM + Ri + Ro) + Ri*Ro.
2037
                temp3 = new(big.Int).Mul(PPM, temp2)
2038
                temp4 = new(big.Int).Mul(Ri, Ro)
2039
                denominator.Add(temp3, temp4)
2040

2041
                // We overestimate the outgoing amount by taking the ceiling of
2042
                // the division. This means that we may round slightly up by a
2043
                // MilliSatoshi, but this helps to ensure that we don't hit min
2044
                // HTLC constrains in the context of finding the minimum amount
2045
                // of a route.
2046
                // ceil = floor((numerator + denominator - 1) / denominator)
2047
                ceil := new(big.Int).Add(&numerator, &denominator)
2048
                ceil.Sub(ceil, big.NewInt(1))
2049
                ceil.Div(ceil, &denominator)
2050

2051
                return lnwire.MilliSatoshi(ceil.Int64())
2052
        }
2053

2054
        // Otherwise the inbound fee made up for the outbound fee, which is why
2055
        // we just return the incoming amount.
2056
        return incomingAmt
2057
}
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