Skip to content

Commit 82e20d7

Browse files
committed
multi: thread context through RegisterAttempt method
1 parent 602d379 commit 82e20d7

File tree

9 files changed

+70
-41
lines changed

9 files changed

+70
-41
lines changed

payments/db/interface.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ type PaymentControl interface {
6464
InitPayment(context.Context, lntypes.Hash, *PaymentCreationInfo) error
6565

6666
// RegisterAttempt atomically records the provided HTLCAttemptInfo.
67-
RegisterAttempt(lntypes.Hash, *HTLCAttemptInfo) (*MPPayment, error)
67+
RegisterAttempt(context.Context, lntypes.Hash,
68+
*HTLCAttemptInfo) (*MPPayment, error)
6869

6970
// SettleAttempt marks the given attempt settled with the preimage. If
7071
// this is a multi shard payment, this might implicitly mean the

payments/db/kv_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func deserializePaymentIndex(r io.Reader) (lntypes.Hash, error) {
359359

360360
// RegisterAttempt atomically records the provided HTLCAttemptInfo to the
361361
// DB.
362-
func (p *KVStore) RegisterAttempt(paymentHash lntypes.Hash,
362+
func (p *KVStore) RegisterAttempt(_ context.Context, paymentHash lntypes.Hash,
363363
attempt *HTLCAttemptInfo) (*MPPayment, error) {
364364

365365
// Serialize the information before opening the db transaction.

payments/db/kv_store_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestKVStoreDeleteNonInFlight(t *testing.T) {
8080
t.Fatalf("unable to send htlc message: %v", err)
8181
}
8282
_, err = paymentDB.RegisterAttempt(
83-
info.PaymentIdentifier, attempt,
83+
ctx, info.PaymentIdentifier, attempt,
8484
)
8585
if err != nil {
8686
t.Fatalf("unable to send htlc message: %v", err)

payments/db/payment_test.go

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func createTestPayments(t *testing.T, p DB, payments []*payment) {
143143
require.NoError(t, err, "unable to send htlc message")
144144

145145
// Register and fail the first attempt for all payments.
146-
_, err = p.RegisterAttempt(info.PaymentIdentifier, attempt)
146+
_, err = p.RegisterAttempt(ctx, info.PaymentIdentifier, attempt)
147147
require.NoError(t, err, "unable to send htlc message")
148148

149149
htlcFailure := HTLCFailUnreadable
@@ -167,7 +167,7 @@ func createTestPayments(t *testing.T, p DB, payments []*payment) {
167167
require.NoError(t, err)
168168
attemptID++
169169

170-
_, err = p.RegisterAttempt(info.PaymentIdentifier, attempt)
170+
_, err = p.RegisterAttempt(ctx, info.PaymentIdentifier, attempt)
171171
require.NoError(t, err, "unable to send htlc message")
172172

173173
switch payments[i].status {
@@ -584,7 +584,7 @@ func TestMPPRecordValidation(t *testing.T) {
584584
info.Value, [32]byte{1},
585585
)
586586

587-
_, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt)
587+
_, err = paymentDB.RegisterAttempt(ctx, info.PaymentIdentifier, attempt)
588588
require.NoError(t, err, "unable to send htlc message")
589589

590590
// Now try to register a non-MPP attempt, which should fail.
@@ -596,21 +596,27 @@ func TestMPPRecordValidation(t *testing.T) {
596596

597597
attempt2.Route.FinalHop().MPP = nil
598598

599-
_, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt2)
599+
_, err = paymentDB.RegisterAttempt(
600+
ctx, info.PaymentIdentifier, attempt2,
601+
)
600602
require.ErrorIs(t, err, ErrMPPayment)
601603

602604
// Try to register attempt one with a different payment address.
603605
attempt2.Route.FinalHop().MPP = record.NewMPP(
604606
info.Value, [32]byte{2},
605607
)
606-
_, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt2)
608+
_, err = paymentDB.RegisterAttempt(
609+
ctx, info.PaymentIdentifier, attempt2,
610+
)
607611
require.ErrorIs(t, err, ErrMPPPaymentAddrMismatch)
608612

609613
// Try registering one with a different total amount.
610614
attempt2.Route.FinalHop().MPP = record.NewMPP(
611615
info.Value/2, [32]byte{1},
612616
)
613-
_, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt2)
617+
_, err = paymentDB.RegisterAttempt(
618+
ctx, info.PaymentIdentifier, attempt2,
619+
)
614620
require.ErrorIs(t, err, ErrMPPTotalAmountMismatch)
615621

616622
// Create and init a new payment. This time we'll check that we cannot
@@ -633,7 +639,9 @@ func TestMPPRecordValidation(t *testing.T) {
633639
require.NoError(t, err, "unable to send htlc message")
634640

635641
attempt.Route.FinalHop().MPP = nil
636-
_, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt)
642+
_, err = paymentDB.RegisterAttempt(
643+
ctx, info.PaymentIdentifier, attempt,
644+
)
637645
require.NoError(t, err, "unable to send htlc message")
638646

639647
// Attempt to register an MPP attempt, which should fail.
@@ -647,7 +655,9 @@ func TestMPPRecordValidation(t *testing.T) {
647655
info.Value, [32]byte{1},
648656
)
649657

650-
_, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt2)
658+
_, err = paymentDB.RegisterAttempt(
659+
ctx, info.PaymentIdentifier, attempt2,
660+
)
651661
require.ErrorIs(t, err, ErrNonMPPayment)
652662
}
653663

@@ -1452,7 +1462,7 @@ func TestSwitchDoubleSend(t *testing.T) {
14521462
require.ErrorIs(t, err, ErrPaymentExists)
14531463

14541464
// Record an attempt.
1455-
_, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt)
1465+
_, err = paymentDB.RegisterAttempt(ctx, info.PaymentIdentifier, attempt)
14561466
require.NoError(t, err, "unable to send htlc message")
14571467
assertDBPaymentstatus(
14581468
t, paymentDB, info.PaymentIdentifier, StatusInFlight,
@@ -1563,7 +1573,7 @@ func TestSwitchFail(t *testing.T) {
15631573
// Record a new attempt. In this test scenario, the attempt fails.
15641574
// However, this is not communicated to control tower in the current
15651575
// implementation. It only registers the initiation of the attempt.
1566-
_, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt)
1576+
_, err = paymentDB.RegisterAttempt(ctx, info.PaymentIdentifier, attempt)
15671577
require.NoError(t, err, "unable to register attempt")
15681578

15691579
htlcReason := HTLCFailUnreadable
@@ -1593,7 +1603,7 @@ func TestSwitchFail(t *testing.T) {
15931603
)
15941604
require.NoError(t, err)
15951605

1596-
_, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt)
1606+
_, err = paymentDB.RegisterAttempt(ctx, info.PaymentIdentifier, attempt)
15971607
require.NoError(t, err, "unable to send htlc message")
15981608
assertDBPaymentstatus(
15991609
t, paymentDB, info.PaymentIdentifier, StatusInFlight,
@@ -1711,7 +1721,7 @@ func TestMultiShard(t *testing.T) {
17111721
attempts = append(attempts, a)
17121722

17131723
_, err = paymentDB.RegisterAttempt(
1714-
info.PaymentIdentifier, a,
1724+
ctx, info.PaymentIdentifier, a,
17151725
)
17161726
if err != nil {
17171727
t.Fatalf("unable to send htlc message: %v", err)
@@ -1743,7 +1753,9 @@ func TestMultiShard(t *testing.T) {
17431753
info.Value, [32]byte{1},
17441754
)
17451755

1746-
_, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, b)
1756+
_, err = paymentDB.RegisterAttempt(
1757+
ctx, info.PaymentIdentifier, b,
1758+
)
17471759
require.ErrorIs(t, err, ErrValueExceedsAmt)
17481760

17491761
// Fail the second attempt.
@@ -1850,7 +1862,9 @@ func TestMultiShard(t *testing.T) {
18501862
info.Value, [32]byte{1},
18511863
)
18521864

1853-
_, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, b)
1865+
_, err = paymentDB.RegisterAttempt(
1866+
ctx, info.PaymentIdentifier, b,
1867+
)
18541868
if test.settleFirst {
18551869
require.ErrorIs(
18561870
t, err, ErrPaymentPendingSettled,
@@ -1949,7 +1963,9 @@ func TestMultiShard(t *testing.T) {
19491963
)
19501964

19511965
// Finally assert we cannot register more attempts.
1952-
_, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, b)
1966+
_, err = paymentDB.RegisterAttempt(
1967+
ctx, info.PaymentIdentifier, b,
1968+
)
19531969
require.ErrorIs(t, err, registerErr)
19541970
}
19551971

@@ -2352,7 +2368,7 @@ func TestQueryPayments(t *testing.T) {
23522368
require.NoError(t, err)
23532369

23542370
_, err = paymentDB.RegisterAttempt(
2355-
lastPaymentInfo.PaymentIdentifier,
2371+
ctx, lastPaymentInfo.PaymentIdentifier,
23562372
&attempt.HTLCAttemptInfo,
23572373
)
23582374
require.NoError(t, err)

payments/db/sql_store.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -886,10 +886,9 @@ func (s *SQLStore) insertRouteHops(ctx context.Context, db SQLQueries,
886886
// RegisterAttempt registers an attempt for a payment.
887887
//
888888
// This is part of the DB interface.
889-
func (s *SQLStore) RegisterAttempt(paymentHash lntypes.Hash,
890-
attempt *HTLCAttemptInfo) (*MPPayment, error) {
891-
892-
ctx := context.TODO()
889+
func (s *SQLStore) RegisterAttempt(ctx context.Context,
890+
paymentHash lntypes.Hash, attempt *HTLCAttemptInfo) (*MPPayment,
891+
error) {
893892

894893
var mpPayment *MPPayment
895894

routing/control_tower.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ type ControlTower interface {
3131
// RegisterAttempt atomically records the provided HTLCAttemptInfo.
3232
//
3333
// NOTE: Subscribers should be notified by the new state of the payment.
34-
RegisterAttempt(lntypes.Hash, *paymentsdb.HTLCAttemptInfo) error
34+
RegisterAttempt(context.Context, lntypes.Hash,
35+
*paymentsdb.HTLCAttemptInfo) error
3536

3637
// SettleAttempt marks the given attempt settled with the preimage. If
3738
// this is a multi shard payment, this might implicitly mean the the
@@ -196,13 +197,13 @@ func (p *controlTower) DeleteFailedAttempts(paymentHash lntypes.Hash) error {
196197

197198
// RegisterAttempt atomically records the provided HTLCAttemptInfo to the
198199
// DB.
199-
func (p *controlTower) RegisterAttempt(paymentHash lntypes.Hash,
200-
attempt *paymentsdb.HTLCAttemptInfo) error {
200+
func (p *controlTower) RegisterAttempt(ctx context.Context,
201+
paymentHash lntypes.Hash, attempt *paymentsdb.HTLCAttemptInfo) error {
201202

202203
p.paymentsMtx.Lock(paymentHash)
203204
defer p.paymentsMtx.Unlock(paymentHash)
204205

205-
payment, err := p.db.RegisterAttempt(paymentHash, attempt)
206+
payment, err := p.db.RegisterAttempt(ctx, paymentHash, attempt)
206207
if err != nil {
207208
return err
208209
}

routing/control_tower_test.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ func TestControlTowerSubscribeSuccess(t *testing.T) {
9292
require.NoError(t, err, "expected subscribe to succeed, but got")
9393

9494
// Register an attempt.
95-
err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
95+
err = pControl.RegisterAttempt(
96+
t.Context(), info.PaymentIdentifier, attempt,
97+
)
9698
if err != nil {
9799
t.Fatal(err)
98100
}
@@ -221,7 +223,9 @@ func TestKVStoreSubscribeAllSuccess(t *testing.T) {
221223
require.NoError(t, err, "expected subscribe to succeed, but got: %v")
222224

223225
// Register an attempt.
224-
err = pControl.RegisterAttempt(info1.PaymentIdentifier, attempt1)
226+
err = pControl.RegisterAttempt(
227+
t.Context(), info1.PaymentIdentifier, attempt1,
228+
)
225229
require.NoError(t, err)
226230

227231
// Initiate a second payment after the subscription is already active.
@@ -232,7 +236,9 @@ func TestKVStoreSubscribeAllSuccess(t *testing.T) {
232236
require.NoError(t, err)
233237

234238
// Register an attempt on the second payment.
235-
err = pControl.RegisterAttempt(info2.PaymentIdentifier, attempt2)
239+
err = pControl.RegisterAttempt(
240+
t.Context(), info2.PaymentIdentifier, attempt2,
241+
)
236242
require.NoError(t, err)
237243

238244
// Mark the first payment as successful.
@@ -341,7 +347,9 @@ func TestKVStoreSubscribeAllImmediate(t *testing.T) {
341347
require.NoError(t, err)
342348

343349
// Register a payment update.
344-
err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
350+
err = pControl.RegisterAttempt(
351+
t.Context(), info.PaymentIdentifier, attempt,
352+
)
345353
require.NoError(t, err)
346354

347355
subscription, err := pControl.SubscribeAllPayments()
@@ -414,7 +422,9 @@ func TestKVStoreUnsubscribeSuccess(t *testing.T) {
414422
subscription1.Close()
415423

416424
// Register a payment update.
417-
err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
425+
err = pControl.RegisterAttempt(
426+
t.Context(), info.PaymentIdentifier, attempt,
427+
)
418428
require.NoError(t, err)
419429

420430
// Assert only subscription 2 receives the update.
@@ -479,10 +489,10 @@ func testKVStoreSubscribeFail(t *testing.T, registerAttempt,
479489
// making any attempts at all.
480490
if registerAttempt {
481491
// Register an attempt.
482-
err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
483-
if err != nil {
484-
t.Fatal(err)
485-
}
492+
err = pControl.RegisterAttempt(
493+
t.Context(), info.PaymentIdentifier, attempt,
494+
)
495+
require.NoError(t, err)
486496

487497
// Fail the payment attempt.
488498
failInfo := paymentsdb.HTLCFailInfo{

routing/mock_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ func (m *mockControlTowerOld) DeleteFailedAttempts(phash lntypes.Hash) error {
354354
return nil
355355
}
356356

357-
func (m *mockControlTowerOld) RegisterAttempt(phash lntypes.Hash,
358-
a *paymentsdb.HTLCAttemptInfo) error {
357+
func (m *mockControlTowerOld) RegisterAttempt(_ context.Context,
358+
phash lntypes.Hash, a *paymentsdb.HTLCAttemptInfo) error {
359359

360360
if m.registerAttempt != nil {
361361
m.registerAttempt <- registerAttemptArgs{a}
@@ -746,8 +746,8 @@ func (m *mockControlTower) DeleteFailedAttempts(phash lntypes.Hash) error {
746746
return args.Error(0)
747747
}
748748

749-
func (m *mockControlTower) RegisterAttempt(phash lntypes.Hash,
750-
a *paymentsdb.HTLCAttemptInfo) error {
749+
func (m *mockControlTower) RegisterAttempt(_ context.Context,
750+
phash lntypes.Hash, a *paymentsdb.HTLCAttemptInfo) error {
751751

752752
args := m.Called(phash, a)
753753
return args.Error(0)

routing/payment_lifecycle.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,8 @@ func (p *paymentLifecycle) collectResult(
584584
func (p *paymentLifecycle) registerAttempt(rt *route.Route,
585585
remainingAmt lnwire.MilliSatoshi) (*paymentsdb.HTLCAttempt, error) {
586586

587+
ctx := context.TODO()
588+
587589
// If this route will consume the last remaining amount to send
588590
// to the receiver, this will be our last shard (for now).
589591
isLastAttempt := rt.ReceiverAmt() == remainingAmt
@@ -601,7 +603,7 @@ func (p *paymentLifecycle) registerAttempt(rt *route.Route,
601603
// Switch for its whereabouts. The route is needed to handle the result
602604
// when it eventually comes back.
603605
err = p.router.cfg.Control.RegisterAttempt(
604-
p.identifier, &attempt.HTLCAttemptInfo,
606+
ctx, p.identifier, &attempt.HTLCAttemptInfo,
605607
)
606608

607609
return attempt, err

0 commit comments

Comments
 (0)