channeldb: rename pControl to paymentDB in tests

This commit is contained in:
ziggie
2025-08-01 17:03:11 +02:00
parent 2d07d44d1a
commit 8bd4744ee8
2 changed files with 114 additions and 114 deletions

View File

@@ -63,64 +63,64 @@ func TestKVPaymentsDBSwitchFail(t *testing.T) {
db, err := MakeTestDB(t) db, err := MakeTestDB(t)
require.NoError(t, err, "unable to init db") require.NoError(t, err, "unable to init db")
pControl := NewKVPaymentsDB(db) paymentDB := NewKVPaymentsDB(db)
info, attempt, preimg, err := genInfo(t) info, attempt, preimg, err := genInfo(t)
require.NoError(t, err, "unable to generate htlc message") require.NoError(t, err, "unable to generate htlc message")
// Sends base htlc message which initiate StatusInFlight. // Sends base htlc message which initiate StatusInFlight.
err = pControl.InitPayment(info.PaymentIdentifier, info) err = paymentDB.InitPayment(info.PaymentIdentifier, info)
require.NoError(t, err, "unable to send htlc message") require.NoError(t, err, "unable to send htlc message")
assertPaymentIndex(t, pControl, info.PaymentIdentifier) assertPaymentIndex(t, paymentDB, info.PaymentIdentifier)
assertPaymentStatus( assertPaymentStatus(
t, pControl, info.PaymentIdentifier, StatusInitiated, t, paymentDB, info.PaymentIdentifier, StatusInitiated,
) )
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, nil, nil, t, paymentDB, info.PaymentIdentifier, info, nil, nil,
) )
// Fail the payment, which should moved it to Failed. // Fail the payment, which should moved it to Failed.
failReason := FailureReasonNoRoute failReason := FailureReasonNoRoute
_, err = pControl.Fail(info.PaymentIdentifier, failReason) _, err = paymentDB.Fail(info.PaymentIdentifier, failReason)
require.NoError(t, err, "unable to fail payment hash") require.NoError(t, err, "unable to fail payment hash")
// Verify the status is indeed Failed. // Verify the status is indeed Failed.
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusFailed) assertPaymentStatus(t, paymentDB, info.PaymentIdentifier, StatusFailed)
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, &failReason, nil, t, paymentDB, info.PaymentIdentifier, info, &failReason, nil,
) )
// Lookup the payment so we can get its old sequence number before it is // Lookup the payment so we can get its old sequence number before it is
// overwritten. // overwritten.
payment, err := pControl.FetchPayment(info.PaymentIdentifier) payment, err := paymentDB.FetchPayment(info.PaymentIdentifier)
require.NoError(t, err) require.NoError(t, err)
// Sends the htlc again, which should succeed since the prior payment // Sends the htlc again, which should succeed since the prior payment
// failed. // failed.
err = pControl.InitPayment(info.PaymentIdentifier, info) err = paymentDB.InitPayment(info.PaymentIdentifier, info)
require.NoError(t, err, "unable to send htlc message") require.NoError(t, err, "unable to send htlc message")
// Check that our index has been updated, and the old index has been // Check that our index has been updated, and the old index has been
// removed. // removed.
assertPaymentIndex(t, pControl, info.PaymentIdentifier) assertPaymentIndex(t, paymentDB, info.PaymentIdentifier)
assertNoIndex(t, pControl, payment.SequenceNum) assertNoIndex(t, paymentDB, payment.SequenceNum)
assertPaymentStatus( assertPaymentStatus(
t, pControl, info.PaymentIdentifier, StatusInitiated, t, paymentDB, info.PaymentIdentifier, StatusInitiated,
) )
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, nil, nil, t, paymentDB, info.PaymentIdentifier, info, nil, nil,
) )
// Record a new attempt. In this test scenario, the attempt fails. // Record a new attempt. In this test scenario, the attempt fails.
// However, this is not communicated to control tower in the current // However, this is not communicated to control tower in the current
// implementation. It only registers the initiation of the attempt. // implementation. It only registers the initiation of the attempt.
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt) _, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt)
require.NoError(t, err, "unable to register attempt") require.NoError(t, err, "unable to register attempt")
htlcReason := HTLCFailUnreadable htlcReason := HTLCFailUnreadable
_, err = pControl.FailAttempt( _, err = paymentDB.FailAttempt(
info.PaymentIdentifier, attempt.AttemptID, info.PaymentIdentifier, attempt.AttemptID,
&HTLCFailInfo{ &HTLCFailInfo{
Reason: htlcReason, Reason: htlcReason,
@@ -130,7 +130,7 @@ func TestKVPaymentsDBSwitchFail(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
assertPaymentStatus( assertPaymentStatus(
t, pControl, info.PaymentIdentifier, StatusInFlight, t, paymentDB, info.PaymentIdentifier, StatusInFlight,
) )
htlc := &htlcStatus{ htlc := &htlcStatus{
@@ -138,25 +138,25 @@ func TestKVPaymentsDBSwitchFail(t *testing.T) {
failure: &htlcReason, failure: &htlcReason,
} }
assertPaymentInfo(t, pControl, info.PaymentIdentifier, info, nil, htlc) assertPaymentInfo(t, paymentDB, info.PaymentIdentifier, info, nil, htlc)
// Record another attempt. // Record another attempt.
attempt.AttemptID = 1 attempt.AttemptID = 1
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt) _, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt)
require.NoError(t, err, "unable to send htlc message") require.NoError(t, err, "unable to send htlc message")
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) assertPaymentStatus(t, paymentDB, info.PaymentIdentifier, StatusInFlight)
htlc = &htlcStatus{ htlc = &htlcStatus{
HTLCAttemptInfo: attempt, HTLCAttemptInfo: attempt,
} }
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, nil, htlc, t, paymentDB, info.PaymentIdentifier, info, nil, htlc,
) )
// Settle the attempt and verify that status was changed to // Settle the attempt and verify that status was changed to
// StatusSucceeded. // StatusSucceeded.
payment, err = pControl.SettleAttempt( payment, err = paymentDB.SettleAttempt(
info.PaymentIdentifier, attempt.AttemptID, info.PaymentIdentifier, attempt.AttemptID,
&HTLCSettleInfo{ &HTLCSettleInfo{
Preimage: preimg, Preimage: preimg,
@@ -176,16 +176,16 @@ func TestKVPaymentsDBSwitchFail(t *testing.T) {
spew.Sdump(payment.HTLCs[0].Route), err) spew.Sdump(payment.HTLCs[0].Route), err)
} }
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusSucceeded) assertPaymentStatus(t, paymentDB, info.PaymentIdentifier, StatusSucceeded)
htlc.settle = &preimg htlc.settle = &preimg
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, nil, htlc, t, paymentDB, info.PaymentIdentifier, info, nil, htlc,
) )
// Attempt a final payment, which should now fail since the prior // Attempt a final payment, which should now fail since the prior
// payment succeed. // payment succeed.
err = pControl.InitPayment(info.PaymentIdentifier, info) err = paymentDB.InitPayment(info.PaymentIdentifier, info)
if !errors.Is(err, ErrAlreadyPaid) { if !errors.Is(err, ErrAlreadyPaid) {
t.Fatalf("unable to send htlc message: %v", err) t.Fatalf("unable to send htlc message: %v", err)
} }
@@ -199,63 +199,63 @@ func TestKVPaymentsDBSwitchDoubleSend(t *testing.T) {
db, err := MakeTestDB(t) db, err := MakeTestDB(t)
require.NoError(t, err, "unable to init db") require.NoError(t, err, "unable to init db")
pControl := NewKVPaymentsDB(db) paymentDB := NewKVPaymentsDB(db)
info, attempt, preimg, err := genInfo(t) info, attempt, preimg, err := genInfo(t)
require.NoError(t, err, "unable to generate htlc message") require.NoError(t, err, "unable to generate htlc message")
// Sends base htlc message which initiate base status and move it to // Sends base htlc message which initiate base status and move it to
// StatusInFlight and verifies that it was changed. // StatusInFlight and verifies that it was changed.
err = pControl.InitPayment(info.PaymentIdentifier, info) err = paymentDB.InitPayment(info.PaymentIdentifier, info)
require.NoError(t, err, "unable to send htlc message") require.NoError(t, err, "unable to send htlc message")
assertPaymentIndex(t, pControl, info.PaymentIdentifier) assertPaymentIndex(t, paymentDB, info.PaymentIdentifier)
assertPaymentStatus( assertPaymentStatus(
t, pControl, info.PaymentIdentifier, StatusInitiated, t, paymentDB, info.PaymentIdentifier, StatusInitiated,
) )
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, nil, nil, t, paymentDB, info.PaymentIdentifier, info, nil, nil,
) )
// Try to initiate double sending of htlc message with the same // Try to initiate double sending of htlc message with the same
// payment hash, should result in error indicating that payment has // payment hash, should result in error indicating that payment has
// already been sent. // already been sent.
err = pControl.InitPayment(info.PaymentIdentifier, info) err = paymentDB.InitPayment(info.PaymentIdentifier, info)
require.ErrorIs(t, err, ErrPaymentExists) require.ErrorIs(t, err, ErrPaymentExists)
// Record an attempt. // Record an attempt.
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt) _, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt)
require.NoError(t, err, "unable to send htlc message") require.NoError(t, err, "unable to send htlc message")
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) assertPaymentStatus(t, paymentDB, info.PaymentIdentifier, StatusInFlight)
htlc := &htlcStatus{ htlc := &htlcStatus{
HTLCAttemptInfo: attempt, HTLCAttemptInfo: attempt,
} }
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, nil, htlc, t, paymentDB, info.PaymentIdentifier, info, nil, htlc,
) )
// Sends base htlc message which initiate StatusInFlight. // Sends base htlc message which initiate StatusInFlight.
err = pControl.InitPayment(info.PaymentIdentifier, info) err = paymentDB.InitPayment(info.PaymentIdentifier, info)
if !errors.Is(err, ErrPaymentInFlight) { if !errors.Is(err, ErrPaymentInFlight) {
t.Fatalf("payment control wrong behaviour: " + t.Fatalf("payment control wrong behaviour: " +
"double sending must trigger ErrPaymentInFlight error") "double sending must trigger ErrPaymentInFlight error")
} }
// After settling, the error should be ErrAlreadyPaid. // After settling, the error should be ErrAlreadyPaid.
_, err = pControl.SettleAttempt( _, err = paymentDB.SettleAttempt(
info.PaymentIdentifier, attempt.AttemptID, info.PaymentIdentifier, attempt.AttemptID,
&HTLCSettleInfo{ &HTLCSettleInfo{
Preimage: preimg, Preimage: preimg,
}, },
) )
require.NoError(t, err, "error shouldn't have been received, got") require.NoError(t, err, "error shouldn't have been received, got")
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusSucceeded) assertPaymentStatus(t, paymentDB, info.PaymentIdentifier, StatusSucceeded)
htlc.settle = &preimg htlc.settle = &preimg
assertPaymentInfo(t, pControl, info.PaymentIdentifier, info, nil, htlc) assertPaymentInfo(t, paymentDB, info.PaymentIdentifier, info, nil, htlc)
err = pControl.InitPayment(info.PaymentIdentifier, info) err = paymentDB.InitPayment(info.PaymentIdentifier, info)
if !errors.Is(err, ErrAlreadyPaid) { if !errors.Is(err, ErrAlreadyPaid) {
t.Fatalf("unable to send htlc message: %v", err) t.Fatalf("unable to send htlc message: %v", err)
} }
@@ -269,13 +269,13 @@ func TestKVPaymentsDBSuccessesWithoutInFlight(t *testing.T) {
db, err := MakeTestDB(t) db, err := MakeTestDB(t)
require.NoError(t, err, "unable to init db") require.NoError(t, err, "unable to init db")
pControl := NewKVPaymentsDB(db) paymentDB := NewKVPaymentsDB(db)
info, _, preimg, err := genInfo(t) info, _, preimg, err := genInfo(t)
require.NoError(t, err, "unable to generate htlc message") require.NoError(t, err, "unable to generate htlc message")
// Attempt to complete the payment should fail. // Attempt to complete the payment should fail.
_, err = pControl.SettleAttempt( _, err = paymentDB.SettleAttempt(
info.PaymentIdentifier, 0, info.PaymentIdentifier, 0,
&HTLCSettleInfo{ &HTLCSettleInfo{
Preimage: preimg, Preimage: preimg,
@@ -294,13 +294,13 @@ func TestKVPaymentsDBFailsWithoutInFlight(t *testing.T) {
db, err := MakeTestDB(t) db, err := MakeTestDB(t)
require.NoError(t, err, "unable to init db") require.NoError(t, err, "unable to init db")
pControl := NewKVPaymentsDB(db) paymentDB := NewKVPaymentsDB(db)
info, _, _, err := genInfo(t) info, _, _, err := genInfo(t)
require.NoError(t, err, "unable to generate htlc message") require.NoError(t, err, "unable to generate htlc message")
// Calling Fail should return an error. // Calling Fail should return an error.
_, err = pControl.Fail(info.PaymentIdentifier, FailureReasonNoRoute) _, err = paymentDB.Fail(info.PaymentIdentifier, FailureReasonNoRoute)
if err != ErrPaymentNotInitiated { if err != ErrPaymentNotInitiated {
t.Fatalf("expected ErrPaymentNotInitiated, got %v", err) t.Fatalf("expected ErrPaymentNotInitiated, got %v", err)
} }
@@ -319,7 +319,7 @@ func TestKVPaymentsDBDeleteNonInFlight(t *testing.T) {
// start at 1, so 9999 is a safe bet for this test. // start at 1, so 9999 is a safe bet for this test.
var duplicateSeqNr = 9999 var duplicateSeqNr = 9999
pControl := NewKVPaymentsDB(db) paymentDB := NewKVPaymentsDB(db)
payments := []struct { payments := []struct {
failed bool failed bool
@@ -357,11 +357,11 @@ func TestKVPaymentsDBDeleteNonInFlight(t *testing.T) {
} }
// Sends base htlc message which initiate StatusInFlight. // Sends base htlc message which initiate StatusInFlight.
err = pControl.InitPayment(info.PaymentIdentifier, info) err = paymentDB.InitPayment(info.PaymentIdentifier, info)
if err != nil { if err != nil {
t.Fatalf("unable to send htlc message: %v", err) t.Fatalf("unable to send htlc message: %v", err)
} }
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt) _, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt)
if err != nil { if err != nil {
t.Fatalf("unable to send htlc message: %v", err) t.Fatalf("unable to send htlc message: %v", err)
} }
@@ -373,7 +373,7 @@ func TestKVPaymentsDBDeleteNonInFlight(t *testing.T) {
if p.failed { if p.failed {
// Fail the payment attempt. // Fail the payment attempt.
htlcFailure := HTLCFailUnreadable htlcFailure := HTLCFailUnreadable
_, err := pControl.FailAttempt( _, err := paymentDB.FailAttempt(
info.PaymentIdentifier, attempt.AttemptID, info.PaymentIdentifier, attempt.AttemptID,
&HTLCFailInfo{ &HTLCFailInfo{
Reason: htlcFailure, Reason: htlcFailure,
@@ -385,22 +385,22 @@ func TestKVPaymentsDBDeleteNonInFlight(t *testing.T) {
// Fail the payment, which should moved it to Failed. // Fail the payment, which should moved it to Failed.
failReason := FailureReasonNoRoute failReason := FailureReasonNoRoute
_, err = pControl.Fail(info.PaymentIdentifier, failReason) _, err = paymentDB.Fail(info.PaymentIdentifier, failReason)
if err != nil { if err != nil {
t.Fatalf("unable to fail payment hash: %v", err) t.Fatalf("unable to fail payment hash: %v", err)
} }
// Verify the status is indeed Failed. // Verify the status is indeed Failed.
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusFailed) assertPaymentStatus(t, paymentDB, info.PaymentIdentifier, StatusFailed)
htlc.failure = &htlcFailure htlc.failure = &htlcFailure
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, t, paymentDB, info.PaymentIdentifier, info,
&failReason, htlc, &failReason, htlc,
) )
} else if p.success { } else if p.success {
// Verifies that status was changed to StatusSucceeded. // Verifies that status was changed to StatusSucceeded.
_, err := pControl.SettleAttempt( _, err := paymentDB.SettleAttempt(
info.PaymentIdentifier, attempt.AttemptID, info.PaymentIdentifier, attempt.AttemptID,
&HTLCSettleInfo{ &HTLCSettleInfo{
Preimage: preimg, Preimage: preimg,
@@ -410,18 +410,18 @@ func TestKVPaymentsDBDeleteNonInFlight(t *testing.T) {
t.Fatalf("error shouldn't have been received, got: %v", err) t.Fatalf("error shouldn't have been received, got: %v", err)
} }
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusSucceeded) assertPaymentStatus(t, paymentDB, info.PaymentIdentifier, StatusSucceeded)
htlc.settle = &preimg htlc.settle = &preimg
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, nil, htlc, t, paymentDB, info.PaymentIdentifier, info, nil, htlc,
) )
numSuccess++ numSuccess++
} else { } else {
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) assertPaymentStatus(t, paymentDB, info.PaymentIdentifier, StatusInFlight)
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, nil, htlc, t, paymentDB, info.PaymentIdentifier, info, nil, htlc,
) )
numInflight++ numInflight++
@@ -431,7 +431,7 @@ func TestKVPaymentsDBDeleteNonInFlight(t *testing.T) {
// add one. // add one.
if p.hasDuplicate { if p.hasDuplicate {
appendDuplicatePayment( appendDuplicatePayment(
t, pControl.db, info.PaymentIdentifier, t, paymentDB.db, info.PaymentIdentifier,
uint64(duplicateSeqNr), preimg, uint64(duplicateSeqNr), preimg,
) )
duplicateSeqNr++ duplicateSeqNr++
@@ -521,7 +521,7 @@ func TestKVPaymentsDBDeletePayments(t *testing.T) {
db, err := MakeTestDB(t) db, err := MakeTestDB(t)
require.NoError(t, err, "unable to init db") require.NoError(t, err, "unable to init db")
pControl := NewKVPaymentsDB(db) paymentDB := NewKVPaymentsDB(db)
// Register three payments: // Register three payments:
// 1. A payment with two failed attempts. // 1. A payment with two failed attempts.
@@ -535,7 +535,7 @@ func TestKVPaymentsDBDeletePayments(t *testing.T) {
// Use helper function to register the test payments in the data and // Use helper function to register the test payments in the data and
// populate the data to the payments slice. // populate the data to the payments slice.
createTestPayments(t, pControl, payments) createTestPayments(t, paymentDB, payments)
// Check that all payments are there as we added them. // Check that all payments are there as we added them.
assertPayments(t, db, payments) assertPayments(t, db, payments)
@@ -582,7 +582,7 @@ func TestKVPaymentsDBDeleteSinglePayment(t *testing.T) {
db, err := MakeTestDB(t) db, err := MakeTestDB(t)
require.NoError(t, err, "unable to init db") require.NoError(t, err, "unable to init db")
pControl := NewKVPaymentsDB(db) paymentDB := NewKVPaymentsDB(db)
// Register four payments: // Register four payments:
// All payments will have one failed HTLC attempt and one HTLC attempt // All payments will have one failed HTLC attempt and one HTLC attempt
@@ -607,7 +607,7 @@ func TestKVPaymentsDBDeleteSinglePayment(t *testing.T) {
// Use helper function to register the test payments in the data and // Use helper function to register the test payments in the data and
// populate the data to the payments slice. // populate the data to the payments slice.
createTestPayments(t, pControl, payments) createTestPayments(t, paymentDB, payments)
// Check that all payments are there as we added them. // Check that all payments are there as we added them.
assertPayments(t, db, payments) assertPayments(t, db, payments)
@@ -687,7 +687,7 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
t.Fatalf("unable to init db: %v", err) t.Fatalf("unable to init db: %v", err)
} }
pControl := NewKVPaymentsDB(db) paymentDB := NewKVPaymentsDB(db)
info, attempt, preimg, err := genInfo(t) info, attempt, preimg, err := genInfo(t)
if err != nil { if err != nil {
@@ -695,17 +695,17 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
} }
// Init the payment, moving it to the StatusInFlight state. // Init the payment, moving it to the StatusInFlight state.
err = pControl.InitPayment(info.PaymentIdentifier, info) err = paymentDB.InitPayment(info.PaymentIdentifier, info)
if err != nil { if err != nil {
t.Fatalf("unable to send htlc message: %v", err) t.Fatalf("unable to send htlc message: %v", err)
} }
assertPaymentIndex(t, pControl, info.PaymentIdentifier) assertPaymentIndex(t, paymentDB, info.PaymentIdentifier)
assertPaymentStatus( assertPaymentStatus(
t, pControl, info.PaymentIdentifier, StatusInitiated, t, paymentDB, info.PaymentIdentifier, StatusInitiated,
) )
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, nil, nil, t, paymentDB, info.PaymentIdentifier, info, nil, nil,
) )
// Create three unique attempts we'll use for the test, and // Create three unique attempts we'll use for the test, and
@@ -724,12 +724,12 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
a.AttemptID = i a.AttemptID = i
attempts = append(attempts, &a) attempts = append(attempts, &a)
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &a) _, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, &a)
if err != nil { if err != nil {
t.Fatalf("unable to send htlc message: %v", err) t.Fatalf("unable to send htlc message: %v", err)
} }
assertPaymentStatus( assertPaymentStatus(
t, pControl, info.PaymentIdentifier, t, paymentDB, info.PaymentIdentifier,
StatusInFlight, StatusInFlight,
) )
@@ -737,7 +737,7 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
HTLCAttemptInfo: &a, HTLCAttemptInfo: &a,
} }
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, nil, htlc, t, paymentDB, info.PaymentIdentifier, info, nil, htlc,
) )
} }
@@ -746,13 +746,13 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
// will be too large. // will be too large.
b := *attempt b := *attempt
b.AttemptID = 3 b.AttemptID = 3
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b) _, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, &b)
require.ErrorIs(t, err, ErrValueExceedsAmt) require.ErrorIs(t, err, ErrValueExceedsAmt)
// Fail the second attempt. // Fail the second attempt.
a := attempts[1] a := attempts[1]
htlcFail := HTLCFailUnreadable htlcFail := HTLCFailUnreadable
_, err = pControl.FailAttempt( _, err = paymentDB.FailAttempt(
info.PaymentIdentifier, a.AttemptID, info.PaymentIdentifier, a.AttemptID,
&HTLCFailInfo{ &HTLCFailInfo{
Reason: htlcFail, Reason: htlcFail,
@@ -767,11 +767,11 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
failure: &htlcFail, failure: &htlcFail,
} }
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, nil, htlc, t, paymentDB, info.PaymentIdentifier, info, nil, htlc,
) )
// Payment should still be in-flight. // Payment should still be in-flight.
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) assertPaymentStatus(t, paymentDB, info.PaymentIdentifier, StatusInFlight)
// Depending on the test case, settle or fail the first attempt. // Depending on the test case, settle or fail the first attempt.
a = attempts[0] a = attempts[0]
@@ -781,7 +781,7 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
var firstFailReason *FailureReason var firstFailReason *FailureReason
if test.settleFirst { if test.settleFirst {
_, err := pControl.SettleAttempt( _, err := paymentDB.SettleAttempt(
info.PaymentIdentifier, a.AttemptID, info.PaymentIdentifier, a.AttemptID,
&HTLCSettleInfo{ &HTLCSettleInfo{
Preimage: preimg, Preimage: preimg,
@@ -795,10 +795,10 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
// Assert that the HTLC has had the preimage recorded. // Assert that the HTLC has had the preimage recorded.
htlc.settle = &preimg htlc.settle = &preimg
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, nil, htlc, t, paymentDB, info.PaymentIdentifier, info, nil, htlc,
) )
} else { } else {
_, err := pControl.FailAttempt( _, err := paymentDB.FailAttempt(
info.PaymentIdentifier, a.AttemptID, info.PaymentIdentifier, a.AttemptID,
&HTLCFailInfo{ &HTLCFailInfo{
Reason: htlcFail, Reason: htlcFail,
@@ -812,13 +812,13 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
// Assert the failure was recorded. // Assert the failure was recorded.
htlc.failure = &htlcFail htlc.failure = &htlcFail
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, nil, htlc, t, paymentDB, info.PaymentIdentifier, info, nil, htlc,
) )
// We also record a payment level fail, to move it into // We also record a payment level fail, to move it into
// a terminal state. // a terminal state.
failReason := FailureReasonNoRoute failReason := FailureReasonNoRoute
_, err = pControl.Fail(info.PaymentIdentifier, failReason) _, err = paymentDB.Fail(info.PaymentIdentifier, failReason)
if err != nil { if err != nil {
t.Fatalf("unable to fail payment hash: %v", err) t.Fatalf("unable to fail payment hash: %v", err)
} }
@@ -830,7 +830,7 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
// The payment is now considered pending fail, since // The payment is now considered pending fail, since
// there is still an active HTLC. // there is still an active HTLC.
assertPaymentStatus( assertPaymentStatus(
t, pControl, info.PaymentIdentifier, t, paymentDB, info.PaymentIdentifier,
StatusInFlight, StatusInFlight,
) )
} }
@@ -839,14 +839,14 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
// that the payment has reached a terminal condition. // that the payment has reached a terminal condition.
b = *attempt b = *attempt
b.AttemptID = 3 b.AttemptID = 3
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b) _, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, &b)
if test.settleFirst { if test.settleFirst {
require.ErrorIs(t, err, ErrPaymentPendingSettled) require.ErrorIs(t, err, ErrPaymentPendingSettled)
} else { } else {
require.ErrorIs(t, err, ErrPaymentPendingFailed) require.ErrorIs(t, err, ErrPaymentPendingFailed)
} }
assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) assertPaymentStatus(t, paymentDB, info.PaymentIdentifier, StatusInFlight)
// Settle or fail the remaining attempt based on the testcase. // Settle or fail the remaining attempt based on the testcase.
a = attempts[2] a = attempts[2]
@@ -855,7 +855,7 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
} }
if test.settleLast { if test.settleLast {
// Settle the last outstanding attempt. // Settle the last outstanding attempt.
_, err = pControl.SettleAttempt( _, err = paymentDB.SettleAttempt(
info.PaymentIdentifier, a.AttemptID, info.PaymentIdentifier, a.AttemptID,
&HTLCSettleInfo{ &HTLCSettleInfo{
Preimage: preimg, Preimage: preimg,
@@ -865,12 +865,12 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
htlc.settle = &preimg htlc.settle = &preimg
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, t, paymentDB, info.PaymentIdentifier,
info, firstFailReason, htlc, info, firstFailReason, htlc,
) )
} else { } else {
// Fail the attempt. // Fail the attempt.
_, err := pControl.FailAttempt( _, err := paymentDB.FailAttempt(
info.PaymentIdentifier, a.AttemptID, info.PaymentIdentifier, a.AttemptID,
&HTLCFailInfo{ &HTLCFailInfo{
Reason: htlcFail, Reason: htlcFail,
@@ -884,7 +884,7 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
// Assert the failure was recorded. // Assert the failure was recorded.
htlc.failure = &htlcFail htlc.failure = &htlcFail
assertPaymentInfo( assertPaymentInfo(
t, pControl, info.PaymentIdentifier, info, t, paymentDB, info.PaymentIdentifier, info,
firstFailReason, htlc, firstFailReason, htlc,
) )
@@ -893,7 +893,7 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
// write a terminal failure to the database without // write a terminal failure to the database without
// syncing. // syncing.
failReason := FailureReasonPaymentDetails failReason := FailureReasonPaymentDetails
_, err = pControl.Fail(info.PaymentIdentifier, failReason) _, err = paymentDB.Fail(info.PaymentIdentifier, failReason)
require.NoError(t, err, "unable to fail") require.NoError(t, err, "unable to fail")
} }
@@ -926,11 +926,11 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
} }
assertPaymentStatus( assertPaymentStatus(
t, pControl, info.PaymentIdentifier, finalStatus, t, paymentDB, info.PaymentIdentifier, finalStatus,
) )
// Finally assert we cannot register more attempts. // Finally assert we cannot register more attempts.
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b) _, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, &b)
require.Equal(t, registerErr, err) require.Equal(t, registerErr, err)
} }
@@ -951,13 +951,13 @@ func TestKVPaymentsDBMPPRecordValidation(t *testing.T) {
db, err := MakeTestDB(t) db, err := MakeTestDB(t)
require.NoError(t, err, "unable to init db") require.NoError(t, err, "unable to init db")
pControl := NewKVPaymentsDB(db) paymentDB := NewKVPaymentsDB(db)
info, attempt, _, err := genInfo(t) info, attempt, _, err := genInfo(t)
require.NoError(t, err, "unable to generate htlc message") require.NoError(t, err, "unable to generate htlc message")
// Init the payment. // Init the payment.
err = pControl.InitPayment(info.PaymentIdentifier, info) err = paymentDB.InitPayment(info.PaymentIdentifier, info)
require.NoError(t, err, "unable to send htlc message") require.NoError(t, err, "unable to send htlc message")
// Create three unique attempts we'll use for the test, and // Create three unique attempts we'll use for the test, and
@@ -970,14 +970,14 @@ func TestKVPaymentsDBMPPRecordValidation(t *testing.T) {
info.Value, [32]byte{1}, info.Value, [32]byte{1},
) )
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt) _, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt)
require.NoError(t, err, "unable to send htlc message") require.NoError(t, err, "unable to send htlc message")
// Now try to register a non-MPP attempt, which should fail. // Now try to register a non-MPP attempt, which should fail.
b := *attempt b := *attempt
b.AttemptID = 1 b.AttemptID = 1
b.Route.FinalHop().MPP = nil b.Route.FinalHop().MPP = nil
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b) _, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, &b)
if err != ErrMPPayment { if err != ErrMPPayment {
t.Fatalf("expected ErrMPPayment, got: %v", err) t.Fatalf("expected ErrMPPayment, got: %v", err)
} }
@@ -986,7 +986,7 @@ func TestKVPaymentsDBMPPRecordValidation(t *testing.T) {
b.Route.FinalHop().MPP = record.NewMPP( b.Route.FinalHop().MPP = record.NewMPP(
info.Value, [32]byte{2}, info.Value, [32]byte{2},
) )
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b) _, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, &b)
if err != ErrMPPPaymentAddrMismatch { if err != ErrMPPPaymentAddrMismatch {
t.Fatalf("expected ErrMPPPaymentAddrMismatch, got: %v", err) t.Fatalf("expected ErrMPPPaymentAddrMismatch, got: %v", err)
} }
@@ -995,7 +995,7 @@ func TestKVPaymentsDBMPPRecordValidation(t *testing.T) {
b.Route.FinalHop().MPP = record.NewMPP( b.Route.FinalHop().MPP = record.NewMPP(
info.Value/2, [32]byte{1}, info.Value/2, [32]byte{1},
) )
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b) _, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, &b)
if err != ErrMPPTotalAmountMismatch { if err != ErrMPPTotalAmountMismatch {
t.Fatalf("expected ErrMPPTotalAmountMismatch, got: %v", err) t.Fatalf("expected ErrMPPTotalAmountMismatch, got: %v", err)
} }
@@ -1005,11 +1005,11 @@ func TestKVPaymentsDBMPPRecordValidation(t *testing.T) {
info, attempt, _, err = genInfo(t) info, attempt, _, err = genInfo(t)
require.NoError(t, err, "unable to generate htlc message") require.NoError(t, err, "unable to generate htlc message")
err = pControl.InitPayment(info.PaymentIdentifier, info) err = paymentDB.InitPayment(info.PaymentIdentifier, info)
require.NoError(t, err, "unable to send htlc message") require.NoError(t, err, "unable to send htlc message")
attempt.Route.FinalHop().MPP = nil attempt.Route.FinalHop().MPP = nil
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt) _, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, attempt)
require.NoError(t, err, "unable to send htlc message") require.NoError(t, err, "unable to send htlc message")
// Attempt to register an MPP attempt, which should fail. // Attempt to register an MPP attempt, which should fail.
@@ -1019,7 +1019,7 @@ func TestKVPaymentsDBMPPRecordValidation(t *testing.T) {
info.Value, [32]byte{1}, info.Value, [32]byte{1},
) )
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b) _, err = paymentDB.RegisterAttempt(info.PaymentIdentifier, &b)
if err != ErrNonMPPayment { if err != ErrNonMPPayment {
t.Fatalf("expected ErrNonMPPayment, got: %v", err) t.Fatalf("expected ErrNonMPPayment, got: %v", err)
} }
@@ -1044,7 +1044,7 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
require.NoError(t, err, "unable to init db") require.NoError(t, err, "unable to init db")
db.keepFailedPaymentAttempts = keepFailedPaymentAttempts db.keepFailedPaymentAttempts = keepFailedPaymentAttempts
pControl := NewKVPaymentsDB(db) paymentDB := NewKVPaymentsDB(db)
// Register three payments: // Register three payments:
// All payments will have one failed HTLC attempt and one HTLC attempt // All payments will have one failed HTLC attempt and one HTLC attempt
@@ -1067,14 +1067,14 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
// Use helper function to register the test payments in the data and // Use helper function to register the test payments in the data and
// populate the data to the payments slice. // populate the data to the payments slice.
createTestPayments(t, pControl, payments) createTestPayments(t, paymentDB, payments)
// Check that all payments are there as we added them. // Check that all payments are there as we added them.
assertPayments(t, db, payments) assertPayments(t, db, payments)
// Calling DeleteFailedAttempts on a failed payment should delete all // Calling DeleteFailedAttempts on a failed payment should delete all
// HTLCs. // HTLCs.
require.NoError(t, pControl.DeleteFailedAttempts(payments[0].id)) require.NoError(t, paymentDB.DeleteFailedAttempts(payments[0].id))
// Expect all HTLCs to be deleted if the config is set to delete them. // Expect all HTLCs to be deleted if the config is set to delete them.
if !keepFailedPaymentAttempts { if !keepFailedPaymentAttempts {
@@ -1085,9 +1085,9 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
// Calling DeleteFailedAttempts on an in-flight payment should return // Calling DeleteFailedAttempts on an in-flight payment should return
// an error. // an error.
if keepFailedPaymentAttempts { if keepFailedPaymentAttempts {
require.NoError(t, pControl.DeleteFailedAttempts(payments[1].id)) require.NoError(t, paymentDB.DeleteFailedAttempts(payments[1].id))
} else { } else {
require.Error(t, pControl.DeleteFailedAttempts(payments[1].id)) require.Error(t, paymentDB.DeleteFailedAttempts(payments[1].id))
} }
// Since DeleteFailedAttempts returned an error, we should expect the // Since DeleteFailedAttempts returned an error, we should expect the
@@ -1095,7 +1095,7 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
assertPayments(t, db, payments) assertPayments(t, db, payments)
// Cleaning up a successful payment should remove failed htlcs. // Cleaning up a successful payment should remove failed htlcs.
require.NoError(t, pControl.DeleteFailedAttempts(payments[2].id)) require.NoError(t, paymentDB.DeleteFailedAttempts(payments[2].id))
// Expect all HTLCs except for the settled one to be deleted if the // Expect all HTLCs except for the settled one to be deleted if the
// config is set to delete them. // config is set to delete them.
if !keepFailedPaymentAttempts { if !keepFailedPaymentAttempts {
@@ -1107,10 +1107,10 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
// DeleteFailedAttempts is ignored, even for non-existent // DeleteFailedAttempts is ignored, even for non-existent
// payments, if the control tower is configured to keep failed // payments, if the control tower is configured to keep failed
// HTLCs. // HTLCs.
require.NoError(t, pControl.DeleteFailedAttempts(lntypes.ZeroHash)) require.NoError(t, paymentDB.DeleteFailedAttempts(lntypes.ZeroHash))
} else { } else {
// Attempting to cleanup a non-existent payment returns an error. // Attempting to cleanup a non-existent payment returns an error.
require.Error(t, pControl.DeleteFailedAttempts(lntypes.ZeroHash)) require.Error(t, paymentDB.DeleteFailedAttempts(lntypes.ZeroHash))
} }
} }

