paymentsdb: declare helper functions and add comments

This commit is contained in:
ziggie
2025-08-13 15:05:11 +02:00
parent 82242f5342
commit f87841d638
2 changed files with 20 additions and 6 deletions

View File

@@ -20,6 +20,8 @@ import (
// TestKVStoreDeleteNonInFlight checks that calling DeletePayments only // TestKVStoreDeleteNonInFlight checks that calling DeletePayments only
// deletes payments from the database that are not in-flight. // deletes payments from the database that are not in-flight.
//
// TODO(ziggie): Make this test db agnostic.
func TestKVStoreDeleteNonInFlight(t *testing.T) { func TestKVStoreDeleteNonInFlight(t *testing.T) {
t.Parallel() t.Parallel()
@@ -249,9 +251,11 @@ type htlcStatus struct {
// fetchPaymentIndexEntry gets the payment hash for the sequence number provided // fetchPaymentIndexEntry gets the payment hash for the sequence number provided
// from our payment indexes bucket. // from our payment indexes bucket.
func fetchPaymentIndexEntry(_ *testing.T, p *KVStore, func fetchPaymentIndexEntry(t *testing.T, p *KVStore,
sequenceNumber uint64) (*lntypes.Hash, error) { sequenceNumber uint64) (*lntypes.Hash, error) {
t.Helper()
var hash lntypes.Hash var hash lntypes.Hash
if err := kvdb.View(p.db, func(tx walletdb.ReadTx) error { if err := kvdb.View(p.db, func(tx walletdb.ReadTx) error {
@@ -319,6 +323,8 @@ func assertNoIndex(t *testing.T, p DB, seqNr uint64) {
func makeFakeInfo(t *testing.T) (*PaymentCreationInfo, func makeFakeInfo(t *testing.T) (*PaymentCreationInfo,
*HTLCAttemptInfo) { *HTLCAttemptInfo) {
t.Helper()
var preimg lntypes.Preimage var preimg lntypes.Preimage
copy(preimg[:], rev[:]) copy(preimg[:], rev[:])
@@ -677,6 +683,8 @@ func putDuplicatePayment(t *testing.T, duplicateBucket kvdb.RwBucket,
// TestQueryPayments tests retrieval of payments with forwards and reversed // TestQueryPayments tests retrieval of payments with forwards and reversed
// queries. // queries.
//
// TODO(ziggie): Make this test db agnostic.
func TestQueryPayments(t *testing.T) { func TestQueryPayments(t *testing.T) {
// Define table driven test for QueryPayments. // Define table driven test for QueryPayments.
// Test payments have sequence indices [1, 3, 4, 5, 6, 7]. // Test payments have sequence indices [1, 3, 4, 5, 6, 7].

View File

@@ -112,6 +112,8 @@ type payment struct {
// the payments slice. Each payment will receive one failed HTLC and another // the payments slice. Each payment will receive one failed HTLC and another
// HTLC depending on the final status of the payment provided. // HTLC depending on the final status of the payment provided.
func createTestPayments(t *testing.T, p DB, payments []*payment) { func createTestPayments(t *testing.T, p DB, payments []*payment) {
t.Helper()
attemptID := uint64(0) attemptID := uint64(0)
for i := 0; i < len(payments); i++ { for i := 0; i < len(payments); i++ {
@@ -193,7 +195,9 @@ func createTestPayments(t *testing.T, p DB, payments []*payment) {
// assertRouteEquals compares to routes for equality and returns an error if // assertRouteEquals compares to routes for equality and returns an error if
// they are not equal. // they are not equal.
func assertRouteEqual(a, b *route.Route) error { func assertRouteEqual(t *testing.T, a, b *route.Route) error {
t.Helper()
if !reflect.DeepEqual(a, b) { if !reflect.DeepEqual(a, b) {
return fmt.Errorf("HTLCAttemptInfos don't match: %v vs %v", return fmt.Errorf("HTLCAttemptInfos don't match: %v vs %v",
spew.Sdump(a), spew.Sdump(b)) spew.Sdump(a), spew.Sdump(b))
@@ -239,7 +243,7 @@ func assertPaymentInfo(t *testing.T, p DB, hash lntypes.Hash,
} }
htlc := payment.HTLCs[a.AttemptID] htlc := payment.HTLCs[a.AttemptID]
if err := assertRouteEqual(&htlc.Route, &a.Route); err != nil { if err := assertRouteEqual(t, &htlc.Route, &a.Route); err != nil {
t.Fatal("routes do not match") t.Fatal("routes do not match")
} }
@@ -331,7 +335,9 @@ func assertDBPayments(t *testing.T, paymentDB DB, payments []*payment) {
} }
// genPreimage generates a random preimage. // genPreimage generates a random preimage.
func genPreimage() ([32]byte, error) { func genPreimage(t *testing.T) ([32]byte, error) {
t.Helper()
var preimage [32]byte var preimage [32]byte
if _, err := io.ReadFull(rand.Reader, preimage[:]); err != nil { if _, err := io.ReadFull(rand.Reader, preimage[:]); err != nil {
return preimage, err return preimage, err
@@ -344,7 +350,7 @@ func genPreimage() ([32]byte, error) {
func genInfo(t *testing.T) (*PaymentCreationInfo, *HTLCAttemptInfo, func genInfo(t *testing.T) (*PaymentCreationInfo, *HTLCAttemptInfo,
lntypes.Preimage, error) { lntypes.Preimage, error) {
preimage, err := genPreimage() preimage, err := genPreimage(t)
if err != nil { if err != nil {
return nil, nil, preimage, fmt.Errorf("unable to "+ return nil, nil, preimage, fmt.Errorf("unable to "+
"generate preimage: %v", err) "generate preimage: %v", err)
@@ -1466,7 +1472,7 @@ func TestSwitchFail(t *testing.T) {
len(payment.HTLCs)) len(payment.HTLCs))
} }
err = assertRouteEqual(&payment.HTLCs[0].Route, &attempt.Route) err = assertRouteEqual(t, &payment.HTLCs[0].Route, &attempt.Route)
if err != nil { if err != nil {
t.Fatalf("unexpected route returned: %v vs %v: %v", t.Fatalf("unexpected route returned: %v vs %v: %v",
spew.Sdump(attempt.Route), spew.Sdump(attempt.Route),