Skip to content

Commit b1d6078

Browse files
committed
multi: thread context through DeleteFailedAttempts
1 parent ac70d70 commit b1d6078

File tree

7 files changed

+37
-18
lines changed

7 files changed

+37
-18
lines changed

payments/db/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ type PaymentControl interface {
9393
// DeleteFailedAttempts removes all failed HTLCs from the db. It should
9494
// be called for a given payment whenever all inflight htlcs are
9595
// completed, and the payment has reached a final terminal state.
96-
DeleteFailedAttempts(lntypes.Hash) error
96+
DeleteFailedAttempts(context.Context, lntypes.Hash) error
9797
}
9898

9999
// DBMPPayment is an interface that represents the payment state during a

payments/db/kv_store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ func (p *KVStore) InitPayment(_ context.Context, paymentHash lntypes.Hash,
290290

291291
// DeleteFailedAttempts deletes all failed htlcs for a payment if configured
292292
// by the KVStore db.
293-
func (p *KVStore) DeleteFailedAttempts(hash lntypes.Hash) error {
294-
ctx := context.TODO()
293+
func (p *KVStore) DeleteFailedAttempts(ctx context.Context,
294+
hash lntypes.Hash) error {
295295

296296
if !p.keepFailedPaymentAttempts {
297297
const failedHtlcsOnly = true

payments/db/payment_test.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,9 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
495495

496496
// Calling DeleteFailedAttempts on a failed payment should delete all
497497
// HTLCs.
498-
require.NoError(t, paymentDB.DeleteFailedAttempts(payments[0].id))
498+
require.NoError(t, paymentDB.DeleteFailedAttempts(
499+
t.Context(), payments[0].id,
500+
))
499501

500502
// Expect all HTLCs to be deleted if the config is set to delete them.
501503
if !keepFailedPaymentAttempts {
@@ -510,19 +512,25 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
510512
// operation are performed in general therefore we do NOT expect an
511513
// error in this case.
512514
if keepFailedPaymentAttempts {
513-
require.NoError(t, paymentDB.DeleteFailedAttempts(
514-
payments[1].id),
515+
err := paymentDB.DeleteFailedAttempts(
516+
t.Context(), payments[1].id,
515517
)
518+
require.NoError(t, err)
516519
} else {
517-
require.Error(t, paymentDB.DeleteFailedAttempts(payments[1].id))
520+
err := paymentDB.DeleteFailedAttempts(
521+
t.Context(), payments[1].id,
522+
)
523+
require.Error(t, err)
518524
}
519525

520526
// Since DeleteFailedAttempts returned an error, we should expect the
521527
// payment to be unchanged.
522528
assertDBPayments(t, paymentDB, payments)
523529

524530
// Cleaning up a successful payment should remove failed htlcs.
525-
require.NoError(t, paymentDB.DeleteFailedAttempts(payments[2].id))
531+
require.NoError(t, paymentDB.DeleteFailedAttempts(
532+
t.Context(), payments[2].id,
533+
))
526534

527535
// Expect all HTLCs except for the settled one to be deleted if the
528536
// config is set to delete them.
@@ -539,13 +547,17 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
539547
// payments, if the control tower is configured to keep failed
540548
// HTLCs.
541549
require.NoError(
542-
t, paymentDB.DeleteFailedAttempts(lntypes.ZeroHash),
550+
t, paymentDB.DeleteFailedAttempts(
551+
t.Context(), lntypes.ZeroHash,
552+
),
543553
)
544554
} else {
545555
// Attempting to cleanup a non-existent payment returns an
546556
// error.
547557
require.Error(
548-
t, paymentDB.DeleteFailedAttempts(lntypes.ZeroHash),
558+
t, paymentDB.DeleteFailedAttempts(
559+
t.Context(), lntypes.ZeroHash,
560+
),
549561
)
550562
}
551563
}

payments/db/sql_store.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,13 +640,14 @@ func (s *SQLStore) DeletePayment(ctx context.Context, paymentHash lntypes.Hash,
640640
// DeleteFailedAttempts removes all failed HTLCs from the db. It should
641641
// be called for a given payment whenever all inflight htlcs are
642642
// completed, and the payment has reached a final terminal state.
643-
func (s *SQLStore) DeleteFailedAttempts(paymentHash lntypes.Hash) error {
643+
func (s *SQLStore) DeleteFailedAttempts(ctx context.Context,
644+
paymentHash lntypes.Hash) error {
645+
644646
// In case we are configured to keep failed payment attempts, we exit
645647
// early.
646648
if s.keepFailedPaymentAttempts {
647649
return nil
648650
}
649-
ctx := context.TODO()
650651

651652
err := s.db.ExecTx(ctx, sqldb.WriteTxOpt(), func(db SQLQueries) error {
652653
// We first fetch the payment to get the payment ID.

routing/control_tower.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type ControlTower interface {
2626
// DeleteFailedAttempts removes all failed HTLCs from the db. It should
2727
// be called for a given payment whenever all inflight htlcs are
2828
// completed, and the payment has reached a final settled state.
29-
DeleteFailedAttempts(lntypes.Hash) error
29+
DeleteFailedAttempts(context.Context, lntypes.Hash) error
3030

3131
// RegisterAttempt atomically records the provided HTLCAttemptInfo.
3232
//
@@ -192,8 +192,10 @@ func (p *controlTower) InitPayment(ctx context.Context,
192192

193193
// DeleteFailedAttempts deletes all failed htlcs if the payment was
194194
// successfully settled.
195-
func (p *controlTower) DeleteFailedAttempts(paymentHash lntypes.Hash) error {
196-
return p.db.DeleteFailedAttempts(paymentHash)
195+
func (p *controlTower) DeleteFailedAttempts(ctx context.Context,
196+
paymentHash lntypes.Hash) error {
197+
198+
return p.db.DeleteFailedAttempts(ctx, paymentHash)
197199
}
198200

199201
// RegisterAttempt atomically records the provided HTLCAttemptInfo to the

routing/mock_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ func (m *mockControlTowerOld) InitPayment(_ context.Context,
328328
return nil
329329
}
330330

331-
func (m *mockControlTowerOld) DeleteFailedAttempts(phash lntypes.Hash) error {
331+
func (m *mockControlTowerOld) DeleteFailedAttempts(_ context.Context,
332+
phash lntypes.Hash) error {
333+
332334
p, ok := m.payments[phash]
333335
if !ok {
334336
return paymentsdb.ErrPaymentNotInitiated
@@ -742,7 +744,9 @@ func (m *mockControlTower) InitPayment(_ context.Context, phash lntypes.Hash,
742744
return args.Error(0)
743745
}
744746

745-
func (m *mockControlTower) DeleteFailedAttempts(phash lntypes.Hash) error {
747+
func (m *mockControlTower) DeleteFailedAttempts(_ context.Context,
748+
phash lntypes.Hash) error {
749+
746750
args := m.Called(phash)
747751
return args.Error(0)
748752
}

routing/payment_lifecycle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ lifecycle:
328328
// Optionally delete the failed attempts from the database. Depends on
329329
// the database options deleting attempts is not allowed so this will
330330
// just be a no-op.
331-
err = p.router.cfg.Control.DeleteFailedAttempts(p.identifier)
331+
err = p.router.cfg.Control.DeleteFailedAttempts(ctx, p.identifier)
332332
if err != nil {
333333
log.Errorf("Error deleting failed htlc attempts for payment "+
334334
"%v: %v", p.identifier, err)

0 commit comments

Comments
 (0)