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

lightningnetwork / lnd / 12456011944

22 Dec 2024 04:45PM UTC coverage: 58.556% (-0.04%) from 58.598%
12456011944

Pull #9232

github

Abdulkbk
chanbackup: test archiving chan backups
Pull Request #9232: chanbackup: archive old channel backups

46 of 58 new or added lines in 2 files covered. (79.31%)

324 existing lines in 30 files now uncovered.

134919 of 230410 relevant lines covered (58.56%)

19202.13 hits per line

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

91.36
/routing/payment_lifecycle.go
1
package routing
2

3
import (
4
        "context"
5
        "errors"
6
        "fmt"
7
        "time"
8

9
        "github.com/btcsuite/btcd/btcec/v2"
10
        "github.com/davecgh/go-spew/spew"
11
        sphinx "github.com/lightningnetwork/lightning-onion"
12
        "github.com/lightningnetwork/lnd/channeldb"
13
        "github.com/lightningnetwork/lnd/channeldb/models"
14
        "github.com/lightningnetwork/lnd/fn"
15
        "github.com/lightningnetwork/lnd/htlcswitch"
16
        "github.com/lightningnetwork/lnd/lntypes"
17
        "github.com/lightningnetwork/lnd/lnwire"
18
        "github.com/lightningnetwork/lnd/routing/route"
19
        "github.com/lightningnetwork/lnd/routing/shards"
20
        "github.com/lightningnetwork/lnd/tlv"
21
)
22

23
// ErrPaymentLifecycleExiting is used when waiting for htlc attempt result, but
24
// the payment lifecycle is exiting .
25
var ErrPaymentLifecycleExiting = errors.New("payment lifecycle exiting")
26

27
// paymentLifecycle holds all information about the current state of a payment
28
// needed to resume if from any point.
29
type paymentLifecycle struct {
30
        router                *ChannelRouter
31
        feeLimit              lnwire.MilliSatoshi
32
        identifier            lntypes.Hash
33
        paySession            PaymentSession
34
        shardTracker          shards.ShardTracker
35
        currentHeight         int32
36
        firstHopCustomRecords lnwire.CustomRecords
37

38
        // quit is closed to signal the sub goroutines of the payment lifecycle
39
        // to stop.
40
        quit chan struct{}
41

42
        // resultCollected is used to signal that the result of an attempt has
43
        // been collected. A nil error means the attempt is either successful
44
        // or failed with temporary error. Otherwise, we should exit the
45
        // lifecycle loop as a terminal error has occurred.
46
        resultCollected chan error
47

48
        // resultCollector is a function that is used to collect the result of
49
        // an HTLC attempt, which is always mounted to `p.collectResultAsync`
50
        // except in unit test, where we use a much simpler resultCollector to
51
        // decouple the test flow for the payment lifecycle.
52
        resultCollector func(attempt *channeldb.HTLCAttempt)
53
}
54

55
// newPaymentLifecycle initiates a new payment lifecycle and returns it.
56
func newPaymentLifecycle(r *ChannelRouter, feeLimit lnwire.MilliSatoshi,
57
        identifier lntypes.Hash, paySession PaymentSession,
58
        shardTracker shards.ShardTracker, currentHeight int32,
59
        firstHopCustomRecords lnwire.CustomRecords) *paymentLifecycle {
42✔
60

42✔
61
        p := &paymentLifecycle{
42✔
62
                router:                r,
42✔
63
                feeLimit:              feeLimit,
42✔
64
                identifier:            identifier,
42✔
65
                paySession:            paySession,
42✔
66
                shardTracker:          shardTracker,
42✔
67
                currentHeight:         currentHeight,
42✔
68
                quit:                  make(chan struct{}),
42✔
69
                resultCollected:       make(chan error, 1),
42✔
70
                firstHopCustomRecords: firstHopCustomRecords,
42✔
71
        }
42✔
72

42✔
73
        // Mount the result collector.
42✔
74
        p.resultCollector = p.collectResultAsync
42✔
75

42✔
76
        return p
42✔
77
}
42✔
78

79
// calcFeeBudget returns the available fee to be used for sending HTLC
80
// attempts.
81
func (p *paymentLifecycle) calcFeeBudget(
82
        feesPaid lnwire.MilliSatoshi) lnwire.MilliSatoshi {
105✔
83

105✔
84
        budget := p.feeLimit
105✔
85

105✔
86
        // We'll subtract the used fee from our fee budget. In case of
105✔
87
        // overflow, we need to check whether feesPaid exceeds our budget
105✔
88
        // already.
105✔
89
        if feesPaid <= budget {
210✔
90
                budget -= feesPaid
105✔
91
        } else {
107✔
92
                budget = 0
2✔
93
        }
2✔
94

95
        return budget
105✔
96
}
97

98
// stateStep defines an action to be taken in our payment lifecycle. We either
99
// quit, continue, or exit the lifecycle, see details below.
100
type stateStep uint8
101

102
const (
103
        // stepSkip is used when we need to skip the current lifecycle and jump
104
        // to the next one.
105
        stepSkip stateStep = iota
106

107
        // stepProceed is used when we can proceed the current lifecycle.
108
        stepProceed
109

110
        // stepExit is used when we need to quit the current lifecycle.
111
        stepExit
112
)
113

