2017-11-17 02:37:08 +03:00
|
|
|
package lntest
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
import (
|
|
|
|
"bytes"
|
2019-09-29 01:43:42 +03:00
|
|
|
"context"
|
2017-11-03 21:52:02 +03:00
|
|
|
"encoding/hex"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
2019-11-29 18:22:38 +03:00
|
|
|
"sync/atomic"
|
2017-11-03 21:52:02 +03:00
|
|
|
"time"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2020-09-30 01:15:05 +03:00
|
|
|
"github.com/btcsuite/btcd/integration/rpctest"
|
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2018-09-07 02:49:23 +03:00
|
|
|
"github.com/btcsuite/btcutil"
|
2018-07-31 10:17:17 +03:00
|
|
|
"github.com/go-errors/errors"
|
2019-03-11 02:32:59 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chanbackup"
|
2018-07-31 10:17:17 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2019-05-06 13:19:36 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
|
2019-03-19 19:09:27 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
2020-07-28 03:25:33 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
|
2019-06-04 02:12:20 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
|
2019-06-21 02:55:13 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/watchtowerrpc"
|
2019-06-08 03:46:31 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/wtclientrpc"
|
2019-09-19 22:46:29 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
2018-07-31 10:17:17 +03:00
|
|
|
"github.com/lightningnetwork/lnd/macaroons"
|
2019-06-04 02:12:20 +03:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
2019-06-08 03:46:31 +03:00
|
|
|
"gopkg.in/macaroon.v2"
|
2017-11-03 21:52:02 +03:00
|
|
|
)
|
|
|
|
|
2019-04-15 17:03:15 +03:00
|
|
|
const (
|
2019-11-29 18:22:38 +03:00
|
|
|
// defaultNodePort is the start of the range for listening ports of
|
|
|
|
// harness nodes. Ports are monotonically increasing starting from this
|
|
|
|
// number and are determined by the results of nextAvailablePort().
|
2020-11-04 13:03:25 +03:00
|
|
|
defaultNodePort = 5555
|
2017-11-17 02:37:08 +03:00
|
|
|
|
2019-11-29 18:22:38 +03:00
|
|
|
// logPubKeyBytes is the number of bytes of the node's PubKey that will
|
|
|
|
// be appended to the log file name. The whole PubKey is too long and
|
|
|
|
// not really necessary to quickly identify what node produced which
|
|
|
|
// log file.
|
2018-04-27 11:42:04 +03:00
|
|
|
logPubKeyBytes = 4
|
|
|
|
|
2017-11-17 02:37:08 +03:00
|
|
|
// trickleDelay is the amount of time in milliseconds between each
|
|
|
|
// release of announcements by AuthenticatedGossiper to the network.
|
|
|
|
trickleDelay = 50
|
|
|
|
)
|
|
|
|
|
2019-04-15 17:03:15 +03:00
|
|
|
var (
|
|
|
|
// numActiveNodes is the number of active nodes within the test network.
|
2019-11-21 15:56:48 +03:00
|
|
|
numActiveNodes = 0
|
|
|
|
numActiveNodesMtx sync.Mutex
|
2019-04-15 17:03:15 +03:00
|
|
|
|
2019-11-29 18:22:38 +03:00
|
|
|
// lastPort is the last port determined to be free for use by a new
|
|
|
|
// node. It should be used atomically.
|
|
|
|
lastPort uint32 = defaultNodePort
|
|
|
|
|
2019-04-15 17:03:15 +03:00
|
|
|
// logOutput is a flag that can be set to append the output from the
|
|
|
|
// seed nodes to log files.
|
|
|
|
logOutput = flag.Bool("logoutput", false,
|
2019-04-15 17:28:01 +03:00
|
|
|
"log output from node n to file output-n.log")
|
|
|
|
|
2020-11-04 13:03:30 +03:00
|
|
|
// logSubDir is the default directory where the logs are written to if
|
|
|
|
// logOutput is true.
|
|
|
|
logSubDir = flag.String("logdir", ".", "default dir to write logs to")
|
|
|
|
|
2019-04-15 17:28:01 +03:00
|
|
|
// goroutineDump is a flag that can be set to dump the active
|
|
|
|
// goroutines of test nodes on failure.
|
|
|
|
goroutineDump = flag.Bool("goroutinedump", false,
|
|
|
|
"write goroutine dump from node n to file pprof-n.log")
|
2019-04-15 17:03:15 +03:00
|
|
|
)
|
|
|
|
|
2019-11-29 18:22:38 +03:00
|
|
|
// nextAvailablePort returns the first port that is available for listening by
|
|
|
|
// a new node. It panics if no port is found and the maximum available TCP port
|
|
|
|
// is reached.
|
|
|
|
func nextAvailablePort() int {
|
|
|
|
port := atomic.AddUint32(&lastPort, 1)
|
|
|
|
for port < 65535 {
|
|
|
|
// If there are no errors while attempting to listen on this
|
|
|
|
// port, close the socket and return it as available. While it
|
|
|
|
// could be the case that some other process picks up this port
|
|
|
|
// between the time the socket is closed and it's reopened in
|
|
|
|
// the harness node, in practice in CI servers this seems much
|
|
|
|
// less likely than simply some other process already being
|
|
|
|
// bound at the start of the tests.
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", port)
|
|
|
|
l, err := net.Listen("tcp4", addr)
|
|
|
|
if err == nil {
|
|
|
|
err := l.Close()
|
|
|
|
if err == nil {
|
|
|
|
return int(port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
port = atomic.AddUint32(&lastPort, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// No ports available? Must be a mistake.
|
|
|
|
panic("no ports available for listening")
|
|
|
|
}
|
2019-11-21 15:56:48 +03:00
|
|
|
|
2020-11-04 13:03:25 +03:00
|
|
|
// ApplyPortOffset adds the given offset to the lastPort variable, making it
|
|
|
|
// possible to run the tests in parallel without colliding on the same ports.
|
|
|
|
func ApplyPortOffset(offset uint32) {
|
|
|
|
_ = atomic.AddUint32(&lastPort, offset)
|
|
|
|
}
|
|
|
|
|
2020-11-04 13:03:30 +03:00
|
|
|
// GetLogDir returns the passed --logdir flag or the default value if it wasn't
|
|
|
|
// set.
|
|
|
|
func GetLogDir() string {
|
|
|
|
if logSubDir != nil && *logSubDir != "" {
|
|
|
|
return *logSubDir
|
|
|
|
}
|
|
|
|
return "."
|
|
|
|
}
|
|
|
|
|
2019-11-29 18:22:38 +03:00
|
|
|
// generateListeningPorts returns four ints representing ports to listen on
|
|
|
|
// designated for the current lightning network test. This returns the next
|
|
|
|
// available ports for the p2p, rpc, rest and profiling services.
|
|
|
|
func generateListeningPorts() (int, int, int, int) {
|
|
|
|
p2p := nextAvailablePort()
|
|
|
|
rpc := nextAvailablePort()
|
|
|
|
rest := nextAvailablePort()
|
|
|
|
profile := nextAvailablePort()
|
2019-04-15 17:03:15 +03:00
|
|
|
|
|
|
|
return p2p, rpc, rest, profile
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// BackendConfig is an interface that abstracts away the specific chain backend
|
|
|
|
// node implementation.
|
|
|
|
type BackendConfig interface {
|
|
|
|
// GenArgs returns the arguments needed to be passed to LND at startup
|
|
|
|
// for using this node as a chain backend.
|
|
|
|
GenArgs() []string
|
|
|
|
|
2019-05-24 15:17:48 +03:00
|
|
|
// ConnectMiner is called to establish a connection to the test miner.
|
|
|
|
ConnectMiner() error
|
|
|
|
|
2020-03-27 11:59:18 +03:00
|
|
|
// DisconnectMiner is called to disconnect the miner.
|
2019-05-24 15:17:48 +03:00
|
|
|
DisconnectMiner() error
|
2019-05-24 15:17:49 +03:00
|
|
|
|
|
|
|
// Name returns the name of the backend type.
|
|
|
|
Name() string
|
2018-12-14 11:24:00 +03:00
|
|
|
}
|
|
|
|
|
2019-12-17 12:48:17 +03:00
|
|
|
type NodeConfig struct {
|
2020-10-29 17:37:17 +03:00
|
|
|
Name string
|
|
|
|
|
|
|
|
// LogFilenamePrefix is is used to prefix node log files. Can be used
|
|
|
|
// to store the current test case for simpler postmortem debugging.
|
|
|
|
LogFilenamePrefix string
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
BackendCfg BackendConfig
|
|
|
|
NetParams *chaincfg.Params
|
|
|
|
BaseDir string
|
|
|
|
ExtraArgs []string
|
2017-11-04 00:06:07 +03:00
|
|
|
|
2018-03-22 01:46:37 +03:00
|
|
|
DataDir string
|
|
|
|
LogDir string
|
|
|
|
TLSCertPath string
|
|
|
|
TLSKeyPath string
|
|
|
|
AdminMacPath string
|
|
|
|
ReadMacPath string
|
|
|
|
InvoiceMacPath string
|
|
|
|
|
2019-03-11 02:32:18 +03:00
|
|
|
HasSeed bool
|
|
|
|
Password []byte
|
2018-04-03 02:57:25 +03:00
|
|
|
|
2019-04-15 17:03:15 +03:00
|
|
|
P2PPort int
|
|
|
|
RPCPort int
|
|
|
|
RESTPort int
|
|
|
|
ProfilePort int
|
2019-12-12 13:06:00 +03:00
|
|
|
|
|
|
|
AcceptKeySend bool
|
2020-09-10 16:48:39 +03:00
|
|
|
|
|
|
|
FeeURL string
|
2020-06-22 20:36:12 +03:00
|
|
|
|
|
|
|
Etcd bool
|
2017-11-04 00:06:07 +03:00
|
|
|
}
|
|
|
|
|
2019-12-17 12:48:17 +03:00
|
|
|
func (cfg NodeConfig) P2PAddr() string {
|
2017-11-04 00:06:07 +03:00
|
|
|
return net.JoinHostPort("127.0.0.1", strconv.Itoa(cfg.P2PPort))
|
|
|
|
}
|
|
|
|
|
2019-12-17 12:48:17 +03:00
|
|
|
func (cfg NodeConfig) RPCAddr() string {
|
2017-11-04 00:06:07 +03:00
|
|
|
return net.JoinHostPort("127.0.0.1", strconv.Itoa(cfg.RPCPort))
|
|
|
|
}
|
|
|
|
|
2019-12-17 12:48:17 +03:00
|
|
|
func (cfg NodeConfig) RESTAddr() string {
|
2017-12-17 20:28:38 +03:00
|
|
|
return net.JoinHostPort("127.0.0.1", strconv.Itoa(cfg.RESTPort))
|
|
|
|
}
|
|
|
|
|
2020-10-26 18:36:09 +03:00
|
|
|
// DBDir returns the holding directory path of the graph database.
|
|
|
|
func (cfg NodeConfig) DBDir() string {
|
|
|
|
return filepath.Join(cfg.DataDir, "graph", cfg.NetParams.Name)
|
|
|
|
}
|
|
|
|
|
2019-12-17 12:48:17 +03:00
|
|
|
func (cfg NodeConfig) DBPath() string {
|
2020-10-26 18:36:09 +03:00
|
|
|
return filepath.Join(cfg.DBDir(), "channel.db")
|
2017-11-04 00:06:07 +03:00
|
|
|
}
|
|
|
|
|
2019-12-17 12:48:17 +03:00
|
|
|
func (cfg NodeConfig) ChanBackupPath() string {
|
2019-03-11 02:32:59 +03:00
|
|
|
return filepath.Join(
|
|
|
|
cfg.DataDir, "chain", "bitcoin",
|
|
|
|
fmt.Sprintf(
|
|
|
|
"%v/%v", cfg.NetParams.Name,
|
|
|
|
chanbackup.DefaultBackupFileName,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-11-04 00:06:07 +03:00
|
|
|
// genArgs generates a slice of command line arguments from the lightning node
|
|
|
|
// config struct.
|
2019-12-17 12:48:17 +03:00
|
|
|
func (cfg NodeConfig) genArgs() []string {
|
2017-11-04 00:06:07 +03:00
|
|
|
var args []string
|
|
|
|
|
|
|
|
switch cfg.NetParams {
|
|
|
|
case &chaincfg.TestNet3Params:
|
|
|
|
args = append(args, "--bitcoin.testnet")
|
|
|
|
case &chaincfg.SimNetParams:
|
|
|
|
args = append(args, "--bitcoin.simnet")
|
|
|
|
case &chaincfg.RegressionNetParams:
|
|
|
|
args = append(args, "--bitcoin.regtest")
|
|
|
|
}
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
backendArgs := cfg.BackendCfg.GenArgs()
|
|
|
|
args = append(args, backendArgs...)
|
2017-11-04 00:06:07 +03:00
|
|
|
args = append(args, "--bitcoin.active")
|
|
|
|
args = append(args, "--nobootstrap")
|
|
|
|
args = append(args, "--debuglevel=debug")
|
2017-12-19 19:07:35 +03:00
|
|
|
args = append(args, "--bitcoin.defaultchanconfs=1")
|
2020-11-25 03:40:54 +03:00
|
|
|
args = append(args, fmt.Sprintf("--db.batch-commit-interval=%v", 10*time.Millisecond))
|
2018-10-29 07:52:08 +03:00
|
|
|
args = append(args, fmt.Sprintf("--bitcoin.defaultremotedelay=%v", DefaultCSV))
|
2017-12-17 20:28:38 +03:00
|
|
|
args = append(args, fmt.Sprintf("--rpclisten=%v", cfg.RPCAddr()))
|
|
|
|
args = append(args, fmt.Sprintf("--restlisten=%v", cfg.RESTAddr()))
|
2020-06-03 17:34:10 +03:00
|
|
|
args = append(args, fmt.Sprintf("--restcors=https://%v", cfg.RESTAddr()))
|
2017-12-17 20:28:38 +03:00
|
|
|
args = append(args, fmt.Sprintf("--listen=%v", cfg.P2PAddr()))
|
2018-03-31 05:39:11 +03:00
|
|
|
args = append(args, fmt.Sprintf("--externalip=%v", cfg.P2PAddr()))
|
2017-11-04 00:06:07 +03:00
|
|
|
args = append(args, fmt.Sprintf("--logdir=%v", cfg.LogDir))
|
|
|
|
args = append(args, fmt.Sprintf("--datadir=%v", cfg.DataDir))
|
|
|
|
args = append(args, fmt.Sprintf("--tlscertpath=%v", cfg.TLSCertPath))
|
|
|
|
args = append(args, fmt.Sprintf("--tlskeypath=%v", cfg.TLSKeyPath))
|
|
|
|
args = append(args, fmt.Sprintf("--configfile=%v", cfg.DataDir))
|
|
|
|
args = append(args, fmt.Sprintf("--adminmacaroonpath=%v", cfg.AdminMacPath))
|
|
|
|
args = append(args, fmt.Sprintf("--readonlymacaroonpath=%v", cfg.ReadMacPath))
|
2018-03-22 01:46:37 +03:00
|
|
|
args = append(args, fmt.Sprintf("--invoicemacaroonpath=%v", cfg.InvoiceMacPath))
|
2017-11-04 00:06:07 +03:00
|
|
|
args = append(args, fmt.Sprintf("--trickledelay=%v", trickleDelay))
|
2019-04-15 17:03:15 +03:00
|
|
|
args = append(args, fmt.Sprintf("--profile=%d", cfg.ProfilePort))
|
2020-11-30 03:14:00 +03:00
|
|
|
args = append(args, fmt.Sprintf("--protocol.legacy.no-gossip-throttle"))
|
2017-11-04 00:06:07 +03:00
|
|
|
|
2018-04-03 02:57:25 +03:00
|
|
|
if !cfg.HasSeed {
|
2018-09-05 04:49:37 +03:00
|
|
|
args = append(args, "--noseedbackup")
|
2018-04-03 02:57:25 +03:00
|
|
|
}
|
|
|
|
|
2017-11-04 00:06:07 +03:00
|
|
|
if cfg.ExtraArgs != nil {
|
|
|
|
args = append(args, cfg.ExtraArgs...)
|
|
|
|
}
|
|
|
|
|
2019-12-12 13:06:00 +03:00
|
|
|
if cfg.AcceptKeySend {
|
2020-01-16 15:13:59 +03:00
|
|
|
args = append(args, "--accept-keysend")
|
2019-12-12 13:06:00 +03:00
|
|
|
}
|
|
|
|
|
2020-06-22 20:36:12 +03:00
|
|
|
if cfg.Etcd {
|
|
|
|
args = append(args, "--db.backend=etcd")
|
|
|
|
args = append(args, "--db.etcd.embedded")
|
|
|
|
}
|
|
|
|
|
2020-09-10 16:48:39 +03:00
|
|
|
if cfg.FeeURL != "" {
|
|
|
|
args = append(args, "--feeurl="+cfg.FeeURL)
|
|
|
|
}
|
|
|
|
|
2017-11-04 00:06:07 +03:00
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
// HarnessNode represents an instance of lnd running within our test network
|
2017-12-18 05:40:05 +03:00
|
|
|
// harness. Each HarnessNode instance also fully embeds an RPC client in
|
2017-11-17 02:37:08 +03:00
|
|
|
// order to pragmatically drive the node.
|
2017-11-03 21:52:02 +03:00
|
|
|
type HarnessNode struct {
|
2019-12-17 12:48:17 +03:00
|
|
|
Cfg *NodeConfig
|
2017-11-17 02:37:08 +03:00
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
// NodeID is a unique identifier for the node within a NetworkHarness.
|
|
|
|
NodeID int
|
2017-11-17 02:37:08 +03:00
|
|
|
|
|
|
|
// PubKey is the serialized compressed identity public key of the node.
|
|
|
|
// This field will only be populated once the node itself has been
|
|
|
|
// started via the start() method.
|
|
|
|
PubKey [33]byte
|
|
|
|
PubKeyStr string
|
|
|
|
|
|
|
|
cmd *exec.Cmd
|
|
|
|
pidFile string
|
2018-01-09 14:58:13 +03:00
|
|
|
logFile *os.File
|
2017-11-17 02:37:08 +03:00
|
|
|
|
|
|
|
// processExit is a channel that's closed once it's detected that the
|
2017-11-03 21:52:02 +03:00
|
|
|
// process this instance of HarnessNode is bound to has exited.
|
2017-11-17 02:37:08 +03:00
|
|
|
processExit chan struct{}
|
|
|
|
|
|
|
|
chanWatchRequests chan *chanWatchRequest
|
|
|
|
|
2018-08-16 12:00:52 +03:00
|
|
|
// For each outpoint, we'll track an integer which denotes the number of
|
|
|
|
// edges seen for that channel within the network. When this number
|
|
|
|
// reaches 2, then it means that both edge advertisements has propagated
|
|
|
|
// through the network.
|
|
|
|
openChans map[wire.OutPoint]int
|
|
|
|
openClients map[wire.OutPoint][]chan struct{}
|
|
|
|
|
|
|
|
closedChans map[wire.OutPoint]struct{}
|
|
|
|
closeClients map[wire.OutPoint][]chan struct{}
|
|
|
|
|
2017-11-17 02:37:08 +03:00
|
|
|
quit chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
|
|
|
lnrpc.LightningClient
|
2018-04-03 02:57:25 +03:00
|
|
|
|
|
|
|
lnrpc.WalletUnlockerClient
|
2019-05-06 13:19:36 +03:00
|
|
|
|
|
|
|
invoicesrpc.InvoicesClient
|
2019-03-19 19:09:27 +03:00
|
|
|
|
2020-07-28 03:25:33 +03:00
|
|
|
// SignerClient cannot be embedded because the name collisions of the
|
|
|
|
// methods SignMessage and VerifyMessage.
|
|
|
|
SignerClient signrpc.SignerClient
|
|
|
|
|
2019-12-16 23:32:45 +03:00
|
|
|
// conn is the underlying connection to the grpc endpoint of the node.
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
|
2019-06-21 02:55:13 +03:00
|
|
|
// RouterClient, WalletKitClient, WatchtowerClient cannot be embedded,
|
|
|
|
// because a name collision would occur with LightningClient.
|
|
|
|
RouterClient routerrpc.RouterClient
|
|
|
|
WalletKitClient walletrpc.WalletKitClient
|
2019-06-08 03:46:31 +03:00
|
|
|
Watchtower watchtowerrpc.WatchtowerClient
|
|
|
|
WatchtowerClient wtclientrpc.WatchtowerClientClient
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
// Assert *HarnessNode implements the lnrpc.LightningClient interface.
|
|
|
|
var _ lnrpc.LightningClient = (*HarnessNode)(nil)
|
2018-04-03 02:57:25 +03:00
|
|
|
var _ lnrpc.WalletUnlockerClient = (*HarnessNode)(nil)
|
2019-05-06 13:19:36 +03:00
|
|
|
var _ invoicesrpc.InvoicesClient = (*HarnessNode)(nil)
|
2017-11-03 21:52:02 +03:00
|
|
|
|
|
|
|
// newNode creates a new test lightning node instance from the passed config.
|
2019-12-17 12:48:17 +03:00
|
|
|
func newNode(cfg NodeConfig) (*HarnessNode, error) {
|
2017-11-04 00:06:07 +03:00
|
|
|
if cfg.BaseDir == "" {
|
|
|
|
var err error
|
|
|
|
cfg.BaseDir, err = ioutil.TempDir("", "lndtest-node")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
2017-11-04 00:06:07 +03:00
|
|
|
cfg.DataDir = filepath.Join(cfg.BaseDir, "data")
|
|
|
|
cfg.LogDir = filepath.Join(cfg.BaseDir, "log")
|
2017-11-17 02:37:08 +03:00
|
|
|
cfg.TLSCertPath = filepath.Join(cfg.DataDir, "tls.cert")
|
|
|
|
cfg.TLSKeyPath = filepath.Join(cfg.DataDir, "tls.key")
|
2019-12-17 17:01:36 +03:00
|
|
|
|
|
|
|
networkDir := filepath.Join(
|
|
|
|
cfg.DataDir, "chain", "bitcoin", cfg.NetParams.Name,
|
|
|
|
)
|
|
|
|
cfg.AdminMacPath = filepath.Join(networkDir, "admin.macaroon")
|
|
|
|
cfg.ReadMacPath = filepath.Join(networkDir, "readonly.macaroon")
|
|
|
|
cfg.InvoiceMacPath = filepath.Join(networkDir, "invoice.macaroon")
|
2017-11-17 02:37:08 +03:00
|
|
|
|
2019-04-15 17:03:15 +03:00
|
|
|
cfg.P2PPort, cfg.RPCPort, cfg.RESTPort, cfg.ProfilePort = generateListeningPorts()
|
2017-11-17 02:37:08 +03:00
|
|
|
|
2020-01-16 15:13:59 +03:00
|
|
|
// Run all tests with accept keysend. The keysend code is very isolated
|
|
|
|
// and it is highly unlikely that it would affect regular itests when
|
|
|
|
// enabled.
|
2019-12-12 13:06:00 +03:00
|
|
|
cfg.AcceptKeySend = true
|
|
|
|
|
2019-11-21 15:56:48 +03:00
|
|
|
numActiveNodesMtx.Lock()
|
2017-11-04 00:06:07 +03:00
|
|
|
nodeNum := numActiveNodes
|
|
|
|
numActiveNodes++
|
2019-11-21 15:56:48 +03:00
|
|
|
numActiveNodesMtx.Unlock()
|
2017-11-17 02:37:08 +03:00
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
return &HarnessNode{
|
2019-12-17 12:48:17 +03:00
|
|
|
Cfg: &cfg,
|
2017-11-03 21:52:02 +03:00
|
|
|
NodeID: nodeNum,
|
2017-11-17 02:37:08 +03:00
|
|
|
chanWatchRequests: make(chan *chanWatchRequest),
|
2018-08-16 12:00:52 +03:00
|
|
|
openChans: make(map[wire.OutPoint]int),
|
|
|
|
openClients: make(map[wire.OutPoint][]chan struct{}),
|
|
|
|
|
|
|
|
closedChans: make(map[wire.OutPoint]struct{}),
|
|
|
|
closeClients: make(map[wire.OutPoint][]chan struct{}),
|
2017-11-17 02:37:08 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-09-30 01:15:05 +03:00
|
|
|
// NewMiner creates a new miner using btcd backend. The logDir specifies the
|
|
|
|
// miner node's log dir. When tests finished, during clean up, its logs are
|
|
|
|
// copied to a file specified as logFilename.
|
|
|
|
func NewMiner(logDir, logFilename string, netParams *chaincfg.Params,
|
|
|
|
handler *rpcclient.NotificationHandlers) (*rpctest.Harness,
|
|
|
|
func() error, error) {
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"--rejectnonstd",
|
|
|
|
"--txindex",
|
|
|
|
"--nowinservice",
|
|
|
|
"--nobanning",
|
|
|
|
"--debuglevel=debug",
|
|
|
|
"--logdir=" + logDir,
|
|
|
|
"--trickleinterval=100ms",
|
|
|
|
}
|
|
|
|
|
|
|
|
miner, err := rpctest.New(netParams, handler, args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf(
|
|
|
|
"unable to create mining node: %v", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanUp := func() error {
|
|
|
|
if err := miner.TearDown(); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"failed to tear down miner, got error: %s", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// After shutting down the miner, we'll make a copy of the log
|
|
|
|
// file before deleting the temporary log dir.
|
2020-11-04 13:03:30 +03:00
|
|
|
logFile := fmt.Sprintf("%s/%s/btcd.log", logDir, netParams.Name)
|
|
|
|
copyPath := fmt.Sprintf("%s/../%s", logDir, logFilename)
|
|
|
|
err := CopyFile(filepath.Clean(copyPath), logFile)
|
2020-09-30 01:15:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to copy file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.RemoveAll(logDir); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"cannot remove dir %s: %v", logDir, err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return miner, cleanUp, nil
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
// DBPath returns the filepath to the channeldb database file for this node.
|
|
|
|
func (hn *HarnessNode) DBPath() string {
|
2019-12-17 12:48:17 +03:00
|
|
|
return hn.Cfg.DBPath()
|
2017-11-03 21:52:02 +03:00
|
|
|
}
|
|
|
|
|
2020-10-26 18:36:09 +03:00
|
|
|
// DBDir returns the path for the directory holding channeldb file(s).
|
|
|
|
func (hn *HarnessNode) DBDir() string {
|
|
|
|
return hn.Cfg.DBDir()
|
|
|
|
}
|
|
|
|
|
2018-06-10 11:02:59 +03:00
|
|
|
// Name returns the name of this node set during initialization.
|
|
|
|
func (hn *HarnessNode) Name() string {
|
2019-12-17 12:48:17 +03:00
|
|
|
return hn.Cfg.Name
|
2018-06-10 11:02:59 +03:00
|
|
|
}
|
|
|
|
|
2019-05-25 07:54:55 +03:00
|
|
|
// TLSCertStr returns the path where the TLS certificate is stored.
|
|
|
|
func (hn *HarnessNode) TLSCertStr() string {
|
2019-12-17 12:48:17 +03:00
|
|
|
return hn.Cfg.TLSCertPath
|
2019-05-25 07:54:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TLSKeyStr returns the path where the TLS key is stored.
|
|
|
|
func (hn *HarnessNode) TLSKeyStr() string {
|
2019-12-17 12:48:17 +03:00
|
|
|
return hn.Cfg.TLSKeyPath
|
2019-05-25 07:54:55 +03:00
|
|
|
}
|
|
|
|
|
2019-03-11 02:32:59 +03:00
|
|
|
// ChanBackupPath returns the fielpath to the on-disk channels.backup file for
|
|
|
|
// this node.
|
|
|
|
func (hn *HarnessNode) ChanBackupPath() string {
|
2019-12-17 12:48:17 +03:00
|
|
|
return hn.Cfg.ChanBackupPath()
|
2019-03-11 02:32:59 +03:00
|
|
|
}
|
|
|
|
|
2018-05-26 11:04:09 +03:00
|
|
|
// AdminMacPath returns the filepath to the admin.macaroon file for this node.
|
|
|
|
func (hn *HarnessNode) AdminMacPath() string {
|
2019-12-17 12:48:17 +03:00
|
|
|
return hn.Cfg.AdminMacPath
|
2018-05-26 11:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadMacPath returns the filepath to the readonly.macaroon file for this node.
|
|
|
|
func (hn *HarnessNode) ReadMacPath() string {
|
2019-12-17 12:48:17 +03:00
|
|
|
return hn.Cfg.ReadMacPath
|
2018-05-26 11:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// InvoiceMacPath returns the filepath to the invoice.macaroon file for this
|
|
|
|
// node.
|
|
|
|
func (hn *HarnessNode) InvoiceMacPath() string {
|
2019-12-17 12:48:17 +03:00
|
|
|
return hn.Cfg.InvoiceMacPath
|
2018-05-26 11:04:09 +03:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:37:08 +03:00
|
|
|
// Start launches a new process running lnd. Additionally, the PID of the
|
|
|
|
// launched process is saved in order to possibly kill the process forcibly
|
|
|
|
// later.
|
2017-11-04 01:21:35 +03:00
|
|
|
//
|
|
|
|
// This may not clean up properly if an error is returned, so the caller should
|
|
|
|
// call shutdown() regardless of the return value.
|
2019-12-17 17:01:23 +03:00
|
|
|
func (hn *HarnessNode) start(lndBinary string, lndError chan<- error) error {
|
2017-11-04 01:21:35 +03:00
|
|
|
hn.quit = make(chan struct{})
|
|
|
|
|
2019-12-17 12:48:17 +03:00
|
|
|
args := hn.Cfg.genArgs()
|
2019-12-17 17:01:23 +03:00
|
|
|
hn.cmd = exec.Command(lndBinary, args...)
|
2017-11-17 02:37:08 +03:00
|
|
|
|
|
|
|
// Redirect stderr output to buffer
|
|
|
|
var errb bytes.Buffer
|
2017-11-03 21:52:02 +03:00
|
|
|
hn.cmd.Stderr = &errb
|
2017-11-17 02:37:08 +03:00
|
|
|
|
2018-04-30 07:45:36 +03:00
|
|
|
// Make sure the log file cleanup function is initialized, even
|
|
|
|
// if no log file is created.
|
|
|
|
var finalizeLogfile = func() {
|
|
|
|
if hn.logFile != nil {
|
|
|
|
hn.logFile.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:37:08 +03:00
|
|
|
// If the logoutput flag is passed, redirect output from the nodes to
|
|
|
|
// log files.
|
|
|
|
if *logOutput {
|
2020-11-04 13:03:30 +03:00
|
|
|
dir := GetLogDir()
|
2020-10-29 17:37:17 +03:00
|
|
|
fileName := fmt.Sprintf("%s/%d-%s-%s-%s.log", dir, hn.NodeID,
|
|
|
|
hn.Cfg.LogFilenamePrefix, hn.Cfg.Name,
|
|
|
|
hex.EncodeToString(hn.PubKey[:logPubKeyBytes]))
|
|
|
|
|
|
|
|
// If the node's PubKey is not yet initialized, create a
|
|
|
|
// temporary file name. Later, after the PubKey has been
|
|
|
|
// initialized, the file can be moved to its final name with
|
|
|
|
// the PubKey included.
|
2018-04-30 07:45:36 +03:00
|
|
|
if bytes.Equal(hn.PubKey[:4], []byte{0, 0, 0, 0}) {
|
2020-10-29 17:37:17 +03:00
|
|
|
fileName = fmt.Sprintf("%s/%d-%s-%s-tmp__.log", dir,
|
|
|
|
hn.NodeID, hn.Cfg.LogFilenamePrefix,
|
|
|
|
hn.Cfg.Name)
|
2018-04-30 07:45:36 +03:00
|
|
|
|
2020-10-29 17:37:17 +03:00
|
|
|
// Once the node has done its work, the log file can be
|
|
|
|
// renamed.
|
2018-04-30 07:45:36 +03:00
|
|
|
finalizeLogfile = func() {
|
|
|
|
if hn.logFile != nil {
|
|
|
|
hn.logFile.Close()
|
|
|
|
|
2020-11-04 13:03:30 +03:00
|
|
|
pubKeyHex := hex.EncodeToString(
|
|
|
|
hn.PubKey[:logPubKeyBytes],
|
|
|
|
)
|
2020-10-29 17:37:17 +03:00
|
|
|
newFileName := fmt.Sprintf("%s/"+
|
|
|
|
"%d-%s-%s-%s.log",
|
|
|
|
dir, hn.NodeID,
|
|
|
|
hn.Cfg.LogFilenamePrefix,
|
2020-11-04 13:03:30 +03:00
|
|
|
hn.Cfg.Name, pubKeyHex)
|
2018-04-30 07:45:36 +03:00
|
|
|
err := os.Rename(fileName, newFileName)
|
|
|
|
if err != nil {
|
2018-07-31 11:29:12 +03:00
|
|
|
fmt.Printf("could not rename "+
|
|
|
|
"%s to %s: %v\n",
|
|
|
|
fileName, newFileName,
|
|
|
|
err)
|
2018-04-30 07:45:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:37:08 +03:00
|
|
|
// Create file if not exists, otherwise append.
|
2018-01-09 14:58:13 +03:00
|
|
|
file, err := os.OpenFile(fileName,
|
2017-11-17 02:37:08 +03:00
|
|
|
os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass node's stderr to both errb and the file.
|
|
|
|
w := io.MultiWriter(&errb, file)
|
2017-11-03 21:52:02 +03:00
|
|
|
hn.cmd.Stderr = w
|
2017-11-17 02:37:08 +03:00
|
|
|
|
|
|
|
// Pass the node's stdout only to the file.
|
2017-11-03 21:52:02 +03:00
|
|
|
hn.cmd.Stdout = file
|
2018-01-09 14:58:13 +03:00
|
|
|
|
|
|
|
// Let the node keep a reference to this file, such
|
|
|
|
// that we can add to it if necessary.
|
|
|
|
hn.logFile = file
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
if err := hn.cmd.Start(); err != nil {
|
2017-11-17 02:37:08 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch a new goroutine which that bubbles up any potential fatal
|
|
|
|
// process errors to the goroutine running the tests.
|
2017-11-04 01:21:35 +03:00
|
|
|
hn.processExit = make(chan struct{})
|
2018-07-17 10:13:05 +03:00
|
|
|
hn.wg.Add(1)
|
2017-11-17 02:37:08 +03:00
|
|
|
go func() {
|
2018-07-17 10:13:05 +03:00
|
|
|
defer hn.wg.Done()
|
2017-11-03 21:52:02 +03:00
|
|
|
|
2018-07-17 10:13:05 +03:00
|
|
|
err := hn.cmd.Wait()
|
2017-11-17 02:37:08 +03:00
|
|
|
if err != nil {
|
|
|
|
lndError <- errors.Errorf("%v\n%v\n", err, errb.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal any onlookers that this process has exited.
|
2017-11-03 21:52:02 +03:00
|
|
|
close(hn.processExit)
|
2018-04-30 07:45:36 +03:00
|
|
|
|
|
|
|
// Make sure log file is closed and renamed if necessary.
|
|
|
|
finalizeLogfile()
|
2017-11-17 02:37:08 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Write process ID to a file.
|
2017-11-03 21:52:02 +03:00
|
|
|
if err := hn.writePidFile(); err != nil {
|
|
|
|
hn.cmd.Process.Kill()
|
2017-11-17 02:37:08 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since Stop uses the LightningClient to stop the node, if we fail to get a
|
|
|
|
// connected client, we have to kill the process.
|
2019-12-17 12:48:17 +03:00
|
|
|
useMacaroons := !hn.Cfg.HasSeed
|
2018-04-03 02:57:25 +03:00
|
|
|
conn, err := hn.ConnectRPC(useMacaroons)
|
2017-11-17 02:37:08 +03:00
|
|
|
if err != nil {
|
2017-11-03 21:52:02 +03:00
|
|
|
hn.cmd.Process.Kill()
|
2017-11-17 02:37:08 +03:00
|
|
|
return err
|
|
|
|
}
|
2018-04-03 02:57:25 +03:00
|
|
|
|
|
|
|
// If the node was created with a seed, we will need to perform an
|
|
|
|
// additional step to unlock the wallet. The connection returned will
|
|
|
|
// only use the TLS certs, and can only perform operations necessary to
|
|
|
|
// unlock the daemon.
|
2019-12-17 12:48:17 +03:00
|
|
|
if hn.Cfg.HasSeed {
|
2018-04-03 02:57:25 +03:00
|
|
|
hn.WalletUnlockerClient = lnrpc.NewWalletUnlockerClient(conn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return hn.initLightningClient(conn)
|
|
|
|
}
|
|
|
|
|
2019-03-11 02:31:05 +03:00
|
|
|
// initClientWhenReady waits until the main gRPC server is detected as active,
|
|
|
|
// then complete the normal HarnessNode gRPC connection creation. This can be
|
|
|
|
// used it a node has just been unlocked, or has its wallet state initialized.
|
|
|
|
func (hn *HarnessNode) initClientWhenReady() error {
|
|
|
|
var (
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
connErr error
|
|
|
|
)
|
2019-09-19 22:46:29 +03:00
|
|
|
if err := wait.NoError(func() error {
|
2019-03-11 02:31:05 +03:00
|
|
|
conn, connErr = hn.ConnectRPC(true)
|
|
|
|
return connErr
|
2019-03-11 02:32:18 +03:00
|
|
|
}, 5*time.Second); err != nil {
|
|
|
|
return err
|
2019-03-11 02:31:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return hn.initLightningClient(conn)
|
|
|
|
}
|
|
|
|
|
2018-04-03 02:57:25 +03:00
|
|
|
// Init initializes a harness node by passing the init request via rpc. After
|
2020-10-06 18:23:40 +03:00
|
|
|
// the request is submitted, this method will block until a
|
|
|
|
// macaroon-authenticated RPC connection can be established to the harness node.
|
2018-04-03 02:57:25 +03:00
|
|
|
// Once established, the new connection is used to initialize the
|
|
|
|
// LightningClient and subscribes the HarnessNode to topology changes.
|
|
|
|
func (hn *HarnessNode) Init(ctx context.Context,
|
2020-10-06 18:23:40 +03:00
|
|
|
initReq *lnrpc.InitWalletRequest) (*lnrpc.InitWalletResponse, error) {
|
2018-04-03 02:57:25 +03:00
|
|
|
|
2020-10-06 18:23:40 +03:00
|
|
|
ctxt, cancel := context.WithTimeout(ctx, DefaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
response, err := hn.InitWallet(ctxt, initReq)
|
2018-04-03 02:57:25 +03:00
|
|
|
if err != nil {
|
2020-10-06 18:23:40 +03:00
|
|
|
return nil, err
|
2018-04-03 02:57:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the wallet to finish unlocking, such that we can connect to
|
|
|
|
// it via a macaroon-authenticated rpc connection.
|
2020-10-06 18:23:40 +03:00
|
|
|
var conn *grpc.ClientConn
|
|
|
|
if err = wait.Predicate(func() bool {
|
|
|
|
// If the node has been initialized stateless, we need to pass
|
|
|
|
// the macaroon to the client.
|
|
|
|
if initReq.StatelessInit {
|
|
|
|
adminMac := &macaroon.Macaroon{}
|
|
|
|
err := adminMac.UnmarshalBinary(response.AdminMacaroon)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
conn, err = hn.ConnectRPCWithMacaroon(adminMac)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normal initialization, we expect a macaroon to be in the
|
|
|
|
// file system.
|
|
|
|
conn, err = hn.ConnectRPC(true)
|
|
|
|
return err == nil
|
|
|
|
}, DefaultTimeout); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, hn.initLightningClient(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitChangePassword initializes a harness node by passing the change password
|
|
|
|
// request via RPC. After the request is submitted, this method will block until
|
|
|
|
// a macaroon-authenticated RPC connection can be established to the harness
|
|
|
|
// node. Once established, the new connection is used to initialize the
|
|
|
|
// LightningClient and subscribes the HarnessNode to topology changes.
|
|
|
|
func (hn *HarnessNode) InitChangePassword(ctx context.Context,
|
|
|
|
chngPwReq *lnrpc.ChangePasswordRequest) (*lnrpc.ChangePasswordResponse,
|
|
|
|
error) {
|
|
|
|
|
|
|
|
ctxt, cancel := context.WithTimeout(ctx, DefaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
response, err := hn.ChangePassword(ctxt, chngPwReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the wallet to finish unlocking, such that we can connect to
|
|
|
|
// it via a macaroon-authenticated rpc connection.
|
|
|
|
var conn *grpc.ClientConn
|
|
|
|
if err = wait.Predicate(func() bool {
|
|
|
|
// If the node has been initialized stateless, we need to pass
|
|
|
|
// the macaroon to the client.
|
|
|
|
if chngPwReq.StatelessInit {
|
|
|
|
adminMac := &macaroon.Macaroon{}
|
|
|
|
err := adminMac.UnmarshalBinary(response.AdminMacaroon)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
conn, err = hn.ConnectRPCWithMacaroon(adminMac)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normal initialization, we expect a macaroon to be in the
|
|
|
|
// file system.
|
|
|
|
conn, err = hn.ConnectRPC(true)
|
|
|
|
return err == nil
|
|
|
|
}, DefaultTimeout); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, hn.initLightningClient(conn)
|
2019-03-11 02:31:05 +03:00
|
|
|
}
|
2019-03-11 02:32:18 +03:00
|
|
|
|
|
|
|
// Unlock attempts to unlock the wallet of the target HarnessNode. This method
|
|
|
|
// should be called after the restart of a HarnessNode that was created with a
|
|
|
|
// seed+password. Once this method returns, the HarnessNode will be ready to
|
|
|
|
// accept normal gRPC requests and harness command.
|
|
|
|
func (hn *HarnessNode) Unlock(ctx context.Context,
|
|
|
|
unlockReq *lnrpc.UnlockWalletRequest) error {
|
|
|
|
|
|
|
|
ctxt, _ := context.WithTimeout(ctx, DefaultTimeout)
|
|
|
|
|
|
|
|
// Otherwise, we'll need to unlock the node before it's able to start
|
|
|
|
// up properly.
|
|
|
|
if _, err := hn.UnlockWallet(ctxt, unlockReq); err != nil {
|
2018-04-03 02:57:25 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-11 02:32:18 +03:00
|
|
|
// Now that the wallet has been unlocked, we'll wait for the RPC client
|
|
|
|
// to be ready, then establish the normal gRPC connection.
|
|
|
|
return hn.initClientWhenReady()
|
2018-04-03 02:57:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// initLightningClient constructs the grpc LightningClient from the given client
|
|
|
|
// connection and subscribes the harness node to graph topology updates.
|
2019-01-27 18:05:30 +03:00
|
|
|
// This method also spawns a lightning network watcher for this node,
|
|
|
|
// which watches for topology changes.
|
2018-04-03 02:57:25 +03:00
|
|
|
func (hn *HarnessNode) initLightningClient(conn *grpc.ClientConn) error {
|
|
|
|
// Construct the LightningClient that will allow us to use the
|
|
|
|
// HarnessNode directly for normal rpc operations.
|
2019-12-16 23:32:45 +03:00
|
|
|
hn.conn = conn
|
2017-11-03 21:52:02 +03:00
|
|
|
hn.LightningClient = lnrpc.NewLightningClient(conn)
|
2019-05-06 13:19:36 +03:00
|
|
|
hn.InvoicesClient = invoicesrpc.NewInvoicesClient(conn)
|
2019-03-19 19:09:27 +03:00
|
|
|
hn.RouterClient = routerrpc.NewRouterClient(conn)
|
2019-06-04 02:12:20 +03:00
|
|
|
hn.WalletKitClient = walletrpc.NewWalletKitClient(conn)
|
2019-06-08 03:46:31 +03:00
|
|
|
hn.Watchtower = watchtowerrpc.NewWatchtowerClient(conn)
|
|
|
|
hn.WatchtowerClient = wtclientrpc.NewWatchtowerClientClient(conn)
|
2020-07-28 03:25:33 +03:00
|
|
|
hn.SignerClient = signrpc.NewSignerClient(conn)
|
2017-11-17 02:37:08 +03:00
|
|
|
|
2018-04-03 02:57:25 +03:00
|
|
|
// Set the harness node's pubkey to what the node claims in GetInfo.
|
|
|
|
err := hn.FetchNodeInfo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 04:14:41 +03:00
|
|
|
// Due to a race condition between the ChannelRouter starting and us
|
|
|
|
// making the subscription request, it's possible for our graph
|
|
|
|
// subscription to fail. To ensure we don't start listening for updates
|
|
|
|
// until then, we'll create a dummy subscription to ensure we can do so
|
|
|
|
// successfully before proceeding. We use a dummy subscription in order
|
|
|
|
// to not consume an update from the real one.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.NoError(func() error {
|
2019-04-26 04:14:41 +03:00
|
|
|
req := &lnrpc.GraphTopologySubscription{}
|
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
topologyClient, err := hn.SubscribeChannelGraph(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll wait to receive an error back within a one second
|
|
|
|
// timeout. This is needed since creating the client's stream is
|
|
|
|
// independent of the graph subscription being created. The
|
|
|
|
// stream is closed from the server's side upon an error.
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
if _, err := topologyClient.Recv(); err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err = <-errChan:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelFunc()
|
|
|
|
return err
|
|
|
|
}, DefaultTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-03 02:57:25 +03:00
|
|
|
// Launch the watcher that will hook into graph related topology change
|
|
|
|
// from the PoV of this node.
|
|
|
|
hn.wg.Add(1)
|
2020-04-13 19:33:48 +03:00
|
|
|
subscribed := make(chan error)
|
|
|
|
go hn.lightningNetworkWatcher(subscribed)
|
2018-04-03 02:57:25 +03:00
|
|
|
|
2020-04-13 19:33:48 +03:00
|
|
|
return <-subscribed
|
2018-04-03 02:57:25 +03:00
|
|
|
}
|
|
|
|
|
2019-01-27 18:05:30 +03:00
|
|
|
// FetchNodeInfo queries an unlocked node to retrieve its public key.
|
2018-04-03 02:57:25 +03:00
|
|
|
func (hn *HarnessNode) FetchNodeInfo() error {
|
2017-11-17 02:37:08 +03:00
|
|
|
// Obtain the lnid of this node for quick identification purposes.
|
|
|
|
ctxb := context.Background()
|
2017-11-03 21:52:02 +03:00
|
|
|
info, err := hn.GetInfo(ctxb, &lnrpc.GetInfoRequest{})
|
2017-11-17 02:37:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
hn.PubKeyStr = info.IdentityPubkey
|
2017-11-17 02:37:08 +03:00
|
|
|
|
|
|
|
pubkey, err := hex.DecodeString(info.IdentityPubkey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-03 21:52:02 +03:00
|
|
|
copy(hn.PubKey[:], pubkey)
|
2017-11-17 02:37:08 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-09 14:58:13 +03:00
|
|
|
// AddToLog adds a line of choice to the node's logfile. This is useful
|
|
|
|
// to interleave test output with output from the node.
|
|
|
|
func (hn *HarnessNode) AddToLog(line string) error {
|
|
|
|
// If this node was not set up with a log file, just return early.
|
|
|
|
if hn.logFile == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if _, err := hn.logFile.WriteString(line); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:37:08 +03:00
|
|
|
// writePidFile writes the process ID of the running lnd process to a .pid file.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (hn *HarnessNode) writePidFile() error {
|
2019-12-17 12:48:17 +03:00
|
|
|
filePath := filepath.Join(hn.Cfg.BaseDir, fmt.Sprintf("%v.pid", hn.NodeID))
|
2017-11-17 02:37:08 +03:00
|
|
|
|
|
|
|
pid, err := os.Create(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer pid.Close()
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
_, err = fmt.Fprintf(pid, "%v\n", hn.cmd.Process.Pid)
|
2017-11-17 02:37:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
hn.pidFile = filePath
|
2017-11-17 02:37:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-26 11:04:09 +03:00
|
|
|
// ReadMacaroon waits a given duration for the macaroon file to be created. If
|
|
|
|
// the file is readable within the timeout, its content is de-serialized as a
|
|
|
|
// macaroon and returned.
|
|
|
|
func (hn *HarnessNode) ReadMacaroon(macPath string, timeout time.Duration) (
|
|
|
|
*macaroon.Macaroon, error) {
|
|
|
|
|
2020-04-20 18:09:05 +03:00
|
|
|
// Wait until macaroon file is created and has valid content before
|
|
|
|
// using it.
|
|
|
|
var mac *macaroon.Macaroon
|
|
|
|
err := wait.NoError(func() error {
|
|
|
|
macBytes, err := ioutil.ReadFile(macPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error reading macaroon file: %v", err)
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
|
|
|
|
2020-04-20 18:09:05 +03:00
|
|
|
newMac := &macaroon.Macaroon{}
|
|
|
|
if err = newMac.UnmarshalBinary(macBytes); err != nil {
|
|
|
|
return fmt.Errorf("error unmarshalling macaroon "+
|
|
|
|
"file: %v", err)
|
|
|
|
}
|
|
|
|
mac = newMac
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, timeout)
|
|
|
|
|
|
|
|
return mac, err
|
2018-05-26 11:04:09 +03:00
|
|
|
}
|
2018-04-03 02:57:25 +03:00
|
|
|
|
2018-05-26 11:04:09 +03:00
|
|
|
// ConnectRPCWithMacaroon uses the TLS certificate and given macaroon to
|
|
|
|
// create a gRPC client connection.
|
|
|
|
func (hn *HarnessNode) ConnectRPCWithMacaroon(mac *macaroon.Macaroon) (
|
|
|
|
*grpc.ClientConn, error) {
|
|
|
|
|
2020-04-20 18:09:05 +03:00
|
|
|
// Wait until TLS certificate is created and has valid content before
|
|
|
|
// using it, up to 30 sec.
|
|
|
|
var tlsCreds credentials.TransportCredentials
|
|
|
|
err := wait.NoError(func() error {
|
|
|
|
var err error
|
|
|
|
tlsCreds, err = credentials.NewClientTLSFromFile(
|
|
|
|
hn.Cfg.TLSCertPath, "",
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}, DefaultTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading TLS cert: %v", err)
|
2018-04-03 02:57:25 +03:00
|
|
|
}
|
|
|
|
|
2020-04-20 18:09:05 +03:00
|
|
|
opts := []grpc.DialOption{
|
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithTransportCredentials(tlsCreds),
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
2018-04-03 02:57:25 +03:00
|
|
|
|
2020-04-09 14:29:48 +03:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2018-05-26 11:04:09 +03:00
|
|
|
if mac == nil {
|
2020-04-09 14:29:48 +03:00
|
|
|
return grpc.DialContext(ctx, hn.Cfg.RPCAddr(), opts...)
|
2018-05-26 11:04:09 +03:00
|
|
|
}
|
2018-04-03 02:57:25 +03:00
|
|
|
macCred := macaroons.NewMacaroonCredential(mac)
|
|
|
|
opts = append(opts, grpc.WithPerRPCCredentials(macCred))
|
|
|
|
|
2019-12-17 12:48:17 +03:00
|
|
|
return grpc.DialContext(ctx, hn.Cfg.RPCAddr(), opts...)
|
2018-05-26 11:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectRPC uses the TLS certificate and admin macaroon files written by the
|
|
|
|
// lnd node to create a gRPC client connection.
|
|
|
|
func (hn *HarnessNode) ConnectRPC(useMacs bool) (*grpc.ClientConn, error) {
|
|
|
|
// If we don't want to use macaroons, just pass nil, the next method
|
|
|
|
// will handle it correctly.
|
|
|
|
if !useMacs {
|
|
|
|
return hn.ConnectRPCWithMacaroon(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we should use a macaroon, always take the admin macaroon as a
|
|
|
|
// default.
|
2019-12-17 12:48:17 +03:00
|
|
|
mac, err := hn.ReadMacaroon(hn.Cfg.AdminMacPath, DefaultTimeout)
|
2018-05-26 11:04:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return hn.ConnectRPCWithMacaroon(mac)
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
|
|
|
|
2018-02-23 01:27:24 +03:00
|
|
|
// SetExtraArgs assigns the ExtraArgs field for the node's configuration. The
|
|
|
|
// changes will take effect on restart.
|
|
|
|
func (hn *HarnessNode) SetExtraArgs(extraArgs []string) {
|
2019-12-17 12:48:17 +03:00
|
|
|
hn.Cfg.ExtraArgs = extraArgs
|
2018-02-23 01:27:24 +03:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:37:08 +03:00
|
|
|
// cleanup cleans up all the temporary files created by the node's process.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (hn *HarnessNode) cleanup() error {
|
2019-12-17 12:48:17 +03:00
|
|
|
return os.RemoveAll(hn.Cfg.BaseDir)
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop attempts to stop the active lnd process.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (hn *HarnessNode) stop() error {
|
2017-11-04 01:21:35 +03:00
|
|
|
// Do nothing if the process is not running.
|
|
|
|
if hn.processExit == nil {
|
2017-11-17 02:37:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-04 01:21:35 +03:00
|
|
|
// If start() failed before creating a client, we will just wait for the
|
|
|
|
// child process to die.
|
|
|
|
if hn.LightningClient != nil {
|
|
|
|
// Don't watch for error because sometimes the RPC connection gets
|
|
|
|
// closed before a response is returned.
|
|
|
|
req := lnrpc.StopRequest{}
|
|
|
|
ctx := context.Background()
|
|
|
|
hn.LightningClient.StopDaemon(ctx, &req)
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
|
|
|
|
2017-11-04 01:21:35 +03:00
|
|
|
// Wait for lnd process and other goroutines to exit.
|
2018-04-13 10:09:26 +03:00
|
|
|
select {
|
|
|
|
case <-hn.processExit:
|
|
|
|
case <-time.After(60 * time.Second):
|
|
|
|
return fmt.Errorf("process did not exit")
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
close(hn.quit)
|
|
|
|
hn.wg.Wait()
|
2017-11-17 02:37:08 +03:00
|
|
|
|
2017-11-04 01:21:35 +03:00
|
|
|
hn.quit = nil
|
|
|
|
hn.processExit = nil
|
2017-11-03 21:52:02 +03:00
|
|
|
hn.LightningClient = nil
|
2018-04-03 02:57:25 +03:00
|
|
|
hn.WalletUnlockerClient = nil
|
2019-06-08 03:46:31 +03:00
|
|
|
hn.Watchtower = nil
|
2019-06-21 02:55:13 +03:00
|
|
|
hn.WatchtowerClient = nil
|
2019-12-16 23:32:45 +03:00
|
|
|
|
|
|
|
// Close any attempts at further grpc connections.
|
|
|
|
if hn.conn != nil {
|
|
|
|
err := hn.conn.Close()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error attempting to stop grpc client: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 01:21:35 +03:00
|
|
|
return nil
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
|
|
|
|
2017-11-04 00:40:57 +03:00
|
|
|
// shutdown stops the active lnd process and cleans up any temporary directories
|
2017-11-17 02:37:08 +03:00
|
|
|
// created along the way.
|
2017-11-04 00:40:57 +03:00
|
|
|
func (hn *HarnessNode) shutdown() error {
|
2017-11-03 21:52:02 +03:00
|
|
|
if err := hn.stop(); err != nil {
|
2017-11-17 02:37:08 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-11-03 21:52:02 +03:00
|
|
|
if err := hn.cleanup(); err != nil {
|
2017-11-17 02:37:08 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// closeChanWatchRequest is a request to the lightningNetworkWatcher to be
|
|
|
|
// notified once it's detected within the test Lightning Network, that a
|
|
|
|
// channel has either been added or closed.
|
|
|
|
type chanWatchRequest struct {
|
|
|
|
chanPoint wire.OutPoint
|
|
|
|
|
|
|
|
chanOpen bool
|
|
|
|
|
|
|
|
eventChan chan struct{}
|
|
|
|
}
|
|
|
|
|
2018-01-11 07:59:30 +03:00
|
|
|
// getChanPointFundingTxid returns the given channel point's funding txid in
|
|
|
|
// raw bytes.
|
|
|
|
func getChanPointFundingTxid(chanPoint *lnrpc.ChannelPoint) ([]byte, error) {
|
|
|
|
var txid []byte
|
|
|
|
|
|
|
|
// A channel point's funding txid can be get/set as a byte slice or a
|
|
|
|
// string. In the case it is a string, decode it.
|
|
|
|
switch chanPoint.GetFundingTxid().(type) {
|
|
|
|
case *lnrpc.ChannelPoint_FundingTxidBytes:
|
|
|
|
txid = chanPoint.GetFundingTxidBytes()
|
|
|
|
case *lnrpc.ChannelPoint_FundingTxidStr:
|
|
|
|
s := chanPoint.GetFundingTxidStr()
|
|
|
|
h, err := chainhash.NewHashFromStr(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
txid = h[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return txid, nil
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:37:08 +03:00
|
|
|
// lightningNetworkWatcher is a goroutine which is able to dispatch
|
|
|
|
// notifications once it has been observed that a target channel has been
|
|
|
|
// closed or opened within the network. In order to dispatch these
|
|
|
|
// notifications, the GraphTopologySubscription client exposed as part of the
|
|
|
|
// gRPC interface is used.
|
2020-04-13 19:33:48 +03:00
|
|
|
func (hn *HarnessNode) lightningNetworkWatcher(subscribed chan error) {
|
2017-11-03 21:52:02 +03:00
|
|
|
defer hn.wg.Done()
|
2017-11-17 02:37:08 +03:00
|
|
|
|
|
|
|
graphUpdates := make(chan *lnrpc.GraphTopologyUpdate)
|
2017-11-03 21:52:02 +03:00
|
|
|
hn.wg.Add(1)
|
2017-11-17 02:37:08 +03:00
|
|
|
go func() {
|
2017-11-03 21:52:02 +03:00
|
|
|
defer hn.wg.Done()
|
2017-11-17 02:37:08 +03:00
|
|
|
|
|
|
|
req := &lnrpc.GraphTopologySubscription{}
|
2018-04-25 06:34:30 +03:00
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
2020-04-13 19:33:48 +03:00
|
|
|
defer cancelFunc()
|
2018-04-25 06:34:30 +03:00
|
|
|
topologyClient, err := hn.SubscribeChannelGraph(ctx, req)
|
2017-11-17 02:37:08 +03:00
|
|
|
if err != nil {
|
2020-04-13 19:33:48 +03:00
|
|
|
msg := fmt.Sprintf("%s(%d): unable to create topology "+
|
|
|
|
"client: %v (%s)", hn.Name(), hn.NodeID, err,
|
|
|
|
time.Now().String())
|
|
|
|
subscribed <- fmt.Errorf(msg)
|
|
|
|
return
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
2020-04-13 19:33:48 +03:00
|
|
|
close(subscribed)
|
2018-04-25 06:34:30 +03:00
|
|
|
|
2017-11-17 02:37:08 +03:00
|
|
|
for {
|
|
|
|
update, err := topologyClient.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case graphUpdates <- update:
|
2017-11-03 21:52:02 +03:00
|
|
|
case <-hn.quit:
|
2017-11-17 02:37:08 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
// A new graph update has just been received, so we'll examine
|
|
|
|
// the current set of registered clients to see if we can
|
|
|
|
// dispatch any requests.
|
|
|
|
case graphUpdate := <-graphUpdates:
|
|
|
|
// For each new channel, we'll increment the number of
|
|
|
|
// edges seen by one.
|
|
|
|
for _, newChan := range graphUpdate.ChannelUpdates {
|
2018-01-11 07:59:30 +03:00
|
|
|
txidHash, _ := getChanPointFundingTxid(newChan.ChanPoint)
|
|
|
|
txid, _ := chainhash.NewHash(txidHash)
|
2017-11-17 02:37:08 +03:00
|
|
|
op := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: newChan.ChanPoint.OutputIndex,
|
|
|
|
}
|
2018-08-16 12:00:52 +03:00
|
|
|
hn.openChans[op]++
|
2017-11-17 02:37:08 +03:00
|
|
|
|
|
|
|
// For this new channel, if the number of edges
|
|
|
|
// seen is less than two, then the channel
|
|
|
|
// hasn't been fully announced yet.
|
2018-08-16 12:00:52 +03:00
|
|
|
if numEdges := hn.openChans[op]; numEdges < 2 {
|
2017-11-17 02:37:08 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll notify all the registered
|
|
|
|
// clients and remove the dispatched clients.
|
2018-08-16 12:00:52 +03:00
|
|
|
for _, eventChan := range hn.openClients[op] {
|
2017-11-17 02:37:08 +03:00
|
|
|
close(eventChan)
|
|
|
|
}
|
2018-08-16 12:00:52 +03:00
|
|
|
delete(hn.openClients, op)
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// For each channel closed, we'll mark that we've
|
|
|
|
// detected a channel closure while lnd was pruning the
|
|
|
|
// channel graph.
|
|
|
|
for _, closedChan := range graphUpdate.ClosedChans {
|
2018-01-11 07:59:30 +03:00
|
|
|
txidHash, _ := getChanPointFundingTxid(closedChan.ChanPoint)
|
|
|
|
txid, _ := chainhash.NewHash(txidHash)
|
2017-11-17 02:37:08 +03:00
|
|
|
op := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: closedChan.ChanPoint.OutputIndex,
|
|
|
|
}
|
2018-08-16 12:00:52 +03:00
|
|
|
hn.closedChans[op] = struct{}{}
|
2017-11-17 02:37:08 +03:00
|
|
|
|
|
|
|
// As the channel has been closed, we'll notify
|
|
|
|
// all register clients.
|
2018-08-16 12:00:52 +03:00
|
|
|
for _, eventChan := range hn.closeClients[op] {
|
2017-11-17 02:37:08 +03:00
|
|
|
close(eventChan)
|
|
|
|
}
|
2018-08-16 12:00:52 +03:00
|
|
|
delete(hn.closeClients, op)
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A new watch request, has just arrived. We'll either be able
|
|
|
|
// to dispatch immediately, or need to add the client for
|
|
|
|
// processing later.
|
2017-11-03 21:52:02 +03:00
|
|
|
case watchRequest := <-hn.chanWatchRequests:
|
2017-11-17 02:37:08 +03:00
|
|
|
targetChan := watchRequest.chanPoint
|
|
|
|
|
|
|
|
// TODO(roasbeef): add update type also, checks for
|
|
|
|
// multiple of 2
|
|
|
|
if watchRequest.chanOpen {
|
2018-04-18 05:02:04 +03:00
|
|
|
// If this is an open request, then it can be
|
2017-11-17 02:37:08 +03:00
|
|
|
// dispatched if the number of edges seen for
|
|
|
|
// the channel is at least two.
|
2018-08-16 12:00:52 +03:00
|
|
|
if numEdges := hn.openChans[targetChan]; numEdges >= 2 {
|
2017-11-17 02:37:08 +03:00
|
|
|
close(watchRequest.eventChan)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll add this to the list of
|
|
|
|
// watch open clients for this out point.
|
2018-08-16 12:00:52 +03:00
|
|
|
hn.openClients[targetChan] = append(
|
|
|
|
hn.openClients[targetChan],
|
|
|
|
watchRequest.eventChan,
|
|
|
|
)
|
2017-11-17 02:37:08 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a close request, then it can be
|
|
|
|
// immediately dispatched if we've already seen a
|
|
|
|
// channel closure for this channel.
|
2018-08-16 12:00:52 +03:00
|
|
|
if _, ok := hn.closedChans[targetChan]; ok {
|
2017-11-17 02:37:08 +03:00
|
|
|
close(watchRequest.eventChan)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll add this to the list of close watch
|
|
|
|
// clients for this out point.
|
2018-08-16 12:00:52 +03:00
|
|
|
hn.closeClients[targetChan] = append(
|
|
|
|
hn.closeClients[targetChan],
|
|
|
|
watchRequest.eventChan,
|
|
|
|
)
|
2017-11-17 02:37:08 +03:00
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
case <-hn.quit:
|
2017-11-17 02:37:08 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForNetworkChannelOpen will block until a channel with the target
|
|
|
|
// outpoint is seen as being fully advertised within the network. A channel is
|
|
|
|
// considered "fully advertised" once both of its directional edges has been
|
|
|
|
// advertised within the test Lightning Network.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (hn *HarnessNode) WaitForNetworkChannelOpen(ctx context.Context,
|
2017-11-17 02:37:08 +03:00
|
|
|
op *lnrpc.ChannelPoint) error {
|
|
|
|
|
|
|
|
eventChan := make(chan struct{})
|
|
|
|
|
2018-01-11 07:59:30 +03:00
|
|
|
txidHash, err := getChanPointFundingTxid(op)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
txid, err := chainhash.NewHash(txidHash)
|
2017-11-17 02:37:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
hn.chanWatchRequests <- &chanWatchRequest{
|
2017-11-17 02:37:08 +03:00
|
|
|
chanPoint: wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: op.OutputIndex,
|
|
|
|
},
|
|
|
|
eventChan: eventChan,
|
|
|
|
chanOpen: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-eventChan:
|
|
|
|
return nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return fmt.Errorf("channel not opened before timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForNetworkChannelClose will block until a channel with the target
|
|
|
|
// outpoint is seen as closed within the network. A channel is considered
|
|
|
|
// closed once a transaction spending the funding outpoint is seen within a
|
|
|
|
// confirmed block.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (hn *HarnessNode) WaitForNetworkChannelClose(ctx context.Context,
|
2017-11-17 02:37:08 +03:00
|
|
|
op *lnrpc.ChannelPoint) error {
|
|
|
|
|
|
|
|
eventChan := make(chan struct{})
|
|
|
|
|
2018-01-11 07:59:30 +03:00
|
|
|
txidHash, err := getChanPointFundingTxid(op)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
txid, err := chainhash.NewHash(txidHash)
|
2017-11-17 02:37:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
hn.chanWatchRequests <- &chanWatchRequest{
|
2017-11-17 02:37:08 +03:00
|
|
|
chanPoint: wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: op.OutputIndex,
|
|
|
|
},
|
|
|
|
eventChan: eventChan,
|
|
|
|
chanOpen: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-eventChan:
|
|
|
|
return nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return fmt.Errorf("channel not closed before timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForBlockchainSync will block until the target nodes has fully
|
|
|
|
// synchronized with the blockchain. If the passed context object has a set
|
|
|
|
// timeout, then the goroutine will continually poll until the timeout has
|
|
|
|
// elapsed. In the case that the chain isn't synced before the timeout is up,
|
|
|
|
// then this function will return an error.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (hn *HarnessNode) WaitForBlockchainSync(ctx context.Context) error {
|
2017-11-17 02:37:08 +03:00
|
|
|
errChan := make(chan error, 1)
|
|
|
|
retryDelay := time.Millisecond * 100
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-11-03 21:52:02 +03:00
|
|
|
case <-hn.quit:
|
2017-11-17 02:37:08 +03:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
getInfoReq := &lnrpc.GetInfoRequest{}
|
2017-11-03 21:52:02 +03:00
|
|
|
getInfoResp, err := hn.GetInfo(ctx, getInfoReq)
|
2017-11-17 02:37:08 +03:00
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if getInfoResp.SyncedToChain {
|
|
|
|
errChan <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.After(retryDelay):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2017-11-03 21:52:02 +03:00
|
|
|
case <-hn.quit:
|
2017-11-17 02:37:08 +03:00
|
|
|
return nil
|
|
|
|
case err := <-errChan:
|
|
|
|
return err
|
|
|
|
case <-ctx.Done():
|
2020-04-14 20:56:05 +03:00
|
|
|
return fmt.Errorf("timeout while waiting for blockchain sync")
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
|
|
|
}
|
2017-11-03 21:52:02 +03:00
|
|
|
|
2018-08-10 07:15:40 +03:00
|
|
|
// WaitForBalance waits until the node sees the expected confirmed/unconfirmed
|
|
|
|
// balance within their wallet.
|
2018-09-10 16:02:06 +03:00
|
|
|
func (hn *HarnessNode) WaitForBalance(expectedBalance btcutil.Amount, confirmed bool) error {
|
2018-08-10 07:15:40 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
req := &lnrpc.WalletBalanceRequest{}
|
|
|
|
|
2018-09-07 02:49:23 +03:00
|
|
|
var lastBalance btcutil.Amount
|
2018-08-10 07:15:40 +03:00
|
|
|
doesBalanceMatch := func() bool {
|
|
|
|
balance, err := hn.WalletBalance(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if confirmed {
|
2018-09-07 02:49:23 +03:00
|
|
|
lastBalance = btcutil.Amount(balance.ConfirmedBalance)
|
2018-09-10 16:02:06 +03:00
|
|
|
return btcutil.Amount(balance.ConfirmedBalance) == expectedBalance
|
2018-08-10 07:15:40 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 02:49:23 +03:00
|
|
|
lastBalance = btcutil.Amount(balance.UnconfirmedBalance)
|
2018-09-10 16:02:06 +03:00
|
|
|
return btcutil.Amount(balance.UnconfirmedBalance) == expectedBalance
|
2018-08-10 07:15:40 +03:00
|
|
|
}
|
|
|
|
|
2019-09-19 22:46:29 +03:00
|
|
|
err := wait.Predicate(doesBalanceMatch, 30*time.Second)
|
2018-08-10 07:15:40 +03:00
|
|
|
if err != nil {
|
2018-09-07 02:49:23 +03:00
|
|
|
return fmt.Errorf("balances not synced after deadline: "+
|
|
|
|
"expected %v, only have %v", expectedBalance, lastBalance)
|
2018-08-10 07:15:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|