2020-01-10 15:27:46 +01:00
|
|
|
// +build !bitcoind,!neutrino
|
2019-05-24 14:17:48 +02:00
|
|
|
|
2018-12-14 09:24:00 +01:00
|
|
|
package lntest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2020-09-02 21:15:04 +02:00
|
|
|
"errors"
|
2018-12-14 09:24:00 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2019-05-24 14:17:48 +02:00
|
|
|
"github.com/btcsuite/btcd/btcjson"
|
2018-12-14 09:24:00 +01:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/integration/rpctest"
|
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
|
|
|
)
|
|
|
|
|
2020-11-04 11:03:30 +01:00
|
|
|
// logDirPattern is the pattern of the name of the temporary log directory.
|
|
|
|
const logDirPattern = "%s/.backendlogs"
|
2018-12-14 09:24:00 +01:00
|
|
|
|
2020-09-03 12:59:08 +02:00
|
|
|
// temp is used to signal we want to establish a temporary connection using the
|
2019-05-24 14:17:48 +02:00
|
|
|
// btcd Node API.
|
|
|
|
//
|
|
|
|
// NOTE: Cannot be const, since the node API expects a reference.
|
2020-09-03 12:59:08 +02:00
|
|
|
var temp = "temp"
|
2019-05-24 14:17:48 +02:00
|
|
|
|
2018-12-14 09:24:00 +01:00
|
|
|
// BtcdBackendConfig is an implementation of the BackendConfig interface
|
|
|
|
// backed by a btcd node.
|
|
|
|
type BtcdBackendConfig struct {
|
2018-12-14 09:24:00 +01:00
|
|
|
// rpcConfig houses the connection config to the backing btcd instance.
|
|
|
|
rpcConfig rpcclient.ConnConfig
|
2018-12-14 09:24:00 +01:00
|
|
|
|
2019-05-24 14:17:48 +02: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 09:24:00 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:51:22 +02:00
|
|
|
// A compile time assertion to ensure BtcdBackendConfig meets the BackendConfig
|
|
|
|
// interface.
|
|
|
|
var _ BackendConfig = (*BtcdBackendConfig)(nil)
|
|
|
|
|
2018-12-14 09:24:00 +01: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 09:24:00 +01:00
|
|
|
encodedCert := hex.EncodeToString(b.rpcConfig.Certificates)
|
2018-12-14 09:24:00 +01:00
|
|
|
args = append(args, "--bitcoin.node=btcd")
|
2018-12-14 09:24:00 +01: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 09:24:00 +01:00
|
|
|
args = append(args, fmt.Sprintf("--btcd.rawrpccert=%v", encodedCert))
|
|
|
|
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2019-05-24 14:17:48 +02:00
|
|
|
// ConnectMiner is called to establish a connection to the test miner.
|
|
|
|
func (b BtcdBackendConfig) ConnectMiner() error {
|
2020-09-03 12:59:08 +02:00
|
|
|
return b.harness.Node.Node(btcjson.NConnect, b.minerAddr, &temp)
|
2019-05-24 14:17:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DisconnectMiner is called to disconnect the miner.
|
|
|
|
func (b BtcdBackendConfig) DisconnectMiner() error {
|
2020-09-03 12:59:08 +02:00
|
|
|
return b.harness.Node.Node(btcjson.NDisconnect, b.minerAddr, &temp)
|
2018-12-14 09:24:00 +01:00
|
|
|
}
|
|
|
|
|
2019-05-24 14:17:49 +02:00
|
|
|
// Name returns the name of the backend type.
|
|
|
|
func (b BtcdBackendConfig) Name() string {
|
|
|
|
return "btcd"
|
|
|
|
}
|
|
|
|
|
2019-05-24 14:17:48 +02: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 13:51:22 +02:00
|
|
|
func NewBackend(miner string, netParams *chaincfg.Params) (
|
2020-09-02 21:15:04 +02:00
|
|
|
*BtcdBackendConfig, func() error, error) {
|
2019-07-11 13:51:22 +02:00
|
|
|
|
2020-11-04 11:03:30 +01:00
|
|
|
baseLogDir := fmt.Sprintf(logDirPattern, GetLogDir())
|
2018-12-14 09:24:00 +01:00
|
|
|
args := []string{
|
|
|
|
"--rejectnonstd",
|
|
|
|
"--txindex",
|
|
|
|
"--trickleinterval=100ms",
|
|
|
|
"--debuglevel=debug",
|
2020-11-04 11:03:30 +01:00
|
|
|
"--logdir=" + baseLogDir,
|
2020-07-31 10:32:30 +02:00
|
|
|
"--nowinservice",
|
2020-09-03 12:59:08 +02: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 09:24:00 +01: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 14:17:48 +02:00
|
|
|
rpcConfig: chainBackend.RPCConfig(),
|
|
|
|
harness: chainBackend,
|
|
|
|
minerAddr: miner,
|
2018-12-14 09:24:00 +01:00
|
|
|
}
|
|
|
|
|
2020-09-02 21:15:04 +02:00
|
|
|
cleanUp := func() error {
|
|
|
|
var errStr string
|
|
|
|
if err := chainBackend.TearDown(); err != nil {
|
|
|
|
errStr += err.Error() + "\n"
|
|
|
|
}
|
2018-12-14 09:24:00 +01: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 11:03:30 +01:00
|
|
|
logFile := baseLogDir + "/" + netParams.Name + "/btcd.log"
|
|
|
|
logDestination := fmt.Sprintf(
|
|
|
|
"%s/output_btcd_chainbackend.log", GetLogDir(),
|
|
|
|
)
|
|
|
|
err := CopyFile(logDestination, logFile)
|
2018-12-14 09:24:00 +01:00
|
|
|
if err != nil {
|
2020-09-02 21:15:04 +02:00
|
|
|
errStr += fmt.Sprintf("unable to copy file: %v\n", err)
|
2018-12-14 09:24:00 +01:00
|
|
|
}
|
2020-11-04 11:03:30 +01:00
|
|
|
if err = os.RemoveAll(baseLogDir); err != nil {
|
2020-09-02 21:15:04 +02:00
|
|
|
errStr += fmt.Sprintf(
|
2020-11-04 11:03:30 +01:00
|
|
|
"cannot remove dir %s: %v\n", baseLogDir, err,
|
2020-09-02 21:15:04 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
if errStr != "" {
|
|
|
|
return errors.New(errStr)
|
2018-12-14 09:24:00 +01:00
|
|
|
}
|
2020-09-02 21:15:04 +02:00
|
|
|
return nil
|
2018-12-14 09:24:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return bd, cleanUp, nil
|
|
|
|
}
|