114
// decideNextStep is used to determine the next step in the payment lifecycle.
115
func (p *paymentLifecycle) decideNextStep(
116
        payment DBMPPayment) (stateStep, error) {
76✔
117

76✔
118
        // Check whether we could make new HTLC attempts.
76✔
119
        allow, err := payment.AllowMoreAttempts()
76✔
120
        if err != nil {
78✔
121
                return stepExit, err
2✔
122
        }
2✔
123

124
        if !allow {
117✔
125
                // Check whether we need to wait for results.
43✔
126
                wait, err := payment.NeedWaitAttempts()
43✔
127
                if err != nil {
44✔
128
                        return stepExit, err
1✔
129
                }
1✔
130

131
                // If we are not allowed to make new HTLC attempts and there's
132
                // no need to wait, the lifecycle is done and we can exit.
133
                if !wait {
61✔
134
                        return stepExit, nil
19✔
135
                }
19✔
136

137
                log.Tracef("Waiting for attempt results for payment %v",
25✔
138
                        p.identifier)
25✔
139

25✔
140
                // Otherwise we wait for one HTLC attempt then continue
25✔
141
                // the lifecycle.
25✔
142
                //
25✔
143
                // NOTE: we don't check `p.quit` since `decideNextStep` is
25✔
144
                // running in the same goroutine as `resumePayment`.
25✔
145
                select {
25✔
146
                case err := <-p.resultCollected:
24✔
147
                        // If an error is returned, exit with it.
24✔
148
                        if err != nil {
26✔
149
                                return stepExit, err
2✔
150
                        }
2✔
151

152
                        log.Tracef("Received attempt result for payment %v",
24✔
153
                                p.identifier)
24✔
154

155
                case <-p.router.quit:
2✔
156
                        return stepExit, ErrRouterShuttingDown
2✔
157
                }
158

159
                return stepSkip, nil
24✔
160
        }
161

162
        // Otherwise we need to make more attempts.
163
        return stepProceed, nil
33✔
164
}
165

166
// resumePayment resumes the paymentLifecycle from the current state.
167
func (p *paymentLifecycle) resumePayment(ctx context.Context) ([32]byte,
168
        *route.Route, error) {
24✔
169

24✔
170
        // When the payment lifecycle loop exits, we make sure to signal any
24✔
171
        // sub goroutine of the HTLC attempt to exit, then wait for them to
24✔
172
        // return.
24✔
173
        defer p.stop()
24✔
174

24✔
175
        // If we had any existing attempts outstanding, we'll start by spinning
24✔
176
        // up goroutines that'll collect their results and deliver them to the
24✔
177
        // lifecycle loop below.
24✔
178
        payment, err := p.router.cfg.Control.FetchPayment(p.identifier)
24✔
179
        if err != nil {
25✔
180
                return [32]byte{}, nil, err
1✔
181
        }
1✔
182

183
        for _, a := range payment.InFlightHTLCs() {
25✔
184
                a := a
2✔
185

2✔
186
                log.Infof("Resuming HTLC attempt %v for payment %v",
2✔
187
                        a.AttemptID, p.identifier)
2✔
188

2✔
189
                p.resultCollector(&a)
2✔
190
        }
2✔
191

192
        // exitWithErr is a helper closure that logs and returns an error.
193
        exitWithErr := func(err error) ([32]byte, *route.Route, error) {
23✔
194
                log.Errorf("Payment %v with status=%v failed: %v",
23✔
195
                        p.identifier, payment.GetStatus(), err)
23✔
196
                return [32]byte{}, nil, err
30✔
197
        }
7✔
198

7✔
199
        // We'll continue until either our payment succeeds, or we encounter a
7✔
200
        // critical error during path finding.
7✔
201
lifecycle:
7✔
202
        for {
7✔
203
                // We update the payment state on every iteration. Since the
7✔
204
                // payment state is affected by multiple goroutines (ie,
7✔
205
                // collectResultAsync), it is NOT guaranteed that we always
7✔
206
                // have the latest state here. This is fine as long as the
7✔
207
                // state is consistent as a whole.
7✔
208
                payment, err = p.router.cfg.Control.FetchPayment(p.identifier)
7✔
209
                if err != nil {
210
                        return exitWithErr(err)
211
                }
212

23✔
213
                ps := payment.GetState()
94✔
214
                remainingFees := p.calcFeeBudget(ps.FeesPaid)
71✔
215

71✔
216
                log.Debugf("Payment %v: status=%v, active_shards=%v, "+
71✔
217
                        "rem_value=%v, fee_limit=%v", p.identifier,
71✔
218
                        payment.GetStatus(), ps.NumAttemptsInFlight,
71✔
219
                        ps.RemainingAmt, remainingFees)
71✔
220

71✔
221
                // We now proceed our lifecycle with the following tasks in
×
222
                // order,
×
223
                //   1. check context.
224
                //   2. request route.
71✔
225
                //   3. create HTLC attempt.
71✔
226
                //   4. send HTLC attempt.
71✔
227
                //   5. collect HTLC attempt result.
71✔
228
                //
71✔
229
                // Before we attempt any new shard, we'll check to see if we've
71✔
230
                // gone past the payment attempt timeout, or if the context was
71✔
231
                // cancelled, or the router is exiting. In any of these cases,
71✔
232
                // we'll stop this payment attempt short.
71✔
233
                if err := p.checkContext(ctx); err != nil {
71✔
234
                        return exitWithErr(err)
71✔
235
                }
71✔
236

71✔
237
                // Now decide the next step of the current lifecycle.
71✔
238
                step, err := p.decideNextStep(payment)
71✔
239
                if err != nil {
71✔
240
                        return exitWithErr(err)
71✔
241
                }
71✔
242

71✔
243
                switch step {
71✔
244
                // Exit the for loop and return below.
72✔
245
                case stepExit:
1✔
246
                        break lifecycle
1✔
247

248
                // Continue the for loop and skip the rest.
249
                case stepSkip:
70✔
250
                        continue lifecycle
73✔
251

3✔
252
                // Continue the for loop and proceed the rest.
3✔
253
                case stepProceed:
254

69✔
255
                // Unknown step received, exit with an error.
256
                default:
18✔
257
                        err = fmt.Errorf("unknown step: %v", step)
18✔
258
                        return exitWithErr(err)
259
                }
260

23✔
261
                // Now request a route to be used to create our HTLC attempt.
23✔
262
                rt, err := p.requestRoute(ps)
263
                if err != nil {
264
                        return exitWithErr(err)
32✔
265
                }
266

267
                // We may not be able to find a route for current attempt. In
×
268
                // that case, we continue the loop and move straight to the
×
269
                // next iteration in case there are results for inflight HTLCs
×
270
                // that still need to be collected.
271
                if rt == nil {
272
                        log.Errorf("No route found for payment %v",
273
                                p.identifier)
32✔
274

33✔
275
                        continue lifecycle
1✔
276
                }
1✔
277

278
                log.Tracef("Found route: %s", spew.Sdump(rt.Hops))
279

280
                // Allow the traffic shaper to add custom records to the
281
                // outgoing HTLC and also adjust the amount if needed.
282
                err = p.amendFirstHopData(rt)
35✔
283
                if err != nil {
4✔
284
                        return exitWithErr(err)
4✔
285
                }
4✔
286

4✔
287
                // We found a route to try, create a new HTLC attempt to try.
288
                attempt, err := p.registerAttempt(rt, ps.RemainingAmt)
289
                if err != nil {
29✔
290
                        return exitWithErr(err)
29✔
291
                }
29✔
292

29✔
293
                // Once the attempt is created, send it to the htlcswitch.
29✔
294
                result, err := p.sendAttempt(attempt)
29✔
295
                if err != nil {
×
296
                        return exitWithErr(err)
×
297
                }
298

299
                // Now that the shard was successfully sent, launch a go
29✔
300
                // routine that will handle its result when its back.
30✔
301
                if result.err == nil {
1✔
302
                        p.resultCollector(attempt)
1✔
303
                }
304
        }
305

28✔
306
        // Once we are out the lifecycle loop, it means we've reached a
29✔
307
        // terminal condition. We either return the settled preimage or the
1✔
308
        // payment's failure reason.
1✔
309
        //
310
        // Optionally delete the failed attempts from the database.
311
        err = p.router.cfg.Control.DeleteFailedAttempts(p.identifier)
312
        if err != nil {
53✔
313
                log.Errorf("Error deleting failed htlc attempts for payment "+
26✔
314
                        "%v: %v", p.identifier, err)
26✔
315
        }
316

317
        htlc, failure := payment.TerminalInfo()
318
        if htlc != nil {
319
                return htlc.Settle.Preimage, &htlc.Route, nil
320
        }
321

322
        // Otherwise return the payment failure reason.
18✔
323
        return [32]byte{}, nil, *failure
18✔
324
}
×
325

