From 976bb3797294f652f9334a76142dbf30e46090f0 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Wed, 1 May 2024 19:21:00 +0800 Subject: [PATCH] lntest+itest: add method `AssertNumTxsInMempool` and `AssertTxInBlock` in harness Prepare to make `HarnessTest.Miner` a private instance to sync height. --- itest/lnd_channel_backup_test.go | 12 +++--- itest/lnd_channel_force_close_test.go | 10 ++--- itest/lnd_funding_test.go | 4 +- itest/lnd_misc_test.go | 2 +- itest/lnd_multi-hop_test.go | 62 +++++++++++++-------------- itest/lnd_nonstd_sweep_test.go | 2 +- itest/lnd_onchain_test.go | 6 +-- itest/lnd_open_channel_test.go | 6 +-- itest/lnd_psbt_test.go | 20 ++++----- itest/lnd_recovery_test.go | 6 +-- itest/lnd_revocation_test.go | 8 ++-- itest/lnd_signer_test.go | 4 +- itest/lnd_sweep_test.go | 12 +++--- itest/lnd_taproot_test.go | 10 ++--- itest/lnd_wallet_import_test.go | 4 +- itest/lnd_watchtower_test.go | 4 +- itest/lnd_zero_conf_test.go | 4 +- lntest/harness.go | 2 +- lntest/harness_assertion.go | 8 ++-- lntest/harness_miner.go | 15 +++++++ 20 files changed, 108 insertions(+), 93 deletions(-) diff --git a/itest/lnd_channel_backup_test.go b/itest/lnd_channel_backup_test.go index 0e7393dc3..28f956909 100644 --- a/itest/lnd_channel_backup_test.go +++ b/itest/lnd_channel_backup_test.go @@ -783,7 +783,7 @@ func runChanRestoreScenarioForceClose(ht *lntest.HarnessTest, zeroConf bool) { ) // We now wait until both Dave's closing tx. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // Now that we're able to make our restored now, we'll shutdown the old // Dave node as we'll be storing it shortly below. @@ -1272,7 +1272,7 @@ func testDataLossProtection(ht *lntest.HarnessTest) { ht.MineBlocks(1) // Dave should sweep his funds. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // Mine a block to confirm the sweep, and make sure Dave got his // balance back. @@ -1428,7 +1428,7 @@ func assertTimeLockSwept(ht *lntest.HarnessTest, carol, dave *node.HarnessNode, // Mine a block to trigger the sweeps. ht.MineBlocks(1) - ht.Miner.AssertNumTxsInMempool(expectedTxes) + ht.AssertNumTxsInMempool(expectedTxes) // Carol should consider the channel pending force close (since she is // waiting for her sweep to confirm). @@ -1462,9 +1462,9 @@ func assertTimeLockSwept(ht *lntest.HarnessTest, carol, dave *node.HarnessNode, // Mine a block to trigger the sweeps. ht.MineEmptyBlocks(1) - daveSweep := ht.Miner.AssertNumTxsInMempool(1)[0] + daveSweep := ht.AssertNumTxsInMempool(1)[0] block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, daveSweep) + ht.AssertTxInBlock(block, daveSweep) // Now the channel should be fully closed also from Dave's POV. ht.AssertNumPendingForceClose(dave, 0) @@ -1510,7 +1510,7 @@ func assertDLPExecuted(ht *lntest.HarnessTest, // Upon reconnection, the nodes should detect that Dave is out of sync. // Carol should force close the channel using her latest commitment. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // Channel should be in the state "waiting close" for Carol since she // broadcasted the force close tx. diff --git a/itest/lnd_channel_force_close_test.go b/itest/lnd_channel_force_close_test.go index 592669f32..7251421ef 100644 --- a/itest/lnd_channel_force_close_test.go +++ b/itest/lnd_channel_force_close_test.go @@ -389,7 +389,7 @@ func channelForceClosureTest(ht *lntest.HarnessTest, // So we fetch the node's mempool to ensure it has been properly // broadcast. ht.MineEmptyBlocks(1) - sweepingTXID := ht.Miner.AssertNumTxsInMempool(1)[0] + sweepingTXID := ht.AssertNumTxsInMempool(1)[0] // Fetch the sweep transaction, all input it's spending should be from // the commitment transaction which was broadcast on-chain. @@ -547,7 +547,7 @@ func channelForceClosureTest(ht *lntest.HarnessTest, // NOTE: after restart, all the htlc timeout txns will be offered to // the sweeper with `Immediate` set to true, so they won't be // aggregated. - htlcTxIDs := ht.Miner.AssertNumTxsInMempool(numInvoices) + htlcTxIDs := ht.AssertNumTxsInMempool(numInvoices) // Retrieve each htlc timeout txn from the mempool, and ensure it is // well-formed. This entails verifying that each only spends from @@ -733,7 +733,7 @@ func channelForceClosureTest(ht *lntest.HarnessTest, } // Wait for the single sweep txn to appear in the mempool. - htlcSweepTxID := ht.Miner.AssertNumTxsInMempool(1)[0] + htlcSweepTxID := ht.AssertNumTxsInMempool(1)[0] // Fetch the htlc sweep transaction from the mempool. htlcSweepTx := ht.Miner.GetRawTransaction(htlcSweepTxID) @@ -818,7 +818,7 @@ func channelForceClosureTest(ht *lntest.HarnessTest, // Generate the final block that sweeps all htlc funds into the user's // wallet, and make sure the sweep is in this block. block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, htlcSweepTxID) + ht.AssertTxInBlock(block, htlcSweepTxID) // Now that the channel has been fully swept, it should no longer show // up within the pending channels RPC. @@ -935,7 +935,7 @@ func testFailingChannel(ht *lntest.HarnessTest) { ht.MineEmptyBlocks(1) // Carol should have broadcast her sweeping tx. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // Mine two blocks to confirm Carol's sweeping tx, which will by now // Alice's commit output should be offered to her sweeper. diff --git a/itest/lnd_funding_test.go b/itest/lnd_funding_test.go index 38ca71d92..21b68f2cb 100644 --- a/itest/lnd_funding_test.go +++ b/itest/lnd_funding_test.go @@ -859,7 +859,7 @@ func testChannelFundingPersistence(ht *lntest.HarnessTest) { // channel has been opened. The funding transaction should be found // within the newly mined block. block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, fundingTxID) + ht.AssertTxInBlock(block, fundingTxID) // Get the height that our transaction confirmed at. _, height := ht.Miner.GetBestBlock() @@ -1047,7 +1047,7 @@ func testBatchChanFunding(ht *lntest.HarnessTest) { // Mine the batch transaction and check the network topology. block := ht.MineBlocksAndAssertNumTxes(6, 1)[0] - ht.Miner.AssertTxInBlock(block, txHash) + ht.AssertTxInBlock(block, txHash) ht.AssertTopologyChannelOpen(alice, chanPoint1) ht.AssertTopologyChannelOpen(alice, chanPoint2) ht.AssertTopologyChannelOpen(alice, chanPoint3) diff --git a/itest/lnd_misc_test.go b/itest/lnd_misc_test.go index 206d5748d..ba242ac8e 100644 --- a/itest/lnd_misc_test.go +++ b/itest/lnd_misc_test.go @@ -416,7 +416,7 @@ func testMaxPendingChannels(ht *lntest.HarnessTest) { // Ensure that the funding transaction enters a block, and is // properly advertised by Alice. - ht.Miner.AssertTxInBlock(block, fundingTxID) + ht.AssertTxInBlock(block, fundingTxID) ht.AssertTopologyChannelOpen(alice, fundingChanPoint) // The channel should be listed in the peer information diff --git a/itest/lnd_multi-hop_test.go b/itest/lnd_multi-hop_test.go index 72f07c5ef..93cf8f863 100644 --- a/itest/lnd_multi-hop_test.go +++ b/itest/lnd_multi-hop_test.go @@ -246,7 +246,7 @@ func runMultiHopHtlcLocalTimeout(ht *lntest.HarnessTest, ht.MineBlocks(int(numBlocks)) // Bob's force close transaction should now be found in the mempool. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) op := ht.OutPointFromChannelPoint(bobChanPoint) closeTx := ht.Miner.AssertOutpointInMempool(op) @@ -276,7 +276,7 @@ func runMultiHopHtlcLocalTimeout(ht *lntest.HarnessTest, // 1. Bob's sweeping tx anchor sweep should now be found in the mempool. // 2. Bob's HTLC timeout tx sweep should now be found in the mempool. // Carol's anchor sweep should be failed due to output being dust. - ht.Miner.AssertNumTxsInMempool(2) + ht.AssertNumTxsInMempool(2) htlcOutpoint := wire.OutPoint{Hash: closeTx.TxHash(), Index: 2} commitOutpoint := wire.OutPoint{Hash: closeTx.TxHash(), Index: 3} @@ -501,7 +501,7 @@ func runMultiHopReceiverChainClaim(ht *lntest.HarnessTest, // At this point, Carol should broadcast her active commitment // transaction in order to go to the chain and sweep her HTLC. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) closingTx := ht.Miner.AssertOutpointInMempool( ht.OutPointFromChannelPoint(bobChanPoint), @@ -526,7 +526,7 @@ func runMultiHopReceiverChainClaim(ht *lntest.HarnessTest, // scenarios, as we are using a wallet utxo, which means any txns using // that wallet utxo must pay more fees. On the other hand, there's no // way to remove that anchor-CPFP tx from the mempool. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // After the force close transaction is mined, Carol should offer her // second level HTLC tx to the sweeper, which means we should see two @@ -600,7 +600,7 @@ func runMultiHopReceiverChainClaim(ht *lntest.HarnessTest, ht.MineEmptyBlocks(1) // We should have a new transaction in the mempool. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // Finally, if we mine an additional block to confirm Carol's second // level success transaction. Carol should not show a pending channel @@ -761,7 +761,7 @@ func runMultiHopLocalForceCloseOnChainHtlcTimeout(ht *lntest.HarnessTest, ) txid := commitSweepTx.TxHash() block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, &txid) + ht.AssertTxInBlock(block, &txid) blocksMined++ } @@ -789,7 +789,7 @@ func runMultiHopLocalForceCloseOnChainHtlcTimeout(ht *lntest.HarnessTest, // Next, we'll mine an additional block. This should serve to confirm // the second layer timeout transaction. block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, &timeoutTx) + ht.AssertTxInBlock(block, &timeoutTx) // With the second layer timeout transaction confirmed, Bob should have // canceled backwards the HTLC that carol sent. @@ -1007,12 +1007,12 @@ func runMultiHopRemoteForceCloseOnChainHtlcTimeout(ht *lntest.HarnessTest, // Bob's sweeping transaction should now be found in the mempool at // this point. - sweepTx := ht.Miner.AssertNumTxsInMempool(1)[0] + sweepTx := ht.AssertNumTxsInMempool(1)[0] // If we mine an additional block, then this should confirm Bob's // transaction which sweeps the direct HTLC output. block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, sweepTx) + ht.AssertTxInBlock(block, sweepTx) // Now that the sweeping transaction has been confirmed, Bob should // cancel back that HTLC. As a result, Alice should not know of any @@ -1047,7 +1047,7 @@ func runMultiHopRemoteForceCloseOnChainHtlcTimeout(ht *lntest.HarnessTest, ) bobCommitSweepTxid := bobCommitSweep.TxHash() block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, &bobCommitSweepTxid) + ht.AssertTxInBlock(block, &bobCommitSweepTxid) } ht.AssertNumPendingForceClose(bob, 0) @@ -1191,7 +1191,7 @@ func runMultiHopHtlcLocalChainClaim(ht *lntest.HarnessTest, blocksMined++ // Assert the expected num of txns are found in the mempool. - ht.Miner.AssertNumTxsInMempool(expectedTxes) + ht.AssertNumTxsInMempool(expectedTxes) // Mine a block to clean up the mempool for the rest of the test. ht.MineBlocksAndAssertNumTxes(1, expectedTxes) @@ -1215,7 +1215,7 @@ func runMultiHopHtlcLocalChainClaim(ht *lntest.HarnessTest, ht.MineEmptyBlocks(int(numBlocks - blocksMined)) // Carol's commitment transaction should now be in the mempool. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // Look up the closing transaction. It should be spending from the // funding transaction, @@ -1226,7 +1226,7 @@ func runMultiHopHtlcLocalChainClaim(ht *lntest.HarnessTest, // Mine a block that should confirm the commit tx. block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, &closingTxid) + ht.AssertTxInBlock(block, &closingTxid) // After the force close transaction is mined, Carol should offer her // second-level success HTLC tx and anchor to the sweeper. @@ -1273,7 +1273,7 @@ func runMultiHopHtlcLocalChainClaim(ht *lntest.HarnessTest, ht.MineEmptyBlocks(1) // Assert transactions can be found in the mempool. - ht.Miner.AssertNumTxsInMempool(expectedTxes) + ht.AssertNumTxsInMempool(expectedTxes) // At this point we suspend Alice to make sure she'll handle the // on-chain settle after a restart. @@ -1345,7 +1345,7 @@ func runMultiHopHtlcLocalChainClaim(ht *lntest.HarnessTest, bobSecondLevelCSV-- // Carol's sweep tx should be broadcast. - carolSweep := ht.Miner.AssertNumTxsInMempool(1)[0] + carolSweep := ht.AssertNumTxsInMempool(1)[0] // Bob should offer his second level tx to his sweeper. ht.AssertNumPendingSweeps(bob, 1) @@ -1353,7 +1353,7 @@ func runMultiHopHtlcLocalChainClaim(ht *lntest.HarnessTest, // Mining one additional block, Bob's second level tx is mature, and he // can sweep the output. block = ht.MineBlocksAndAssertNumTxes(bobSecondLevelCSV, 1)[0] - ht.Miner.AssertTxInBlock(block, carolSweep) + ht.AssertTxInBlock(block, carolSweep) bobSweep := ht.Miner.GetNumTxsFromMempool(1)[0] bobSweepTxid := bobSweep.TxHash() @@ -1362,7 +1362,7 @@ func runMultiHopHtlcLocalChainClaim(ht *lntest.HarnessTest, // Now Bob should have no pending channels anymore, as this just // resolved it by the confirmation of the sweep transaction. block = ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, &bobSweepTxid) + ht.AssertTxInBlock(block, &bobSweepTxid) // With the script-enforced lease commitment type, Alice and Bob still // haven't been able to sweep their respective commit outputs due to the @@ -1558,7 +1558,7 @@ func runMultiHopHtlcRemoteChainClaim(ht *lntest.HarnessTest, ht.MineEmptyBlocks(int(numBlocks) - blocksMined) // Carol's commitment transaction should now be in the mempool. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // The closing transaction should be spending from the funding // transaction. @@ -1574,7 +1574,7 @@ func runMultiHopHtlcRemoteChainClaim(ht *lntest.HarnessTest, // Mine a block, which should contain: the commitment. block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, &closingTxid) + ht.AssertTxInBlock(block, &closingTxid) // After the force close transaction is mined, Carol should offer her // second level HTLC tx to the sweeper, along with her anchor output. @@ -1612,7 +1612,7 @@ func runMultiHopHtlcRemoteChainClaim(ht *lntest.HarnessTest, // anchor sweeping. ht.MineBlocksAndAssertNumTxes(1, 1) carolSecondLevelCSV-- - ht.Miner.AssertNumTxsInMempool(2) + ht.AssertNumTxsInMempool(2) // Mine a block to confirm the expected transactions. ht.MineBlocksAndAssertNumTxes(1, 2) @@ -1633,7 +1633,7 @@ func runMultiHopHtlcRemoteChainClaim(ht *lntest.HarnessTest, // We'll now mine a block which should confirm Bob's HTLC sweep // transaction. block = ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, &bobHtlcSweepTxid) + ht.AssertTxInBlock(block, &bobHtlcSweepTxid) carolSecondLevelCSV-- // Now that the sweeping transaction has been confirmed, Bob should now @@ -1656,12 +1656,12 @@ func runMultiHopHtlcRemoteChainClaim(ht *lntest.HarnessTest, // Mine a block to trigger the sweep of the second level tx. ht.MineEmptyBlocks(1) - carolSweep := ht.Miner.AssertNumTxsInMempool(1)[0] + carolSweep := ht.AssertNumTxsInMempool(1)[0] // When Carol's sweep gets confirmed, she should have no more pending // channels. block = ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, carolSweep) + ht.AssertTxInBlock(block, carolSweep) ht.AssertNumPendingForceClose(carol, 0) // With the script-enforced lease commitment type, Alice and Bob still @@ -1881,7 +1881,7 @@ func runMultiHopHtlcAggregation(ht *lntest.HarnessTest, // Bob's force close transaction should now be found in the mempool. If // there are anchors, we expect it to be offered to Bob's sweeper. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // Bob has two anchor sweep requests, one for remote (invalid) and the // other for local. @@ -1925,7 +1925,7 @@ func runMultiHopHtlcAggregation(ht *lntest.HarnessTest, ht.MineBlocksAndAssertNumTxes(1, 1) // The above mined block will trigger Bob to sweep his anchor output. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // Let Alice settle her invoices. When Bob now gets the preimages, he // has no other option than to broadcast his second-level transactions @@ -2162,7 +2162,7 @@ func runMultiHopHtlcAggregation(ht *lntest.HarnessTest, // level sweep. Now Bob should have no pending channels anymore, as // this just resolved it by the confirmation of the sweep transaction. block := ht.MineBlocksAndAssertNumTxes(1, numExpected)[0] - ht.Miner.AssertTxInBlock(block, &bobSweep) + ht.AssertTxInBlock(block, &bobSweep) // For leased channels, we need to mine one more block to confirm Bob's // commit output sweep. @@ -2441,7 +2441,7 @@ func runExtraPreimageFromRemoteCommit(ht *lntest.HarnessTest, // Mine a block to trigger the sweep, and clean up the anchor sweeping // tx. ht.MineBlocksAndAssertNumTxes(1, 1) - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // Restart Bob. Once he finishes syncing the channel state, he should // notice the force close from Carol. @@ -2457,7 +2457,7 @@ func runExtraPreimageFromRemoteCommit(ht *lntest.HarnessTest, // We should now have Carol's htlc success tx in the mempool. numTxesMempool := 1 - ht.Miner.AssertNumTxsInMempool(numTxesMempool) + ht.AssertNumTxsInMempool(numTxesMempool) // For neutrino backend, the timeout resolver needs to extract the // preimage from the blocks. @@ -2667,15 +2667,15 @@ func runExtraPreimageFromLocalCommit(ht *lntest.HarnessTest, switch c { case lnrpc.CommitmentType_LEGACY: htlcOutpoint.Index = 0 - ht.Miner.AssertNumTxsInMempool(2) + ht.AssertNumTxsInMempool(2) case lnrpc.CommitmentType_ANCHORS, lnrpc.CommitmentType_SIMPLE_TAPROOT: htlcOutpoint.Index = 2 - ht.Miner.AssertNumTxsInMempool(2) + ht.AssertNumTxsInMempool(2) case lnrpc.CommitmentType_SCRIPT_ENFORCED_LEASE: htlcOutpoint.Index = 2 - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) } // Get the current height to compute number of blocks to mine to diff --git a/itest/lnd_nonstd_sweep_test.go b/itest/lnd_nonstd_sweep_test.go index a85fbc786..6285fbdd6 100644 --- a/itest/lnd_nonstd_sweep_test.go +++ b/itest/lnd_nonstd_sweep_test.go @@ -96,7 +96,7 @@ func testNonStdSweepInner(ht *lntest.HarnessTest, address string) { carol.RPC.SendCoins(sendReq) // Fetch the txid so we can grab the raw transaction. - txid := ht.Miner.AssertNumTxsInMempool(1)[0] + txid := ht.AssertNumTxsInMempool(1)[0] tx := ht.Miner.GetRawTransaction(txid) msgTx := tx.MsgTx() diff --git a/itest/lnd_onchain_test.go b/itest/lnd_onchain_test.go index e66fc1b4d..92b1ec66b 100644 --- a/itest/lnd_onchain_test.go +++ b/itest/lnd_onchain_test.go @@ -717,7 +717,7 @@ func testRemoveTx(ht *lntest.HarnessTest) { TargetConf: 6, } alice.RPC.SendCoins(sendReq) - txID := ht.Miner.AssertNumTxsInMempool(1)[0] + txID := ht.AssertNumTxsInMempool(1)[0] // Make sure the unspent number of utxos is 2 and the unconfirmed // balances add up. @@ -767,7 +767,7 @@ func testRemoveTx(ht *lntest.HarnessTest) { // shows up in alice's wallet although we removed the transaction from // the wallet when it was unconfirmed. block := ht.Miner.MineBlocks(1)[0] - ht.Miner.AssertTxInBlock(block, txID) + ht.AssertTxInBlock(block, txID) // Verify that alice has 2 confirmed unspent utxos in her default // wallet. @@ -861,7 +861,7 @@ func testListSweeps(ht *lntest.HarnessTest) { ht.MineEmptyBlocks(1) // Now we can expect that the sweep has been broadcast. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // List all unconfirmed sweeps that alice's node had broadcast. sweepResp := alice.RPC.ListSweeps(false, -1) diff --git a/itest/lnd_open_channel_test.go b/itest/lnd_open_channel_test.go index 2ef426766..04179d1cf 100644 --- a/itest/lnd_open_channel_test.go +++ b/itest/lnd_open_channel_test.go @@ -45,7 +45,7 @@ func testOpenChannelAfterReorg(ht *lntest.HarnessTest) { // Wait for miner to have seen the funding tx. The temporary miner is // disconnected, and won't see the transaction. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // At this point, the channel's funding transaction will have been // broadcast, but not confirmed, and the channel should be pending. @@ -59,7 +59,7 @@ func testOpenChannelAfterReorg(ht *lntest.HarnessTest) { // channel on the original miner's chain, which should be considered // open. block := ht.MineBlocksAndAssertNumTxes(10, 1)[0] - ht.Miner.AssertTxInBlock(block, fundingTxID) + ht.AssertTxInBlock(block, fundingTxID) _, err = tempMiner.Client.Generate(15) require.NoError(ht, err, "unable to generate blocks") @@ -115,7 +115,7 @@ func testOpenChannelAfterReorg(ht *lntest.HarnessTest) { // Cleanup by mining the funding tx again, then closing the channel. block = ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, fundingTxID) + ht.AssertTxInBlock(block, fundingTxID) ht.CloseChannel(alice, chanPoint) } diff --git a/itest/lnd_psbt_test.go b/itest/lnd_psbt_test.go index 2d4118599..fe3e3a1d6 100644 --- a/itest/lnd_psbt_test.go +++ b/itest/lnd_psbt_test.go @@ -261,7 +261,7 @@ func runPsbtChanFunding(ht *lntest.HarnessTest, carol, dave *node.HarnessNode, } // No transaction should have been published yet. - ht.Miner.AssertNumTxsInMempool(0) + ht.AssertNumTxsInMempool(0) // Let's progress the second channel now. This time we'll use the raw // wire format transaction directly. @@ -295,7 +295,7 @@ func runPsbtChanFunding(ht *lntest.HarnessTest, carol, dave *node.HarnessNode, txHash := finalTx.TxHash() block := ht.MineBlocksAndAssertNumTxes(6, 1)[0] - ht.Miner.AssertTxInBlock(block, &txHash) + ht.AssertTxInBlock(block, &txHash) ht.AssertTopologyChannelOpen(carol, chanPoint) ht.AssertTopologyChannelOpen(carol, chanPoint2) @@ -456,7 +456,7 @@ func runPsbtChanFundingExternal(ht *lntest.HarnessTest, carol, finalizeRes := alice.RPC.FinalizePsbt(finalizeReq) // No transaction should have been published yet. - ht.Miner.AssertNumTxsInMempool(0) + ht.AssertNumTxsInMempool(0) // Great, now let's publish the final raw transaction. var finalTx wire.MsgTx @@ -470,7 +470,7 @@ func runPsbtChanFundingExternal(ht *lntest.HarnessTest, carol, // Now we can mine a block to get the transaction confirmed, then wait // for the new channel to be propagated through the network. block := ht.MineBlocksAndAssertNumTxes(6, 1)[0] - ht.Miner.AssertTxInBlock(block, &txHash) + ht.AssertTxInBlock(block, &txHash) ht.AssertTopologyChannelOpen(carol, chanPoint) ht.AssertTopologyChannelOpen(carol, chanPoint2) @@ -627,7 +627,7 @@ func runPsbtChanFundingSingleStep(ht *lntest.HarnessTest, carol, txHash := finalTx.TxHash() block := ht.MineBlocksAndAssertNumTxes(6, 1)[0] - ht.Miner.AssertTxInBlock(block, &txHash) + ht.AssertTxInBlock(block, &txHash) ht.AssertTopologyChannelOpen(carol, chanPoint) // Next, to make sure the channel functions as normal, we'll make some @@ -1326,7 +1326,7 @@ func extractPublishAndMine(ht *lntest.HarnessTest, node *node.HarnessNode, // Mine one block which should contain two transactions. block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] txHash := finalTx.TxHash() - ht.Miner.AssertTxInBlock(block, &txHash) + ht.AssertTxInBlock(block, &txHash) return finalTx } @@ -1432,8 +1432,8 @@ func assertPsbtSpend(ht *lntest.HarnessTest, alice *node.HarnessNode, block := ht.MineBlocksAndAssertNumTxes(1, 2)[0] firstTxHash := prevTx.TxHash() secondTxHash := finalTx.TxHash() - ht.Miner.AssertTxInBlock(block, &firstTxHash) - ht.Miner.AssertTxInBlock(block, &secondTxHash) + ht.AssertTxInBlock(block, &firstTxHash) + ht.AssertTxInBlock(block, &secondTxHash) } // assertPsbtFundSignSpend funds a PSBT from the internal wallet and then @@ -1794,7 +1794,7 @@ func testPsbtChanFundingWithUnstableUtxos(ht *lntest.HarnessTest) { txHash := finalTx.TxHash() block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, &txHash) + ht.AssertTxInBlock(block, &txHash) // Now we do the same but instead use preselected utxos to verify that // these utxos respects the utxo restrictions on sweeper unconfirmed @@ -1941,7 +1941,7 @@ func testPsbtChanFundingWithUnstableUtxos(ht *lntest.HarnessTest) { txHash = finalTx.TxHash() block = ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, &txHash) + ht.AssertTxInBlock(block, &txHash) ht.CloseChannel(carol, channelPoint3) } diff --git a/itest/lnd_recovery_test.go b/itest/lnd_recovery_test.go index 020c44fa4..1bb3eded7 100644 --- a/itest/lnd_recovery_test.go +++ b/itest/lnd_recovery_test.go @@ -260,11 +260,11 @@ func testOnchainFundRecovery(ht *lntest.HarnessTest) { } resp := node.RPC.SendCoins(req) - txid := ht.Miner.AssertNumTxsInMempool(1)[0] + txid := ht.AssertNumTxsInMempool(1)[0] require.Equal(ht, txid.String(), resp.Txid) block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, txid) + ht.AssertTxInBlock(block, txid) } restoreCheckBalance(finalBalance, 9, 20, promptChangeAddr) @@ -428,7 +428,7 @@ func testRescanAddressDetection(ht *lntest.HarnessTest) { }) // Wait until the spending tx is found and mine a block to confirm it. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) ht.MineBlocks(1) // The wallet should still just see a single UTXO of the change output diff --git a/itest/lnd_revocation_test.go b/itest/lnd_revocation_test.go index be87d8a75..56260c7d1 100644 --- a/itest/lnd_revocation_test.go +++ b/itest/lnd_revocation_test.go @@ -122,7 +122,7 @@ func breachRetributionTestCase(ht *lntest.HarnessTest, // update, then ensure that the closing transaction was included in the // block. block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] - ht.Miner.AssertTxInBlock(block, breachTXID) + ht.AssertTxInBlock(block, breachTXID) // Construct to_remote output which pays to Bob. Based on the output // ordering, the first output in this breach tx is the to_remote @@ -174,7 +174,7 @@ func breachRetributionTestCase(ht *lntest.HarnessTest, // transaction which was just accepted into the mempool. block = ht.MineBlocksAndAssertNumTxes(1, 1)[0] justiceTxid := justiceTx.TxHash() - ht.Miner.AssertTxInBlock(block, &justiceTxid) + ht.AssertTxInBlock(block, &justiceTxid) ht.AssertNodeNumChannels(carol, 0) @@ -363,7 +363,7 @@ func revokedCloseRetributionZeroValueRemoteOutputCase(ht *lntest.HarnessTest, // transaction which was just accepted into the mempool. block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] justiceTxid := justiceTx.TxHash() - ht.Miner.AssertTxInBlock(block, &justiceTxid) + ht.AssertTxInBlock(block, &justiceTxid) // At this point, Dave should have no pending channels. ht.AssertNodeNumChannels(dave, 0) @@ -559,7 +559,7 @@ func revokedCloseRetributionRemoteHodlCase(ht *lntest.HarnessTest, breachTXID := ht.WaitForChannelCloseEvent(closeUpdates) require.Equal(ht, closeTxID[:], breachTXID[:], "expected breach ID to be equal to close ID") - ht.Miner.AssertTxInBlock(block, breachTXID) + ht.AssertTxInBlock(block, breachTXID) // Query the mempool for Dave's justice transaction, this should be // broadcast as Carol's contract breaching transaction gets confirmed diff --git a/itest/lnd_signer_test.go b/itest/lnd_signer_test.go index 23773eb71..bf9033cad 100644 --- a/itest/lnd_signer_test.go +++ b/itest/lnd_signer_test.go @@ -296,7 +296,7 @@ func assertSignOutputRaw(ht *lntest.HarnessTest, alice.RPC.SendCoins(req) // Wait until the TX is found in the mempool. - txid := ht.Miner.AssertNumTxsInMempool(1)[0] + txid := ht.AssertNumTxsInMempool(1)[0] targetOutputIndex := ht.GetOutputIndex(txid, targetAddr.String()) @@ -359,7 +359,7 @@ func assertSignOutputRaw(ht *lntest.HarnessTest, }) // Wait until the spending tx is found. - txid = ht.Miner.AssertNumTxsInMempool(1)[0] + txid = ht.AssertNumTxsInMempool(1)[0] p2wkhOutputIndex := ht.GetOutputIndex(txid, p2wkhAdrr.String()) op := &lnrpc.OutPoint{ diff --git a/itest/lnd_sweep_test.go b/itest/lnd_sweep_test.go index 75b69084c..48cafcff8 100644 --- a/itest/lnd_sweep_test.go +++ b/itest/lnd_sweep_test.go @@ -266,7 +266,7 @@ func testSweepCPFPAnchorOutgoingTimeout(ht *lntest.HarnessTest) { // We expect to see two txns in the mempool, // - Bob's force close tx. // - Bob's anchor sweep tx. - ht.Miner.AssertNumTxsInMempool(2) + ht.AssertNumTxsInMempool(2) // We expect the fees to increase by i*delta. expectedFee := startFeeAnchor + feeDelta.MulF64(float64(i)) @@ -887,7 +887,7 @@ func testSweepHTLCs(ht *lntest.HarnessTest) { ht.AssertNumPendingSweeps(bob, 2) // Assert Bob's force closing tx has been broadcast. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // Mine the force close tx, which triggers Bob's contractcourt to offer // his outgoing HTLC to his sweeper. @@ -982,7 +982,7 @@ func testSweepHTLCs(ht *lntest.HarnessTest) { outgoingFuncPosition++ // We should see Bob's sweeping tx in the mempool. - ht.Miner.AssertNumTxsInMempool(1) + ht.AssertNumTxsInMempool(1) // Make sure Bob's old sweeping tx has been removed from the // mempool. @@ -1156,7 +1156,7 @@ func testSweepHTLCs(ht *lntest.HarnessTest) { // We should see two txns in the mempool, // - the incoming HTLC sweeping tx. // - the outgoing HTLC sweeping tx. - ht.Miner.AssertNumTxsInMempool(2) + ht.AssertNumTxsInMempool(2) // Make sure Bob's old sweeping txns have been removed from the // mempool. @@ -1600,7 +1600,7 @@ func testSweepCommitOutputAndAnchor(ht *lntest.HarnessTest) { // We expect to see both Alice's and Bob's sweeping txns in the // mempool. - ht.Miner.AssertNumTxsInMempool(2) + ht.AssertNumTxsInMempool(2) // Make sure Alice's old sweeping tx has been removed from the // mempool. @@ -1687,7 +1687,7 @@ func testSweepCommitOutputAndAnchor(ht *lntest.HarnessTest) { // We expect to see both Alice's and Bob's sweeping txns in the // mempool. - ht.Miner.AssertNumTxsInMempool(2) + ht.AssertNumTxsInMempool(2) // Make sure Alice's old sweeping tx has been removed from the // mempool. diff --git a/itest/lnd_taproot_test.go b/itest/lnd_taproot_test.go index 3b5cb4799..3bc642289 100644 --- a/itest/lnd_taproot_test.go +++ b/itest/lnd_taproot_test.go @@ -107,7 +107,7 @@ func testTaprootSendCoinsKeySpendBip86(ht *lntest.HarnessTest, TargetConf: 6, }) - txid := ht.Miner.AssertNumTxsInMempool(1)[0] + txid := ht.AssertNumTxsInMempool(1)[0] // Wait until bob has seen the tx and considers it as owned. p2trOutputIndex := ht.GetOutputIndex(txid, p2trResp.Address) @@ -162,7 +162,7 @@ func testTaprootComputeInputScriptKeySpendBip86(ht *lntest.HarnessTest, alice.RPC.SendCoins(req) // Wait until bob has seen the tx and considers it as owned. - txid := ht.Miner.AssertNumTxsInMempool(1)[0] + txid := ht.AssertNumTxsInMempool(1)[0] p2trOutputIndex := ht.GetOutputIndex(txid, p2trAddr.String()) op := &lnrpc.OutPoint{ TxidBytes: txid[:], @@ -1413,7 +1413,7 @@ func clearWalletImportedTapscriptBalance(ht *lntest.HarnessTest, // Mine one block which should contain the sweep transaction. block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] sweepTxHash := sweepTx.TxHash() - ht.Miner.AssertTxInBlock(block, &sweepTxHash) + ht.AssertTxInBlock(block, &sweepTxHash) } // testScriptHashLock returns a simple bitcoin script that locks the funds to @@ -1481,7 +1481,7 @@ func sendToTaprootOutput(ht *lntest.HarnessTest, hn *node.HarnessNode, hn.RPC.SendCoins(req) // Wait until the TX is found in the mempool. - txid := ht.Miner.AssertNumTxsInMempool(1)[0] + txid := ht.AssertNumTxsInMempool(1)[0] p2trOutputIndex := ht.GetOutputIndex(txid, tapScriptAddr.String()) p2trOutpoint := wire.OutPoint{ Hash: *txid, @@ -1592,7 +1592,7 @@ func confirmAddress(ht *lntest.HarnessTest, hn *node.HarnessNode, addrString string) { // Wait until the tx that sends to the address is found. - txid := ht.Miner.AssertNumTxsInMempool(1)[0] + txid := ht.AssertNumTxsInMempool(1)[0] // Wait until bob has seen the tx and considers it as owned. addrOutputIndex := ht.GetOutputIndex(txid, addrString) diff --git a/itest/lnd_wallet_import_test.go b/itest/lnd_wallet_import_test.go index 2d41da893..b9cce9f1e 100644 --- a/itest/lnd_wallet_import_test.go +++ b/itest/lnd_wallet_import_test.go @@ -372,7 +372,7 @@ func fundChanAndCloseFromImportedAccount(ht *lntest.HarnessTest, srcNode, ) block := ht.MineBlocksAndAssertNumTxes(6, 1)[0] - ht.Miner.AssertTxInBlock(block, txHash) + ht.AssertTxInBlock(block, txHash) confBalanceAfterChan += chanChangeUtxoAmt ht.AssertWalletAccountBalance(srcNode, account, 0, 0) @@ -389,7 +389,7 @@ func fundChanAndCloseFromImportedAccount(ht *lntest.HarnessTest, srcNode, ) block := ht.MineBlocksAndAssertNumTxes(6, 1)[0] - ht.Miner.AssertTxInBlock(block, txHash) + ht.AssertTxInBlock(block, txHash) confBalanceAfterChan += chanChangeUtxoAmt ht.AssertWalletAccountBalance( diff --git a/itest/lnd_watchtower_test.go b/itest/lnd_watchtower_test.go index c64b1e43a..ab724c0b6 100644 --- a/itest/lnd_watchtower_test.go +++ b/itest/lnd_watchtower_test.go @@ -502,7 +502,7 @@ func testRevokedCloseRetributionAltruistWatchtowerCase(ht *lntest.HarnessTest, block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] breachTXID := ht.WaitForChannelCloseEvent(closeUpdates) - ht.Miner.AssertTxInBlock(block, breachTXID) + ht.AssertTxInBlock(block, breachTXID) // The breachTXID should match the above closeTxID. require.EqualValues(ht, breachTXID, closeTxID) @@ -510,7 +510,7 @@ func testRevokedCloseRetributionAltruistWatchtowerCase(ht *lntest.HarnessTest, // Query the mempool for Dave's justice transaction, this should be // broadcast as Carol's contract breaching transaction gets confirmed // above. - justiceTXID := ht.Miner.AssertNumTxsInMempool(1)[0] + justiceTXID := ht.AssertNumTxsInMempool(1)[0] // Query for the mempool transaction found above. Then assert that all // the inputs of this transaction are spending outputs generated by diff --git a/itest/lnd_zero_conf_test.go b/itest/lnd_zero_conf_test.go index a02e5c006..eb253e4dd 100644 --- a/itest/lnd_zero_conf_test.go +++ b/itest/lnd_zero_conf_test.go @@ -110,7 +110,7 @@ func testZeroConfChannelOpen(ht *lntest.HarnessTest) { fundingTxID := ht.GetChanPointFundingTxid(fundingPoint2) - ht.Miner.AssertTxInBlock(block, fundingTxID) + ht.AssertTxInBlock(block, fundingTxID) daveInvoiceResp3 := dave.RPC.AddInvoice(daveInvoiceParams) ht.CompletePaymentRequests( @@ -159,7 +159,7 @@ func testZeroConfChannelOpen(ht *lntest.HarnessTest) { block = ht.MineBlocksAndAssertNumTxes(6, 1)[0] fundingTxID = ht.GetChanPointFundingTxid(fundingPoint3) - ht.Miner.AssertTxInBlock(block, fundingTxID) + ht.AssertTxInBlock(block, fundingTxID) // Wait until Eve's ZeroConf channel is replaced by the confirmed SCID // in her graph. diff --git a/lntest/harness.go b/lntest/harness.go index 6e8476c41..a2e5caf7e 100644 --- a/lntest/harness.go +++ b/lntest/harness.go @@ -1193,7 +1193,7 @@ func (h *HarnessTest) openChannel(alice, bob *node.HarnessNode, // Check that the funding tx is found in the first block. fundingTxID := h.GetChanPointFundingTxid(fundingChanPoint) - h.Miner.AssertTxInBlock(block, fundingTxID) + h.AssertTxInBlock(block, fundingTxID) // Check that both alice and bob have seen the channel from their // network topology. diff --git a/lntest/harness_assertion.go b/lntest/harness_assertion.go index f3d8244ed..e1e3aeead 100644 --- a/lntest/harness_assertion.go +++ b/lntest/harness_assertion.go @@ -656,7 +656,7 @@ func (h *HarnessTest) AssertStreamChannelCoopClosed(hn *node.HarnessNode, // Consume one close event and assert the closing txid can be found in // the block. closingTxid := h.WaitForChannelCloseEvent(stream) - h.Miner.AssertTxInBlock(block, closingTxid) + h.AssertTxInBlock(block, closingTxid) // We should see zero waiting close channels now. h.AssertNumWaitingClose(hn, 0) @@ -700,7 +700,7 @@ func (h *HarnessTest) AssertStreamChannelForceClosed(hn *node.HarnessNode, // Consume one close event and assert the closing txid can be found in // the block. closingTxid := h.WaitForChannelCloseEvent(stream) - h.Miner.AssertTxInBlock(block, closingTxid) + h.AssertTxInBlock(block, closingTxid) // We should see zero waiting close channels and 1 pending force close // channels now. @@ -2564,7 +2564,7 @@ func (h *HarnessTest) AssertClosingTxInMempool(cp *lnrpc.ChannelPoint, } // Wait for the expected txes to be found in the mempool. - h.Miner.AssertNumTxsInMempool(expectedTxes) + h.AssertNumTxsInMempool(expectedTxes) // Get the closing tx from the mempool. op := h.OutPointFromChannelPoint(cp) @@ -2578,7 +2578,7 @@ func (h *HarnessTest) AssertClosingTxInMempool(cp *lnrpc.ChannelPoint, // will assert the anchor sweep tx is also in the mempool. func (h *HarnessTest) MineClosingTx(cp *lnrpc.ChannelPoint) *wire.MsgTx { // Wait for the expected txes to be found in the mempool. - h.Miner.AssertNumTxsInMempool(1) + h.AssertNumTxsInMempool(1) // Get the closing tx from the mempool. op := h.OutPointFromChannelPoint(cp) diff --git a/lntest/harness_miner.go b/lntest/harness_miner.go index 5be0cf502..9d4dd99d6 100644 --- a/lntest/harness_miner.go +++ b/lntest/harness_miner.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/btcsuite/btcd/blockchain" + "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "github.com/lightningnetwork/lnd/lntest/node" "github.com/lightningnetwork/lnd/lntest/wait" @@ -177,3 +178,17 @@ func (h *HarnessTest) mineTillForceCloseResolved(hn *node.HarnessNode) { require.NoErrorf(h, err, "assert force close resolved timeout") } + +// AssertNumTxsInMempool polls until finding the desired number of transactions +// in the provided miner's mempool. It will asserrt if this number is not met +// after the given timeout. +func (h *HarnessTest) AssertNumTxsInMempool(n int) []*chainhash.Hash { + return h.Miner.AssertNumTxsInMempool(n) +} + +// AssertTxInBlock asserts that a given txid can be found in the passed block. +func (h *HarnessTest) AssertTxInBlock(block *wire.MsgBlock, + txid *chainhash.Hash) { + + h.Miner.AssertTxInBlock(block, txid) +}