diff --git a/lntest/harness_miner.go b/lntest/harness_miner.go index 238fd3365..b9f54619a 100644 --- a/lntest/harness_miner.go +++ b/lntest/harness_miner.go @@ -42,7 +42,7 @@ type HarnessMiner struct { // NewMiner creates a new miner using btcd backend with the default log file // dir and name. -func NewMinerTemp() (*HarnessMiner, error) { // TODO(yy): rename +func NewMiner() (*HarnessMiner, error) { return newMiner(minerLogDir, minerLogFilename) } diff --git a/lntest/harness_net.go b/lntest/harness_net.go index 76e571274..118cea1b5 100644 --- a/lntest/harness_net.go +++ b/lntest/harness_net.go @@ -17,7 +17,6 @@ import ( "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" - "github.com/btcsuite/btcd/integration/rpctest" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" @@ -55,7 +54,7 @@ type NetworkHarness struct { // Miner is a reference to a running full node that can be used to // create new blocks on the network. - Miner *rpctest.Harness + Miner *HarnessMiner // BackendCfg houses the information necessary to use a node as LND // chain backend, such as rpc configuration, P2P information etc. @@ -94,7 +93,7 @@ type NetworkHarness struct { // TODO(roasbeef): add option to use golang's build library to a binary of the // current repo. This will save developers from having to manually `go install` // within the repo each time before changes -func NewNetworkHarness(r *rpctest.Harness, b BackendConfig, lndBinary string, +func NewNetworkHarness(m *HarnessMiner, b BackendConfig, lndBinary string, dbBackend DatabaseBackend) (*NetworkHarness, error) { feeService := startFeeService() @@ -105,8 +104,8 @@ func NewNetworkHarness(r *rpctest.Harness, b BackendConfig, lndBinary string, activeNodes: make(map[int]*HarnessNode), nodesByPub: make(map[string]*HarnessNode), lndErrorChan: make(chan error), - netParams: r.ActiveNet, - Miner: r, + netParams: m.ActiveNet, + Miner: m, BackendCfg: b, feeService: feeService, runCtx: ctxt, @@ -949,41 +948,6 @@ func saveProfilesPage(node *HarnessNode) error { return nil } -// waitForTxInMempool blocks until the target txid is seen in the mempool. If -// the transaction isn't seen within the network before the passed timeout, -// then an error is returned. -func (n *NetworkHarness) waitForTxInMempool(txid chainhash.Hash) error { - ticker := time.NewTicker(50 * time.Millisecond) - defer ticker.Stop() - - ctxt, cancel := context.WithTimeout(n.runCtx, DefaultTimeout) - defer cancel() - - var mempool []*chainhash.Hash - for { - select { - case <-n.runCtx.Done(): - return fmt.Errorf("NetworkHarness has been torn down") - case <-ctxt.Done(): - return fmt.Errorf("wanted %v, found %v txs "+ - "in mempool: %v", txid, len(mempool), mempool) - - case <-ticker.C: - var err error - mempool, err = n.Miner.Client.GetRawMempool() - if err != nil { - return err - } - - for _, mempoolTx := range mempool { - if *mempoolTx == txid { - return nil - } - } - } - } -} - // OpenChannelParams houses the params to specify when opening a new channel. type OpenChannelParams struct { // Amt is the local amount being put into the channel. @@ -1332,7 +1296,7 @@ func (n *NetworkHarness) CloseChannel(lnNode *HarnessNode, return fmt.Errorf("unable to decode closeTxid: "+ "%v", err) } - if err := n.waitForTxInMempool(*closeTxid); err != nil { + if err := n.Miner.waitForTxInMempool(*closeTxid); err != nil { return fmt.Errorf("error while waiting for "+ "broadcast tx: %v", err) } @@ -1601,36 +1565,6 @@ func (n *NetworkHarness) SetFeeEstimateWithConf( n.feeService.setFeeWithConf(fee, conf) } -// CopyFile copies the file src to dest. -func CopyFile(dest, src string) error { - s, err := os.Open(src) - if err != nil { - return err - } - defer s.Close() - - d, err := os.Create(dest) - if err != nil { - return err - } - - if _, err := io.Copy(d, s); err != nil { - d.Close() - return err - } - - return d.Close() -} - -// FileExists returns true if the file at path exists. -func FileExists(path string) bool { - if _, err := os.Stat(path); os.IsNotExist(err) { - return false - } - - return true -} - // copyAll copies all files and directories from srcDir to dstDir recursively. // Note that this function does not support links. func copyAll(dstDir, srcDir string) error { diff --git a/lntest/harness_node.go b/lntest/harness_node.go index 58fef454c..0ee4ef6f7 100644 --- a/lntest/harness_node.go +++ b/lntest/harness_node.go @@ -18,8 +18,6 @@ import ( "time" "github.com/btcsuite/btcd/chaincfg" - "github.com/btcsuite/btcd/integration/rpctest" - "github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" "github.com/jackc/pgx/v4/pgxpool" @@ -465,74 +463,6 @@ func executePgQuery(query string) error { return err } -// NewMiner creates a new miner using btcd backend. The baseLogDir specifies -// the miner node's log dir. When tests are finished, during clean up, its log -// files, including any compressed log files from logrotate, are copied to -// logDir as logFilename. -func NewMiner(baseLogDir, logFilename string, netParams *chaincfg.Params, - handler *rpcclient.NotificationHandlers, - btcdBinary string) (*rpctest.Harness, func() error, error) { - - args := []string{ - "--rejectnonstd", - "--txindex", - "--nowinservice", - "--nobanning", - "--debuglevel=debug", - "--logdir=" + baseLogDir, - "--trickleinterval=100ms", - // Don't disconnect if a reply takes too long. - "--nostalldetect", - } - - miner, err := rpctest.New(netParams, handler, args, btcdBinary) - if err != nil { - return nil, nil, fmt.Errorf( - "unable to create mining node: %v", err, - ) - } - - cleanUp := func() error { - if err := miner.TearDown(); err != nil { - return fmt.Errorf( - "failed to tear down miner, got error: %s", err, - ) - } - - // After shutting down the miner, we'll make a copy of - // the log files before deleting the temporary log dir. - logDir := fmt.Sprintf("%s/%s", baseLogDir, netParams.Name) - files, err := ioutil.ReadDir(logDir) - if err != nil { - return fmt.Errorf("unable to read log directory: %v", err) - } - - for _, file := range files { - logFile := fmt.Sprintf( - "%s/%s", logDir, file.Name(), - ) - newFilename := strings.Replace(file.Name(), "btcd.log", logFilename, 1) - copyPath := fmt.Sprintf( - "%s/../%s", baseLogDir, newFilename, - ) - - err := CopyFile(filepath.Clean(copyPath), logFile) - if err != nil { - return fmt.Errorf("unable to copy file: %v", err) - } - } - - if err = os.RemoveAll(baseLogDir); err != nil { - return fmt.Errorf( - "cannot remove dir %s: %v", baseLogDir, err, - ) - } - return nil - } - - return miner, cleanUp, nil -} - // String gives the internal state of the node which is useful for debugging. func (hn *HarnessNode) String() string { type nodeCfg struct { diff --git a/lntest/itest/assertions.go b/lntest/itest/assertions.go index 822508d5a..922e34de8 100644 --- a/lntest/itest/assertions.go +++ b/lntest/itest/assertions.go @@ -10,7 +10,6 @@ import ( "time" "github.com/btcsuite/btcd/chaincfg/chainhash" - "github.com/btcsuite/btcd/integration/rpctest" "github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" @@ -691,7 +690,7 @@ func assertChannelPolicy(t *harnessTest, node *lntest.HarnessNode, // assertMinerBlockHeightDelta ensures that tempMiner is 'delta' blocks ahead // of miner. func assertMinerBlockHeightDelta(t *harnessTest, - miner, tempMiner *rpctest.Harness, delta int32) { + miner, tempMiner *lntest.HarnessMiner, delta int32) { // Ensure the chain lengths are what we expect. var predErr error diff --git a/lntest/itest/lnd_channel_force_close_test.go b/lntest/itest/lnd_channel_force_close_test.go index 9c0069e18..5a9c6e207 100644 --- a/lntest/itest/lnd_channel_force_close_test.go +++ b/lntest/itest/lnd_channel_force_close_test.go @@ -7,7 +7,6 @@ import ( "testing" "github.com/btcsuite/btcd/blockchain" - "github.com/btcsuite/btcd/integration/rpctest" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" "github.com/go-errors/errors" @@ -212,7 +211,7 @@ func testCommitmentTransactionDeadline(net *lntest.NetworkHarness, // calculateTxnsFeeRate takes a list of transactions and estimates the fee rate // used to sweep them. func calculateTxnsFeeRate(t *testing.T, - miner *rpctest.Harness, txns []*wire.MsgTx) int64 { + miner *lntest.HarnessMiner, txns []*wire.MsgTx) int64 { var totalWeight, totalFee int64 for _, tx := range txns { diff --git a/lntest/itest/lnd_open_channel_test.go b/lntest/itest/lnd_open_channel_test.go index 52ae84a00..392db06d8 100644 --- a/lntest/itest/lnd_open_channel_test.go +++ b/lntest/itest/lnd_open_channel_test.go @@ -8,7 +8,6 @@ import ( "github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/integration/rpctest" - "github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcutil" "github.com/lightningnetwork/lnd/funding" "github.com/lightningnetwork/lnd/lnrpc" @@ -34,16 +33,13 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) { ) // Set up a new miner that we can use to cause a reorg. - tempLogDir := fmt.Sprintf("%s/.tempminerlogs", lntest.GetLogDir()) + tempLogDir := ".tempminerlogs" logFilename := "output-open_channel_reorg-temp_miner.log" - tempMiner, tempMinerCleanUp, err := lntest.NewMiner( - tempLogDir, logFilename, harnessNetParams, - &rpcclient.NotificationHandlers{}, lntest.GetBtcdBinary(), - ) + tempMiner, err := lntest.NewTempMiner(tempLogDir, logFilename) require.NoError(t.t, err, "failed to create temp miner") defer func() { require.NoError( - t.t, tempMinerCleanUp(), + t.t, tempMiner.Stop(), "failed to clean up temp miner", ) }() @@ -61,7 +57,7 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) { if err != nil { t.Fatalf("unable to remove node: %v", err) } - nodeSlice := []*rpctest.Harness{net.Miner, tempMiner} + nodeSlice := []*rpctest.Harness{net.Miner.Harness, tempMiner.Harness} if err := rpctest.JoinNodes(nodeSlice, rpctest.Blocks); err != nil { t.Fatalf("unable to join node on blocks: %v", err) } @@ -186,7 +182,7 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) { t.Fatalf("unable to remove node: %v", err) } - nodes := []*rpctest.Harness{tempMiner, net.Miner} + nodes := []*rpctest.Harness{tempMiner.Harness, net.Miner.Harness} if err := rpctest.JoinNodes(nodes, rpctest.Blocks); err != nil { t.Fatalf("unable to join node on blocks: %v", err) } diff --git a/lntest/itest/lnd_test.go b/lntest/itest/lnd_test.go index 0b342e382..a2e991184 100644 --- a/lntest/itest/lnd_test.go +++ b/lntest/itest/lnd_test.go @@ -9,7 +9,6 @@ import ( "time" "github.com/btcsuite/btcd/integration/rpctest" - "github.com/btcsuite/btcd/rpcclient" "github.com/lightningnetwork/lnd/lntest" "github.com/stretchr/testify/require" ) @@ -113,14 +112,10 @@ func TestLightningNetworkDaemon(t *testing.T) { // guarantees of getting included in to blocks. // // We will also connect it to our chain backend. - minerLogDir := fmt.Sprintf("%s/.minerlogs", logDir) - miner, minerCleanUp, err := lntest.NewMiner( - minerLogDir, "output_btcd_miner.log", harnessNetParams, - &rpcclient.NotificationHandlers{}, lntest.GetBtcdBinary(), - ) + miner, err := lntest.NewMiner() require.NoError(t, err, "failed to create new miner") defer func() { - require.NoError(t, minerCleanUp(), "failed to clean up miner") + require.NoError(t, miner.Stop(), "failed to stop miner") }() // Start a chain backend. diff --git a/lntest/test_common.go b/lntest/test_common.go index 717694ceb..e858c168c 100644 --- a/lntest/test_common.go +++ b/lntest/test_common.go @@ -4,7 +4,9 @@ import ( "errors" "flag" "fmt" + "io" "net" + "os" "sync/atomic" "github.com/btcsuite/btcd/wire" @@ -162,3 +164,24 @@ func CheckChannelPolicy(policy, expectedPolicy *lnrpc.RoutingPolicy) error { return nil } + +// CopyFile copies the file src to dest. +func CopyFile(dest, src string) error { + s, err := os.Open(src) + if err != nil { + return err + } + defer s.Close() + + d, err := os.Create(dest) + if err != nil { + return err + } + + if _, err := io.Copy(d, s); err != nil { + d.Close() + return err + } + + return d.Close() +}