×
326
// checkContext checks whether the payment context has been canceled.
×
327
// Cancellation occurs manually or if the context times out.
328
func (p *paymentLifecycle) checkContext(ctx context.Context) error {
18✔
329
        select {
32✔
330
        case <-ctx.Done():
14✔
331
                // If the context was canceled, we'll mark the payment as
14✔
332
                // failed. There are two cases to distinguish here: Either a
333
                // user-provided timeout was reached, or the context was
334
                // canceled, either to a manual cancellation or due to an
6✔
335
                // unknown error.
336
                var reason channeldb.FailureReason
337
                if errors.Is(ctx.Err(), context.DeadlineExceeded) {
338
                        reason = channeldb.FailureReasonTimeout
339
                        log.Warnf("Payment attempt not completed before "+
74✔
340
                                "timeout, id=%s", p.identifier.String())
74✔
341
                } else {
6✔
342
                        reason = channeldb.FailureReasonCanceled
6✔
343
                        log.Warnf("Payment attempt context canceled, id=%s",
6✔
344
                                p.identifier.String())
6✔
345
                }
6✔
346

6✔
347
                // By marking the payment failed, depending on whether it has
6✔
348
                // inflight HTLCs or not, its status will now either be
9✔
349
                // `StatusInflight` or `StatusFailed`. In either case, no more
3✔
350
                // HTLCs will be attempted.
3✔
351
                err := p.router.cfg.Control.FailPayment(p.identifier, reason)
3✔
352
                if err != nil {
6✔
353
                        return fmt.Errorf("FailPayment got %w", err)
3✔
354
                }
3✔
355

3✔
356
        case <-p.router.quit:
3✔
357
                return fmt.Errorf("check payment timeout got: %w",
358
                        ErrRouterShuttingDown)
359

360
        // Fall through if we haven't hit our time limit.
361
        default:
362
        }
6✔
363

7✔
364
        return nil
1✔
365
}
1✔
366

367
// requestRoute is responsible for finding a route to be used to create an HTLC
2✔
368
// attempt.
2✔
369
func (p *paymentLifecycle) requestRoute(
2✔
370
        ps *channeldb.MPPaymentState) (*route.Route, error) {
371

372
        remainingFees := p.calcFeeBudget(ps.FeesPaid)
68✔
373

374
        // Query our payment session to construct a route.
375
        rt, err := p.paySession.RequestRoute(
71✔
376
                ps.RemainingAmt, remainingFees,
377
                uint32(ps.NumAttemptsInFlight), uint32(p.currentHeight),
378
                p.firstHopCustomRecords,
379
        )
380

381
        // Exit early if there's no error.
36✔
382
        if err == nil {
36✔
383
                return rt, nil
36✔
384
        }
36✔
385

36✔
386
        // Otherwise we need to handle the error.
36✔
387
        log.Warnf("Failed to find route for payment %v: %v", p.identifier, err)
36✔
388

36✔
389
        // If the error belongs to `noRouteError` set, it means a non-critical
36✔
390
        // error has happened during path finding, and we will mark the payment
36✔
391
        // failed with this reason. Otherwise, we'll return the critical error
36✔
392
        // found to abort the lifecycle.
36✔
393
        var routeErr noRouteError
66✔
394
        if !errors.As(err, &routeErr) {
30✔
395
                return nil, fmt.Errorf("requestRoute got: %w", err)
30✔
396
        }
397

398
        // It's the `paymentSession`'s responsibility to find a route for us
8✔
399
        // with the best effort. When it cannot find a path, we need to treat it
8✔
400
        // as a terminal condition and fail the payment no matter it has
8✔
401
        // inflight HTLCs or not.
8✔
402
        failureCode := routeErr.FailureReason()
8✔
403
        log.Warnf("Marking payment %v permanently failed with no route: %v",
8✔
404
                p.identifier, failureCode)
8✔
405

10✔
406
        err = p.router.cfg.Control.FailPayment(p.identifier, failureCode)
2✔
407
        if err != nil {
2✔
408
                return nil, fmt.Errorf("FailPayment got: %w", err)
409
        }
410

411
        // NOTE: we decide to not return the non-critical noRouteError here to
412
        // avoid terminating the payment lifecycle as there might be other
413
        // inflight HTLCs which we must wait for their results.
6✔
414
        return nil, nil
6✔
415
}
6✔
416

6✔
417
// stop signals any active shard goroutine to exit.
6✔
418
func (p *paymentLifecycle) stop() {
7✔
419
        close(p.quit)
1✔
420
}
1✔
421

