From aec00b1277243b23983e6cae300ba1b4853a25bc Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 24 May 2019 14:17:48 +0200 Subject: [PATCH] lntest+lnd_test: add Connect and Disconnect miner for BackendCfgs This commit gives the current chainbackend the ability to connect and disconnect the chain backend at will. We do this to let the chain backend initiate the connection to the miner, not the other way around. This is a preparation for using Neutrino as a backend, as it only allows making outbound connections. We must also move the setup of the chainbackend to after to miner, to know the address to connect to. --- lnd_test.go | 24 +++++++++--------------- lntest/btcd.go | 37 +++++++++++++++++++++++++++---------- lntest/node.go | 8 +++++--- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/lnd_test.go b/lnd_test.go index bf48c1bc..e014ee35 100644 --- a/lnd_test.go +++ b/lnd_test.go @@ -1763,7 +1763,6 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) { var ( ctxb = context.Background() temp = "temp" - perm = "perm" ) // Set up a new miner that we can use to cause a reorg. @@ -1901,9 +1900,7 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) { // Now we disconnect Alice's chain backend from the original miner, and // connect the two miners together. Since the temporary miner knows // about a longer chain, both miners should sync to that chain. - err = net.Miner.Node.Node( - btcjson.NRemove, net.BackendCfg.P2PAddr(), &perm, - ) + err = net.BackendCfg.DisconnectMiner() if err != nil { t.Fatalf("unable to remove node: %v", err) } @@ -1934,9 +1931,7 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) { t.Fatalf("unable to remove node: %v", err) } - err = net.Miner.Node.Node( - btcjson.NConnect, net.BackendCfg.P2PAddr(), &perm, - ) + err = net.BackendCfg.ConnectMiner() if err != nil { t.Fatalf("unable to remove node: %v", err) } @@ -13316,13 +13311,6 @@ var testsCases = []*testCase{ func TestLightningNetworkDaemon(t *testing.T) { ht := newHarnessTest(t) - // Start a btcd chain backend. - chainBackend, cleanUp, err := lntest.NewBtcdBackend() - if err != nil { - ht.Fatalf("unable to start btcd: %v", err) - } - defer cleanUp() - // Declare the network harness here to gain access to its // 'OnTxAccepted' call back. var lndHarness *lntest.NetworkHarness @@ -13343,7 +13331,6 @@ func TestLightningNetworkDaemon(t *testing.T) { "--debuglevel=debug", "--logdir=" + minerLogDir, "--trickleinterval=100ms", - "--connect=" + chainBackend.P2PAddr(), } handlers := &rpcclient.NotificationHandlers{ OnTxAccepted: func(hash *chainhash.Hash, amt btcutil.Amount) { @@ -13373,6 +13360,13 @@ func TestLightningNetworkDaemon(t *testing.T) { } }() + // Start a btcd chain backend. + chainBackend, cleanUp, err := lntest.NewBtcdBackend(miner.P2PAddress()) + if err != nil { + ht.Fatalf("unable to start btcd: %v", err) + } + defer cleanUp() + if err := miner.SetUp(true, 50); err != nil { ht.Fatalf("unable to set up mining node: %v", err) } diff --git a/lntest/btcd.go b/lntest/btcd.go index 48e4d71f..84cd6830 100644 --- a/lntest/btcd.go +++ b/lntest/btcd.go @@ -5,6 +5,7 @@ import ( "fmt" "os" + "github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/integration/rpctest" "github.com/btcsuite/btcd/rpcclient" @@ -13,14 +14,23 @@ import ( // logDir is the name of the temporary log directory. const logDir = "./.backendlogs" +// perm is used to signal we want to establish a permanent connection using the +// btcd Node API. +// +// NOTE: Cannot be const, since the node API expects a reference. +var perm = "perm" + // BtcdBackendConfig is an implementation of the BackendConfig interface // backed by a btcd node. type BtcdBackendConfig struct { // rpcConfig houses the connection config to the backing btcd instance. rpcConfig rpcclient.ConnConfig - // p2pAddress is the p2p address of the btcd instance. - p2pAddress string + // harness is the backing btcd instance. + harness *rpctest.Harness + + // minerAddr is the p2p address of the miner to connect to. + minerAddr string } // GenArgs returns the arguments needed to be passed to LND at startup for @@ -37,21 +47,27 @@ func (b BtcdBackendConfig) GenArgs() []string { return args } -// P2PAddr returns the address of this node to be used when connection over the -// Bitcoin P2P network. -func (b BtcdBackendConfig) P2PAddr() string { - return b.p2pAddress +// ConnectMiner is called to establish a connection to the test miner. +func (b BtcdBackendConfig) ConnectMiner() error { + return b.harness.Node.Node(btcjson.NConnect, b.minerAddr, &perm) +} + +// DisconnectMiner is called to disconnect the miner. +func (b BtcdBackendConfig) DisconnectMiner() error { + return b.harness.Node.Node(btcjson.NRemove, b.minerAddr, &perm) } // NewBtcdBackend starts a new rpctest.Harness and returns a BtcdBackendConfig -// for that node. -func NewBtcdBackend() (*BtcdBackendConfig, func(), error) { +// for that node. miner should be set to the P2P address of the miner to +// connect to. +func NewBtcdBackend(miner string) (*BtcdBackendConfig, func(), error) { args := []string{ "--rejectnonstd", "--txindex", "--trickleinterval=100ms", "--debuglevel=debug", "--logdir=" + logDir, + "--connect=" + miner, } netParams := &chaincfg.SimNetParams chainBackend, err := rpctest.New(netParams, nil, args) @@ -64,8 +80,9 @@ func NewBtcdBackend() (*BtcdBackendConfig, func(), error) { } bd := &BtcdBackendConfig{ - rpcConfig: chainBackend.RPCConfig(), - p2pAddress: chainBackend.P2PAddress(), + rpcConfig: chainBackend.RPCConfig(), + harness: chainBackend, + minerAddr: miner, } cleanUp := func() { diff --git a/lntest/node.go b/lntest/node.go index 3afbd5d0..8aa69e69 100644 --- a/lntest/node.go +++ b/lntest/node.go @@ -99,9 +99,11 @@ type BackendConfig interface { // for using this node as a chain backend. GenArgs() []string - // P2PAddr returns the address of this node to be used when connection - // over the Bitcoin P2P network. - P2PAddr() string + // ConnectMiner is called to establish a connection to the test miner. + ConnectMiner() error + + // DisconnectMiner is called to bitconneeeect the miner. + DisconnectMiner() error } type nodeConfig struct {