From 16f05105c1eb0b5d24d48f1e4378c1bfbc8e0fb7 Mon Sep 17 00:00:00 2001 From: erik Date: Sat, 22 Jul 2023 09:14:15 +0200 Subject: [PATCH] itest: add gettransactiondetails --- itest/lnd_misc_test.go | 32 +++++++++++--------------------- lntest/rpc/wallet_kit.go | 14 ++++++++++++++ lnwallet/test/test_interface.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/itest/lnd_misc_test.go b/itest/lnd_misc_test.go index 62a568e29..a88abb7b3 100644 --- a/itest/lnd_misc_test.go +++ b/itest/lnd_misc_test.go @@ -829,32 +829,22 @@ func testSweepAllCoins(ht *lntest.HarnessTest) { // assertTxLabel is a helper function which finds a target tx in our // set of transactions and checks that it has the desired label. assertTxLabel := func(targetTx, label string) error { - // List all transactions relevant to our wallet, and find the - // tx so that we can check the correct label has been set. - txResp := ainz.RPC.GetTransactions(nil) + // Get the transaction from our wallet so that we can check + // that the correct label has been set. + txResp := ainz.RPC.GetTransaction( + &walletrpc.GetTransactionRequest{ + Txid: targetTx, + }, + ) + require.NotNilf(ht, txResp, "target tx %v not found", targetTx) - var target *lnrpc.Transaction - - // First we need to find the target tx. - for _, txn := range txResp.Transactions { - if txn.TxHash == targetTx { - target = txn - break - } - } - - // If we cannot find it, return an error. - if target == nil { - return fmt.Errorf("target tx %v not found", targetTx) - } - - // Otherwise, check the labels are matched. - if target.Label == label { + // Make sure the labels match. + if txResp.Label == label { return nil } return fmt.Errorf("labels not match, want: "+ - "%v, got %v", label, target.Label) + "%v, got %v", label, txResp.Label) } // waitTxLabel waits until the desired tx label is found or timeout. diff --git a/lntest/rpc/wallet_kit.go b/lntest/rpc/wallet_kit.go index f28eedf6a..5e330192f 100644 --- a/lntest/rpc/wallet_kit.go +++ b/lntest/rpc/wallet_kit.go @@ -3,6 +3,7 @@ package rpc import ( "context" + "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/signrpc" "github.com/lightningnetwork/lnd/lnrpc/walletrpc" "github.com/stretchr/testify/require" @@ -196,6 +197,19 @@ func (h *HarnessRPC) PublishTransaction( return resp } +// GetTransaction makes a RPC call to the node's WalletKitClient and asserts. +func (h *HarnessRPC) GetTransaction( + req *walletrpc.GetTransactionRequest) *lnrpc.Transaction { + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + resp, err := h.WalletKit.GetTransaction(ctxt, req) + h.NoError(err, "GetTransaction") + + return resp +} + // BumpFee makes a RPC call to the node's WalletKitClient and asserts. func (h *HarnessRPC) BumpFee( req *walletrpc.BumpFeeRequest) *walletrpc.BumpFeeResponse { diff --git a/lnwallet/test/test_interface.go b/lnwallet/test/test_interface.go index 66958278e..aea0aa15e 100644 --- a/lnwallet/test/test_interface.go +++ b/lnwallet/test/test_interface.go @@ -1685,6 +1685,30 @@ func newTx(t *testing.T, r *rpctest.Harness, pubKey *btcec.PublicKey, return tx1 } +// testGetTransactionDetails checks that GetTransactionDetails returns the +// correct amount after a transaction has been sent from alice to bob. +func testGetTransactionDetails(r *rpctest.Harness, + alice, bob *lnwallet.LightningWallet, t *testing.T) { + + const txFee = int64(14500) + + bobPkScript := newPkScript(t, bob, lnwallet.WitnessPubKey) + txFeeRate := chainfee.SatPerKWeight(2500) + amountSats := btcutil.Amount(btcutil.SatoshiPerBitcoin - txFee) + output := &wire.TxOut{ + Value: int64(amountSats), + PkScript: bobPkScript, + } + tx := sendCoins(t, r, alice, bob, output, txFeeRate, true, 1) + txHash := tx.TxHash() + assertTxInWallet(t, alice, txHash, true) + assertTxInWallet(t, bob, txHash, true) + + txDetails, err := bob.GetTransactionDetails(&txHash) + require.NoError(t, err, "unable to receive transaction details") + require.Equal(t, txDetails.Value, amountSats, "tx value") +} + // testPublishTransaction checks that PublishTransaction returns the expected // error types in case the transaction being published conflicts with the // current mempool or chain. @@ -2794,6 +2818,10 @@ var walletTests = []walletTestCase{ name: "transaction details", test: testListTransactionDetails, }, + { + name: "get transaction details", + test: testGetTransactionDetails, + }, { name: "publish transaction", test: testPublishTransaction,