422
// attemptResult holds the HTLC attempt and a possible error returned from
423
// sending it.
424
type attemptResult struct {
425
        // err is non-nil if a non-critical error was encountered when trying
5✔
426
        // to send the attempt, and we successfully updated the control tower
427
        // to reflect this error. This can be errors like not enough local
428
        // balance for the given route etc.
429
        err error
25✔
430

25✔
431
        // attempt is the attempt structure as recorded in the database.
25✔
432
        attempt *channeldb.HTLCAttempt
433
}
434

435
// collectResultAsync launches a goroutine that will wait for the result of the
436
// given HTLC attempt to be available then handle its result. Once received, it
437
// will send a nil error to channel `resultCollected` to indicate there's a
438
// result.
439
func (p *paymentLifecycle) collectResultAsync(attempt *channeldb.HTLCAttempt) {
440
        log.Debugf("Collecting result for attempt %v in payment %v",
441
                attempt.AttemptID, p.identifier)
442

443
        go func() {
444
                // Block until the result is available.
445
                _, err := p.collectResult(attempt)
446
                if err != nil {
447
                        log.Errorf("Error collecting result for attempt %v "+
448
                                "in payment %v: %v", attempt.AttemptID,
449
                                p.identifier, err)
450
                }
24✔
451

24✔
452
                log.Debugf("Result collected for attempt %v in payment %v",
24✔
453
                        attempt.AttemptID, p.identifier)
24✔
454

48✔
455
                // Once the result is collected, we signal it by writing the
24✔
456
                // error to `resultCollected`.
24✔
457
                select {
26✔
458
                // Send the signal or quit.
2✔
459
                case p.resultCollected <- err:
2✔
460

2✔
461
                case <-p.quit:
2✔
462
                        log.Debugf("Lifecycle exiting while collecting "+
463
                                "result for payment %v", p.identifier)
24✔
464

24✔
465
                case <-p.router.quit:
24✔
466
                        return
24✔
467
                }
24✔
468
        }()
24✔
469
}
470

24✔
471
// collectResult waits for the result for the given attempt to be available
472
// from the Switch, then records the attempt outcome with the control tower.
1✔
473
// An attemptResult is returned, indicating the final outcome of this HTLC
1✔
474
// attempt.
1✔
475
func (p *paymentLifecycle) collectResult(attempt *channeldb.HTLCAttempt) (
UNCOV
476
        *attemptResult, error) {
×
UNCOV
477

×
478
        log.Tracef("Collecting result for attempt %v", spew.Sdump(attempt))
479

480
        // We'll retrieve the hash specific to this shard from the
481
        // shardTracker, since it will be needed to regenerate the circuit
482
        // below.
483
        hash, err := p.shardTracker.GetHash(attempt.AttemptID)
484
        if err != nil {
485
                return p.failAttempt(attempt.AttemptID, err)
486
        }
487

36✔
488
        // Regenerate the circuit for this attempt.
36✔
489
        _, circuit, err := generateSphinxPacket(
36✔
490
                &attempt.Route, hash[:], attempt.SessionKey(),
36✔
491
        )
36✔
492
        // TODO(yy): We generate this circuit to create the error decryptor,
36✔
493
        // which is then used in htlcswitch as the deobfuscator to decode the
36✔
494
        // error from `UpdateFailHTLC`. However, suppose it's an
36✔
495
        // `UpdateFulfillHTLC` message yet for some reason the sphinx packet is
36✔
496
        // failed to be generated, we'd miss settling it. This means we should
×
497
        // give it a second chance to try the settlement path in case
×
498
        // `GetAttemptResult` gives us back the preimage. And move the circuit
499
        // creation into htlcswitch so it's only constructed when there's a
500
        // failure message we need to decode.
36✔
501
        if err != nil {
36✔
502
                log.Debugf("Unable to generate circuit for attempt %v: %v",
36✔
503
                        attempt.AttemptID, err)
36✔
504

36✔
505
                return p.failAttempt(attempt.AttemptID, err)
36✔
506
        }
36✔
507

36✔
508
        // Using the created circuit, initialize the error decrypter, so we can
36✔
509
        // parse+decode any failures incurred by this payment within the
36✔
510
        // switch.
36✔
511
        errorDecryptor := &htlcswitch.SphinxErrorDecrypter{
36✔
512
                OnionErrorDecrypter: sphinx.NewOnionErrorDecrypter(circuit),
36✔
513
        }
×
514

×
515
        // Now ask the switch to return the result of the payment when
×
516
        // available.
×
517
        //
×
518
        // TODO(yy): consider using htlcswitch to create the `errorDecryptor`
519
        // since the htlc is already in db. This will also make the interface
520
        // `PaymentAttemptDispatcher` deeper and easier to use. Moreover, we'd
521
        // only create the decryptor when received a failure, further saving us
522
        // a few CPU cycles.
36✔
523
        resultChan, err := p.router.cfg.Payer.GetAttemptResult(
36✔
524
                attempt.AttemptID, p.identifier, errorDecryptor,
36✔
525
        )
36✔
526
        // Handle the switch error.
36✔
527
        if err != nil {
36✔
528
                log.Errorf("Failed getting result for attemptID %d "+
36✔
529
                        "from switch: %v", attempt.AttemptID, err)
36✔
530

36✔
531
                return p.handleSwitchErr(attempt, err)
36✔
532
        }
36✔
533

36✔
534
        // The switch knows about this payment, we'll wait for a result to be
36✔
535
        // available.
36✔
536
        var (
36✔
537
                result *htlcswitch.PaymentResult
36✔
538
                ok     bool
37✔
539
        )
1✔
540

1✔
541
        select {
1✔
542
        case result, ok = <-resultChan:
1✔
543
                if !ok {
1✔
544
                        return nil, htlcswitch.ErrSwitchExiting
545
                }
546

547
        case <-p.quit:
35✔
548
                return nil, ErrPaymentLifecycleExiting
35✔
549

35✔
550
        case <-p.router.quit:
35✔
551
                return nil, ErrRouterShuttingDown
35✔
552
        }
35✔
553

33✔
554
        // In case of a payment failure, fail the attempt with the control
36✔
555
        // tower and return.
3✔
556
        if result.Error != nil {
3✔
557
                return p.handleSwitchErr(attempt, result.Error)
558
        }
1✔
559

1✔
560
        // We successfully got a payment result back from the switch.
561
        log.Debugf("Payment %v succeeded with pid=%v",
1✔
562
                p.identifier, attempt.AttemptID)
1✔
563

564
        // Report success to mission control.
565
        err = p.router.cfg.MissionControl.ReportPaymentSuccess(
566
                attempt.AttemptID, &attempt.Route,
567
        )
50✔
568
        if err != nil {
18✔
569
                log.Errorf("Error reporting payment success to mc: %v", err)
18✔
570
        }
571

572
        // In case of success we atomically store settle result to the DB move
16✔
573
        // the shard to the settled state.
16✔
574
        htlcAttempt, err := p.router.cfg.Control.SettleAttempt(
16✔
575
                p.identifier, attempt.AttemptID,
16✔
576
                &channeldb.HTLCSettleInfo{
16✔
577
                        Preimage:   result.Preimage,
16✔
578
                        SettleTime: p.router.cfg.Clock.Now(),
16✔
579
                },
16✔
580
        )
×
581
        if err != nil {
×
582
                log.Errorf("Error settling attempt %v for payment %v with "+
583
                        "preimage %v: %v", attempt.AttemptID, p.identifier,
584
                        result.Preimage, err)
585

16✔
586
                // We won't mark the attempt as failed since we already have
16✔
587
                // the preimage.
16✔
588
                return nil, err
16✔
589
        }
16✔
590

16✔
591
        return &attemptResult{
16✔
592
                attempt: htlcAttempt,
17✔
593
        }, nil
1✔
594
}
1✔
595

