itest+lntest: fix onchain tests

This commit is contained in:
yyforyongyu
2024-04-08 16:48:43 +08:00
parent 9c34eb7a56
commit d4de6dd236
2 changed files with 30 additions and 20 deletions

View File

@@ -581,10 +581,20 @@ func testAnchorThirdPartySpend(ht *lntest.HarnessTest) {
ht.MineBlocksAndAssertNumTxes(1, 1) ht.MineBlocksAndAssertNumTxes(1, 1)
forceCloseTxID, _ := chainhash.NewHashFromStr(aliceCloseTx) forceCloseTxID, _ := chainhash.NewHashFromStr(aliceCloseTx)
// Alice's should have the anchor sweep request.
ht.AssertNumPendingSweeps(alice, 1)
// Mine 3 blocks so Alice will sweep her commit output.
forceClose := ht.AssertChannelPendingForceClose(alice, aliceChanPoint1)
ht.MineEmptyBlocks(int(forceClose.BlocksTilMaturity) - 1)
// Alice's should have two sweep request - one for anchor output, the
// other for commit output.
ht.AssertNumPendingSweeps(alice, 2)
// Mine one block to trigger Alice's sweeper to reconsider the anchor // Mine one block to trigger Alice's sweeper to reconsider the anchor
// sweeping. Because we are now sweeping at the fee rate floor, the // sweeping - it will be swept with her commit output together in one
// sweeper will consider this input has positive yield thus attempts // tx.
// the sweeping.
ht.MineEmptyBlocks(1) ht.MineEmptyBlocks(1)
sweepTxns := ht.Miner.GetNumTxsFromMempool(1) sweepTxns := ht.Miner.GetNumTxsFromMempool(1)
_, aliceAnchor := ht.FindCommitAndAnchor(sweepTxns, aliceCloseTx) _, aliceAnchor := ht.FindCommitAndAnchor(sweepTxns, aliceCloseTx)
@@ -624,21 +634,20 @@ func testAnchorThirdPartySpend(ht *lntest.HarnessTest) {
ht.AssertTransactionInWallet(alice, aliceAnchor.SweepTx.TxHash()) ht.AssertTransactionInWallet(alice, aliceAnchor.SweepTx.TxHash())
ht.AssertTransactionInWallet(alice, *sweepAllTxID) ht.AssertTransactionInWallet(alice, *sweepAllTxID)
// Next, we'll shutdown Alice, and allow 16 blocks to pass so that the // Next, we mine enough blocks to pass so that the anchor output can be
// anchor output can be swept by anyone. Rather than use the normal API // swept by anyone. Rather than use the normal API call, we'll generate
// call, we'll generate a series of _empty_ blocks here. // a series of _empty_ blocks here.
aliceRestart := ht.SuspendNode(alice) //
// TODO(yy): also check the restart behavior of Alice.
const anchorCsv = 16 const anchorCsv = 16
ht.MineEmptyBlocks(anchorCsv - 1) ht.MineEmptyBlocks(anchorCsv - defaultCSV - 1)
// Before we sweep the anchor, we'll restart Alice.
require.NoErrorf(ht, aliceRestart(), "unable to restart alice")
// Now that the channel has been closed, and Alice has an unconfirmed // Now that the channel has been closed, and Alice has an unconfirmed
// transaction spending the output produced by her anchor sweep, we'll // transaction spending the output produced by her anchor sweep, we'll
// mine a transaction that double spends the output. // mine a transaction that double spends the output.
thirdPartyAnchorSweep := genAnchorSweep(ht, aliceAnchor, anchorCsv) thirdPartyAnchorSweep := genAnchorSweep(ht, aliceAnchor, anchorCsv)
ht.Miner.MineBlockWithTxes([]*btcutil.Tx{thirdPartyAnchorSweep}) ht.Logf("Third party tx=%v", thirdPartyAnchorSweep.TxHash())
ht.Miner.MineBlockWithTx(thirdPartyAnchorSweep)
// At this point, we should no longer find Alice's transaction that // At this point, we should no longer find Alice's transaction that
// tried to sweep the anchor in her wallet. // tried to sweep the anchor in her wallet.
@@ -652,6 +661,11 @@ func testAnchorThirdPartySpend(ht *lntest.HarnessTest) {
// response is still present. // response is still present.
assertAnchorOutputLost(ht, alice, aliceChanPoint1) assertAnchorOutputLost(ht, alice, aliceChanPoint1)
// We now one block so Alice's commit output will be re-offered to her
// sweeper again.
ht.MineEmptyBlocks(1)
ht.AssertNumPendingSweeps(alice, 1)
// At this point Alice's CSV output should already be fully spent and // At this point Alice's CSV output should already be fully spent and
// the channel marked as being resolved. We mine a block first, as so // the channel marked as being resolved. We mine a block first, as so
// far we've been generating custom blocks this whole time. // far we've been generating custom blocks this whole time.
@@ -702,7 +716,7 @@ func assertAnchorOutputLost(ht *lntest.HarnessTest, hn *node.HarnessNode,
// In practice, we just re-use the existing witness, and track on our own // In practice, we just re-use the existing witness, and track on our own
// output producing a 1-in-1-out transaction. // output producing a 1-in-1-out transaction.
func genAnchorSweep(ht *lntest.HarnessTest, func genAnchorSweep(ht *lntest.HarnessTest,
aliceAnchor *lntest.SweptOutput, anchorCsv uint32) *btcutil.Tx { aliceAnchor *lntest.SweptOutput, anchorCsv uint32) *wire.MsgTx {
// At this point, we have the transaction that Alice used to try to // At this point, we have the transaction that Alice used to try to
// sweep her anchor. As this is actually just something anyone can // sweep her anchor. As this is actually just something anyone can
@@ -739,7 +753,7 @@ func genAnchorSweep(ht *lntest.HarnessTest,
Value: anchorSize - 1, Value: anchorSize - 1,
}) })
return btcutil.NewTx(tx) return tx
} }
// testRemoveTx tests that we are able to remove an unconfirmed transaction // testRemoveTx tests that we are able to remove an unconfirmed transaction
@@ -912,7 +926,7 @@ func testListSweeps(ht *lntest.HarnessTest) {
ht.AssertNumPendingSweeps(alice, 1) ht.AssertNumPendingSweeps(alice, 1)
// Mine a block to trigger the sweep. // Mine a block to trigger the sweep.
ht.MineBlocks(1) ht.MineEmptyBlocks(1)
// Now we can expect that the sweep has been broadcast. // Now we can expect that the sweep has been broadcast.
pendingTxHash := ht.Miner.AssertNumTxsInMempool(1) pendingTxHash := ht.Miner.AssertNumTxsInMempool(1)
@@ -939,7 +953,7 @@ func testListSweeps(ht *lntest.HarnessTest) {
require.Lenf(ht, txIDs, 3, "number of sweeps, starting from height 0") require.Lenf(ht, txIDs, 3, "number of sweeps, starting from height 0")
// Mine the pending sweep and make sure it is no longer returned. // Mine the pending sweep and make sure it is no longer returned.
ht.MineBlocks(1) ht.MineBlocksAndAssertNumTxes(1, 1)
sweepResp = alice.RPC.ListSweeps(false, -1) sweepResp = alice.RPC.ListSweeps(false, -1)
txIDs = sweepResp.GetTransactionIds().TransactionIds txIDs = sweepResp.GetTransactionIds().TransactionIds
require.Empty(ht, txIDs, "pending sweep should not be returned") require.Empty(ht, txIDs, "pending sweep should not be returned")

View File

@@ -323,10 +323,6 @@ func (h *HarnessMiner) AssertTxNotInMempool(txid chainhash.Hash) *wire.MsgTx {
// it as it's an unexpected behavior. // it as it's an unexpected behavior.
mempool := h.GetRawMempool() mempool := h.GetRawMempool()
if len(mempool) == 0 {
return fmt.Errorf("empty mempool")
}
for _, memTx := range mempool { for _, memTx := range mempool {
// Check the values are equal. // Check the values are equal.
if txid.IsEqual(memTx) { if txid.IsEqual(memTx) {