From be5535517bcd9bd490005ebb08cba616d4e8b281 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Mon, 25 Nov 2024 21:56:47 +0800 Subject: [PATCH] itest+lntest: add new method `FundNumCoins` Most of the time we only need to fund the node with given number of UTXOs without concerning the amount, so we add the more efficient funding method as it mines a single block in the end. --- itest/lnd_sweep_test.go | 12 ++++++------ lntest/harness.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/itest/lnd_sweep_test.go b/itest/lnd_sweep_test.go index 5d76557972..e7710f3c2a 100644 --- a/itest/lnd_sweep_test.go +++ b/itest/lnd_sweep_test.go @@ -771,24 +771,24 @@ func testSweepHTLCs(ht *lntest.HarnessTest) { // Bob needs two more wallet utxos: // - when sweeping anchors, he needs one utxo for each sweep. // - when sweeping HTLCs, he needs one utxo for each sweep. - ht.FundCoins(btcutil.SatoshiPerBitcoin, bob) - ht.FundCoins(btcutil.SatoshiPerBitcoin, bob) + numUTXOs := 2 // Bob should have enough wallet UTXOs here to sweep the HTLC in the // end of this test. However, due to a known issue, Bob's wallet may // report there's no UTXO available. For details, // - https://github.com/lightningnetwork/lnd/issues/8786 // - // TODO(yy): remove this step once the issue is resolved. - ht.FundCoins(btcutil.SatoshiPerBitcoin, bob) + // TODO(yy): remove this extra UTXO once the issue is resolved. + numUTXOs++ // For neutrino backend, we need two more UTXOs for Bob to create his // sweeping txns. if ht.IsNeutrinoBackend() { - ht.FundCoins(btcutil.SatoshiPerBitcoin, bob) - ht.FundCoins(btcutil.SatoshiPerBitcoin, bob) + numUTXOs += 2 } + ht.FundNumCoins(bob, numUTXOs) + // Subscribe the invoices. stream1 := carol.RPC.SubscribeSingleInvoice(payHashSettled[:]) stream2 := carol.RPC.SubscribeSingleInvoice(payHashHold[:]) diff --git a/lntest/harness.go b/lntest/harness.go index 82adbd9891..8e9e6310ce 100644 --- a/lntest/harness.go +++ b/lntest/harness.go @@ -1379,6 +1379,40 @@ func (h *HarnessTest) FundCoinsP2TR(amt btcutil.Amount, h.fundCoins(amt, target, lnrpc.AddressType_TAPROOT_PUBKEY, true) } +// FundNumCoins attempts to send the given number of UTXOs from the internal +// mining node to the targeted lightning node using a P2WKH address. Each UTXO +// has an amount of 1 BTC. 1 blocks are mined to confirm the tx. +func (h *HarnessTest) FundNumCoins(hn *node.HarnessNode, num int) { + // Get the initial balance first. + resp := hn.RPC.WalletBalance() + initialBalance := btcutil.Amount(resp.ConfirmedBalance) + + const fundAmount = 1 * btcutil.SatoshiPerBitcoin + + // Send out the outputs from the miner. + for i := 0; i < num; i++ { + h.createAndSendOutput( + hn, fundAmount, lnrpc.AddressType_WITNESS_PUBKEY_HASH, + ) + } + + // Wait for ListUnspent to show the correct number of unconfirmed + // UTXOs. + // + // Since neutrino doesn't support unconfirmed outputs, skip this check. + if !h.IsNeutrinoBackend() { + h.AssertNumUTXOsUnconfirmed(hn, num) + } + + // Mine a block to confirm the transactions. + h.MineBlocksAndAssertNumTxes(1, num) + + // Now block until the wallet have fully synced up. + totalAmount := btcutil.Amount(fundAmount * num) + expectedBalance := initialBalance + totalAmount + h.WaitForBalanceConfirmed(hn, expectedBalance) +} + // completePaymentRequestsAssertStatus sends payments from a node to complete // all payment requests. This function does not return until all payments // have reached the specified status.