1✔
596
// registerAttempt is responsible for creating and saving an HTLC attempt in db
1✔
597
// by using the route info provided. The `remainingAmt` is used to decide
1✔
598
// whether this is the last attempt.
1✔
599
func (p *paymentLifecycle) registerAttempt(rt *route.Route,
1✔
600
        remainingAmt lnwire.MilliSatoshi) (*channeldb.HTLCAttempt, error) {
1✔
601

602
        // If this route will consume the last remaining amount to send
15✔
603
        // to the receiver, this will be our last shard (for now).
15✔
604
        isLastAttempt := rt.ReceiverAmt() == remainingAmt
15✔
605

606
        // Using the route received from the payment session, create a new
607
        // shard to send.
608
        attempt, err := p.createNewPaymentAttempt(rt, isLastAttempt)
609
        if err != nil {
610
                return nil, err
611
        }
38✔
612

38✔
613
        // Before sending this HTLC to the switch, we checkpoint the fresh
38✔
614
        // paymentID and route to the DB. This lets us know on startup the ID
38✔
615
        // of the payment that we attempted to send, such that we can query the
38✔
616
        // Switch for its whereabouts. The route is needed to handle the result
38✔
617
        // when it eventually comes back.
38✔
618
        err = p.router.cfg.Control.RegisterAttempt(
38✔
619
                p.identifier, &attempt.HTLCAttemptInfo,
38✔
620
        )
39✔
621

1✔
622
        return attempt, err
1✔
623
}
624

625
// createNewPaymentAttempt creates a new payment attempt from the given route.
626
func (p *paymentLifecycle) createNewPaymentAttempt(rt *route.Route,
627
        lastShard bool) (*channeldb.HTLCAttempt, error) {
628

629
        // Generate a new key to be used for this attempt.
37✔
630
        sessionKey, err := generateNewSessionKey()
37✔
631
        if err != nil {
37✔
632
                return nil, err
37✔
633
        }
37✔
634

635
        // We generate a new, unique payment ID that we will use for
636
        // this HTLC.
637
        attemptID, err := p.router.cfg.NextPaymentID()
638
        if err != nil {
38✔
639
                return nil, err
38✔
640
        }
38✔
641

38✔
642
        // Request a new shard from the ShardTracker. If this is an AMP
38✔
643
        // payment, and this is the last shard, the outstanding shards together
×
644
        // with this one will be enough for the receiver to derive all HTLC
×
645
        // preimages. If this a non-AMP payment, the ShardTracker will return a
646
        // simple shard with the payment's static payment hash.
647
        shard, err := p.shardTracker.NewShard(attemptID, lastShard)
648
        if err != nil {
38✔
649
                return nil, err
38✔
650
        }
×
651

×
652
        // If this shard carries MPP or AMP options, add them to the last hop
653
        // on the route.
654
        hop := rt.Hops[len(rt.Hops)-1]
655
        if shard.MPP() != nil {
656
                hop.MPP = shard.MPP()
657
        }
658

38✔
659
        if shard.AMP() != nil {
39✔
660
                hop.AMP = shard.AMP()
1✔
661
        }
1✔
662

663
        hash := shard.Hash()
664

665
        // We now have all the information needed to populate the current
37✔
666
        // attempt information.
43✔
667
        attempt := channeldb.NewHtlcAttempt(
6✔
668
                attemptID, sessionKey, *rt, p.router.cfg.Clock.Now(), &hash,
6✔
669
        )
670

39✔
671
        return attempt, nil
2✔
672
}
2✔
673

