From 009a5c28bc7c3a4744f97cc2a6dab190bf496d68 Mon Sep 17 00:00:00 2001 From: cyberguru1 Date: Tue, 7 Jul 2026 11:22:14 -0500 Subject: [PATCH] accounts: add payment methods to Store interface Introduce the ListAccountPayments and CountAccountPayments methods to the accounts Store interface. ListAccountPayments enables retrieval of a paginated list of payment entries associated with a given account ID, supporting offset and limit. CountAccountPayments returns the total number of payments associated with the account. --- accounts/interface.go | 10 ++++++ accounts/store_test.go | 74 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/accounts/interface.go b/accounts/interface.go index 85486662..f78a66f7 100644 --- a/accounts/interface.go +++ b/accounts/interface.go @@ -285,6 +285,16 @@ type Store interface { DeleteAccountPayment(_ context.Context, id AccountID, hash lntypes.Hash) error + // ListAccountPayments returns a paginated list of payments + // associated with the given account, sorted in ascending + // lexicographical order of their payment hash. + ListAccountPayments(ctx context.Context, id AccountID, offset, + limit int32) ([]*AccountPaymentEntry, error) + + // CountAccountPayments returns the total number of payments associated + // with the given account. + CountAccountPayments(ctx context.Context, id AccountID) (uint64, error) + // RemoveAccount finds an account by its ID and removes it from the¨ // store. RemoveAccount(ctx context.Context, id AccountID) error diff --git a/accounts/store_test.go b/accounts/store_test.go index c3610093..100f5801 100644 --- a/accounts/store_test.go +++ b/accounts/store_test.go @@ -906,3 +906,77 @@ func TestCheckLabel(t *testing.T) { }) } } + +// TestListAccountPayments tests listing and counting payment entries associated +// with a given account. +func TestListAccountPayments(t *testing.T) { + t.Parallel() + ctx := context.Background() + + store := NewTestDB(t, clock.NewTestClock(time.Now())) + + // Listing payments for non-existent account should fail. + _, err := store.ListAccountPayments( + ctx, AccountID{}, 0, 0, + ) + require.ErrorIs(t, err, ErrAccNotFound) + + acct, err := store.NewAccount( + ctx, 10000, time.Time{}, "payment-list", + ) + require.NoError(t, err) + + // Initially, there should be no payments. + payments, err := store.ListAccountPayments( + ctx, acct.ID, 0, 0, + ) + require.NoError(t, err) + require.Empty(t, payments) + + count, err := store.CountAccountPayments(ctx, acct.ID) + require.NoError(t, err) + require.Zero(t, count) + + // Add 3 payments. + hash1 := lntypes.Hash{1} + hash2 := lntypes.Hash{2} + hash3 := lntypes.Hash{3} + + _, err = store.UpsertAccountPayment( + ctx, acct.ID, hash1, 100, lnrpc.Payment_IN_FLIGHT, + ) + require.NoError(t, err) + + _, err = store.UpsertAccountPayment( + ctx, acct.ID, hash2, 200, lnrpc.Payment_SUCCEEDED, + ) + require.NoError(t, err) + + _, err = store.UpsertAccountPayment( + ctx, acct.ID, hash3, 300, lnrpc.Payment_FAILED, + ) + require.NoError(t, err) + + // Test counting all payments. + count, err = store.CountAccountPayments(ctx, acct.ID) + require.NoError(t, err) + require.EqualValues(t, 3, count) + + // List all payments in default order (ascending by hash). + payments, err = store.ListAccountPayments( + ctx, acct.ID, 0, 3, + ) + require.NoError(t, err) + require.Len(t, payments, 3) + require.Equal(t, hash1, payments[0].Hash) + require.Equal(t, hash2, payments[1].Hash) + require.Equal(t, hash3, payments[2].Hash) + + // Test offset and limit. + payments, err = store.ListAccountPayments( + ctx, acct.ID, 1, 1, + ) + require.NoError(t, err) + require.Len(t, payments, 1) + require.Equal(t, hash2, payments[0].Hash) +}