2020-01-10 17:27:46 +03:00
|
|
|
// +build !bitcoind,!neutrino
|
2019-05-24 15:17:48 +03:00
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
package lntest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2020-09-02 22:15:04 +03:00
|
|
|
"errors"
|
2018-12-14 11:24:00 +03:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2020-11-04 13:03:30 +03:00
|
|
|
// logDirPattern is the pattern of the name of the temporary log directory.
|
|
|
|
const logDirPattern = "%s/.backendlogs"
|
2018-12-14 11:24:00 +03:00
|
|
|
|
2020-09-03 13:59:08 +03:00
|
|
|
// temp is used to signal we want to establish a temporary connection using the
|
2019-05-24 15:17:48 +03:00
|
|
|
// btcd Node API.
|
|
|
|
//
|
|
|
|
// NOTE: Cannot be const, since the node API expects a reference.
|
2020-09-03 13:59:08 +03:00
|
|
|
var temp = "temp"
|
2019-05-24 15:17:48 +03:00
|
|
|
|
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 {
|
2021-03-10 01:12:26 +03:00
|
|
|
return b.harness.Client.Node(btcjson.NConnect, b.minerAddr, &temp)
|
2019-05-24 15:17:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DisconnectMiner is called to disconnect the miner.
|
|
|
|
func (b BtcdBackendConfig) DisconnectMiner() error {
|
2021-03-10 01:12:26 +03:00
|
|
|
return b.harness.Client.Node(btcjson.NDisconnect, b.minerAddr, &temp)
|
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) (
|
2020-09-02 22:15:04 +03:00
|
|
|
*BtcdBackendConfig, func() error, error) {
|
2019-07-11 14:51:22 +03:00
|
|
|
|
2020-11-04 13:03:30 +03:00
|
|
|
baseLogDir := fmt.Sprintf(logDirPattern, GetLogDir())
|
2018-12-14 11:24:00 +03:00
|
|
|
args := []string{
|
|
|
|
"--rejectnonstd",
|
|
|
|
"--txindex",
|
|
|
|
"--trickleinterval=100ms",
|
|
|
|
"--debuglevel=debug",
|
2020-11-04 13:03:30 +03:00
|
|
|
"--logdir=" + baseLogDir,
|
2020-07-31 11:32:30 +03:00
|
|
|
"--nowinservice",
|
2020-09-03 13:59:08 +03:00
|
|
|
// The miner will get banned and disconnected from the node if
|
|
|
|
// its requested data are not found. We add a nobanning flag to
|
|
|
|
// make sure they stay connected if it happens.
|
|
|
|
"--nobanning",
|
2018-12-14 11:24:00 +03:00
|
|
|
}
|
2020-12-03 13:30:22 +03:00
|
|
|
chainBackend, err := rpctest.New(netParams, nil, args, GetBtcdBinary())
|
2018-12-14 11:24:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("unable to create btcd node: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:30:23 +03:00
|
|
|
// We want to overwrite some of the connection settings to make the
|
|
|
|
// tests more robust. We might need to restart the backend while there
|
|
|
|
// are already blocks present, which will take a bit longer than the
|
|
|
|
// 1 second the default settings amount to. Doubling both values will
|
|
|
|
// give us retries up to 4 seconds.
|
|
|
|
chainBackend.MaxConnRetries = rpctest.DefaultMaxConnectionRetries * 2
|
|
|
|
chainBackend.ConnectionRetryTimeout = rpctest.DefaultConnectionRetryTimeout * 2
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-02 22:15:04 +03:00
|
|
|
cleanUp := func() error {
|
|
|
|
var errStr string
|
|
|
|
if err := chainBackend.TearDown(); err != nil {
|
|
|
|
errStr += err.Error() + "\n"
|
|
|
|
}
|
2018-12-14 11:24:00 +03:00
|
|
|
|
|
|
|
// After shutting down the chain backend, we'll make a copy of
|
|
|
|
// the log file before deleting the temporary log dir.
|
2020-11-04 13:03:30 +03:00
|
|
|
logFile := baseLogDir + "/" + netParams.Name + "/btcd.log"
|
|
|
|
logDestination := fmt.Sprintf(
|
|
|
|
"%s/output_btcd_chainbackend.log", GetLogDir(),
|
|
|
|
)
|
|
|
|
err := CopyFile(logDestination, logFile)
|
2018-12-14 11:24:00 +03:00
|
|
|
if err != nil {
|
2020-09-02 22:15:04 +03:00
|
|
|
errStr += fmt.Sprintf("unable to copy file: %v\n", err)
|
2018-12-14 11:24:00 +03:00
|
|
|
}
|
2020-11-04 13:03:30 +03:00
|
|
|
if err = os.RemoveAll(baseLogDir); err != nil {
|
2020-09-02 22:15:04 +03:00
|
|
|
errStr += fmt.Sprintf(
|
2020-11-04 13:03:30 +03:00
|
|
|
"cannot remove dir %s: %v\n", baseLogDir, err,
|
2020-09-02 22:15:04 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
if errStr != "" {
|
|
|
|
return errors.New(errStr)
|
2018-12-14 11:24:00 +03:00
|
|
|
}
|
2020-09-02 22:15:04 +03:00
|
|
|
return nil
|
2018-12-14 11:24:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return bd, cleanUp, nil
|
|
|
|
}
|