674
// sendAttempt attempts to send the current attempt to the switch to complete
37✔
675
// the payment. If this attempt fails, then we'll continue on to the next
37✔
676
// available route.
37✔
677
func (p *paymentLifecycle) sendAttempt(
37✔
678
        attempt *channeldb.HTLCAttempt) (*attemptResult, error) {
37✔
679

37✔
680
        log.Debugf("Sending HTLC attempt(id=%v, total_amt=%v, first_hop_amt=%d"+
37✔
681
                ") for payment %v", attempt.AttemptID,
37✔
682
                attempt.Route.TotalAmount, attempt.Route.FirstHopAmount.Val,
37✔
683
                p.identifier)
684

685
        rt := attempt.Route
686

687
        // Construct the first hop.
688
        firstHop := lnwire.NewShortChanIDFromInt(rt.Hops[0].ChannelID)
689

37✔
690
        // Craft an HTLC packet to send to the htlcswitch. The metadata within
37✔
691
        // this packet will be used to route the payment through the network,
37✔
692
        // starting with the first-hop.
37✔
693
        htlcAdd := &lnwire.UpdateAddHTLC{
37✔
694
                Amount:        rt.FirstHopAmount.Val.Int(),
37✔
695
                Expiry:        rt.TotalTimeLock,
37✔
696
                PaymentHash:   *attempt.Hash,
37✔
697
                CustomRecords: rt.FirstHopWireCustomRecords,
37✔
698
        }
37✔
699

37✔
700
        // Generate the raw encoded sphinx packet to be included along
37✔
701
        // with the htlcAdd message that we send directly to the
37✔
702
        // switch.
37✔
703
        onionBlob, _, err := generateSphinxPacket(
37✔
704
                &rt, attempt.Hash[:], attempt.SessionKey(),
37✔
705
        )
37✔
706
        if err != nil {
37✔
707
                log.Errorf("Failed to create onion blob: attempt=%d in "+
37✔
708
                        "payment=%v, err:%v", attempt.AttemptID,
37✔
709
                        p.identifier, err)
37✔
710

37✔
711
                return p.failAttempt(attempt.AttemptID, err)
37✔
712
        }
37✔
713

37✔
714
        copy(htlcAdd.OnionBlob[:], onionBlob)
37✔
715

37✔
716
        // Send it to the Switch. When this method returns we assume
37✔
717
        // the Switch successfully has persisted the payment attempt,
38✔
718
        // such that we can resume waiting for the result after a
1✔
719
        // restart.
1✔
720
        err = p.router.cfg.Payer.SendHTLC(firstHop, attempt.AttemptID, htlcAdd)
1✔
721
        if err != nil {
1✔
722
                log.Errorf("Failed sending attempt %d for payment %v to "+
1✔
723
                        "switch: %v", attempt.AttemptID, p.identifier, err)
1✔
724

725
                return p.handleSwitchErr(attempt, err)
36✔
726
        }
36✔
727

36✔
728
        log.Debugf("Attempt %v for payment %v successfully sent to switch, "+
36✔
729
                "route: %v", attempt.AttemptID, p.identifier, &attempt.Route)
36✔
730

36✔
731
        return &attemptResult{
36✔
732
                attempt: attempt,
43✔
733
        }, nil
7✔
734
}
7✔
735

7✔
736
// amendFirstHopData is a function that calls the traffic shaper to allow it to
7✔
737
// add custom records to the outgoing HTLC and also adjust the amount if
7✔
738
// needed.
739
func (p *paymentLifecycle) amendFirstHopData(rt *route.Route) error {
31✔
740
        // The first hop amount on the route is the full route amount if not
31✔
741
        // overwritten by the traffic shaper. So we set the initial value now
31✔
742
        // and potentially overwrite it later.
31✔
743
        rt.FirstHopAmount = tlv.NewRecordT[tlv.TlvType0](
31✔
744
                tlv.NewBigSizeT(rt.TotalAmount),
31✔
745
        )
746

747
        // By default, we set the first hop custom records to the initial
748
        // value requested by the RPC. The traffic shaper may overwrite this
749
        // value.
750
        rt.FirstHopWireCustomRecords = p.firstHopCustomRecords
38✔
751

38✔
752
        // extraDataRequest is a helper struct to pass the custom records and
38✔
753
        // amount back from the traffic shaper.
38✔
754
        type extraDataRequest struct {
38✔
755
                customRecords fn.Option[lnwire.CustomRecords]
38✔
756

38✔
757
                amount fn.Option[lnwire.MilliSatoshi]
38✔
758
        }
38✔
759

38✔
760
        // If a hook exists that may affect our outgoing message, we call it now
38✔
761
        // and apply its side effects to the UpdateAddHTLC message.
38✔
762
        result, err := fn.MapOptionZ(
38✔
763
                p.router.cfg.TrafficShaper,
38✔
764
                func(ts TlvTrafficShaper) fn.Result[extraDataRequest] {
38✔
765
                        newAmt, newRecords, err := ts.ProduceHtlcExtraData(
38✔
766
                                rt.TotalAmount, p.firstHopCustomRecords,
38✔
767
                        )
38✔
768
                        if err != nil {
38✔
769
                                return fn.Err[extraDataRequest](err)
38✔
770
                        }
38✔
771

38✔
772
                        // Make sure we only received valid records.
38✔
773
                        if err := newRecords.Validate(); err != nil {
38✔
774
                                return fn.Err[extraDataRequest](err)
38✔
775
                        }
38✔
776

74✔
777
                        log.Debugf("TLV traffic shaper returned custom "+
36✔
778
                                "records %v and amount %d msat for HTLC",
36✔
779
                                spew.Sdump(newRecords), newAmt)
36✔
780

36✔
781
                        return fn.Ok(extraDataRequest{
×
782
                                customRecords: fn.Some(newRecords),
×
783
                                amount:        fn.Some(newAmt),
784
                        })
785
                },
36✔
786
        ).Unpack()
×
787
        if err != nil {
×
788
                return fmt.Errorf("traffic shaper failed to produce extra "+
789
                        "data: %w", err)
36✔
790
        }
36✔
791

36✔
792
        // Apply the side effects to the UpdateAddHTLC message.
36✔
793
        result.customRecords.WhenSome(func(records lnwire.CustomRecords) {
36✔
794
                rt.FirstHopWireCustomRecords = records
36✔
795
        })
36✔
796
        result.amount.WhenSome(func(amount lnwire.MilliSatoshi) {
36✔
797
                rt.FirstHopAmount = tlv.NewRecordT[tlv.TlvType0](
798
                        tlv.NewBigSizeT(amount),
799
                )
38✔
800
        })
×
801

×
802
        return nil
×
803
}
804

