Skip to content

Commit 4a27dbc

Browse files
committed
multi: thread context through SettleAttempt
1 parent 82e20d7 commit 4a27dbc

File tree

9 files changed

+33
-25
lines changed

9 files changed

+33
-25
lines changed

payments/db/interface.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ type PaymentControl interface {
7575
// error to prevent us from making duplicate payments to the same
7676
// payment hash. The provided preimage is atomically saved to the DB
7777
// for record keeping.
78-
SettleAttempt(lntypes.Hash, uint64, *HTLCSettleInfo) (*MPPayment, error)
78+
SettleAttempt(context.Context, lntypes.Hash, uint64,
79+
*HTLCSettleInfo) (*MPPayment, error)
7980

8081
// FailAttempt marks the given payment attempt failed.
8182
FailAttempt(lntypes.Hash, uint64, *HTLCFailInfo) (*MPPayment, error)

payments/db/kv_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ func (p *KVStore) RegisterAttempt(_ context.Context, paymentHash lntypes.Hash,
430430
// After invoking this method, InitPayment should always return an error to
431431
// prevent us from making duplicate payments to the same payment hash. The
432432
// provided preimage is atomically saved to the DB for record keeping.
433-
func (p *KVStore) SettleAttempt(hash lntypes.Hash,
433+
func (p *KVStore) SettleAttempt(_ context.Context, hash lntypes.Hash,
434434
attemptID uint64, settleInfo *HTLCSettleInfo) (*MPPayment, error) {
435435

436436
var b bytes.Buffer

payments/db/kv_store_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func TestKVStoreDeleteNonInFlight(t *testing.T) {
128128
case p.success:
129129
// Verifies that status was changed to StatusSucceeded.
130130
_, err := paymentDB.SettleAttempt(
131-
info.PaymentIdentifier, attempt.AttemptID,
131+
ctx, info.PaymentIdentifier, attempt.AttemptID,
132132
&HTLCSettleInfo{
133133
Preimage: preimg,
134134
},

payments/db/payment_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func createTestPayments(t *testing.T, p DB, payments []*payment) {
190190
// Settle the attempt
191191
case StatusSucceeded:
192192
_, err := p.SettleAttempt(
193-
info.PaymentIdentifier, attempt.AttemptID,
193+
ctx, info.PaymentIdentifier, attempt.AttemptID,
194194
&HTLCSettleInfo{
195195
Preimage: preimg,
196196
},
@@ -1337,6 +1337,7 @@ func TestSuccessesWithoutInFlight(t *testing.T) {
13371337

13381338
// Attempt to complete the payment should fail.
13391339
_, err = paymentDB.SettleAttempt(
1340+
t.Context(),
13401341
info.PaymentIdentifier, 0,
13411342
&HTLCSettleInfo{
13421343
Preimage: preimg,
@@ -1484,7 +1485,7 @@ func TestSwitchDoubleSend(t *testing.T) {
14841485

14851486
// After settling, the error should be ErrAlreadyPaid.
14861487
_, err = paymentDB.SettleAttempt(
1487-
info.PaymentIdentifier, attempt.AttemptID,
1488+
ctx, info.PaymentIdentifier, attempt.AttemptID,
14881489
&HTLCSettleInfo{
14891490
Preimage: preimg,
14901491
},
@@ -1620,7 +1621,7 @@ func TestSwitchFail(t *testing.T) {
16201621
// Settle the attempt and verify that status was changed to
16211622
// StatusSucceeded.
16221623
payment, err = paymentDB.SettleAttempt(
1623-
info.PaymentIdentifier, attempt.AttemptID,
1624+
ctx, info.PaymentIdentifier, attempt.AttemptID,
16241625
&HTLCSettleInfo{
16251626
Preimage: preimg,
16261627
},
@@ -1793,7 +1794,7 @@ func TestMultiShard(t *testing.T) {
17931794
var firstFailReason *FailureReason
17941795
if test.settleFirst {
17951796
_, err := paymentDB.SettleAttempt(
1796-
info.PaymentIdentifier, a.AttemptID,
1797+
ctx, info.PaymentIdentifier, a.AttemptID,
17971798
&HTLCSettleInfo{
17981799
Preimage: preimg,
17991800
},
@@ -1887,7 +1888,7 @@ func TestMultiShard(t *testing.T) {
18871888
if test.settleLast {
18881889
// Settle the last outstanding attempt.
18891890
_, err = paymentDB.SettleAttempt(
1890-
info.PaymentIdentifier, a.AttemptID,
1891+
ctx, info.PaymentIdentifier, a.AttemptID,
18911892
&HTLCSettleInfo{
18921893
Preimage: preimg,
18931894
},
@@ -2377,7 +2378,7 @@ func TestQueryPayments(t *testing.T) {
23772378
copy(preimg[:], rev[:])
23782379

23792380
_, err = paymentDB.SettleAttempt(
2380-
lastPaymentInfo.PaymentIdentifier,
2381+
ctx, lastPaymentInfo.PaymentIdentifier,
23812382
attempt.AttemptID,
23822383
&HTLCSettleInfo{
23832384
Preimage: preimg,

payments/db/sql_store.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -991,11 +991,9 @@ func (s *SQLStore) RegisterAttempt(ctx context.Context,
991991
}
992992

993993
// SettleAttempt marks the given attempt settled with the preimage.
994-
func (s *SQLStore) SettleAttempt(paymentHash lntypes.Hash,
994+
func (s *SQLStore) SettleAttempt(ctx context.Context, paymentHash lntypes.Hash,
995995
attemptID uint64, settleInfo *HTLCSettleInfo) (*MPPayment, error) {
996996

997-
ctx := context.TODO()
998-
999997
var mpPayment *MPPayment
1000998

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

routing/control_tower.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ type ControlTower interface {
4444
// for record keeping.
4545
//
4646
// NOTE: Subscribers should be notified by the new state of the payment.
47-
SettleAttempt(lntypes.Hash, uint64, *paymentsdb.HTLCSettleInfo) (
48-
*paymentsdb.HTLCAttempt, error)
47+
SettleAttempt(context.Context, lntypes.Hash, uint64,
48+
*paymentsdb.HTLCSettleInfo) (*paymentsdb.HTLCAttempt, error)
4949

5050
// FailAttempt marks the given payment attempt failed.
5151
//
@@ -217,14 +217,17 @@ func (p *controlTower) RegisterAttempt(ctx context.Context,
217217
// SettleAttempt marks the given attempt settled with the preimage. If
218218
// this is a multi shard payment, this might implicitly mean the the
219219
// full payment succeeded.
220-
func (p *controlTower) SettleAttempt(paymentHash lntypes.Hash,
221-
attemptID uint64, settleInfo *paymentsdb.HTLCSettleInfo) (
222-
*paymentsdb.HTLCAttempt, error) {
220+
func (p *controlTower) SettleAttempt(ctx context.Context,
221+
paymentHash lntypes.Hash, attemptID uint64,
222+
settleInfo *paymentsdb.HTLCSettleInfo) (*paymentsdb.HTLCAttempt,
223+
error) {
223224

224225
p.paymentsMtx.Lock(paymentHash)
225226
defer p.paymentsMtx.Unlock(paymentHash)
226227

227-
payment, err := p.db.SettleAttempt(paymentHash, attemptID, settleInfo)
228+
payment, err := p.db.SettleAttempt(
229+
ctx, paymentHash, attemptID, settleInfo,
230+
)
228231
if err != nil {
229232
return nil, err
230233
}

routing/control_tower_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ func TestControlTowerSubscribeSuccess(t *testing.T) {
108108
Preimage: preimg,
109109
}
110110
htlcAttempt, err := pControl.SettleAttempt(
111-
info.PaymentIdentifier, attempt.AttemptID, &settleInfo,
111+
t.Context(), info.PaymentIdentifier, attempt.AttemptID,
112+
&settleInfo,
112113
)
113114
if err != nil {
114115
t.Fatal(err)
@@ -246,7 +247,8 @@ func TestKVStoreSubscribeAllSuccess(t *testing.T) {
246247
Preimage: preimg1,
247248
}
248249
htlcAttempt1, err := pControl.SettleAttempt(
249-
info1.PaymentIdentifier, attempt1.AttemptID, &settleInfo1,
250+
t.Context(), info1.PaymentIdentifier, attempt1.AttemptID,
251+
&settleInfo1,
250252
)
251253
require.NoError(t, err)
252254
require.Equal(
@@ -259,7 +261,8 @@ func TestKVStoreSubscribeAllSuccess(t *testing.T) {
259261
Preimage: preimg2,
260262
}
261263
htlcAttempt2, err := pControl.SettleAttempt(
262-
info2.PaymentIdentifier, attempt2.AttemptID, &settleInfo2,
264+
t.Context(), info2.PaymentIdentifier, attempt2.AttemptID,
265+
&settleInfo2,
263266
)
264267
require.NoError(t, err)
265268
require.Equal(

routing/mock_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,8 @@ func (m *mockControlTowerOld) RegisterAttempt(_ context.Context,
408408
return nil
409409
}
410410

411-
func (m *mockControlTowerOld) SettleAttempt(phash lntypes.Hash,
412-
pid uint64, settleInfo *paymentsdb.HTLCSettleInfo) (
411+
func (m *mockControlTowerOld) SettleAttempt(_ context.Context,
412+
phash lntypes.Hash, pid uint64, settleInfo *paymentsdb.HTLCSettleInfo) (
413413
*paymentsdb.HTLCAttempt, error) {
414414

415415
if m.settleAttempt != nil {
@@ -753,7 +753,7 @@ func (m *mockControlTower) RegisterAttempt(_ context.Context,
753753
return args.Error(0)
754754
}
755755

756-
func (m *mockControlTower) SettleAttempt(phash lntypes.Hash,
756+
func (m *mockControlTower) SettleAttempt(_ context.Context, phash lntypes.Hash,
757757
pid uint64, settleInfo *paymentsdb.HTLCSettleInfo) (
758758
*paymentsdb.HTLCAttempt, error) {
759759

routing/payment_lifecycle.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,8 @@ func (p *paymentLifecycle) reloadPayment() (paymentsdb.DBMPPayment,
11661166
func (p *paymentLifecycle) handleAttemptResult(attempt *paymentsdb.HTLCAttempt,
11671167
result *htlcswitch.PaymentResult) (*attemptResult, error) {
11681168

1169+
ctx := context.TODO()
1170+
11691171
// If the result has an error, we need to further process it by failing
11701172
// the attempt and maybe fail the payment.
11711173
if result.Error != nil {
@@ -1187,7 +1189,7 @@ func (p *paymentLifecycle) handleAttemptResult(attempt *paymentsdb.HTLCAttempt,
11871189
// In case of success we atomically store settle result to the DB and
11881190
// move the shard to the settled state.
11891191
htlcAttempt, err := p.router.cfg.Control.SettleAttempt(
1190-
p.identifier, attempt.AttemptID,
1192+
ctx, p.identifier, attempt.AttemptID,
11911193
&paymentsdb.HTLCSettleInfo{
11921194
Preimage: result.Preimage,
11931195
SettleTime: p.router.cfg.Clock.Now(),

0 commit comments

Comments
 (0)