Skip to content

Commit fcd4345

Browse files
authored
Merge pull request #10307 from ziggie1984/introduce-sql-schema-payments-part-5
[Part 5|*] Thread Contexts through payment methods Part 1
2 parents 4a6c7ab + 736f645 commit fcd4345

File tree

14 files changed

+399
-245
lines changed

14 files changed

+399
-245
lines changed

docs/release-notes/release-notes-0.21.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383
functions](https://github.com/lightningnetwork/lnd/pull/10368)
8484
* Finalize SQL payments implementation [enabling unit and itests
8585
for SQL backend](https://github.com/lightningnetwork/lnd/pull/10292)
86+
* [Thread context through payment
87+
db functions Part 1](https://github.com/lightningnetwork/lnd/pull/10307)
88+
8689

8790
## Code Health
8891

payments/db/interface.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,24 @@ type PaymentReader interface {
2121

2222
// FetchPayment fetches the payment corresponding to the given payment
2323
// hash.
24-
FetchPayment(paymentHash lntypes.Hash) (*MPPayment, error)
24+
FetchPayment(ctx context.Context,
25+
paymentHash lntypes.Hash) (*MPPayment, error)
2526

2627
// FetchInFlightPayments returns all payments with status InFlight.
27-
FetchInFlightPayments() ([]*MPPayment, error)
28+
FetchInFlightPayments(ctx context.Context) ([]*MPPayment, error)
2829
}
2930

3031
// PaymentWriter represents the interface to write operations to the payments
3132
// database.
3233
type PaymentWriter interface {
3334
// DeletePayment deletes a payment from the DB given its payment hash.
34-
DeletePayment(paymentHash lntypes.Hash, failedAttemptsOnly bool) error
35+
DeletePayment(ctx context.Context, paymentHash lntypes.Hash,
36+
failedAttemptsOnly bool) error
3537

3638
// DeletePayments deletes all payments from the DB given the specified
3739
// flags.
38-
DeletePayments(failedOnly, failedAttemptsOnly bool) (int, error)
40+
DeletePayments(ctx context.Context, failedOnly,
41+
failedAttemptsOnly bool) (int, error)
3942

4043
PaymentControl
4144
}
@@ -58,7 +61,7 @@ type PaymentControl interface {
5861
// exists in the database before creating a new payment. However, it
5962
// should allow the user making a subsequent payment if the payment is
6063
// in a Failed state.
61-
InitPayment(lntypes.Hash, *PaymentCreationInfo) error
64+
InitPayment(context.Context, lntypes.Hash, *PaymentCreationInfo) error
6265

6366
// RegisterAttempt atomically records the provided HTLCAttemptInfo.
6467
//
@@ -72,7 +75,8 @@ type PaymentControl interface {
7275
// - Result: 1700 sats sent, exceeding the payment amount
7376
// The payment router/controller layer is responsible for ensuring
7477
// serialized access per payment hash.
75-
RegisterAttempt(lntypes.Hash, *HTLCAttemptInfo) (*MPPayment, error)
78+
RegisterAttempt(context.Context, lntypes.Hash,
79+
*HTLCAttemptInfo) (*MPPayment, error)
7680

7781
// SettleAttempt marks the given attempt settled with the preimage. If
7882
// this is a multi shard payment, this might implicitly mean the
@@ -82,23 +86,25 @@ type PaymentControl interface {
8286
// error to prevent us from making duplicate payments to the same
8387
// payment hash. The provided preimage is atomically saved to the DB
8488
// for record keeping.
85-
SettleAttempt(lntypes.Hash, uint64, *HTLCSettleInfo) (*MPPayment, error)
89+
SettleAttempt(context.Context, lntypes.Hash, uint64,
90+
*HTLCSettleInfo) (*MPPayment, error)
8691

8792
// FailAttempt marks the given payment attempt failed.
88-
FailAttempt(lntypes.Hash, uint64, *HTLCFailInfo) (*MPPayment, error)
93+
FailAttempt(context.Context, lntypes.Hash, uint64,
94+
*HTLCFailInfo) (*MPPayment, error)
8995

9096
// Fail transitions a payment into the Failed state, and records
9197
// the ultimate reason the payment failed. Note that this should only
9298
// be called when all active attempts are already failed. After
9399
// invoking this method, InitPayment should return nil on its next call
94100
// for this payment hash, allowing the user to make a subsequent
95101
// payment.
96-
Fail(lntypes.Hash, FailureReason) (*MPPayment, error)
102+
Fail(context.Context, lntypes.Hash, FailureReason) (*MPPayment, error)
97103

98104
// DeleteFailedAttempts removes all failed HTLCs from the db. It should
99105
// be called for a given payment whenever all inflight htlcs are
100106
// completed, and the payment has reached a final terminal state.
101-
DeleteFailedAttempts(lntypes.Hash) error
107+
DeleteFailedAttempts(context.Context, lntypes.Hash) error
102108
}
103109

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

payments/db/kv_store.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func initKVStore(db kvdb.Backend) error {
186186
// making sure it does not already exist as an in-flight payment. When this
187187
// method returns successfully, the payment is guaranteed to be in the InFlight
188188
// state.
189-
func (p *KVStore) InitPayment(paymentHash lntypes.Hash,
189+
func (p *KVStore) InitPayment(_ context.Context, paymentHash lntypes.Hash,
190190
info *PaymentCreationInfo) error {
191191

192192
// Obtain a new sequence number for this payment. This is used
@@ -290,12 +290,14 @@ func (p *KVStore) InitPayment(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 {
293+
func (p *KVStore) DeleteFailedAttempts(ctx context.Context,
294+
hash lntypes.Hash) error {
295+
294296
// TODO(ziggie): Refactor to not mix application logic with database
295297
// logic. This decision should be made in the application layer.
296298
if !p.keepFailedPaymentAttempts {
297299
const failedHtlcsOnly = true
298-
err := p.DeletePayment(hash, failedHtlcsOnly)
300+
err := p.DeletePayment(ctx, hash, failedHtlcsOnly)
299301
if err != nil {
300302
return err
301303
}
@@ -359,7 +361,7 @@ func deserializePaymentIndex(r io.Reader) (lntypes.Hash, error) {
359361

360362
// RegisterAttempt atomically records the provided HTLCAttemptInfo to the
361363
// DB.
362-
func (p *KVStore) RegisterAttempt(paymentHash lntypes.Hash,
364+
func (p *KVStore) RegisterAttempt(_ context.Context, paymentHash lntypes.Hash,
363365
attempt *HTLCAttemptInfo) (*MPPayment, error) {
364366

365367
// Serialize the information before opening the db transaction.
@@ -430,7 +432,7 @@ func (p *KVStore) RegisterAttempt(paymentHash lntypes.Hash,
430432
// After invoking this method, InitPayment should always return an error to
431433
// prevent us from making duplicate payments to the same payment hash. The
432434
// provided preimage is atomically saved to the DB for record keeping.
433-
func (p *KVStore) SettleAttempt(hash lntypes.Hash,
435+
func (p *KVStore) SettleAttempt(_ context.Context, hash lntypes.Hash,
434436
attemptID uint64, settleInfo *HTLCSettleInfo) (*MPPayment, error) {
435437

436438
var b bytes.Buffer
@@ -443,7 +445,7 @@ func (p *KVStore) SettleAttempt(hash lntypes.Hash,
443445
}
444446

445447
// FailAttempt marks the given payment attempt failed.
446-
func (p *KVStore) FailAttempt(hash lntypes.Hash,
448+
func (p *KVStore) FailAttempt(_ context.Context, hash lntypes.Hash,
447449
attemptID uint64, failInfo *HTLCFailInfo) (*MPPayment, error) {
448450

449451
var b bytes.Buffer
@@ -528,7 +530,7 @@ func (p *KVStore) updateHtlcKey(paymentHash lntypes.Hash,
528530
// payment failed. After invoking this method, InitPayment should return nil on
529531
// its next call for this payment hash, allowing the switch to make a
530532
// subsequent payment.
531-
func (p *KVStore) Fail(paymentHash lntypes.Hash,
533+
func (p *KVStore) Fail(_ context.Context, paymentHash lntypes.Hash,
532534
reason FailureReason) (*MPPayment, error) {
533535

534536
var (
@@ -585,8 +587,8 @@ func (p *KVStore) Fail(paymentHash lntypes.Hash,
585587
}
586588

587589
// FetchPayment returns information about a payment from the database.
588-
func (p *KVStore) FetchPayment(paymentHash lntypes.Hash) (
589-
*MPPayment, error) {
590+
func (p *KVStore) FetchPayment(_ context.Context,
591+
paymentHash lntypes.Hash) (*MPPayment, error) {
590592

591593
var payment *MPPayment
592594
err := kvdb.View(p.db, func(tx kvdb.RTx) error {
@@ -741,7 +743,9 @@ func fetchPaymentStatus(bucket kvdb.RBucket) (PaymentStatus, error) {
741743
}
742744

743745
// FetchInFlightPayments returns all payments with status InFlight.
744-
func (p *KVStore) FetchInFlightPayments() ([]*MPPayment, error) {
746+
func (p *KVStore) FetchInFlightPayments(_ context.Context) ([]*MPPayment,
747+
error) {
748+
745749
var (
746750
inFlights []*MPPayment
747751
start = time.Now()
@@ -1275,7 +1279,7 @@ func fetchPaymentWithSequenceNumber(tx kvdb.RTx, paymentHash lntypes.Hash,
12751279
// DeletePayment deletes a payment from the DB given its payment hash. If
12761280
// failedHtlcsOnly is set, only failed HTLC attempts of the payment will be
12771281
// deleted.
1278-
func (p *KVStore) DeletePayment(paymentHash lntypes.Hash,
1282+
func (p *KVStore) DeletePayment(_ context.Context, paymentHash lntypes.Hash,
12791283
failedHtlcsOnly bool) error {
12801284

12811285
return kvdb.Update(p.db, func(tx kvdb.RwTx) error {
@@ -1372,7 +1376,7 @@ func (p *KVStore) DeletePayment(paymentHash lntypes.Hash,
13721376
// failedHtlcsOnly is set, the payment itself won't be deleted, only failed HTLC
13731377
// attempts. The method returns the number of deleted payments, which is always
13741378
// 0 if failedHtlcsOnly is set.
1375-
func (p *KVStore) DeletePayments(failedOnly,
1379+
func (p *KVStore) DeletePayments(_ context.Context, failedOnly,
13761380
failedHtlcsOnly bool) (int, error) {
13771381

13781382
var numPayments int

payments/db/kv_store_test.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
func TestKVStoreDeleteNonInFlight(t *testing.T) {
3131
t.Parallel()
3232

33+
ctx := t.Context()
34+
3335
paymentDB := NewKVTestDB(t)
3436

3537
// Create a sequence number for duplicate payments that will not collide
@@ -78,12 +80,12 @@ func TestKVStoreDeleteNonInFlight(t *testing.T) {
7880
require.NoError(t, err)
7981

8082
// Sends base htlc message which initiate StatusInFlight.
81-
err = paymentDB.InitPayment(info.PaymentIdentifier, info)
83+
err = paymentDB.InitPayment(ctx, info.PaymentIdentifier, info)
8284
if err != nil {
8385
t.Fatalf("unable to send htlc message: %v", err)
8486
}
8587
_, err = paymentDB.RegisterAttempt(
86-
info.PaymentIdentifier, attempt,
88+
ctx, info.PaymentIdentifier, attempt,
8789
)
8890
if err != nil {
8991
t.Fatalf("unable to send htlc message: %v", err)
@@ -98,7 +100,7 @@ func TestKVStoreDeleteNonInFlight(t *testing.T) {
98100
// Fail the payment attempt.
99101
htlcFailure := HTLCFailUnreadable
100102
_, err := paymentDB.FailAttempt(
101-
info.PaymentIdentifier, attempt.AttemptID,
103+
ctx, info.PaymentIdentifier, attempt.AttemptID,
102104
&HTLCFailInfo{
103105
Reason: htlcFailure,
104106
},
@@ -110,7 +112,7 @@ func TestKVStoreDeleteNonInFlight(t *testing.T) {
110112
// Fail the payment, which should moved it to Failed.
111113
failReason := FailureReasonNoRoute
112114
_, err = paymentDB.Fail(
113-
info.PaymentIdentifier, failReason,
115+
ctx, info.PaymentIdentifier, failReason,
114116
)
115117
if err != nil {
116118
t.Fatalf("unable to fail payment hash: %v", err)
@@ -131,7 +133,7 @@ func TestKVStoreDeleteNonInFlight(t *testing.T) {
131133
case p.success:
132134
// Verifies that status was changed to StatusSucceeded.
133135
_, err := paymentDB.SettleAttempt(
134-
info.PaymentIdentifier, attempt.AttemptID,
136+
ctx, info.PaymentIdentifier, attempt.AttemptID,
135137
&HTLCSettleInfo{
136138
Preimage: preimg,
137139
},
@@ -180,7 +182,7 @@ func TestKVStoreDeleteNonInFlight(t *testing.T) {
180182
}
181183

182184
// Delete all failed payments.
183-
numPayments, err := paymentDB.DeletePayments(true, false)
185+
numPayments, err := paymentDB.DeletePayments(ctx, true, false)
184186
require.NoError(t, err)
185187
require.EqualValues(t, 1, numPayments)
186188

@@ -216,7 +218,7 @@ func TestKVStoreDeleteNonInFlight(t *testing.T) {
216218
}
217219

218220
// Now delete all payments except in-flight.
219-
numPayments, err = paymentDB.DeletePayments(false, false)
221+
numPayments, err = paymentDB.DeletePayments(ctx, false, false)
220222
require.NoError(t, err)
221223
require.EqualValues(t, 2, numPayments)
222224

@@ -407,19 +409,21 @@ func deletePayment(t *testing.T, db kvdb.Backend, paymentHash lntypes.Hash,
407409
func TestFetchPaymentWithSequenceNumber(t *testing.T) {
408410
paymentDB := NewKVTestDB(t)
409411

412+
ctx := t.Context()
413+
410414
// Generate a test payment which does not have duplicates.
411415
noDuplicates, _, err := genInfo(t)
412416
require.NoError(t, err)
413417

414418
// Create a new payment entry in the database.
415419
err = paymentDB.InitPayment(
416-
noDuplicates.PaymentIdentifier, noDuplicates,
420+
ctx, noDuplicates.PaymentIdentifier, noDuplicates,
417421
)
418422
require.NoError(t, err)
419423

420424
// Fetch the payment so we can get its sequence nr.
421425
noDuplicatesPayment, err := paymentDB.FetchPayment(
422-
noDuplicates.PaymentIdentifier,
426+
ctx, noDuplicates.PaymentIdentifier,
423427
)
424428
require.NoError(t, err)
425429

@@ -429,13 +433,13 @@ func TestFetchPaymentWithSequenceNumber(t *testing.T) {
429433

430434
// Create a new payment entry in the database.
431435
err = paymentDB.InitPayment(
432-
hasDuplicates.PaymentIdentifier, hasDuplicates,
436+
ctx, hasDuplicates.PaymentIdentifier, hasDuplicates,
433437
)
434438
require.NoError(t, err)
435439

436440
// Fetch the payment so we can get its sequence nr.
437441
hasDuplicatesPayment, err := paymentDB.FetchPayment(
438-
hasDuplicates.PaymentIdentifier,
442+
ctx, hasDuplicates.PaymentIdentifier,
439443
)
440444
require.NoError(t, err)
441445

@@ -740,14 +744,14 @@ func TestKVStoreQueryPaymentsDuplicates(t *testing.T) {
740744

741745
// Create a new payment entry in the database.
742746
err = paymentDB.InitPayment(
743-
info.PaymentIdentifier, info,
747+
ctx, info.PaymentIdentifier, info,
744748
)
745749
require.NoError(t, err)
746750

747751
// Immediately delete the payment with index 2.
748752
if i == 1 {
749753
pmt, err := paymentDB.FetchPayment(
750-
info.PaymentIdentifier,
754+
ctx, info.PaymentIdentifier,
751755
)
752756
require.NoError(t, err)
753757

@@ -764,7 +768,7 @@ func TestKVStoreQueryPaymentsDuplicates(t *testing.T) {
764768
// duplicate payments will always be succeeded.
765769
if i == (nonDuplicatePayments - 1) {
766770
pmt, err := paymentDB.FetchPayment(
767-
info.PaymentIdentifier,
771+
ctx, info.PaymentIdentifier,
768772
)
769773
require.NoError(t, err)
770774

0 commit comments

Comments
 (0)