2021-08-23 10:35:48 +02:00
|
|
|
//go:build bitcoind
|
2020-10-09 13:35:00 +02:00
|
|
|
// +build bitcoind
|
|
|
|
|
|
|
|
package lntest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
2022-08-12 07:07:16 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntemp/node"
|
2020-10-09 13:35:00 +02:00
|
|
|
)
|
|
|
|
|
2020-11-04 11:03:30 +01:00
|
|
|
// logDirPattern is the pattern of the name of the temporary log directory.
|
|
|
|
const logDirPattern = "%s/.backendlogs"
|
2020-10-09 13:35:00 +02:00
|
|
|
|
|
|
|
// BitcoindBackendConfig is an implementation of the BackendConfig interface
|
|
|
|
// backed by a Bitcoind node.
|
|
|
|
type BitcoindBackendConfig struct {
|
2021-12-14 01:11:49 +01:00
|
|
|
rpcHost string
|
|
|
|
rpcUser string
|
|
|
|
rpcPass string
|
|
|
|
zmqBlockPath string
|
|
|
|
zmqTxPath string
|
|
|
|
p2pPort int
|
2020-10-09 13:35:00 +02:00
|
|
|
rpcClient *rpcclient.Client
|
2022-03-10 11:32:32 +01:00
|
|
|
rpcPolling bool
|
2020-10-09 13:35:00 +02:00
|
|
|
|
|
|
|
// minerAddr is the p2p address of the miner to connect to.
|
|
|
|
minerAddr string
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time assertion to ensure BitcoindBackendConfig meets the
|
|
|
|
// BackendConfig interface.
|
2022-08-12 07:07:16 +02:00
|
|
|
var _ node.BackendConfig = (*BitcoindBackendConfig)(nil)
|
2020-10-09 13:35:00 +02:00
|
|
|
|
|
|
|
// GenArgs returns the arguments needed to be passed to LND at startup for
|
|
|
|
// using this node as a chain backend.
|
|
|
|
func (b BitcoindBackendConfig) GenArgs() []string {
|
|
|
|
var args []string
|
|
|
|
args = append(args, "--bitcoin.node=bitcoind")
|
2021-12-14 01:11:49 +01:00
|
|
|
args = append(args, fmt.Sprintf("--bitcoind.rpchost=%v", b.rpcHost))
|
|
|
|
args = append(args, fmt.Sprintf("--bitcoind.rpcuser=%v", b.rpcUser))
|
|
|
|
args = append(args, fmt.Sprintf("--bitcoind.rpcpass=%v", b.rpcPass))
|
2022-03-10 11:32:32 +01:00
|
|
|
|
|
|
|
if b.rpcPolling {
|
|
|
|
args = append(args, fmt.Sprintf("--bitcoind.rpcpolling"))
|
|
|
|
args = append(args,
|
|
|
|
fmt.Sprintf("--bitcoind.blockpollinginterval=10ms"))
|
|
|
|
args = append(args,
|
|
|
|
fmt.Sprintf("--bitcoind.txpollinginterval=10ms"))
|
|
|
|
} else {
|
|
|
|
args = append(args, fmt.Sprintf("--bitcoind.zmqpubrawblock=%v",
|
|
|
|
b.zmqBlockPath))
|
|
|
|
args = append(args, fmt.Sprintf("--bitcoind.zmqpubrawtx=%v",
|
|
|
|
b.zmqTxPath))
|
|
|
|
}
|
2020-10-09 13:35:00 +02:00
|
|
|
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectMiner is called to establish a connection to the test miner.
|
|
|
|
func (b BitcoindBackendConfig) ConnectMiner() error {
|
|
|
|
return b.rpcClient.AddNode(b.minerAddr, rpcclient.ANAdd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisconnectMiner is called to disconnect the miner.
|
|
|
|
func (b BitcoindBackendConfig) DisconnectMiner() error {
|
|
|
|
return b.rpcClient.AddNode(b.minerAddr, rpcclient.ANRemove)
|
|
|
|
}
|
|
|
|
|
2021-12-14 01:05:51 +01:00
|
|
|
// Credentials returns the rpc username, password and host for the backend.
|
|
|
|
func (b BitcoindBackendConfig) Credentials() (string, string, string, error) {
|
|
|
|
return b.rpcUser, b.rpcPass, b.rpcHost, nil
|
|
|
|
}
|
|
|
|
|
2020-10-09 13:35:00 +02:00
|
|
|
// Name returns the name of the backend type.
|
|
|
|
func (b BitcoindBackendConfig) Name() string {
|
|
|
|
return "bitcoind"
|
|
|
|
}
|
|
|
|
|
|
|
|
// newBackend starts a bitcoind node with the given extra parameters and returns
|
|
|
|
// a BitcoindBackendConfig for that node.
|
2022-03-10 11:32:32 +01:00
|
|
|
func newBackend(miner string, netParams *chaincfg.Params, extraArgs []string,
|
|
|
|
rpcPolling bool) (*BitcoindBackendConfig, func() error, error) {
|
2020-10-09 13:35:00 +02:00
|
|
|
|
2022-08-12 07:07:16 +02:00
|
|
|
baseLogDir := fmt.Sprintf(logDirPattern, node.GetLogDir())
|
2020-10-09 13:35:00 +02:00
|
|
|
if netParams != &chaincfg.RegressionNetParams {
|
|
|
|
return nil, nil, fmt.Errorf("only regtest supported")
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:03:30 +01:00
|
|
|
if err := os.MkdirAll(baseLogDir, 0700); err != nil {
|
2020-10-09 13:35:00 +02:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:03:30 +01:00
|
|
|
logFile, err := filepath.Abs(baseLogDir + "/bitcoind.log")
|
2020-10-09 13:35:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tempBitcoindDir, err := ioutil.TempDir("", "bitcoind")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil,
|
|
|
|
fmt.Errorf("unable to create temp directory: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-08-12 07:07:16 +02:00
|
|
|
zmqBlockAddr := fmt.Sprintf("tcp://127.0.0.1:%d",
|
|
|
|
node.NextAvailablePort())
|
|
|
|
zmqTxAddr := fmt.Sprintf("tcp://127.0.0.1:%d",
|
|
|
|
node.NextAvailablePort())
|
|
|
|
rpcPort := node.NextAvailablePort()
|
|
|
|
p2pPort := node.NextAvailablePort()
|
2020-10-09 13:35:00 +02:00
|
|
|
|
2020-10-09 13:35:02 +02:00
|
|
|
cmdArgs := []string{
|
|
|
|
"-datadir=" + tempBitcoindDir,
|
2020-10-09 13:35:00 +02:00
|
|
|
"-whitelist=127.0.0.1", // whitelist localhost to speed up relay
|
2020-10-09 13:35:02 +02:00
|
|
|
"-rpcauth=weks:469e9bb14ab2360f8e226efed5ca6f" +
|
|
|
|
"d$507c670e800a95284294edb5773b05544b" +
|
2020-10-09 13:35:00 +02:00
|
|
|
"220110063096c221be9933c82d38e1",
|
|
|
|
fmt.Sprintf("-rpcport=%d", rpcPort),
|
|
|
|
fmt.Sprintf("-port=%d", p2pPort),
|
2020-11-04 11:03:27 +01:00
|
|
|
"-zmqpubrawblock=" + zmqBlockAddr,
|
|
|
|
"-zmqpubrawtx=" + zmqTxAddr,
|
2020-10-09 13:35:02 +02:00
|
|
|
"-debuglogfile=" + logFile,
|
|
|
|
}
|
|
|
|
cmdArgs = append(cmdArgs, extraArgs...)
|
|
|
|
bitcoind := exec.Command("bitcoind", cmdArgs...)
|
2020-10-09 13:35:00 +02:00
|
|
|
|
|
|
|
err = bitcoind.Start()
|
|
|
|
if err != nil {
|
2020-10-09 13:35:02 +02:00
|
|
|
if err := os.RemoveAll(tempBitcoindDir); err != nil {
|
|
|
|
fmt.Printf("unable to remote temp dir %v: %v",
|
|
|
|
tempBitcoindDir, err)
|
|
|
|
}
|
2020-10-09 13:35:00 +02:00
|
|
|
return nil, nil, fmt.Errorf("couldn't start bitcoind: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanUp := func() error {
|
2020-10-09 13:35:02 +02:00
|
|
|
_ = bitcoind.Process.Kill()
|
|
|
|
_ = bitcoind.Wait()
|
2020-10-09 13:35:00 +02:00
|
|
|
|
|
|
|
var errStr string
|
|
|
|
// 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
|
|
|
logDestination := fmt.Sprintf(
|
2022-08-12 07:07:16 +02:00
|
|
|
"%s/output_bitcoind_chainbackend.log", node.GetLogDir(),
|
2020-11-04 11:03:30 +01:00
|
|
|
)
|
2022-08-12 07:07:16 +02:00
|
|
|
err := node.CopyFile(logDestination, logFile)
|
2020-10-09 13:35:00 +02:00
|
|
|
if err != nil {
|
|
|
|
errStr += fmt.Sprintf("unable to copy file: %v\n", err)
|
|
|
|
}
|
2020-11-04 11:03:30 +01:00
|
|
|
if err = os.RemoveAll(baseLogDir); err != nil {
|
2020-10-09 13:35:00 +02:00
|
|
|
errStr += fmt.Sprintf(
|
2020-11-04 11:03:30 +01:00
|
|
|
"cannot remove dir %s: %v\n", baseLogDir, err,
|
2020-10-09 13:35:00 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(tempBitcoindDir); err != nil {
|
|
|
|
errStr += fmt.Sprintf(
|
|
|
|
"cannot remove dir %s: %v\n",
|
|
|
|
tempBitcoindDir, err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if errStr != "" {
|
|
|
|
return errors.New(errStr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow process to start.
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
rpcHost := fmt.Sprintf("127.0.0.1:%d", rpcPort)
|
|
|
|
rpcUser := "weks"
|
|
|
|
rpcPass := "weks"
|
|
|
|
|
|
|
|
rpcCfg := rpcclient.ConnConfig{
|
|
|
|
Host: rpcHost,
|
|
|
|
User: rpcUser,
|
|
|
|
Pass: rpcPass,
|
|
|
|
DisableConnectOnNew: true,
|
|
|
|
DisableAutoReconnect: false,
|
|
|
|
DisableTLS: true,
|
|
|
|
HTTPPostMode: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := rpcclient.New(&rpcCfg, nil)
|
|
|
|
if err != nil {
|
2020-10-09 13:35:02 +02:00
|
|
|
_ = cleanUp()
|
2020-10-09 13:35:00 +02:00
|
|
|
return nil, nil, fmt.Errorf("unable to create rpc client: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bd := BitcoindBackendConfig{
|
2021-12-14 01:11:49 +01:00
|
|
|
rpcHost: rpcHost,
|
|
|
|
rpcUser: rpcUser,
|
|
|
|
rpcPass: rpcPass,
|
|
|
|
zmqBlockPath: zmqBlockAddr,
|
|
|
|
zmqTxPath: zmqTxAddr,
|
|
|
|
p2pPort: p2pPort,
|
2020-10-09 13:35:00 +02:00
|
|
|
rpcClient: client,
|
|
|
|
minerAddr: miner,
|
2022-03-10 11:32:32 +01:00
|
|
|
rpcPolling: rpcPolling,
|
2020-10-09 13:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &bd, cleanUp, nil
|
|
|
|
}
|