From c170a9830bbf854380f9e58421fdfbd478b76927 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Fri, 15 Mar 2024 12:54:35 +0100 Subject: [PATCH] multi: use chainntnfs.NewMiner for miners in unit tests With the chainntnfs.NewMiner now being optimized for not creating nodes with colliding ports, we use it in all unit tests that spin up temporary miners. --- chainntnfs/test/test_interface.go | 27 ++++++--------------------- lnwallet/btcwallet/signer_test.go | 18 ++++++++---------- lnwallet/test/test_interface.go | 17 +++++------------ routing/chainview/interface_test.go | 29 +++++++---------------------- 4 files changed, 26 insertions(+), 65 deletions(-) diff --git a/chainntnfs/test/test_interface.go b/chainntnfs/test/test_interface.go index a091c286e..a11f751c6 100644 --- a/chainntnfs/test/test_interface.go +++ b/chainntnfs/test/test_interface.go @@ -1043,14 +1043,9 @@ func testReorgConf(miner *rpctest.Harness, notifier chainntnfs.TestChainNotifier, scriptDispatch bool, t *testing.T) { // Set up a new miner that we can use to cause a reorg. - miner2, err := rpctest.New( - chainntnfs.NetParams, nil, []string{"--txindex"}, "", + miner2 := chainntnfs.NewMiner( + t, chainntnfs.NetParams, []string{"--txindex"}, false, 0, ) - require.NoError(t, err, "unable to create mining node") - if err := miner2.SetUp(false, 0); err != nil { - t.Fatalf("unable to set up mining node: %v", err) - } - defer miner2.TearDown() // We start by connecting the new miner to our original miner, // such that it will sync to our original chain. @@ -1204,14 +1199,9 @@ func testReorgSpend(miner *rpctest.Harness, require.NoError(t, err, "unable to register for spend") // Set up a new miner that we can use to cause a reorg. - miner2, err := rpctest.New( - chainntnfs.NetParams, nil, []string{"--txindex"}, "", + miner2 := chainntnfs.NewMiner( + t, chainntnfs.NetParams, []string{"--txindex"}, false, 0, ) - require.NoError(t, err, "unable to create mining node") - if err := miner2.SetUp(false, 0); err != nil { - t.Fatalf("unable to set up mining node: %v", err) - } - defer miner2.TearDown() // We start by connecting the new miner to our original miner, in order // to have a consistent view of the chain from both miners. They should @@ -1527,14 +1517,9 @@ func testCatchUpOnMissedBlocksWithReorg(miner1 *rpctest.Harness, var wg sync.WaitGroup // Set up a new miner that we can use to cause a reorg. - miner2, err := rpctest.New( - chainntnfs.NetParams, nil, []string{"--txindex"}, "", + miner2 := chainntnfs.NewMiner( + t, chainntnfs.NetParams, []string{"--txindex"}, false, 0, ) - require.NoError(t, err, "unable to create mining node") - if err := miner2.SetUp(false, 0); err != nil { - t.Fatalf("unable to set up mining node: %v", err) - } - defer miner2.TearDown() // We start by connecting the new miner to our original miner, // such that it will sync to our original chain. diff --git a/lnwallet/btcwallet/signer_test.go b/lnwallet/btcwallet/signer_test.go index d51714ad0..7e5a3a20e 100644 --- a/lnwallet/btcwallet/signer_test.go +++ b/lnwallet/btcwallet/signer_test.go @@ -18,6 +18,7 @@ import ( "github.com/btcsuite/btcwallet/chain" "github.com/btcsuite/btcwallet/waddrmgr" "github.com/lightningnetwork/lnd/blockcache" + "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/lnwallet" "github.com/stretchr/testify/require" @@ -294,8 +295,7 @@ func TestScriptImport(t *testing.T) { func newTestWallet(t *testing.T, netParams *chaincfg.Params, seedBytes []byte) (*BtcWallet, *rpctest.Harness) { - chainBackend, miner, backendCleanup := getChainBackend(t, netParams) - t.Cleanup(backendCleanup) + chainBackend, miner := getChainBackend(t, netParams) loaderOpt := LoaderWithLocalWalletDB(t.TempDir(), false, time.Minute) config := Config{ @@ -324,16 +324,16 @@ func newTestWallet(t *testing.T, netParams *chaincfg.Params, // getChainBackend returns a simple btcd based chain backend to back the wallet. func getChainBackend(t *testing.T, netParams *chaincfg.Params) (chain.Interface, - *rpctest.Harness, func()) { + *rpctest.Harness) { - miningNode, err := rpctest.New(netParams, nil, nil, "") - require.NoError(t, err) - require.NoError(t, miningNode.SetUp(true, 25)) + miningNode := chainntnfs.NewMiner( + t, netParams, []string{"--txindex"}, true, 25, + ) // Next, mine enough blocks in order for SegWit and the CSV package // soft-fork to activate on RegNet. numBlocks := netParams.MinerConfirmationWindow * 2 - _, err = miningNode.Client.Generate(numBlocks) + _, err := miningNode.Client.Generate(numBlocks) require.NoError(t, err) rpcConfig := miningNode.RPCConfig() @@ -343,9 +343,7 @@ func getChainBackend(t *testing.T, netParams *chaincfg.Params) (chain.Interface, ) require.NoError(t, err) - return chainClient, miningNode, func() { - _ = miningNode.TearDown() - } + return chainClient, miningNode } // hardenedKey returns a key of a hardened derivation key path. diff --git a/lnwallet/test/test_interface.go b/lnwallet/test/test_interface.go index caa222cf4..42f63be51 100644 --- a/lnwallet/test/test_interface.go +++ b/lnwallet/test/test_interface.go @@ -2120,11 +2120,9 @@ func testReorgWalletBalance(r *rpctest.Harness, w *lnwallet.LightningWallet, // Now we cause a reorganization as follows. // Step 1: create a new miner and start it. - r2, err := rpctest.New(r.ActiveNet, nil, []string{"--txindex"}, "") - require.NoError(t, err, "unable to create mining node") - err = r2.SetUp(false, 0) - require.NoError(t, err, "unable to set up mining node") - defer r2.TearDown() + r2 := chainntnfs.NewMiner( + t, r.ActiveNet, []string{"--txindex"}, false, 0, + ) newBalance, err := w.ConfirmedBalance(1, lnwallet.DefaultAccountName) require.NoError(t, err, "unable to query for balance") if origBalance != newBalance { @@ -3103,14 +3101,9 @@ func TestLightningWallet(t *testing.T, targetBackEnd string) { // dedicated miner to generate blocks, cause re-orgs, etc. We'll set // up this node with a chain length of 125, so we have plenty of BTC // to play around with. - miningNode, err := rpctest.New( - netParams, nil, []string{"--txindex"}, "", + miningNode := chainntnfs.NewMiner( + t, netParams, []string{"--txindex"}, true, 25, ) - require.NoError(t, err, "unable to create mining node") - defer miningNode.TearDown() - if err := miningNode.SetUp(true, 25); err != nil { - t.Fatalf("unable to set up mining node: %v", err) - } // Next mine enough blocks in order for segwit and the CSV package // soft-fork to activate on RegNet. diff --git a/routing/chainview/interface_test.go b/routing/chainview/interface_test.go index 010f33807..c3bf9ac06 100644 --- a/routing/chainview/interface_test.go +++ b/routing/chainview/interface_test.go @@ -25,6 +25,7 @@ import ( _ "github.com/btcsuite/btcwallet/walletdb/bdb" // Required to register the boltdb walletdb implementation. "github.com/lightninglabs/neutrino" "github.com/lightningnetwork/lnd/blockcache" + "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/lntest/wait" @@ -481,22 +482,9 @@ func testFilterBlockDisconnected(node *rpctest.Harness, // Create a node that has a shorter chain than the main chain, so we // can trigger a reorg. - reorgNode, err := rpctest.New(netParams, nil, []string{"--txindex"}, "") - require.NoError(t, err, "unable to create mining node") - defer reorgNode.TearDown() - - // We want to overwrite some of the connection settings to make the - // tests more robust. We might need to restart the backend while there - // are already blocks present, which will take a bit longer than the - // 1 second the default settings amount to. Doubling both values will - // give us retries up to 4 seconds. - reorgNode.MaxConnRetries = rpctest.DefaultMaxConnectionRetries * 2 - reorgNode.ConnectionRetryTimeout = rpctest.DefaultConnectionRetryTimeout * 2 - - // This node's chain will be 105 blocks. - if err := reorgNode.SetUp(true, 5); err != nil { - t.Fatalf("unable to set up mining node: %v", err) - } + reorgNode := chainntnfs.NewMiner( + t, netParams, []string{"--txindex"}, true, 5, + ) _, bestHeight, err := reorgNode.Client.GetBestBlock() require.NoError(t, err, "error getting best block") @@ -996,12 +984,9 @@ func TestFilteredChainView(t *testing.T) { // dedicated miner to generate blocks, cause re-orgs, etc. We'll set up // this node with a chain length of 125, so we have plenty of BTC to // play around with. - miner, err := rpctest.New(netParams, nil, []string{"--txindex"}, "") - require.NoError(t, err, "unable to create mining node") - defer miner.TearDown() - if err := miner.SetUp(true, 25); err != nil { - t.Fatalf("unable to set up mining node: %v", err) - } + miner := chainntnfs.NewMiner( + t, netParams, []string{"--txindex"}, true, 25, + ) rpcConfig := miner.RPCConfig() p2pAddr := miner.P2PAddress()