Skip to content

Commit ac70d70

Browse files
committed
multi: thread context through Fail payment functions
1 parent 6b88d04 commit ac70d70

File tree

10 files changed

+36
-23
lines changed

10 files changed

+36
-23
lines changed

payments/db/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type PaymentControl interface {
8888
// invoking this method, InitPayment should return nil on its next call
8989
// for this payment hash, allowing the user to make a subsequent
9090
// payment.
91-
Fail(lntypes.Hash, FailureReason) (*MPPayment, error)
91+
Fail(context.Context, lntypes.Hash, FailureReason) (*MPPayment, error)
9292

9393
// DeleteFailedAttempts removes all failed HTLCs from the db. It should
9494
// be called for a given payment whenever all inflight htlcs are

payments/db/kv_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ func (p *KVStore) updateHtlcKey(paymentHash lntypes.Hash,
528528
// payment failed. After invoking this method, InitPayment should return nil on
529529
// its next call for this payment hash, allowing the switch to make a
530530
// subsequent payment.
531-
func (p *KVStore) Fail(paymentHash lntypes.Hash,
531+
func (p *KVStore) Fail(_ context.Context, paymentHash lntypes.Hash,
532532
reason FailureReason) (*MPPayment, error) {
533533

534534
var (

payments/db/kv_store_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestKVStoreDeleteNonInFlight(t *testing.T) {
107107
// Fail the payment, which should moved it to Failed.
108108
failReason := FailureReasonNoRoute
109109
_, err = paymentDB.Fail(
110-
info.PaymentIdentifier, failReason,
110+
ctx, info.PaymentIdentifier, failReason,
111111
)
112112
if err != nil {
113113
t.Fatalf("unable to fail payment hash: %v", err)

payments/db/payment_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ func createTestPayments(t *testing.T, p DB, payments []*payment) {
183183
require.NoError(t, err, "unable to fail htlc")
184184

185185
failReason := FailureReasonNoRoute
186-
_, err = p.Fail(info.PaymentIdentifier,
187-
failReason)
186+
_, err = p.Fail(
187+
ctx, info.PaymentIdentifier, failReason,
188+
)
188189
require.NoError(t, err, "unable to fail payment hash")
189190

190191
// Settle the attempt
@@ -1361,7 +1362,7 @@ func TestFailsWithoutInFlight(t *testing.T) {
13611362

13621363
// Calling Fail should return an error.
13631364
_, err = paymentDB.Fail(
1364-
info.PaymentIdentifier, FailureReasonNoRoute,
1365+
t.Context(), info.PaymentIdentifier, FailureReasonNoRoute,
13651366
)
13661367
require.ErrorIs(t, err, ErrPaymentNotInitiated)
13671368
}
@@ -1537,7 +1538,7 @@ func TestSwitchFail(t *testing.T) {
15371538

15381539
// Fail the payment, which should moved it to Failed.
15391540
failReason := FailureReasonNoRoute
1540-
_, err = paymentDB.Fail(info.PaymentIdentifier, failReason)
1541+
_, err = paymentDB.Fail(ctx, info.PaymentIdentifier, failReason)
15411542
require.NoError(t, err, "unable to fail payment hash")
15421543

15431544
// Verify the status is indeed Failed.
@@ -1833,7 +1834,7 @@ func TestMultiShard(t *testing.T) {
18331834
// a terminal state.
18341835
failReason := FailureReasonNoRoute
18351836
_, err = paymentDB.Fail(
1836-
info.PaymentIdentifier, failReason,
1837+
ctx, info.PaymentIdentifier, failReason,
18371838
)
18381839
if err != nil {
18391840
t.Fatalf("unable to fail payment hash: %v", err)
@@ -1926,7 +1927,7 @@ func TestMultiShard(t *testing.T) {
19261927
// syncing.
19271928
failReason := FailureReasonPaymentDetails
19281929
_, err = paymentDB.Fail(
1929-
info.PaymentIdentifier, failReason,
1930+
ctx, info.PaymentIdentifier, failReason,
19301931
)
19311932
require.NoError(t, err, "unable to fail")
19321933
}

payments/db/sql_store.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,11 +1102,9 @@ func (s *SQLStore) FailAttempt(ctx context.Context, paymentHash lntypes.Hash,
11021102
// active attempts are already failed. After invoking this method, InitPayment
11031103
// should return nil on its next call for this payment hash, allowing the user
11041104
// to make a subsequent payments for the same payment hash.
1105-
func (s *SQLStore) Fail(paymentHash lntypes.Hash,
1105+
func (s *SQLStore) Fail(ctx context.Context, paymentHash lntypes.Hash,
11061106
reason FailureReason) (*MPPayment, error) {
11071107

1108-
ctx := context.TODO()
1109-
11101108
var mpPayment *MPPayment
11111109

11121110
err := s.db.ExecTx(ctx, sqldb.WriteTxOpt(), func(db SQLQueries) error {

routing/control_tower.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ type ControlTower interface {
6666
// payment.
6767
//
6868
// NOTE: Subscribers should be notified by the new state of the payment.
69-
FailPayment(lntypes.Hash, paymentsdb.FailureReason) error
69+
FailPayment(context.Context, lntypes.Hash,
70+
paymentsdb.FailureReason) error
7071

7172
// FetchInFlightPayments returns all payments with status InFlight.
7273
FetchInFlightPayments(ctx context.Context) ([]*paymentsdb.MPPayment,
@@ -272,13 +273,13 @@ func (p *controlTower) FetchPayment(ctx context.Context,
272273
//
273274
// NOTE: This method will overwrite the failure reason if the payment is already
274275
// failed.
275-
func (p *controlTower) FailPayment(paymentHash lntypes.Hash,
276-
reason paymentsdb.FailureReason) error {
276+
func (p *controlTower) FailPayment(ctx context.Context,
277+
paymentHash lntypes.Hash, reason paymentsdb.FailureReason) error {
277278

278279
p.paymentsMtx.Lock(paymentHash)
279280
defer p.paymentsMtx.Unlock(paymentHash)
280281

281-
payment, err := p.db.Fail(paymentHash, reason)
282+
payment, err := p.db.Fail(ctx, paymentHash, reason)
282283
if err != nil {
283284
return err
284285
}

routing/control_tower_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,8 @@ func testKVStoreSubscribeFail(t *testing.T, registerAttempt,
516516

517517
// Mark the payment as failed.
518518
err = pControl.FailPayment(
519-
info.PaymentIdentifier, paymentsdb.FailureReasonTimeout,
519+
t.Context(), info.PaymentIdentifier,
520+
paymentsdb.FailureReasonTimeout,
520521
)
521522
if err != nil {
522523
t.Fatal(err)

routing/mock_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ func (m *mockControlTowerOld) FailAttempt(_ context.Context, phash lntypes.Hash,
491491
return nil, fmt.Errorf("pid not found")
492492
}
493493

494-
func (m *mockControlTowerOld) FailPayment(phash lntypes.Hash,
494+
func (m *mockControlTowerOld) FailPayment(_ context.Context, phash lntypes.Hash,
495495
reason paymentsdb.FailureReason) error {
496496

497497
m.Lock()
@@ -782,7 +782,7 @@ func (m *mockControlTower) FailAttempt(_ context.Context, phash lntypes.Hash,
782782
return attempt.(*paymentsdb.HTLCAttempt), args.Error(1)
783783
}
784784

785-
func (m *mockControlTower) FailPayment(phash lntypes.Hash,
785+
func (m *mockControlTower) FailPayment(_ context.Context, phash lntypes.Hash,
786786
reason paymentsdb.FailureReason) error {
787787

788788
args := m.Called(phash, reason)

routing/payment_lifecycle.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,9 @@ func (p *paymentLifecycle) checkContext(ctx context.Context) error {
368368
// inflight HTLCs or not, its status will now either be
369369
// `StatusInflight` or `StatusFailed`. In either case, no more
370370
// HTLCs will be attempted.
371-
err := p.router.cfg.Control.FailPayment(p.identifier, reason)
371+
err := p.router.cfg.Control.FailPayment(
372+
ctx, p.identifier, reason,
373+
)
372374
if err != nil {
373375
return fmt.Errorf("FailPayment got %w", err)
374376
}
@@ -389,6 +391,8 @@ func (p *paymentLifecycle) checkContext(ctx context.Context) error {
389391
func (p *paymentLifecycle) requestRoute(
390392
ps *paymentsdb.MPPaymentState) (*route.Route, error) {
391393

394+
ctx := context.TODO()
395+
392396
remainingFees := p.calcFeeBudget(ps.FeesPaid)
393397

394398
// Query our payment session to construct a route.
@@ -430,7 +434,9 @@ func (p *paymentLifecycle) requestRoute(
430434
log.Warnf("Marking payment %v permanently failed with no route: %v",
431435
p.identifier, failureCode)
432436

433-
err = p.router.cfg.Control.FailPayment(p.identifier, failureCode)
437+
err = p.router.cfg.Control.FailPayment(
438+
ctx, p.identifier, failureCode,
439+
)
434440
if err != nil {
435441
return nil, fmt.Errorf("FailPayment got: %w", err)
436442
}
@@ -800,6 +806,8 @@ func (p *paymentLifecycle) failPaymentAndAttempt(
800806
attemptID uint64, reason *paymentsdb.FailureReason,
801807
sendErr error) (*attemptResult, error) {
802808

809+
ctx := context.TODO()
810+
803811
log.Errorf("Payment %v failed: final_outcome=%v, raw_err=%v",
804812
p.identifier, *reason, sendErr)
805813

@@ -808,7 +816,9 @@ func (p *paymentLifecycle) failPaymentAndAttempt(
808816
// NOTE: we must fail the payment first before failing the attempt.
809817
// Otherwise, once the attempt is marked as failed, another goroutine
810818
// might make another attempt while we are failing the payment.
811-
err := p.router.cfg.Control.FailPayment(p.identifier, *reason)
819+
err := p.router.cfg.Control.FailPayment(
820+
ctx, p.identifier, *reason,
821+
)
812822
if err != nil {
813823
log.Errorf("Unable to fail payment: %v", err)
814824
return nil, err

routing/router.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,9 @@ func (r *ChannelRouter) sendToRoute(htlcHash lntypes.Hash, rt *route.Route,
10881088
return nil
10891089
}
10901090

1091-
return r.cfg.Control.FailPayment(paymentIdentifier, reason)
1091+
return r.cfg.Control.FailPayment(
1092+
ctx, paymentIdentifier, reason,
1093+
)
10921094
}
10931095

10941096
log.Debugf("SendToRoute for payment %v with skipTempErr=%v",

0 commit comments

Comments
 (0)