lntest+itest: make Miner a private instance

This commit is contained in:
yyforyongyu 2024-05-01 19:09:45 +08:00 committed by yyforyongyu
parent be4dba5da6
commit 14e7b134d9
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
9 changed files with 61 additions and 51 deletions

View File

@ -709,7 +709,7 @@ func channelForceClosureTest(ht *lntest.HarnessTest,
// NOTE: we don't check `len(mempool) == 1` because it will
// give us false positive.
err := wait.NoError(func() error {
mempool := ht.Miner.GetRawMempool()
mempool := ht.Miner().GetRawMempool()
if len(mempool) == 2 {
return nil
}

View File

@ -125,7 +125,7 @@ func testNonStdSweepInner(ht *lntest.HarnessTest, address string) {
// Fetch the vsize of the transaction so we can determine if the
// transaction pays >= 1 sat/vbyte.
rawTx := ht.Miner.GetRawTransactionVerbose(txid)
rawTx := ht.Miner().GetRawTransactionVerbose(txid)
// Require fee >= vbytes.
require.True(ht, fee >= int(rawTx.Vsize))

View File

@ -32,7 +32,7 @@ func testOpenChannelAfterReorg(ht *lntest.HarnessTest) {
// Create a temp miner.
tempMiner := ht.SpawnTempMiner()
miner := ht.Miner
miner := ht.Miner()
alice, bob := ht.Alice, ht.Bob
// Create a new channel that requires 1 confs before it's considered

View File

@ -296,7 +296,7 @@ func wsTestCaseSubscription(ht *lntest.HarnessTest) {
}()
// Mine a block and make sure we get a message for it.
blockHashes := ht.Miner.GenerateBlocks(1)
blockHashes := ht.Miner().GenerateBlocks(1)
select {
case msg := <-msgChan:
require.Equal(
@ -388,7 +388,7 @@ func wsTestCaseSubscriptionMacaroon(ht *lntest.HarnessTest) {
}()
// Mine a block and make sure we get a message for it.
blockHashes := ht.Miner.GenerateBlocks(1)
blockHashes := ht.Miner().GenerateBlocks(1)
select {
case msg := <-msgChan:
require.Equal(

View File

@ -579,7 +579,7 @@ func revokedCloseRetributionRemoteHodlCase(ht *lntest.HarnessTest,
// NOTE: We don't use `ht.GetRawTransaction`
// which asserts a txid must be found as the HTLC
// spending txes might be aggregated.
tx, err := ht.Miner.Client.GetRawTransaction(txid)
tx, err := ht.Miner().Client.GetRawTransaction(txid)
if err != nil {
return nil, err
}

View File

@ -172,7 +172,7 @@ func testSweepCPFPAnchorOutgoingTimeout(ht *lntest.HarnessTest) {
// Remember the force close height so we can calculate the deadline
// height.
_, forceCloseHeight := ht.Miner.GetBestBlock()
_, forceCloseHeight := ht.GetBestBlock()
// Bob should have two pending sweeps,
// - anchor sweeping from his local commitment.
@ -304,7 +304,7 @@ func testSweepCPFPAnchorOutgoingTimeout(ht *lntest.HarnessTest) {
//
// Once out of the above loop, we expect to be 2 blocks before the CPFP
// deadline.
_, currentHeight := ht.Miner.GetBestBlock()
_, currentHeight := ht.GetBestBlock()
require.Equal(ht, int(anchorDeadline-2), int(currentHeight))
// Mine one more block, we'd use up all the CPFP budget.
@ -512,7 +512,7 @@ func testSweepCPFPAnchorIncomingTimeout(ht *lntest.HarnessTest) {
forceCloseHeight := htlc.ExpirationHeight - goToChainDelta
// Mine till the goToChainHeight is reached.
_, currentHeight := ht.Miner.GetBestBlock()
_, currentHeight := ht.GetBestBlock()
numBlocks := forceCloseHeight - uint32(currentHeight)
ht.MineEmptyBlocks(int(numBlocks))
@ -641,7 +641,7 @@ func testSweepCPFPAnchorIncomingTimeout(ht *lntest.HarnessTest) {
//
// Once out of the above loop, we expect to be 2 blocks before the CPFP
// deadline.
_, currentHeight = ht.Miner.GetBestBlock()
_, currentHeight = ht.GetBestBlock()
require.Equal(ht, int(anchorDeadline-2), int(currentHeight))
// Mine one more block, we'd use up all the CPFP budget.
@ -1380,7 +1380,7 @@ func testSweepCommitOutputAndAnchor(ht *lntest.HarnessTest) {
//
// TODO(yy): assert they are equal once blocks are synced via
// `blockbeat`.
_, currentHeight := ht.Miner.GetBestBlock()
_, currentHeight := ht.GetBestBlock()
actualDeadline := int32(pendingSweepBob.DeadlineHeight) - currentHeight
if actualDeadline != int32(deadlineB) {
ht.Logf("!!! Found unsynced block between sweeper and "+
@ -1438,7 +1438,7 @@ func testSweepCommitOutputAndAnchor(ht *lntest.HarnessTest) {
//
// TODO(yy): assert they are equal once blocks are synced via
// `blockbeat`.
_, currentHeight = ht.Miner.GetBestBlock()
_, currentHeight = ht.GetBestBlock()
actualDeadline = int32(aliceCommit.DeadlineHeight) - currentHeight
if actualDeadline != int32(deadlineA) {
ht.Logf("!!! Found unsynced block between Alice's sweeper and "+

View File

@ -96,7 +96,7 @@ func testTaprootSendCoinsKeySpendBip86(ht *lntest.HarnessTest,
// Assert this is a segwit v1 address that starts with bcrt1p.
require.Contains(
ht, p2trResp.Address, ht.Miner.ActiveNet.Bech32HRPSegwit+"1p",
ht, p2trResp.Address, ht.Miner().ActiveNet.Bech32HRPSegwit+"1p",
)
// Send the coins from Alice's wallet to her own, but to the new p2tr

View File

@ -77,9 +77,9 @@ type HarnessTest struct {
// Embed the standbyNodes so we can easily access them via `ht.Alice`.
standbyNodes
// Miner is a reference to a running full node that can be used to
// miner is a reference to a running full node that can be used to
// create new blocks on the network.
Miner *miner.HarnessMiner
miner *miner.HarnessMiner
// manager handles the start and stop of a given node.
manager *nodeManager
@ -188,7 +188,7 @@ func (h *HarnessTest) Start(chain node.BackendConfig,
h.manager.feeServiceURL = h.feeService.URL()
// Assemble the miner.
h.Miner = miner
h.miner = miner
}
// ChainBackendName returns the chain backend name used in the test.
@ -254,7 +254,7 @@ func (h *HarnessTest) createAndSendOutput(target *node.HarnessNode,
PkScript: addrScript,
Value: int64(amt),
}
h.Miner.SendOutput(output, defaultMinerFeeRate)
h.miner.SendOutput(output, defaultMinerFeeRate)
}
// SetupRemoteSigningStandbyNodes starts the initial seeder nodes within the
@ -371,7 +371,7 @@ func (h *HarnessTest) Stop() {
h.stopChainBackend()
// Stop the miner.
h.Miner.Stop()
h.miner.Stop()
}
// RunTestCase executes a harness test case. Any errors or panics will be
@ -414,7 +414,7 @@ func (h *HarnessTest) Subtest(t *testing.T) *HarnessTest {
st := &HarnessTest{
T: t,
manager: h.manager,
Miner: h.Miner,
miner: h.miner,
standbyNodes: h.standbyNodes,
feeService: h.feeService,
lndErrorChan: make(chan error, lndErrorChanSize),
@ -424,7 +424,7 @@ func (h *HarnessTest) Subtest(t *testing.T) *HarnessTest {
st.runCtx, st.cancel = context.WithCancel(h.runCtx)
// Inherit the subtest for the miner.
st.Miner.T = st.T
st.miner.T = st.T
// Reset the standby nodes.
st.resetStandbyNodes(t)
@ -467,7 +467,7 @@ func (h *HarnessTest) Subtest(t *testing.T) *HarnessTest {
st.shutdownNonStandbyNodes()
// We require the mempool to be cleaned from the test.
require.Empty(st, st.Miner.GetRawMempool(), "mempool not "+
require.Empty(st, st.miner.GetRawMempool(), "mempool not "+
"cleaned, please mine blocks to clean them all.")
// Finally, cancel the run context. We have to do it here
@ -1298,7 +1298,7 @@ func (h *HarnessTest) CloseChannelAssertPending(hn *node.HarnessNode,
pendingClose.ClosePending.Txid)
// Assert the closing tx is in the mempool.
h.Miner.AssertTxInMempool(closeTxid)
h.miner.AssertTxInMempool(closeTxid)
return stream, closeTxid
}
@ -1396,7 +1396,7 @@ func (h *HarnessTest) fundCoins(amt btcutil.Amount, target *node.HarnessNode,
PkScript: addrScript,
Value: int64(amt),
}
h.Miner.SendOutput(output, defaultMinerFeeRate)
h.miner.SendOutput(output, defaultMinerFeeRate)
// Encode the pkScript in hex as this the format that it will be
// returned via rpc.
@ -1903,7 +1903,7 @@ func (h *HarnessTest) CalculateTxFee(tx *wire.MsgTx) btcutil.Amount {
var balance btcutil.Amount
for _, in := range tx.TxIn {
parentHash := in.PreviousOutPoint.Hash
rawTx := h.Miner.GetRawTransaction(&parentHash)
rawTx := h.miner.GetRawTransaction(&parentHash)
parent := rawTx.MsgTx()
value := parent.TxOut[in.PreviousOutPoint.Index].Value
@ -2121,12 +2121,12 @@ func (h *HarnessTest) ReceiveChannelEvent(
func (h *HarnessTest) GetOutputIndex(txid *chainhash.Hash, addr string) int {
// We'll then extract the raw transaction from the mempool in order to
// determine the index of the p2tr output.
tx := h.Miner.GetRawTransaction(txid)
tx := h.miner.GetRawTransaction(txid)
p2trOutputIndex := -1
for i, txOut := range tx.MsgTx().TxOut {
_, addrs, _, err := txscript.ExtractPkScriptAddrs(
txOut.PkScript, h.Miner.ActiveNet,
txOut.PkScript, h.miner.ActiveNet,
)
require.NoError(h, err)

View File

@ -13,6 +13,16 @@ import (
"github.com/stretchr/testify/require"
)
// Miner returns the miner instance.
//
// NOTE: Caller should keep in mind that when using this private instance,
// certain states won't be managed by the HarnessTest anymore. For instance,
// when mining directly, the nodes managed by the HarnessTest can be out of
// sync, and the `HarnessTest.CurrentHeight()` won't be accurate.
func (h *HarnessTest) Miner() *miner.HarnessMiner {
return h.miner
}
// MineBlocks mines blocks and asserts all active nodes have synced to the
// chain. It assumes no txns are expected in the blocks.
//
@ -23,7 +33,7 @@ func (h *HarnessTest) MineBlocks(num int) {
// Mine num of blocks.
for i := 0; i < num; i++ {
block := h.Miner.MineBlocks(1)[0]
block := h.miner.MineBlocks(1)[0]
// Check the block doesn't have any txns except the coinbase.
if len(block.Transactions) <= 1 {
@ -63,7 +73,7 @@ func (h *HarnessTest) MineBlocks(num int) {
func (h *HarnessTest) MineEmptyBlocks(num int) []*wire.MsgBlock {
require.Less(h, num, maxBlocksAllowed, "too many blocks to mine")
blocks := h.Miner.MineEmptyBlocks(num)
blocks := h.miner.MineEmptyBlocks(num)
// Finally, make sure all the active nodes are synced.
h.AssertActiveNodesSynced()
@ -85,16 +95,16 @@ func (h *HarnessTest) MineBlocksAndAssertNumTxes(num uint32,
txids := h.AssertNumTxsInMempool(numTxs)
// Mine blocks.
blocks := h.Miner.MineBlocksSlow(num)
blocks := h.miner.MineBlocksSlow(num)
// Assert that all the transactions were included in the first block.
for _, txid := range txids {
h.Miner.AssertTxInBlock(blocks[0], txid)
h.miner.AssertTxInBlock(blocks[0], txid)
}
// Make sure the mempool has been updated.
for _, txid := range txids {
h.Miner.AssertTxNotInMempool(*txid)
h.miner.AssertTxNotInMempool(*txid)
}
// Finally, make sure all the active nodes are synced.
@ -126,7 +136,7 @@ func (h *HarnessTest) cleanMempool() {
var bestBlock *wire.MsgBlock
err := wait.NoError(func() error {
// If mempool is empty, exit.
mem := h.Miner.GetRawMempool()
mem := h.miner.GetRawMempool()
if len(mem) == 0 {
_, height := h.GetBestBlock()
h.Logf("Mined %d blocks when cleanup the mempool",
@ -136,7 +146,7 @@ func (h *HarnessTest) cleanMempool() {
}
// Otherwise mine a block.
blocks := h.Miner.MineBlocksSlow(1)
blocks := h.miner.MineBlocksSlow(1)
bestBlock = blocks[len(blocks)-1]
// Make sure all the active nodes are synced.
@ -183,7 +193,7 @@ func (h *HarnessTest) mineTillForceCloseResolved(hn *node.HarnessNode) {
// AssertTxInMempool asserts a given transaction can be found in the mempool.
func (h *HarnessTest) AssertTxInMempool(txid *chainhash.Hash) *wire.MsgTx {
return h.Miner.AssertTxInMempool(txid)
return h.miner.AssertTxInMempool(txid)
}
// AssertTxNotInMempool asserts a given transaction cannot be found in the
@ -193,76 +203,76 @@ func (h *HarnessTest) AssertTxInMempool(txid *chainhash.Hash) *wire.MsgTx {
// entered the mempool before. Otherwise it might give false positive and the
// tx may enter the mempool after the check.
func (h *HarnessTest) AssertTxNotInMempool(txid chainhash.Hash) *wire.MsgTx {
return h.Miner.AssertTxNotInMempool(txid)
return h.miner.AssertTxNotInMempool(txid)
}
// 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)
return h.miner.AssertNumTxsInMempool(n)
}
// AssertOutpointInMempool asserts a given outpoint can be found in the mempool.
func (h *HarnessTest) AssertOutpointInMempool(op wire.OutPoint) *wire.MsgTx {
return h.Miner.AssertOutpointInMempool(op)
return h.miner.AssertOutpointInMempool(op)
}
// 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)
h.miner.AssertTxInBlock(block, txid)
}
// GetNumTxsFromMempool polls until finding the desired number of transactions
// in the miner's mempool and returns the full transactions to the caller.
func (h *HarnessTest) GetNumTxsFromMempool(n int) []*wire.MsgTx {
return h.Miner.GetNumTxsFromMempool(n)
return h.miner.GetNumTxsFromMempool(n)
}
// GetBestBlock makes a RPC request to miner and asserts.
func (h *HarnessTest) GetBestBlock() (*chainhash.Hash, int32) {
return h.Miner.GetBestBlock()
return h.miner.GetBestBlock()
}
// MineBlockWithTx mines a single block to include the specifies tx only.
func (h *HarnessTest) MineBlockWithTx(tx *wire.MsgTx) *wire.MsgBlock {
return h.Miner.MineBlockWithTx(tx)
return h.miner.MineBlockWithTx(tx)
}
// ConnectToMiner connects the miner to a temp miner.
func (h *HarnessTest) ConnectToMiner(tempMiner *miner.HarnessMiner) {
h.Miner.ConnectMiner(tempMiner)
h.miner.ConnectMiner(tempMiner)
}
// DisconnectFromMiner disconnects the miner from the temp miner.
func (h *HarnessTest) DisconnectFromMiner(tempMiner *miner.HarnessMiner) {
h.Miner.DisconnectMiner(tempMiner)
h.miner.DisconnectMiner(tempMiner)
}
// GetRawMempool makes a RPC call to the miner's GetRawMempool and
// asserts.
func (h *HarnessTest) GetRawMempool() []*chainhash.Hash {
return h.Miner.GetRawMempool()
return h.miner.GetRawMempool()
}
// GetRawTransaction makes a RPC call to the miner's GetRawTransaction and
// asserts.
func (h *HarnessTest) GetRawTransaction(txid *chainhash.Hash) *btcutil.Tx {
return h.Miner.GetRawTransaction(txid)
return h.miner.GetRawTransaction(txid)
}
// NewMinerAddress creates a new address for the miner and asserts.
func (h *HarnessTest) NewMinerAddress() btcutil.Address {
return h.Miner.NewMinerAddress()
return h.miner.NewMinerAddress()
}
// SpawnTempMiner creates a temp miner and syncs it with the current miner.
// Once miners are synced, the temp miner is disconnected from the original
// miner and returned.
func (h *HarnessTest) SpawnTempMiner() *miner.HarnessMiner {
return h.Miner.SpawnTempMiner()
return h.miner.SpawnTempMiner()
}
// CreateTransaction uses the miner to create a transaction using the given
@ -270,7 +280,7 @@ func (h *HarnessTest) SpawnTempMiner() *miner.HarnessMiner {
func (h *HarnessTest) CreateTransaction(outputs []*wire.TxOut,
feeRate btcutil.Amount) *wire.MsgTx {
return h.Miner.CreateTransaction(outputs, feeRate)
return h.miner.CreateTransaction(outputs, feeRate)
}
// SendOutputsWithoutChange uses the miner to send the given outputs using the
@ -278,7 +288,7 @@ func (h *HarnessTest) CreateTransaction(outputs []*wire.TxOut,
func (h *HarnessTest) SendOutputsWithoutChange(outputs []*wire.TxOut,
feeRate btcutil.Amount) *chainhash.Hash {
return h.Miner.SendOutputsWithoutChange(outputs, feeRate)
return h.miner.SendOutputsWithoutChange(outputs, feeRate)
}
// AssertMinerBlockHeightDelta ensures that tempMiner is 'delta' blocks ahead
@ -286,7 +296,7 @@ func (h *HarnessTest) SendOutputsWithoutChange(outputs []*wire.TxOut,
func (h *HarnessTest) AssertMinerBlockHeightDelta(
tempMiner *miner.HarnessMiner, delta int32) {
h.Miner.AssertMinerBlockHeightDelta(tempMiner, delta)
h.miner.AssertMinerBlockHeightDelta(tempMiner, delta)
}
// SendRawTransaction submits the encoded transaction to the server which will
@ -294,7 +304,7 @@ func (h *HarnessTest) AssertMinerBlockHeightDelta(
func (h *HarnessTest) SendRawTransaction(tx *wire.MsgTx,
allowHighFees bool) (chainhash.Hash, error) {
txid, err := h.Miner.Client.SendRawTransaction(tx, allowHighFees)
txid, err := h.miner.Client.SendRawTransaction(tx, allowHighFees)
require.NoError(h, err)
return *txid, nil