805
// failAttemptAndPayment fails both the payment and its attempt via the
74✔
806
// router's control tower, which marks the payment as failed in db.
36✔
807
func (p *paymentLifecycle) failPaymentAndAttempt(
36✔
808
        attemptID uint64, reason *channeldb.FailureReason,
74✔
809
        sendErr error) (*attemptResult, error) {
36✔
810

36✔
811
        log.Errorf("Payment %v failed: final_outcome=%v, raw_err=%v",
36✔
812
                p.identifier, *reason, sendErr)
36✔
813

814
        // Fail the payment via control tower.
38✔
815
        //
816
        // NOTE: we must fail the payment first before failing the attempt.
817
        // Otherwise, once the attempt is marked as failed, another goroutine
818
        // might make another attempt while we are failing the payment.
819
        err := p.router.cfg.Control.FailPayment(p.identifier, *reason)
820
        if err != nil {
821
                log.Errorf("Unable to fail payment: %v", err)
7✔
822
                return nil, err
7✔
823
        }
7✔
824

7✔
825
        // Fail the attempt.
7✔
826
        return p.failAttempt(attemptID, sendErr)
7✔
827
}
7✔
828

7✔
829
// handleSwitchErr inspects the given error from the Switch and determines
7✔
830
// whether we should make another payment attempt, or if it should be
7✔
831
// considered a terminal error. Terminal errors will be recorded with the
7✔
832
// control tower. It analyzes the sendErr for the payment attempt received from
7✔
833
// the switch and updates mission control and/or channel policies. Depending on
×
834
// the error type, the error is either the final outcome of the payment or we
×
835
// need to continue with an alternative route. A final outcome is indicated by
×
836
// a non-nil reason value.
837
func (p *paymentLifecycle) handleSwitchErr(attempt *channeldb.HTLCAttempt,
838
        sendErr error) (*attemptResult, error) {
7✔
839

840
        internalErrorReason := channeldb.FailureReasonError
841
        attemptID := attempt.AttemptID
842

843
        // reportAndFail is a helper closure that reports the failure to the
844
        // mission control, which helps us to decide whether we want to retry
845
        // the payment or not. If a non nil reason is returned from mission
846
        // control, it will further fail the payment via control tower.
847
        reportAndFail := func(srcIdx *int,
848
                msg lnwire.FailureMessage) (*attemptResult, error) {
849

850
                // Report outcome to mission control.
24✔
851
                reason, err := p.router.cfg.MissionControl.ReportPaymentFail(
24✔
852
                        attemptID, &attempt.Route, srcIdx, msg,
24✔
853
                )
24✔
854
                if err != nil {
24✔
855
                        log.Errorf("Error reporting payment result to mc: %v",
24✔
856
                                err)
24✔
857

24✔
858
                        reason = &internalErrorReason
24✔
859
                }
24✔
860

45✔
861
                // Fail the attempt only if there's no reason.
21✔
862
                if reason == nil {
21✔
863
                        // Fail the attempt.
21✔
864
                        return p.failAttempt(attemptID, sendErr)
21✔
865
                }
21✔
866

21✔
867
                // Otherwise fail both the payment and the attempt.
×
868
                return p.failPaymentAndAttempt(attemptID, reason, sendErr)
×
869
        }
×
870

×
871
        // If this attempt ID is unknown to the Switch, it means it was never
×
872
        // checkpointed and forwarded by the switch before a restart. In this
873
        // case we can safely send a new payment attempt, and wait for its
874
        // result to be available.
40✔
875
        if errors.Is(sendErr, htlcswitch.ErrPaymentIDNotFound) {
19✔
876
                log.Debugf("Attempt ID %v for payment %v not found in the "+
19✔
877
                        "Switch, retrying.", attempt.AttemptID, p.identifier)
19✔
878

879
                return p.failAttempt(attemptID, sendErr)
880
        }
4✔
881

882
        if errors.Is(sendErr, htlcswitch.ErrUnreadableFailureMessage) {
883
                log.Warn("Unreadable failure when sending htlc: id=%v, hash=%v",
884
                        attempt.AttemptID, attempt.Hash)
885

886
                // Since this error message cannot be decrypted, we will send a
887
                // nil error message to our mission controller and fail the
24✔
888
                // payment.
×
889
                return reportAndFail(nil, nil)
×
890
        }
×
891

×
892
        // If the error is a ClearTextError, we have received a valid wire
×
893
        // failure message, either from our own outgoing link or from a node
894
        // down the route. If the error is not related to the propagation of
25✔
895
        // our payment, we can stop trying because an internal error has
1✔
896
        // occurred.
1✔
897
        var rtErr htlcswitch.ClearTextError
1✔
898
        ok := errors.As(sendErr, &rtErr)
1✔
899
        if !ok {
1✔
900
                return p.failPaymentAndAttempt(
1✔
901
                        attemptID, &internalErrorReason, sendErr,
1✔
902
                )
1✔
903
        }
904

905
        // failureSourceIdx is the index of the node that the failure occurred
906
        // at. If the ClearTextError received is not a ForwardingError the
907
        // payment error occurred at our node, so we leave this value as 0
908
        // to indicate that the failure occurred locally. If the error is a
909
        // ForwardingError, it did not originate at our node, so we set
23✔
910
        // failureSourceIdx to the index of the node where the failure occurred.
23✔
911
        failureSourceIdx := 0
26✔
912
        var source *htlcswitch.ForwardingError
3✔
913
        ok = errors.As(rtErr, &source)
3✔
914
        if ok {
3✔
915
                failureSourceIdx = source.FailureSourceIdx
3✔
916
        }
917

918
        // Extract the wire failure and apply channel update if it contains one.
919
        // If we received an unknown failure message from a node along the
920
        // route, the failure message will be nil.
921
        failureMessage := rtErr.WireMessage()
922
        err := p.handleFailureMessage(
923
                &attempt.Route, failureSourceIdx, failureMessage,
20✔
924
        )
20✔
925
        if err != nil {
20✔
926
                return p.failPaymentAndAttempt(
40✔
927
                        attemptID, &internalErrorReason, sendErr,
20✔
928
                )
20✔
929
        }
930

931
        log.Tracef("Node=%v reported failure when sending htlc",
932
                failureSourceIdx)
933

20✔
934
        return reportAndFail(&failureSourceIdx, failureMessage)
20✔
935
}
20✔
936

