Skip to content

Commit

Permalink
lnwallet+lntest: add test for pagination
Browse files Browse the repository at this point in the history
Here we add unit test to cover getting exactly how many
transactions we requested for, and first and last index
offsets are returned correctly.
  • Loading branch information
Abdulkbk committed Nov 7, 2024
1 parent 8fe74aa commit c5dea0e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lntest/mock/walletcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,10 @@ func (w *WalletController) ListUnspentWitness(int32, int32,

// ListTransactionDetails currently returns dummy values.
func (w *WalletController) ListTransactionDetails(int32, int32,
string, uint, int) ([]*lnwallet.TransactionDetail, error) {
string, uint, int) ([]*lnwallet.TransactionDetail,
uint64, uint64, error) {

return nil, nil
return nil, 0, 0, nil
}

// LeaseOutput returns the current time and a nil error.
Expand Down
4 changes: 2 additions & 2 deletions lnwallet/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@ func (w *mockWalletController) ListUnspentWitness(int32, int32,

// ListTransactionDetails currently returns dummy values.
func (w *mockWalletController) ListTransactionDetails(int32, int32,
string) ([]*TransactionDetail, error) {
string, uint, int) ([]*TransactionDetail, uint64, uint64, error) {

return nil, nil
return nil, 0, 0, nil
}

// LeaseOutput returns the current time and a nil error.
Expand Down
61 changes: 56 additions & 5 deletions lnwallet/test/test_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func assertTxInWallet(t *testing.T, w *lnwallet.LightningWallet,
// We'll fetch all of our transaction and go through each one until
// finding the expected transaction with its expected confirmation
// status.
txs, err := w.ListTransactionDetails(
txs, _, _, err := w.ListTransactionDetails(
0, btcwallet.UnconfirmedHeight, "", 0, 1000,
)
require.NoError(t, err, "unable to retrieve transactions")
Expand Down Expand Up @@ -1103,7 +1103,7 @@ func testListTransactionDetails(miner *rpctest.Harness,
// should be confirmed.
err = waitForWalletSync(miner, alice)
require.NoError(t, err, "Couldn't sync Alice's wallet")
txDetails, err := alice.ListTransactionDetails(
txDetails, _, _, err := alice.ListTransactionDetails(
startHeight, chainTip, "", 0, 1000,
)
require.NoError(t, err, "unable to fetch tx details")
Expand Down Expand Up @@ -1215,7 +1215,7 @@ func testListTransactionDetails(miner *rpctest.Harness,
// unconfirmed transactions. The transaction above should be included
// with a confirmation height of 0, indicating that it has not been
// mined yet.
txDetails, err = alice.ListTransactionDetails(
txDetails, _, _, err = alice.ListTransactionDetails(
chainTip, btcwallet.UnconfirmedHeight, "", 0, 1000,
)
require.NoError(t, err, "unable to fetch tx details")
Expand Down Expand Up @@ -1268,7 +1268,7 @@ func testListTransactionDetails(miner *rpctest.Harness,
// transactions from the last block.
err = waitForWalletSync(miner, alice)
require.NoError(t, err, "Couldn't sync Alice's wallet")
txDetails, err = alice.ListTransactionDetails(
txDetails, _, _, err = alice.ListTransactionDetails(
chainTip, chainTip, "", 0, 1000,
)
require.NoError(t, err, "unable to fetch tx details")
Expand Down Expand Up @@ -1311,13 +1311,64 @@ func testListTransactionDetails(miner *rpctest.Harness,

// Query for transactions only in the latest block. We do not expect
// any transactions to be returned.
txDetails, err = alice.ListTransactionDetails(
txDetails, _, _, err = alice.ListTransactionDetails(
chainTip, chainTip, "", 0, 1000,
)
require.NoError(t, err, "unexpected error")
if len(txDetails) != 0 {
t.Fatalf("expected 0 transactions, got: %v", len(txDetails))
}

// Next we try to query for only 5 transactions.
var (
firstIdx, lastIdx uint64
)
txDetails, firstIdx, lastIdx, err = alice.ListTransactionDetails(
startHeight, chainTip, "", 0, 5,
)
require.NoError(t, err, "unexpected error")
require.Len(t, txDetails, 5)
require.EqualValues(t, 1, firstIdx)
require.EqualValues(t, 5, lastIdx)

// Query for transactions, setting max_transactions to zero. We expect
// no transactions to be returned.
txDetails, _, _, err = alice.ListTransactionDetails(
startHeight, chainTip, "", 0, 0,
)
require.NoError(t, err, "unexpected error")
require.Len(t, txDetails, 0)

// Query for transactions, more than we have in the wallet (10). We
// expect less transactions to be returned.
txDetails, _, _, err = alice.ListTransactionDetails(
startHeight, chainTip, "", 0, 1000,
)
require.NoError(t, err, "unexpected error")
require.Less(t, len(txDetails), 1000)

// Query for transactions, using a -1 as the max_transactions.
// We expect to get the whole (10) transactions.
txDetails, _, _, err = alice.ListTransactionDetails(
startHeight, chainTip, "", 0, -1,
)
require.NoError(t, err, "unexpected error")
require.Len(t, txDetails, 10)

// Query for transactions where the offset is greater than the number
// of transactions available. We expect no transactions to be returned.
txDetails, _, _, err = alice.ListTransactionDetails(
startHeight, chainTip, "", 10, 10,
)
require.NoError(t, err, "unexpected error")
require.Len(t, txDetails, 0)

// Query for transactions, using a negative value less than -1 as the
// max_transactions. We expect to get an error.
_, _, _, err = alice.ListTransactionDetails(
startHeight, chainTip, "", 0, -10,
)
require.Error(t, err, "unexpected error")
}

func testTransactionSubscriptions(miner *rpctest.Harness,
Expand Down

0 comments on commit c5dea0e

Please sign in to comment.