Skip to content

Commit

Permalink
itest+lntest: add new method FundNumCoins
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yyforyongyu committed Nov 26, 2024
1 parent b011d8c commit be55355
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
12 changes: 6 additions & 6 deletions itest/lnd_sweep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[:])
Expand Down
34 changes: 34 additions & 0 deletions lntest/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit be55355

Please sign in to comment.