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.
This commit is contained in:
Johan T. Halseth 2019-05-24 14:17:48 +02:00
parent 1788fa1566
commit aec00b1277
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
3 changed files with 41 additions and 28 deletions

@ -1763,7 +1763,6 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) {
var ( var (
ctxb = context.Background() ctxb = context.Background()
temp = "temp" temp = "temp"
perm = "perm"
) )
// Set up a new miner that we can use to cause a reorg. // 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 // Now we disconnect Alice's chain backend from the original miner, and
// connect the two miners together. Since the temporary miner knows // connect the two miners together. Since the temporary miner knows
// about a longer chain, both miners should sync to that chain. // about a longer chain, both miners should sync to that chain.
err = net.Miner.Node.Node( err = net.BackendCfg.DisconnectMiner()
btcjson.NRemove, net.BackendCfg.P2PAddr(), &perm,
)
if err != nil { if err != nil {
t.Fatalf("unable to remove node: %v", err) 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) t.Fatalf("unable to remove node: %v", err)
} }
err = net.Miner.Node.Node( err = net.BackendCfg.ConnectMiner()
btcjson.NConnect, net.BackendCfg.P2PAddr(), &perm,
)
if err != nil { if err != nil {
t.Fatalf("unable to remove node: %v", err) t.Fatalf("unable to remove node: %v", err)
} }
@ -13316,13 +13311,6 @@ var testsCases = []*testCase{
func TestLightningNetworkDaemon(t *testing.T) { func TestLightningNetworkDaemon(t *testing.T) {
ht := newHarnessTest(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 // Declare the network harness here to gain access to its
// 'OnTxAccepted' call back. // 'OnTxAccepted' call back.
var lndHarness *lntest.NetworkHarness var lndHarness *lntest.NetworkHarness
@ -13343,7 +13331,6 @@ func TestLightningNetworkDaemon(t *testing.T) {
"--debuglevel=debug", "--debuglevel=debug",
"--logdir=" + minerLogDir, "--logdir=" + minerLogDir,
"--trickleinterval=100ms", "--trickleinterval=100ms",
"--connect=" + chainBackend.P2PAddr(),
} }
handlers := &rpcclient.NotificationHandlers{ handlers := &rpcclient.NotificationHandlers{
OnTxAccepted: func(hash *chainhash.Hash, amt btcutil.Amount) { 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 { if err := miner.SetUp(true, 50); err != nil {
ht.Fatalf("unable to set up mining node: %v", err) ht.Fatalf("unable to set up mining node: %v", err)
} }

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/integration/rpctest" "github.com/btcsuite/btcd/integration/rpctest"
"github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/rpcclient"
@ -13,14 +14,23 @@ import (
// logDir is the name of the temporary log directory. // logDir is the name of the temporary log directory.
const logDir = "./.backendlogs" 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 // BtcdBackendConfig is an implementation of the BackendConfig interface
// backed by a btcd node. // backed by a btcd node.
type BtcdBackendConfig struct { type BtcdBackendConfig struct {
// rpcConfig houses the connection config to the backing btcd instance. // rpcConfig houses the connection config to the backing btcd instance.
rpcConfig rpcclient.ConnConfig rpcConfig rpcclient.ConnConfig
// p2pAddress is the p2p address of the btcd instance. // harness is the backing btcd instance.
p2pAddress string 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 // GenArgs returns the arguments needed to be passed to LND at startup for
@ -37,21 +47,27 @@ func (b BtcdBackendConfig) GenArgs() []string {
return args return args
} }
// P2PAddr returns the address of this node to be used when connection over the // ConnectMiner is called to establish a connection to the test miner.
// Bitcoin P2P network. func (b BtcdBackendConfig) ConnectMiner() error {
func (b BtcdBackendConfig) P2PAddr() string { return b.harness.Node.Node(btcjson.NConnect, b.minerAddr, &perm)
return b.p2pAddress }
// 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 // NewBtcdBackend starts a new rpctest.Harness and returns a BtcdBackendConfig
// for that node. // for that node. miner should be set to the P2P address of the miner to
func NewBtcdBackend() (*BtcdBackendConfig, func(), error) { // connect to.
func NewBtcdBackend(miner string) (*BtcdBackendConfig, func(), error) {
args := []string{ args := []string{
"--rejectnonstd", "--rejectnonstd",
"--txindex", "--txindex",
"--trickleinterval=100ms", "--trickleinterval=100ms",
"--debuglevel=debug", "--debuglevel=debug",
"--logdir=" + logDir, "--logdir=" + logDir,
"--connect=" + miner,
} }
netParams := &chaincfg.SimNetParams netParams := &chaincfg.SimNetParams
chainBackend, err := rpctest.New(netParams, nil, args) chainBackend, err := rpctest.New(netParams, nil, args)
@ -64,8 +80,9 @@ func NewBtcdBackend() (*BtcdBackendConfig, func(), error) {
} }
bd := &BtcdBackendConfig{ bd := &BtcdBackendConfig{
rpcConfig: chainBackend.RPCConfig(), rpcConfig: chainBackend.RPCConfig(),
p2pAddress: chainBackend.P2PAddress(), harness: chainBackend,
minerAddr: miner,
} }
cleanUp := func() { cleanUp := func() {

@ -99,9 +99,11 @@ type BackendConfig interface {
// for using this node as a chain backend. // for using this node as a chain backend.
GenArgs() []string GenArgs() []string
// P2PAddr returns the address of this node to be used when connection // ConnectMiner is called to establish a connection to the test miner.
// over the Bitcoin P2P network. ConnectMiner() error
P2PAddr() string
// DisconnectMiner is called to bitconneeeect the miner.
DisconnectMiner() error
} }
type nodeConfig struct { type nodeConfig struct {