diff --git a/lntest/btcd.go b/lntest/btcd.go index 2d735fa2b..6b607978e 100644 --- a/lntest/btcd.go +++ b/lntest/btcd.go @@ -14,6 +14,7 @@ import ( "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/integration/rpctest" "github.com/btcsuite/btcd/rpcclient" + "github.com/lightningnetwork/lnd/lntest/miner" "github.com/lightningnetwork/lnd/lntest/node" ) @@ -53,12 +54,14 @@ func (b BtcdBackendConfig) GenArgs() []string { // ConnectMiner is called to establish a connection to the test miner. func (b BtcdBackendConfig) ConnectMiner() error { - return b.harness.Client.Node(btcjson.NConnect, b.minerAddr, &temp) + return b.harness.Client.Node(btcjson.NConnect, b.minerAddr, &miner.Temp) } // DisconnectMiner is called to disconnect the miner. func (b BtcdBackendConfig) DisconnectMiner() error { - return b.harness.Client.Node(btcjson.NDisconnect, b.minerAddr, &temp) + return b.harness.Client.Node( + btcjson.NDisconnect, b.minerAddr, &miner.Temp, + ) } // Credentials returns the rpc username, password and host for the backend. diff --git a/lntest/harness.go b/lntest/harness.go index 92549427f..20c1c1462 100644 --- a/lntest/harness.go +++ b/lntest/harness.go @@ -18,6 +18,7 @@ import ( "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc" "github.com/lightningnetwork/lnd/lnrpc/walletrpc" + "github.com/lightningnetwork/lnd/lntest/miner" "github.com/lightningnetwork/lnd/lntest/node" "github.com/lightningnetwork/lnd/lntest/rpc" "github.com/lightningnetwork/lnd/lntest/wait" @@ -78,7 +79,7 @@ type HarnessTest struct { // Miner is a reference to a running full node that can be used to // create new blocks on the network. - Miner *HarnessMiner + Miner *miner.HarnessMiner // manager handles the start and stop of a given node. manager *nodeManager @@ -158,7 +159,9 @@ func NewHarnessTest(t *testing.T, lndBinary string, feeService WebFeeService, // Start will assemble the chain backend and the miner for the HarnessTest. It // also starts the fee service and watches lnd process error. -func (h *HarnessTest) Start(chain node.BackendConfig, miner *HarnessMiner) { +func (h *HarnessTest) Start(chain node.BackendConfig, + miner *miner.HarnessMiner) { + // Spawn a new goroutine to watch for any fatal errors that any of the // running lnd processes encounter. If an error occurs, then the test // case should naturally as a result and we log the server error here @@ -1599,7 +1602,7 @@ func (h *HarnessTest) OpenChannelPsbt(srcNode, destNode *node.HarnessNode, // Make sure the channel funding address has the correct type for the // given commitment type. fundingAddr, err := btcutil.DecodeAddress( - upd.PsbtFund.FundingAddress, harnessNetParams, + upd.PsbtFund.FundingAddress, miner.HarnessNetParams, ) require.NoError(h, err) diff --git a/lntest/harness_assertion.go b/lntest/harness_assertion.go index 9ee9bc4e4..f3d8244ed 100644 --- a/lntest/harness_assertion.go +++ b/lntest/harness_assertion.go @@ -23,6 +23,7 @@ import ( "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc" "github.com/lightningnetwork/lnd/lnrpc/walletrpc" + "github.com/lightningnetwork/lnd/lntest/miner" "github.com/lightningnetwork/lnd/lntest/node" "github.com/lightningnetwork/lnd/lntest/rpc" "github.com/lightningnetwork/lnd/lntest/wait" @@ -937,7 +938,7 @@ func (h *HarnessTest) RandomPreimage() lntypes.Preimage { // DecodeAddress decodes a given address and asserts there's no error. func (h *HarnessTest) DecodeAddress(addr string) btcutil.Address { - resp, err := btcutil.DecodeAddress(addr, harnessNetParams) + resp, err := btcutil.DecodeAddress(addr, miner.HarnessNetParams) require.NoError(h, err, "DecodeAddress failed") return resp @@ -2015,6 +2016,7 @@ func (h *HarnessTest) CreateBurnAddr(addrType lnrpc.AddressType) ([]byte, require.NoError(h, err) randomKeyBytes := randomPrivKey.PubKey().SerializeCompressed() + harnessNetParams := miner.HarnessNetParams var addr btcutil.Address switch addrType { diff --git a/lntest/harness_node_manager.go b/lntest/harness_node_manager.go index 3b0c412d9..90a74f75e 100644 --- a/lntest/harness_node_manager.go +++ b/lntest/harness_node_manager.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/lightningnetwork/lnd/lnrpc" + "github.com/lightningnetwork/lnd/lntest/miner" "github.com/lightningnetwork/lnd/lntest/node" "github.com/lightningnetwork/lnd/lntest/wait" ) @@ -88,7 +89,7 @@ func (nm *nodeManager) newNode(t *testing.T, name string, extraArgs []string, NativeSQL: nm.nativeSQL, NodeID: nm.nextNodeID(), LndBinary: nm.lndBinary, - NetParams: harnessNetParams, + NetParams: miner.HarnessNetParams, SkipUnlock: noAuth, } diff --git a/lntest/harness_setup.go b/lntest/harness_setup.go index 8aed3b880..166880bae 100644 --- a/lntest/harness_setup.go +++ b/lntest/harness_setup.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/btcsuite/btcd/integration/rpctest" + "github.com/lightningnetwork/lnd/lntest/miner" "github.com/lightningnetwork/lnd/lntest/node" "github.com/lightningnetwork/lnd/lntest/wait" "github.com/stretchr/testify/require" @@ -65,27 +66,27 @@ func SetupHarness(t *testing.T, binaryPath, dbBackendName string, // transactions on simnet to reject them. Transactions on the lightning network // should always be standard to get better guarantees of getting included in to // blocks. -func prepareMiner(ctxt context.Context, t *testing.T) *HarnessMiner { - miner := NewMiner(ctxt, t) +func prepareMiner(ctxt context.Context, t *testing.T) *miner.HarnessMiner { + m := miner.NewMiner(ctxt, t) // Before we start anything, we want to overwrite some of the // connection settings to make the tests more robust. We might need to // restart the miner 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. - miner.MaxConnRetries = rpctest.DefaultMaxConnectionRetries * 2 - miner.ConnectionRetryTimeout = rpctest.DefaultConnectionRetryTimeout * 2 + m.MaxConnRetries = rpctest.DefaultMaxConnectionRetries * 2 + m.ConnectionRetryTimeout = rpctest.DefaultConnectionRetryTimeout * 2 // Set up miner and connect chain backend to it. - require.NoError(t, miner.SetUp(true, 50)) - require.NoError(t, miner.Client.NotifyNewTransactions(false)) + require.NoError(t, m.SetUp(true, 50)) + require.NoError(t, m.Client.NotifyNewTransactions(false)) // Next mine enough blocks in order for segwit and the CSV package // soft-fork to activate on SimNet. - numBlocks := harnessNetParams.MinerConfirmationWindow * 2 - miner.GenerateBlocks(numBlocks) + numBlocks := miner.HarnessNetParams.MinerConfirmationWindow * 2 + m.GenerateBlocks(numBlocks) - return miner + return m } // prepareChainBackend creates a new chain backend. @@ -93,7 +94,7 @@ func prepareChainBackend(t *testing.T, minerAddr string) (node.BackendConfig, func()) { chainBackend, cleanUp, err := NewBackend( - minerAddr, harnessNetParams, + minerAddr, miner.HarnessNetParams, ) require.NoError(t, err, "new backend") diff --git a/lntest/harness_miner.go b/lntest/miner/miner.go similarity index 98% rename from lntest/harness_miner.go rename to lntest/miner/miner.go index f6348c078..0a81a07af 100644 --- a/lntest/harness_miner.go +++ b/lntest/miner/miner.go @@ -1,4 +1,4 @@ -package lntest +package miner import ( "bytes" @@ -34,13 +34,13 @@ const ( ) var ( - harnessNetParams = &chaincfg.RegressionNetParams + HarnessNetParams = &chaincfg.RegressionNetParams // temp is used to signal we want to establish a temporary connection // using the btcd Node API. // // NOTE: Cannot be const, since the node API expects a reference. - temp = "temp" + Temp = "temp" ) type HarnessMiner struct { @@ -99,7 +99,7 @@ func newMiner(ctxb context.Context, t *testing.T, minerDirName, "--nostalldetect", } - miner, err := rpctest.New(harnessNetParams, handler, args, btcdBinary) + miner, err := rpctest.New(HarnessNetParams, handler, args, btcdBinary) require.NoError(t, err, "unable to create mining node") ctxt, cancel := context.WithCancel(ctxb) @@ -119,7 +119,7 @@ func newMiner(ctxb context.Context, t *testing.T, minerDirName, func (h *HarnessMiner) saveLogs() { // After shutting down the miner, we'll make a copy of the log files // before deleting the temporary log dir. - path := fmt.Sprintf("%s/%s", h.logPath, harnessNetParams.Name) + path := fmt.Sprintf("%s/%s", h.logPath, HarnessNetParams.Name) files, err := os.ReadDir(path) require.NoError(h, err, "unable to read log directory") @@ -130,7 +130,7 @@ func (h *HarnessMiner) saveLogs() { copyPath := fmt.Sprintf("%s/../%s", h.logPath, newFilename) logFile := fmt.Sprintf("%s/%s", path, file.Name()) - err := CopyFile(filepath.Clean(copyPath), logFile) + err := node.CopyFile(filepath.Clean(copyPath), logFile) require.NoError(h, err, "unable to copy file") } @@ -533,7 +533,7 @@ func (h *HarnessMiner) SpawnTempMiner() *HarnessMiner { require.NoError(tempMiner.SetUp(false, 0), "unable to setup miner") // Connect the temp miner to the original miner. - err := h.Client.Node(btcjson.NConnect, tempMiner.P2PAddress(), &temp) + err := h.Client.Node(btcjson.NConnect, tempMiner.P2PAddress(), &Temp) require.NoError(err, "unable to connect node") // Sync the blocks. @@ -546,7 +546,7 @@ func (h *HarnessMiner) SpawnTempMiner() *HarnessMiner { // Once synced, we now disconnect the temp miner so it'll be // independent from the original miner. - err = h.Client.Node(btcjson.NDisconnect, tempMiner.P2PAddress(), &temp) + err = h.Client.Node(btcjson.NDisconnect, tempMiner.P2PAddress(), &Temp) require.NoError(err, "unable to disconnect miners") return tempMiner @@ -557,7 +557,7 @@ func (h *HarnessMiner) ConnectMiner(tempMiner *HarnessMiner) { require := require.New(h.T) // Connect the current miner to the temporary miner. - err := h.Client.Node(btcjson.NConnect, tempMiner.P2PAddress(), &temp) + err := h.Client.Node(btcjson.NConnect, tempMiner.P2PAddress(), &Temp) require.NoError(err, "unable to connect temp miner") nodes := []*rpctest.Harness{tempMiner.Harness, h.Harness} @@ -567,7 +567,7 @@ func (h *HarnessMiner) ConnectMiner(tempMiner *HarnessMiner) { // DisconnectMiner disconnects the miner from the temp miner. func (h *HarnessMiner) DisconnectMiner(tempMiner *HarnessMiner) { - err := h.Client.Node(btcjson.NDisconnect, tempMiner.P2PAddress(), &temp) + err := h.Client.Node(btcjson.NDisconnect, tempMiner.P2PAddress(), &Temp) require.NoError(h.T, err, "unable to disconnect temp miner") } @@ -597,6 +597,6 @@ func (h *HarnessMiner) AssertMinerBlockHeightDelta(tempMiner *HarnessMiner, } return nil - }, DefaultTimeout) + }, wait.DefaultTimeout) require.NoError(h.T, err, "failed to assert block height delta") }