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 (
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)
}

@ -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() {

@ -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 {