2017-11-17 02:37:08 +03:00
|
|
|
package lntest
|
2017-06-19 16:53:52 +03:00
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
import (
|
2019-02-12 00:02:11 +03:00
|
|
|
"encoding/hex"
|
2018-03-31 08:31:37 +03:00
|
|
|
"errors"
|
2017-11-03 21:52:02 +03:00
|
|
|
"fmt"
|
2018-10-10 13:55:14 +03:00
|
|
|
"io"
|
2017-11-03 21:52:02 +03:00
|
|
|
"io/ioutil"
|
2018-10-10 13:55:14 +03:00
|
|
|
"os"
|
2018-03-14 12:18:32 +03:00
|
|
|
"strings"
|
2017-11-03 21:52:02 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/integration/rpctest"
|
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2018-07-31 10:17:17 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2018-08-22 10:32:42 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2017-11-03 21:52:02 +03:00
|
|
|
)
|
|
|
|
|
2018-10-29 07:52:08 +03:00
|
|
|
const (
|
|
|
|
// DefaultCSV is the CSV delay (remotedelay) we will start our test
|
|
|
|
// nodes with.
|
|
|
|
DefaultCSV = 4
|
|
|
|
|
|
|
|
// MinerMempoolTimeout is the max time we will wait for a transaction
|
|
|
|
// to propagate to the mining node's mempool.
|
|
|
|
MinerMempoolTimeout = time.Second * 30
|
|
|
|
|
|
|
|
// ChannelOpenTimeout is the max time we will wait before a channel to
|
|
|
|
// be considered opened.
|
|
|
|
ChannelOpenTimeout = time.Second * 30
|
|
|
|
|
|
|
|
// ChannelCloseTimeout is the max time we will wait before a channel is
|
|
|
|
// considered closed.
|
|
|
|
ChannelCloseTimeout = time.Second * 30
|
|
|
|
|
|
|
|
// DefaultTimeout is a timeout that will be used for various wait
|
|
|
|
// scenarios where no custom timeout value is defined.
|
|
|
|
DefaultTimeout = time.Second * 30
|
|
|
|
)
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
// NetworkHarness is an integration testing harness for the lightning network.
|
2016-08-30 07:55:01 +03:00
|
|
|
// The harness by default is created with two active nodes on the network:
|
|
|
|
// Alice and Bob.
|
2017-11-03 21:52:02 +03:00
|
|
|
type NetworkHarness struct {
|
2016-08-30 07:55:01 +03:00
|
|
|
netParams *chaincfg.Params
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
// Miner is a reference to a running full node that can be used to create
|
|
|
|
// new blocks on the network.
|
|
|
|
Miner *rpctest.Harness
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// BackendCfg houses the information necessary to use a node as LND
|
|
|
|
// chain backend, such as rpc configuration, P2P information etc.
|
|
|
|
BackendCfg BackendConfig
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
activeNodes map[int]*HarnessNode
|
2016-08-30 07:55:01 +03:00
|
|
|
|
2017-12-21 13:32:54 +03:00
|
|
|
nodesByPub map[string]*HarnessNode
|
|
|
|
|
2016-09-10 23:14:28 +03:00
|
|
|
// Alice and Bob are the initial seeder nodes that are automatically
|
|
|
|
// created to be the initial participants of the test network.
|
2017-11-03 21:52:02 +03:00
|
|
|
Alice *HarnessNode
|
|
|
|
Bob *HarnessNode
|
2016-08-31 05:34:13 +03:00
|
|
|
|
2017-11-17 04:31:41 +03:00
|
|
|
seenTxns chan *chainhash.Hash
|
2017-03-14 08:15:41 +03:00
|
|
|
bitcoinWatchRequests chan *txWatchRequest
|
|
|
|
|
2016-10-15 16:47:09 +03:00
|
|
|
// Channel for transmitting stderr output from failed lightning node
|
|
|
|
// to main process.
|
|
|
|
lndErrorChan chan error
|
|
|
|
|
2017-10-25 05:54:18 +03:00
|
|
|
quit chan struct{}
|
|
|
|
|
2017-11-17 04:31:41 +03:00
|
|
|
mtx sync.Mutex
|
2016-08-30 07:55:01 +03:00
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
// NewNetworkHarness creates a new network test harness.
|
2016-08-30 08:07:54 +03:00
|
|
|
// TODO(roasbeef): add option to use golang's build library to a binary of the
|
2018-02-07 06:11:11 +03:00
|
|
|
// current repo. This will save developers from having to manually `go install`
|
2016-09-15 21:59:51 +03:00
|
|
|
// within the repo each time before changes
|
2018-12-14 11:24:00 +03:00
|
|
|
func NewNetworkHarness(r *rpctest.Harness, b BackendConfig) (*NetworkHarness, error) {
|
2017-11-17 04:31:41 +03:00
|
|
|
n := NetworkHarness{
|
2017-11-03 21:52:02 +03:00
|
|
|
activeNodes: make(map[int]*HarnessNode),
|
2017-12-21 13:32:54 +03:00
|
|
|
nodesByPub: make(map[string]*HarnessNode),
|
2017-11-17 04:31:41 +03:00
|
|
|
seenTxns: make(chan *chainhash.Hash),
|
2017-03-14 08:15:41 +03:00
|
|
|
bitcoinWatchRequests: make(chan *txWatchRequest),
|
|
|
|
lndErrorChan: make(chan error),
|
2017-11-17 04:31:41 +03:00
|
|
|
netParams: r.ActiveNet,
|
|
|
|
Miner: r,
|
2018-12-14 11:24:00 +03:00
|
|
|
BackendCfg: b,
|
2017-10-25 05:54:18 +03:00
|
|
|
quit: make(chan struct{}),
|
2016-08-30 07:55:01 +03:00
|
|
|
}
|
2017-11-17 04:31:41 +03:00
|
|
|
go n.networkWatcher()
|
|
|
|
return &n, nil
|
2016-08-30 07:55:01 +03:00
|
|
|
}
|
|
|
|
|
2017-12-21 13:32:54 +03:00
|
|
|
// LookUpNodeByPub queries the set of active nodes to locate a node according
|
|
|
|
// to its public key. The second value will be true if the node was found, and
|
|
|
|
// false otherwise.
|
|
|
|
func (n *NetworkHarness) LookUpNodeByPub(pubStr string) (*HarnessNode, error) {
|
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
|
|
|
|
node, ok := n.nodesByPub[pubStr]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unable to find node")
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:00:09 +03:00
|
|
|
// ProcessErrors returns a channel used for reporting any fatal process errors.
|
|
|
|
// If any of the active nodes within the harness' test network incur a fatal
|
|
|
|
// error, that error is sent over this channel.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) ProcessErrors() <-chan error {
|
2016-10-24 05:00:09 +03:00
|
|
|
return n.lndErrorChan
|
|
|
|
}
|
|
|
|
|
2016-08-30 07:55:01 +03:00
|
|
|
// fakeLogger is a fake grpclog.Logger implementation. This is used to stop
|
|
|
|
// grpc's logger from printing directly to stdout.
|
|
|
|
type fakeLogger struct{}
|
|
|
|
|
|
|
|
func (f *fakeLogger) Fatal(args ...interface{}) {}
|
|
|
|
func (f *fakeLogger) Fatalf(format string, args ...interface{}) {}
|
|
|
|
func (f *fakeLogger) Fatalln(args ...interface{}) {}
|
|
|
|
func (f *fakeLogger) Print(args ...interface{}) {}
|
|
|
|
func (f *fakeLogger) Printf(format string, args ...interface{}) {}
|
|
|
|
func (f *fakeLogger) Println(args ...interface{}) {}
|
|
|
|
|
|
|
|
// SetUp starts the initial seeder nodes within the test harness. The initial
|
|
|
|
// node's wallets will be funded wallets with ten 1 BTC outputs each. Finally
|
|
|
|
// rpc clients capable of communicating with the initial seeder nodes are
|
2017-11-17 04:31:41 +03:00
|
|
|
// created. Nodes are initialized with the given extra command line flags, which
|
|
|
|
// should be formatted properly - "--arg=value".
|
|
|
|
func (n *NetworkHarness) SetUp(lndArgs []string) error {
|
2016-08-30 07:55:01 +03:00
|
|
|
// Swap out grpc's default logger with out fake logger which drops the
|
|
|
|
// statements on the floor.
|
|
|
|
grpclog.SetLogger(&fakeLogger{})
|
2017-10-25 05:35:54 +03:00
|
|
|
|
2016-08-30 07:55:01 +03:00
|
|
|
// Start the initial seeder nodes within the test network, then connect
|
|
|
|
// their respective RPC clients.
|
2016-08-31 02:44:47 +03:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
errChan := make(chan error, 2)
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-04-27 11:42:04 +03:00
|
|
|
node, err := n.NewNode("Alice", lndArgs)
|
2017-11-17 04:31:41 +03:00
|
|
|
if err != nil {
|
2016-08-31 02:44:47 +03:00
|
|
|
errChan <- err
|
2017-11-17 04:31:41 +03:00
|
|
|
return
|
2016-08-31 02:44:47 +03:00
|
|
|
}
|
2017-11-17 04:31:41 +03:00
|
|
|
n.Alice = node
|
2016-08-31 02:44:47 +03:00
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-04-27 11:42:04 +03:00
|
|
|
node, err := n.NewNode("Bob", lndArgs)
|
2017-11-17 04:31:41 +03:00
|
|
|
if err != nil {
|
2016-08-31 02:44:47 +03:00
|
|
|
errChan <- err
|
2017-11-17 04:31:41 +03:00
|
|
|
return
|
2016-08-31 02:44:47 +03:00
|
|
|
}
|
2017-11-17 04:31:41 +03:00
|
|
|
n.Bob = node
|
2016-08-31 02:44:47 +03:00
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
2016-08-30 07:55:01 +03:00
|
|
|
return err
|
2016-08-31 02:44:47 +03:00
|
|
|
default:
|
2016-08-30 07:55:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load up the wallets of the seeder nodes with 10 outputs of 1 BTC
|
|
|
|
// each.
|
|
|
|
ctxb := context.Background()
|
2017-02-10 02:28:32 +03:00
|
|
|
addrReq := &lnrpc.NewAddressRequest{
|
2018-09-27 16:49:44 +03:00
|
|
|
Type: lnrpc.AddressType_WITNESS_PUBKEY_HASH,
|
2017-02-10 02:28:32 +03:00
|
|
|
}
|
2016-09-10 23:14:28 +03:00
|
|
|
clients := []lnrpc.LightningClient{n.Alice, n.Bob}
|
2016-08-30 07:55:01 +03:00
|
|
|
for _, client := range clients {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
resp, err := client.NewAddress(ctxb, addrReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
addr, err := btcutil.DecodeAddress(resp.Address, n.netParams)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
addrScript, err := txscript.PayToAddrScript(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
output := &wire.TxOut{
|
|
|
|
PkScript: addrScript,
|
|
|
|
Value: btcutil.SatoshiPerBitcoin,
|
|
|
|
}
|
2018-07-28 04:20:58 +03:00
|
|
|
_, err = n.Miner.SendOutputs([]*wire.TxOut{output}, 7500)
|
|
|
|
if err != nil {
|
2016-08-30 07:55:01 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We generate several blocks in order to give the outputs created
|
|
|
|
// above a good number of confirmations.
|
|
|
|
if _, err := n.Miner.Node.Generate(10); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, make a connection between both of the nodes.
|
2016-09-26 20:31:07 +03:00
|
|
|
if err := n.ConnectNodes(ctxb, n.Alice, n.Bob); err != nil {
|
2016-08-30 07:55:01 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-31 02:44:47 +03:00
|
|
|
// Now block until both wallets have fully synced up.
|
2017-07-05 01:52:58 +03:00
|
|
|
expectedBalance := int64(btcutil.SatoshiPerBitcoin * 10)
|
2016-08-31 02:44:47 +03:00
|
|
|
balReq := &lnrpc.WalletBalanceRequest{}
|
2016-09-26 21:54:13 +03:00
|
|
|
balanceTicker := time.Tick(time.Millisecond * 50)
|
2017-08-11 00:05:04 +03:00
|
|
|
balanceTimeout := time.After(time.Second * 30)
|
2016-08-31 02:44:47 +03:00
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-balanceTicker:
|
2016-09-10 23:14:28 +03:00
|
|
|
aliceResp, err := n.Alice.WalletBalance(ctxb, balReq)
|
2016-08-31 02:44:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-10 23:14:28 +03:00
|
|
|
bobResp, err := n.Bob.WalletBalance(ctxb, balReq)
|
2016-08-31 02:44:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-26 16:17:57 +03:00
|
|
|
if aliceResp.ConfirmedBalance == expectedBalance &&
|
|
|
|
bobResp.ConfirmedBalance == expectedBalance {
|
2016-08-31 02:44:47 +03:00
|
|
|
break out
|
|
|
|
}
|
2017-08-11 00:05:04 +03:00
|
|
|
case <-balanceTimeout:
|
2016-09-18 03:34:39 +03:00
|
|
|
return fmt.Errorf("balances not synced after deadline")
|
2016-08-31 02:44:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 07:55:01 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-31 21:59:08 +03:00
|
|
|
// TearDownAll tears down all active nodes within the test lightning network.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) TearDownAll() error {
|
2017-12-21 13:32:54 +03:00
|
|
|
|
2016-08-31 21:59:08 +03:00
|
|
|
for _, node := range n.activeNodes {
|
2017-11-04 00:40:57 +03:00
|
|
|
if err := n.ShutdownNode(node); err != nil {
|
2016-08-31 21:59:08 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 05:35:54 +03:00
|
|
|
close(n.lndErrorChan)
|
2017-10-25 05:54:18 +03:00
|
|
|
close(n.quit)
|
|
|
|
|
2016-08-31 21:59:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// NewNode fully initializes a returns a new HarnessNode bound to the
|
2016-09-26 20:30:24 +03:00
|
|
|
// current instance of the network harness. The created node is running, but
|
|
|
|
// not yet connected to other nodes within the network.
|
2018-04-27 11:42:04 +03:00
|
|
|
func (n *NetworkHarness) NewNode(name string, extraArgs []string) (*HarnessNode, error) {
|
|
|
|
return n.newNode(name, extraArgs, false)
|
2018-04-03 02:57:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeWithSeed fully initializes a new HarnessNode after creating a fresh
|
|
|
|
// aezeed. The provided password is used as both the aezeed password and the
|
|
|
|
// wallet password. The generated mnemonic is returned along with the
|
|
|
|
// initialized harness node.
|
2018-04-27 11:42:04 +03:00
|
|
|
func (n *NetworkHarness) NewNodeWithSeed(name string, extraArgs []string,
|
2018-04-03 02:57:04 +03:00
|
|
|
password []byte) (*HarnessNode, []string, error) {
|
|
|
|
|
2018-04-27 11:42:04 +03:00
|
|
|
node, err := n.newNode(name, extraArgs, true)
|
2018-04-03 02:57:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := time.Duration(time.Second * 15)
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// Create a request to generate a new aezeed. The new seed will have the
|
|
|
|
// same password as the internal wallet.
|
|
|
|
genSeedReq := &lnrpc.GenSeedRequest{
|
|
|
|
AezeedPassphrase: password,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
|
|
|
genSeedResp, err := node.GenSeed(ctxt, genSeedReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the seed created, construct the init request to the node,
|
|
|
|
// including the newly generated seed.
|
|
|
|
initReq := &lnrpc.InitWalletRequest{
|
|
|
|
WalletPassword: password,
|
|
|
|
CipherSeedMnemonic: genSeedResp.CipherSeedMnemonic,
|
|
|
|
AezeedPassphrase: password,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the init request via rpc to finish unlocking the node. This will
|
|
|
|
// also initialize the macaroon-authenticated LightningClient.
|
|
|
|
err = node.Init(ctxb, initReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the node started, we can now record its public key within the
|
|
|
|
// global mapping.
|
|
|
|
n.RegisterNode(node)
|
|
|
|
|
|
|
|
return node, genSeedResp.CipherSeedMnemonic, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreNodeWithSeed fully initializes a HarnessNode using a chosen mnemonic,
|
|
|
|
// password, and recovery window. After providing the initialization request to
|
|
|
|
// unlock the node, this method will finish initializing the LightningClient
|
|
|
|
// such that the HarnessNode can be used for regular rpc operations.
|
2018-04-27 11:42:04 +03:00
|
|
|
func (n *NetworkHarness) RestoreNodeWithSeed(name string, extraArgs []string,
|
2018-04-03 02:57:04 +03:00
|
|
|
password []byte, mnemonic []string,
|
|
|
|
recoveryWindow int32) (*HarnessNode, error) {
|
|
|
|
|
2018-04-27 11:42:04 +03:00
|
|
|
node, err := n.newNode(name, extraArgs, true)
|
2018-04-03 02:57:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
initReq := &lnrpc.InitWalletRequest{
|
|
|
|
WalletPassword: password,
|
|
|
|
CipherSeedMnemonic: mnemonic,
|
|
|
|
AezeedPassphrase: password,
|
|
|
|
RecoveryWindow: recoveryWindow,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = node.Init(context.Background(), initReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the node started, we can now record its public key within the
|
|
|
|
// global mapping.
|
|
|
|
n.RegisterNode(node)
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newNode initializes a new HarnessNode, supporting the ability to initialize a
|
|
|
|
// wallet with or without a seed. If hasSeed is false, the returned harness node
|
|
|
|
// can be used immediately. Otherwise, the node will require an additional
|
|
|
|
// initialization phase where the wallet is either created or restored.
|
2018-04-30 07:45:36 +03:00
|
|
|
func (n *NetworkHarness) newNode(name string, extraArgs []string,
|
|
|
|
hasSeed bool) (*HarnessNode, error) {
|
2017-11-03 21:52:02 +03:00
|
|
|
node, err := newNode(nodeConfig{
|
2018-12-14 11:24:00 +03:00
|
|
|
Name: name,
|
|
|
|
HasSeed: hasSeed,
|
|
|
|
BackendCfg: n.BackendCfg,
|
|
|
|
NetParams: n.netParams,
|
|
|
|
ExtraArgs: extraArgs,
|
2017-11-04 00:06:07 +03:00
|
|
|
})
|
2016-09-26 20:30:24 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-25 05:39:50 +03:00
|
|
|
// Put node in activeNodes to ensure Shutdown is called even if Start
|
|
|
|
// returns an error.
|
2017-11-17 04:31:41 +03:00
|
|
|
n.mtx.Lock()
|
2017-11-03 21:52:02 +03:00
|
|
|
n.activeNodes[node.NodeID] = node
|
2017-11-17 04:31:41 +03:00
|
|
|
n.mtx.Unlock()
|
2017-10-25 05:39:50 +03:00
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
if err := node.start(n.lndErrorChan); err != nil {
|
2016-09-26 20:30:24 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-03 02:57:04 +03:00
|
|
|
// If this node is to have a seed, it will need to be unlocked or
|
|
|
|
// initialized via rpc. Delay registering it with the network until it
|
|
|
|
// can be driven via an unlocked rpc connection.
|
|
|
|
if node.cfg.HasSeed {
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2017-12-21 13:32:54 +03:00
|
|
|
// With the node started, we can now record its public key within the
|
|
|
|
// global mapping.
|
2018-04-03 02:57:04 +03:00
|
|
|
n.RegisterNode(node)
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterNode records a new HarnessNode in the NetworkHarnesses map of known
|
|
|
|
// nodes. This method should only be called with nodes that have successfully
|
|
|
|
// retrieved their public keys via FetchNodeInfo.
|
|
|
|
func (n *NetworkHarness) RegisterNode(node *HarnessNode) {
|
2017-12-21 13:32:54 +03:00
|
|
|
n.mtx.Lock()
|
|
|
|
n.nodesByPub[node.PubKeyStr] = node
|
|
|
|
n.mtx.Unlock()
|
2016-09-26 20:30:24 +03:00
|
|
|
}
|
2016-09-26 20:31:07 +03:00
|
|
|
|
2018-10-29 07:52:09 +03:00
|
|
|
func (n *NetworkHarness) connect(ctx context.Context,
|
|
|
|
req *lnrpc.ConnectPeerRequest, a *HarnessNode) error {
|
|
|
|
|
|
|
|
syncTimeout := time.After(15 * time.Second)
|
|
|
|
tryconnect:
|
|
|
|
if _, err := a.ConnectPeer(ctx, req); err != nil {
|
|
|
|
// If the chain backend is still syncing, retry.
|
|
|
|
if strings.Contains(err.Error(), "still syncing") {
|
|
|
|
select {
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
goto tryconnect
|
|
|
|
case <-syncTimeout:
|
|
|
|
return fmt.Errorf("chain backend did not " +
|
|
|
|
"finish syncing")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-14 12:18:32 +03:00
|
|
|
// EnsureConnected will try to connect to two nodes, returning no error if they
|
|
|
|
// are already connected. If the nodes were not connected previously, this will
|
|
|
|
// behave the same as ConnectNodes. If a pending connection request has already
|
|
|
|
// been made, the method will block until the two nodes appear in each other's
|
|
|
|
// peers list, or until the 15s timeout expires.
|
|
|
|
func (n *NetworkHarness) EnsureConnected(ctx context.Context, a, b *HarnessNode) error {
|
2018-03-31 08:31:37 +03:00
|
|
|
// errConnectionRequested is used to signal that a connection was
|
|
|
|
// requested successfully, which is distinct from already being
|
|
|
|
// connected to the peer.
|
|
|
|
errConnectionRequested := errors.New("connection request in progress")
|
|
|
|
|
|
|
|
tryConnect := func(a, b *HarnessNode) error {
|
|
|
|
ctxt, _ := context.WithTimeout(ctx, 15*time.Second)
|
|
|
|
bInfo, err := b.GetInfo(ctxt, &lnrpc.GetInfoRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-14 12:18:32 +03:00
|
|
|
|
2018-03-31 08:31:37 +03:00
|
|
|
req := &lnrpc.ConnectPeerRequest{
|
|
|
|
Addr: &lnrpc.LightningAddress{
|
|
|
|
Pubkey: bInfo.IdentityPubkey,
|
|
|
|
Host: b.cfg.P2PAddr(),
|
|
|
|
},
|
|
|
|
}
|
2018-03-14 12:18:32 +03:00
|
|
|
|
2018-03-31 08:31:37 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctx, 15*time.Second)
|
2018-10-29 07:52:09 +03:00
|
|
|
err = n.connect(ctxt, req, a)
|
2018-03-31 08:31:37 +03:00
|
|
|
switch {
|
2018-03-14 12:18:32 +03:00
|
|
|
|
2018-03-31 08:31:37 +03:00
|
|
|
// Request was successful, wait for both to display the
|
|
|
|
// connection.
|
|
|
|
case err == nil:
|
|
|
|
return errConnectionRequested
|
2018-03-14 12:18:32 +03:00
|
|
|
|
2018-03-31 08:31:37 +03:00
|
|
|
// If the two are already connected, we return early with no
|
|
|
|
// error.
|
|
|
|
case strings.Contains(err.Error(), "already connected to peer"):
|
|
|
|
return nil
|
2018-03-14 12:18:32 +03:00
|
|
|
|
2018-03-31 08:31:37 +03:00
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aErr := tryConnect(a, b)
|
|
|
|
bErr := tryConnect(b, a)
|
|
|
|
switch {
|
|
|
|
case aErr == nil && bErr == nil:
|
|
|
|
// If both reported already being connected to each other, we
|
|
|
|
// can exit early.
|
2018-03-14 12:18:32 +03:00
|
|
|
return nil
|
|
|
|
|
2018-03-31 08:31:37 +03:00
|
|
|
case aErr != errConnectionRequested:
|
|
|
|
// Return any critical errors returned by either alice.
|
|
|
|
return aErr
|
|
|
|
|
|
|
|
case bErr != errConnectionRequested:
|
|
|
|
// Return any critical errors returned by either bob.
|
|
|
|
return bErr
|
|
|
|
|
2018-03-14 12:18:32 +03:00
|
|
|
default:
|
2018-03-31 08:31:37 +03:00
|
|
|
// Otherwise one or both requested a connection, so we wait for
|
|
|
|
// the peers lists to reflect the connection.
|
2018-03-14 12:18:32 +03:00
|
|
|
}
|
|
|
|
|
2018-03-31 08:31:37 +03:00
|
|
|
findSelfInPeerList := func(a, b *HarnessNode) bool {
|
2018-03-14 12:18:32 +03:00
|
|
|
// If node B is seen in the ListPeers response from node A,
|
|
|
|
// then we can exit early as the connection has been fully
|
|
|
|
// established.
|
2018-03-31 08:31:37 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctx, 15*time.Second)
|
|
|
|
resp, err := b.ListPeers(ctxt, &lnrpc.ListPeersRequest{})
|
2018-03-14 12:18:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, peer := range resp.Peers {
|
2018-03-31 08:31:37 +03:00
|
|
|
if peer.PubKey == a.PubKeyStr {
|
2018-03-14 12:18:32 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2018-03-31 08:31:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err := WaitPredicate(func() bool {
|
|
|
|
return findSelfInPeerList(a, b) && findSelfInPeerList(b, a)
|
2018-03-14 12:18:32 +03:00
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("peers not connected within 15 seconds")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-26 20:31:07 +03:00
|
|
|
// ConnectNodes establishes an encrypted+authenticated p2p connection from node
|
2017-04-14 01:11:20 +03:00
|
|
|
// a towards node b. The function will return a non-nil error if the connection
|
|
|
|
// was unable to be established.
|
|
|
|
//
|
|
|
|
// NOTE: This function may block for up to 15-seconds as it will not return
|
|
|
|
// until the new connection is detected as being known to both nodes.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) ConnectNodes(ctx context.Context, a, b *HarnessNode) error {
|
2016-09-26 20:31:07 +03:00
|
|
|
bobInfo, err := b.GetInfo(ctx, &lnrpc.GetInfoRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &lnrpc.ConnectPeerRequest{
|
|
|
|
Addr: &lnrpc.LightningAddress{
|
2016-10-28 05:43:31 +03:00
|
|
|
Pubkey: bobInfo.IdentityPubkey,
|
2017-11-04 00:06:07 +03:00
|
|
|
Host: b.cfg.P2PAddr(),
|
2016-09-26 20:31:07 +03:00
|
|
|
},
|
|
|
|
}
|
2018-10-29 07:52:09 +03:00
|
|
|
|
|
|
|
if err := n.connect(ctx, req, a); err != nil {
|
2017-04-14 01:11:20 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-21 13:44:15 +03:00
|
|
|
err = WaitPredicate(func() bool {
|
2017-04-14 01:11:20 +03:00
|
|
|
// If node B is seen in the ListPeers response from node A,
|
|
|
|
// then we can exit early as the connection has been fully
|
|
|
|
// established.
|
|
|
|
resp, err := a.ListPeers(ctx, &lnrpc.ListPeersRequest{})
|
|
|
|
if err != nil {
|
2017-12-21 13:44:15 +03:00
|
|
|
return false
|
2017-04-14 01:11:20 +03:00
|
|
|
}
|
2017-12-21 13:44:15 +03:00
|
|
|
|
2017-04-14 01:11:20 +03:00
|
|
|
for _, peer := range resp.Peers {
|
|
|
|
if peer.PubKey == b.PubKeyStr {
|
2017-12-21 13:44:15 +03:00
|
|
|
return true
|
2017-04-14 01:11:20 +03:00
|
|
|
}
|
|
|
|
}
|
2017-12-21 13:44:15 +03:00
|
|
|
|
|
|
|
return false
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("peers not connected within 15 seconds")
|
2017-04-14 01:11:20 +03:00
|
|
|
}
|
2017-12-21 13:44:15 +03:00
|
|
|
|
|
|
|
return nil
|
2016-09-26 20:31:07 +03:00
|
|
|
}
|
|
|
|
|
2017-05-05 10:00:39 +03:00
|
|
|
// DisconnectNodes disconnects node a from node b by sending RPC message
|
|
|
|
// from a node to b node
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) DisconnectNodes(ctx context.Context, a, b *HarnessNode) error {
|
2017-05-05 10:00:39 +03:00
|
|
|
bobInfo, err := b.GetInfo(ctx, &lnrpc.GetInfoRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &lnrpc.DisconnectPeerRequest{
|
|
|
|
PubKey: bobInfo.IdentityPubkey,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := a.DisconnectPeer(ctx, req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-09 11:03:15 +03:00
|
|
|
// RestartNode attempts to restart a lightning node by shutting it down
|
2016-11-15 02:49:02 +03:00
|
|
|
// cleanly, then restarting the process. This function is fully blocking. Upon
|
|
|
|
// restart, the RPC connection to the node will be re-attempted, continuing iff
|
2016-11-22 06:08:44 +03:00
|
|
|
// the connection attempt is successful. If the callback parameter is non-nil,
|
|
|
|
// then the function will be executed after the node shuts down, but *before*
|
|
|
|
// the process has been started up again.
|
2016-11-15 02:49:02 +03:00
|
|
|
//
|
|
|
|
// This method can be useful when testing edge cases such as a node broadcast
|
|
|
|
// and invalidated prior state, or persistent state recovery, simulating node
|
|
|
|
// crashes, etc.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) RestartNode(node *HarnessNode, callback func() error) error {
|
2017-11-04 01:21:35 +03:00
|
|
|
if err := node.stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if callback != nil {
|
|
|
|
if err := callback(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return node.start(n.lndErrorChan)
|
2016-11-15 02:49:02 +03:00
|
|
|
}
|
|
|
|
|
2018-08-09 11:03:15 +03:00
|
|
|
// SuspendNode stops the given node and returns a callback that can be used to
|
|
|
|
// start it again.
|
|
|
|
func (n *NetworkHarness) SuspendNode(node *HarnessNode) (func() error, error) {
|
|
|
|
if err := node.stop(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
restart := func() error {
|
|
|
|
return node.start(n.lndErrorChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
return restart, nil
|
|
|
|
}
|
|
|
|
|
2017-11-04 00:40:57 +03:00
|
|
|
// ShutdownNode stops an active lnd process and returns when the process has
|
|
|
|
// exited and any temporary directories have been cleaned up.
|
|
|
|
func (n *NetworkHarness) ShutdownNode(node *HarnessNode) error {
|
|
|
|
if err := node.shutdown(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(n.activeNodes, node.NodeID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-09 06:37:04 +03:00
|
|
|
// StopNode stops the target node, but doesn't yet clean up its directories.
|
|
|
|
// This can be used to temporarily bring a node down during a test, to be later
|
|
|
|
// started up again.
|
|
|
|
func (n *NetworkHarness) StopNode(node *HarnessNode) error {
|
|
|
|
return node.stop()
|
|
|
|
}
|
|
|
|
|
2016-09-26 20:31:07 +03:00
|
|
|
// TODO(roasbeef): add a WithChannel higher-order function?
|
|
|
|
// * python-like context manager w.r.t using a channel within a test
|
|
|
|
// * possibly adds more funds to the target wallet if the funds are not
|
|
|
|
// enough
|
|
|
|
|
2017-03-14 08:15:41 +03:00
|
|
|
// txWatchRequest encapsulates a request to the harness' Bitcoin network
|
|
|
|
// watcher to dispatch a notification once a transaction with the target txid
|
|
|
|
// is seen within the test network.
|
|
|
|
type txWatchRequest struct {
|
2017-01-06 00:56:27 +03:00
|
|
|
txid chainhash.Hash
|
2016-08-31 05:34:13 +03:00
|
|
|
eventChan chan struct{}
|
|
|
|
}
|
|
|
|
|
2018-09-28 06:59:59 +03:00
|
|
|
// networkWatcher is a goroutine which accepts async notification
|
2017-03-14 08:15:41 +03:00
|
|
|
// requests for the broadcast of a target transaction, and then dispatches the
|
|
|
|
// transaction once its seen on the Bitcoin network.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) networkWatcher() {
|
2017-01-06 00:56:27 +03:00
|
|
|
seenTxns := make(map[chainhash.Hash]struct{})
|
|
|
|
clients := make(map[chainhash.Hash][]chan struct{})
|
2016-08-31 05:34:13 +03:00
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
2017-10-25 05:54:18 +03:00
|
|
|
case <-n.quit:
|
|
|
|
return
|
|
|
|
|
2017-03-14 08:15:41 +03:00
|
|
|
case req := <-n.bitcoinWatchRequests:
|
2016-08-31 05:34:13 +03:00
|
|
|
// If we've already seen this transaction, then
|
|
|
|
// immediately dispatch the request. Otherwise, append
|
|
|
|
// to the list of clients who are watching for the
|
|
|
|
// broadcast of this transaction.
|
|
|
|
if _, ok := seenTxns[req.txid]; ok {
|
|
|
|
close(req.eventChan)
|
|
|
|
} else {
|
|
|
|
clients[req.txid] = append(clients[req.txid], req.eventChan)
|
|
|
|
}
|
|
|
|
case txid := <-n.seenTxns:
|
2016-09-14 04:56:35 +03:00
|
|
|
// Add this txid to our set of "seen" transactions. So
|
|
|
|
// we're able to dispatch any notifications for this
|
|
|
|
// txid which arrive *after* it's seen within the
|
|
|
|
// network.
|
2017-11-17 04:31:41 +03:00
|
|
|
seenTxns[*txid] = struct{}{}
|
2016-08-31 05:34:13 +03:00
|
|
|
|
|
|
|
// If there isn't a registered notification for this
|
|
|
|
// transaction then ignore it.
|
2017-11-17 04:31:41 +03:00
|
|
|
txClients, ok := clients[*txid]
|
2016-08-31 05:34:13 +03:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, dispatch the notification to all clients,
|
|
|
|
// cleaning up the now un-needed state.
|
|
|
|
for _, client := range txClients {
|
|
|
|
close(client)
|
|
|
|
}
|
2017-11-17 04:31:41 +03:00
|
|
|
delete(clients, *txid)
|
2016-08-31 05:34:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 21:59:08 +03:00
|
|
|
// OnTxAccepted is a callback to be called each time a new transaction has been
|
|
|
|
// broadcast on the network.
|
2017-11-17 04:31:41 +03:00
|
|
|
func (n *NetworkHarness) OnTxAccepted(hash *chainhash.Hash) {
|
2017-10-25 05:54:18 +03:00
|
|
|
select {
|
2017-11-17 04:31:41 +03:00
|
|
|
case n.seenTxns <- hash:
|
2017-10-25 05:54:18 +03:00
|
|
|
case <-n.quit:
|
|
|
|
return
|
|
|
|
}
|
2016-08-31 05:34:13 +03:00
|
|
|
}
|
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
// WaitForTxBroadcast blocks until the target txid is seen on the network. If
|
|
|
|
// the transaction isn't seen within the network before the passed timeout,
|
2016-11-22 06:08:44 +03:00
|
|
|
// then an error is returned.
|
|
|
|
// TODO(roasbeef): add another method which creates queue of all seen transactions
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) WaitForTxBroadcast(ctx context.Context, txid chainhash.Hash) error {
|
2017-10-25 05:54:18 +03:00
|
|
|
// Return immediately if harness has been torn down.
|
|
|
|
select {
|
|
|
|
case <-n.quit:
|
2017-11-03 21:52:02 +03:00
|
|
|
return fmt.Errorf("NetworkHarness has been torn down")
|
2017-10-25 05:54:18 +03:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-08-31 05:34:13 +03:00
|
|
|
eventChan := make(chan struct{})
|
|
|
|
|
2017-03-14 08:15:41 +03:00
|
|
|
n.bitcoinWatchRequests <- &txWatchRequest{
|
|
|
|
txid: txid,
|
|
|
|
eventChan: eventChan,
|
|
|
|
}
|
2016-08-31 05:34:13 +03:00
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
select {
|
|
|
|
case <-eventChan:
|
|
|
|
return nil
|
2017-10-25 05:54:18 +03:00
|
|
|
case <-n.quit:
|
2017-11-03 21:52:02 +03:00
|
|
|
return fmt.Errorf("NetworkHarness has been torn down")
|
2016-09-18 03:34:39 +03:00
|
|
|
case <-ctx.Done():
|
|
|
|
return fmt.Errorf("tx not seen before context timeout")
|
|
|
|
}
|
2016-08-31 05:34:13 +03:00
|
|
|
}
|
|
|
|
|
2018-08-22 10:32:42 +03:00
|
|
|
// OpenChannelParams houses the params to specify when opening a new channel.
|
|
|
|
type OpenChannelParams struct {
|
|
|
|
// Amt is the local amount being put into the channel.
|
|
|
|
Amt btcutil.Amount
|
|
|
|
|
|
|
|
// PushAmt is the amount that should be pushed to the remote when the
|
|
|
|
// channel is opened.
|
|
|
|
PushAmt btcutil.Amount
|
|
|
|
|
|
|
|
// Private is a boolan indicating whether the opened channel should be
|
|
|
|
// private.
|
|
|
|
Private bool
|
|
|
|
|
|
|
|
// SpendUnconfirmed is a boolean indicating whether we can utilize
|
|
|
|
// unconfirmed outputs to fund the channel.
|
|
|
|
SpendUnconfirmed bool
|
|
|
|
|
2018-09-06 11:48:46 +03:00
|
|
|
// MinHtlc is the htlc_minimum_msat value set when opening the channel.
|
2018-08-22 10:32:42 +03:00
|
|
|
MinHtlc lnwire.MilliSatoshi
|
|
|
|
}
|
|
|
|
|
2016-10-15 16:18:38 +03:00
|
|
|
// OpenChannel attempts to open a channel between srcNode and destNode with the
|
2016-09-18 03:34:39 +03:00
|
|
|
// passed channel funding parameters. If the passed context has a timeout, then
|
2016-10-15 16:18:38 +03:00
|
|
|
// if the timeout is reached before the channel pending notification is
|
2018-08-10 07:17:06 +03:00
|
|
|
// received, an error is returned. The confirmed boolean determines whether we
|
|
|
|
// should fund the channel with confirmed outputs or not.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) OpenChannel(ctx context.Context,
|
2018-08-22 10:32:42 +03:00
|
|
|
srcNode, destNode *HarnessNode, p OpenChannelParams) (
|
|
|
|
lnrpc.Lightning_OpenChannelClient, error) {
|
2016-08-31 21:59:08 +03:00
|
|
|
|
2017-07-12 02:27:11 +03:00
|
|
|
// Wait until srcNode and destNode have the latest chain synced.
|
|
|
|
// Otherwise, we may run into a check within the funding manager that
|
|
|
|
// prevents any funding workflows from being kicked off if the chain
|
|
|
|
// isn't yet synced.
|
2017-06-19 16:53:52 +03:00
|
|
|
if err := srcNode.WaitForBlockchainSync(ctx); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to sync srcNode chain: %v", err)
|
|
|
|
}
|
|
|
|
if err := destNode.WaitForBlockchainSync(ctx); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to sync destNode chain: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-22 10:32:42 +03:00
|
|
|
minConfs := int32(1)
|
|
|
|
if p.SpendUnconfirmed {
|
|
|
|
minConfs = 0
|
2018-08-10 07:17:06 +03:00
|
|
|
}
|
|
|
|
|
2016-08-31 21:59:08 +03:00
|
|
|
openReq := &lnrpc.OpenChannelRequest{
|
2016-10-28 05:43:31 +03:00
|
|
|
NodePubkey: destNode.PubKey[:],
|
2018-08-22 10:32:42 +03:00
|
|
|
LocalFundingAmount: int64(p.Amt),
|
|
|
|
PushSat: int64(p.PushAmt),
|
|
|
|
Private: p.Private,
|
2018-08-10 07:17:06 +03:00
|
|
|
MinConfs: minConfs,
|
2018-10-17 03:02:07 +03:00
|
|
|
SpendUnconfirmed: p.SpendUnconfirmed,
|
2018-08-22 10:32:42 +03:00
|
|
|
MinHtlcMsat: int64(p.MinHtlc),
|
2016-08-31 21:59:08 +03:00
|
|
|
}
|
2016-09-15 21:59:51 +03:00
|
|
|
|
2016-08-31 21:59:08 +03:00
|
|
|
respStream, err := srcNode.OpenChannel(ctx, openReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to open channel between "+
|
|
|
|
"alice and bob: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
chanOpen := make(chan struct{})
|
|
|
|
errChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
// Consume the "channel pending" update. This waits until the node
|
|
|
|
// notifies us that the final message in the channel funding workflow
|
|
|
|
// has been sent to the remote node.
|
|
|
|
resp, err := respStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
2016-09-26 20:52:25 +03:00
|
|
|
return
|
2016-09-18 03:34:39 +03:00
|
|
|
}
|
|
|
|
if _, ok := resp.Update.(*lnrpc.OpenStatusUpdate_ChanPending); !ok {
|
|
|
|
errChan <- fmt.Errorf("expected channel pending update, "+
|
|
|
|
"instead got %v", resp)
|
2016-09-26 20:52:25 +03:00
|
|
|
return
|
2016-09-18 03:34:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
close(chanOpen)
|
|
|
|
}()
|
2016-08-31 21:59:08 +03:00
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-01-24 05:19:54 +03:00
|
|
|
return nil, fmt.Errorf("timeout reached before chan pending "+
|
|
|
|
"update sent: %v", err)
|
2016-09-18 03:34:39 +03:00
|
|
|
case err := <-errChan:
|
|
|
|
return nil, err
|
|
|
|
case <-chanOpen:
|
|
|
|
return respStream, nil
|
|
|
|
}
|
2016-08-31 21:59:08 +03:00
|
|
|
}
|
|
|
|
|
2017-01-31 07:21:52 +03:00
|
|
|
// OpenPendingChannel attempts to open a channel between srcNode and destNode with the
|
|
|
|
// passed channel funding parameters. If the passed context has a timeout, then
|
|
|
|
// if the timeout is reached before the channel pending notification is
|
|
|
|
// received, an error is returned.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) OpenPendingChannel(ctx context.Context,
|
|
|
|
srcNode, destNode *HarnessNode, amt btcutil.Amount,
|
2017-07-31 00:25:38 +03:00
|
|
|
pushAmt btcutil.Amount) (*lnrpc.PendingUpdate, error) {
|
2017-01-31 07:21:52 +03:00
|
|
|
|
2017-06-19 16:53:52 +03:00
|
|
|
// Wait until srcNode and destNode have blockchain synced
|
|
|
|
if err := srcNode.WaitForBlockchainSync(ctx); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to sync srcNode chain: %v", err)
|
|
|
|
}
|
|
|
|
if err := destNode.WaitForBlockchainSync(ctx); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to sync destNode chain: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-01-31 07:21:52 +03:00
|
|
|
openReq := &lnrpc.OpenChannelRequest{
|
|
|
|
NodePubkey: destNode.PubKey[:],
|
|
|
|
LocalFundingAmount: int64(amt),
|
|
|
|
PushSat: int64(pushAmt),
|
2017-11-14 04:35:15 +03:00
|
|
|
Private: false,
|
2017-01-31 07:21:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
respStream, err := srcNode.OpenChannel(ctx, openReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to open channel between "+
|
|
|
|
"alice and bob: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chanPending := make(chan *lnrpc.PendingUpdate)
|
|
|
|
errChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
// Consume the "channel pending" update. This waits until the node
|
|
|
|
// notifies us that the final message in the channel funding workflow
|
|
|
|
// has been sent to the remote node.
|
|
|
|
resp, err := respStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pendingResp, ok := resp.Update.(*lnrpc.OpenStatusUpdate_ChanPending)
|
|
|
|
if !ok {
|
|
|
|
errChan <- fmt.Errorf("expected channel pending update, "+
|
|
|
|
"instead got %v", resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
chanPending <- pendingResp.ChanPending
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, fmt.Errorf("timeout reached before chan pending " +
|
|
|
|
"update sent")
|
|
|
|
case err := <-errChan:
|
|
|
|
return nil, err
|
|
|
|
case pendingChan := <-chanPending:
|
|
|
|
return pendingChan, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 21:59:08 +03:00
|
|
|
// WaitForChannelOpen waits for a notification that a channel is open by
|
2016-09-18 03:34:39 +03:00
|
|
|
// consuming a message from the past open channel stream. If the passed context
|
|
|
|
// has a timeout, then if the timeout is reached before the channel has been
|
|
|
|
// opened, then an error is returned.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) WaitForChannelOpen(ctx context.Context,
|
2016-09-18 03:34:39 +03:00
|
|
|
openChanStream lnrpc.Lightning_OpenChannelClient) (*lnrpc.ChannelPoint, error) {
|
|
|
|
|
|
|
|
errChan := make(chan error)
|
|
|
|
respChan := make(chan *lnrpc.ChannelPoint)
|
|
|
|
go func() {
|
|
|
|
resp, err := openChanStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
errChan <- fmt.Errorf("unable to read rpc resp: %v", err)
|
2016-09-26 20:52:25 +03:00
|
|
|
return
|
2016-09-18 03:34:39 +03:00
|
|
|
}
|
|
|
|
fundingResp, ok := resp.Update.(*lnrpc.OpenStatusUpdate_ChanOpen)
|
|
|
|
if !ok {
|
|
|
|
errChan <- fmt.Errorf("expected channel open update, "+
|
|
|
|
"instead got %v", resp)
|
2016-09-26 20:52:25 +03:00
|
|
|
return
|
2016-09-18 03:34:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
respChan <- fundingResp.ChanOpen.ChannelPoint
|
|
|
|
}()
|
2016-08-31 21:59:08 +03:00
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, fmt.Errorf("timeout reached while waiting for " +
|
|
|
|
"channel open")
|
|
|
|
case err := <-errChan:
|
|
|
|
return nil, err
|
|
|
|
case chanPoint := <-respChan:
|
|
|
|
return chanPoint, nil
|
|
|
|
}
|
2016-08-31 21:59:08 +03:00
|
|
|
}
|
|
|
|
|
2018-09-28 06:59:59 +03:00
|
|
|
// CloseChannel attempts to close the channel indicated by the
|
2016-09-18 03:34:39 +03:00
|
|
|
// passed channel point, initiated by the passed lnNode. If the passed context
|
2018-09-28 06:59:59 +03:00
|
|
|
// has a timeout, an error is returned if that timeout is reached before the
|
|
|
|
// channel close is pending.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) CloseChannel(ctx context.Context,
|
|
|
|
lnNode *HarnessNode, cp *lnrpc.ChannelPoint,
|
2017-01-06 00:56:27 +03:00
|
|
|
force bool) (lnrpc.Lightning_CloseChannelClient, *chainhash.Hash, error) {
|
2016-08-31 21:59:08 +03:00
|
|
|
|
2017-11-19 03:38:30 +03:00
|
|
|
// Create a channel outpoint that we can use to compare to channels
|
|
|
|
// from the ListChannelsResponse.
|
2018-01-11 07:59:30 +03:00
|
|
|
txidHash, err := getChanPointFundingTxid(cp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
fundingTxID, err := chainhash.NewHash(txidHash)
|
2017-11-19 03:38:30 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
chanPoint := wire.OutPoint{
|
|
|
|
Hash: *fundingTxID,
|
|
|
|
Index: cp.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2017-12-21 13:43:07 +03:00
|
|
|
// We'll wait for *both* nodes to read the channel as active if we're
|
|
|
|
// performing a cooperative channel closure.
|
|
|
|
if !force {
|
|
|
|
timeout := time.Second * 15
|
2017-11-19 03:38:30 +03:00
|
|
|
listReq := &lnrpc.ListChannelsRequest{}
|
2017-12-21 13:43:07 +03:00
|
|
|
|
|
|
|
// We define two helper functions, one two locate a particular
|
|
|
|
// channel, and the other to check if a channel is active or
|
|
|
|
// not.
|
|
|
|
filterChannel := func(node *HarnessNode,
|
2018-03-13 22:11:30 +03:00
|
|
|
op wire.OutPoint) (*lnrpc.Channel, error) {
|
2017-12-21 13:43:07 +03:00
|
|
|
listResp, err := node.ListChannels(ctx, listReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range listResp.Channels {
|
|
|
|
if c.ChannelPoint == op.String() {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unable to find channel")
|
2017-11-19 03:38:30 +03:00
|
|
|
}
|
2017-12-21 13:43:07 +03:00
|
|
|
activeChanPredicate := func(node *HarnessNode) func() bool {
|
|
|
|
return func() bool {
|
|
|
|
channel, err := filterChannel(node, chanPoint)
|
|
|
|
if err != nil {
|
2018-04-18 15:19:58 +03:00
|
|
|
return false
|
2017-12-21 13:43:07 +03:00
|
|
|
}
|
2017-11-19 03:38:30 +03:00
|
|
|
|
2017-12-21 13:43:07 +03:00
|
|
|
return channel.Active
|
2017-11-19 03:38:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 13:43:07 +03:00
|
|
|
// Next, we'll fetch the target channel in order to get the
|
2018-02-07 06:11:11 +03:00
|
|
|
// harness node that will be receiving the channel close request.
|
2017-12-21 13:43:07 +03:00
|
|
|
targetChan, err := filterChannel(lnNode, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
receivingNode, err := n.LookUpNodeByPub(targetChan.RemotePubkey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2017-11-19 03:38:30 +03:00
|
|
|
}
|
|
|
|
|
2017-12-21 13:43:07 +03:00
|
|
|
// Before proceeding, we'll ensure that the channel is active
|
|
|
|
// for both nodes.
|
|
|
|
err = WaitPredicate(activeChanPredicate(lnNode), timeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("channel of closing " +
|
|
|
|
"node not active in time")
|
|
|
|
}
|
|
|
|
err = WaitPredicate(activeChanPredicate(receivingNode), timeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("channel of receiving " +
|
|
|
|
"node not active in time")
|
|
|
|
}
|
2017-11-19 03:38:30 +03:00
|
|
|
}
|
|
|
|
|
2016-08-31 21:59:08 +03:00
|
|
|
closeReq := &lnrpc.CloseChannelRequest{
|
2016-09-12 22:33:22 +03:00
|
|
|
ChannelPoint: cp,
|
|
|
|
Force: force,
|
2016-08-31 21:59:08 +03:00
|
|
|
}
|
|
|
|
closeRespStream, err := lnNode.CloseChannel(ctx, closeReq)
|
|
|
|
if err != nil {
|
2016-12-14 02:32:44 +03:00
|
|
|
return nil, nil, fmt.Errorf("unable to close channel: %v", err)
|
2016-08-31 21:59:08 +03:00
|
|
|
}
|
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
errChan := make(chan error)
|
2017-01-06 00:56:27 +03:00
|
|
|
fin := make(chan *chainhash.Hash)
|
2016-09-18 03:34:39 +03:00
|
|
|
go func() {
|
|
|
|
// Consume the "channel close" update in order to wait for the closing
|
|
|
|
// transaction to be broadcast, then wait for the closing tx to be seen
|
|
|
|
// within the network.
|
|
|
|
closeResp, err := closeRespStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pendingClose, ok := closeResp.Update.(*lnrpc.CloseStatusUpdate_ClosePending)
|
|
|
|
if !ok {
|
|
|
|
errChan <- fmt.Errorf("expected channel close update, "+
|
|
|
|
"instead got %v", pendingClose)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-06 00:56:27 +03:00
|
|
|
closeTxid, err := chainhash.NewHash(pendingClose.ClosePending.Txid)
|
2016-09-18 03:34:39 +03:00
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := n.WaitForTxBroadcast(ctx, *closeTxid); err != nil {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
2016-12-14 02:32:44 +03:00
|
|
|
fin <- closeTxid
|
2016-09-18 03:34:39 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait until either the deadline for the context expires, an error
|
|
|
|
// occurs, or the channel close update is received.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2016-12-14 02:32:44 +03:00
|
|
|
return nil, nil, fmt.Errorf("timeout reached before channel close " +
|
2016-09-18 03:34:39 +03:00
|
|
|
"initiated")
|
|
|
|
case err := <-errChan:
|
2016-12-14 02:32:44 +03:00
|
|
|
return nil, nil, err
|
|
|
|
case closeTxid := <-fin:
|
|
|
|
return closeRespStream, closeTxid, nil
|
2016-09-14 04:56:35 +03:00
|
|
|
}
|
2016-08-31 21:59:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForChannelClose waits for a notification from the passed channel close
|
2016-09-18 03:34:39 +03:00
|
|
|
// stream that the node has deemed the channel has been fully closed. If the
|
|
|
|
// passed context has a timeout, then if the timeout is reached before the
|
|
|
|
// notification is received then an error is returned.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) WaitForChannelClose(ctx context.Context,
|
2017-01-06 00:56:27 +03:00
|
|
|
closeChanStream lnrpc.Lightning_CloseChannelClient) (*chainhash.Hash, error) {
|
2016-09-18 03:34:39 +03:00
|
|
|
|
|
|
|
errChan := make(chan error)
|
|
|
|
updateChan := make(chan *lnrpc.CloseStatusUpdate_ChanClose)
|
|
|
|
go func() {
|
|
|
|
closeResp, err := closeChanStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
2016-08-31 21:59:08 +03:00
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
closeFin, ok := closeResp.Update.(*lnrpc.CloseStatusUpdate_ChanClose)
|
|
|
|
if !ok {
|
|
|
|
errChan <- fmt.Errorf("expected channel close update, "+
|
|
|
|
"instead got %v", closeFin)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updateChan <- closeFin
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait until either the deadline for the context expires, an error
|
|
|
|
// occurs, or the channel close update is received.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, fmt.Errorf("timeout reached before update sent")
|
|
|
|
case err := <-errChan:
|
|
|
|
return nil, err
|
|
|
|
case update := <-updateChan:
|
2017-01-06 00:56:27 +03:00
|
|
|
return chainhash.NewHash(update.ChanClose.ClosingTxid)
|
2016-09-18 03:34:39 +03:00
|
|
|
}
|
2016-08-31 21:59:08 +03:00
|
|
|
}
|
|
|
|
|
2018-05-23 08:45:55 +03:00
|
|
|
// AssertChannelExists asserts that an active channel identified by the
|
|
|
|
// specified channel point exists from the point-of-view of the node.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) AssertChannelExists(ctx context.Context,
|
|
|
|
node *HarnessNode, chanPoint *wire.OutPoint) error {
|
2016-08-31 21:59:08 +03:00
|
|
|
|
2016-09-26 20:31:07 +03:00
|
|
|
req := &lnrpc.ListChannelsRequest{}
|
2017-12-21 13:35:23 +03:00
|
|
|
|
|
|
|
var predErr error
|
|
|
|
pred := func() bool {
|
|
|
|
resp, err := node.ListChannels(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable fetch node's channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, channel := range resp.Channels {
|
|
|
|
if channel.ChannelPoint == chanPoint.String() {
|
2018-05-23 08:45:55 +03:00
|
|
|
return channel.Active
|
2017-12-21 13:35:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := WaitPredicate(pred, time.Second*15); err != nil {
|
|
|
|
return fmt.Errorf("channel not found: %v", predErr)
|
2016-08-31 21:59:08 +03:00
|
|
|
}
|
|
|
|
|
2017-12-21 13:35:23 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-21 13:33:28 +03:00
|
|
|
// WaitPredicate is a helper test function that will wait for a timeout period
|
|
|
|
// of time until the passed predicate returns true. This function is helpful as
|
|
|
|
// timing doesn't always line up well when running integration tests with
|
|
|
|
// several running lnd nodes. This function gives callers a way to assert that
|
|
|
|
// some property is upheld within a particular time frame.
|
|
|
|
func WaitPredicate(pred func() bool, timeout time.Duration) error {
|
2018-06-11 16:58:23 +03:00
|
|
|
const pollInterval = 20 * time.Millisecond
|
|
|
|
|
2017-12-21 13:33:28 +03:00
|
|
|
exitTimer := time.After(timeout)
|
|
|
|
for {
|
2018-06-11 16:58:23 +03:00
|
|
|
<-time.After(pollInterval)
|
|
|
|
|
2017-12-21 13:33:28 +03:00
|
|
|
select {
|
|
|
|
case <-exitTimer:
|
|
|
|
return fmt.Errorf("predicate not satisfied after time out")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if pred() {
|
2016-09-26 20:31:07 +03:00
|
|
|
return nil
|
2016-09-06 22:04:08 +03:00
|
|
|
}
|
2016-08-30 07:55:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 15:38:05 +03:00
|
|
|
// WaitNoError is a wrapper around WaitPredicate that waits for the passed
|
|
|
|
// method f to execute without error, and returns the last error encountered if
|
|
|
|
// this doesn't happen within the timeout.
|
|
|
|
func WaitNoError(f func() error, timeout time.Duration) error {
|
|
|
|
var predErr error
|
|
|
|
pred := func() bool {
|
|
|
|
if err := f(); err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If f() doesn't succeed within the timeout, return the last
|
|
|
|
// encountered error.
|
|
|
|
if err := WaitPredicate(pred, timeout); err != nil {
|
|
|
|
return predErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:09:49 +03:00
|
|
|
// WaitInvariant is a helper test function that will wait for a timeout period
|
|
|
|
// of time, verifying that a statement remains true for the entire duration.
|
|
|
|
// This function is helpful as timing doesn't always line up well when running
|
|
|
|
// integration tests with several running lnd nodes. This function gives callers
|
|
|
|
// a way to assert that some property is maintained over a particular time
|
|
|
|
// frame.
|
|
|
|
func WaitInvariant(statement func() bool, timeout time.Duration) error {
|
|
|
|
const pollInterval = 20 * time.Millisecond
|
|
|
|
|
|
|
|
exitTimer := time.After(timeout)
|
|
|
|
for {
|
|
|
|
<-time.After(pollInterval)
|
|
|
|
|
|
|
|
// Fail if the invariant is broken while polling.
|
|
|
|
if !statement() {
|
|
|
|
return fmt.Errorf("invariant broken before time out")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-exitTimer:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 23:14:28 +03:00
|
|
|
// DumpLogs reads the current logs generated by the passed node, and returns
|
|
|
|
// the logs as a single string. This function is useful for examining the logs
|
|
|
|
// of a particular node in the case of a test failure.
|
2016-09-15 21:59:51 +03:00
|
|
|
// Logs from lightning node being generated with delay - you should
|
|
|
|
// add time.Sleep() in order to get all logs.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) DumpLogs(node *HarnessNode) (string, error) {
|
2016-09-10 23:14:28 +03:00
|
|
|
logFile := fmt.Sprintf("%v/simnet/lnd.log", node.cfg.LogDir)
|
|
|
|
|
2016-09-15 21:59:51 +03:00
|
|
|
buf, err := ioutil.ReadFile(logFile)
|
2016-08-30 07:55:01 +03:00
|
|
|
if err != nil {
|
2016-09-10 23:14:28 +03:00
|
|
|
return "", err
|
2016-08-30 07:55:01 +03:00
|
|
|
}
|
|
|
|
|
2016-09-15 21:59:51 +03:00
|
|
|
return string(buf), nil
|
2016-08-30 07:55:01 +03:00
|
|
|
}
|
2016-09-26 20:31:07 +03:00
|
|
|
|
2017-01-12 01:21:04 +03:00
|
|
|
// SendCoins attempts to send amt satoshis from the internal mining node to the
|
2018-08-10 07:16:20 +03:00
|
|
|
// targeted lightning node using a P2WKH address. 6 blocks are mined after in
|
|
|
|
// order to confirm the transaction.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (n *NetworkHarness) SendCoins(ctx context.Context, amt btcutil.Amount,
|
|
|
|
target *HarnessNode) error {
|
2016-09-26 20:31:07 +03:00
|
|
|
|
2018-04-03 02:57:04 +03:00
|
|
|
return n.sendCoins(
|
2018-09-27 16:49:44 +03:00
|
|
|
ctx, amt, target, lnrpc.AddressType_WITNESS_PUBKEY_HASH,
|
2018-08-10 07:16:20 +03:00
|
|
|
true,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendCoinsUnconfirmed sends coins from the internal mining node to the target
|
|
|
|
// lightning node using a P2WPKH address. No blocks are mined after, so the
|
|
|
|
// transaction remains unconfirmed.
|
|
|
|
func (n *NetworkHarness) SendCoinsUnconfirmed(ctx context.Context,
|
|
|
|
amt btcutil.Amount, target *HarnessNode) error {
|
|
|
|
|
|
|
|
return n.sendCoins(
|
2018-09-27 16:49:44 +03:00
|
|
|
ctx, amt, target, lnrpc.AddressType_WITNESS_PUBKEY_HASH,
|
2018-08-10 07:16:20 +03:00
|
|
|
false,
|
2018-04-03 02:57:04 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendCoinsNP2WKH attempts to send amt satoshis from the internal mining node
|
|
|
|
// to the targeted lightning node using a NP2WKH address.
|
|
|
|
func (n *NetworkHarness) SendCoinsNP2WKH(ctx context.Context,
|
|
|
|
amt btcutil.Amount, target *HarnessNode) error {
|
|
|
|
|
|
|
|
return n.sendCoins(
|
2018-09-27 16:49:44 +03:00
|
|
|
ctx, amt, target, lnrpc.AddressType_NESTED_PUBKEY_HASH,
|
2018-08-10 07:16:20 +03:00
|
|
|
true,
|
2018-04-03 02:57:04 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendCoins attempts to send amt satoshis from the internal mining node to the
|
2018-08-10 07:16:20 +03:00
|
|
|
// targeted lightning node. The confirmed boolean indicates whether the
|
|
|
|
// transaction that pays to the target should confirm.
|
2018-04-03 02:57:04 +03:00
|
|
|
func (n *NetworkHarness) sendCoins(ctx context.Context, amt btcutil.Amount,
|
2018-09-27 16:49:44 +03:00
|
|
|
target *HarnessNode, addrType lnrpc.AddressType,
|
2018-08-10 07:16:20 +03:00
|
|
|
confirmed bool) error {
|
2018-04-03 02:57:04 +03:00
|
|
|
|
2016-09-26 21:54:13 +03:00
|
|
|
balReq := &lnrpc.WalletBalanceRequest{}
|
|
|
|
initialBalance, err := target.WalletBalance(ctx, balReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-26 20:31:07 +03:00
|
|
|
// First, obtain an address from the target lightning node, preferring
|
|
|
|
// to receive a p2wkh address s.t the output can immediately be used as
|
|
|
|
// an input to a funding transaction.
|
|
|
|
addrReq := &lnrpc.NewAddressRequest{
|
2018-04-03 02:57:04 +03:00
|
|
|
Type: addrType,
|
2016-09-26 20:31:07 +03:00
|
|
|
}
|
|
|
|
resp, err := target.NewAddress(ctx, addrReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
addr, err := btcutil.DecodeAddress(resp.Address, n.netParams)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
addrScript, err := txscript.PayToAddrScript(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a transaction which creates an output to the target
|
|
|
|
// pkScript of the desired amount.
|
|
|
|
output := &wire.TxOut{
|
|
|
|
PkScript: addrScript,
|
|
|
|
Value: int64(amt),
|
|
|
|
}
|
2018-07-28 04:20:58 +03:00
|
|
|
_, err = n.Miner.SendOutputs([]*wire.TxOut{output}, 7500)
|
|
|
|
if err != nil {
|
2016-09-26 20:31:07 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-12 00:02:11 +03:00
|
|
|
// Encode the pkScript in hex as this the format that it will be
|
|
|
|
// returned via rpc.
|
|
|
|
expPkScriptStr := hex.EncodeToString(addrScript)
|
|
|
|
|
|
|
|
// Now, wait for ListUnspent to show the unconfirmed transaction
|
|
|
|
// containing the correct pkscript.
|
|
|
|
err = WaitNoError(func() error {
|
|
|
|
req := &lnrpc.ListUnspentRequest{}
|
|
|
|
resp, err := target.ListUnspent(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// When using this method, there should only ever be on
|
|
|
|
// unconfirmed transaction.
|
|
|
|
if len(resp.Utxos) != 1 {
|
|
|
|
return fmt.Errorf("number of unconfirmed utxos "+
|
|
|
|
"should be 1, found %d", len(resp.Utxos))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that the lone unconfirmed utxo contains the same
|
|
|
|
// pkscript as the output generated above.
|
2019-02-12 00:02:25 +03:00
|
|
|
pkScriptStr := resp.Utxos[0].PkScript
|
2019-02-12 00:02:11 +03:00
|
|
|
if strings.Compare(pkScriptStr, expPkScriptStr) != 0 {
|
|
|
|
return fmt.Errorf("pkscript mismatch, want: %s, "+
|
|
|
|
"found: %s", expPkScriptStr, pkScriptStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, 15*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unconfirmed utxo was not found in "+
|
|
|
|
"ListUnspent: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-10 07:16:20 +03:00
|
|
|
// If the transaction should remain unconfirmed, then we'll wait until
|
|
|
|
// the target node's unconfirmed balance reflects the expected balance
|
|
|
|
// and exit.
|
|
|
|
if !confirmed {
|
2018-09-10 16:02:06 +03:00
|
|
|
expectedBalance := btcutil.Amount(initialBalance.UnconfirmedBalance) + amt
|
2018-08-10 07:16:20 +03:00
|
|
|
return target.WaitForBalance(expectedBalance, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll generate 6 new blocks to ensure the output gains a
|
|
|
|
// sufficient number of confirmations and wait for the balance to
|
|
|
|
// reflect what's expected.
|
2016-09-26 20:31:07 +03:00
|
|
|
if _, err := n.Miner.Node.Generate(6); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-10 16:02:06 +03:00
|
|
|
expectedBalance := btcutil.Amount(initialBalance.ConfirmedBalance) + amt
|
2018-08-10 07:15:40 +03:00
|
|
|
return target.WaitForBalance(expectedBalance, true)
|
2016-09-26 20:31:07 +03:00
|
|
|
}
|
2018-10-10 13:55:14 +03:00
|
|
|
|
|
|
|
// CopyFile copies the file src to dest.
|
|
|
|
func CopyFile(dest, src string) error {
|
|
|
|
s, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
d, err := os.Create(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.Copy(d, s); err != nil {
|
|
|
|
d.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.Close()
|
|
|
|
}
|