2017-07-05 02:05:05 +03:00
|
|
|
// +build rpctest
|
|
|
|
|
2016-08-30 08:07:54 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2016-11-22 06:16:44 +03:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-01-08 07:25:45 +03:00
|
|
|
"strings"
|
2016-09-26 20:41:57 +03:00
|
|
|
"sync"
|
2016-10-24 05:00:09 +03:00
|
|
|
"testing"
|
2016-09-14 05:00:47 +03:00
|
|
|
"time"
|
2016-10-24 05:00:09 +03:00
|
|
|
|
2016-11-16 23:42:58 +03:00
|
|
|
"sync/atomic"
|
|
|
|
|
2016-12-31 03:41:59 +03:00
|
|
|
"encoding/hex"
|
|
|
|
"reflect"
|
|
|
|
|
2017-07-31 06:24:48 +03:00
|
|
|
"crypto/rand"
|
|
|
|
prand "math/rand"
|
2017-05-17 19:06:52 +03:00
|
|
|
|
2017-06-25 15:05:26 +03:00
|
|
|
"github.com/btcsuite/btclog"
|
2016-09-26 20:40:58 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2016-10-15 16:47:09 +03:00
|
|
|
"github.com/go-errors/errors"
|
2016-09-15 22:24:52 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2016-11-24 11:49:18 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2017-01-06 00:56:27 +03:00
|
|
|
"github.com/roasbeef/btcd/chaincfg"
|
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2017-08-25 04:54:17 +03:00
|
|
|
"github.com/roasbeef/btcd/integration/rpctest"
|
|
|
|
"github.com/roasbeef/btcd/rpcclient"
|
2016-08-30 08:07:54 +03:00
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
"github.com/roasbeef/btcutil"
|
2016-10-24 05:00:09 +03:00
|
|
|
"golang.org/x/net/context"
|
2016-10-15 16:24:56 +03:00
|
|
|
"google.golang.org/grpc"
|
2016-08-30 08:07:54 +03:00
|
|
|
)
|
|
|
|
|
2016-10-26 15:28:05 +03:00
|
|
|
// harnessTest wraps a regular testing.T providing enhanced error detection
|
|
|
|
// and propagation. All error will be augmented with a full stack-trace in
|
2017-01-13 08:01:50 +03:00
|
|
|
// order to aid in debugging. Additionally, any panics caused by active
|
2016-10-26 15:28:05 +03:00
|
|
|
// test cases will also be handled and represented as fatals.
|
2016-10-24 05:00:09 +03:00
|
|
|
type harnessTest struct {
|
2016-10-26 15:28:05 +03:00
|
|
|
t *testing.T
|
2016-10-15 16:47:09 +03:00
|
|
|
|
2016-10-26 15:28:05 +03:00
|
|
|
// testCase is populated during test execution and represents the
|
|
|
|
// current test case.
|
|
|
|
testCase *testCase
|
2016-10-15 16:47:09 +03:00
|
|
|
}
|
|
|
|
|
2016-10-24 05:00:09 +03:00
|
|
|
// newHarnessTest creates a new instance of a harnessTest from a regular
|
|
|
|
// testing.T instance.
|
|
|
|
func newHarnessTest(t *testing.T) *harnessTest {
|
|
|
|
return &harnessTest{t, nil}
|
2016-10-15 16:47:09 +03:00
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// Fatalf causes the current active test case to fail with a fatal error. All
|
|
|
|
// integration tests should mark test failures solely with this method due to
|
2016-10-24 05:00:09 +03:00
|
|
|
// the error stack traces it produces.
|
|
|
|
func (h *harnessTest) Fatalf(format string, a ...interface{}) {
|
2016-10-26 15:28:05 +03:00
|
|
|
stacktrace := errors.Wrap(fmt.Sprintf(format, a...), 1).ErrorStack()
|
2016-10-24 05:00:09 +03:00
|
|
|
|
2016-10-26 15:28:05 +03:00
|
|
|
if h.testCase != nil {
|
2016-11-16 23:42:58 +03:00
|
|
|
h.t.Fatalf("Failed: (%v): exited with error: \n"+
|
2016-10-26 15:28:05 +03:00
|
|
|
"%v", h.testCase.name, stacktrace)
|
|
|
|
} else {
|
|
|
|
h.t.Fatalf("Error outside of test: %v", stacktrace)
|
2016-10-15 16:47:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// RunTestCase executes a harness test case. Any errors or panics will be
|
2016-10-26 15:28:05 +03:00
|
|
|
// represented as fatal.
|
|
|
|
func (h *harnessTest) RunTestCase(testCase *testCase, net *networkHarness) {
|
|
|
|
h.testCase = testCase
|
|
|
|
defer func() {
|
|
|
|
h.testCase = nil
|
|
|
|
}()
|
2016-10-24 05:00:09 +03:00
|
|
|
|
2016-10-26 15:28:05 +03:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
description := errors.Wrap(err, 2).ErrorStack()
|
|
|
|
h.t.Fatalf("Failed: (%v) paniced with: \n%v",
|
|
|
|
h.testCase.name, description)
|
|
|
|
}
|
|
|
|
}()
|
2016-10-24 05:00:09 +03:00
|
|
|
|
2016-10-26 15:28:05 +03:00
|
|
|
testCase.test(net, h)
|
2017-06-19 16:53:52 +03:00
|
|
|
|
2016-10-26 15:28:05 +03:00
|
|
|
return
|
|
|
|
}
|
2016-10-24 05:00:09 +03:00
|
|
|
|
2016-10-26 15:28:05 +03:00
|
|
|
func (h *harnessTest) Logf(format string, args ...interface{}) {
|
|
|
|
h.t.Logf(format, args...)
|
|
|
|
}
|
2016-10-15 16:47:09 +03:00
|
|
|
|
2016-10-26 15:28:05 +03:00
|
|
|
func (h *harnessTest) Log(args ...interface{}) {
|
|
|
|
h.t.Log(args...)
|
2016-10-15 16:47:09 +03:00
|
|
|
}
|
|
|
|
|
2017-01-06 00:56:27 +03:00
|
|
|
func assertTxInBlock(t *harnessTest, block *wire.MsgBlock, txid *chainhash.Hash) {
|
2016-12-13 02:55:36 +03:00
|
|
|
for _, tx := range block.Transactions {
|
2017-01-06 00:56:27 +03:00
|
|
|
sha := tx.TxHash()
|
2016-12-13 02:55:36 +03:00
|
|
|
if bytes.Equal(txid[:], sha[:]) {
|
2016-08-30 08:07:54 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("funding tx was not included in block")
|
2016-08-30 08:07:54 +03:00
|
|
|
}
|
|
|
|
|
2016-10-15 16:24:56 +03:00
|
|
|
// mineBlocks mine 'num' of blocks and check that blocks are present in
|
|
|
|
// node blockchain.
|
2016-12-13 02:55:36 +03:00
|
|
|
func mineBlocks(t *harnessTest, net *networkHarness, num uint32) []*wire.MsgBlock {
|
|
|
|
blocks := make([]*wire.MsgBlock, num)
|
2016-10-15 16:24:56 +03:00
|
|
|
|
|
|
|
blockHashes, err := net.Miner.Node.Generate(num)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, blockHash := range blockHashes {
|
|
|
|
block, err := net.Miner.Node.GetBlock(blockHash)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to get block: %v", err)
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
blocks[i] = block
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocks
|
|
|
|
}
|
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
// openChannelAndAssert attempts to open a channel with the specified
|
|
|
|
// parameters extended from Alice to Bob. Additionally, two items are asserted
|
2016-10-15 16:47:09 +03:00
|
|
|
// after the channel is considered open: the funding transaction should be
|
2016-09-18 03:34:39 +03:00
|
|
|
// found within a block, and that Alice can report the status of the new
|
|
|
|
// channel.
|
2017-02-23 01:49:04 +03:00
|
|
|
func openChannelAndAssert(ctx context.Context, t *harnessTest, net *networkHarness,
|
2017-01-10 06:34:22 +03:00
|
|
|
alice, bob *lightningNode, fundingAmt btcutil.Amount,
|
|
|
|
pushAmt btcutil.Amount) *lnrpc.ChannelPoint {
|
2016-09-18 03:34:39 +03:00
|
|
|
|
2017-01-10 06:34:22 +03:00
|
|
|
chanOpenUpdate, err := net.OpenChannel(ctx, alice, bob, fundingAmt,
|
2017-07-31 06:24:48 +03:00
|
|
|
pushAmt)
|
2016-09-18 03:34:39 +03:00
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to open channel: %v", err)
|
2016-09-18 03:34:39 +03:00
|
|
|
}
|
2016-09-15 21:59:51 +03:00
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
// Mine a block, then wait for Alice's node to notify us that the
|
|
|
|
// channel has been opened. The funding transaction should be found
|
|
|
|
// within the newly mined block.
|
2016-10-24 05:00:09 +03:00
|
|
|
block := mineBlocks(t, net, 1)[0]
|
2016-10-15 16:24:56 +03:00
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
fundingChanPoint, err := net.WaitForChannelOpen(ctx, chanOpenUpdate)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("error while waiting for channel open: %v", err)
|
2016-09-18 03:34:39 +03:00
|
|
|
}
|
2017-01-06 00:56:27 +03:00
|
|
|
fundingTxID, err := chainhash.NewHash(fundingChanPoint.FundingTxid)
|
2016-09-18 03:34:39 +03:00
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to create sha hash: %v", err)
|
2016-09-18 03:34:39 +03:00
|
|
|
}
|
2016-10-24 05:00:09 +03:00
|
|
|
assertTxInBlock(t, block, fundingTxID)
|
2016-09-15 21:59:51 +03:00
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
// The channel should be listed in the peer information returned by
|
|
|
|
// both peers.
|
|
|
|
chanPoint := wire.OutPoint{
|
|
|
|
Hash: *fundingTxID,
|
|
|
|
Index: fundingChanPoint.OutputIndex,
|
|
|
|
}
|
2016-10-15 16:47:09 +03:00
|
|
|
if err := net.AssertChannelExists(ctx, alice, &chanPoint); err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to assert channel existence: %v", err)
|
2016-09-15 21:59:51 +03:00
|
|
|
}
|
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
return fundingChanPoint
|
|
|
|
}
|
2016-09-15 21:59:51 +03:00
|
|
|
|
2016-10-15 16:47:09 +03:00
|
|
|
// closeChannelAndAssert attempts to close a channel identified by the passed
|
2016-09-18 03:34:39 +03:00
|
|
|
// channel point owned by the passed lighting node. A fully blocking channel
|
|
|
|
// closure is attempted, therefore the passed context should be a child derived
|
|
|
|
// via timeout from a base parent. Additionally, once the channel has been
|
|
|
|
// detected as closed, an assertion checks that the transaction is found within
|
|
|
|
// a block.
|
2017-02-23 01:49:04 +03:00
|
|
|
func closeChannelAndAssert(ctx context.Context, t *harnessTest, net *networkHarness,
|
2017-01-06 00:56:27 +03:00
|
|
|
node *lightningNode, fundingChanPoint *lnrpc.ChannelPoint, force bool) *chainhash.Hash {
|
2016-09-18 03:34:39 +03:00
|
|
|
|
2016-12-14 02:32:44 +03:00
|
|
|
closeUpdates, _, err := net.CloseChannel(ctx, node, fundingChanPoint, force)
|
2016-09-18 03:34:39 +03:00
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to close channel: %v", err)
|
2016-09-18 03:34:39 +03:00
|
|
|
}
|
2016-09-15 21:59:51 +03:00
|
|
|
|
2017-05-05 03:15:11 +03:00
|
|
|
txid, err := chainhash.NewHash(fundingChanPoint.FundingTxid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to convert to chainhash: %v", err)
|
|
|
|
}
|
|
|
|
chanPointStr := fmt.Sprintf("%v:%v", txid, fundingChanPoint.OutputIndex)
|
|
|
|
|
2017-05-05 04:06:12 +03:00
|
|
|
// If we didn't force close the transaction, at this point, the channel
|
|
|
|
// should now be marked as being in the state of "pending close".
|
|
|
|
if !force {
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelRequest{}
|
|
|
|
pendingChanResp, err := node.PendingChannels(ctx, pendingChansRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for pending channels: %v", err)
|
|
|
|
}
|
|
|
|
var found bool
|
|
|
|
for _, pendingClose := range pendingChanResp.PendingClosingChannels {
|
|
|
|
if pendingClose.Channel.ChannelPoint == chanPointStr {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("channel not marked as pending close")
|
2017-05-05 03:15:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
// Finally, generate a single block, wait for the final close status
|
|
|
|
// update, then ensure that the closing transaction was included in the
|
|
|
|
// block.
|
2016-10-24 05:00:09 +03:00
|
|
|
block := mineBlocks(t, net, 1)[0]
|
2016-09-15 21:59:51 +03:00
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
closingTxid, err := net.WaitForChannelClose(ctx, closeUpdates)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("error while waiting for channel close: %v", err)
|
2016-09-15 21:59:51 +03:00
|
|
|
}
|
|
|
|
|
2016-10-24 05:00:09 +03:00
|
|
|
assertTxInBlock(t, block, closingTxid)
|
2016-11-22 06:13:03 +03:00
|
|
|
|
|
|
|
return closingTxid
|
2016-09-15 21:59:51 +03:00
|
|
|
}
|
|
|
|
|
2017-05-05 02:16:16 +03:00
|
|
|
// numOpenChannelsPending sends an RPC request to a node to get a count of the
|
|
|
|
// node's channels that are currently in a pending state (with a broadcast, but
|
|
|
|
// not confirmed funding transaction).
|
|
|
|
func numOpenChannelsPending(ctxt context.Context, node *lightningNode) (int, error) {
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelRequest{}
|
2017-01-31 07:21:52 +03:00
|
|
|
resp, err := node.PendingChannels(ctxt, pendingChansRequest)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-05-05 02:16:16 +03:00
|
|
|
return len(resp.PendingOpenChannels), nil
|
2017-01-31 07:21:52 +03:00
|
|
|
}
|
|
|
|
|
2017-05-05 02:16:16 +03:00
|
|
|
// assertNumOpenChannelsPending asserts that a pair of nodes have the expected
|
2017-01-31 07:21:52 +03:00
|
|
|
// number of pending channels between them.
|
2017-05-05 02:16:16 +03:00
|
|
|
func assertNumOpenChannelsPending(ctxt context.Context, t *harnessTest,
|
2017-01-31 07:21:52 +03:00
|
|
|
alice, bob *lightningNode, expected int) {
|
2017-05-05 02:16:16 +03:00
|
|
|
aliceNumChans, err := numOpenChannelsPending(ctxt, alice)
|
2017-01-31 07:21:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error fetching alice's node (%v) pending channels %v",
|
|
|
|
alice.nodeID, err)
|
|
|
|
}
|
2017-05-05 02:16:16 +03:00
|
|
|
bobNumChans, err := numOpenChannelsPending(ctxt, bob)
|
2017-01-31 07:21:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error fetching bob's node (%v) pending channels %v",
|
|
|
|
bob.nodeID, err)
|
|
|
|
}
|
|
|
|
if aliceNumChans != expected {
|
|
|
|
t.Fatalf("number of pending channels for alice incorrect. "+
|
|
|
|
"expected %v, got %v", expected, aliceNumChans)
|
|
|
|
}
|
|
|
|
if bobNumChans != expected {
|
|
|
|
t.Fatalf("number of pending channels for bob incorrect. "+
|
|
|
|
"expected %v, got %v",
|
|
|
|
expected, bobNumChans)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 10:00:39 +03:00
|
|
|
//assertNumConnections asserts number current connections between two peers
|
2017-08-09 02:50:37 +03:00
|
|
|
func assertNumConnections(
|
|
|
|
ctxt context.Context,
|
|
|
|
t *harnessTest,
|
|
|
|
alice, bob *lightningNode,
|
|
|
|
expected int) {
|
|
|
|
|
|
|
|
const nPolls = 10
|
|
|
|
|
|
|
|
tick := time.Tick(300 * time.Millisecond)
|
|
|
|
for i := nPolls - 1; i >= 0; i-- {
|
|
|
|
select {
|
|
|
|
case <-tick:
|
|
|
|
aNumPeers, err := alice.ListPeers(ctxt, &lnrpc.ListPeersRequest{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch alice's node (%v) list peers %v",
|
|
|
|
alice.nodeID, err)
|
|
|
|
}
|
|
|
|
bNumPeers, err := bob.ListPeers(ctxt, &lnrpc.ListPeersRequest{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch bob's node (%v) list peers %v",
|
|
|
|
bob.nodeID, err)
|
|
|
|
}
|
|
|
|
if len(aNumPeers.Peers) != expected {
|
2017-08-09 21:44:57 +03:00
|
|
|
// Continue polling if this is not the final
|
|
|
|
// loop.
|
2017-08-09 02:50:37 +03:00
|
|
|
if i > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatalf("number of peers connected to alice is incorrect: "+
|
|
|
|
"expected %v, got %v", expected, len(aNumPeers.Peers))
|
|
|
|
}
|
|
|
|
if len(bNumPeers.Peers) != expected {
|
2017-08-09 21:44:57 +03:00
|
|
|
// Continue polling if this is not the final
|
|
|
|
// loop.
|
2017-08-09 02:50:37 +03:00
|
|
|
if i > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatalf("number of peers connected to bob is incorrect: "+
|
|
|
|
"expected %v, got %v", expected, len(bNumPeers.Peers))
|
|
|
|
}
|
2017-08-09 21:44:57 +03:00
|
|
|
|
|
|
|
// Alice and Bob both have the required number of
|
|
|
|
// peers, stop polling and return to caller.
|
|
|
|
return
|
2017-08-09 02:50:37 +03:00
|
|
|
}
|
2017-05-05 10:00:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 21:45:02 +03:00
|
|
|
// calcStaticFee calculates appropriate fees for commitment transactions. This
|
|
|
|
// function provides a simple way to allow test balance assertions to take fee
|
|
|
|
// calculations into account.
|
2017-05-17 05:12:21 +03:00
|
|
|
//
|
2017-05-01 21:45:02 +03:00
|
|
|
// TODO(bvu): Refactor when dynamic fee estimation is added.
|
2017-05-17 05:12:21 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbeef): can remove as fee info now exposed in listchannels?
|
2017-05-01 21:45:02 +03:00
|
|
|
func calcStaticFee(numHTLCs int) btcutil.Amount {
|
|
|
|
const (
|
|
|
|
commitWeight = btcutil.Amount(724)
|
|
|
|
htlcWeight = 172
|
2017-05-17 05:12:21 +03:00
|
|
|
feePerKw = btcutil.Amount(50/4) * 1000
|
2017-05-01 21:45:02 +03:00
|
|
|
)
|
2017-05-17 05:12:21 +03:00
|
|
|
return feePerKw * (commitWeight +
|
2017-05-01 21:45:02 +03:00
|
|
|
btcutil.Amount(htlcWeight*numHTLCs)) / 1000
|
|
|
|
}
|
|
|
|
|
2016-09-15 21:59:51 +03:00
|
|
|
// testBasicChannelFunding performs a test exercising expected behavior from a
|
2016-08-30 08:07:54 +03:00
|
|
|
// basic funding workflow. The test creates a new channel between Alice and
|
|
|
|
// Bob, then immediately closes the channel after asserting some expected post
|
2016-09-15 21:59:51 +03:00
|
|
|
// conditions. Finally, the chain itself is checked to ensure the closing
|
2016-08-30 08:07:54 +03:00
|
|
|
// transaction was mined.
|
2016-10-24 05:00:09 +03:00
|
|
|
func testBasicChannelFunding(net *networkHarness, t *harnessTest) {
|
2016-09-18 03:34:39 +03:00
|
|
|
timeout := time.Duration(time.Second * 5)
|
2016-08-31 05:36:33 +03:00
|
|
|
ctxb := context.Background()
|
2016-09-15 22:24:52 +03:00
|
|
|
|
2017-08-08 02:30:29 +03:00
|
|
|
chanAmt := maxFundingAmount
|
2017-01-10 06:34:22 +03:00
|
|
|
pushAmt := btcutil.Amount(100000)
|
2016-08-31 05:36:33 +03:00
|
|
|
|
2016-09-15 22:24:52 +03:00
|
|
|
// First establish a channel with a capacity of 0.5 BTC between Alice
|
2017-01-10 06:34:22 +03:00
|
|
|
// and Bob with Alice pushing 100k satoshis to Bob's side during
|
|
|
|
// funding. This function will block until the channel itself is fully
|
2016-09-15 22:24:52 +03:00
|
|
|
// open or an error occurs in the funding process. A series of
|
|
|
|
// assertions will be executed to ensure the funding process completed
|
|
|
|
// successfully.
|
2016-09-18 03:34:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
chanPoint := openChannelAndAssert(ctxt, t, net, net.Alice, net.Bob,
|
2017-01-10 06:34:22 +03:00
|
|
|
chanAmt, pushAmt)
|
|
|
|
|
2017-03-15 06:06:33 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, time.Second*15)
|
|
|
|
err := net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't report channel: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:34:22 +03:00
|
|
|
// With then channel open, ensure that the amount specified above has
|
|
|
|
// properly been pushed to Bob.
|
|
|
|
balReq := &lnrpc.ChannelBalanceRequest{}
|
|
|
|
aliceBal, err := net.Alice.ChannelBalance(ctxb, balReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get alice's balance: %v", err)
|
|
|
|
}
|
|
|
|
bobBal, err := net.Bob.ChannelBalance(ctxb, balReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get bobs's balance: %v", err)
|
|
|
|
}
|
2017-05-01 21:45:02 +03:00
|
|
|
if aliceBal.Balance != int64(chanAmt-pushAmt-calcStaticFee(0)) {
|
2017-01-17 04:08:48 +03:00
|
|
|
t.Fatalf("alice's balance is incorrect: expected %v got %v",
|
2017-05-01 21:45:02 +03:00
|
|
|
chanAmt-pushAmt-calcStaticFee(0), aliceBal)
|
2017-01-10 06:34:22 +03:00
|
|
|
}
|
|
|
|
if bobBal.Balance != int64(pushAmt) {
|
2017-01-17 04:08:48 +03:00
|
|
|
t.Fatalf("bob's balance is incorrect: expected %v got %v",
|
2017-01-25 04:12:51 +03:00
|
|
|
pushAmt, bobBal.Balance)
|
2017-01-10 06:34:22 +03:00
|
|
|
}
|
2016-09-15 22:24:52 +03:00
|
|
|
|
|
|
|
// Finally, immediately close the channel. This function will also
|
|
|
|
// block until the channel is closed and will additionally assert the
|
|
|
|
// relevant channel closing post conditions.
|
2016-09-18 03:34:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
2016-09-15 21:59:51 +03:00
|
|
|
}
|
2016-08-30 08:07:54 +03:00
|
|
|
|
2017-05-05 10:00:39 +03:00
|
|
|
// testDisconnectingTargetPeer performs a test which
|
|
|
|
// disconnects Alice-peer from Bob-peer and then re-connects them again
|
|
|
|
func testDisconnectingTargetPeer(net *networkHarness, t *harnessTest) {
|
|
|
|
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// Check existing connection.
|
|
|
|
assertNumConnections(ctxb, t, net.Alice, net.Bob, 1)
|
|
|
|
|
2017-08-08 02:30:29 +03:00
|
|
|
chanAmt := maxFundingAmount
|
2017-05-05 10:00:39 +03:00
|
|
|
pushAmt := btcutil.Amount(0)
|
|
|
|
|
|
|
|
timeout := time.Duration(time.Second * 10)
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
|
|
|
|
|
|
|
// Create a new channel that requires 1 confs before it's considered
|
|
|
|
// open, then broadcast the funding transaction
|
|
|
|
const numConfs = 1
|
|
|
|
pendingUpdate, err := net.OpenPendingChannel(ctxt, net.Alice, net.Bob,
|
2017-07-31 06:24:48 +03:00
|
|
|
chanAmt, pushAmt)
|
2017-05-05 10:00:39 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to open channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, the channel's funding transaction will have
|
|
|
|
// been broadcast, but not confirmed. Alice and Bob's nodes
|
|
|
|
// should reflect this when queried via RPC.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
assertNumOpenChannelsPending(ctxt, t, net.Alice, net.Bob, 1)
|
|
|
|
|
|
|
|
// Disconnect Alice-peer from Bob-peer and get error
|
|
|
|
// causes by one pending channel with detach node is existing.
|
|
|
|
if err := net.DisconnectNodes(ctxt, net.Alice, net.Bob); err == nil {
|
|
|
|
t.Fatalf("Bob's peer was disconnected from Alice's"+
|
|
|
|
" while one pending channel is existing: err %v", err)
|
|
|
|
}
|
|
|
|
|
2017-07-06 03:48:44 +03:00
|
|
|
time.Sleep(time.Millisecond * 300)
|
|
|
|
|
2017-05-05 10:00:39 +03:00
|
|
|
// Check existing connection.
|
|
|
|
assertNumConnections(ctxb, t, net.Alice, net.Bob, 1)
|
|
|
|
|
|
|
|
fundingTxID, err := chainhash.NewHash(pendingUpdate.Txid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to convert funding txid into chainhash.Hash:"+
|
|
|
|
" %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mine a block, then wait for Alice's node to notify us that the
|
|
|
|
// channel has been opened. The funding transaction should be found
|
|
|
|
// within the newly mined block.
|
|
|
|
block := mineBlocks(t, net, 1)[0]
|
|
|
|
assertTxInBlock(t, block, fundingTxID)
|
|
|
|
|
|
|
|
// At this point, the channel should be fully opened and there should
|
|
|
|
// be no pending channels remaining for either node.
|
2017-07-12 02:27:11 +03:00
|
|
|
time.Sleep(time.Millisecond * 300)
|
2017-05-05 10:00:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
|
|
|
|
assertNumOpenChannelsPending(ctxt, t, net.Alice, net.Bob, 0)
|
|
|
|
|
|
|
|
// The channel should be listed in the peer information returned by
|
|
|
|
// both peers.
|
|
|
|
outPoint := wire.OutPoint{
|
|
|
|
Hash: *fundingTxID,
|
|
|
|
Index: pendingUpdate.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check both nodes to ensure that the channel is ready for operation.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
if err := net.AssertChannelExists(ctxt, net.Alice, &outPoint); err != nil {
|
|
|
|
t.Fatalf("unable to assert channel existence: %v", err)
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
if err := net.AssertChannelExists(ctxt, net.Bob, &outPoint); err != nil {
|
|
|
|
t.Fatalf("unable to assert channel existence: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, immediately close the channel. This function will also
|
|
|
|
// block until the channel is closed and will additionally assert the
|
|
|
|
// relevant channel closing post conditions.
|
|
|
|
chanPoint := &lnrpc.ChannelPoint{
|
|
|
|
FundingTxid: pendingUpdate.Txid,
|
|
|
|
OutputIndex: pendingUpdate.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect Alice-peer from Bob-peer and get error
|
|
|
|
// causes by one active channel with detach node is existing.
|
|
|
|
if err := net.DisconnectNodes(ctxt, net.Alice, net.Bob); err == nil {
|
|
|
|
t.Fatalf("Bob's peer was disconnected from Alice's"+
|
|
|
|
" while one active channel is existing: err %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check existing connection.
|
|
|
|
assertNumConnections(ctxb, t, net.Alice, net.Bob, 1)
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, true)
|
|
|
|
|
|
|
|
// Disconnect Alice-peer from Bob-peer without getting error
|
|
|
|
// about existing channels.
|
|
|
|
if err := net.DisconnectNodes(ctxt, net.Alice, net.Bob); err != nil {
|
|
|
|
t.Fatalf("unable to disconnect Bob's peer from Alice's: err %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check zero peer connections.
|
|
|
|
assertNumConnections(ctxb, t, net.Alice, net.Bob, 0)
|
|
|
|
|
|
|
|
// Finally, re-connect both nodes.
|
2017-08-19 08:54:56 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-05-05 10:00:39 +03:00
|
|
|
if err := net.ConnectNodes(ctxt, net.Alice, net.Bob); err != nil {
|
|
|
|
t.Fatalf("unable to connect Alice's peer to Bob's: err %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check existing connection.
|
|
|
|
assertNumConnections(ctxb, t, net.Alice, net.Bob, 1)
|
|
|
|
}
|
|
|
|
|
2017-01-31 07:21:52 +03:00
|
|
|
// testFundingPersistence is intended to ensure that the Funding Manager
|
|
|
|
// persists the state of new channels prior to broadcasting the channel's
|
|
|
|
// funding transaction. This ensures that the daemon maintains an up-to-date
|
|
|
|
// representation of channels if the system is restarted or disconnected.
|
|
|
|
// testFundingPersistence mirrors testBasicChannelFunding, but adds restarts
|
|
|
|
// and checks for the state of channels with unconfirmed funding transactions.
|
|
|
|
func testChannelFundingPersistence(net *networkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
2017-08-08 02:30:29 +03:00
|
|
|
chanAmt := maxFundingAmount
|
2017-01-31 07:21:52 +03:00
|
|
|
pushAmt := btcutil.Amount(0)
|
|
|
|
|
2017-02-25 03:30:58 +03:00
|
|
|
timeout := time.Duration(time.Second * 10)
|
|
|
|
|
2017-07-31 06:26:30 +03:00
|
|
|
// As we need to create a channel that requires more than 1
|
|
|
|
// confirmation before it's open, with the current set of defaults,
|
|
|
|
// we'll need to create a new node instance.
|
|
|
|
const numConfs = 5
|
|
|
|
carolArgs := []string{fmt.Sprintf("--defaultchanconfs=%v", numConfs)}
|
|
|
|
carol, err := net.NewNode(carolArgs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
2017-08-19 08:54:56 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
2017-07-31 06:26:30 +03:00
|
|
|
if err := net.ConnectNodes(ctxt, net.Alice, carol); err != nil {
|
|
|
|
t.Fatalf("unable to connect alice to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-02-25 03:30:58 +03:00
|
|
|
// Create a new channel that requires 5 confs before it's considered
|
|
|
|
// open, then broadcast the funding transaction
|
2017-08-19 08:54:56 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-07-31 06:26:30 +03:00
|
|
|
pendingUpdate, err := net.OpenPendingChannel(ctxt, net.Alice, carol,
|
|
|
|
chanAmt, pushAmt)
|
2017-01-31 07:21:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to open channel: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-07-31 06:26:30 +03:00
|
|
|
// At this point, the channel's funding transaction will have been
|
|
|
|
// broadcast, but not confirmed. Alice and Bob's nodes should reflect
|
|
|
|
// this when queried via RPC.
|
2017-02-25 03:30:58 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-07-31 06:26:30 +03:00
|
|
|
assertNumOpenChannelsPending(ctxt, t, net.Alice, carol, 1)
|
2017-01-31 07:21:52 +03:00
|
|
|
|
|
|
|
// Restart both nodes to test that the appropriate state has been
|
|
|
|
// persisted and that both nodes recover gracefully.
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
2017-07-31 06:26:30 +03:00
|
|
|
if err := net.RestartNode(carol, nil); err != nil {
|
2017-01-31 07:21:52 +03:00
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fundingTxID, err := chainhash.NewHash(pendingUpdate.Txid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to convert funding txid into chainhash.Hash:"+
|
|
|
|
" %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mine a block, then wait for Alice's node to notify us that the
|
|
|
|
// channel has been opened. The funding transaction should be found
|
|
|
|
// within the newly mined block.
|
|
|
|
block := mineBlocks(t, net, 1)[0]
|
|
|
|
assertTxInBlock(t, block, fundingTxID)
|
|
|
|
|
|
|
|
// Restart both nodes to test that the appropriate state has been
|
|
|
|
// persisted and that both nodes recover gracefully.
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
2017-07-31 06:26:30 +03:00
|
|
|
if err := net.RestartNode(carol, nil); err != nil {
|
2017-01-31 07:21:52 +03:00
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following block ensures that after both nodes have restarted,
|
|
|
|
// they have reconnected before the execution of the next test.
|
2017-03-30 04:24:44 +03:00
|
|
|
peersTimeout := time.After(15 * time.Second)
|
2017-01-31 07:21:52 +03:00
|
|
|
checkPeersTick := time.NewTicker(100 * time.Millisecond)
|
|
|
|
defer checkPeersTick.Stop()
|
|
|
|
peersPoll:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-peersTimeout:
|
|
|
|
t.Fatalf("peers unable to reconnect after restart")
|
|
|
|
case <-checkPeersTick.C:
|
2017-07-31 06:26:30 +03:00
|
|
|
peers, err := carol.ListPeers(ctxb,
|
2017-01-31 07:21:52 +03:00
|
|
|
&lnrpc.ListPeersRequest{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ListPeers error: %v\n", err)
|
|
|
|
}
|
|
|
|
if len(peers.Peers) > 0 {
|
|
|
|
break peersPoll
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-25 03:30:58 +03:00
|
|
|
// Next, mine enough blocks s.t the channel will open with a single
|
|
|
|
// additional block mined.
|
|
|
|
if _, err := net.Miner.Node.Generate(3); err != nil {
|
|
|
|
t.Fatalf("unable to mine blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both nodes should still show a single channel as pending.
|
2017-08-22 10:50:30 +03:00
|
|
|
time.Sleep(time.Second * 1)
|
2017-02-25 03:30:58 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-07-31 06:26:30 +03:00
|
|
|
assertNumOpenChannelsPending(ctxt, t, net.Alice, carol, 1)
|
2017-02-25 03:30:58 +03:00
|
|
|
|
|
|
|
// Finally, mine the last block which should mark the channel as open.
|
|
|
|
if _, err := net.Miner.Node.Generate(1); err != nil {
|
|
|
|
t.Fatalf("unable to mine blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-01-31 07:21:52 +03:00
|
|
|
// At this point, the channel should be fully opened and there should
|
|
|
|
// be no pending channels remaining for either node.
|
2017-08-22 10:50:30 +03:00
|
|
|
time.Sleep(time.Second * 1)
|
2017-02-25 03:30:58 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-07-31 06:26:30 +03:00
|
|
|
assertNumOpenChannelsPending(ctxt, t, net.Alice, carol, 0)
|
2017-01-31 07:21:52 +03:00
|
|
|
|
|
|
|
// The channel should be listed in the peer information returned by
|
|
|
|
// both peers.
|
|
|
|
outPoint := wire.OutPoint{
|
|
|
|
Hash: *fundingTxID,
|
|
|
|
Index: pendingUpdate.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check both nodes to ensure that the channel is ready for operation.
|
2017-02-25 03:30:58 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-01-31 07:21:52 +03:00
|
|
|
if err := net.AssertChannelExists(ctxt, net.Alice, &outPoint); err != nil {
|
|
|
|
t.Fatalf("unable to assert channel existence: %v", err)
|
|
|
|
}
|
2017-02-25 03:30:58 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-07-31 06:26:30 +03:00
|
|
|
if err := net.AssertChannelExists(ctxt, carol, &outPoint); err != nil {
|
2017-01-31 07:21:52 +03:00
|
|
|
t.Fatalf("unable to assert channel existence: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, immediately close the channel. This function will also
|
|
|
|
// block until the channel is closed and will additionally assert the
|
|
|
|
// relevant channel closing post conditions.
|
|
|
|
chanPoint := &lnrpc.ChannelPoint{
|
|
|
|
FundingTxid: pendingUpdate.Txid,
|
|
|
|
OutputIndex: pendingUpdate.OutputIndex,
|
|
|
|
}
|
2017-02-25 03:30:58 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-05-05 04:06:12 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
2017-07-31 06:26:30 +03:00
|
|
|
|
|
|
|
// Clean up carol's node.
|
|
|
|
if err := carol.Shutdown(); err != nil {
|
|
|
|
t.Fatalf("unable to shutdown carol: %v", err)
|
|
|
|
}
|
2017-01-31 07:21:52 +03:00
|
|
|
}
|
|
|
|
|
2016-09-15 21:59:51 +03:00
|
|
|
// testChannelBalance creates a new channel between Alice and Bob, then
|
|
|
|
// checks channel balance to be equal amount specified while creation of channel.
|
2016-10-24 05:00:09 +03:00
|
|
|
func testChannelBalance(net *networkHarness, t *harnessTest) {
|
2016-09-18 03:34:39 +03:00
|
|
|
timeout := time.Duration(time.Second * 5)
|
2016-10-15 16:47:09 +03:00
|
|
|
|
2017-08-08 02:30:29 +03:00
|
|
|
// Open a channel with 0.16 BTC between Alice and Bob, ensuring the
|
2016-10-15 16:47:09 +03:00
|
|
|
// channel has been opened properly.
|
2017-08-08 02:30:29 +03:00
|
|
|
amount := maxFundingAmount
|
2016-10-15 16:47:09 +03:00
|
|
|
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
2016-08-31 05:36:33 +03:00
|
|
|
|
2016-09-15 22:24:52 +03:00
|
|
|
// Creates a helper closure to be used below which asserts the proper
|
|
|
|
// response to a channel balance RPC.
|
2016-10-15 16:47:09 +03:00
|
|
|
checkChannelBalance := func(node lnrpc.LightningClient,
|
|
|
|
amount btcutil.Amount) {
|
|
|
|
|
|
|
|
response, err := node.ChannelBalance(ctx, &lnrpc.ChannelBalanceRequest{})
|
2016-09-15 21:59:51 +03:00
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to get channel balance: %v", err)
|
2016-09-15 21:59:51 +03:00
|
|
|
}
|
2016-08-31 05:36:33 +03:00
|
|
|
|
2016-09-15 21:59:51 +03:00
|
|
|
balance := btcutil.Amount(response.Balance)
|
|
|
|
if balance != amount {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("channel balance wrong: %v != %v", balance,
|
2016-10-15 16:47:09 +03:00
|
|
|
amount)
|
2016-09-15 21:59:51 +03:00
|
|
|
}
|
2016-08-30 08:07:54 +03:00
|
|
|
}
|
2016-08-31 21:59:08 +03:00
|
|
|
|
2017-02-23 01:49:04 +03:00
|
|
|
chanPoint := openChannelAndAssert(ctx, t, net, net.Alice, net.Bob,
|
2017-01-10 06:34:22 +03:00
|
|
|
amount, 0)
|
2016-09-15 21:59:51 +03:00
|
|
|
|
2017-03-15 06:06:33 +03:00
|
|
|
// Wait for both Alice and Bob to recognize this new channel.
|
|
|
|
ctxt, _ := context.WithTimeout(context.Background(), timeout)
|
|
|
|
err := net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(context.Background(), timeout)
|
|
|
|
err = net.Bob.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-09-15 22:24:52 +03:00
|
|
|
// As this is a single funder channel, Alice's balance should be
|
|
|
|
// exactly 0.5 BTC since now state transitions have taken place yet.
|
2017-05-01 21:45:02 +03:00
|
|
|
checkChannelBalance(net.Alice, amount-calcStaticFee(0))
|
2016-09-15 21:59:51 +03:00
|
|
|
|
2016-09-15 22:24:52 +03:00
|
|
|
// Ensure Bob currently has no available balance within the channel.
|
2016-09-15 21:59:51 +03:00
|
|
|
checkChannelBalance(net.Bob, 0)
|
|
|
|
|
2016-09-15 22:24:52 +03:00
|
|
|
// Finally close the channel between Alice and Bob, asserting that the
|
|
|
|
// channel has been properly closed on-chain.
|
2016-10-15 16:47:09 +03:00
|
|
|
ctx, _ = context.WithTimeout(context.Background(), timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctx, t, net, net.Alice, chanPoint, false)
|
2016-08-30 08:07:54 +03:00
|
|
|
}
|
|
|
|
|
2016-09-15 21:59:51 +03:00
|
|
|
// testChannelForceClosure performs a test to exercise the behavior of "force"
|
|
|
|
// closing a channel or unilaterally broadcasting the latest local commitment
|
2016-09-14 05:00:47 +03:00
|
|
|
// state on-chain. The test creates a new channel between Alice and Bob, then
|
|
|
|
// force closes the channel after some cursory assertions. Within the test, two
|
|
|
|
// transactions should be broadcast on-chain, the commitment transaction itself
|
|
|
|
// (which closes the channel), and the sweep transaction a few blocks later
|
2016-12-14 02:32:44 +03:00
|
|
|
// once the output(s) become mature. This test also includes several restarts
|
|
|
|
// to ensure that the transaction output states are persisted throughout
|
|
|
|
// the forced closure process.
|
2016-09-14 05:00:47 +03:00
|
|
|
//
|
2016-12-14 02:32:44 +03:00
|
|
|
// TODO(roasbeef): also add an unsettled HTLC before force closing.
|
2016-10-24 05:00:09 +03:00
|
|
|
func testChannelForceClosure(net *networkHarness, t *harnessTest) {
|
2016-12-14 02:32:44 +03:00
|
|
|
timeout := time.Duration(time.Second * 10)
|
2016-09-14 05:00:47 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2017-02-08 07:28:50 +03:00
|
|
|
// Before we start, obtain Bob's current wallet balance, we'll check to
|
|
|
|
// ensure that at the end of the force closure by Alice, Bob recognizes
|
|
|
|
// his new on-chain output.
|
|
|
|
bobBalReq := &lnrpc.WalletBalanceRequest{}
|
|
|
|
bobBalResp, err := net.Bob.WalletBalance(ctxb, bobBalReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get bob's balance: %v", err)
|
|
|
|
}
|
|
|
|
bobStartingBalance := btcutil.Amount(bobBalResp.Balance * 1e8)
|
|
|
|
|
|
|
|
// First establish a channel with a capacity of 100k satoshis between
|
|
|
|
// Alice and Bob. We also push 50k satoshis of the initial amount
|
|
|
|
// towards Bob.
|
2016-09-14 05:00:47 +03:00
|
|
|
numFundingConfs := uint32(1)
|
|
|
|
chanAmt := btcutil.Amount(10e4)
|
2017-02-08 07:28:50 +03:00
|
|
|
pushAmt := btcutil.Amount(5e4)
|
2016-09-14 05:00:47 +03:00
|
|
|
chanOpenUpdate, err := net.OpenChannel(ctxb, net.Alice, net.Bob,
|
2017-07-31 06:24:48 +03:00
|
|
|
chanAmt, pushAmt)
|
2016-09-14 05:00:47 +03:00
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to open channel: %v", err)
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
2016-10-15 16:47:09 +03:00
|
|
|
|
2016-09-14 05:00:47 +03:00
|
|
|
if _, err := net.Miner.Node.Generate(numFundingConfs); err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to mine block: %v", err)
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
2016-10-15 16:47:09 +03:00
|
|
|
|
2016-09-18 03:34:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
|
|
|
chanPoint, err := net.WaitForChannelOpen(ctxt, chanOpenUpdate)
|
2016-09-14 05:00:47 +03:00
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("error while waiting for channel to open: %v", err)
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the channel is open, immediately execute a force closure of
|
|
|
|
// the channel. This will also assert that the commitment transaction
|
2016-09-15 22:24:52 +03:00
|
|
|
// was immediately broadcast in order to fulfill the force closure
|
2016-09-14 05:00:47 +03:00
|
|
|
// request.
|
2016-12-14 02:32:44 +03:00
|
|
|
_, closingTxID, err := net.CloseChannel(ctxb, net.Alice, chanPoint, true)
|
2016-09-14 05:00:47 +03:00
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to execute force channel closure: %v", err)
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
|
|
|
|
2017-05-05 03:38:16 +03:00
|
|
|
// Now that the channel has been force closed, it should show up in the
|
|
|
|
// PendingChannels RPC under the force close section.
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelRequest{}
|
|
|
|
pendingChanResp, err := net.Alice.PendingChannels(ctxb, pendingChansRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for pending channels: %v", err)
|
|
|
|
}
|
|
|
|
var found bool
|
|
|
|
txid, _ := chainhash.NewHash(chanPoint.FundingTxid[:])
|
|
|
|
op := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
for _, forceClose := range pendingChanResp.PendingForceClosingChannels {
|
|
|
|
if forceClose.Channel.ChannelPoint == op.String() {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("channel not marked as force close for alice")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): should check default value in config here instead,
|
|
|
|
// or make delay a param
|
|
|
|
const defaultCSV = 4
|
|
|
|
|
2016-12-14 02:32:44 +03:00
|
|
|
// The several restarts in this test are intended to ensure that when a
|
|
|
|
// channel is force-closed, the UTXO nursery has persisted the state of
|
|
|
|
// the channel in the closure process and will recover the correct state
|
|
|
|
// when the system comes back on line. This restart tests state
|
|
|
|
// persistence at the beginning of the process, when the commitment
|
|
|
|
// transaction has been broadcast but not yet confirmed in a block.
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
2017-02-08 07:28:50 +03:00
|
|
|
|
2016-09-14 05:00:47 +03:00
|
|
|
// Mine a block which should confirm the commitment transaction
|
|
|
|
// broadcast as a result of the force closure.
|
|
|
|
if _, err := net.Miner.Node.Generate(1); err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
2016-12-14 02:32:44 +03:00
|
|
|
|
|
|
|
// The following sleep provides time for the UTXO nursery to move the
|
|
|
|
// output from the preschool to the kindergarten database buckets
|
|
|
|
// prior to RestartNode() being triggered. Without this sleep, the
|
|
|
|
// database update may fail, causing the UTXO nursery to retry the move
|
|
|
|
// operation upon restart. This will change the blockheights from what
|
|
|
|
// is expected by the test.
|
|
|
|
// TODO(bvu): refactor out this sleep.
|
|
|
|
duration := time.Millisecond * 300
|
|
|
|
time.Sleep(duration)
|
|
|
|
|
2017-05-05 03:38:16 +03:00
|
|
|
// Now that the channel has been force closed, it should now have the
|
|
|
|
// height and number of blocks to confirm populated.
|
|
|
|
pendingChan, err := net.Alice.PendingChannels(ctxb, pendingChansRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for pending channels: %v", err)
|
|
|
|
}
|
|
|
|
if len(pendingChan.PendingForceClosingChannels) == 0 {
|
|
|
|
t.Fatalf("channel not marked as force close for alice")
|
|
|
|
}
|
|
|
|
forceClosedChan := pendingChan.PendingForceClosingChannels[0]
|
|
|
|
if forceClosedChan.MaturityHeight == 0 {
|
|
|
|
t.Fatalf("force close channel marked as not confirmed")
|
|
|
|
}
|
|
|
|
if forceClosedChan.BlocksTilMaturity != defaultCSV {
|
|
|
|
t.Fatalf("force closed channel has incorrect maturity time: "+
|
|
|
|
"expected %v, got %v", forceClosedChan.BlocksTilMaturity,
|
|
|
|
defaultCSV)
|
|
|
|
}
|
|
|
|
|
2016-12-14 02:32:44 +03:00
|
|
|
// The following restart is intended to ensure that outputs from the
|
|
|
|
// force close commitment transaction have been persisted once the
|
|
|
|
// transaction has been confirmed, but before the outputs are spendable
|
|
|
|
// (the "kindergarten" bucket.)
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Currently within the codebase, the default CSV is 4 relative blocks.
|
2016-12-14 02:32:44 +03:00
|
|
|
// For the persistence test, we generate three blocks, then trigger
|
|
|
|
// a restart and then generate the final block that should trigger
|
|
|
|
// the creation of the sweep transaction.
|
|
|
|
if _, err := net.Miner.Node.Generate(defaultCSV - 1); err != nil {
|
|
|
|
t.Fatalf("unable to mine blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following restart checks to ensure that outputs in the kindergarten
|
|
|
|
// bucket are persisted while waiting for the required number of
|
|
|
|
// confirmations to be reported.
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := net.Miner.Node.Generate(1); err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to mine blocks: %v", err)
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, the sweeping transaction should now be broadcast. So
|
|
|
|
// we fetch the node's mempool to ensure it has been properly
|
2016-09-15 22:24:52 +03:00
|
|
|
// broadcast.
|
2017-01-06 00:56:27 +03:00
|
|
|
var sweepingTXID *chainhash.Hash
|
|
|
|
var mempool []*chainhash.Hash
|
2016-12-14 02:32:44 +03:00
|
|
|
mempoolTimeout := time.After(3 * time.Second)
|
2017-01-30 04:16:49 +03:00
|
|
|
checkMempoolTick := time.NewTicker(100 * time.Millisecond)
|
|
|
|
defer checkMempoolTick.Stop()
|
2016-09-14 05:00:47 +03:00
|
|
|
mempoolPoll:
|
|
|
|
for {
|
|
|
|
select {
|
2016-12-14 02:32:44 +03:00
|
|
|
case <-mempoolTimeout:
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("sweep tx not found in mempool")
|
2017-01-30 04:16:49 +03:00
|
|
|
case <-checkMempoolTick.C:
|
2016-09-14 05:00:47 +03:00
|
|
|
mempool, err = net.Miner.Node.GetRawMempool()
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to fetch node's mempool: %v", err)
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
2016-12-14 02:32:44 +03:00
|
|
|
if len(mempool) != 0 {
|
|
|
|
break mempoolPoll
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There should be exactly one transaction within the mempool at this
|
|
|
|
// point.
|
|
|
|
// TODO(roasbeef): assertion may not necessarily hold with concurrent
|
|
|
|
// test executions
|
|
|
|
if len(mempool) != 1 {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("node's mempool is wrong size, expected 1 got %v",
|
2016-09-14 05:00:47 +03:00
|
|
|
len(mempool))
|
|
|
|
}
|
|
|
|
sweepingTXID = mempool[0]
|
|
|
|
|
|
|
|
// Fetch the sweep transaction, all input it's spending should be from
|
2016-09-15 22:24:52 +03:00
|
|
|
// the commitment transaction which was broadcast on-chain.
|
2016-09-14 05:00:47 +03:00
|
|
|
sweepTx, err := net.Miner.Node.GetRawTransaction(sweepingTXID)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to fetch sweep tx: %v", err)
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
|
|
|
for _, txIn := range sweepTx.MsgTx().TxIn {
|
|
|
|
if !closingTxID.IsEqual(&txIn.PreviousOutPoint.Hash) {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("sweep transaction not spending from commit "+
|
2016-09-14 05:00:47 +03:00
|
|
|
"tx %v, instead spending %v",
|
|
|
|
closingTxID, txIn.PreviousOutPoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we mine an additional block which should include the sweep
|
|
|
|
// transaction as the input scripts and the sequence locks on the
|
|
|
|
// inputs should be properly met.
|
|
|
|
blockHash, err := net.Miner.Node.Generate(1)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
|
|
|
block, err := net.Miner.Node.GetBlock(blockHash[0])
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to get block: %v", err)
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
2016-10-15 16:47:09 +03:00
|
|
|
|
2017-01-06 00:56:27 +03:00
|
|
|
assertTxInBlock(t, block, sweepTx.Hash())
|
2017-02-08 07:28:50 +03:00
|
|
|
|
2017-05-05 03:38:16 +03:00
|
|
|
// Now that the channel has been fully swept, it should no longer show
|
|
|
|
// up within the pending channels RPC.
|
2017-05-06 01:57:09 +03:00
|
|
|
time.Sleep(time.Millisecond * 300)
|
2017-05-05 03:38:16 +03:00
|
|
|
pendingChans, err := net.Alice.PendingChannels(ctxb, pendingChansRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for pending channels: %v", err)
|
|
|
|
}
|
|
|
|
if len(pendingChans.PendingForceClosingChannels) != 0 {
|
|
|
|
t.Fatalf("no channels should be shown as force closed")
|
|
|
|
}
|
|
|
|
|
2017-02-08 07:28:50 +03:00
|
|
|
// At this point, Bob should now be aware of his new immediately
|
|
|
|
// spendable on-chain balance, as it was Alice who broadcast the
|
|
|
|
// commitment transaction.
|
|
|
|
bobBalResp, err = net.Bob.WalletBalance(ctxb, bobBalReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get bob's balance: %v", err)
|
|
|
|
}
|
|
|
|
bobExpectedBalance := bobStartingBalance + pushAmt
|
|
|
|
if btcutil.Amount(bobBalResp.Balance*1e8) < bobExpectedBalance {
|
|
|
|
t.Fatalf("bob's balance is incorrect: expected %v got %v",
|
|
|
|
bobExpectedBalance, btcutil.Amount(bobBalResp.Balance*1e8))
|
|
|
|
}
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
|
|
|
|
2016-10-24 05:00:09 +03:00
|
|
|
func testSingleHopInvoice(net *networkHarness, t *harnessTest) {
|
2016-09-26 20:40:58 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
timeout := time.Duration(time.Second * 5)
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice being
|
|
|
|
// the sole funder of the channel.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
|
|
|
chanAmt := btcutil.Amount(100000)
|
2017-02-23 01:49:04 +03:00
|
|
|
chanPoint := openChannelAndAssert(ctxt, t, net, net.Alice, net.Bob,
|
2017-01-10 06:34:22 +03:00
|
|
|
chanAmt, 0)
|
2016-09-26 20:40:58 +03:00
|
|
|
|
2017-04-30 10:20:47 +03:00
|
|
|
assertAmountSent := func(amt btcutil.Amount) {
|
2017-04-17 01:27:28 +03:00
|
|
|
// Both channels should also have properly accounted from the
|
|
|
|
// amount that has been sent/received over the channel.
|
2017-01-07 23:46:10 +03:00
|
|
|
listReq := &lnrpc.ListChannelsRequest{}
|
|
|
|
aliceListChannels, err := net.Alice.ListChannels(ctxb, listReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for alice's channel list: %v", err)
|
|
|
|
}
|
|
|
|
aliceSatoshisSent := aliceListChannels.Channels[0].TotalSatoshisSent
|
|
|
|
if aliceSatoshisSent != int64(amt) {
|
|
|
|
t.Fatalf("Alice's satoshis sent is incorrect got %v, expected %v",
|
|
|
|
aliceSatoshisSent, amt)
|
|
|
|
}
|
|
|
|
|
|
|
|
bobListChannels, err := net.Bob.ListChannels(ctxb, listReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for bob's channel list: %v", err)
|
|
|
|
}
|
|
|
|
bobSatoshisReceived := bobListChannels.Channels[0].TotalSatoshisReceived
|
|
|
|
if bobSatoshisReceived != int64(amt) {
|
|
|
|
t.Fatalf("Bob's satoshis received is incorrect got %v, expected %v",
|
|
|
|
bobSatoshisReceived, amt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 20:40:58 +03:00
|
|
|
// Now that the channel is open, create an invoice for Bob which
|
|
|
|
// expects a payment of 1000 satoshis from Alice paid via a particular
|
2017-01-13 08:01:50 +03:00
|
|
|
// preimage.
|
2016-09-26 20:40:58 +03:00
|
|
|
const paymentAmt = 1000
|
|
|
|
preimage := bytes.Repeat([]byte("A"), 32)
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
|
|
|
invoiceResp, err := net.Bob.AddInvoice(ctxb, invoice)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
2016-09-26 20:40:58 +03:00
|
|
|
}
|
|
|
|
|
2017-03-15 06:06:33 +03:00
|
|
|
// Wait for Alice to recognize and advertise the new channel generated
|
|
|
|
// above.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2017-08-22 10:50:30 +03:00
|
|
|
err = net.Bob.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2017-03-15 06:06:33 +03:00
|
|
|
|
2016-09-26 20:40:58 +03:00
|
|
|
// With the invoice for Bob added, send a payment towards Alice paying
|
|
|
|
// to the above generated invoice.
|
|
|
|
sendStream, err := net.Alice.SendPayment(ctxb)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to create alice payment stream: %v", err)
|
2016-09-26 20:40:58 +03:00
|
|
|
}
|
|
|
|
sendReq := &lnrpc.SendRequest{
|
|
|
|
PaymentHash: invoiceResp.RHash,
|
|
|
|
Dest: net.Bob.PubKey[:],
|
|
|
|
Amt: paymentAmt,
|
|
|
|
}
|
|
|
|
if err := sendStream.Send(sendReq); err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
2016-09-26 20:40:58 +03:00
|
|
|
}
|
2017-02-21 11:26:28 +03:00
|
|
|
|
|
|
|
// Ensure we obtain the proper preimage in the response.
|
2017-05-19 15:18:21 +03:00
|
|
|
if resp, err := sendStream.Recv(); err != nil {
|
|
|
|
t.Fatalf("payment stream has been close: %v", err)
|
|
|
|
} else if resp.PaymentError != "" {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("error when attempting recv: %v", err)
|
2017-05-19 15:18:21 +03:00
|
|
|
} else if !bytes.Equal(preimage, resp.PaymentPreimage) {
|
2017-02-21 11:26:28 +03:00
|
|
|
t.Fatalf("preimage mismatch: expected %v, got %v", preimage,
|
2017-05-19 15:18:21 +03:00
|
|
|
resp.GetPaymentPreimage())
|
2017-02-21 11:26:28 +03:00
|
|
|
}
|
2016-09-26 20:40:58 +03:00
|
|
|
|
|
|
|
// Bob's invoice should now be found and marked as settled.
|
2016-10-16 00:45:26 +03:00
|
|
|
payHash := &lnrpc.PaymentHash{
|
|
|
|
RHash: invoiceResp.RHash,
|
|
|
|
}
|
2016-09-26 20:40:58 +03:00
|
|
|
dbInvoice, err := net.Bob.LookupInvoice(ctxb, payHash)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to lookup invoice: %v", err)
|
2016-09-26 20:40:58 +03:00
|
|
|
}
|
|
|
|
if !dbInvoice.Settled {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("bob's invoice should be marked as settled: %v",
|
2016-09-26 20:40:58 +03:00
|
|
|
spew.Sdump(dbInvoice))
|
|
|
|
}
|
|
|
|
|
2017-01-07 23:46:10 +03:00
|
|
|
// With the payment completed all balance related stats should be
|
|
|
|
// properly updated.
|
2017-04-17 01:27:28 +03:00
|
|
|
time.Sleep(time.Millisecond * 200)
|
2017-04-30 10:20:47 +03:00
|
|
|
assertAmountSent(paymentAmt)
|
2016-09-26 20:40:58 +03:00
|
|
|
|
2017-01-07 23:46:10 +03:00
|
|
|
// Create another invoice for Bob, this time leaving off the preimage
|
|
|
|
// to one will be randomly generated. We'll test the proper
|
|
|
|
// encoding/decoding of the zpay32 payment requests.
|
|
|
|
invoice = &lnrpc.Invoice{
|
|
|
|
Memo: "test3",
|
|
|
|
Value: paymentAmt,
|
2016-09-26 20:40:58 +03:00
|
|
|
}
|
2017-01-07 23:46:10 +03:00
|
|
|
invoiceResp, err = net.Bob.AddInvoice(ctxb, invoice)
|
2016-11-19 03:20:44 +03:00
|
|
|
if err != nil {
|
2017-01-07 23:46:10 +03:00
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
2016-11-19 03:20:44 +03:00
|
|
|
}
|
|
|
|
|
2017-01-07 23:46:10 +03:00
|
|
|
// Next send another payment, but this time using a zpay32 encoded
|
|
|
|
// invoice rather than manually specifying the payment details.
|
|
|
|
if err := sendStream.Send(&lnrpc.SendRequest{
|
|
|
|
PaymentRequest: invoiceResp.PaymentRequest,
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
2016-11-19 03:20:44 +03:00
|
|
|
}
|
2017-05-19 15:18:21 +03:00
|
|
|
if resp, err := sendStream.Recv(); err != nil {
|
|
|
|
t.Fatalf("payment stream has been closed: %v", err)
|
|
|
|
} else if resp.PaymentError != "" {
|
|
|
|
t.Fatalf("error when attempting recv: %v",
|
|
|
|
resp.PaymentError)
|
2016-11-19 03:20:44 +03:00
|
|
|
}
|
|
|
|
|
2017-01-07 23:46:10 +03:00
|
|
|
// The second payment should also have succeeded, with the balances
|
|
|
|
// being update accordingly.
|
2017-04-17 01:27:28 +03:00
|
|
|
time.Sleep(time.Millisecond * 200)
|
2017-04-30 10:20:47 +03:00
|
|
|
assertAmountSent(paymentAmt * 2)
|
2017-01-07 23:46:10 +03:00
|
|
|
|
2016-09-26 20:40:58 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
2016-09-26 20:40:58 +03:00
|
|
|
}
|
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
func testListPayments(net *networkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
2016-12-31 03:41:59 +03:00
|
|
|
timeout := time.Duration(time.Second * 5)
|
2016-12-05 14:59:36 +03:00
|
|
|
|
2016-12-31 03:41:59 +03:00
|
|
|
// First start by deleting all payments that Alice knows of. This will
|
|
|
|
// allow us to execute the test with a clean state for Alice.
|
|
|
|
delPaymentsReq := &lnrpc.DeleteAllPaymentsRequest{}
|
|
|
|
if _, err := net.Alice.DeleteAllPayments(ctxb, delPaymentsReq); err != nil {
|
|
|
|
t.Fatalf("unable to delete payments: %v", err)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that there are no payments before test.
|
|
|
|
reqInit := &lnrpc.ListPaymentsRequest{}
|
2016-12-31 03:41:59 +03:00
|
|
|
paymentsRespInit, err := net.Alice.ListPayments(ctxb, reqInit)
|
2016-12-05 14:59:36 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error when obtaining Alice payments: %v", err)
|
|
|
|
}
|
|
|
|
if len(paymentsRespInit.Payments) != 0 {
|
2016-12-21 12:19:01 +03:00
|
|
|
t.Fatalf("incorrect number of payments, got %v, want %v",
|
|
|
|
len(paymentsRespInit.Payments), 0)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 03:41:59 +03:00
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
2016-12-05 14:59:36 +03:00
|
|
|
chanAmt := btcutil.Amount(100000)
|
2016-12-31 03:41:59 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
chanPoint := openChannelAndAssert(ctxt, t, net, net.Alice, net.Bob,
|
2017-01-10 06:34:22 +03:00
|
|
|
chanAmt, 0)
|
2016-12-05 14:59:36 +03:00
|
|
|
|
|
|
|
// Now that the channel is open, create an invoice for Bob which
|
2016-12-31 03:41:59 +03:00
|
|
|
// expects a payment of 1000 satoshis from Alice paid via a particular
|
2017-01-13 08:01:50 +03:00
|
|
|
// preimage.
|
2016-12-05 14:59:36 +03:00
|
|
|
const paymentAmt = 1000
|
|
|
|
preimage := bytes.Repeat([]byte("B"), 32)
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
addInvoiceCtxt, _ := context.WithTimeout(ctxb, timeout)
|
|
|
|
invoiceResp, err := net.Bob.AddInvoice(addInvoiceCtxt, invoice)
|
2016-12-05 14:59:36 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-15 06:06:33 +03:00
|
|
|
// Wait for Alice to recognize and advertise the new channel generated
|
|
|
|
// above.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
// With the invoice for Bob added, send a payment towards Alice paying
|
|
|
|
// to the above generated invoice.
|
2016-12-31 03:41:59 +03:00
|
|
|
sendStream, err := net.Alice.SendPayment(ctxb)
|
2016-12-05 14:59:36 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create alice payment stream: %v", err)
|
|
|
|
}
|
|
|
|
sendReq := &lnrpc.SendRequest{
|
|
|
|
PaymentHash: invoiceResp.RHash,
|
|
|
|
Dest: net.Bob.PubKey[:],
|
|
|
|
Amt: paymentAmt,
|
|
|
|
}
|
|
|
|
if err := sendStream.Send(sendReq); err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
2017-05-19 15:18:21 +03:00
|
|
|
if resp, err := sendStream.Recv(); err != nil {
|
|
|
|
t.Fatalf("payment stream has been closed: %v", err)
|
|
|
|
} else if resp.PaymentError != "" {
|
|
|
|
t.Fatalf("error when attempting recv: %v",
|
|
|
|
resp.PaymentError)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 03:41:59 +03:00
|
|
|
// Grab Alice's list of payments, she should show the existence of
|
|
|
|
// exactly one payment.
|
2016-12-05 14:59:36 +03:00
|
|
|
req := &lnrpc.ListPaymentsRequest{}
|
2016-12-31 03:41:59 +03:00
|
|
|
paymentsResp, err := net.Alice.ListPayments(ctxb, req)
|
2016-12-05 14:59:36 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error when obtaining Alice payments: %v", err)
|
|
|
|
}
|
|
|
|
if len(paymentsResp.Payments) != 1 {
|
2016-12-21 12:19:01 +03:00
|
|
|
t.Fatalf("incorrect number of payments, got %v, want %v",
|
|
|
|
len(paymentsResp.Payments), 1)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
p := paymentsResp.Payments[0]
|
|
|
|
|
2016-12-31 03:41:59 +03:00
|
|
|
// Ensure that the stored path shows a direct payment to Bob with no
|
|
|
|
// other nodes in-between.
|
2016-12-21 12:19:01 +03:00
|
|
|
expectedPath := []string{
|
2016-12-05 14:59:36 +03:00
|
|
|
net.Bob.PubKeyStr,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(p.Path, expectedPath) {
|
2016-12-21 12:19:01 +03:00
|
|
|
t.Fatalf("incorrect path, got %v, want %v",
|
|
|
|
p.Path, expectedPath)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 03:41:59 +03:00
|
|
|
// The payment amount should also match our previous payment directly.
|
2016-12-05 14:59:36 +03:00
|
|
|
if p.Value != paymentAmt {
|
2016-12-21 12:19:01 +03:00
|
|
|
t.Fatalf("incorrect amount, got %v, want %v",
|
|
|
|
p.Value, paymentAmt)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 03:41:59 +03:00
|
|
|
// The payment hash (or r-hash) should have been stored correctly.
|
2016-12-05 14:59:36 +03:00
|
|
|
correctRHash := hex.EncodeToString(invoiceResp.RHash)
|
2016-12-31 03:41:59 +03:00
|
|
|
if !reflect.DeepEqual(p.PaymentHash, correctRHash) {
|
2016-12-21 12:19:01 +03:00
|
|
|
t.Fatalf("incorrect RHash, got %v, want %v",
|
2016-12-31 03:41:59 +03:00
|
|
|
p.PaymentHash, correctRHash)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 03:41:59 +03:00
|
|
|
// Finally, as we made a single-hop direct payment, there should have
|
|
|
|
// been no fee applied.
|
2016-12-05 14:59:36 +03:00
|
|
|
if p.Fee != 0 {
|
|
|
|
t.Fatalf("incorrect Fee, got %v, want %v", p.Fee, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all payments from Alice. DB should have no payments.
|
2016-12-31 03:41:59 +03:00
|
|
|
delReq := &lnrpc.DeleteAllPaymentsRequest{}
|
|
|
|
_, err = net.Alice.DeleteAllPayments(ctxb, delReq)
|
2016-12-05 14:59:36 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't delete payments at the end: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that there are no payments before test.
|
2016-12-31 03:41:59 +03:00
|
|
|
listReq := &lnrpc.ListPaymentsRequest{}
|
|
|
|
paymentsResp, err = net.Alice.ListPayments(ctxb, listReq)
|
2016-12-05 14:59:36 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error when obtaining Alice payments: %v", err)
|
|
|
|
}
|
2016-12-31 03:41:59 +03:00
|
|
|
if len(paymentsResp.Payments) != 0 {
|
2016-12-21 12:19:01 +03:00
|
|
|
t.Fatalf("incorrect number of payments, got %v, want %v",
|
|
|
|
len(paymentsRespInit.Payments), 0)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 03:41:59 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
2016-10-24 05:00:09 +03:00
|
|
|
func testMultiHopPayments(net *networkHarness, t *harnessTest) {
|
2016-09-26 20:41:57 +03:00
|
|
|
const chanAmt = btcutil.Amount(100000)
|
|
|
|
ctxb := context.Background()
|
|
|
|
timeout := time.Duration(time.Second * 5)
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(ctxt, t, net, net.Alice,
|
2017-01-10 06:34:22 +03:00
|
|
|
net.Bob, chanAmt, 0)
|
2017-06-17 01:20:05 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
err := net.Alice.WaitForNetworkChannelOpen(ctxt, chanPointAlice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't advertise her channel: %v", err)
|
|
|
|
}
|
2016-10-15 16:47:09 +03:00
|
|
|
|
2017-01-06 00:56:27 +03:00
|
|
|
aliceChanTXID, err := chainhash.NewHash(chanPointAlice.FundingTxid)
|
2016-09-26 20:41:57 +03:00
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to create sha hash: %v", err)
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
aliceFundPoint := wire.OutPoint{
|
|
|
|
Hash: *aliceChanTXID,
|
|
|
|
Index: chanPointAlice.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new node (Carol), load her with some funds, then establish
|
|
|
|
// a connection between Carol and Alice with a channel that has
|
|
|
|
// identical capacity to the one created above.
|
|
|
|
//
|
|
|
|
// The network topology should now look like: Carol -> Alice -> Bob
|
|
|
|
carol, err := net.NewNode(nil)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
if err := net.ConnectNodes(ctxb, carol, net.Alice); err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to connect carol to alice: %v", err)
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
err = net.SendCoins(ctxb, btcutil.SatoshiPerBitcoin, carol)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
chanPointCarol := openChannelAndAssert(ctxt, t, net, carol,
|
2017-01-10 06:34:22 +03:00
|
|
|
net.Alice, chanAmt, 0)
|
2016-10-15 16:47:09 +03:00
|
|
|
|
2017-01-06 00:56:27 +03:00
|
|
|
carolChanTXID, err := chainhash.NewHash(chanPointCarol.FundingTxid)
|
2016-09-26 20:41:57 +03:00
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to create sha hash: %v", err)
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
carolFundPoint := wire.OutPoint{
|
|
|
|
Hash: *carolChanTXID,
|
|
|
|
Index: chanPointCarol.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create 5 invoices for Bob, which expect a payment from Carol for 1k
|
|
|
|
// satoshis with a different preimage each time.
|
|
|
|
const numPayments = 5
|
|
|
|
const paymentAmt = 1000
|
|
|
|
rHashes := make([][]byte, numPayments)
|
|
|
|
for i := 0; i < numPayments; i++ {
|
|
|
|
invoice := &lnrpc.Invoice{
|
2017-01-08 08:20:02 +03:00
|
|
|
Memo: "testing",
|
|
|
|
Value: paymentAmt,
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
resp, err := net.Bob.AddInvoice(ctxb, invoice)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rHashes[i] = resp.RHash
|
|
|
|
}
|
|
|
|
|
2017-03-15 06:06:33 +03:00
|
|
|
// Wait for carol to recognize both the Channel from herself to Carol,
|
|
|
|
// and also the channel from Alice to Bob.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPointCarol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("carol didn't advertise her channel: %v", err)
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPointAlice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("carol didn't see the alice->bob channel before timeout: %v", err)
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Using Carol as the source, pay to the 5 invoices from Bob created above.
|
|
|
|
carolPayStream, err := carol.SendPayment(ctxb)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to create payment stream for carol: %v", err)
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Concurrently pay off all 5 of Bob's invoices. Each of the goroutines
|
|
|
|
// will unblock on the recv once the HTLC it sent has been fully
|
|
|
|
// settled.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, rHash := range rHashes {
|
|
|
|
sendReq := &lnrpc.SendRequest{
|
|
|
|
PaymentHash: rHash,
|
|
|
|
Dest: net.Bob.PubKey[:],
|
|
|
|
Amt: paymentAmt,
|
|
|
|
}
|
|
|
|
|
2017-01-06 00:00:40 +03:00
|
|
|
if err := carolPayStream.Send(sendReq); err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
2017-05-19 15:18:21 +03:00
|
|
|
|
|
|
|
if resp, err := carolPayStream.Recv(); err != nil {
|
|
|
|
t.Fatalf("payment stream has been closed: %v", err)
|
|
|
|
} else if resp.PaymentError != "" {
|
|
|
|
t.Fatalf("unable to recv pay resp: %v",
|
|
|
|
resp.PaymentError)
|
2017-01-06 00:00:40 +03:00
|
|
|
}
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
finClear := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(finClear)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Second * 10):
|
2017-01-13 08:01:50 +03:00
|
|
|
t.Fatalf("HTLCs not cleared after 10 seconds")
|
2016-09-26 20:41:57 +03:00
|
|
|
case <-finClear:
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:20:05 +03:00
|
|
|
// When asserting the amount of satoshis moved, we'll factor in the
|
|
|
|
// default base fee, as we didn't modify the fee structure when
|
|
|
|
// creating the seed nodes in the network.
|
|
|
|
const baseFee = 1
|
|
|
|
|
2017-04-30 10:20:47 +03:00
|
|
|
assertAmountPaid := func(node *lightningNode,
|
|
|
|
chanPoint wire.OutPoint, amountSent,
|
|
|
|
amountReceived int64) {
|
2016-10-15 16:47:09 +03:00
|
|
|
|
2016-10-30 17:55:25 +03:00
|
|
|
channelName := ""
|
|
|
|
switch chanPoint {
|
|
|
|
case carolFundPoint:
|
|
|
|
channelName = "Carol(local) => Alice(remote)"
|
|
|
|
case aliceFundPoint:
|
|
|
|
channelName = "Alice(local) => Bob(remote)"
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
2016-10-30 17:55:25 +03:00
|
|
|
|
2017-04-30 10:20:47 +03:00
|
|
|
checkAmountPaid := func() error {
|
2016-10-30 17:55:25 +03:00
|
|
|
listReq := &lnrpc.ListChannelsRequest{}
|
|
|
|
resp, err := node.ListChannels(ctxb, listReq)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to for node's "+
|
|
|
|
"channels: %v", err)
|
|
|
|
}
|
|
|
|
for _, channel := range resp.Channels {
|
|
|
|
if channel.ChannelPoint != chanPoint.String() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-04-30 10:20:47 +03:00
|
|
|
if channel.TotalSatoshisSent != amountSent {
|
|
|
|
return fmt.Errorf("%v: incorrect amount"+
|
|
|
|
" sent: %v != %v", channelName,
|
|
|
|
channel.TotalSatoshisSent,
|
|
|
|
amountSent)
|
2016-10-30 17:55:25 +03:00
|
|
|
}
|
2017-04-30 10:20:47 +03:00
|
|
|
if channel.TotalSatoshisReceived !=
|
|
|
|
amountReceived {
|
2017-06-17 01:20:05 +03:00
|
|
|
return fmt.Errorf("%v: incorrect amount"+
|
2017-04-30 10:20:47 +03:00
|
|
|
" received: %v != %v",
|
|
|
|
channelName,
|
|
|
|
channel.TotalSatoshisReceived,
|
|
|
|
amountReceived)
|
2016-10-30 17:55:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
2016-10-30 17:55:25 +03:00
|
|
|
return fmt.Errorf("channel not found")
|
|
|
|
}
|
2016-09-26 20:41:57 +03:00
|
|
|
|
2016-10-30 17:55:25 +03:00
|
|
|
// As far as HTLC inclusion in commitment transaction might be
|
|
|
|
// postponed we will try to check the balance couple of
|
|
|
|
// times, and then if after some period of time we receive wrong
|
|
|
|
// balance return the error.
|
|
|
|
// TODO(roasbeef): remove sleep after invoice notification hooks
|
|
|
|
// are in place
|
|
|
|
var timeover uint32
|
|
|
|
go func() {
|
|
|
|
<-time.After(time.Second * 20)
|
|
|
|
atomic.StoreUint32(&timeover, 1)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
isTimeover := atomic.LoadUint32(&timeover) == 1
|
2017-04-30 10:20:47 +03:00
|
|
|
if err := checkAmountPaid(); err != nil {
|
2016-10-30 17:55:25 +03:00
|
|
|
if isTimeover {
|
2017-04-30 10:20:47 +03:00
|
|
|
t.Fatalf("Check amount Paid failed: %v", err)
|
2016-10-30 17:55:25 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point all the channels within our proto network should be
|
|
|
|
// shifted by 5k satoshis in the direction of Bob, the sink within the
|
2016-10-30 17:55:25 +03:00
|
|
|
// payment flow generated above. The order of asserts corresponds to
|
|
|
|
// increasing of time is needed to embed the HTLC in commitment
|
|
|
|
// transaction, in channel Carol->Alice->Bob, order is Bob,Alice,Carol.
|
2017-04-30 10:20:47 +03:00
|
|
|
const amountPaid = int64(5000)
|
|
|
|
assertAmountPaid(net.Bob, aliceFundPoint, int64(0), amountPaid)
|
|
|
|
assertAmountPaid(net.Alice, aliceFundPoint, amountPaid, int64(0))
|
2017-06-17 01:20:05 +03:00
|
|
|
assertAmountPaid(net.Alice, carolFundPoint, int64(0),
|
|
|
|
amountPaid+(baseFee*numPayments))
|
|
|
|
assertAmountPaid(carol, carolFundPoint, amountPaid+(baseFee*numPayments),
|
|
|
|
int64(0))
|
2016-09-26 20:41:57 +03:00
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
2016-09-26 20:41:57 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPointCarol, false)
|
2017-01-07 23:48:15 +03:00
|
|
|
|
|
|
|
// Finally, shutdown the node we created for the duration of the tests,
|
|
|
|
// only leaving the two seed nodes (Alice and Bob) within our test
|
|
|
|
// network.
|
2017-03-15 06:06:33 +03:00
|
|
|
if err := carol.Shutdown(); err != nil {
|
2017-01-07 23:48:15 +03:00
|
|
|
t.Fatalf("unable to shutdown carol: %v", err)
|
|
|
|
}
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
|
2016-10-24 05:00:09 +03:00
|
|
|
func testInvoiceSubscriptions(net *networkHarness, t *harnessTest) {
|
2016-10-16 00:45:26 +03:00
|
|
|
const chanAmt = btcutil.Amount(500000)
|
|
|
|
ctxb := context.Background()
|
|
|
|
timeout := time.Duration(time.Second * 5)
|
|
|
|
|
|
|
|
// Open a channel with 500k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
chanPoint := openChannelAndAssert(ctxt, t, net, net.Alice, net.Bob,
|
2017-01-10 06:34:22 +03:00
|
|
|
chanAmt, 0)
|
2016-10-16 00:45:26 +03:00
|
|
|
|
|
|
|
// Next create a new invoice for Bob requesting 1k satoshis.
|
2016-11-22 06:51:58 +03:00
|
|
|
// TODO(roasbeef): make global list of invoices for each node to re-use
|
|
|
|
// and avoid collisions
|
2016-10-16 00:45:26 +03:00
|
|
|
const paymentAmt = 1000
|
|
|
|
preimage := bytes.Repeat([]byte{byte(90)}, 32)
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
|
|
|
invoiceResp, err := net.Bob.AddInvoice(ctxb, invoice)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
2016-10-16 00:45:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new invoice subscription client for Bob, the notification
|
|
|
|
// should be dispatched shortly below.
|
|
|
|
req := &lnrpc.InvoiceSubscription{}
|
|
|
|
bobInvoiceSubscription, err := net.Bob.SubscribeInvoices(ctxb, req)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to subscribe to bob's invoice updates: %v", err)
|
2016-10-16 00:45:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
updateSent := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
invoiceUpdate, err := bobInvoiceSubscription.Recv()
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to recv invoice update: %v", err)
|
2016-10-16 00:45:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The invoice update should exactly match the invoice created
|
|
|
|
// above, but should now be settled.
|
|
|
|
if !invoiceUpdate.Settled {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("invoice not settled but shoudl be")
|
2016-10-16 00:45:26 +03:00
|
|
|
}
|
|
|
|
if !bytes.Equal(invoiceUpdate.RPreimage, invoice.RPreimage) {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("payment preimages don't match: expected %v, got %v",
|
2016-10-16 00:45:26 +03:00
|
|
|
invoice.RPreimage, invoiceUpdate.RPreimage)
|
|
|
|
}
|
|
|
|
|
|
|
|
close(updateSent)
|
|
|
|
}()
|
|
|
|
|
2017-03-15 06:06:33 +03:00
|
|
|
// Wait for the channel to be recognized by both Alice and Bob before
|
|
|
|
// continuing the rest of the test.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(roasbeef): will need to make num blocks to advertise a
|
|
|
|
// node param
|
|
|
|
t.Fatalf("channel not seen by alice before timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-10-16 00:45:26 +03:00
|
|
|
// With the assertion above set up, send a payment from Alice to Bob
|
|
|
|
// which should finalize and settle the invoice.
|
|
|
|
sendStream, err := net.Alice.SendPayment(ctxb)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to create alice payment stream: %v", err)
|
2016-10-16 00:45:26 +03:00
|
|
|
}
|
|
|
|
sendReq := &lnrpc.SendRequest{
|
|
|
|
PaymentHash: invoiceResp.RHash,
|
|
|
|
Dest: net.Bob.PubKey[:],
|
|
|
|
Amt: paymentAmt,
|
|
|
|
}
|
|
|
|
if err := sendStream.Send(sendReq); err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
2016-10-16 00:45:26 +03:00
|
|
|
}
|
2017-05-19 15:18:21 +03:00
|
|
|
if resp, err := sendStream.Recv(); err != nil {
|
|
|
|
t.Fatalf("payment stream has been closed: %v", err)
|
|
|
|
} else if resp.PaymentError != "" {
|
|
|
|
t.Fatalf("error when attempting recv: %v",
|
|
|
|
resp.PaymentError)
|
2016-10-16 00:45:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Second * 5):
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("update not sent after 5 seconds")
|
2016-10-16 00:45:26 +03:00
|
|
|
case <-updateSent: // Fall through on success
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
2016-10-16 00:45:26 +03:00
|
|
|
}
|
|
|
|
|
2016-10-17 07:36:43 +03:00
|
|
|
// testBasicChannelCreation test multiple channel opening and closing.
|
2016-10-24 05:00:09 +03:00
|
|
|
func testBasicChannelCreation(net *networkHarness, t *harnessTest) {
|
|
|
|
const (
|
|
|
|
numChannels = 2
|
|
|
|
timeout = time.Duration(time.Second * 5)
|
2017-08-08 02:30:29 +03:00
|
|
|
amount = maxFundingAmount
|
2016-10-24 05:00:09 +03:00
|
|
|
)
|
2016-10-17 07:36:43 +03:00
|
|
|
|
|
|
|
// Open the channel between Alice and Bob, asserting that the
|
|
|
|
// channel has been properly open on-chain.
|
2016-10-24 05:00:09 +03:00
|
|
|
chanPoints := make([]*lnrpc.ChannelPoint, numChannels)
|
|
|
|
for i := 0; i < numChannels; i++ {
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
chanPoints[i] = openChannelAndAssert(ctx, t, net, net.Alice,
|
2017-01-10 06:34:22 +03:00
|
|
|
net.Bob, amount, 0)
|
2017-08-09 12:17:24 +03:00
|
|
|
|
|
|
|
// We need to give Bob a bit of time to make sure the newly
|
|
|
|
// opened channel is not still pending.
|
|
|
|
if i != numChannels-1 {
|
|
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
}
|
2016-10-17 07:36:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close the channel between Alice and Bob, asserting that the
|
|
|
|
// channel has been properly closed on-chain.
|
|
|
|
for _, chanPoint := range chanPoints {
|
2016-10-24 05:00:09 +03:00
|
|
|
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctx, t, net, net.Alice, chanPoint, false)
|
2016-10-17 07:36:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 16:24:56 +03:00
|
|
|
// testMaxPendingChannels checks that error is returned from remote peer if
|
|
|
|
// max pending channel number was exceeded and that '--maxpendingchannels' flag
|
|
|
|
// exists and works properly.
|
2016-10-24 05:00:09 +03:00
|
|
|
func testMaxPendingChannels(net *networkHarness, t *harnessTest) {
|
2016-10-15 16:24:56 +03:00
|
|
|
maxPendingChannels := defaultMaxPendingChannels + 1
|
2017-08-08 02:30:29 +03:00
|
|
|
amount := maxFundingAmount
|
2016-10-15 16:24:56 +03:00
|
|
|
|
|
|
|
timeout := time.Duration(time.Second * 10)
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
|
|
|
|
|
|
|
// Create a new node (Carol) with greater number of max pending
|
|
|
|
// channels.
|
|
|
|
args := []string{
|
|
|
|
fmt.Sprintf("--maxpendingchannels=%v", maxPendingChannels),
|
|
|
|
}
|
|
|
|
carol, err := net.NewNode(args)
|
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
2016-10-24 05:00:09 +03:00
|
|
|
|
|
|
|
ctx, _ = context.WithTimeout(context.Background(), timeout)
|
2016-10-15 16:24:56 +03:00
|
|
|
if err := net.ConnectNodes(ctx, net.Alice, carol); err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to connect carol to alice: %v", err)
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
|
2016-10-24 05:00:09 +03:00
|
|
|
ctx, _ = context.WithTimeout(context.Background(), timeout)
|
2016-10-15 16:24:56 +03:00
|
|
|
carolBalance := btcutil.Amount(maxPendingChannels) * amount
|
|
|
|
if err := net.SendCoins(ctx, carolBalance, carol); err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send open channel requests without generating new blocks thereby
|
2017-03-15 06:06:33 +03:00
|
|
|
// increasing pool of pending channels. Then check that we can't open
|
|
|
|
// the channel if the number of pending channels exceed max value.
|
2016-10-15 16:24:56 +03:00
|
|
|
openStreams := make([]lnrpc.Lightning_OpenChannelClient, maxPendingChannels)
|
|
|
|
for i := 0; i < maxPendingChannels; i++ {
|
2016-10-24 05:00:09 +03:00
|
|
|
ctx, _ = context.WithTimeout(context.Background(), timeout)
|
2017-01-10 06:34:22 +03:00
|
|
|
stream, err := net.OpenChannel(ctx, net.Alice, carol, amount,
|
2017-07-31 06:24:48 +03:00
|
|
|
0)
|
2016-10-15 16:24:56 +03:00
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to open channel: %v", err)
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
openStreams[i] = stream
|
|
|
|
}
|
|
|
|
|
|
|
|
// Carol exhausted available amount of pending channels, next open
|
|
|
|
// channel request should cause ErrorGeneric to be sent back to Alice.
|
2016-10-24 05:00:09 +03:00
|
|
|
ctx, _ = context.WithTimeout(context.Background(), timeout)
|
2017-07-31 06:24:48 +03:00
|
|
|
_, err = net.OpenChannel(ctx, net.Alice, carol, amount, 0)
|
2016-10-15 16:24:56 +03:00
|
|
|
if err == nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("error wasn't received")
|
2017-01-25 04:12:51 +03:00
|
|
|
} else if grpc.Code(err) != lnwire.ErrMaxPendingChannels.ToGrpcCode() {
|
2016-10-26 15:25:42 +03:00
|
|
|
t.Fatalf("not expected error was received: %v", err)
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
|
2017-03-15 06:06:33 +03:00
|
|
|
// For now our channels are in pending state, in order to not interfere
|
|
|
|
// with other tests we should clean up - complete opening of the
|
|
|
|
// channel and then close it.
|
2016-10-15 16:24:56 +03:00
|
|
|
|
2017-03-15 06:06:33 +03:00
|
|
|
// Mine a block, then wait for node's to notify us that the channel has
|
|
|
|
// been opened. The funding transactions should be found within the
|
2016-10-15 16:24:56 +03:00
|
|
|
// newly mined block.
|
2016-10-24 05:00:09 +03:00
|
|
|
block := mineBlocks(t, net, 1)[0]
|
2016-10-15 16:24:56 +03:00
|
|
|
|
|
|
|
chanPoints := make([]*lnrpc.ChannelPoint, maxPendingChannels)
|
|
|
|
for i, stream := range openStreams {
|
2016-12-08 09:38:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(context.Background(), timeout)
|
|
|
|
fundingChanPoint, err := net.WaitForChannelOpen(ctxt, stream)
|
2016-10-15 16:24:56 +03:00
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("error while waiting for channel open: %v", err)
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
|
2017-01-06 00:56:27 +03:00
|
|
|
fundingTxID, err := chainhash.NewHash(fundingChanPoint.FundingTxid)
|
2016-10-15 16:24:56 +03:00
|
|
|
if err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to create sha hash: %v", err)
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
|
2017-03-15 06:06:33 +03:00
|
|
|
// Ensure that the funding transaction enters a block, and is
|
|
|
|
// properly advertised by Alice.
|
2016-10-24 05:00:09 +03:00
|
|
|
assertTxInBlock(t, block, fundingTxID)
|
2017-03-15 06:06:33 +03:00
|
|
|
ctxt, _ = context.WithTimeout(context.Background(), timeout)
|
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, fundingChanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("channel not seen on network before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2016-10-15 16:24:56 +03:00
|
|
|
|
|
|
|
// The channel should be listed in the peer information
|
|
|
|
// returned by both peers.
|
|
|
|
chanPoint := wire.OutPoint{
|
|
|
|
Hash: *fundingTxID,
|
|
|
|
Index: fundingChanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
if err := net.AssertChannelExists(ctx, net.Alice, &chanPoint); err != nil {
|
2016-10-24 05:00:09 +03:00
|
|
|
t.Fatalf("unable to assert channel existence: %v", err)
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
chanPoints[i] = fundingChanPoint
|
|
|
|
}
|
|
|
|
|
2017-01-07 23:48:15 +03:00
|
|
|
// Next, close the channel between Alice and Carol, asserting that the
|
|
|
|
// channel has been properly closed on-chain.
|
2016-10-15 16:24:56 +03:00
|
|
|
for _, chanPoint := range chanPoints {
|
2016-12-31 03:41:59 +03:00
|
|
|
ctxt, _ := context.WithTimeout(context.Background(), timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
2017-01-07 23:48:15 +03:00
|
|
|
|
|
|
|
// Finally, shutdown the node we created for the duration of the tests,
|
|
|
|
// only leaving the two seed nodes (Alice and Bob) within our test
|
|
|
|
// network.
|
2017-03-15 06:06:33 +03:00
|
|
|
if err := carol.Shutdown(); err != nil {
|
2017-01-07 23:48:15 +03:00
|
|
|
t.Fatalf("unable to shutdown carol: %v", err)
|
|
|
|
}
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
|
2016-11-22 06:16:44 +03:00
|
|
|
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()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRevokedCloseRetribution(net *networkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
const (
|
|
|
|
timeout = time.Duration(time.Second * 5)
|
2017-08-08 02:30:29 +03:00
|
|
|
chanAmt = maxFundingAmount
|
2016-11-22 06:16:44 +03:00
|
|
|
paymentAmt = 10000
|
|
|
|
numInvoices = 6
|
|
|
|
)
|
|
|
|
|
|
|
|
// In order to test Alice's response to an uncooperative channel
|
|
|
|
// closure by Bob, we'll first open up a channel between them with a
|
|
|
|
// 0.5 BTC value.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
chanPoint := openChannelAndAssert(ctxt, t, net, net.Alice, net.Bob,
|
2017-01-10 06:34:22 +03:00
|
|
|
chanAmt, 0)
|
2016-11-22 06:16:44 +03:00
|
|
|
|
|
|
|
// With the channel open, we'll create a few invoices for Bob that
|
|
|
|
// Alice will pay to in order to advance the state of the channel.
|
|
|
|
bobPaymentHashes := make([][]byte, numInvoices)
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
2016-11-22 06:51:58 +03:00
|
|
|
preimage := bytes.Repeat([]byte{byte(255 - i)}, 32)
|
2016-11-22 06:16:44 +03:00
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
|
|
|
resp, err := net.Bob.AddInvoice(ctxb, invoice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bobPaymentHashes[i] = resp.RHash
|
|
|
|
}
|
|
|
|
|
|
|
|
// As we'll be querying the state of bob's channels frequently we'll
|
|
|
|
// create a closure helper function for the purpose.
|
|
|
|
getBobChanInfo := func() (*lnrpc.ActiveChannel, error) {
|
|
|
|
req := &lnrpc.ListChannelsRequest{}
|
|
|
|
bobChannelInfo, err := net.Bob.ListChannels(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(bobChannelInfo.Channels) != 1 {
|
|
|
|
t.Fatalf("bob should only have a single channel, instead he has %v",
|
|
|
|
len(bobChannelInfo.Channels))
|
|
|
|
}
|
|
|
|
|
|
|
|
return bobChannelInfo.Channels[0], nil
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:46:58 +03:00
|
|
|
// Wait for Alice to receive the channel edge from the funding manager.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
err := net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't see the alice->bob channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-11-22 06:16:44 +03:00
|
|
|
// Open up a payment stream to Alice that we'll use to send payment to
|
|
|
|
// Bob. We also create a small helper function to send payments to Bob,
|
|
|
|
// consuming the payment hashes we generated above.
|
|
|
|
alicePayStream, err := net.Alice.SendPayment(ctxb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for alice: %v", err)
|
|
|
|
}
|
|
|
|
sendPayments := func(start, stop int) error {
|
|
|
|
for i := start; i < stop; i++ {
|
|
|
|
sendReq := &lnrpc.SendRequest{
|
|
|
|
PaymentHash: bobPaymentHashes[i],
|
|
|
|
Dest: net.Bob.PubKey[:],
|
|
|
|
Amt: paymentAmt,
|
|
|
|
}
|
|
|
|
if err := alicePayStream.Send(sendReq); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-19 15:18:21 +03:00
|
|
|
if resp, err := alicePayStream.Recv(); err != nil {
|
|
|
|
t.Fatalf("payment stream has been closed: %v", err)
|
|
|
|
} else if resp.PaymentError != "" {
|
|
|
|
t.Fatalf("error when attempting recv: %v",
|
|
|
|
resp.PaymentError)
|
2016-11-22 06:16:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send payments from Alice to Bob using 3 of Bob's payment hashes
|
|
|
|
// generated above.
|
|
|
|
if err := sendPayments(0, numInvoices/2); err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next query for Bob's channel state, as we sent 3 payments of 10k
|
|
|
|
// satoshis each, Bob should now see his balance as being 30k satoshis.
|
2017-04-18 02:09:47 +03:00
|
|
|
time.Sleep(time.Millisecond * 200)
|
2016-11-22 06:16:44 +03:00
|
|
|
bobChan, err := getBobChanInfo()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get bob's channel info: %v", err)
|
|
|
|
}
|
|
|
|
if bobChan.LocalBalance != 30000 {
|
|
|
|
t.Fatalf("bob's balance is incorrect, got %v, expected %v",
|
|
|
|
bobChan.LocalBalance, 30000)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab Bob's current commitment height (update number), we'll later
|
|
|
|
// revert him to this state after additional updates to force him to
|
|
|
|
// broadcast this soon to be revoked state.
|
|
|
|
bobStateNumPreCopy := bobChan.NumUpdates
|
|
|
|
|
|
|
|
// Create a temporary file to house Bob's database state at this
|
|
|
|
// particular point in history.
|
|
|
|
bobTempDbPath, err := ioutil.TempDir("", "bob-past-state")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp db folder: %v", err)
|
|
|
|
}
|
|
|
|
bobTempDbFile := filepath.Join(bobTempDbPath, "channel.db")
|
|
|
|
defer os.Remove(bobTempDbPath)
|
|
|
|
|
|
|
|
// With the temporary file created, copy Bob's current state into the
|
|
|
|
// temporary file we created above. Later after more updates, we'll
|
|
|
|
// restore this state.
|
2017-05-03 06:38:03 +03:00
|
|
|
bobDbPath := filepath.Join(net.Bob.cfg.DataDir, "simnet/bitcoin/channel.db")
|
2016-11-22 06:16:44 +03:00
|
|
|
if err := copyFile(bobTempDbFile, bobDbPath); err != nil {
|
|
|
|
t.Fatalf("unable to copy database files: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, send payments from Alice to Bob, consuming Bob's remaining
|
|
|
|
// payment hashes.
|
|
|
|
if err := sendPayments(numInvoices/2, numInvoices); err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bobChan, err = getBobChanInfo()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get bob chan info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we shutdown Bob, copying over the his temporary database state
|
|
|
|
// which has the *prior* channel state over his current most up to date
|
|
|
|
// state. With this, we essentially force Bob to travel back in time
|
|
|
|
// within the channel's history.
|
|
|
|
if err = net.RestartNode(net.Bob, func() error {
|
|
|
|
return os.Rename(bobTempDbFile, bobDbPath)
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("unable to restart node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now query for Bob's channel state, it should show that he's at a
|
|
|
|
// state number in the past, not the *latest* state.
|
|
|
|
bobChan, err = getBobChanInfo()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get bob chan info: %v", err)
|
|
|
|
}
|
|
|
|
if bobChan.NumUpdates != bobStateNumPreCopy {
|
2016-11-22 06:51:58 +03:00
|
|
|
t.Fatalf("db copy failed: %v", bobChan.NumUpdates)
|
2016-11-22 06:16:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now force Bob to execute a *force* channel closure by unilaterally
|
|
|
|
// broadcasting his current channel state. This is actually the
|
|
|
|
// commitment transaction of a prior *revoked* state, so he'll soon
|
|
|
|
// feel the wrath of Alice's retribution.
|
2017-02-23 01:49:04 +03:00
|
|
|
breachTXID := closeChannelAndAssert(ctxb, t, net, net.Bob, chanPoint,
|
2016-11-22 06:16:44 +03:00
|
|
|
true)
|
|
|
|
|
|
|
|
// Query the mempool for Alice's justice transaction, this should be
|
|
|
|
// broadcast as Bob's contract breaching transaction gets confirmed
|
|
|
|
// above.
|
2017-01-06 00:56:27 +03:00
|
|
|
var justiceTXID *chainhash.Hash
|
2016-11-29 06:43:57 +03:00
|
|
|
breakTimeout := time.After(time.Second * 5)
|
2016-11-22 06:16:44 +03:00
|
|
|
poll:
|
|
|
|
for {
|
|
|
|
select {
|
2016-11-29 06:43:57 +03:00
|
|
|
case <-breakTimeout:
|
2016-11-22 06:16:44 +03:00
|
|
|
t.Fatalf("justice tx not found in mempool")
|
|
|
|
default:
|
2016-11-29 06:43:57 +03:00
|
|
|
}
|
2016-11-22 06:16:44 +03:00
|
|
|
|
2016-11-29 06:43:57 +03:00
|
|
|
mempool, err := net.Miner.Node.GetRawMempool()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get mempool: %v", err)
|
|
|
|
}
|
2016-11-22 06:16:44 +03:00
|
|
|
|
2016-11-29 06:43:57 +03:00
|
|
|
if len(mempool) == 0 {
|
|
|
|
continue
|
2016-11-22 06:16:44 +03:00
|
|
|
}
|
2016-11-29 06:43:57 +03:00
|
|
|
|
|
|
|
justiceTXID = mempool[0]
|
|
|
|
break poll
|
2016-11-22 06:16:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query for the mempool transaction found above. Then assert that all
|
|
|
|
// the inputs of this transaction are spending outputs generated by
|
|
|
|
// Bob's breach transaction above.
|
|
|
|
justiceTx, err := net.Miner.Node.GetRawTransaction(justiceTXID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for justice tx: %v", err)
|
|
|
|
}
|
|
|
|
for _, txIn := range justiceTx.MsgTx().TxIn {
|
|
|
|
if !bytes.Equal(txIn.PreviousOutPoint.Hash[:], breachTXID[:]) {
|
2016-11-22 06:51:58 +03:00
|
|
|
t.Fatalf("justice tx not spending commitment utxo "+
|
|
|
|
"instead is: %v", txIn.PreviousOutPoint)
|
2016-11-22 06:16:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now mine a block, this transaction should include Alice's justice
|
|
|
|
// transaction which was just accepted into the mempool.
|
|
|
|
block := mineBlocks(t, net, 1)[0]
|
|
|
|
|
|
|
|
// The block should have exactly *two* transactions, one of which is
|
|
|
|
// the justice transaction.
|
2016-12-13 02:55:36 +03:00
|
|
|
if len(block.Transactions) != 2 {
|
2016-11-22 06:51:58 +03:00
|
|
|
t.Fatalf("transaction wasn't mined")
|
2016-11-22 06:16:44 +03:00
|
|
|
}
|
2017-01-06 00:56:27 +03:00
|
|
|
justiceSha := block.Transactions[1].TxHash()
|
|
|
|
if !bytes.Equal(justiceTx.Hash()[:], justiceSha[:]) {
|
2016-11-22 06:16:44 +03:00
|
|
|
t.Fatalf("justice tx wasn't mined")
|
|
|
|
}
|
|
|
|
|
2016-12-27 08:52:56 +03:00
|
|
|
// Finally, obtain Alice's channel state, she shouldn't report any
|
2016-11-22 06:16:44 +03:00
|
|
|
// channel as she just successfully brought Bob to justice by sweeping
|
|
|
|
// all the channel funds.
|
|
|
|
req := &lnrpc.ListChannelsRequest{}
|
|
|
|
aliceChanInfo, err := net.Alice.ListChannels(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for alice's channels: %v", err)
|
|
|
|
}
|
|
|
|
if len(aliceChanInfo.Channels) != 0 {
|
2016-11-22 06:51:58 +03:00
|
|
|
t.Fatalf("alice shouldn't have a channel: %v",
|
2016-11-22 06:16:44 +03:00
|
|
|
spew.Sdump(aliceChanInfo.Channels))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 07:25:45 +03:00
|
|
|
func testHtlcErrorPropagation(net *networkHarness, t *harnessTest) {
|
|
|
|
// In this test we wish to exercise the daemon's correct parsing,
|
|
|
|
// handling, and propagation of errors that occur while processing a
|
|
|
|
// multi-hop payment.
|
|
|
|
timeout := time.Duration(time.Second * 5)
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
2017-08-08 02:30:29 +03:00
|
|
|
const chanAmt = maxFundingAmount
|
2017-01-08 07:25:45 +03:00
|
|
|
|
|
|
|
// First establish a channel with a capacity of 0.5 BTC between Alice
|
|
|
|
// and Bob.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(ctxt, t, net, net.Alice, net.Bob,
|
2017-01-10 06:34:22 +03:00
|
|
|
chanAmt, 0)
|
2017-03-15 06:06:33 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
if err := net.Alice.WaitForNetworkChannelOpen(ctxt, chanPointAlice); err != nil {
|
|
|
|
t.Fatalf("channel not seen by alice before timeout: %v", err)
|
|
|
|
}
|
2017-01-08 07:25:45 +03:00
|
|
|
|
2017-05-01 21:45:02 +03:00
|
|
|
commitFee := calcStaticFee(0)
|
2017-01-08 07:25:45 +03:00
|
|
|
assertBaseBalance := func() {
|
|
|
|
balReq := &lnrpc.ChannelBalanceRequest{}
|
|
|
|
aliceBal, err := net.Alice.ChannelBalance(ctxb, balReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get channel balance: %v", err)
|
|
|
|
}
|
|
|
|
bobBal, err := net.Bob.ChannelBalance(ctxb, balReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get channel balance: %v", err)
|
|
|
|
}
|
2017-05-01 21:45:02 +03:00
|
|
|
if aliceBal.Balance != int64(chanAmt-commitFee) {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("alice has an incorrect balance: expected %v got %v",
|
2017-05-01 21:45:02 +03:00
|
|
|
int64(chanAmt-commitFee), aliceBal)
|
2017-01-08 07:25:45 +03:00
|
|
|
}
|
2017-05-01 21:45:02 +03:00
|
|
|
if bobBal.Balance != int64(chanAmt-commitFee) {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("bob has an incorrect balance: expected %v got %v",
|
2017-05-01 21:45:02 +03:00
|
|
|
int64(chanAmt-commitFee), bobBal)
|
2017-01-08 07:25:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we'd like to test some multi-hop failure scenarios, we'll
|
|
|
|
// introduce another node into our test network: Carol.
|
|
|
|
carol, err := net.NewNode(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll create a connection from Bob to Carol, and open a
|
|
|
|
// channel between them so we have the topology: Alice -> Bob -> Carol.
|
|
|
|
// The channel created will be of lower capacity that the one created
|
|
|
|
// above.
|
|
|
|
if err := net.ConnectNodes(ctxb, net.Bob, carol); err != nil {
|
|
|
|
t.Fatalf("unable to connect bob to carol: %v", err)
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-08-08 02:30:29 +03:00
|
|
|
const bobChanAmt = maxFundingAmount
|
2017-02-23 01:49:04 +03:00
|
|
|
chanPointBob := openChannelAndAssert(ctxt, t, net, net.Bob, carol,
|
2017-01-10 06:34:22 +03:00
|
|
|
chanAmt, 0)
|
2017-01-08 07:25:45 +03:00
|
|
|
|
2017-01-30 04:16:49 +03:00
|
|
|
// Ensure that Alice has Carol in her routing table before proceeding.
|
|
|
|
nodeInfoReq := &lnrpc.NodeInfoRequest{
|
|
|
|
PubKey: carol.PubKeyStr,
|
|
|
|
}
|
|
|
|
checkTableTimeout := time.After(time.Second * 10)
|
|
|
|
checkTableTicker := time.NewTicker(100 * time.Millisecond)
|
|
|
|
defer checkTableTicker.Stop()
|
|
|
|
|
|
|
|
out:
|
2017-03-15 06:06:33 +03:00
|
|
|
// TODO(roasbeef): make into async hook for node announcements
|
2017-01-30 04:16:49 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-checkTableTicker.C:
|
|
|
|
_, err := net.Alice.GetNodeInfo(ctxb, nodeInfoReq)
|
|
|
|
if err != nil && strings.Contains(err.Error(),
|
|
|
|
"unable to find") {
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
break out
|
|
|
|
case <-checkTableTimeout:
|
|
|
|
t.Fatalf("carol's node announcement didn't propagate within " +
|
|
|
|
"the timeout period")
|
|
|
|
}
|
|
|
|
}
|
2017-01-08 07:25:45 +03:00
|
|
|
|
|
|
|
// With the channels, open we can now start to test our multi-hop error
|
|
|
|
// scenarios. First, we'll generate an invoice from carol that we'll
|
|
|
|
// use to test some error cases.
|
|
|
|
const payAmt = 10000
|
|
|
|
invoiceReq := &lnrpc.Invoice{
|
|
|
|
Memo: "kek99",
|
|
|
|
Value: payAmt,
|
|
|
|
}
|
|
|
|
carolInvoice, err := carol.AddInvoice(ctxb, invoiceReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate carol invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-15 06:06:33 +03:00
|
|
|
// Before we send the payment, ensure that the announcement of the new
|
|
|
|
// channel has been processed by Alice.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
if err := net.Alice.WaitForNetworkChannelOpen(ctxt, chanPointBob); err != nil {
|
|
|
|
t.Fatalf("channel not seen by alice before timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-01-08 07:25:45 +03:00
|
|
|
// TODO(roasbeef): return failure response rather than failing entire
|
|
|
|
// stream on payment error.
|
|
|
|
alicePayStream, err := net.Alice.SendPayment(ctxb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the first scenario, we'll test the cancellation of an HTLC with
|
|
|
|
// an unknown payment hash.
|
|
|
|
sendReq := &lnrpc.SendRequest{
|
|
|
|
PaymentHash: bytes.Repeat([]byte("Z"), 32), // Wrong hash.
|
|
|
|
Dest: carol.PubKey[:],
|
|
|
|
Amt: payAmt,
|
|
|
|
}
|
|
|
|
if err := alicePayStream.Send(sendReq); err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The payment should've resulted in an error since we went it with the
|
|
|
|
// wrong payment hash.
|
2017-05-19 15:18:21 +03:00
|
|
|
|
|
|
|
if resp, err := alicePayStream.Recv(); err != nil {
|
|
|
|
t.Fatalf("payment stream has been closed: %v", err)
|
|
|
|
} else if resp.PaymentError == "" {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("payment should have been rejected due to invalid " +
|
|
|
|
"payment hash")
|
2017-06-29 16:40:45 +03:00
|
|
|
} else if !strings.Contains(resp.PaymentError,
|
|
|
|
lnwire.CodeUnknownPaymentHash.String()) {
|
2017-01-08 07:25:45 +03:00
|
|
|
// TODO(roasbeef): make into proper gRPC error code
|
2017-06-29 16:40:45 +03:00
|
|
|
t.Fatalf("payment should have failed due to unknown payment hash, "+
|
|
|
|
"instead failed due to: %v", resp.PaymentError)
|
2017-01-08 07:25:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The balances of all parties should be the same as initially since
|
|
|
|
// the HTLC was cancelled.
|
|
|
|
assertBaseBalance()
|
|
|
|
|
|
|
|
// We need to create another payment stream since the first one was
|
|
|
|
// closed due to an error.
|
|
|
|
alicePayStream, err = net.Alice.SendPayment(ctxb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll test the case of a recognized payHash but, an incorrect
|
|
|
|
// value on the extended HTLC.
|
|
|
|
sendReq = &lnrpc.SendRequest{
|
|
|
|
PaymentHash: carolInvoice.RHash,
|
|
|
|
Dest: carol.PubKey[:],
|
|
|
|
Amt: 1000, // 10k satoshis are expected.
|
|
|
|
}
|
|
|
|
if err := alicePayStream.Send(sendReq); err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The payment should fail with an error since we sent 1k satoshis
|
|
|
|
// isn't of 10k as was requested.
|
2017-05-19 15:18:21 +03:00
|
|
|
|
|
|
|
if resp, err := alicePayStream.Recv(); err != nil {
|
|
|
|
t.Fatalf("payment stream has been closed: %v", err)
|
|
|
|
} else if resp.PaymentError == "" {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("payment should have been rejected due to wrong " +
|
|
|
|
"HTLC amount")
|
2017-06-29 16:40:45 +03:00
|
|
|
} else if !strings.Contains(resp.PaymentError,
|
|
|
|
lnwire.CodeIncorrectPaymentAmount.String()) {
|
2017-01-30 04:07:54 +03:00
|
|
|
t.Fatalf("payment should have failed due to wrong amount, "+
|
2017-06-29 16:40:45 +03:00
|
|
|
"instead failed due to: %v", resp.PaymentError)
|
2017-01-08 07:25:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The balances of all parties should be the same as initially since
|
|
|
|
// the HTLC was cancelled.
|
|
|
|
assertBaseBalance()
|
|
|
|
|
|
|
|
// Next we'll test an error that occurs mid-route due to an outgoing
|
|
|
|
// link having insufficient capacity. In order to do so, we'll first
|
|
|
|
// need to unbalance the link connecting Bob<->Carol.
|
|
|
|
bobPayStream, err := net.Bob.SendPayment(ctxb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// To do so, we'll push most of the funds in the channel over to
|
|
|
|
// Alice's side, leaving on 10k satoshis of available balance for bob.
|
2017-08-22 10:34:11 +03:00
|
|
|
// There's a max payment amount, so we'll have to do this
|
|
|
|
// incrementally.
|
|
|
|
amtToSend := int64(chanAmt) - 10000
|
|
|
|
amtSent := int64(0)
|
|
|
|
for amtSent != amtToSend {
|
|
|
|
// We'll send in chunks of the max payment amount. If we're
|
|
|
|
// about to send too much, then we'll only send the amount
|
|
|
|
// remaining.
|
|
|
|
toSend := int64(maxPaymentMSat.ToSatoshis())
|
|
|
|
if toSend+amtSent > amtToSend {
|
|
|
|
toSend = amtToSend - amtSent
|
|
|
|
}
|
2017-05-19 15:18:21 +03:00
|
|
|
|
2017-08-22 10:34:11 +03:00
|
|
|
invoiceReq = &lnrpc.Invoice{
|
|
|
|
Value: toSend,
|
|
|
|
}
|
|
|
|
carolInvoice2, err := carol.AddInvoice(ctxb, invoiceReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate carol invoice: %v", err)
|
|
|
|
}
|
|
|
|
if err := bobPayStream.Send(&lnrpc.SendRequest{
|
|
|
|
PaymentRequest: carolInvoice2.PaymentRequest,
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp, err := bobPayStream.Recv(); err != nil {
|
|
|
|
t.Fatalf("payment stream has been closed: %v", err)
|
|
|
|
} else if resp.PaymentError != "" {
|
|
|
|
t.Fatalf("bob's payment failed: %v", resp.PaymentError)
|
|
|
|
}
|
|
|
|
|
|
|
|
amtSent += toSend
|
2017-01-08 07:25:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, Alice has 50mil satoshis on her side of the channel,
|
|
|
|
// but Bob only has 10k available on his side of the channel. So a
|
|
|
|
// payment from Alice to Carol worth 100k satoshis should fail.
|
|
|
|
alicePayStream, err = net.Alice.SendPayment(ctxb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream: %v", err)
|
|
|
|
}
|
|
|
|
invoiceReq = &lnrpc.Invoice{
|
|
|
|
Value: 100000,
|
|
|
|
}
|
|
|
|
carolInvoice3, err := carol.AddInvoice(ctxb, invoiceReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate carol invoice: %v", err)
|
|
|
|
}
|
|
|
|
if err := alicePayStream.Send(&lnrpc.SendRequest{
|
|
|
|
PaymentRequest: carolInvoice3.PaymentRequest,
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
2017-05-19 15:18:21 +03:00
|
|
|
|
|
|
|
if resp, err := alicePayStream.Recv(); err != nil {
|
|
|
|
t.Fatalf("payment stream has been closed: %v", err)
|
|
|
|
} else if resp.PaymentError == "" {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("payment should fail due to insufficient "+
|
|
|
|
"capacity: %v", err)
|
2017-06-29 16:40:45 +03:00
|
|
|
} else if !strings.Contains(resp.PaymentError,
|
|
|
|
lnwire.CodeTemporaryChannelFailure.String()) {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("payment should fail due to insufficient capacity, "+
|
2017-06-29 16:40:45 +03:00
|
|
|
"instead: %v", resp.PaymentError)
|
2017-01-08 07:25:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// For our final test, we'll ensure that if a target link isn't
|
|
|
|
// available for what ever reason then the payment fails accordingly.
|
|
|
|
//
|
|
|
|
// We'll attempt to complete the original invoice we created with Carol
|
|
|
|
// above, but before we do so, Carol will go offline, resulting in a
|
|
|
|
// failed payment.
|
2017-03-15 06:06:33 +03:00
|
|
|
if err := carol.Shutdown(); err != nil {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("unable to shutdown carol: %v", err)
|
|
|
|
}
|
2017-01-08 08:20:02 +03:00
|
|
|
time.Sleep(time.Second * 2)
|
2017-01-08 07:25:45 +03:00
|
|
|
alicePayStream, err = net.Alice.SendPayment(ctxb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream: %v", err)
|
|
|
|
}
|
|
|
|
if err := alicePayStream.Send(&lnrpc.SendRequest{
|
|
|
|
PaymentRequest: carolInvoice.PaymentRequest,
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
2017-05-19 15:18:21 +03:00
|
|
|
|
|
|
|
if resp, err := alicePayStream.Recv(); err != nil {
|
|
|
|
t.Fatalf("payment stream has been closed: %v", err)
|
|
|
|
} else if resp.PaymentError == "" {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("payment should have failed")
|
2017-06-29 16:40:45 +03:00
|
|
|
} else if !strings.Contains(resp.PaymentError,
|
|
|
|
lnwire.CodeUnknownNextPeer.String()) {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("payment should fail due to unknown hop, instead: %v",
|
2017-06-29 16:40:45 +03:00
|
|
|
resp.PaymentError)
|
2017-01-08 07:25:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, immediately close the channel. This function will also
|
|
|
|
// block until the channel is closed and will additionally assert the
|
|
|
|
// relevant channel closing post conditions.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
2017-01-08 07:25:45 +03:00
|
|
|
|
|
|
|
// Force close Bob's final channel, also mining enough blocks to
|
|
|
|
// trigger a sweep of the funds by the utxoNursery.
|
|
|
|
// TODO(roasbeef): use config value for default CSV here.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Bob, chanPointBob, true)
|
2017-01-08 07:25:45 +03:00
|
|
|
if _, err := net.Miner.Node.Generate(5); err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 08:23:19 +03:00
|
|
|
func testGraphTopologyNotifications(net *networkHarness, t *harnessTest) {
|
2017-08-08 02:30:29 +03:00
|
|
|
const chanAmt = maxFundingAmount
|
2017-03-14 08:23:19 +03:00
|
|
|
timeout := time.Duration(time.Second * 5)
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// We'll first start by establishing a notification client to Alice
|
|
|
|
// which'll send us notifications upon detected changes in the channel
|
|
|
|
// graph.
|
|
|
|
req := &lnrpc.GraphTopologySubscription{}
|
|
|
|
topologyClient, err := net.Alice.SubscribeChannelGraph(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create topology client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open a new channel between Alice and Bob.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
|
|
|
chanPoint := openChannelAndAssert(ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
chanAmt, 0)
|
|
|
|
|
|
|
|
// We'll launch a goroutine that'll be responsible for proxying all
|
|
|
|
// notifications recv'd from the client into the channel below.
|
|
|
|
quit := make(chan struct{})
|
2017-08-05 04:34:10 +03:00
|
|
|
graphUpdates := make(chan *lnrpc.GraphTopologyUpdate, 4)
|
2017-03-14 08:23:19 +03:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
graphUpdate, err := topologyClient.Recv()
|
2017-08-03 07:00:21 +03:00
|
|
|
select {
|
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2017-03-14 08:23:19 +03:00
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("unable to recv graph update: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-05 04:34:10 +03:00
|
|
|
select {
|
|
|
|
case graphUpdates <- graphUpdate:
|
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
}
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-07-14 22:47:52 +03:00
|
|
|
// The channel opening above should've triggered a few notifications
|
|
|
|
// sent to the notification client. We'll expect two channel updates,
|
|
|
|
// and two node announcements.
|
|
|
|
const numExpectedUpdates = 4
|
2017-03-14 08:23:19 +03:00
|
|
|
for i := 0; i < numExpectedUpdates; i++ {
|
|
|
|
select {
|
|
|
|
// Ensure that a new update for both created edges is properly
|
|
|
|
// dispatched to our registered client.
|
|
|
|
case graphUpdate := <-graphUpdates:
|
2017-08-05 04:34:10 +03:00
|
|
|
|
2017-07-14 22:47:52 +03:00
|
|
|
if len(graphUpdate.ChannelUpdates) > 0 {
|
|
|
|
chanUpdate := graphUpdate.ChannelUpdates[0]
|
|
|
|
if chanUpdate.Capacity != int64(chanAmt) {
|
|
|
|
t.Fatalf("channel capacities mismatch:"+
|
|
|
|
" expected %v, got %v", chanAmt,
|
|
|
|
btcutil.Amount(chanUpdate.Capacity))
|
|
|
|
}
|
|
|
|
switch chanUpdate.AdvertisingNode {
|
|
|
|
case net.Alice.PubKeyStr:
|
|
|
|
case net.Bob.PubKeyStr:
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown advertising node: %v",
|
|
|
|
chanUpdate.AdvertisingNode)
|
|
|
|
}
|
|
|
|
switch chanUpdate.ConnectingNode {
|
|
|
|
case net.Alice.PubKeyStr:
|
|
|
|
case net.Bob.PubKeyStr:
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown connecting node: %v",
|
|
|
|
chanUpdate.ConnectingNode)
|
|
|
|
}
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
|
|
|
|
2017-07-14 22:47:52 +03:00
|
|
|
if len(graphUpdate.NodeUpdates) > 0 {
|
|
|
|
nodeUpdate := graphUpdate.NodeUpdates[0]
|
|
|
|
switch nodeUpdate.IdentityKey {
|
|
|
|
case net.Alice.PubKeyStr:
|
|
|
|
case net.Bob.PubKeyStr:
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown node: %v",
|
|
|
|
nodeUpdate.IdentityKey)
|
|
|
|
}
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
2017-03-15 06:06:33 +03:00
|
|
|
case <-time.After(time.Second * 10):
|
2017-07-14 22:47:52 +03:00
|
|
|
t.Fatalf("timeout waiting for graph notification %v", i)
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, blockHeight, err := net.Miner.Node.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get current blockheight %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we'll test that updates upon a channel closure are properly sent
|
|
|
|
// when channels are closed within the network.
|
|
|
|
ctxt, _ = context.WithTimeout(context.Background(), timeout)
|
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
|
|
|
|
|
|
|
// Similar to the case above, we should receive another notification
|
|
|
|
// detailing the channel closure.
|
|
|
|
select {
|
|
|
|
case graphUpdate := <-graphUpdates:
|
|
|
|
if len(graphUpdate.ClosedChans) != 1 {
|
|
|
|
t.Fatalf("expected a single update, instead "+
|
|
|
|
"have %v", len(graphUpdate.ClosedChans))
|
|
|
|
}
|
|
|
|
|
|
|
|
closedChan := graphUpdate.ClosedChans[0]
|
|
|
|
if closedChan.ClosedHeight != uint32(blockHeight+1) {
|
|
|
|
t.Fatalf("close heights of channel mismatch: expected "+
|
2017-03-15 01:38:04 +03:00
|
|
|
"%v, got %v", blockHeight+1, closedChan.ClosedHeight)
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
|
|
|
if !bytes.Equal(closedChan.ChanPoint.FundingTxid,
|
|
|
|
chanPoint.FundingTxid) {
|
|
|
|
t.Fatalf("channel point hash mismatch: expected %v, "+
|
|
|
|
"got %v", chanPoint.FundingTxid,
|
|
|
|
closedChan.ChanPoint.FundingTxid)
|
|
|
|
}
|
|
|
|
if closedChan.ChanPoint.OutputIndex != chanPoint.OutputIndex {
|
|
|
|
t.Fatalf("output index mismatch: expected %v, got %v",
|
|
|
|
chanPoint.OutputIndex, closedChan.ChanPoint)
|
|
|
|
}
|
2017-03-15 06:06:33 +03:00
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("notification for channel closure not " +
|
|
|
|
"sent")
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// For the final portion of the test, we'll ensure that once a new node
|
2017-07-14 22:47:52 +03:00
|
|
|
// appears in the network, the proper notification is dispatched. Note
|
|
|
|
// that a node that does not have any channels open is ignored, so first
|
|
|
|
// we disconnect Alice and Bob, open a channel between Bob and Carol,
|
|
|
|
// and finally connect Alice to Bob again.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
if err := net.DisconnectNodes(ctxt, net.Alice, net.Bob); err != nil {
|
|
|
|
t.Fatalf("unable to disconnect alice and bob: %v", err)
|
|
|
|
}
|
2017-03-14 08:23:19 +03:00
|
|
|
carol, err := net.NewNode(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-07-14 22:47:52 +03:00
|
|
|
if err := net.ConnectNodes(ctxb, net.Bob, carol); err != nil {
|
|
|
|
t.Fatalf("unable to connect bob to carol: %v", err)
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
2017-07-14 22:47:52 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
chanPoint = openChannelAndAssert(ctxt, t, net, net.Bob, carol,
|
|
|
|
chanAmt, 0)
|
2017-03-14 08:23:19 +03:00
|
|
|
|
2017-07-14 22:47:52 +03:00
|
|
|
time.Sleep(time.Millisecond * 300)
|
|
|
|
|
|
|
|
// Reconnect Alice and Bob. This should result in the nodes syncing up
|
|
|
|
// their respective graph state, with the new addition being the
|
|
|
|
// existence of Carol in the graph, and also the channel between Bob
|
|
|
|
// and Carol. Note that we will also receive a node announcement from
|
|
|
|
// Bob, since a node will update its node announcement after a new
|
|
|
|
// channel is opened.
|
|
|
|
if err := net.ConnectNodes(ctxb, net.Alice, net.Bob); err != nil {
|
|
|
|
t.Fatalf("unable to connect alice to bob: %v", err)
|
|
|
|
}
|
2017-03-14 08:23:19 +03:00
|
|
|
|
2017-07-14 22:47:52 +03:00
|
|
|
// We should receive an update advertising the newly connected node,
|
|
|
|
// Bob's new node announcement, and the channel between Bob and Carol.
|
2017-08-05 04:34:10 +03:00
|
|
|
for i := 0; i < 3; i++ {
|
2017-07-14 22:47:52 +03:00
|
|
|
select {
|
|
|
|
case graphUpdate := <-graphUpdates:
|
|
|
|
|
|
|
|
if len(graphUpdate.NodeUpdates) > 0 {
|
|
|
|
nodeUpdate := graphUpdate.NodeUpdates[0]
|
|
|
|
switch nodeUpdate.IdentityKey {
|
|
|
|
case carol.PubKeyStr:
|
|
|
|
case net.Bob.PubKeyStr:
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown node update pubey: %v",
|
|
|
|
nodeUpdate.IdentityKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(graphUpdate.ChannelUpdates) > 0 {
|
|
|
|
chanUpdate := graphUpdate.ChannelUpdates[0]
|
|
|
|
if chanUpdate.Capacity != int64(chanAmt) {
|
|
|
|
t.Fatalf("channel capacities mismatch:"+
|
|
|
|
" expected %v, got %v", chanAmt,
|
|
|
|
btcutil.Amount(chanUpdate.Capacity))
|
|
|
|
}
|
|
|
|
switch chanUpdate.AdvertisingNode {
|
|
|
|
case carol.PubKeyStr:
|
|
|
|
case net.Bob.PubKeyStr:
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown advertising node: %v",
|
|
|
|
chanUpdate.AdvertisingNode)
|
|
|
|
}
|
|
|
|
switch chanUpdate.ConnectingNode {
|
|
|
|
case carol.PubKeyStr:
|
|
|
|
case net.Bob.PubKeyStr:
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown connecting node: %v",
|
|
|
|
chanUpdate.ConnectingNode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("timeout waiting for graph notification %v", i)
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 22:47:52 +03:00
|
|
|
// Close the channel between Bob and Carol.
|
|
|
|
ctxt, _ = context.WithTimeout(context.Background(), timeout)
|
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Bob, chanPoint, false)
|
|
|
|
|
2017-03-14 08:23:19 +03:00
|
|
|
close(quit)
|
|
|
|
|
|
|
|
// Finally, shutdown carol as our test has concluded successfully.
|
2017-03-15 06:06:33 +03:00
|
|
|
if err := carol.Shutdown(); err != nil {
|
2017-03-14 08:23:19 +03:00
|
|
|
t.Fatalf("unable to shutdown carol: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 03:24:22 +03:00
|
|
|
// testNodeAnnouncement ensures that when a node is started with one or more
|
|
|
|
// external IP addresses specified on the command line, that those addresses
|
|
|
|
// announced to the network and reported in the network graph.
|
|
|
|
func testNodeAnnouncement(net *networkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
ipAddresses := map[string]bool{
|
|
|
|
"192.168.1.1:8333": true,
|
|
|
|
"[2001:db8:85a3:8d3:1319:8a2e:370:7348]:8337": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
var lndArgs []string
|
2017-03-20 12:24:55 +03:00
|
|
|
for address := range ipAddresses {
|
2017-02-23 03:24:22 +03:00
|
|
|
lndArgs = append(lndArgs, "--externalip="+address)
|
|
|
|
}
|
|
|
|
|
|
|
|
dave, err := net.NewNode(lndArgs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-07-14 22:47:52 +03:00
|
|
|
// We must let Dave have an open channel before he can send a node
|
|
|
|
// announcement, so we open a channel with Bob,
|
|
|
|
if err := net.ConnectNodes(ctxb, net.Bob, dave); err != nil {
|
|
|
|
t.Fatalf("unable to connect bob to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := time.Duration(time.Second * 5)
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
|
|
|
chanPoint := openChannelAndAssert(ctxt, t, net, net.Bob, dave,
|
|
|
|
1000000, 0)
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 300)
|
|
|
|
|
|
|
|
// When Alice now connects with Dave, Alice will get his node announcement.
|
2017-02-23 03:24:22 +03:00
|
|
|
if err := net.ConnectNodes(ctxb, net.Alice, dave); err != nil {
|
|
|
|
t.Fatalf("unable to connect bob to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-04-17 01:27:28 +03:00
|
|
|
time.Sleep(time.Millisecond * 200)
|
2017-02-23 03:24:22 +03:00
|
|
|
req := &lnrpc.ChannelGraphRequest{}
|
|
|
|
chanGraph, err := net.Alice.DescribeGraph(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for alice's routing table: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range chanGraph.Nodes {
|
|
|
|
if node.PubKey == dave.PubKeyStr {
|
|
|
|
for _, address := range node.Addresses {
|
|
|
|
addrStr := address.String()
|
|
|
|
|
|
|
|
// parse the IP address from the string
|
|
|
|
// representation of the TCPAddr
|
|
|
|
parts := strings.Split(addrStr, "\"")
|
|
|
|
if ipAddresses[parts[3]] {
|
|
|
|
delete(ipAddresses, parts[3])
|
|
|
|
} else {
|
2017-03-25 11:40:33 +03:00
|
|
|
if !strings.HasPrefix(parts[3],
|
|
|
|
"127.0.0.1:") {
|
|
|
|
t.Fatalf("unexpected IP: %v",
|
|
|
|
parts[3])
|
|
|
|
}
|
2017-02-23 03:24:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(ipAddresses) != 0 {
|
|
|
|
t.Fatalf("expected IP addresses not in channel "+
|
|
|
|
"graph: %v", ipAddresses)
|
|
|
|
}
|
2017-04-17 01:27:28 +03:00
|
|
|
|
2017-07-14 22:47:52 +03:00
|
|
|
// Close the channel between Bob and Dave.
|
2017-08-19 08:54:56 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-07-14 22:47:52 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Bob, chanPoint, false)
|
|
|
|
|
2017-04-17 01:27:28 +03:00
|
|
|
if err := dave.Shutdown(); err != nil {
|
|
|
|
t.Fatalf("unable to shutdown dave: %v", err)
|
|
|
|
}
|
2017-02-23 03:24:22 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 05:28:10 +03:00
|
|
|
func testNodeSignVerify(net *networkHarness, t *harnessTest) {
|
2017-04-29 14:44:29 +03:00
|
|
|
timeout := time.Duration(time.Second * 5)
|
2017-04-20 05:28:10 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2017-08-08 02:30:29 +03:00
|
|
|
chanAmt := maxFundingAmount
|
2017-04-29 14:44:29 +03:00
|
|
|
pushAmt := btcutil.Amount(100000)
|
2017-04-20 05:28:10 +03:00
|
|
|
|
2017-04-29 14:44:29 +03:00
|
|
|
// Create a channel between alice and bob.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
|
|
|
aliceBobCh := openChannelAndAssert(ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
chanAmt, pushAmt)
|
2017-04-20 05:28:10 +03:00
|
|
|
|
2017-04-29 14:44:29 +03:00
|
|
|
aliceMsg := []byte("alice msg")
|
2017-04-20 05:28:10 +03:00
|
|
|
|
2017-04-29 14:44:29 +03:00
|
|
|
// alice signs "alice msg" and sends her signature to bob.
|
2017-04-20 05:28:10 +03:00
|
|
|
sigReq := &lnrpc.SignMessageRequest{Msg: aliceMsg}
|
|
|
|
sigResp, err := net.Alice.SignMessage(ctxb, sigReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("SignMessage rpc call failed: %v", err)
|
|
|
|
}
|
|
|
|
aliceSig := sigResp.Signature
|
|
|
|
|
2017-04-29 14:44:29 +03:00
|
|
|
// bob verifying alice's signature should succeed since alice and bob are
|
|
|
|
// connected.
|
|
|
|
verifyReq := &lnrpc.VerifyMessageRequest{Msg: aliceMsg, Signature: aliceSig}
|
2017-04-20 05:28:10 +03:00
|
|
|
verifyResp, err := net.Bob.VerifyMessage(ctxb, verifyReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("VerifyMessage failed: %v", err)
|
|
|
|
}
|
|
|
|
if !verifyResp.Valid {
|
|
|
|
t.Fatalf("alice's signature didn't validate")
|
|
|
|
}
|
2017-04-29 14:44:29 +03:00
|
|
|
if verifyResp.Pubkey != net.Alice.PubKeyStr {
|
|
|
|
t.Fatalf("alice's signature doesn't contain alice's pubkey.")
|
|
|
|
}
|
2017-04-20 05:28:10 +03:00
|
|
|
|
2017-04-29 14:44:29 +03:00
|
|
|
// carol is a new node that is unconnected to alice or bob.
|
2017-04-20 05:28:10 +03:00
|
|
|
carol, err := net.NewNode(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
carolMsg := []byte("carol msg")
|
|
|
|
|
2017-04-29 14:44:29 +03:00
|
|
|
// carol signs "carol msg" and sends her signature to bob.
|
2017-04-20 05:28:10 +03:00
|
|
|
sigReq = &lnrpc.SignMessageRequest{Msg: carolMsg}
|
|
|
|
sigResp, err = carol.SignMessage(ctxb, sigReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("SignMessage rpc call failed: %v", err)
|
|
|
|
}
|
|
|
|
carolSig := sigResp.Signature
|
|
|
|
|
2017-04-29 14:44:29 +03:00
|
|
|
// bob verifying carol's signature should fail since they are not connected.
|
|
|
|
verifyReq = &lnrpc.VerifyMessageRequest{Msg: carolMsg, Signature: carolSig}
|
2017-04-20 05:28:10 +03:00
|
|
|
verifyResp, err = net.Bob.VerifyMessage(ctxb, verifyReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("VerifyMessage failed: %v", err)
|
|
|
|
}
|
|
|
|
if verifyResp.Valid {
|
|
|
|
t.Fatalf("carol's signature should not be valid")
|
|
|
|
}
|
2017-04-29 14:44:29 +03:00
|
|
|
if verifyResp.Pubkey != carol.PubKeyStr {
|
|
|
|
t.Fatalf("carol's signature doesn't contain her pubkey")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up carol's node.
|
|
|
|
if err := carol.Shutdown(); err != nil {
|
|
|
|
t.Fatalf("unable to shutdown carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the channel between alice and bob.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, aliceBobCh, false)
|
2017-04-20 05:28:10 +03:00
|
|
|
}
|
|
|
|
|
2017-05-17 19:06:52 +03:00
|
|
|
// testAsyncPayments tests the performance of the async payments, and also
|
|
|
|
// checks that balances of both sides can't be become negative under stress
|
|
|
|
// payment strikes.
|
|
|
|
func testAsyncPayments(net *networkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// As we'll be querying the channels state frequently we'll
|
|
|
|
// create a closure helper function for the purpose.
|
|
|
|
getChanInfo := func(node *lightningNode) (*lnrpc.ActiveChannel, error) {
|
|
|
|
req := &lnrpc.ListChannelsRequest{}
|
|
|
|
channelInfo, err := node.ListChannels(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(channelInfo.Channels) != 1 {
|
|
|
|
t.Fatalf("node should only have a single channel, "+
|
|
|
|
"instead he has %v",
|
|
|
|
len(channelInfo.Channels))
|
|
|
|
}
|
|
|
|
|
|
|
|
return channelInfo.Channels[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
timeout = time.Duration(time.Second * 5)
|
|
|
|
paymentAmt = 100
|
|
|
|
)
|
|
|
|
|
|
|
|
// First establish a channel with a capacity equals to the overall
|
|
|
|
// amount of payments, between Alice and Bob, at the end of the test
|
|
|
|
// Alice should send all money from her side to Bob.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
|
|
|
chanPoint := openChannelAndAssert(ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
paymentAmt*2000, 0)
|
|
|
|
|
|
|
|
info, err := getChanInfo(net.Alice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get alice channel info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the number of invoices.
|
|
|
|
numInvoices := int(info.LocalBalance / paymentAmt)
|
|
|
|
bobAmt := int64(numInvoices * paymentAmt)
|
|
|
|
aliceAmt := info.LocalBalance - bobAmt
|
|
|
|
|
|
|
|
// Send one more payment in order to cause insufficient capacity error.
|
|
|
|
numInvoices++
|
|
|
|
|
|
|
|
// Initialize seed random in order to generate invoices.
|
2017-07-31 06:24:48 +03:00
|
|
|
prand.Seed(time.Now().UnixNano())
|
2017-05-17 19:06:52 +03:00
|
|
|
|
|
|
|
// With the channel open, we'll create a invoices for Bob that
|
|
|
|
// Alice will pay to in order to advance the state of the channel.
|
|
|
|
bobPaymentHashes := make([][]byte, numInvoices)
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
preimage := make([]byte, 32)
|
|
|
|
_, err := rand.Read(preimage)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate preimage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
|
|
|
resp, err := net.Bob.AddInvoice(ctxb, invoice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bobPaymentHashes[i] = resp.RHash
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Alice to receive the channel edge from the funding manager.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't see the alice->bob channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open up a payment stream to Alice that we'll use to send payment to
|
|
|
|
// Bob. We also create a small helper function to send payments to Bob,
|
|
|
|
// consuming the payment hashes we generated above.
|
|
|
|
alicePayStream, err := net.Alice.SendPayment(ctxb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send payments from Alice to Bob using of Bob's payment hashes
|
|
|
|
// generated above.
|
|
|
|
now := time.Now()
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
sendReq := &lnrpc.SendRequest{
|
|
|
|
PaymentHash: bobPaymentHashes[i],
|
|
|
|
Dest: net.Bob.PubKey[:],
|
|
|
|
Amt: paymentAmt,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := alicePayStream.Send(sendReq); err != nil {
|
|
|
|
t.Fatalf("unable to send payment: "+
|
2017-06-29 16:40:45 +03:00
|
|
|
"stream has been closed: %v", err)
|
2017-05-17 19:06:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-29 16:40:45 +03:00
|
|
|
// We should receive one insufficient capacity error, because we are
|
|
|
|
// sending on one invoice bigger.
|
2017-05-17 19:06:52 +03:00
|
|
|
errorReceived := false
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
if resp, err := alicePayStream.Recv(); err != nil {
|
|
|
|
t.Fatalf("payment stream have been closed: %v", err)
|
|
|
|
} else if resp.PaymentError != "" {
|
|
|
|
if strings.Contains(resp.PaymentError,
|
2017-06-29 16:40:45 +03:00
|
|
|
lnwire.CodeTemporaryChannelFailure.String()) {
|
2017-05-17 19:06:52 +03:00
|
|
|
if errorReceived {
|
|
|
|
t.Fatalf("redundant payment "+
|
|
|
|
"error: %v", resp.PaymentError)
|
|
|
|
}
|
|
|
|
|
|
|
|
errorReceived = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-29 16:40:45 +03:00
|
|
|
t.Fatalf("unable to send payment: %v", resp.PaymentError)
|
2017-05-17 19:06:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !errorReceived {
|
|
|
|
t.Fatalf("insufficient capacity error haven't been received")
|
|
|
|
}
|
|
|
|
|
|
|
|
// All payments have been sent, mark the finish time.
|
|
|
|
timeTaken := time.Since(now)
|
|
|
|
|
|
|
|
// Next query for Bob's and Alice's channel states, in order to confirm
|
|
|
|
// that all payment have been successful transmitted.
|
|
|
|
aliceChan, err := getChanInfo(net.Alice)
|
|
|
|
if len(aliceChan.PendingHtlcs) != 0 {
|
|
|
|
t.Fatalf("alice's pending htlcs is incorrect, got %v, "+
|
|
|
|
"expected %v", len(aliceChan.PendingHtlcs), 0)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get bob's channel info: %v", err)
|
|
|
|
}
|
|
|
|
if aliceChan.RemoteBalance != bobAmt {
|
|
|
|
t.Fatalf("alice's remote balance is incorrect, got %v, "+
|
|
|
|
"expected %v", aliceChan.RemoteBalance, bobAmt)
|
|
|
|
}
|
|
|
|
if aliceChan.LocalBalance != aliceAmt {
|
|
|
|
t.Fatalf("alice's local balance is incorrect, got %v, "+
|
|
|
|
"expected %v", aliceChan.LocalBalance, aliceAmt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Bob to receive revocation from Alice.
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
bobChan, err := getChanInfo(net.Bob)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get bob's channel info: %v", err)
|
|
|
|
}
|
|
|
|
if len(bobChan.PendingHtlcs) != 0 {
|
|
|
|
t.Fatalf("bob's pending htlcs is incorrect, got %v, "+
|
|
|
|
"expected %v", len(bobChan.PendingHtlcs), 0)
|
|
|
|
}
|
|
|
|
if bobChan.LocalBalance != bobAmt {
|
|
|
|
t.Fatalf("bob's local balance is incorrect, got %v, expected"+
|
|
|
|
" %v", bobChan.LocalBalance, bobAmt)
|
|
|
|
}
|
|
|
|
if bobChan.RemoteBalance != aliceAmt {
|
|
|
|
t.Fatalf("bob's remote balance is incorrect, got %v, "+
|
|
|
|
"expected %v", bobChan.RemoteBalance, aliceAmt)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("\tBenchmark info: Elapsed time: ", timeTaken)
|
|
|
|
t.Log("\tBenchmark info: TPS: ", float64(numInvoices)/float64(timeTaken.Seconds()))
|
|
|
|
|
|
|
|
// Finally, immediately close the channel. This function will also
|
|
|
|
// block until the channel is closed and will additionally assert the
|
|
|
|
// relevant channel closing post conditions.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
|
|
|
}
|
|
|
|
|
2017-05-18 17:36:55 +03:00
|
|
|
// testBidirectionalAsyncPayments tests that nodes are able to send the
|
|
|
|
// payments to each other in async manner without blocking.
|
|
|
|
func testBidirectionalAsyncPayments(net *networkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// As we'll be querying the channels state frequently we'll
|
|
|
|
// create a closure helper function for the purpose.
|
|
|
|
getChanInfo := func(node *lightningNode) (*lnrpc.ActiveChannel, error) {
|
|
|
|
req := &lnrpc.ListChannelsRequest{}
|
|
|
|
channelInfo, err := node.ListChannels(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(channelInfo.Channels) != 1 {
|
|
|
|
t.Fatalf("node should only have a single channel, "+
|
|
|
|
"instead he has %v",
|
|
|
|
len(channelInfo.Channels))
|
|
|
|
}
|
|
|
|
|
|
|
|
return channelInfo.Channels[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
timeout = time.Duration(time.Second * 5)
|
|
|
|
paymentAmt = 100
|
|
|
|
)
|
|
|
|
|
|
|
|
// First establish a channel with a capacity equals to the overall
|
|
|
|
// amount of payments, between Alice and Bob, at the end of the test
|
|
|
|
// Alice should send all money from her side to Bob.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, timeout)
|
|
|
|
chanPoint := openChannelAndAssert(ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
paymentAmt*2000, paymentAmt*1000)
|
|
|
|
|
|
|
|
info, err := getChanInfo(net.Alice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get alice channel info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the number of invoices.
|
|
|
|
numInvoices := int(info.LocalBalance / paymentAmt)
|
|
|
|
|
|
|
|
// Nodes should exchange the same amount of money and because of this
|
|
|
|
// at the end balances should remain the same.
|
|
|
|
aliceAmt := info.LocalBalance
|
|
|
|
bobAmt := info.RemoteBalance
|
|
|
|
|
|
|
|
// Initialize seed random in order to generate invoices.
|
2017-07-31 06:24:48 +03:00
|
|
|
prand.Seed(time.Now().UnixNano())
|
2017-05-18 17:36:55 +03:00
|
|
|
|
|
|
|
// With the channel open, we'll create a invoices for Bob that
|
|
|
|
// Alice will pay to in order to advance the state of the channel.
|
|
|
|
bobPaymentHashes := make([][]byte, numInvoices)
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
preimage := make([]byte, 32)
|
|
|
|
_, err := rand.Read(preimage)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate preimage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
|
|
|
resp, err := net.Bob.AddInvoice(ctxb, invoice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bobPaymentHashes[i] = resp.RHash
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the channel open, we'll create a invoices for Alice that
|
|
|
|
// Bob will pay to in order to advance the state of the channel.
|
|
|
|
alicePaymentHashes := make([][]byte, numInvoices)
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
preimage := make([]byte, 32)
|
|
|
|
_, err := rand.Read(preimage)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate preimage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
|
|
|
resp, err := net.Alice.AddInvoice(ctxb, invoice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
alicePaymentHashes[i] = resp.RHash
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Alice to receive the channel edge from the funding manager.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
2017-06-19 17:39:07 +03:00
|
|
|
if err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint); err != nil {
|
2017-05-18 17:36:55 +03:00
|
|
|
t.Fatalf("alice didn't see the alice->bob channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2017-06-19 17:39:07 +03:00
|
|
|
if err = net.Bob.WaitForNetworkChannelOpen(ctxt, chanPoint); err != nil {
|
|
|
|
t.Fatalf("bob didn't see the bob->alice channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2017-05-18 17:36:55 +03:00
|
|
|
|
|
|
|
// Open up a payment streams to Alice and to Bob, that we'll use to
|
|
|
|
// send payment between nodes.
|
|
|
|
alicePayStream, err := net.Alice.SendPayment(ctxb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bobPayStream, err := net.Bob.SendPayment(ctxb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for bob: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send payments from Alice to Bob and from Bob to Alice in async
|
|
|
|
// manner.
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
aliceSendReq := &lnrpc.SendRequest{
|
|
|
|
PaymentHash: bobPaymentHashes[i],
|
|
|
|
Dest: net.Bob.PubKey[:],
|
|
|
|
Amt: paymentAmt,
|
|
|
|
}
|
|
|
|
|
|
|
|
bobSendReq := &lnrpc.SendRequest{
|
|
|
|
PaymentHash: alicePaymentHashes[i],
|
|
|
|
Dest: net.Alice.PubKey[:],
|
|
|
|
Amt: paymentAmt,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := alicePayStream.Send(aliceSendReq); err != nil {
|
|
|
|
t.Fatalf("unable to send payment: "+
|
|
|
|
"%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bobPayStream.Send(bobSendReq); err != nil {
|
|
|
|
t.Fatalf("unable to send payment: "+
|
|
|
|
"%v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
if resp, err := alicePayStream.Recv(); err != nil {
|
|
|
|
errChan <- errors.Errorf("payment stream has"+
|
|
|
|
" been closed: %v", err)
|
|
|
|
} else if resp.PaymentError != "" {
|
|
|
|
errChan <- errors.Errorf("unable to send "+
|
|
|
|
"payment from alice to bob: %v",
|
|
|
|
resp.PaymentError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errChan <- nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
if resp, err := bobPayStream.Recv(); err != nil {
|
|
|
|
errChan <- errors.Errorf("payment stream has"+
|
|
|
|
" been closed: %v", err)
|
|
|
|
} else if resp.PaymentError != "" {
|
|
|
|
errChan <- errors.Errorf("unable to send "+
|
|
|
|
"payment from bob to alice: %v",
|
|
|
|
resp.PaymentError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errChan <- nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for Alice and Bob receive their payments, and throw and error
|
|
|
|
// if something goes wrong.
|
2017-06-19 16:53:52 +03:00
|
|
|
maxTime := 20 * time.Second
|
2017-05-18 17:36:55 +03:00
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
2017-08-11 00:05:04 +03:00
|
|
|
case <-time.After(maxTime):
|
2017-06-19 16:53:52 +03:00
|
|
|
t.Fatalf("waiting for payments to finish too long "+
|
|
|
|
"(%v)", maxTime)
|
2017-05-18 17:36:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Alice and Bob to receive revocations messages, and update
|
|
|
|
// states, i.e. balance info.
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
aliceInfo, err := getChanInfo(net.Alice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get bob's channel info: %v", err)
|
|
|
|
}
|
|
|
|
if aliceInfo.RemoteBalance != bobAmt {
|
|
|
|
t.Fatalf("alice's remote balance is incorrect, got %v, "+
|
|
|
|
"expected %v", aliceInfo.RemoteBalance, bobAmt)
|
|
|
|
}
|
|
|
|
if aliceInfo.LocalBalance != aliceAmt {
|
|
|
|
t.Fatalf("alice's local balance is incorrect, got %v, "+
|
|
|
|
"expected %v", aliceInfo.LocalBalance, aliceAmt)
|
|
|
|
}
|
|
|
|
if len(aliceInfo.PendingHtlcs) != 0 {
|
|
|
|
t.Fatalf("alice's pending htlcs is incorrect, got %v, "+
|
|
|
|
"expected %v", len(aliceInfo.PendingHtlcs), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next query for Bob's and Alice's channel states, in order to confirm
|
|
|
|
// that all payment have been successful transmitted.
|
|
|
|
bobInfo, err := getChanInfo(net.Bob)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get bob's channel info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if bobInfo.LocalBalance != bobAmt {
|
|
|
|
t.Fatalf("bob's local balance is incorrect, got %v, expected"+
|
|
|
|
" %v", bobInfo.LocalBalance, bobAmt)
|
|
|
|
}
|
|
|
|
if bobInfo.RemoteBalance != aliceAmt {
|
|
|
|
t.Fatalf("bob's remote balance is incorrect, got %v, "+
|
|
|
|
"expected %v", bobInfo.RemoteBalance, aliceAmt)
|
|
|
|
}
|
|
|
|
if len(bobInfo.PendingHtlcs) != 0 {
|
|
|
|
t.Fatalf("bob's pending htlcs is incorrect, got %v, "+
|
|
|
|
"expected %v", len(bobInfo.PendingHtlcs), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, immediately close the channel. This function will also
|
|
|
|
// block until the channel is closed and will additionally assert the
|
|
|
|
// relevant channel closing post conditions.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, timeout)
|
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
|
|
|
}
|
|
|
|
|
2016-10-26 15:28:05 +03:00
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
test func(net *networkHarness, t *harnessTest)
|
|
|
|
}
|
|
|
|
|
|
|
|
var testsCases = []*testCase{
|
|
|
|
{
|
|
|
|
name: "basic funding flow",
|
|
|
|
test: testBasicChannelFunding,
|
|
|
|
},
|
2017-05-05 10:00:39 +03:00
|
|
|
{
|
|
|
|
name: "disconnecting target peer",
|
|
|
|
test: testDisconnectingTargetPeer,
|
|
|
|
},
|
2017-03-14 08:23:19 +03:00
|
|
|
{
|
|
|
|
name: "graph topology notifications",
|
|
|
|
test: testGraphTopologyNotifications,
|
|
|
|
},
|
2017-01-31 07:21:52 +03:00
|
|
|
{
|
|
|
|
name: "funding flow persistence",
|
|
|
|
test: testChannelFundingPersistence,
|
|
|
|
},
|
2016-10-26 15:28:05 +03:00
|
|
|
{
|
|
|
|
name: "channel force closure",
|
|
|
|
test: testChannelForceClosure,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "channel balance",
|
|
|
|
test: testChannelBalance,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single hop invoice",
|
|
|
|
test: testSingleHopInvoice,
|
|
|
|
},
|
2016-12-05 14:59:36 +03:00
|
|
|
{
|
2017-01-04 03:02:51 +03:00
|
|
|
name: "list outgoing payments",
|
2016-12-05 14:59:36 +03:00
|
|
|
test: testListPayments,
|
|
|
|
},
|
2016-10-26 15:28:05 +03:00
|
|
|
{
|
|
|
|
name: "max pending channel",
|
|
|
|
test: testMaxPendingChannels,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi-hop payments",
|
|
|
|
test: testMultiHopPayments,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple channel creation",
|
|
|
|
test: testBasicChannelCreation,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invoice update subscription",
|
|
|
|
test: testInvoiceSubscriptions,
|
|
|
|
},
|
2017-01-08 07:25:45 +03:00
|
|
|
{
|
|
|
|
name: "multi-hop htlc error propagation",
|
|
|
|
test: testHtlcErrorPropagation,
|
|
|
|
},
|
2017-03-21 05:05:37 +03:00
|
|
|
// TODO(roasbeef): multi-path integration test
|
2017-02-23 03:24:22 +03:00
|
|
|
{
|
|
|
|
name: "node announcement",
|
|
|
|
test: testNodeAnnouncement,
|
|
|
|
},
|
2017-04-29 14:44:29 +03:00
|
|
|
{
|
|
|
|
name: "node sign verify",
|
|
|
|
test: testNodeSignVerify,
|
|
|
|
},
|
2017-05-17 19:06:52 +03:00
|
|
|
{
|
|
|
|
name: "async payments benchmark",
|
|
|
|
test: testAsyncPayments,
|
|
|
|
},
|
2017-05-18 17:36:55 +03:00
|
|
|
{
|
|
|
|
name: "async bidirectional payments",
|
|
|
|
test: testBidirectionalAsyncPayments,
|
|
|
|
},
|
2016-11-22 06:16:44 +03:00
|
|
|
{
|
|
|
|
// TODO(roasbeef): test always needs to be last as Bob's state
|
|
|
|
// is borked since we trick him into attempting to cheat Alice?
|
|
|
|
name: "revoked uncooperative close retribution",
|
|
|
|
test: testRevokedCloseRetribution,
|
|
|
|
},
|
2016-08-30 08:07:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestLightningNetworkDaemon performs a series of integration tests amongst a
|
2016-09-15 21:59:51 +03:00
|
|
|
// programmatically driven network of lnd nodes.
|
2016-08-30 08:07:54 +03:00
|
|
|
func TestLightningNetworkDaemon(t *testing.T) {
|
2016-10-24 05:00:09 +03:00
|
|
|
ht := newHarnessTest(t)
|
2016-08-30 08:07:54 +03:00
|
|
|
|
2016-08-31 05:34:13 +03:00
|
|
|
// First create the network harness to gain access to its
|
|
|
|
// 'OnTxAccepted' call back.
|
2016-10-15 16:47:09 +03:00
|
|
|
lndHarness, err := newNetworkHarness()
|
2016-08-31 05:34:13 +03:00
|
|
|
if err != nil {
|
2016-10-26 15:28:05 +03:00
|
|
|
ht.Fatalf("unable to create lightning network harness: %v", err)
|
2016-08-31 05:34:13 +03:00
|
|
|
}
|
2017-08-19 03:22:54 +03:00
|
|
|
|
|
|
|
// Set up teardowns. While it's easier to set up the lnd harness before
|
|
|
|
// the btcd harness, they should be torn down in reverse order to
|
|
|
|
// prevent certain types of hangs.
|
|
|
|
var btcdHarness *rpctest.Harness
|
|
|
|
defer func() {
|
|
|
|
if lndHarness != nil {
|
|
|
|
lndHarness.TearDownAll()
|
|
|
|
}
|
|
|
|
if btcdHarness != nil {
|
|
|
|
btcdHarness.TearDown()
|
|
|
|
}
|
|
|
|
}()
|
2016-08-31 05:34:13 +03:00
|
|
|
|
2017-08-25 04:54:17 +03:00
|
|
|
handlers := &rpcclient.NotificationHandlers{
|
2016-10-15 16:47:09 +03:00
|
|
|
OnTxAccepted: lndHarness.OnTxAccepted,
|
2016-08-31 05:34:13 +03:00
|
|
|
}
|
|
|
|
|
2016-11-24 11:49:18 +03:00
|
|
|
// Spawn a new goroutine to watch for any fatal errors that any of the
|
|
|
|
// running lnd processes encounter. If an error occurs, then the test
|
|
|
|
// fails immediately with a fatal error, as far as fatal is happening
|
|
|
|
// inside goroutine main goroutine would not be finished at the same
|
|
|
|
// time as we receive fatal error from lnd process.
|
|
|
|
testsFin := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case err := <-lndHarness.ProcessErrors():
|
|
|
|
ht.Fatalf("lnd finished with error (stderr): "+
|
|
|
|
"\n%v", err)
|
|
|
|
case <-testsFin:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-09-15 21:59:51 +03:00
|
|
|
// First create an instance of the btcd's rpctest.Harness. This will be
|
2016-08-30 08:07:54 +03:00
|
|
|
// used to fund the wallets of the nodes within the test network and to
|
2017-01-14 00:32:37 +03:00
|
|
|
// drive blockchain related events within the network. Revert the default
|
|
|
|
// setting of accepting non-standard transactions on simnet to reject them.
|
|
|
|
// Transactions on the lightning network should always be standard to get
|
|
|
|
// better guarantees of getting included in to blocks.
|
|
|
|
args := []string{"--rejectnonstd"}
|
2017-08-19 03:22:54 +03:00
|
|
|
btcdHarness, err = rpctest.New(harnessNetParams, handlers, args)
|
2016-08-30 08:07:54 +03:00
|
|
|
if err != nil {
|
2016-10-26 15:28:05 +03:00
|
|
|
ht.Fatalf("unable to create mining node: %v", err)
|
2016-08-30 08:07:54 +03:00
|
|
|
}
|
2017-06-25 15:05:26 +03:00
|
|
|
|
|
|
|
// Turn off the btcd rpc logging, otherwise it will lead to panic.
|
|
|
|
// TODO(andrew.shvv|roasbeef) Remove the hack after re-work the way the log
|
|
|
|
// rotator os work.
|
2017-08-25 04:54:17 +03:00
|
|
|
rpcclient.UseLogger(btclog.Disabled)
|
2017-06-25 15:05:26 +03:00
|
|
|
|
2016-10-15 16:47:09 +03:00
|
|
|
if err := btcdHarness.SetUp(true, 50); err != nil {
|
2016-10-26 15:28:05 +03:00
|
|
|
ht.Fatalf("unable to set up mining node: %v", err)
|
2016-08-30 08:07:54 +03:00
|
|
|
}
|
2016-08-31 05:34:13 +03:00
|
|
|
if err := btcdHarness.Node.NotifyNewTransactions(false); err != nil {
|
2016-10-26 15:28:05 +03:00
|
|
|
ht.Fatalf("unable to request transaction notifications: %v", err)
|
2016-08-31 05:34:13 +03:00
|
|
|
}
|
2016-08-30 08:07:54 +03:00
|
|
|
|
2017-01-06 00:17:49 +03:00
|
|
|
// Next mine enough blocks in order for segwit and the CSV package
|
|
|
|
// soft-fork to activate on SimNet.
|
|
|
|
numBlocks := chaincfg.SimNetParams.MinerConfirmationWindow * 2
|
|
|
|
if _, err := btcdHarness.Node.Generate(numBlocks); err != nil {
|
|
|
|
ht.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-31 05:34:13 +03:00
|
|
|
// With the btcd harness created, we can now complete the
|
2016-09-15 22:24:52 +03:00
|
|
|
// initialization of the network. args - list of lnd arguments,
|
|
|
|
// example: "--debuglevel=debug"
|
2016-09-26 20:40:58 +03:00
|
|
|
// TODO(roasbeef): create master balanced channel with all the monies?
|
2016-10-15 16:47:09 +03:00
|
|
|
if err := lndHarness.InitializeSeedNodes(btcdHarness, nil); err != nil {
|
2016-10-26 15:28:05 +03:00
|
|
|
ht.Fatalf("unable to initialize seed nodes: %v", err)
|
2016-08-30 08:07:54 +03:00
|
|
|
}
|
2016-10-15 16:47:09 +03:00
|
|
|
if err = lndHarness.SetUp(); err != nil {
|
2016-10-26 15:28:05 +03:00
|
|
|
ht.Fatalf("unable to set up test lightning network: %v", err)
|
2016-08-30 08:07:54 +03:00
|
|
|
}
|
|
|
|
|
2016-10-26 15:28:05 +03:00
|
|
|
t.Logf("Running %v integration tests", len(testsCases))
|
|
|
|
for _, testCase := range testsCases {
|
2017-06-19 16:53:52 +03:00
|
|
|
success := t.Run(testCase.name, func(t1 *testing.T) {
|
|
|
|
ht := newHarnessTest(t1)
|
|
|
|
ht.RunTestCase(testCase, lndHarness)
|
|
|
|
})
|
2017-06-19 17:39:07 +03:00
|
|
|
|
2017-06-19 16:53:52 +03:00
|
|
|
// Stop at the first failure. Mimic behavior of original test
|
2017-06-19 17:39:07 +03:00
|
|
|
// framework.
|
2017-06-19 16:53:52 +03:00
|
|
|
if !success {
|
|
|
|
break
|
|
|
|
}
|
2016-08-30 08:07:54 +03:00
|
|
|
}
|
2016-11-16 23:42:58 +03:00
|
|
|
|
|
|
|
close(testsFin)
|
2016-08-30 08:07:54 +03:00
|
|
|
}
|