20✔
937
// handleFailureMessage tries to apply a channel update present in the failure
20✔
938
// message if any.
×
939
func (p *paymentLifecycle) handleFailureMessage(rt *route.Route,
×
940
        errorSourceIdx int, failure lnwire.FailureMessage) error {
×
941

×
942
        if failure == nil {
943
                return nil
20✔
944
        }
20✔
945

20✔
946
        // It makes no sense to apply our own channel updates.
20✔
947
        if errorSourceIdx == 0 {
948
                log.Errorf("Channel update of ourselves received")
949

950
                return nil
951
        }
952

20✔
953
        // Extract channel update if the error contains one.
20✔
954
        update := p.router.extractChannelUpdate(failure)
21✔
955
        if update == nil {
1✔
956
                return nil
1✔
957
        }
958

959
        // Parse pubkey to allow validation of the channel update. This should
21✔
960
        // always succeed, otherwise there is something wrong in our
2✔
961
        // implementation. Therefore, return an error.
2✔
962
        errVertex := rt.Hops[errorSourceIdx-1].PubKeyBytes
2✔
963
        errSource, err := btcec.ParsePubKey(errVertex[:])
2✔
964
        if err != nil {
965
                log.Errorf("Cannot parse pubkey: idx=%v, pubkey=%v",
966
                        errorSourceIdx, errVertex)
19✔
967

30✔
968
                return err
11✔
969
        }
11✔
970

971
        var (
972
                isAdditionalEdge bool
973
                policy           *models.CachedEdgePolicy
974
        )
10✔
975

10✔
976
        // Before we apply the channel update, we need to decide whether the
10✔
977
        // update is for additional (ephemeral) edge or normal edge stored in
×
978
        // db.
×
979
        //
×
980
        // Note: the p.paySession might be nil here if it's called inside
×
981
        // SendToRoute where there's no payment lifecycle.
×
982
        if p.paySession != nil {
983
                policy = p.paySession.GetAdditionalEdgePolicy(
10✔
984
                        errSource, update.ShortChannelID.ToUint64(),
10✔
985
                )
10✔
986
                if policy != nil {
10✔
987
                        isAdditionalEdge = true
10✔
988
                }
10✔
989
        }
10✔
990

10✔
991
        // Apply channel update to additional edge policy.
10✔
992
        if isAdditionalEdge {
10✔
993
                if !p.paySession.UpdateAdditionalEdge(
10✔
994
                        update, errSource, policy) {
17✔
995

7✔
996
                        log.Debugf("Invalid channel update received: node=%v",
7✔
997
                                errVertex)
7✔
998
                }
11✔
999
                return nil
4✔
1000
        }
4✔
1001

1002
        // Apply channel update to the channel edge policy in our db.
1003
        if !p.router.cfg.ApplyChannelUpdate(update) {
1004
                log.Debugf("Invalid channel update received: node=%v",
14✔
1005
                        errVertex)
4✔
1006
        }
4✔
1007
        return nil
×
1008
}
×
1009

×
1010
// failAttempt calls control tower to fail the current payment attempt.
×
1011
func (p *paymentLifecycle) failAttempt(attemptID uint64,
4✔
1012
        sendError error) (*attemptResult, error) {
1013

1014
        log.Warnf("Attempt %v for payment %v failed: %v", attemptID,
1015
                p.identifier, sendError)
12✔
1016

4✔
1017
        failInfo := marshallError(
4✔
1018
                sendError,
4✔
1019
                p.router.cfg.Clock.Now(),
8✔
1020
        )
1021

1022
        // Now that we are failing this payment attempt, cancel the shard with
1023
        // the ShardTracker such that it can derive the correct hash for the
1024
        // next attempt.
25✔
1025
        if err := p.shardTracker.CancelShard(attemptID); err != nil {
25✔
1026
                return nil, err
25✔
1027
        }
25✔
1028

25✔
1029
        attempt, err := p.router.cfg.Control.FailAttempt(
25✔
1030
                p.identifier, attemptID, failInfo,
25✔
1031
        )
25✔
1032
        if err != nil {
25✔
1033
                return nil, err
25✔
1034
        }
25✔
1035

25✔
1036
        return &attemptResult{
25✔
1037
                attempt: attempt,
25✔
1038
                err:     sendError,
×
1039
        }, nil
×
1040
}
1041

25✔
1042
// marshallError marshall an error as received from the switch to a structure
25✔
1043
// that is suitable for database storage.
25✔
1044
func marshallError(sendError error, time time.Time) *channeldb.HTLCFailInfo {
28✔
1045
        response := &channeldb.HTLCFailInfo{
3✔
1046
                FailTime: time,
3✔
1047
        }
1048

22✔
1049
        switch {
22✔
1050
        case errors.Is(sendError, htlcswitch.ErrPaymentIDNotFound):
22✔
1051
                response.Reason = channeldb.HTLCFailInternal
22✔
1052
                return response
1053

1054
        case errors.Is(sendError, htlcswitch.ErrUnreadableFailureMessage):
1055
                response.Reason = channeldb.HTLCFailUnreadable
1056
                return response
25✔
1057
        }
25✔
1058

25✔
1059
        var rtErr htlcswitch.ClearTextError
25✔
1060
        ok := errors.As(sendError, &rtErr)
25✔
1061
        if !ok {
25✔
1062
                response.Reason = channeldb.HTLCFailInternal
×
1063
                return response
×
1064
        }
×
1065

1066
        message := rtErr.WireMessage()
1✔
1067
        if message != nil {
1✔
1068
                response.Reason = channeldb.HTLCFailMessage
1✔
1069
                response.Message = message
1070
        } else {
1071
                response.Reason = channeldb.HTLCFailUnknown
24✔
1072
        }
24✔
1073

28✔
1074
        // If the ClearTextError received is a ForwardingError, the error
4✔
1075
        // originated from a node along the route, not locally on our outgoing
4✔
1076
        // link. We set failureSourceIdx to the index of the node where the
4✔
1077
        // failure occurred. If the error is not a ForwardingError, the failure
1078
        // occurred at our node, so we leave the index as 0 to indicate that
20✔
1079
        // we failed locally.
39✔
1080
        var fErr *htlcswitch.ForwardingError
19✔
1081
        ok = errors.As(rtErr, &fErr)
19✔
1082
        if ok {
20✔
1083
                response.FailureSourceIndex = uint32(fErr.FailureSourceIdx)
1✔
1084
        }
1✔
1085

1086
        return response
1087
}
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