lntest+itest: strictly define the behavior of MineBlocks
This commit adds more assertion to `MineBlocks` so the caller won't misuse it.
This commit is contained in:
1 parent
91b20e661b
commit
e553895ddd
8 files changed
+52
-31
No files matched your search
@@ -93,7 +93,7 @@ func testHoldInvoiceForceClose(ht *lntest.HarnessTest) {
|
|||||||
// TODO(yy): fix block height asymmetry among all the subsystems.
|
// TODO(yy): fix block height asymmetry among all the subsystems.
|
||||||
//
|
//
|
||||||
// We first mine enough blocks to trigger an invoice cancelation.
|
// We first mine enough blocks to trigger an invoice cancelation.
|
||||||
ht.MineBlocks(blocksTillCancel)
|
ht.MineBlocks(int(blocksTillCancel))
|
||||||
|
|
||||||
// Wait for the nodes to be synced.
|
// Wait for the nodes to be synced.
|
||||||
ht.WaitForBlockchainSync(alice)
|
ht.WaitForBlockchainSync(alice)
|
||||||
@@ -133,7 +133,7 @@ func testHoldInvoiceForceClose(ht *lntest.HarnessTest) {
|
|||||||
// happen in bitcoind backend, as Alice's CNCT was syncing way faster
|
// happen in bitcoind backend, as Alice's CNCT was syncing way faster
|
||||||
// than Bob's INVC, causing the channel being force closed before the
|
// than Bob's INVC, causing the channel being force closed before the
|
||||||
// invoice cancelation message was received by Alice.
|
// invoice cancelation message was received by Alice.
|
||||||
ht.MineBlocks(blocksTillForce - blocksTillCancel)
|
ht.MineBlocks(int(blocksTillForce - blocksTillCancel))
|
||||||
|
|
||||||
// Wait for the nodes to be synced.
|
// Wait for the nodes to be synced.
|
||||||
ht.WaitForBlockchainSync(alice)
|
ht.WaitForBlockchainSync(alice)
|
||||||
|
|||||||
@@ -243,7 +243,7 @@ func runMultiHopHtlcLocalTimeout(ht *lntest.HarnessTest,
|
|||||||
numBlocks := padCLTV(
|
numBlocks := padCLTV(
|
||||||
uint32(finalCltvDelta - lncfg.DefaultOutgoingBroadcastDelta),
|
uint32(finalCltvDelta - lncfg.DefaultOutgoingBroadcastDelta),
|
||||||
)
|
)
|
||||||
ht.MineBlocks(numBlocks)
|
ht.MineBlocks(int(numBlocks))
|
||||||
|
|
||||||
// Bob's force close transaction should now be found in the mempool.
|
// Bob's force close transaction should now be found in the mempool.
|
||||||
ht.Miner.AssertNumTxsInMempool(1)
|
ht.Miner.AssertNumTxsInMempool(1)
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ func testOpenChannelAfterReorg(ht *lntest.HarnessTest) {
|
|||||||
// and our new miner mine 15. This will also confirm our pending
|
// and our new miner mine 15. This will also confirm our pending
|
||||||
// channel on the original miner's chain, which should be considered
|
// channel on the original miner's chain, which should be considered
|
||||||
// open.
|
// open.
|
||||||
block := ht.MineBlocks(10)[0]
|
block := ht.MineBlocksAndAssertNumTxes(10, 1)[0]
|
||||||
ht.Miner.AssertTxInBlock(block, fundingTxID)
|
ht.Miner.AssertTxInBlock(block, fundingTxID)
|
||||||
_, err = tempMiner.Client.Generate(15)
|
_, err = tempMiner.Client.Generate(15)
|
||||||
require.NoError(ht, err, "unable to generate blocks")
|
require.NoError(ht, err, "unable to generate blocks")
|
||||||
|
|||||||
@@ -263,7 +263,7 @@ func testOnchainFundRecovery(ht *lntest.HarnessTest) {
|
|||||||
txid := ht.Miner.AssertNumTxsInMempool(1)[0]
|
txid := ht.Miner.AssertNumTxsInMempool(1)[0]
|
||||||
require.Equal(ht, txid.String(), resp.Txid)
|
require.Equal(ht, txid.String(), resp.Txid)
|
||||||
|
|
||||||
block := ht.MineBlocks(1)[0]
|
block := ht.MineBlocksAndAssertNumTxes(1, 1)[0]
|
||||||
ht.Miner.AssertTxInBlock(block, txid)
|
ht.Miner.AssertTxInBlock(block, txid)
|
||||||
}
|
}
|
||||||
restoreCheckBalance(finalBalance, 9, 20, promptChangeAddr)
|
restoreCheckBalance(finalBalance, 9, 20, promptChangeAddr)
|
||||||
|
|||||||
@@ -1021,7 +1021,7 @@ func testErrorHandlingOnChainFailure(ht *lntest.HarnessTest) {
|
|||||||
// value.
|
// value.
|
||||||
info := ht.Bob.RPC.GetInfo()
|
info := ht.Bob.RPC.GetInfo()
|
||||||
target := carolHTLC.IncomingExpiry - info.BlockHeight
|
target := carolHTLC.IncomingExpiry - info.BlockHeight
|
||||||
ht.MineBlocks(target)
|
ht.MineBlocks(int(target))
|
||||||
|
|
||||||
// Wait for Bob's timeout transaction in the mempool, since we've
|
// Wait for Bob's timeout transaction in the mempool, since we've
|
||||||
// suspended Carol we don't need to account for her commitment output
|
// suspended Carol we don't need to account for her commitment output
|
||||||
|
|||||||
@@ -872,7 +872,7 @@ func testSweepHTLCs(ht *lntest.HarnessTest) {
|
|||||||
numBlocks := padCLTV(uint32(
|
numBlocks := padCLTV(uint32(
|
||||||
invoiceReqHold.CltvExpiry - lncfg.DefaultOutgoingBroadcastDelta,
|
invoiceReqHold.CltvExpiry - lncfg.DefaultOutgoingBroadcastDelta,
|
||||||
))
|
))
|
||||||
ht.MineBlocks(numBlocks)
|
ht.MineBlocks(int(numBlocks))
|
||||||
|
|
||||||
// Before we mine empty blocks to check the RBF behavior, we need to be
|
// Before we mine empty blocks to check the RBF behavior, we need to be
|
||||||
// aware that Bob's incoming HTLC will expire before his outgoing HTLC
|
// aware that Bob's incoming HTLC will expire before his outgoing HTLC
|
||||||
@@ -1804,7 +1804,7 @@ func createSimpleNetwork(ht *lntest.HarnessTest, nodeCfg []string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Mine 1 block to get the above coins confirmed.
|
// Mine 1 block to get the above coins confirmed.
|
||||||
ht.MineBlocks(1)
|
ht.MineBlocksAndAssertNumTxes(1, numNodes-1)
|
||||||
|
|
||||||
// Open channels in batch to save blocks mined.
|
// Open channels in batch to save blocks mined.
|
||||||
reqs := make([]*lntest.OpenChannelRequest, 0, len(nodes)-1)
|
reqs := make([]*lntest.OpenChannelRequest, 0, len(nodes)-1)
|
||||||
|
|||||||
+7
-7
@@ -1414,24 +1414,24 @@ func (h *HarnessTest) fundCoins(amt btcutil.Amount, target *node.HarnessNode,
|
|||||||
pkScriptStr := utxos[0].PkScript
|
pkScriptStr := utxos[0].PkScript
|
||||||
require.Equal(h, pkScriptStr, expPkScriptStr,
|
require.Equal(h, pkScriptStr, expPkScriptStr,
|
||||||
"pkscript mismatch")
|
"pkscript mismatch")
|
||||||
|
|
||||||
|
expectedBalance := btcutil.Amount(
|
||||||
|
initialBalance.UnconfirmedBalance,
|
||||||
|
) + amt
|
||||||
|
h.WaitForBalanceUnconfirmed(target, expectedBalance)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the transaction should remain unconfirmed, then we'll wait until
|
// If the transaction should remain unconfirmed, then we'll wait until
|
||||||
// the target node's unconfirmed balance reflects the expected balance
|
// the target node's unconfirmed balance reflects the expected balance
|
||||||
// and exit.
|
// and exit.
|
||||||
if !confirmed && !h.IsNeutrinoBackend() {
|
if !confirmed {
|
||||||
expectedBalance := btcutil.Amount(
|
|
||||||
initialBalance.UnconfirmedBalance,
|
|
||||||
) + amt
|
|
||||||
h.WaitForBalanceUnconfirmed(target, expectedBalance)
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, we'll generate 1 new blocks to ensure the output gains a
|
// Otherwise, we'll generate 1 new blocks to ensure the output gains a
|
||||||
// sufficient number of confirmations and wait for the balance to
|
// sufficient number of confirmations and wait for the balance to
|
||||||
// reflect what's expected.
|
// reflect what's expected.
|
||||||
h.MineBlocks(1)
|
h.MineBlocksAndAssertNumTxes(1, 1)
|
||||||
|
|
||||||
expectedBalance := btcutil.Amount(initialBalance.ConfirmedBalance) + amt
|
expectedBalance := btcutil.Amount(initialBalance.ConfirmedBalance) + amt
|
||||||
h.WaitForBalanceConfirmed(target, expectedBalance)
|
h.WaitForBalanceConfirmed(target, expectedBalance)
|
||||||
|
|||||||
+37
-16
@@ -3,6 +3,7 @@ package lntest
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/blockchain"
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
"github.com/lightningnetwork/lnd/lntest/node"
|
"github.com/lightningnetwork/lnd/lntest/node"
|
||||||
"github.com/lightningnetwork/lnd/lntest/wait"
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
||||||
@@ -10,22 +11,46 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// MineBlocks mines blocks and asserts all active nodes have synced to the
|
// MineBlocks mines blocks and asserts all active nodes have synced to the
|
||||||
// chain.
|
// chain. It assumes no txns are expected in the blocks.
|
||||||
//
|
//
|
||||||
// NOTE: this differs from miner's `MineBlocks` as it requires the nodes to be
|
// NOTE: Use `MineBlocksAndAssertNumTxes` if you expect txns in the blocks. Use
|
||||||
// synced.
|
// `MineEmptyBlocks` if you want to make sure that txns stay unconfirmed.
|
||||||
func (h *HarnessTest) MineBlocks(num uint32) []*wire.MsgBlock {
|
func (h *HarnessTest) MineBlocks(num int) {
|
||||||
require.Less(h, num, uint32(maxBlocksAllowed),
|
require.Less(h, num, maxBlocksAllowed, "too many blocks to mine")
|
||||||
"too many blocks to mine")
|
|
||||||
|
|
||||||
// Mining the blocks slow to give `lnd` more time to sync.
|
// Mine num of blocks.
|
||||||
blocks := h.Miner.MineBlocksSlow(num)
|
for i := 0; i < num; i++ {
|
||||||
|
block := h.Miner.MineBlocks(1)[0]
|
||||||
|
|
||||||
// Make sure all the active nodes are synced.
|
// Check the block doesn't have any txns except the coinbase.
|
||||||
bestBlock := blocks[len(blocks)-1]
|
if len(block.Transactions) <= 1 {
|
||||||
h.AssertActiveNodesSyncedTo(bestBlock)
|
// Make sure all the active nodes are synced.
|
||||||
|
h.AssertActiveNodesSyncedTo(block)
|
||||||
|
|
||||||
return blocks
|
// Mine the next block.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a detailed description.
|
||||||
|
desc := fmt.Sprintf("block %v has %d txns:\n",
|
||||||
|
block.BlockHash(), len(block.Transactions)-1)
|
||||||
|
|
||||||
|
// Print all the txns except the coinbase.
|
||||||
|
for _, tx := range block.Transactions {
|
||||||
|
if blockchain.IsCoinBaseTx(tx) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
desc += fmt.Sprintf("%v\n", tx.TxHash())
|
||||||
|
}
|
||||||
|
|
||||||
|
desc += "Consider using `MineBlocksAndAssertNumTxes` if you " +
|
||||||
|
"expect txns, or `MineEmptyBlocks` if you want to " +
|
||||||
|
"keep the txns unconfirmed."
|
||||||
|
|
||||||
|
// Raise an error if the block has txns.
|
||||||
|
require.Fail(h, "MineBlocks", desc)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MineEmptyBlocks mines a given number of empty blocks.
|
// MineEmptyBlocks mines a given number of empty blocks.
|
||||||
@@ -49,10 +74,6 @@ func (h *HarnessTest) MineEmptyBlocks(num int) []*wire.MsgBlock {
|
|||||||
//
|
//
|
||||||
// NOTE: this differs from miner's `MineBlocks` as it requires the nodes to be
|
// NOTE: this differs from miner's `MineBlocks` as it requires the nodes to be
|
||||||
// synced.
|
// synced.
|
||||||
//
|
|
||||||
// TODO(yy): change the APIs to force callers to think about blocks and txns:
|
|
||||||
// - MineBlocksAndAssertNumTxes -> MineBlocks
|
|
||||||
// - add more APIs to mine a single tx.
|
|
||||||
func (h *HarnessTest) MineBlocksAndAssertNumTxes(num uint32,
|
func (h *HarnessTest) MineBlocksAndAssertNumTxes(num uint32,
|
||||||
numTxs int) []*wire.MsgBlock {
|
numTxs int) []*wire.MsgBlock {
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user