2019-05-24 15:17:48 +03:00
|
|
|
// +build btcd
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
package lntest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2019-05-24 15:17:48 +03:00
|
|
|
"github.com/btcsuite/btcd/btcjson"
|
2018-12-14 11:24:00 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/integration/rpctest"
|
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
|
|
|
)
|
|
|
|
|
|
|
|
// logDir is the name of the temporary log directory.
|
|
|
|
const logDir = "./.backendlogs"
|
|
|
|
|
2019-05-24 15:17:48 +03:00
|
|
|
// 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"
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// BtcdBackendConfig is an implementation of the BackendConfig interface
|
|
|
|
// backed by a btcd node.
|
|
|
|
type BtcdBackendConfig struct {
|
2018-12-14 11:24:00 +03:00
|
|
|
// rpcConfig houses the connection config to the backing btcd instance.
|
|
|
|
rpcConfig rpcclient.ConnConfig
|
2018-12-14 11:24:00 +03:00
|
|
|
|
2019-05-24 15:17:48 +03:00
|
|
|
// harness is the backing btcd instance.
|
|
|
|
harness *rpctest.Harness
|
|
|
|
|
|
|
|
// minerAddr is the p2p address of the miner to connect to.
|
|
|
|
minerAddr string
|
2018-12-14 11:24:00 +03:00
|
|
|
}
|
|
|
|
|
2019-07-11 14:51:22 +03:00
|
|
|
// A compile time assertion to ensure BtcdBackendConfig meets the BackendConfig
|
|
|
|
// interface.
|
|
|
|
var _ BackendConfig = (*BtcdBackendConfig)(nil)
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// GenArgs returns the arguments needed to be passed to LND at startup for
|
|
|
|
// using this node as a chain backend.
|
|
|
|
func (b BtcdBackendConfig) GenArgs() []string {
|
|
|
|
var args []string
|
2018-12-14 11:24:00 +03:00
|
|
|
encodedCert := hex.EncodeToString(b.rpcConfig.Certificates)
|
2018-12-14 11:24:00 +03:00
|
|
|
args = append(args, "--bitcoin.node=btcd")
|
2018-12-14 11:24:00 +03:00
|
|
|
args = append(args, fmt.Sprintf("--btcd.rpchost=%v", b.rpcConfig.Host))
|
|
|
|
args = append(args, fmt.Sprintf("--btcd.rpcuser=%v", b.rpcConfig.User))
|
|
|
|
args = append(args, fmt.Sprintf("--btcd.rpcpass=%v", b.rpcConfig.Pass))
|
2018-12-14 11:24:00 +03:00
|
|
|
args = append(args, fmt.Sprintf("--btcd.rawrpccert=%v", encodedCert))
|
|
|
|
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2019-05-24 15:17:48 +03:00
|
|
|
// 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)
|
2018-12-14 11:24:00 +03:00
|
|
|
}
|
|
|
|
|
2019-05-24 15:17:49 +03:00
|
|
|
// Name returns the name of the backend type.
|
|
|
|
func (b BtcdBackendConfig) Name() string {
|
|
|
|
return "btcd"
|
|
|
|
}
|
|
|
|
|
2019-05-24 15:17:48 +03:00
|
|
|
// NewBackend starts a new rpctest.Harness and returns a BtcdBackendConfig for
|
|
|
|
// that node. miner should be set to the P2P address of the miner to connect
|
|
|
|
// to.
|
2019-07-11 14:51:22 +03:00
|
|
|
func NewBackend(miner string, netParams *chaincfg.Params) (
|
|
|
|
*BtcdBackendConfig, func(), error) {
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
args := []string{
|
|
|
|
"--rejectnonstd",
|
|
|
|
"--txindex",
|
|
|
|
"--trickleinterval=100ms",
|
|
|
|
"--debuglevel=debug",
|
|
|
|
"--logdir=" + logDir,
|
2019-05-24 15:17:48 +03:00
|
|
|
"--connect=" + miner,
|
2020-07-31 11:32:30 +03:00
|
|
|
"--nowinservice",
|
2018-12-14 11:24:00 +03:00
|
|
|
}
|
|
|
|
chainBackend, err := rpctest.New(netParams, nil, args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("unable to create btcd node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := chainBackend.SetUp(false, 0); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("unable to set up btcd backend: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bd := &BtcdBackendConfig{
|
2019-05-24 15:17:48 +03:00
|
|
|
rpcConfig: chainBackend.RPCConfig(),
|
|
|
|
harness: chainBackend,
|
|
|
|
minerAddr: miner,
|
2018-12-14 11:24:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanUp := func() {
|
|
|
|
chainBackend.TearDown()
|
|
|
|
|
|
|
|
// After shutting down the chain backend, we'll make a copy of
|
|
|
|
// the log file before deleting the temporary log dir.
|
|
|
|
logFile := logDir + "/" + netParams.Name + "/btcd.log"
|
|
|
|
err := CopyFile("./output_btcd_chainbackend.log", logFile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("unable to copy file: %v\n", err)
|
|
|
|
}
|
|
|
|
if err = os.RemoveAll(logDir); err != nil {
|
|
|
|
fmt.Printf("Cannot remove dir %s: %v\n", logDir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bd, cleanUp, nil
|
|
|
|
}
|