2017-11-17 02:37:08 +03:00
|
|
|
package lntest
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
2018-01-16 19:18:41 +03:00
|
|
|
macaroon "gopkg.in/macaroon.v2"
|
2017-11-03 21:52:02 +03:00
|
|
|
|
|
|
|
"github.com/go-errors/errors"
|
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
|
|
|
"github.com/lightningnetwork/lnd/macaroons"
|
|
|
|
"github.com/roasbeef/btcd/chaincfg"
|
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/roasbeef/btcd/rpcclient"
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
)
|
|
|
|
|
2017-11-17 02:37:08 +03:00
|
|
|
var (
|
|
|
|
// numActiveNodes is the number of active nodes within the test network.
|
|
|
|
numActiveNodes = 0
|
|
|
|
|
|
|
|
// defaultNodePort is the initial p2p port which will be used by the
|
|
|
|
// first created lightning node to listen on for incoming p2p
|
2018-02-07 06:11:11 +03:00
|
|
|
// connections. Subsequent allocated ports for future Lightning nodes
|
2017-12-17 20:28:38 +03:00
|
|
|
// instances will be monotonically increasing numbers calculated as
|
|
|
|
// such: defaultP2pPort + (3 * harness.nodeNum).
|
2017-11-17 02:37:08 +03:00
|
|
|
defaultNodePort = 19555
|
|
|
|
|
|
|
|
// defaultClientPort is the initial rpc port which will be used by the
|
|
|
|
// first created lightning node to listen on for incoming rpc
|
|
|
|
// connections. Subsequent allocated ports for future rpc harness
|
2017-12-17 20:28:38 +03:00
|
|
|
// instances will be monotonically increasing numbers calculated
|
|
|
|
// as such: defaultP2pPort + (3 * harness.nodeNum).
|
2017-11-17 02:37:08 +03:00
|
|
|
defaultClientPort = 19556
|
|
|
|
|
2017-12-17 20:28:38 +03:00
|
|
|
// defaultRestPort is the initial rest port which will be used by the
|
|
|
|
// first created lightning node to listen on for incoming rest
|
|
|
|
// connections. Subsequent allocated ports for future rpc harness
|
|
|
|
// instances will be monotonically increasing numbers calculated
|
|
|
|
// as such: defaultP2pPort + (3 * harness.nodeNum).
|
|
|
|
defaultRestPort = 19557
|
|
|
|
|
2017-11-17 02:37:08 +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,
|
|
|
|
"log output from node n to file outputn.log")
|
|
|
|
|
|
|
|
// trickleDelay is the amount of time in milliseconds between each
|
|
|
|
// release of announcements by AuthenticatedGossiper to the network.
|
|
|
|
trickleDelay = 50
|
|
|
|
)
|
|
|
|
|
2017-12-17 20:28:38 +03:00
|
|
|
// generateListeningPorts returns three ints representing ports to listen on
|
2017-11-17 02:37:08 +03:00
|
|
|
// designated for the current lightning network test. If there haven't been any
|
|
|
|
// test instances created, the default ports are used. Otherwise, in order to
|
2017-12-17 20:28:38 +03:00
|
|
|
// support multiple test nodes running at once, the p2p, rpc, and rest ports
|
|
|
|
// are incremented after each initialization.
|
|
|
|
func generateListeningPorts() (int, int, int) {
|
|
|
|
var p2p, rpc, rest int
|
2017-11-17 02:37:08 +03:00
|
|
|
if numActiveNodes == 0 {
|
|
|
|
p2p = defaultNodePort
|
|
|
|
rpc = defaultClientPort
|
2017-12-17 20:28:38 +03:00
|
|
|
rest = defaultRestPort
|
2017-11-17 02:37:08 +03:00
|
|
|
} else {
|
2017-12-17 20:28:38 +03:00
|
|
|
p2p = defaultNodePort + (3 * numActiveNodes)
|
|
|
|
rpc = defaultClientPort + (3 * numActiveNodes)
|
|
|
|
rest = defaultRestPort + (3 * numActiveNodes)
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
|
|
|
|
2017-12-17 20:28:38 +03:00
|
|
|
return p2p, rpc, rest
|
2017-11-17 02:37:08 +03:00
|
|
|
}
|
|
|
|
|
2017-11-04 00:06:07 +03:00
|
|
|
type nodeConfig struct {
|
|
|
|
RPCConfig *rpcclient.ConnConfig
|
|
|
|
NetParams *chaincfg.Params
|
|
|
|
BaseDir string
|
|
|
|
ExtraArgs []string
|
|
|
|
|
2018-03-22 01:46:37 +03:00
|
|
|
DataDir string
|
|
|
|
LogDir string
|
|
|
|
TLSCertPath string
|
|
|
|
TLSKeyPath string
|
|
|
|
AdminMacPath string
|
|
|
|
ReadMacPath string
|
|
|
|
InvoiceMacPath string
|
|
|
|
|
|
|
|
P2PPort int
|
|
|
|
RPCPort int
|
|
|
|
RESTPort int
|
2017-11-04 00:06:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg nodeConfig) P2PAddr() string {
|
|
|
|
return net.JoinHostPort("127.0.0.1", strconv.Itoa(cfg.P2PPort))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg nodeConfig) RPCAddr() string {
|
|
|
|
return net.JoinHostPort("127.0.0.1", strconv.Itoa(cfg.RPCPort))
|
|
|
|
}
|
|
|
|
|
2017-12-17 20:28:38 +03:00
|
|
|
func (cfg nodeConfig) RESTAddr() string {
|
|
|
|
return net.JoinHostPort("127.0.0.1", strconv.Itoa(cfg.RESTPort))
|
|
|
|
}
|
|
|
|
|
2017-11-04 00:06:07 +03:00
|
|
|
func (cfg nodeConfig) DBPath() string {
|
2018-01-06 00:44:04 +03:00
|
|
|
return filepath.Join(cfg.DataDir, "graph", "simnet/channel.db")
|
2017-11-04 00:06:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// genArgs generates a slice of command line arguments from the lightning node
|
|
|
|
// config struct.
|
|
|
|
func (cfg nodeConfig) genArgs() []string {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedCert := hex.EncodeToString(cfg.RPCConfig.Certificates)
|
|
|
|
args = append(args, "--bitcoin.active")
|
|
|
|
args = append(args, "--nobootstrap")
|
|
|
|
args = append(args, "--noencryptwallet")
|
|
|
|
args = append(args, "--debuglevel=debug")
|
2017-12-19 19:07:35 +03:00
|
|
|
args = append(args, "--bitcoin.defaultchanconfs=1")
|
|
|
|
args = append(args, "--bitcoin.defaultremotedelay=4")
|
2017-12-22 08:23:24 +03:00
|
|
|
args = append(args, fmt.Sprintf("--btcd.rpchost=%v", cfg.RPCConfig.Host))
|
|
|
|
args = append(args, fmt.Sprintf("--btcd.rpcuser=%v", cfg.RPCConfig.User))
|
|
|
|
args = append(args, fmt.Sprintf("--btcd.rpcpass=%v", cfg.RPCConfig.Pass))
|
|
|
|
args = append(args, fmt.Sprintf("--btcd.rawrpccert=%v", encodedCert))
|
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()))
|
|
|
|
args = append(args, fmt.Sprintf("--listen=%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("--externalip=%s", cfg.P2PAddr()))
|
|
|
|
args = append(args, fmt.Sprintf("--trickledelay=%v", trickleDelay))
|
|
|
|
|
|
|
|
if cfg.ExtraArgs != nil {
|
|
|
|
args = append(args, cfg.ExtraArgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-11-04 00:06:07 +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
|
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
|
|
|
lnrpc.LightningClient
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
// Assert *HarnessNode implements the lnrpc.LightningClient interface.
|
|
|
|
var _ lnrpc.LightningClient = (*HarnessNode)(nil)
|
|
|
|
|
|
|
|
// newNode creates a new test lightning node instance from the passed config.
|
|
|
|
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")
|
|
|
|
cfg.AdminMacPath = filepath.Join(cfg.DataDir, "admin.macaroon")
|
|
|
|
cfg.ReadMacPath = filepath.Join(cfg.DataDir, "readonly.macaroon")
|
2018-03-22 01:46:37 +03:00
|
|
|
cfg.InvoiceMacPath = filepath.Join(cfg.DataDir, "invoice.macaroon")
|
2017-11-17 02:37:08 +03:00
|
|
|
|
2017-12-17 20:28:38 +03:00
|
|
|
cfg.P2PPort, cfg.RPCPort, cfg.RESTPort = generateListeningPorts()
|
2017-11-17 02:37:08 +03:00
|
|
|
|
2017-11-04 00:06:07 +03:00
|
|
|
nodeNum := numActiveNodes
|
|
|
|
numActiveNodes++
|
2017-11-17 02:37:08 +03:00
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
return &HarnessNode{
|
2017-11-04 00:06:07 +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),
|
|
|
|
}, 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 {
|
|
|
|
return hn.cfg.DBPath()
|
|
|
|
}
|
|
|
|
|
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.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (hn *HarnessNode) start(lndError chan<- error) error {
|
2017-11-04 01:21:35 +03:00
|
|
|
hn.quit = make(chan struct{})
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
args := hn.cfg.genArgs()
|
2018-02-03 06:00:15 +03:00
|
|
|
args = append(args, fmt.Sprintf("--profile=%d", 9000+hn.NodeID))
|
2018-03-29 09:13:43 +03:00
|
|
|
hn.cmd = exec.Command("./lnd", 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
|
|
|
|
|
|
|
// If the logoutput flag is passed, redirect output from the nodes to
|
|
|
|
// log files.
|
|
|
|
if *logOutput {
|
2018-01-09 14:58:13 +03:00
|
|
|
fileName := fmt.Sprintf("output%d.log", hn.NodeID)
|
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{})
|
2017-11-17 02:37:08 +03:00
|
|
|
go func() {
|
2017-11-03 21:52:02 +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)
|
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.
|
2017-11-03 21:52:02 +03:00
|
|
|
conn, err := hn.connectRPC()
|
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
|
|
|
|
}
|
2017-11-03 21:52:02 +03:00
|
|
|
hn.LightningClient = lnrpc.NewLightningClient(conn)
|
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
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// Launch the watcher that will hook into graph related topology change
|
2017-11-17 02:37:08 +03:00
|
|
|
// from the PoV of this node.
|
2017-11-03 21:52:02 +03:00
|
|
|
hn.wg.Add(1)
|
|
|
|
go hn.lightningNetworkWatcher()
|
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 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// connectRPC uses the TLS certificate and admin macaroon files written by the
|
|
|
|
// lnd node to create a gRPC client connection.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (hn *HarnessNode) connectRPC() (*grpc.ClientConn, error) {
|
2017-11-17 02:37:08 +03:00
|
|
|
// Wait until TLS certificate and admin macaroon are created before
|
|
|
|
// using them, up to 20 sec.
|
|
|
|
tlsTimeout := time.After(30 * time.Second)
|
2017-11-03 21:52:02 +03:00
|
|
|
for !fileExists(hn.cfg.TLSCertPath) || !fileExists(hn.cfg.AdminMacPath) {
|
2017-11-17 02:37:08 +03:00
|
|
|
select {
|
|
|
|
case <-tlsTimeout:
|
|
|
|
return nil, fmt.Errorf("timeout waiting for TLS cert file " +
|
|
|
|
"and admin macaroon file to be created after " +
|
|
|
|
"20 seconds")
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
tlsCreds, err := credentials.NewClientTLSFromFile(hn.cfg.TLSCertPath, "")
|
2017-11-17 02:37:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-03 21:52:02 +03:00
|
|
|
macBytes, err := ioutil.ReadFile(hn.cfg.AdminMacPath)
|
2017-11-17 02:37:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mac := &macaroon.Macaroon{}
|
|
|
|
if err = mac.UnmarshalBinary(macBytes); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
opts := []grpc.DialOption{
|
|
|
|
grpc.WithTransportCredentials(tlsCreds),
|
|
|
|
grpc.WithPerRPCCredentials(macaroons.NewMacaroonCredential(mac)),
|
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithTimeout(time.Second * 20),
|
|
|
|
}
|
2017-11-03 21:52:02 +03:00
|
|
|
return grpc.Dial(hn.cfg.RPCAddr(), opts...)
|
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) {
|
|
|
|
hn.cfg.ExtraArgs = extraArgs
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
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.
|
|
|
|
<-hn.processExit
|
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
|
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.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (hn *HarnessNode) lightningNetworkWatcher() {
|
|
|
|
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
|
|
|
|
|
|
|
ctxb := context.Background()
|
|
|
|
req := &lnrpc.GraphTopologySubscription{}
|
2017-11-03 21:52:02 +03:00
|
|
|
topologyClient, err := hn.SubscribeChannelGraph(ctxb, req)
|
2017-11-17 02:37:08 +03:00
|
|
|
if err != nil {
|
|
|
|
// We panic here in case of an error as failure to
|
|
|
|
// create the topology client will cause all subsequent
|
|
|
|
// tests to fail.
|
|
|
|
panic(fmt.Errorf("unable to create topology "+
|
|
|
|
"client: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
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 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 := 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{})
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
openChans[op]++
|
|
|
|
|
|
|
|
// For this new channel, if the number of edges
|
|
|
|
// seen is less than two, then the channel
|
|
|
|
// hasn't been fully announced yet.
|
|
|
|
if numEdges := openChans[op]; numEdges < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll notify all the registered
|
|
|
|
// clients and remove the dispatched clients.
|
|
|
|
for _, eventChan := range openClients[op] {
|
|
|
|
close(eventChan)
|
|
|
|
}
|
|
|
|
delete(openClients, op)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
closedChans[op] = struct{}{}
|
|
|
|
|
|
|
|
// As the channel has been closed, we'll notify
|
|
|
|
// all register clients.
|
|
|
|
for _, eventChan := range closeClients[op] {
|
|
|
|
close(eventChan)
|
|
|
|
}
|
|
|
|
delete(closeClients, op)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
// If this is a open request, then it can be
|
|
|
|
// dispatched if the number of edges seen for
|
|
|
|
// the channel is at least two.
|
|
|
|
if numEdges := openChans[targetChan]; numEdges >= 2 {
|
|
|
|
close(watchRequest.eventChan)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll add this to the list of
|
|
|
|
// watch open clients for this out point.
|
|
|
|
openClients[targetChan] = append(openClients[targetChan],
|
|
|
|
watchRequest.eventChan)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a close request, then it can be
|
|
|
|
// immediately dispatched if we've already seen a
|
|
|
|
// channel closure for this channel.
|
|
|
|
if _, ok := closedChans[targetChan]; ok {
|
|
|
|
close(watchRequest.eventChan)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll add this to the list of close watch
|
|
|
|
// clients for this out point.
|
|
|
|
closeClients[targetChan] = append(closeClients[targetChan],
|
|
|
|
watchRequest.eventChan)
|
|
|
|
|
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():
|
|
|
|
return fmt.Errorf("Timeout while waiting for blockchain sync")
|
|
|
|
}
|
|
|
|
}
|
2017-11-03 21:52:02 +03:00
|
|
|
|
|
|
|
// fileExists reports whether the named file or directory exists.
|
|
|
|
// This function is taken from https://github.com/btcsuite/btcd
|
|
|
|
func fileExists(name string) bool {
|
|
|
|
if _, err := os.Stat(name); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|