From f97f60d3d461b3d7952a5632658c1da957956e1c Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Fri, 23 May 2025 17:04:40 +0800 Subject: [PATCH] lntest+itest: add `testBumpFeeExternalInput` --- itest/list_on_test.go | 4 ++++ itest/lnd_bump_fee.go | 44 ++++++++++++++++++++++++++++++++++++++++ lntest/rpc/wallet_kit.go | 12 +++++++++++ 3 files changed, 60 insertions(+) diff --git a/itest/list_on_test.go b/itest/list_on_test.go index 88b7bb395..7263b2451 100644 --- a/itest/list_on_test.go +++ b/itest/list_on_test.go @@ -463,6 +463,10 @@ var allTestCases = []*lntest.TestCase{ Name: "bumpfee", TestFunc: testBumpFee, }, + { + Name: "bumpfee external input", + TestFunc: testBumpFeeExternalInput, + }, { Name: "bumpforceclosefee", TestFunc: testBumpForceCloseFee, diff --git a/itest/lnd_bump_fee.go b/itest/lnd_bump_fee.go index 3a4d46cc7..3b89e1088 100644 --- a/itest/lnd_bump_fee.go +++ b/itest/lnd_bump_fee.go @@ -587,3 +587,47 @@ func runBumpFee(ht *lntest.HarnessTest, alice *node.HarnessNode) { // Clean up the mempool. ht.MineBlocksAndAssertNumTxes(1, 2) } + +// testBumpFeeExternalInput assert that when the bump fee RPC is called with an +// outpoint unknown to the node's wallet, an error is returned. +func testBumpFeeExternalInput(ht *lntest.HarnessTest) { + alice := ht.NewNode("Alice", nil) + bob := ht.NewNode("Bob", nil) + + // We'll start the test by sending Alice some coins, which she'll use + // to send to Bob. + ht.FundCoins(btcutil.SatoshiPerBitcoin, alice) + + // Alice sends 0.5 BTC to Bob. This tx should have two outputs - one + // that belongs to Bob, the other is Alice's change output. + tx := ht.SendCoins(alice, bob, btcutil.SatoshiPerBitcoin/2) + txid := tx.TxHash() + + // Find the wrong index to perform the fee bump. We assume the first + // output belongs to Bob, and switch to the second if the second output + // has a larger output value. Given we've funded Alice 1 btc, she then + // sends 0.5 btc to Bob, her change output will be below 0.5 btc after + // paying the mining fees. + wrongIndex := 0 + if tx.TxOut[0].Value < tx.TxOut[1].Value { + wrongIndex = 1 + } + + // Alice now tries to bump the wrong output on this tx. + op := &lnrpc.OutPoint{ + TxidBytes: txid[:], + OutputIndex: uint32(wrongIndex), + } + + // Create a request with the wrong outpoint. + bumpFeeReq := &walletrpc.BumpFeeRequest{ + Outpoint: op, + // We use a force param to create the sweeping tx immediately. + Immediate: true, + } + err := alice.RPC.BumpFeeAssertErr(bumpFeeReq) + require.ErrorContains(ht, err, "does not belong to the wallet") + + // Clean up the mempool. + ht.MineBlocksAndAssertNumTxes(1, 1) +} diff --git a/lntest/rpc/wallet_kit.go b/lntest/rpc/wallet_kit.go index 41b596d4c..02e449cd8 100644 --- a/lntest/rpc/wallet_kit.go +++ b/lntest/rpc/wallet_kit.go @@ -254,6 +254,18 @@ func (h *HarnessRPC) BumpFee( return resp } +// BumpFeeAssertErr makes a RPC call to the node's WalletKitClient and asserts +// that an error is returned. +func (h *HarnessRPC) BumpFeeAssertErr(req *walletrpc.BumpFeeRequest) error { + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + _, err := h.WalletKit.BumpFee(ctxt, req) + require.Errorf(h, err, "%s: expect BumpFee to return an error", h.Name) + + return err +} + // BumpForceCloseFee makes a RPC call to the node's WalletKitClient and asserts. // //nolint:ll