View File

@@ -516,7 +516,7 @@ func TestQueryPayments(t *testing.T) {
// where we have duplicates in the nested duplicates // where we have duplicates in the nested duplicates
// bucket. // bucket.
nonDuplicatePayments := 6 nonDuplicatePayments := 6
pControl := NewKVPaymentsDB(db) paymentDB := NewKVPaymentsDB(db)
for i := 0; i < nonDuplicatePayments; i++ { for i := 0; i < nonDuplicatePayments; i++ {
// Generate a test payment. // Generate a test payment.
@@ -530,7 +530,7 @@ func TestQueryPayments(t *testing.T) {
info.CreationTime = time.Unix(int64(i+1), 0) info.CreationTime = time.Unix(int64(i+1), 0)
// Create a new payment entry in the database. // Create a new payment entry in the database.
err = pControl.InitPayment(info.PaymentIdentifier, info) err = paymentDB.InitPayment(info.PaymentIdentifier, info)
if err != nil { if err != nil {
t.Fatalf("unable to initialize "+ t.Fatalf("unable to initialize "+
"payment in database: %v", err) "payment in database: %v", err)
@@ -538,7 +538,7 @@ func TestQueryPayments(t *testing.T) {
// Immediately delete the payment with index 2. // Immediately delete the payment with index 2.
if i == 1 { if i == 1 {
pmt, err := pControl.FetchPayment( pmt, err := paymentDB.FetchPayment(
info.PaymentIdentifier, info.PaymentIdentifier,
) )
require.NoError(t, err) require.NoError(t, err)
@@ -552,13 +552,13 @@ func TestQueryPayments(t *testing.T) {
// to the parent payment + 1. Note that // to the parent payment + 1. Note that
// duplicate payments will always be succeeded. // duplicate payments will always be succeeded.
if i == (nonDuplicatePayments - 1) { if i == (nonDuplicatePayments - 1) {
pmt, err := pControl.FetchPayment( pmt, err := paymentDB.FetchPayment(
info.PaymentIdentifier, info.PaymentIdentifier,
) )
require.NoError(t, err) require.NoError(t, err)
appendDuplicatePayment( appendDuplicatePayment(
t, pControl.db, t, paymentDB.db,
info.PaymentIdentifier, info.PaymentIdentifier,
pmt.SequenceNum+1, pmt.SequenceNum+1,
preimg, preimg,
@@ -618,18 +618,18 @@ func TestFetchPaymentWithSequenceNumber(t *testing.T) {
db, err := MakeTestDB(t) db, err := MakeTestDB(t)
require.NoError(t, err) require.NoError(t, err)
pControl := NewKVPaymentsDB(db) paymentDB := NewKVPaymentsDB(db)
// Generate a test payment which does not have duplicates. // Generate a test payment which does not have duplicates.
noDuplicates, _, _, err := genInfo(t) noDuplicates, _, _, err := genInfo(t)
require.NoError(t, err) require.NoError(t, err)
// Create a new payment entry in the database. // Create a new payment entry in the database.
err = pControl.InitPayment(noDuplicates.PaymentIdentifier, noDuplicates) err = paymentDB.InitPayment(noDuplicates.PaymentIdentifier, noDuplicates)
require.NoError(t, err) require.NoError(t, err)
// Fetch the payment so we can get its sequence nr. // Fetch the payment so we can get its sequence nr.
noDuplicatesPayment, err := pControl.FetchPayment( noDuplicatesPayment, err := paymentDB.FetchPayment(
noDuplicates.PaymentIdentifier, noDuplicates.PaymentIdentifier,
) )
require.NoError(t, err) require.NoError(t, err)
@@ -639,11 +639,11 @@ func TestFetchPaymentWithSequenceNumber(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Create a new payment entry in the database. // Create a new payment entry in the database.
err = pControl.InitPayment(hasDuplicates.PaymentIdentifier, hasDuplicates) err = paymentDB.InitPayment(hasDuplicates.PaymentIdentifier, hasDuplicates)
require.NoError(t, err) require.NoError(t, err)
// Fetch the payment so we can get its sequence nr. // Fetch the payment so we can get its sequence nr.
hasDuplicatesPayment, err := pControl.FetchPayment( hasDuplicatesPayment, err := paymentDB.FetchPayment(
hasDuplicates.PaymentIdentifier, hasDuplicates.PaymentIdentifier,
) )
require.NoError(t, err) require.NoError(t, err)