2017-07-05 02:05:05 +03:00
|
|
|
// +build rpctest
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
package itest
|
2016-08-30 08:07:54 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-04-25 10:20:13 +03:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2019-01-23 07:45:38 +03:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
2019-04-25 10:20:13 +03:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
2019-01-23 07:45:38 +03:00
|
|
|
"encoding/hex"
|
2019-04-25 10:20:13 +03:00
|
|
|
"encoding/pem"
|
2016-08-30 08:07:54 +03:00
|
|
|
"fmt"
|
2016-11-22 06:16:44 +03:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2019-02-12 00:02:00 +03:00
|
|
|
"math"
|
2019-04-25 10:20:13 +03:00
|
|
|
"math/big"
|
|
|
|
"net"
|
2016-11-22 06:16:44 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-01-23 07:45:38 +03:00
|
|
|
"reflect"
|
2017-01-08 07:25:45 +03:00
|
|
|
"strings"
|
2019-03-12 03:38:38 +03:00
|
|
|
"sync"
|
2019-01-23 07:45:38 +03:00
|
|
|
"sync/atomic"
|
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
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
"github.com/btcsuite/btcd/btcjson"
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/integration/rpctest"
|
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2018-06-15 22:33:49 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/go-errors/errors"
|
2019-05-15 10:02:53 +03:00
|
|
|
"github.com/lightningnetwork/lnd"
|
2019-03-12 03:38:38 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chanbackup"
|
2018-06-15 22:33:49 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2019-06-07 17:42:26 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
|
2019-03-19 19:09:27 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
2018-06-15 22:33:49 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntest"
|
2019-06-07 17:42:26 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2018-06-15 22:33:49 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
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
|
|
|
)
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
var (
|
|
|
|
harnessNetParams = &chaincfg.SimNetParams
|
|
|
|
)
|
|
|
|
|
2018-06-16 15:53:44 +03:00
|
|
|
const (
|
2018-11-06 00:12:02 +03:00
|
|
|
testFeeBase = 1e+6
|
|
|
|
defaultCSV = lntest.DefaultCSV
|
|
|
|
defaultTimeout = lntest.DefaultTimeout
|
|
|
|
minerMempoolTimeout = lntest.MinerMempoolTimeout
|
|
|
|
channelOpenTimeout = lntest.ChannelOpenTimeout
|
|
|
|
channelCloseTimeout = lntest.ChannelCloseTimeout
|
2018-06-16 15:53:44 +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
|
|
|
}
|
|
|
|
|
2019-05-24 15:17:49 +03:00
|
|
|
// Skipf calls the underlying testing.T's Skip method, causing the current test
|
|
|
|
// to be skipped.
|
|
|
|
func (h *harnessTest) Skipf(format string, args ...interface{}) {
|
|
|
|
h.t.Skipf(format, args...)
|
|
|
|
}
|
|
|
|
|
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.
|
2017-11-03 21:52:02 +03:00
|
|
|
func (h *harnessTest) RunTestCase(testCase *testCase,
|
|
|
|
net *lntest.NetworkHarness) {
|
|
|
|
|
2016-10-26 15:28:05 +03:00
|
|
|
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()
|
2018-02-07 06:11:11 +03:00
|
|
|
h.t.Fatalf("Failed: (%v) panicked with: \n%v",
|
2016-10-26 15:28:05 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 14:18:35 +03:00
|
|
|
t.Fatalf("tx was not included in block")
|
2016-08-30 08:07:54 +03:00
|
|
|
}
|
|
|
|
|
2019-03-12 02:24:45 +03:00
|
|
|
func rpcPointToWirePoint(t *harnessTest, chanPoint *lnrpc.ChannelPoint) wire.OutPoint {
|
2019-05-15 10:02:53 +03:00
|
|
|
txid, err := lnd.GetChanPointFundingTxid(chanPoint)
|
2019-03-12 02:24:45 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 16:24:56 +03:00
|
|
|
// mineBlocks mine 'num' of blocks and check that blocks are present in
|
2018-11-29 14:33:11 +03:00
|
|
|
// node blockchain. numTxs should be set to the number of transactions
|
|
|
|
// (excluding the coinbase) we expect to be included in the first mined block.
|
|
|
|
func mineBlocks(t *harnessTest, net *lntest.NetworkHarness,
|
|
|
|
num uint32, numTxs int) []*wire.MsgBlock {
|
|
|
|
|
|
|
|
// If we expect transactions to be included in the blocks we'll mine,
|
|
|
|
// we wait here until they are seen in the miner's mempool.
|
|
|
|
var txids []*chainhash.Hash
|
|
|
|
var err error
|
|
|
|
if numTxs > 0 {
|
|
|
|
txids, err = waitForNTxsInMempool(
|
|
|
|
net.Miner.Node, numTxs, minerMempoolTimeout,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find txns in mempool: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 02:55:36 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-29 14:33:11 +03:00
|
|
|
// Finally, assert that all the transactions were included in the first
|
|
|
|
// block.
|
|
|
|
for _, txid := range txids {
|
|
|
|
assertTxInBlock(t, blocks[0], txid)
|
|
|
|
}
|
|
|
|
|
2016-10-15 16:24:56 +03:00
|
|
|
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-11-03 21:52:02 +03:00
|
|
|
func openChannelAndAssert(ctx context.Context, t *harnessTest,
|
|
|
|
net *lntest.NetworkHarness, alice, bob *lntest.HarnessNode,
|
2018-08-22 10:32:42 +03:00
|
|
|
p lntest.OpenChannelParams) *lnrpc.ChannelPoint {
|
2016-09-18 03:34:39 +03:00
|
|
|
|
2018-03-15 20:02:40 +03:00
|
|
|
chanOpenUpdate, err := net.OpenChannel(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctx, alice, bob, p,
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
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
|
|
|
|
2017-11-14 04:37:20 +03:00
|
|
|
// Mine 6 blocks, then wait for Alice's node to notify us that the
|
2016-09-18 03:34:39 +03:00
|
|
|
// channel has been opened. The funding transaction should be found
|
2018-03-15 20:02:40 +03:00
|
|
|
// within the first newly mined block. We mine 6 blocks so that in the
|
|
|
|
// case that the channel is public, it is announced to the network.
|
2018-11-29 14:33:11 +03:00
|
|
|
block := mineBlocks(t, net, 6, 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
|
|
|
}
|
2019-05-15 10:02:53 +03:00
|
|
|
fundingTxID, err := lnd.GetChanPointFundingTxid(fundingChanPoint)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
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
|
|
|
}
|
2017-11-02 20:38:12 +03:00
|
|
|
if err := net.AssertChannelExists(ctx, bob, &chanPoint); err != nil {
|
|
|
|
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
|
2018-02-07 06:11:11 +03:00
|
|
|
// channel point owned by the passed Lightning node. A fully blocking channel
|
2016-09-18 03:34:39 +03:00
|
|
|
// 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
|
2019-03-13 03:34:42 +03:00
|
|
|
// a block. Finally, this assertion verifies that the node always sends out a
|
|
|
|
// disable update when closing the channel if the channel was previously enabled.
|
|
|
|
//
|
|
|
|
// NOTE: This method assumes that the provided funding point is confirmed
|
|
|
|
// on-chain AND that the edge exists in the node's channel graph. If the funding
|
|
|
|
// transactions was reorged out at some point, use closeReorgedChannelAndAssert.
|
2017-11-03 21:52:02 +03:00
|
|
|
func closeChannelAndAssert(ctx context.Context, t *harnessTest,
|
|
|
|
net *lntest.NetworkHarness, node *lntest.HarnessNode,
|
|
|
|
fundingChanPoint *lnrpc.ChannelPoint, force bool) *chainhash.Hash {
|
2016-09-18 03:34:39 +03:00
|
|
|
|
2019-03-13 03:34:42 +03:00
|
|
|
// Fetch the current channel policy. If the channel is currently
|
|
|
|
// enabled, we will register for graph notifications before closing to
|
|
|
|
// assert that the node sends out a disabling update as a result of the
|
|
|
|
// channel being closed.
|
|
|
|
curPolicy := getChannelPolicies(t, node, node.PubKeyStr, fundingChanPoint)[0]
|
|
|
|
expectDisable := !curPolicy.Disabled
|
|
|
|
|
|
|
|
// If the current channel policy is enabled, begin subscribing the graph
|
|
|
|
// updates before initiating the channel closure.
|
|
|
|
var graphSub *graphSubscription
|
|
|
|
if expectDisable {
|
|
|
|
sub := subscribeGraphNotifications(t, ctx, node)
|
|
|
|
graphSub = &sub
|
|
|
|
defer close(graphSub.quit)
|
|
|
|
}
|
|
|
|
|
|
|
|
closeUpdates, _, err := net.CloseChannel(ctx, node, fundingChanPoint, force)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to close channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the channel policy was enabled prior to the closure, wait until we
|
|
|
|
// received the disabled update.
|
|
|
|
if expectDisable {
|
|
|
|
curPolicy.Disabled = true
|
|
|
|
waitForChannelUpdate(
|
|
|
|
t, *graphSub,
|
|
|
|
[]expectedChanUpdate{
|
|
|
|
{node.PubKeyStr, curPolicy, fundingChanPoint},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return assertChannelClosed(ctx, t, net, node, fundingChanPoint, closeUpdates)
|
|
|
|
}
|
|
|
|
|
|
|
|
// closeReorgedChannelAndAssert attempts to close a channel identified by the
|
|
|
|
// passed channel point owned by the passed Lightning 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.
|
|
|
|
//
|
|
|
|
// NOTE: This method does not verify that the node sends a disable update for
|
|
|
|
// the closed channel.
|
|
|
|
func closeReorgedChannelAndAssert(ctx context.Context, t *harnessTest,
|
|
|
|
net *lntest.NetworkHarness, node *lntest.HarnessNode,
|
|
|
|
fundingChanPoint *lnrpc.ChannelPoint, force bool) *chainhash.Hash {
|
|
|
|
|
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
|
|
|
|
2019-03-13 03:34:42 +03:00
|
|
|
return assertChannelClosed(ctx, t, net, node, fundingChanPoint, closeUpdates)
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertChannelClosed asserts that the channel is properly cleaned up after
|
|
|
|
// initiating a cooperative or local close.
|
|
|
|
func assertChannelClosed(ctx context.Context, t *harnessTest,
|
|
|
|
net *lntest.NetworkHarness, node *lntest.HarnessNode,
|
|
|
|
fundingChanPoint *lnrpc.ChannelPoint,
|
|
|
|
closeUpdates lnrpc.Lightning_CloseChannelClient) *chainhash.Hash {
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
txid, err := lnd.GetChanPointFundingTxid(fundingChanPoint)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2017-05-05 03:15:11 +03:00
|
|
|
chanPointStr := fmt.Sprintf("%v:%v", txid, fundingChanPoint.OutputIndex)
|
|
|
|
|
2018-07-18 14:37:53 +03:00
|
|
|
// At this point, the channel should now be marked as being in the
|
|
|
|
// state of "waiting close".
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
|
|
|
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.WaitingCloseChannels {
|
|
|
|
if pendingClose.Channel.ChannelPoint == chanPointStr {
|
|
|
|
found = true
|
|
|
|
break
|
2017-05-05 03:15:11 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-18 14:37:53 +03:00
|
|
|
if !found {
|
|
|
|
t.Fatalf("channel not marked as waiting close")
|
|
|
|
}
|
2017-05-05 03:15:11 +03:00
|
|
|
|
2018-04-13 04:47:10 +03:00
|
|
|
// We'll now, generate a single block, wait for the final close status
|
2016-09-18 03:34:39 +03:00
|
|
|
// update, then ensure that the closing transaction was included in the
|
|
|
|
// block.
|
2018-11-29 14:33:11 +03:00
|
|
|
block := mineBlocks(t, net, 1, 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
|
|
|
|
2018-05-18 16:21:31 +03:00
|
|
|
// Finally, the transaction should no longer be in the waiting close
|
2018-04-13 04:47:10 +03:00
|
|
|
// state as we've just mined a block that should include the closing
|
2018-07-18 14:37:53 +03:00
|
|
|
// transaction.
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
|
|
|
pendingChanResp, err := node.PendingChannels(
|
|
|
|
ctx, pendingChansRequest,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2018-04-13 04:47:10 +03:00
|
|
|
|
2018-07-18 14:37:53 +03:00
|
|
|
for _, pendingClose := range pendingChanResp.WaitingCloseChannels {
|
|
|
|
if pendingClose.Channel.ChannelPoint == chanPointStr {
|
|
|
|
return false
|
2018-04-13 04:47:10 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-18 14:37:53 +03:00
|
|
|
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("closing transaction not marked as fully closed")
|
2018-04-13 04:47:10 +03:00
|
|
|
}
|
|
|
|
|
2016-11-22 06:13:03 +03:00
|
|
|
return closingTxid
|
2016-09-15 21:59:51 +03:00
|
|
|
}
|
|
|
|
|
2018-11-06 13:03:01 +03:00
|
|
|
// waitForChannelPendingForceClose waits for the node to report that the
|
|
|
|
// channel is pending force close, and that the UTXO nursery is aware of it.
|
|
|
|
func waitForChannelPendingForceClose(ctx context.Context,
|
|
|
|
node *lntest.HarnessNode, fundingChanPoint *lnrpc.ChannelPoint) error {
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
txid, err := lnd.GetChanPointFundingTxid(fundingChanPoint)
|
2018-11-06 13:03:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
op := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: fundingChanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
var predErr error
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
|
|
|
pendingChanResp, err := node.PendingChannels(
|
|
|
|
ctx, pendingChansRequest,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to get pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
forceClose, err := findForceClosedChannel(pendingChanResp, &op)
|
|
|
|
if err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must wait until the UTXO nursery has received the channel
|
|
|
|
// and is aware of its maturity height.
|
|
|
|
if forceClose.MaturityHeight == 0 {
|
|
|
|
predErr = fmt.Errorf("channel had maturity height of 0")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
return predErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanupForceClose mines a force close commitment found in the mempool and
|
|
|
|
// the following sweep transaction from the force closing node.
|
|
|
|
func cleanupForceClose(t *harnessTest, net *lntest.NetworkHarness,
|
|
|
|
node *lntest.HarnessNode, chanPoint *lnrpc.ChannelPoint) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// Wait for the channel to be marked pending force close.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err := waitForChannelPendingForceClose(ctxt, node, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("channel not pending force close: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mine enough blocks for the node to sweep its funds from the force
|
|
|
|
// closed channel.
|
|
|
|
_, err = net.Miner.Node.Generate(defaultCSV)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-29 14:33:11 +03:00
|
|
|
// The node should now sweep the funds, clean up by mining the sweeping
|
2018-11-06 13:03:01 +03:00
|
|
|
// tx.
|
2018-11-29 14:33:11 +03:00
|
|
|
mineBlocks(t, net, 1, 1)
|
2018-11-06 13:03:01 +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).
|
2017-11-03 21:52:02 +03:00
|
|
|
func numOpenChannelsPending(ctxt context.Context, node *lntest.HarnessNode) (int, error) {
|
2018-01-04 23:20:25 +03:00
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
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-11-03 21:52:02 +03:00
|
|
|
alice, bob *lntest.HarnessNode, expected int) {
|
2017-11-03 01:47:59 +03:00
|
|
|
|
2019-02-06 23:47:40 +03:00
|
|
|
err := lntest.WaitNoError(func() error {
|
2017-11-03 01:47:59 +03:00
|
|
|
aliceNumChans, err := numOpenChannelsPending(ctxt, alice)
|
|
|
|
if err != nil {
|
2019-02-06 23:47:40 +03:00
|
|
|
return fmt.Errorf("error fetching alice's node (%v) "+
|
|
|
|
"pending channels %v", alice.NodeID, err)
|
2017-11-03 01:47:59 +03:00
|
|
|
}
|
|
|
|
bobNumChans, err := numOpenChannelsPending(ctxt, bob)
|
|
|
|
if err != nil {
|
2019-02-06 23:47:40 +03:00
|
|
|
return fmt.Errorf("error fetching bob's node (%v) "+
|
|
|
|
"pending channels %v", bob.NodeID, err)
|
2017-11-03 01:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
aliceStateCorrect := aliceNumChans == expected
|
2019-02-06 23:47:40 +03:00
|
|
|
if !aliceStateCorrect {
|
|
|
|
return fmt.Errorf("number of pending channels for "+
|
|
|
|
"alice incorrect. expected %v, got %v",
|
|
|
|
expected, aliceNumChans)
|
2017-11-03 01:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bobStateCorrect := bobNumChans == expected
|
2019-02-06 23:47:40 +03:00
|
|
|
if !bobStateCorrect {
|
|
|
|
return fmt.Errorf("number of pending channels for bob "+
|
|
|
|
"incorrect. expected %v, got %v", expected,
|
|
|
|
bobNumChans)
|
2017-11-03 01:47:59 +03:00
|
|
|
}
|
|
|
|
|
2019-02-06 23:47:40 +03:00
|
|
|
return nil
|
|
|
|
}, 15*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
2017-01-31 07:21:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 01:45:13 +03:00
|
|
|
// assertNumConnections asserts number current connections between two peers.
|
2018-11-28 11:34:50 +03:00
|
|
|
func assertNumConnections(t *harnessTest, alice, bob *lntest.HarnessNode,
|
|
|
|
expected int) {
|
|
|
|
ctxb := context.Background()
|
2017-08-09 02:50:37 +03:00
|
|
|
|
|
|
|
const nPolls = 10
|
|
|
|
|
2017-11-03 01:45:13 +03:00
|
|
|
tick := time.NewTicker(300 * time.Millisecond)
|
|
|
|
defer tick.Stop()
|
|
|
|
|
2017-08-09 02:50:37 +03:00
|
|
|
for i := nPolls - 1; i >= 0; i-- {
|
|
|
|
select {
|
2017-11-03 01:45:13 +03:00
|
|
|
case <-tick.C:
|
2018-11-28 11:34:50 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2017-08-09 02:50:37 +03:00
|
|
|
aNumPeers, err := alice.ListPeers(ctxt, &lnrpc.ListPeersRequest{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch alice's node (%v) list peers %v",
|
2017-11-03 21:52:02 +03:00
|
|
|
alice.NodeID, err)
|
2017-08-09 02:50:37 +03:00
|
|
|
}
|
2018-11-28 11:34:50 +03:00
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-08-09 02:50:37 +03:00
|
|
|
bNumPeers, err := bob.ListPeers(ctxt, &lnrpc.ListPeersRequest{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch bob's node (%v) list peers %v",
|
2017-11-03 21:52:02 +03:00
|
|
|
bob.NodeID, err)
|
2017-08-09 02:50:37 +03:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-08 00:14:39 +03:00
|
|
|
// shutdownAndAssert shuts down the given node and asserts that no errors
|
|
|
|
// occur.
|
|
|
|
func shutdownAndAssert(net *lntest.NetworkHarness, t *harnessTest,
|
|
|
|
node *lntest.HarnessNode) {
|
|
|
|
if err := net.ShutdownNode(node); err != nil {
|
|
|
|
t.Fatalf("unable to shutdown %v: %v", node.Name(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2018-01-23 05:58:13 +03:00
|
|
|
// TODO(conner) remove code duplication
|
2017-05-01 21:45:02 +03:00
|
|
|
func calcStaticFee(numHTLCs int) btcutil.Amount {
|
|
|
|
const (
|
|
|
|
commitWeight = btcutil.Amount(724)
|
|
|
|
htlcWeight = 172
|
2018-02-21 15:03:30 +03:00
|
|
|
feePerKw = btcutil.Amount(50 * 1000 / 4)
|
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
|
|
|
|
}
|
|
|
|
|
2017-10-26 01:32:00 +03:00
|
|
|
// completePaymentRequests sends payments from a lightning node to complete all
|
|
|
|
// payment requests. If the awaitResponse parameter is true, this function
|
|
|
|
// does not return until all payments successfully complete without errors.
|
|
|
|
func completePaymentRequests(ctx context.Context, client lnrpc.LightningClient,
|
|
|
|
paymentRequests []string, awaitResponse bool) error {
|
|
|
|
|
2019-04-15 16:00:51 +03:00
|
|
|
// We start by getting the current state of the client's channels. This
|
|
|
|
// is needed to ensure the payments actually have been committed before
|
|
|
|
// we return.
|
|
|
|
ctxt, _ := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
req := &lnrpc.ListChannelsRequest{}
|
|
|
|
listResp, err := client.ListChannels(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxc, cancel := context.WithCancel(ctx)
|
2017-10-26 01:32:00 +03:00
|
|
|
defer cancel()
|
|
|
|
|
2019-04-15 16:00:51 +03:00
|
|
|
payStream, err := client.SendPayment(ctxc)
|
2017-10-26 01:32:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, payReq := range paymentRequests {
|
2019-03-12 02:24:45 +03:00
|
|
|
sendReq := &lnrpc.SendRequest{
|
|
|
|
PaymentRequest: payReq,
|
|
|
|
}
|
2017-10-26 01:32:00 +03:00
|
|
|
err := payStream.Send(sendReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if awaitResponse {
|
|
|
|
for range paymentRequests {
|
|
|
|
resp, err := payStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.PaymentError != "" {
|
|
|
|
return fmt.Errorf("received payment error: %v",
|
|
|
|
resp.PaymentError)
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 16:00:51 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are not waiting for feedback in the form of a response, but we
|
|
|
|
// should still wait long enough for the server to receive and handle
|
|
|
|
// the send before cancelling the request. We wait for the number of
|
|
|
|
// updates to one of our channels has increased before we return.
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
ctxt, _ = context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
newListResp, err := client.ListChannels(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c1 := range listResp.Channels {
|
|
|
|
for _, c2 := range newListResp.Channels {
|
|
|
|
if c1.ChannelPoint != c2.ChannelPoint {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this channel has an increased numbr of
|
|
|
|
// updates, we assume the payments are
|
|
|
|
// committed, and we can return.
|
|
|
|
if c2.NumUpdates > c1.NumUpdates {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-10-26 01:32:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:23:08 +03:00
|
|
|
// makeFakePayHash creates random pre image hash
|
|
|
|
func makeFakePayHash(t *harnessTest) []byte {
|
|
|
|
randBuf := make([]byte, 32)
|
|
|
|
|
|
|
|
if _, err := rand.Read(randBuf); err != nil {
|
|
|
|
t.Fatalf("internal error, cannot generate random string: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return randBuf
|
|
|
|
}
|
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// createPayReqs is a helper method that will create a slice of payment
|
|
|
|
// requests for the given node.
|
2019-03-28 05:00:27 +03:00
|
|
|
func createPayReqs(node *lntest.HarnessNode, paymentAmt btcutil.Amount,
|
|
|
|
numInvoices int) ([]string, [][]byte, []*lnrpc.Invoice, error) {
|
2018-11-20 17:09:44 +03:00
|
|
|
|
|
|
|
payReqs := make([]string, numInvoices)
|
|
|
|
rHashes := make([][]byte, numInvoices)
|
|
|
|
invoices := make([]*lnrpc.Invoice, numInvoices)
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
preimage := make([]byte, 32)
|
|
|
|
_, err := rand.Read(preimage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("unable to generate "+
|
|
|
|
"preimage: %v", err)
|
|
|
|
}
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: int64(paymentAmt),
|
|
|
|
}
|
2019-03-28 05:00:27 +03:00
|
|
|
ctxt, _ := context.WithTimeout(
|
|
|
|
context.Background(), defaultTimeout,
|
|
|
|
)
|
|
|
|
resp, err := node.AddInvoice(ctxt, invoice)
|
2018-11-20 17:09:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("unable to add "+
|
|
|
|
"invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
payReqs[i] = resp.PaymentRequest
|
|
|
|
rHashes[i] = resp.RHash
|
|
|
|
invoices[i] = invoice
|
|
|
|
}
|
|
|
|
return payReqs, rHashes, invoices, nil
|
|
|
|
}
|
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// getChanInfo is a helper method for getting channel info for a node's sole
|
|
|
|
// channel.
|
|
|
|
func getChanInfo(ctx context.Context, node *lntest.HarnessNode) (
|
|
|
|
*lnrpc.Channel, error) {
|
|
|
|
|
|
|
|
req := &lnrpc.ListChannelsRequest{}
|
|
|
|
channelInfo, err := node.ListChannels(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(channelInfo.Channels) != 1 {
|
|
|
|
return nil, fmt.Errorf("node should only have a single "+
|
2019-05-24 15:17:49 +03:00
|
|
|
"channel, instead it has %v", len(channelInfo.Channels))
|
2018-11-20 17:09:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return channelInfo.Channels[0], nil
|
|
|
|
}
|
|
|
|
|
2018-04-03 02:57:58 +03:00
|
|
|
const (
|
2018-09-27 16:49:44 +03:00
|
|
|
AddrTypeWitnessPubkeyHash = lnrpc.AddressType_WITNESS_PUBKEY_HASH
|
|
|
|
AddrTypeNestedPubkeyHash = lnrpc.AddressType_NESTED_PUBKEY_HASH
|
2018-04-03 02:57:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// testOnchainFundRecovery checks lnd's ability to rescan for onchain outputs
|
|
|
|
// when providing a valid aezeed that owns outputs on the chain. This test
|
|
|
|
// performs multiple restorations using the same seed and various recovery
|
|
|
|
// windows to ensure we detect funds properly.
|
|
|
|
func testOnchainFundRecovery(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// First, create a new node with strong passphrase and grab the mnemonic
|
|
|
|
// used for key derivation. This will bring up Carol with an empty
|
|
|
|
// wallet, and such that she is synced up.
|
|
|
|
password := []byte("The Magic Words are Squeamish Ossifrage")
|
2018-04-27 11:42:04 +03:00
|
|
|
carol, mnemonic, err := net.NewNodeWithSeed("Carol", nil, password)
|
2018-04-03 02:57:58 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create node with seed; %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
shutdownAndAssert(net, t, carol)
|
2018-04-03 02:57:58 +03:00
|
|
|
|
|
|
|
// Create a closure for testing the recovery of Carol's wallet. This
|
|
|
|
// method takes the expected value of Carol's balance when using the
|
|
|
|
// given recovery window. Additionally, the caller can specify an action
|
|
|
|
// to perform on the restored node before the node is shutdown.
|
2019-02-12 00:02:00 +03:00
|
|
|
restoreCheckBalance := func(expAmount int64, expectedNumUTXOs int,
|
|
|
|
recoveryWindow int32, fn func(*lntest.HarnessNode)) {
|
2018-04-03 02:57:58 +03:00
|
|
|
|
|
|
|
// Restore Carol, passing in the password, mnemonic, and
|
|
|
|
// desired recovery window.
|
|
|
|
node, err := net.RestoreNodeWithSeed(
|
2019-03-12 02:26:26 +03:00
|
|
|
"Carol", nil, password, mnemonic, recoveryWindow, nil,
|
2018-04-03 02:57:58 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to restore node: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-12 00:02:00 +03:00
|
|
|
// Query carol for her current wallet balance, and also that we
|
|
|
|
// gain the expected number of UTXOs.
|
|
|
|
var (
|
|
|
|
currBalance int64
|
|
|
|
currNumUTXOs uint32
|
|
|
|
)
|
2018-04-03 02:57:58 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
req := &lnrpc.WalletBalanceRequest{}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-03 02:57:58 +03:00
|
|
|
resp, err := node.WalletBalance(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query wallet balance: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that Carol's balance matches our expected
|
|
|
|
// amount.
|
|
|
|
currBalance = resp.ConfirmedBalance
|
|
|
|
if expAmount != currBalance {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-02-12 00:02:00 +03:00
|
|
|
utxoReq := &lnrpc.ListUnspentRequest{
|
|
|
|
MaxConfs: math.MaxInt32,
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
utxoResp, err := node.ListUnspent(ctxt, utxoReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query utxos: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
currNumUTXOs := len(utxoResp.Utxos)
|
|
|
|
if currNumUTXOs != expectedNumUTXOs {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-04-03 02:57:58 +03:00
|
|
|
return true
|
|
|
|
}, 15*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected restored node to have %d satoshis, "+
|
2019-02-12 00:02:00 +03:00
|
|
|
"instead has %d satoshis, expected %d utxos "+
|
|
|
|
"instead has %d", expAmount, currBalance,
|
|
|
|
expectedNumUTXOs, currNumUTXOs)
|
2018-04-03 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the user provided a callback, execute the commands against
|
|
|
|
// the restored Carol.
|
|
|
|
if fn != nil {
|
|
|
|
fn(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lastly, shutdown this Carol so we can move on to the next
|
|
|
|
// restoration.
|
2018-06-08 00:14:39 +03:00
|
|
|
shutdownAndAssert(net, t, node)
|
2018-04-03 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a closure-factory for building closures that can generate and
|
|
|
|
// skip a configurable number of addresses, before finally sending coins
|
|
|
|
// to a next generated address. The returned closure will apply the same
|
|
|
|
// behavior to both default P2WKH and NP2WKH scopes.
|
|
|
|
skipAndSend := func(nskip int) func(*lntest.HarnessNode) {
|
|
|
|
return func(node *lntest.HarnessNode) {
|
|
|
|
newP2WKHAddrReq := &lnrpc.NewAddressRequest{
|
|
|
|
Type: AddrTypeWitnessPubkeyHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
newNP2WKHAddrReq := &lnrpc.NewAddressRequest{
|
|
|
|
Type: AddrTypeNestedPubkeyHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate and skip the number of addresses requested.
|
|
|
|
for i := 0; i < nskip; i++ {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-03 02:57:58 +03:00
|
|
|
_, err = node.NewAddress(ctxt, newP2WKHAddrReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate new "+
|
|
|
|
"p2wkh address: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-03 02:57:58 +03:00
|
|
|
_, err = node.NewAddress(ctxt, newNP2WKHAddrReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate new "+
|
|
|
|
"np2wkh address: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send one BTC to the next P2WKH address.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-03 02:57:58 +03:00
|
|
|
err = net.SendCoins(
|
|
|
|
ctxt, btcutil.SatoshiPerBitcoin, node,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to node: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// And another to the next NP2WKH address.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-03 02:57:58 +03:00
|
|
|
err = net.SendCoinsNP2WKH(
|
|
|
|
ctxt, btcutil.SatoshiPerBitcoin, node,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to node: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore Carol with a recovery window of 0. Since no coins have been
|
|
|
|
// sent, her balance should be zero.
|
|
|
|
//
|
|
|
|
// After, one BTC is sent to both her first external P2WKH and NP2WKH
|
|
|
|
// addresses.
|
2019-02-12 00:02:00 +03:00
|
|
|
restoreCheckBalance(0, 0, 0, skipAndSend(0))
|
2018-04-03 02:57:58 +03:00
|
|
|
|
|
|
|
// Check that restoring without a look-ahead results in having no funds
|
|
|
|
// in the wallet, even though they exist on-chain.
|
2019-02-12 00:02:00 +03:00
|
|
|
restoreCheckBalance(0, 0, 0, nil)
|
2018-04-03 02:57:58 +03:00
|
|
|
|
2019-02-12 00:02:00 +03:00
|
|
|
// Now, check that using a look-ahead of 1 recovers the balance from
|
|
|
|
// the two transactions above. We should also now have 2 UTXOs in the
|
|
|
|
// wallet at the end of the recovery attempt.
|
2018-04-03 02:57:58 +03:00
|
|
|
//
|
|
|
|
// After, we will generate and skip 9 P2WKH and NP2WKH addresses, and
|
|
|
|
// send another BTC to the subsequent 10th address in each derivation
|
|
|
|
// path.
|
2019-02-12 00:02:00 +03:00
|
|
|
restoreCheckBalance(2*btcutil.SatoshiPerBitcoin, 2, 1, skipAndSend(9))
|
2018-04-03 02:57:58 +03:00
|
|
|
|
|
|
|
// Check that using a recovery window of 9 does not find the two most
|
|
|
|
// recent txns.
|
2019-02-12 00:02:00 +03:00
|
|
|
restoreCheckBalance(2*btcutil.SatoshiPerBitcoin, 2, 9, nil)
|
2018-04-03 02:57:58 +03:00
|
|
|
|
|
|
|
// Extending our recovery window to 10 should find the most recent
|
2019-02-12 00:02:00 +03:00
|
|
|
// transactions, leaving the wallet with 4 BTC total. We should also
|
|
|
|
// learn of the two additional UTXOs created above.
|
2018-04-03 02:57:58 +03:00
|
|
|
//
|
|
|
|
// After, we will skip 19 more addrs, sending to the 20th address past
|
|
|
|
// our last found address, and repeat the same checks.
|
2019-02-12 00:02:00 +03:00
|
|
|
restoreCheckBalance(4*btcutil.SatoshiPerBitcoin, 4, 10, skipAndSend(19))
|
2018-04-03 02:57:58 +03:00
|
|
|
|
|
|
|
// Check that recovering with a recovery window of 19 fails to find the
|
|
|
|
// most recent transactions.
|
2019-02-12 00:02:00 +03:00
|
|
|
restoreCheckBalance(4*btcutil.SatoshiPerBitcoin, 4, 19, nil)
|
2018-04-03 02:57:58 +03:00
|
|
|
|
2019-02-12 00:02:00 +03:00
|
|
|
// Ensure that using a recovery window of 20 succeeds with all UTXOs
|
|
|
|
// found and the final balance reflected.
|
|
|
|
restoreCheckBalance(6*btcutil.SatoshiPerBitcoin, 6, 20, nil)
|
2018-04-03 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
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.
|
2017-11-03 21:52:02 +03:00
|
|
|
func testBasicChannelFunding(net *lntest.NetworkHarness, t *harnessTest) {
|
2016-08-31 05:36:33 +03:00
|
|
|
ctxb := context.Background()
|
2016-09-15 22:24:52 +03:00
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
chanAmt := lnd.MaxBtcFundingAmount
|
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.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-01-10 06:34:22 +03:00
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-03-15 06:06:33 +03:00
|
|
|
err := net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't report channel: %v", err)
|
|
|
|
}
|
2017-10-19 01:13:36 +03:00
|
|
|
err = net.Bob.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't report channel: %v", err)
|
|
|
|
}
|
2017-03-15 06:06:33 +03:00
|
|
|
|
2018-06-15 08:53:00 +03:00
|
|
|
// With the channel open, ensure that the amount specified above has
|
2017-01-10 06:34:22 +03:00
|
|
|
// properly been pushed to Bob.
|
|
|
|
balReq := &lnrpc.ChannelBalanceRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
aliceBal, err := net.Alice.ChannelBalance(ctxt, balReq)
|
2017-01-10 06:34:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get alice's balance: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
bobBal, err := net.Bob.ChannelBalance(ctxt, balReq)
|
2017-01-10 06:34:22 +03:00
|
|
|
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.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
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
|
|
|
|
2019-05-24 15:17:48 +03:00
|
|
|
// testUnconfirmedChannelFunding tests that our unconfirmed change outputs can
|
|
|
|
// be used to fund channels.
|
2018-08-10 07:14:31 +03:00
|
|
|
func testUnconfirmedChannelFunding(net *lntest.NetworkHarness, t *harnessTest) {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2018-08-10 07:14:31 +03:00
|
|
|
const (
|
2019-05-15 10:02:53 +03:00
|
|
|
chanAmt = lnd.MaxBtcFundingAmount
|
2018-08-10 07:14:31 +03:00
|
|
|
pushAmt = btcutil.Amount(100000)
|
|
|
|
)
|
|
|
|
|
|
|
|
// We'll start off by creating a node for Carol.
|
|
|
|
carol, err := net.NewNode("Carol", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create carol's node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2019-05-24 15:17:48 +03:00
|
|
|
// We'll send her some confirmed funds.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2019-05-24 15:17:48 +03:00
|
|
|
err = net.SendCoins(ctxt, 2*chanAmt, carol)
|
2018-08-10 07:14:31 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-24 15:17:48 +03:00
|
|
|
// Now let Carol send some funds to herself, making a unconfirmed
|
|
|
|
// change output.
|
|
|
|
addrReq := &lnrpc.NewAddressRequest{
|
|
|
|
Type: lnrpc.AddressType_WITNESS_PUBKEY_HASH,
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := carol.NewAddress(ctxt, addrReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get new address: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sendReq := &lnrpc.SendCoinsRequest{
|
|
|
|
Addr: resp.Address,
|
|
|
|
Amount: int64(chanAmt) / 5,
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
_, err = carol.SendCoins(ctxt, sendReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-29 14:31:40 +03:00
|
|
|
// Make sure the unconfirmed tx is seen in the mempool.
|
|
|
|
_, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to find tx in miner mempool: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-10 07:14:31 +03:00
|
|
|
// Now, we'll connect her to Alice so that they can open a channel
|
|
|
|
// together. The funding flow should select Carol's unconfirmed output
|
|
|
|
// as she doesn't have any other funds since it's a new node.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-08-10 07:14:31 +03:00
|
|
|
if err := net.ConnectNodes(ctxt, carol, net.Alice); err != nil {
|
|
|
|
t.Fatalf("unable to connect dave to alice: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-08-10 07:14:31 +03:00
|
|
|
chanOpenUpdate, err := net.OpenChannel(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, carol, net.Alice,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
SpendUnconfirmed: true,
|
|
|
|
},
|
2018-08-10 07:14:31 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to open channel between carol and alice: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2018-11-29 14:33:11 +03:00
|
|
|
// Confirm the channel and wait for it to be recognized by both
|
|
|
|
// parties. Two transactions should be mined, the unconfirmed spend and
|
|
|
|
// the funding tx.
|
|
|
|
mineBlocks(t, net, 6, 2)
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-08-10 07:14:31 +03:00
|
|
|
chanPoint, err := net.WaitForChannelOpen(ctxt, chanOpenUpdate)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error while waiting for channel open: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the channel open, we'll check the balances on each side of the
|
|
|
|
// channel as a sanity check to ensure things worked out as intended.
|
|
|
|
balReq := &lnrpc.ChannelBalanceRequest{}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-08-10 07:14:31 +03:00
|
|
|
carolBal, err := carol.ChannelBalance(ctxt, balReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get carol's balance: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-08-10 07:14:31 +03:00
|
|
|
aliceBal, err := net.Alice.ChannelBalance(ctxt, balReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get alice's balance: %v", err)
|
|
|
|
}
|
|
|
|
if carolBal.Balance != int64(chanAmt-pushAmt-calcStaticFee(0)) {
|
|
|
|
t.Fatalf("carol's balance is incorrect: expected %v got %v",
|
|
|
|
chanAmt-pushAmt-calcStaticFee(0), carolBal)
|
|
|
|
}
|
|
|
|
if aliceBal.Balance != int64(pushAmt) {
|
|
|
|
t.Fatalf("alice's balance is incorrect: expected %v got %v",
|
|
|
|
pushAmt, aliceBal.Balance)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we're done with the test, the channel can be closed.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-08-10 07:14:31 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPoint, false)
|
|
|
|
}
|
|
|
|
|
2018-04-30 23:55:31 +03:00
|
|
|
// txStr returns the string representation of the channel's funding transaction.
|
|
|
|
func txStr(chanPoint *lnrpc.ChannelPoint) string {
|
2019-05-15 10:02:53 +03:00
|
|
|
fundingTxID, err := lnd.GetChanPointFundingTxid(chanPoint)
|
2018-04-30 23:55:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
cp := wire.OutPoint{
|
|
|
|
Hash: *fundingTxID,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
return cp.String()
|
|
|
|
}
|
|
|
|
|
2018-08-22 10:32:42 +03:00
|
|
|
// expectedChanUpdate houses params we expect a ChannelUpdate to advertise.
|
|
|
|
type expectedChanUpdate struct {
|
|
|
|
advertisingNode string
|
|
|
|
expectedPolicy *lnrpc.RoutingPolicy
|
|
|
|
chanPoint *lnrpc.ChannelPoint
|
|
|
|
}
|
2018-04-30 23:55:31 +03:00
|
|
|
|
2018-08-22 10:32:42 +03:00
|
|
|
// waitForChannelUpdate waits for a node to receive the expected channel
|
|
|
|
// updates.
|
2018-08-23 08:16:16 +03:00
|
|
|
func waitForChannelUpdate(t *harnessTest, subscription graphSubscription,
|
2018-08-22 10:32:42 +03:00
|
|
|
expUpdates []expectedChanUpdate) {
|
|
|
|
|
|
|
|
// Create an array indicating which expected channel updates we have
|
|
|
|
// received.
|
|
|
|
found := make([]bool, len(expUpdates))
|
2018-04-30 23:55:31 +03:00
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
2018-08-23 08:16:16 +03:00
|
|
|
case graphUpdate := <-subscription.updateChan:
|
2018-04-30 23:55:31 +03:00
|
|
|
for _, update := range graphUpdate.ChannelUpdates {
|
2018-08-22 10:32:42 +03:00
|
|
|
// For each expected update, check if it matches
|
|
|
|
// the update we just received.
|
|
|
|
for i, exp := range expUpdates {
|
|
|
|
fundingTxStr := txStr(update.ChanPoint)
|
|
|
|
if fundingTxStr != txStr(exp.chanPoint) {
|
|
|
|
continue
|
|
|
|
}
|
2018-04-30 23:55:31 +03:00
|
|
|
|
2018-08-22 10:32:42 +03:00
|
|
|
if update.AdvertisingNode !=
|
|
|
|
exp.advertisingNode {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := checkChannelPolicy(
|
|
|
|
update.RoutingPolicy,
|
|
|
|
exp.expectedPolicy,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// We got a policy update that matched
|
|
|
|
// the values and channel point of what
|
|
|
|
// we expected, mark it as found.
|
|
|
|
found[i] = true
|
|
|
|
|
|
|
|
// If we have no more channel updates
|
|
|
|
// we are waiting for, break out of the
|
|
|
|
// loop.
|
|
|
|
rem := 0
|
|
|
|
for _, f := range found {
|
|
|
|
if !f {
|
|
|
|
rem++
|
|
|
|
}
|
|
|
|
}
|
2018-04-30 23:55:31 +03:00
|
|
|
|
2018-08-22 10:32:42 +03:00
|
|
|
if rem == 0 {
|
|
|
|
break out
|
|
|
|
}
|
2018-04-30 23:55:31 +03:00
|
|
|
|
2018-08-22 10:32:42 +03:00
|
|
|
// Since we found a match among the
|
|
|
|
// expected updates, break out of the
|
|
|
|
// inner loop.
|
|
|
|
break
|
2018-04-30 23:55:31 +03:00
|
|
|
}
|
|
|
|
}
|
2018-08-23 08:16:16 +03:00
|
|
|
case err := <-subscription.errChan:
|
|
|
|
t.Fatalf("unable to recv graph update: %v", err)
|
2018-04-30 23:55:31 +03:00
|
|
|
case <-time.After(20 * time.Second):
|
|
|
|
t.Fatalf("did not receive channel update")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 04:13:20 +03:00
|
|
|
// assertNoChannelUpdates ensures that no ChannelUpdates are sent via the
|
|
|
|
// graphSubscription. This method will block for the provided duration before
|
|
|
|
// returning to the caller if successful.
|
|
|
|
func assertNoChannelUpdates(t *harnessTest, subscription graphSubscription,
|
|
|
|
duration time.Duration) {
|
|
|
|
|
|
|
|
timeout := time.After(duration)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case graphUpdate := <-subscription.updateChan:
|
|
|
|
if len(graphUpdate.ChannelUpdates) > 0 {
|
|
|
|
t.Fatalf("received %d channel updates when "+
|
|
|
|
"none were expected",
|
|
|
|
len(graphUpdate.ChannelUpdates))
|
|
|
|
}
|
|
|
|
|
|
|
|
case err := <-subscription.errChan:
|
|
|
|
t.Fatalf("graph subscription failure: %v", err)
|
|
|
|
|
|
|
|
case <-timeout:
|
|
|
|
// No updates received, success.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 03:34:42 +03:00
|
|
|
// getChannelPolicies queries the channel graph and retrieves the current edge
|
|
|
|
// policies for the provided channel points.
|
|
|
|
func getChannelPolicies(t *harnessTest, node *lntest.HarnessNode,
|
|
|
|
advertisingNode string,
|
|
|
|
chanPoints ...*lnrpc.ChannelPoint) []*lnrpc.RoutingPolicy {
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2018-09-26 18:30:09 +03:00
|
|
|
descReq := &lnrpc.ChannelGraphRequest{
|
|
|
|
IncludeUnannounced: true,
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
chanGraph, err := node.DescribeGraph(ctxt, descReq)
|
2018-04-30 23:55:31 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for alice's graph: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-03-13 03:34:42 +03:00
|
|
|
var policies []*lnrpc.RoutingPolicy
|
2018-04-30 23:55:31 +03:00
|
|
|
out:
|
|
|
|
for _, chanPoint := range chanPoints {
|
|
|
|
for _, e := range chanGraph.Edges {
|
|
|
|
if e.ChanPoint != txStr(chanPoint) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Node1Pub == advertisingNode {
|
2019-03-13 03:34:42 +03:00
|
|
|
policies = append(policies, e.Node1Policy)
|
2018-04-30 23:55:31 +03:00
|
|
|
} else {
|
2019-03-13 03:34:42 +03:00
|
|
|
policies = append(policies, e.Node2Policy)
|
2018-04-30 23:55:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
continue out
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've iterated over all the known edges and we weren't
|
|
|
|
// able to find this specific one, then we'll fail.
|
|
|
|
t.Fatalf("did not find edge %v", txStr(chanPoint))
|
|
|
|
}
|
2019-03-13 03:34:42 +03:00
|
|
|
|
|
|
|
return policies
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertChannelPolicy asserts that the passed node's known channel policy for
|
|
|
|
// the passed chanPoint is consistent with the expected policy values.
|
|
|
|
func assertChannelPolicy(t *harnessTest, node *lntest.HarnessNode,
|
|
|
|
advertisingNode string, expectedPolicy *lnrpc.RoutingPolicy,
|
|
|
|
chanPoints ...*lnrpc.ChannelPoint) {
|
|
|
|
|
|
|
|
policies := getChannelPolicies(t, node, advertisingNode, chanPoints...)
|
|
|
|
for _, policy := range policies {
|
|
|
|
err := checkChannelPolicy(policy, expectedPolicy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
}
|
2018-04-30 23:55:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkChannelPolicy checks that the policy matches the expected one.
|
|
|
|
func checkChannelPolicy(policy, expectedPolicy *lnrpc.RoutingPolicy) error {
|
|
|
|
if policy.FeeBaseMsat != expectedPolicy.FeeBaseMsat {
|
|
|
|
return fmt.Errorf("expected base fee %v, got %v",
|
|
|
|
expectedPolicy.FeeBaseMsat, policy.FeeBaseMsat)
|
|
|
|
}
|
|
|
|
if policy.FeeRateMilliMsat != expectedPolicy.FeeRateMilliMsat {
|
|
|
|
return fmt.Errorf("expected fee rate %v, got %v",
|
|
|
|
expectedPolicy.FeeRateMilliMsat,
|
|
|
|
policy.FeeRateMilliMsat)
|
|
|
|
}
|
|
|
|
if policy.TimeLockDelta != expectedPolicy.TimeLockDelta {
|
|
|
|
return fmt.Errorf("expected time lock delta %v, got %v",
|
|
|
|
expectedPolicy.TimeLockDelta,
|
|
|
|
policy.TimeLockDelta)
|
|
|
|
}
|
2018-08-22 10:32:43 +03:00
|
|
|
if policy.MinHtlc != expectedPolicy.MinHtlc {
|
|
|
|
return fmt.Errorf("expected min htlc %v, got %v",
|
|
|
|
expectedPolicy.MinHtlc, policy.MinHtlc)
|
|
|
|
}
|
2018-06-14 05:36:08 +03:00
|
|
|
if policy.Disabled != expectedPolicy.Disabled {
|
|
|
|
return errors.New("edge should be disabled but isn't")
|
|
|
|
}
|
2018-04-30 23:55:31 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-19 19:10:00 +03:00
|
|
|
// testUpdateChannelPolicy tests that policy updates made to a channel
|
|
|
|
// gets propagated to other nodes in the network.
|
|
|
|
func testUpdateChannelPolicy(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
2018-08-22 10:32:43 +03:00
|
|
|
const (
|
|
|
|
defaultFeeBase = 1000
|
|
|
|
defaultFeeRate = 1
|
2019-05-15 10:02:53 +03:00
|
|
|
defaultTimeLockDelta = lnd.DefaultBitcoinTimeLockDelta
|
2018-08-22 10:32:43 +03:00
|
|
|
defaultMinHtlc = 1000
|
|
|
|
)
|
|
|
|
|
2017-12-19 19:10:00 +03:00
|
|
|
// Launch notification clients for all nodes, such that we can
|
2018-04-04 06:18:42 +03:00
|
|
|
// get notified when they discover new channels and updates in the
|
|
|
|
// graph.
|
2018-08-23 08:16:16 +03:00
|
|
|
aliceSub := subscribeGraphNotifications(t, ctxb, net.Alice)
|
|
|
|
defer close(aliceSub.quit)
|
|
|
|
bobSub := subscribeGraphNotifications(t, ctxb, net.Bob)
|
|
|
|
defer close(bobSub.quit)
|
2017-12-19 19:10:00 +03:00
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
chanAmt := lnd.MaxBtcFundingAmount
|
2018-08-22 10:32:43 +03:00
|
|
|
pushAmt := chanAmt / 2
|
2017-12-19 19:10:00 +03:00
|
|
|
|
|
|
|
// Create a channel Alice->Bob.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-12-19 19:10:00 +03:00
|
|
|
|
2018-08-22 10:32:42 +03:00
|
|
|
// We add all the nodes' update channels to a slice, such that we can
|
|
|
|
// make sure they all receive the expected updates.
|
2018-08-23 08:16:16 +03:00
|
|
|
graphSubs := []graphSubscription{aliceSub, bobSub}
|
2018-08-22 10:32:42 +03:00
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, net.Bob}
|
|
|
|
|
2018-08-22 10:32:43 +03:00
|
|
|
// Alice and Bob should see each other's ChannelUpdates, advertising the
|
|
|
|
// default routing policies.
|
|
|
|
expectedPolicy := &lnrpc.RoutingPolicy{
|
|
|
|
FeeBaseMsat: defaultFeeBase,
|
|
|
|
FeeRateMilliMsat: defaultFeeRate,
|
|
|
|
TimeLockDelta: defaultTimeLockDelta,
|
|
|
|
MinHtlc: defaultMinHtlc,
|
|
|
|
}
|
|
|
|
|
2018-08-23 08:16:16 +03:00
|
|
|
for _, graphSub := range graphSubs {
|
2018-08-22 10:32:43 +03:00
|
|
|
waitForChannelUpdate(
|
2018-08-23 08:16:16 +03:00
|
|
|
t, graphSub,
|
2018-08-22 10:32:43 +03:00
|
|
|
[]expectedChanUpdate{
|
|
|
|
{net.Alice.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
{net.Bob.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// They should now know about the default policies.
|
|
|
|
for _, node := range nodes {
|
|
|
|
assertChannelPolicy(
|
|
|
|
t, node, net.Alice.PubKeyStr, expectedPolicy, chanPoint,
|
|
|
|
)
|
|
|
|
assertChannelPolicy(
|
|
|
|
t, node, net.Bob.PubKeyStr, expectedPolicy, chanPoint,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-12-19 19:10:00 +03:00
|
|
|
err := net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't report channel: %v", err)
|
|
|
|
}
|
|
|
|
err = net.Bob.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't report channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create Carol and a new channel Bob->Carol.
|
2018-04-27 11:42:04 +03:00
|
|
|
carol, err := net.NewNode("Carol", nil)
|
2017-12-19 19:10:00 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
|
|
|
|
// Clean up carol's node when the test finishes.
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-08-23 08:16:16 +03:00
|
|
|
carolSub := subscribeGraphNotifications(t, ctxb, carol)
|
|
|
|
defer close(carolSub.quit)
|
2017-12-19 19:10:00 +03:00
|
|
|
|
2018-08-23 08:16:16 +03:00
|
|
|
graphSubs = append(graphSubs, carolSub)
|
2018-08-22 10:32:42 +03:00
|
|
|
nodes = append(nodes, carol)
|
|
|
|
|
2018-08-22 10:32:43 +03:00
|
|
|
// Send some coins to Carol that can be used for channel funding.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-08-22 10:32:43 +03:00
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-12-19 19:10:00 +03:00
|
|
|
if err := net.ConnectNodes(ctxb, carol, net.Bob); err != nil {
|
|
|
|
t.Fatalf("unable to connect dave to alice: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-22 10:32:43 +03:00
|
|
|
// Open the channel Carol->Bob with a custom min_htlc value set. Since
|
|
|
|
// Carol is opening the channel, she will require Bob to not forward
|
|
|
|
// HTLCs smaller than this value, and hence he should advertise it as
|
|
|
|
// part of his ChannelUpdate.
|
|
|
|
const customMinHtlc = 5000
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint2 := openChannelAndAssert(
|
2018-08-22 10:32:43 +03:00
|
|
|
ctxt, t, net, carol, net.Bob,
|
2018-08-22 10:32:42 +03:00
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
2018-08-22 10:32:43 +03:00
|
|
|
MinHtlc: customMinHtlc,
|
2018-08-22 10:32:42 +03:00
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-12-19 19:10:00 +03:00
|
|
|
|
2018-08-22 10:32:43 +03:00
|
|
|
expectedPolicyBob := &lnrpc.RoutingPolicy{
|
|
|
|
FeeBaseMsat: defaultFeeBase,
|
|
|
|
FeeRateMilliMsat: defaultFeeRate,
|
|
|
|
TimeLockDelta: defaultTimeLockDelta,
|
|
|
|
MinHtlc: customMinHtlc,
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedPolicyCarol := &lnrpc.RoutingPolicy{
|
|
|
|
FeeBaseMsat: defaultFeeBase,
|
|
|
|
FeeRateMilliMsat: defaultFeeRate,
|
|
|
|
TimeLockDelta: defaultTimeLockDelta,
|
|
|
|
MinHtlc: defaultMinHtlc,
|
|
|
|
}
|
|
|
|
|
2018-08-23 08:16:16 +03:00
|
|
|
for _, graphSub := range graphSubs {
|
2018-08-22 10:32:43 +03:00
|
|
|
waitForChannelUpdate(
|
2018-08-23 08:16:16 +03:00
|
|
|
t, graphSub,
|
2018-08-22 10:32:43 +03:00
|
|
|
[]expectedChanUpdate{
|
|
|
|
{net.Bob.PubKeyStr, expectedPolicyBob, chanPoint2},
|
|
|
|
{carol.PubKeyStr, expectedPolicyCarol, chanPoint2},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that all nodes now know about the updated policies.
|
|
|
|
for _, node := range nodes {
|
|
|
|
assertChannelPolicy(
|
|
|
|
t, node, net.Bob.PubKeyStr, expectedPolicyBob,
|
|
|
|
chanPoint2,
|
|
|
|
)
|
|
|
|
assertChannelPolicy(
|
|
|
|
t, node, carol.PubKeyStr, expectedPolicyCarol,
|
|
|
|
chanPoint2,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-14 01:35:54 +03:00
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't report channel: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-12-19 19:10:00 +03:00
|
|
|
err = net.Bob.WaitForNetworkChannelOpen(ctxt, chanPoint2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't report channel: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-12-19 19:10:00 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPoint2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("carol didn't report channel: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-22 10:32:44 +03:00
|
|
|
// First we'll try to send a payment from Alice to Carol with an amount
|
|
|
|
// less than the min_htlc value required by Carol. This payment should
|
|
|
|
// fail, as the channel Bob->Carol cannot carry HTLCs this small.
|
|
|
|
payAmt := btcutil.Amount(4)
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
Value: int64(payAmt),
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := carol.AddInvoice(ctxt, invoice)
|
2018-08-22 10:32:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-08-22 10:32:44 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, net.Alice, []string{resp.PaymentRequest}, true,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Alice knows about the channel policy of Carol and should therefore
|
|
|
|
// not be able to find a path during routing.
|
|
|
|
if err == nil ||
|
|
|
|
!strings.Contains(err.Error(), "unable to find a path") {
|
|
|
|
t.Fatalf("expected payment to fail, instead got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we try to send a payment over the channel with a value too low
|
|
|
|
// to be accepted. First we query for a route to route a payment of
|
|
|
|
// 5000 mSAT, as this is accepted.
|
|
|
|
payAmt = btcutil.Amount(5)
|
|
|
|
routesReq := &lnrpc.QueryRoutesRequest{
|
|
|
|
PubKey: carol.PubKeyStr,
|
|
|
|
Amt: int64(payAmt),
|
2019-03-12 04:43:37 +03:00
|
|
|
FinalCltvDelta: defaultTimeLockDelta,
|
2018-08-22 10:32:44 +03:00
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-08-22 10:32:44 +03:00
|
|
|
routes, err := net.Alice.QueryRoutes(ctxt, routesReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get route: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes.Routes) != 1 {
|
|
|
|
t.Fatalf("expected to find 1 route, got %v", len(routes.Routes))
|
|
|
|
}
|
|
|
|
|
|
|
|
// We change the route to carry a payment of 4000 mSAT instead of 5000
|
|
|
|
// mSAT.
|
|
|
|
payAmt = btcutil.Amount(4)
|
|
|
|
amtSat := int64(payAmt)
|
|
|
|
amtMSat := int64(lnwire.NewMSatFromSatoshis(payAmt))
|
|
|
|
routes.Routes[0].Hops[0].AmtToForward = amtSat
|
|
|
|
routes.Routes[0].Hops[0].AmtToForwardMsat = amtMSat
|
|
|
|
routes.Routes[0].Hops[1].AmtToForward = amtSat
|
|
|
|
routes.Routes[0].Hops[1].AmtToForwardMsat = amtMSat
|
|
|
|
|
|
|
|
// Send the payment with the modified value.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-08-22 10:32:44 +03:00
|
|
|
alicePayStream, err := net.Alice.SendToRoute(ctxt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for alice: %v", err)
|
|
|
|
}
|
|
|
|
sendReq := &lnrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: resp.RHash,
|
2018-08-08 12:09:30 +03:00
|
|
|
Route: routes.Routes[0],
|
2018-08-22 10:32:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = alicePayStream.Send(sendReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect this payment to fail, and that the min_htlc value is
|
|
|
|
// communicated back to us, since the attempted HTLC value was too low.
|
|
|
|
sendResp, err := alicePayStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expected as part of the error message.
|
|
|
|
substrs := []string{
|
|
|
|
"AmountBelowMinimum",
|
|
|
|
"HtlcMinimumMsat: (lnwire.MilliSatoshi) 5000 mSAT",
|
|
|
|
}
|
|
|
|
for _, s := range substrs {
|
|
|
|
if !strings.Contains(sendResp.PaymentError, s) {
|
|
|
|
t.Fatalf("expected error to contain \"%v\", instead "+
|
2018-08-30 04:00:54 +03:00
|
|
|
"got %v", s, sendResp.PaymentError)
|
2018-08-22 10:32:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure sending using the original value succeeds.
|
|
|
|
payAmt = btcutil.Amount(5)
|
|
|
|
amtSat = int64(payAmt)
|
|
|
|
amtMSat = int64(lnwire.NewMSatFromSatoshis(payAmt))
|
|
|
|
routes.Routes[0].Hops[0].AmtToForward = amtSat
|
|
|
|
routes.Routes[0].Hops[0].AmtToForwardMsat = amtMSat
|
|
|
|
routes.Routes[0].Hops[1].AmtToForward = amtSat
|
|
|
|
routes.Routes[0].Hops[1].AmtToForwardMsat = amtMSat
|
|
|
|
|
|
|
|
sendReq = &lnrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: resp.RHash,
|
2018-08-08 12:09:30 +03:00
|
|
|
Route: routes.Routes[0],
|
2018-08-22 10:32:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = alicePayStream.Send(sendReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sendResp, err = alicePayStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sendResp.PaymentError != "" {
|
|
|
|
t.Fatalf("expected payment to succeed, instead got %v",
|
|
|
|
sendResp.PaymentError)
|
|
|
|
}
|
|
|
|
|
2018-04-04 06:18:42 +03:00
|
|
|
// With our little cluster set up, we'll update the fees for the
|
|
|
|
// channel Bob side of the Alice->Bob channel, and make sure all nodes
|
|
|
|
// learn about it.
|
2017-12-19 19:10:00 +03:00
|
|
|
baseFee := int64(1500)
|
|
|
|
feeRate := int64(12)
|
|
|
|
timeLockDelta := uint32(66)
|
|
|
|
|
2018-08-22 10:32:43 +03:00
|
|
|
expectedPolicy = &lnrpc.RoutingPolicy{
|
2018-04-30 23:55:31 +03:00
|
|
|
FeeBaseMsat: baseFee,
|
2018-06-16 15:53:44 +03:00
|
|
|
FeeRateMilliMsat: testFeeBase * feeRate,
|
2018-04-30 23:55:31 +03:00
|
|
|
TimeLockDelta: timeLockDelta,
|
2018-08-22 10:32:43 +03:00
|
|
|
MinHtlc: defaultMinHtlc,
|
2018-04-30 23:55:31 +03:00
|
|
|
}
|
|
|
|
|
2017-12-19 19:10:00 +03:00
|
|
|
req := &lnrpc.PolicyUpdateRequest{
|
|
|
|
BaseFeeMsat: baseFee,
|
|
|
|
FeeRate: float64(feeRate),
|
|
|
|
TimeLockDelta: timeLockDelta,
|
2018-04-30 23:55:31 +03:00
|
|
|
Scope: &lnrpc.PolicyUpdateRequest_ChanPoint{
|
|
|
|
ChanPoint: chanPoint,
|
|
|
|
},
|
2017-12-19 19:10:00 +03:00
|
|
|
}
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if _, err := net.Bob.UpdateChannelPolicy(ctxt, req); err != nil {
|
2017-12-19 19:10:00 +03:00
|
|
|
t.Fatalf("unable to get alice's balance: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-04-04 06:18:42 +03:00
|
|
|
// Wait for all nodes to have seen the policy update done by Bob.
|
2018-08-23 08:16:16 +03:00
|
|
|
for _, graphSub := range graphSubs {
|
2018-08-22 10:32:42 +03:00
|
|
|
waitForChannelUpdate(
|
2018-08-23 08:16:16 +03:00
|
|
|
t, graphSub,
|
2018-08-22 10:32:42 +03:00
|
|
|
[]expectedChanUpdate{
|
|
|
|
{net.Bob.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2017-12-19 19:10:00 +03:00
|
|
|
|
2018-04-04 06:18:42 +03:00
|
|
|
// Check that all nodes now know about Bob's updated policy.
|
2018-08-22 10:32:42 +03:00
|
|
|
for _, node := range nodes {
|
|
|
|
assertChannelPolicy(
|
|
|
|
t, node, net.Bob.PubKeyStr, expectedPolicy, chanPoint,
|
|
|
|
)
|
|
|
|
}
|
2018-04-04 06:18:42 +03:00
|
|
|
|
|
|
|
// Now that all nodes have received the new channel update, we'll try
|
|
|
|
// to send a payment from Alice to Carol to ensure that Alice has
|
|
|
|
// internalized this fee update. This shouldn't affect the route that
|
|
|
|
// Alice takes though: we updated the Alice -> Bob channel and she
|
|
|
|
// doesn't pay for transit over that channel as it's direct.
|
2018-08-22 10:32:43 +03:00
|
|
|
// Note that the payment amount is >= the min_htlc value for the
|
|
|
|
// channel Bob->Carol, so it should successfully be forwarded.
|
2018-08-22 10:32:44 +03:00
|
|
|
payAmt = btcutil.Amount(5)
|
|
|
|
invoice = &lnrpc.Invoice{
|
2018-04-04 06:18:42 +03:00
|
|
|
Memo: "testing",
|
|
|
|
Value: int64(payAmt),
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err = carol.AddInvoice(ctxt, invoice)
|
2018-04-04 06:18:42 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-04 06:18:42 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, net.Alice, []string{resp.PaymentRequest}, true,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
2017-12-19 19:10:00 +03:00
|
|
|
|
2018-04-04 06:18:42 +03:00
|
|
|
// We'll now open a channel from Alice directly to Carol.
|
2017-12-19 19:10:00 +03:00
|
|
|
if err := net.ConnectNodes(ctxb, net.Alice, carol); err != nil {
|
|
|
|
t.Fatalf("unable to connect dave to alice: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-04-04 06:18:42 +03:00
|
|
|
chanPoint3 := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-04-04 06:18:42 +03:00
|
|
|
)
|
2017-12-19 19:10:00 +03:00
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-12-19 19:10:00 +03:00
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't report channel: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-12-19 19:10:00 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPoint3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't report channel: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-04-04 06:18:42 +03:00
|
|
|
// Make a global update, and check that both channels' new policies get
|
|
|
|
// propagated.
|
2017-12-19 19:10:00 +03:00
|
|
|
baseFee = int64(800)
|
|
|
|
feeRate = int64(123)
|
|
|
|
timeLockDelta = uint32(22)
|
|
|
|
|
2018-04-30 23:55:31 +03:00
|
|
|
expectedPolicy.FeeBaseMsat = baseFee
|
2018-06-16 15:53:44 +03:00
|
|
|
expectedPolicy.FeeRateMilliMsat = testFeeBase * feeRate
|
2018-04-30 23:55:31 +03:00
|
|
|
expectedPolicy.TimeLockDelta = timeLockDelta
|
|
|
|
|
2017-12-19 19:10:00 +03:00
|
|
|
req = &lnrpc.PolicyUpdateRequest{
|
|
|
|
BaseFeeMsat: baseFee,
|
|
|
|
FeeRate: float64(feeRate),
|
|
|
|
TimeLockDelta: timeLockDelta,
|
|
|
|
}
|
|
|
|
req.Scope = &lnrpc.PolicyUpdateRequest_Global{}
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
_, err = net.Alice.UpdateChannelPolicy(ctxt, req)
|
2017-12-19 19:10:00 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get alice's balance: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-04-04 06:18:42 +03:00
|
|
|
// Wait for all nodes to have seen the policy updates for both of
|
|
|
|
// Alice's channels.
|
2018-08-23 08:16:16 +03:00
|
|
|
for _, graphSub := range graphSubs {
|
2018-08-22 10:32:42 +03:00
|
|
|
waitForChannelUpdate(
|
2018-08-23 08:16:16 +03:00
|
|
|
t, graphSub,
|
2018-08-22 10:32:42 +03:00
|
|
|
[]expectedChanUpdate{
|
|
|
|
{net.Alice.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
{net.Alice.PubKeyStr, expectedPolicy, chanPoint3},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2017-12-19 19:10:00 +03:00
|
|
|
|
2018-04-04 06:18:42 +03:00
|
|
|
// And finally check that all nodes remembers the policy update they
|
|
|
|
// received.
|
2018-08-22 10:32:42 +03:00
|
|
|
for _, node := range nodes {
|
|
|
|
assertChannelPolicy(
|
|
|
|
t, node, net.Alice.PubKeyStr, expectedPolicy,
|
|
|
|
chanPoint, chanPoint3,
|
|
|
|
)
|
|
|
|
}
|
2017-12-19 19:10:00 +03:00
|
|
|
|
|
|
|
// Close the channels.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-12-19 19:10:00 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-12-19 19:10:00 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Bob, chanPoint2, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-08-09 22:37:30 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint3, false)
|
2017-12-19 19:10:00 +03:00
|
|
|
}
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// waitForNodeBlockHeight queries the node for its current block height until
|
|
|
|
// it reaches the passed height.
|
|
|
|
func waitForNodeBlockHeight(ctx context.Context, node *lntest.HarnessNode,
|
|
|
|
height int32) error {
|
|
|
|
var predErr error
|
|
|
|
err := lntest.WaitPredicate(func() bool {
|
|
|
|
ctxt, _ := context.WithTimeout(ctx, 10*time.Second)
|
|
|
|
info, err := node.GetInfo(ctxt, &lnrpc.GetInfoRequest{})
|
|
|
|
if err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if int32(info.BlockHeight) != height {
|
|
|
|
predErr = fmt.Errorf("expected block height to "+
|
|
|
|
"be %v, was %v", height, info.BlockHeight)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, 15*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
return predErr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertMinerBlockHeightDelta ensures that tempMiner is 'delta' blocks ahead
|
|
|
|
// of miner.
|
|
|
|
func assertMinerBlockHeightDelta(t *harnessTest,
|
|
|
|
miner, tempMiner *rpctest.Harness, delta int32) {
|
|
|
|
|
|
|
|
// Ensure the chain lengths are what we expect.
|
|
|
|
var predErr error
|
|
|
|
err := lntest.WaitPredicate(func() bool {
|
|
|
|
_, tempMinerHeight, err := tempMiner.Node.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to get current "+
|
|
|
|
"blockheight %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
_, minerHeight, err := miner.Node.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to get current "+
|
|
|
|
"blockheight %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if tempMinerHeight != minerHeight+delta {
|
|
|
|
predErr = fmt.Errorf("expected new miner(%d) to be %d "+
|
|
|
|
"blocks ahead of original miner(%d)",
|
|
|
|
tempMinerHeight, delta, minerHeight)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(predErr.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 18:56:03 +03:00
|
|
|
// testOpenChannelAfterReorg tests that in the case where we have an open
|
|
|
|
// channel where the funding tx gets reorged out, the channel will no
|
|
|
|
// longer be present in the node's routing table.
|
2017-11-03 21:52:02 +03:00
|
|
|
func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) {
|
2019-05-24 15:17:49 +03:00
|
|
|
// Skip test for neutrino, as we cannot disconnect the miner at will.
|
|
|
|
// TODO(halseth): remove when either can disconnect at will, or restart
|
|
|
|
// node with connection to new miner.
|
|
|
|
if net.BackendCfg.Name() == "neutrino" {
|
|
|
|
t.Skipf("skipping reorg test for neutrino backend")
|
|
|
|
}
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
var (
|
|
|
|
ctxb = context.Background()
|
|
|
|
temp = "temp"
|
|
|
|
)
|
2017-10-02 18:56:03 +03:00
|
|
|
|
|
|
|
// Set up a new miner that we can use to cause a reorg.
|
2018-08-17 05:48:25 +03:00
|
|
|
args := []string{"--rejectnonstd", "--txindex"}
|
2018-12-14 11:24:00 +03:00
|
|
|
tempMiner, err := rpctest.New(harnessNetParams,
|
2017-10-02 18:56:03 +03:00
|
|
|
&rpcclient.NotificationHandlers{}, args)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create mining node: %v", err)
|
|
|
|
}
|
2018-12-14 11:24:00 +03:00
|
|
|
if err := tempMiner.SetUp(false, 0); err != nil {
|
2017-10-02 18:56:03 +03:00
|
|
|
t.Fatalf("unable to set up mining node: %v", err)
|
|
|
|
}
|
2018-12-14 11:24:00 +03:00
|
|
|
defer tempMiner.TearDown()
|
2017-10-02 18:56:03 +03:00
|
|
|
|
|
|
|
// We start by connecting the new miner to our original miner,
|
|
|
|
// such that it will sync to our original chain.
|
2018-12-14 11:24:00 +03:00
|
|
|
err = net.Miner.Node.Node(
|
|
|
|
btcjson.NConnect, tempMiner.P2PAddress(), &temp,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to remove node: %v", err)
|
2017-10-02 18:56:03 +03:00
|
|
|
}
|
2018-12-14 11:24:00 +03:00
|
|
|
nodeSlice := []*rpctest.Harness{net.Miner, tempMiner}
|
2017-10-02 18:56:03 +03:00
|
|
|
if err := rpctest.JoinNodes(nodeSlice, rpctest.Blocks); err != nil {
|
|
|
|
t.Fatalf("unable to join node on blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// The two miners should be on the same blockheight.
|
|
|
|
assertMinerBlockHeightDelta(t, net.Miner, tempMiner, 0)
|
2017-10-02 18:56:03 +03:00
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// We disconnect the two miners, such that we can mine two different
|
|
|
|
// chains and can cause a reorg later.
|
|
|
|
err = net.Miner.Node.Node(
|
|
|
|
btcjson.NDisconnect, tempMiner.P2PAddress(), &temp,
|
|
|
|
)
|
2017-10-02 18:56:03 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to remove node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new channel that requires 1 confs before it's considered
|
|
|
|
// open, then broadcast the funding transaction
|
2019-05-15 10:02:53 +03:00
|
|
|
chanAmt := lnd.MaxBtcFundingAmount
|
2017-10-02 18:56:03 +03:00
|
|
|
pushAmt := btcutil.Amount(0)
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2017-10-02 18:56:03 +03:00
|
|
|
pendingUpdate, err := net.OpenPendingChannel(ctxt, net.Alice, net.Bob,
|
|
|
|
chanAmt, pushAmt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to open channel: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// Wait for miner to have seen the funding tx. The temporary miner is
|
|
|
|
// disconnected, and won't see the transaction.
|
2018-11-29 14:31:40 +03:00
|
|
|
_, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to find funding tx in mempool: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-02 18:56:03 +03:00
|
|
|
// At this point, the channel's funding transaction will have been
|
|
|
|
// broadcast, but not confirmed, and the channel should be pending.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-10-02 18:56:03 +03:00
|
|
|
assertNumOpenChannelsPending(ctxt, 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We now cause a fork, by letting our original miner mine 10 blocks,
|
|
|
|
// and our new miner mine 15. This will also confirm our pending
|
2018-12-14 11:24:00 +03:00
|
|
|
// channel on the original miner's chain, which should be considered
|
|
|
|
// open.
|
2018-11-29 14:33:11 +03:00
|
|
|
block := mineBlocks(t, net, 10, 1)[0]
|
2017-10-02 18:56:03 +03:00
|
|
|
assertTxInBlock(t, block, fundingTxID)
|
2018-12-14 11:24:00 +03:00
|
|
|
if _, err := tempMiner.Node.Generate(15); err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
2017-10-02 18:56:03 +03:00
|
|
|
}
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// Ensure the chain lengths are what we expect, with the temp miner
|
|
|
|
// being 5 blocks ahead.
|
|
|
|
assertMinerBlockHeightDelta(t, net.Miner, tempMiner, 5)
|
|
|
|
|
|
|
|
// Wait for Alice to sync to the original miner's chain.
|
|
|
|
_, minerHeight, err := net.Miner.Node.GetBestBlock()
|
2017-10-02 18:56:03 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get current blockheight %v", err)
|
|
|
|
}
|
2018-12-14 11:24:00 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = waitForNodeBlockHeight(ctxt, net.Alice, minerHeight)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to sync to chain: %v", err)
|
2017-10-02 18:56:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
chanPoint := &lnrpc.ChannelPoint{
|
2018-01-11 07:59:30 +03:00
|
|
|
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
|
|
|
|
FundingTxidBytes: pendingUpdate.Txid,
|
|
|
|
},
|
2017-10-02 18:56:03 +03:00
|
|
|
OutputIndex: pendingUpdate.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure channel is no longer pending.
|
|
|
|
assertNumOpenChannelsPending(ctxt, t, net.Alice, net.Bob, 0)
|
|
|
|
|
|
|
|
// Wait for Alice and Bob to recognize and advertise the new channel
|
|
|
|
// generated above.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-10-02 18:56:03 +03:00
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-10-02 18:56:03 +03:00
|
|
|
err = net.Bob.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice should now have 1 edge in her graph.
|
2018-09-26 18:30:09 +03:00
|
|
|
req := &lnrpc.ChannelGraphRequest{
|
|
|
|
IncludeUnannounced: true,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
chanGraph, err := net.Alice.DescribeGraph(ctxt, req)
|
2017-10-02 18:56:03 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for alice's routing table: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
numEdges := len(chanGraph.Edges)
|
|
|
|
if numEdges != 1 {
|
|
|
|
t.Fatalf("expected to find one edge in the graph, found %d",
|
|
|
|
numEdges)
|
|
|
|
}
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// Now we disconnect Alice's chain backend from the original miner, and
|
|
|
|
// connect the two miners together. Since the temporary miner knows
|
|
|
|
// about a longer chain, both miners should sync to that chain.
|
2019-05-24 15:17:48 +03:00
|
|
|
err = net.BackendCfg.DisconnectMiner()
|
2018-12-14 11:24:00 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to remove node: %v", err)
|
2017-10-02 18:56:03 +03:00
|
|
|
}
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// Connecting to the temporary miner should now cause our original
|
|
|
|
// chain to be re-orged out.
|
|
|
|
err = net.Miner.Node.Node(
|
|
|
|
btcjson.NConnect, tempMiner.P2PAddress(), &temp,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to remove node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := []*rpctest.Harness{tempMiner, net.Miner}
|
|
|
|
if err := rpctest.JoinNodes(nodes, rpctest.Blocks); err != nil {
|
2017-10-02 18:56:03 +03:00
|
|
|
t.Fatalf("unable to join node on blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once again they should be on the same chain.
|
2018-12-14 11:24:00 +03:00
|
|
|
assertMinerBlockHeightDelta(t, net.Miner, tempMiner, 0)
|
|
|
|
|
|
|
|
// Now we disconnect the two miners, and connect our original miner to
|
|
|
|
// our chain backend once again.
|
|
|
|
err = net.Miner.Node.Node(
|
|
|
|
btcjson.NDisconnect, tempMiner.P2PAddress(), &temp,
|
|
|
|
)
|
2017-10-02 18:56:03 +03:00
|
|
|
if err != nil {
|
2018-12-14 11:24:00 +03:00
|
|
|
t.Fatalf("unable to remove node: %v", err)
|
2017-10-02 18:56:03 +03:00
|
|
|
}
|
|
|
|
|
2019-05-24 15:17:48 +03:00
|
|
|
err = net.BackendCfg.ConnectMiner()
|
2017-10-02 18:56:03 +03:00
|
|
|
if err != nil {
|
2018-12-14 11:24:00 +03:00
|
|
|
t.Fatalf("unable to remove node: %v", err)
|
2017-10-02 18:56:03 +03:00
|
|
|
}
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// This should have caused a reorg, and Alice should sync to the longer
|
|
|
|
// chain, where the funding transaction is not confirmed.
|
|
|
|
_, tempMinerHeight, err := tempMiner.Node.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get current blockheight %v", err)
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = waitForNodeBlockHeight(ctxt, net.Alice, tempMinerHeight)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to sync to chain: %v", err)
|
2017-10-02 18:56:03 +03:00
|
|
|
}
|
2017-11-03 07:30:19 +03:00
|
|
|
|
2017-10-02 18:56:03 +03:00
|
|
|
// Since the fundingtx was reorged out, Alice should now have no edges
|
|
|
|
// in her graph.
|
2018-09-26 18:30:09 +03:00
|
|
|
req = &lnrpc.ChannelGraphRequest{
|
|
|
|
IncludeUnannounced: true,
|
|
|
|
}
|
2017-10-02 18:56:03 +03:00
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
var predErr error
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
chanGraph, err = net.Alice.DescribeGraph(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for alice's routing table: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
numEdges = len(chanGraph.Edges)
|
|
|
|
if numEdges != 0 {
|
|
|
|
predErr = fmt.Errorf("expected to find no edge in the graph, found %d",
|
|
|
|
numEdges)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(predErr.Error())
|
2017-10-02 18:56:03 +03:00
|
|
|
}
|
|
|
|
|
2018-10-29 07:52:09 +03:00
|
|
|
// Cleanup by mining the funding tx again, then closing the channel.
|
2018-11-29 14:33:11 +03:00
|
|
|
block = mineBlocks(t, net, 1, 1)[0]
|
2018-10-29 07:52:09 +03:00
|
|
|
assertTxInBlock(t, block, fundingTxID)
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2019-03-13 03:34:42 +03:00
|
|
|
closeReorgedChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
2017-10-02 18:56:03 +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
|
2017-11-03 21:52:02 +03:00
|
|
|
func testDisconnectingTargetPeer(net *lntest.NetworkHarness, t *harnessTest) {
|
2017-05-05 10:00:39 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// Check existing connection.
|
2018-11-28 11:34:50 +03:00
|
|
|
assertNumConnections(t, net.Alice, net.Bob, 1)
|
2017-05-05 10:00:39 +03:00
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
chanAmt := lnd.MaxBtcFundingAmount
|
2017-05-05 10:00:39 +03:00
|
|
|
pushAmt := btcutil.Amount(0)
|
|
|
|
|
|
|
|
// Create a new channel that requires 1 confs before it's considered
|
|
|
|
// open, then broadcast the funding transaction
|
|
|
|
const numConfs = 1
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2017-05-05 10:00:39 +03:00
|
|
|
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.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-05-05 10:00:39 +03:00
|
|
|
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.
|
2018-11-28 11:34:50 +03:00
|
|
|
assertNumConnections(t, net.Alice, net.Bob, 1)
|
2017-05-05 10:00:39 +03:00
|
|
|
|
|
|
|
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.
|
2018-11-29 14:33:11 +03:00
|
|
|
block := mineBlocks(t, net, numConfs, 1)[0]
|
2017-05-05 10:00:39 +03:00
|
|
|
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)
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-05-05 10:00:39 +03:00
|
|
|
|
|
|
|
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.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-05-05 10:00:39 +03:00
|
|
|
if err := net.AssertChannelExists(ctxt, net.Alice, &outPoint); err != nil {
|
|
|
|
t.Fatalf("unable to assert channel existence: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-05-05 10:00:39 +03:00
|
|
|
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{
|
2018-01-11 07:59:30 +03:00
|
|
|
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
|
|
|
|
FundingTxidBytes: pendingUpdate.Txid,
|
|
|
|
},
|
2017-05-05 10:00:39 +03:00
|
|
|
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.
|
2018-11-28 11:34:50 +03:00
|
|
|
assertNumConnections(t, net.Alice, net.Bob, 1)
|
2017-05-05 10:00:39 +03:00
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-05-05 10:00:39 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, true)
|
|
|
|
|
|
|
|
// Disconnect Alice-peer from Bob-peer without getting error
|
|
|
|
// about existing channels.
|
2018-04-13 14:20:12 +03:00
|
|
|
var predErr error
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
if err := net.DisconnectNodes(ctxt, net.Alice, net.Bob); err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to disconnect Bob's peer from Alice's: err %v",
|
|
|
|
predErr)
|
2017-05-05 10:00:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check zero peer connections.
|
2018-11-28 11:34:50 +03:00
|
|
|
assertNumConnections(t, net.Alice, net.Bob, 0)
|
2017-05-05 10:00:39 +03:00
|
|
|
|
|
|
|
// Finally, re-connect both nodes.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
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.
|
2018-11-28 11:34:50 +03:00
|
|
|
assertNumConnections(t, net.Alice, net.Bob, 1)
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-11-06 13:03:01 +03:00
|
|
|
// Cleanup by mining the force close and sweep transaction.
|
|
|
|
cleanupForceClose(t, net, net.Alice, chanPoint)
|
2017-05-05 10:00:39 +03:00
|
|
|
}
|
|
|
|
|
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.
|
2017-11-03 21:52:02 +03:00
|
|
|
func testChannelFundingPersistence(net *lntest.NetworkHarness, t *harnessTest) {
|
2017-01-31 07:21:52 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
chanAmt := lnd.MaxBtcFundingAmount
|
2017-01-31 07:21:52 +03:00
|
|
|
pushAmt := btcutil.Amount(0)
|
|
|
|
|
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
|
2017-12-19 19:10:00 +03:00
|
|
|
carolArgs := []string{fmt.Sprintf("--bitcoin.defaultchanconfs=%v", numConfs)}
|
2018-04-27 11:42:04 +03:00
|
|
|
carol, err := net.NewNode("Carol", carolArgs)
|
2017-07-31 06:26:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
|
|
|
|
// Clean up carol's node when the test finishes.
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
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
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
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.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
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.
|
2018-11-29 14:33:11 +03:00
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
2017-01-31 07:21:52 +03:00
|
|
|
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.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.EnsureConnected(ctxt, net.Alice, carol); err != nil {
|
2018-03-31 05:59:58 +03:00
|
|
|
t.Fatalf("peers unable to reconnect after restart: %v", err)
|
2017-01-31 07:21:52 +03:00
|
|
|
}
|
|
|
|
|
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)
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
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)
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
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.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
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)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
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{
|
2018-01-11 07:59:30 +03:00
|
|
|
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
|
|
|
|
FundingTxidBytes: pendingUpdate.Txid,
|
|
|
|
},
|
2017-01-31 07:21:52 +03:00
|
|
|
OutputIndex: pendingUpdate.OutputIndex,
|
|
|
|
}
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-05-05 04:06:12 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
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.
|
2017-11-03 21:52:02 +03:00
|
|
|
func testChannelBalance(net *lntest.NetworkHarness, t *harnessTest) {
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxb := context.Background()
|
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.
|
2019-05-15 10:02:53 +03:00
|
|
|
amount := lnd.MaxBtcFundingAmount
|
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) {
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
response, err := node.ChannelBalance(ctxt, &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
|
|
|
|
2018-03-31 05:59:58 +03:00
|
|
|
// Before beginning, make sure alice and bob are connected.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.EnsureConnected(ctxt, net.Alice, net.Bob); err != nil {
|
2018-03-31 05:59:58 +03:00
|
|
|
t.Fatalf("unable to connect alice and bob: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
2018-08-22 10:32:42 +03:00
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: amount,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
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.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-03-15 06:06:33 +03:00
|
|
|
err := net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-03-15 06:06:33 +03:00
|
|
|
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.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
2016-08-30 08:07:54 +03:00
|
|
|
}
|
|
|
|
|
2018-12-07 03:49:05 +03:00
|
|
|
// testChannelUnsettledBalance will test that the UnsettledBalance field
|
|
|
|
// is updated according to the number of Pending Htlcs.
|
|
|
|
// Alice will send Htlcs to Carol while she is in hodl mode. This will result
|
|
|
|
// in a build of pending Htlcs. We expect the channels unsettled balance to
|
|
|
|
// equal the sum of all the Pending Htlcs.
|
|
|
|
func testChannelUnsettledBalance(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
const chanAmt = btcutil.Amount(1000000)
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// Create carol in hodl mode.
|
|
|
|
carol, err := net.NewNode("Carol", []string{
|
|
|
|
"--debughtlc",
|
|
|
|
"--hodl.exit-settle",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
|
|
|
// Connect Alice to Carol.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxb, net.Alice, carol); err != nil {
|
|
|
|
t.Fatalf("unable to connect alice to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open a channel between Alice and Carol.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
|
|
|
chanPointAlice := openChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Alice, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Wait for Alice and Carol to receive the channel edge from the
|
|
|
|
// funding manager.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPointAlice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't see the alice->carol channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPointAlice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't see the alice->carol channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Channel should be ready for payments.
|
|
|
|
const (
|
|
|
|
payAmt = 100
|
|
|
|
numInvoices = 6
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create a paystream from Alice to Carol to enable Alice to make
|
|
|
|
// a series of payments.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
alicePayStream, err := net.Alice.SendPayment(ctxt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send payments from Alice to Carol a number of numInvoices
|
|
|
|
// times.
|
|
|
|
carolPubKey := carol.PubKey[:]
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
err = alicePayStream.Send(&lnrpc.SendRequest{
|
|
|
|
Dest: carolPubKey,
|
|
|
|
Amt: int64(payAmt),
|
|
|
|
PaymentHash: makeFakePayHash(t),
|
2019-05-15 10:02:53 +03:00
|
|
|
FinalCltvDelta: lnd.DefaultBitcoinTimeLockDelta,
|
2018-12-07 03:49:05 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send alice htlc: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that the UnsettledBalance for both Alice and Carol
|
|
|
|
// is equal to the amount of invoices * payAmt.
|
|
|
|
var unsettledErr error
|
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, carol}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
// There should be a number of PendingHtlcs equal
|
|
|
|
// to the amount of Invoices sent.
|
|
|
|
unsettledErr = assertNumActiveHtlcs(nodes, numInvoices)
|
|
|
|
if unsettledErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the amount expected for the Unsettled Balance for
|
|
|
|
// this channel.
|
|
|
|
expectedBalance := numInvoices * payAmt
|
|
|
|
|
|
|
|
// Check each nodes UnsettledBalance field.
|
|
|
|
for _, node := range nodes {
|
|
|
|
// Get channel info for the node.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
chanInfo, err := getChanInfo(ctxt, node)
|
|
|
|
if err != nil {
|
|
|
|
unsettledErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that UnsettledBalance is what we expect.
|
|
|
|
if int(chanInfo.UnsettledBalance) != expectedBalance {
|
|
|
|
unsettledErr = fmt.Errorf("unsettled balance failed "+
|
|
|
|
"expected: %v, received: %v", expectedBalance,
|
|
|
|
chanInfo.UnsettledBalance)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, defaultTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unsettled balace error: %v", unsettledErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force and assert the channel closure.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, true)
|
|
|
|
|
|
|
|
// Cleanup by mining the force close and sweep transaction.
|
|
|
|
cleanupForceClose(t, net, net.Alice, chanPointAlice)
|
|
|
|
}
|
|
|
|
|
2017-10-07 02:58:13 +03:00
|
|
|
// findForceClosedChannel searches a pending channel response for a particular
|
|
|
|
// channel, returning the force closed channel upon success.
|
2018-05-23 20:54:12 +03:00
|
|
|
func findForceClosedChannel(pendingChanResp *lnrpc.PendingChannelsResponse,
|
|
|
|
op *wire.OutPoint) (*lnrpc.PendingChannelsResponse_ForceClosedChannel, error) {
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
for _, forceClose := range pendingChanResp.PendingForceClosingChannels {
|
2017-10-07 02:58:13 +03:00
|
|
|
if forceClose.Channel.ChannelPoint == op.String() {
|
2018-05-23 20:54:12 +03:00
|
|
|
return forceClose, nil
|
2017-10-07 02:58:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
return nil, errors.New("channel not marked as force closed")
|
2017-10-07 02:58:13 +03:00
|
|
|
}
|
|
|
|
|
2018-04-13 14:20:12 +03:00
|
|
|
// findWaitingCloseChannel searches a pending channel response for a particular
|
|
|
|
// channel, returning the waiting close channel upon success.
|
2018-05-23 20:54:12 +03:00
|
|
|
func findWaitingCloseChannel(pendingChanResp *lnrpc.PendingChannelsResponse,
|
|
|
|
op *wire.OutPoint) (*lnrpc.PendingChannelsResponse_WaitingCloseChannel, error) {
|
2018-04-13 14:20:12 +03:00
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
for _, waitingClose := range pendingChanResp.WaitingCloseChannels {
|
2018-04-13 14:20:12 +03:00
|
|
|
if waitingClose.Channel.ChannelPoint == op.String() {
|
2018-05-23 20:54:12 +03:00
|
|
|
return waitingClose, nil
|
2018-04-13 14:20:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
return nil, errors.New("channel not marked as waiting close")
|
2018-04-13 14:20:12 +03:00
|
|
|
}
|
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
func checkCommitmentMaturity(
|
2018-01-04 23:20:25 +03:00
|
|
|
forceClose *lnrpc.PendingChannelsResponse_ForceClosedChannel,
|
2018-05-23 20:54:12 +03:00
|
|
|
maturityHeight uint32, blocksTilMaturity int32) error {
|
2017-10-07 02:58:13 +03:00
|
|
|
|
|
|
|
if forceClose.MaturityHeight != maturityHeight {
|
2018-05-23 20:54:12 +03:00
|
|
|
return fmt.Errorf("expected commitment maturity height to be "+
|
|
|
|
"%d, found %d instead", maturityHeight,
|
2017-10-07 02:58:13 +03:00
|
|
|
forceClose.MaturityHeight)
|
|
|
|
}
|
|
|
|
if forceClose.BlocksTilMaturity != blocksTilMaturity {
|
2018-05-23 20:54:12 +03:00
|
|
|
return fmt.Errorf("expected commitment blocks til maturity to "+
|
|
|
|
"be %d, found %d instead", blocksTilMaturity,
|
2017-10-07 02:58:13 +03:00
|
|
|
forceClose.BlocksTilMaturity)
|
|
|
|
}
|
2018-05-23 20:54:12 +03:00
|
|
|
|
|
|
|
return nil
|
2017-10-07 02:58:13 +03:00
|
|
|
}
|
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
// checkForceClosedChannelNumHtlcs verifies that a force closed channel has the
|
2017-10-07 02:58:13 +03:00
|
|
|
// proper number of htlcs.
|
2018-05-23 20:54:12 +03:00
|
|
|
func checkPendingChannelNumHtlcs(
|
2018-01-04 23:20:25 +03:00
|
|
|
forceClose *lnrpc.PendingChannelsResponse_ForceClosedChannel,
|
2018-05-23 20:54:12 +03:00
|
|
|
expectedNumHtlcs int) error {
|
2017-10-07 02:58:13 +03:00
|
|
|
|
|
|
|
if len(forceClose.PendingHtlcs) != expectedNumHtlcs {
|
2018-05-23 20:54:12 +03:00
|
|
|
return fmt.Errorf("expected force closed channel to have %d "+
|
|
|
|
"pending htlcs, found %d instead", expectedNumHtlcs,
|
2017-10-07 02:58:13 +03:00
|
|
|
len(forceClose.PendingHtlcs))
|
|
|
|
}
|
2018-05-23 20:54:12 +03:00
|
|
|
|
|
|
|
return nil
|
2017-10-07 02:58:13 +03:00
|
|
|
}
|
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
// checkNumForceClosedChannels checks that a pending channel response has the
|
2017-10-07 02:58:13 +03:00
|
|
|
// expected number of force closed channels.
|
2018-05-23 20:54:12 +03:00
|
|
|
func checkNumForceClosedChannels(pendingChanResp *lnrpc.PendingChannelsResponse,
|
|
|
|
expectedNumChans int) error {
|
2017-10-07 02:58:13 +03:00
|
|
|
|
|
|
|
if len(pendingChanResp.PendingForceClosingChannels) != expectedNumChans {
|
2018-05-23 20:54:12 +03:00
|
|
|
return fmt.Errorf("expected to find %d force closed channels, "+
|
|
|
|
"got %d", expectedNumChans,
|
2017-10-07 02:58:13 +03:00
|
|
|
len(pendingChanResp.PendingForceClosingChannels))
|
|
|
|
}
|
2018-05-23 20:54:12 +03:00
|
|
|
|
|
|
|
return nil
|
2017-10-07 02:58:13 +03:00
|
|
|
}
|
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
// checkNumWaitingCloseChannels checks that a pending channel response has the
|
2018-04-13 14:20:12 +03:00
|
|
|
// expected number of channels waiting for closing tx to confirm.
|
2018-05-23 20:54:12 +03:00
|
|
|
func checkNumWaitingCloseChannels(pendingChanResp *lnrpc.PendingChannelsResponse,
|
|
|
|
expectedNumChans int) error {
|
2018-04-13 14:20:12 +03:00
|
|
|
|
|
|
|
if len(pendingChanResp.WaitingCloseChannels) != expectedNumChans {
|
2018-05-23 20:54:12 +03:00
|
|
|
return fmt.Errorf("expected to find %d channels waiting "+
|
|
|
|
"closure, got %d", expectedNumChans,
|
2018-04-13 14:20:12 +03:00
|
|
|
len(pendingChanResp.WaitingCloseChannels))
|
|
|
|
}
|
2018-05-23 20:54:12 +03:00
|
|
|
|
|
|
|
return nil
|
2018-04-13 14:20:12 +03:00
|
|
|
}
|
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
// checkPendingHtlcStageAndMaturity uniformly tests all pending htlc's belonging
|
|
|
|
// to a force closed channel, testing for the expected stage number, blocks till
|
|
|
|
// maturity, and the maturity height.
|
|
|
|
func checkPendingHtlcStageAndMaturity(
|
2018-01-04 23:20:25 +03:00
|
|
|
forceClose *lnrpc.PendingChannelsResponse_ForceClosedChannel,
|
2018-05-23 20:54:12 +03:00
|
|
|
stage, maturityHeight uint32, blocksTillMaturity int32) error {
|
2017-10-07 02:58:13 +03:00
|
|
|
|
|
|
|
for _, pendingHtlc := range forceClose.PendingHtlcs {
|
|
|
|
if pendingHtlc.Stage != stage {
|
2018-05-23 20:54:12 +03:00
|
|
|
return fmt.Errorf("expected pending htlc to be stage "+
|
|
|
|
"%d, found %d", stage, pendingHtlc.Stage)
|
2017-10-07 02:58:13 +03:00
|
|
|
}
|
|
|
|
if pendingHtlc.MaturityHeight != maturityHeight {
|
2018-05-23 20:54:12 +03:00
|
|
|
return fmt.Errorf("expected pending htlc maturity "+
|
|
|
|
"height to be %d, instead has %d",
|
|
|
|
maturityHeight, pendingHtlc.MaturityHeight)
|
2017-10-07 02:58:13 +03:00
|
|
|
}
|
|
|
|
if pendingHtlc.BlocksTilMaturity != blocksTillMaturity {
|
2018-05-23 20:54:12 +03:00
|
|
|
return fmt.Errorf("expected pending htlc blocks til "+
|
|
|
|
"maturity to be %d, instead has %d",
|
|
|
|
blocksTillMaturity,
|
2017-10-07 02:58:13 +03:00
|
|
|
pendingHtlc.BlocksTilMaturity)
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 20:54:12 +03:00
|
|
|
|
|
|
|
return nil
|
2017-10-07 02:58:13 +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
|
2017-10-07 02:58:13 +03:00
|
|
|
// state on-chain. The test creates a new channel between Alice and Carol, then
|
|
|
|
// force closes the channel after some cursory assertions. Within the test, a
|
|
|
|
// total of 3 + n transactions will be broadcast, representing the commitment
|
|
|
|
// transaction, a transaction sweeping the local CSV delayed output, a
|
|
|
|
// transaction sweeping the CSV delayed 2nd-layer htlcs outputs, and n
|
|
|
|
// htlc success transactions, where n is the number of payments Alice attempted
|
|
|
|
// to send to Carol. This test 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.
|
2017-11-03 21:52:02 +03:00
|
|
|
func testChannelForceClosure(net *lntest.NetworkHarness, t *harnessTest) {
|
2016-09-14 05:00:47 +03:00
|
|
|
ctxb := context.Background()
|
2018-11-28 11:25:46 +03:00
|
|
|
|
2017-10-07 02:58:13 +03:00
|
|
|
const (
|
|
|
|
chanAmt = btcutil.Amount(10e6)
|
|
|
|
pushAmt = btcutil.Amount(5e6)
|
|
|
|
paymentAmt = 100000
|
|
|
|
numInvoices = 6
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO(roasbeef): should check default value in config here
|
|
|
|
// instead, or make delay a param
|
2019-05-15 10:02:53 +03:00
|
|
|
defaultCLTV := uint32(lnd.DefaultBitcoinTimeLockDelta)
|
2017-10-07 02:58:13 +03:00
|
|
|
|
|
|
|
// Since we'd like to test failure scenarios with outstanding htlcs,
|
|
|
|
// we'll introduce another node into our test network: Carol.
|
2018-04-27 00:08:24 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--debughtlc", "--hodl.exit-settle"})
|
2017-10-07 02:58:13 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:22:22 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
2016-09-14 05:00:47 +03:00
|
|
|
|
2017-10-07 02:58:13 +03:00
|
|
|
// We must let Alice have an open channel before she can send a node
|
|
|
|
// announcement, so we open a channel with Carol,
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Alice, carol); err != nil {
|
2017-10-07 02:58:13 +03:00
|
|
|
t.Fatalf("unable to connect alice to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we start, obtain Carol's current wallet balance, we'll check
|
|
|
|
// to ensure that at the end of the force closure by Alice, Carol
|
|
|
|
// recognizes his new on-chain output.
|
|
|
|
carolBalReq := &lnrpc.WalletBalanceRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
carolBalResp, err := carol.WalletBalance(ctxt, carolBalReq)
|
2017-02-08 07:28:50 +03:00
|
|
|
if err != nil {
|
2017-10-07 02:58:13 +03:00
|
|
|
t.Fatalf("unable to get carol's balance: %v", err)
|
2017-02-08 07:28:50 +03:00
|
|
|
}
|
|
|
|
|
2018-03-26 05:16:39 +03:00
|
|
|
carolStartingBalance := carolBalResp.ConfirmedBalance
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-01-23 05:58:13 +03:00
|
|
|
// Wait for Alice and Carol to receive the channel edge from the
|
|
|
|
// funding manager.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-10-07 02:58:13 +03:00
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
2016-09-14 05:00:47 +03:00
|
|
|
if err != nil {
|
2017-10-07 02:58:13 +03:00
|
|
|
t.Fatalf("alice didn't see the alice->carol channel before "+
|
|
|
|
"timeout: %v", err)
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
2018-01-23 05:58:13 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't see the alice->carol channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2016-10-15 16:47:09 +03:00
|
|
|
|
2018-01-23 05:58:13 +03:00
|
|
|
// Send payments from Alice to Carol, since Carol is htlchodl mode, the
|
|
|
|
// htlc outputs should be left unsettled, and should be swept by the
|
|
|
|
// utxo nursery.
|
2018-11-28 11:15:58 +03:00
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
alicePayStream, err := net.Alice.SendPayment(ctx)
|
2018-01-23 05:58:13 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for alice: %v", err)
|
|
|
|
}
|
2018-08-12 16:23:08 +03:00
|
|
|
|
2018-01-23 05:58:13 +03:00
|
|
|
carolPubKey := carol.PubKey[:]
|
2017-10-07 02:58:13 +03:00
|
|
|
for i := 0; i < numInvoices; i++ {
|
2018-01-23 05:58:13 +03:00
|
|
|
err = alicePayStream.Send(&lnrpc.SendRequest{
|
|
|
|
Dest: carolPubKey,
|
|
|
|
Amt: int64(paymentAmt),
|
2018-08-12 16:23:08 +03:00
|
|
|
PaymentHash: makeFakePayHash(t),
|
2019-05-15 10:02:53 +03:00
|
|
|
FinalCltvDelta: lnd.DefaultBitcoinTimeLockDelta,
|
2018-01-23 05:58:13 +03:00
|
|
|
})
|
2017-10-07 02:58:13 +03:00
|
|
|
if err != nil {
|
2018-01-23 05:58:13 +03:00
|
|
|
t.Fatalf("unable to send alice htlc: %v", err)
|
2017-10-07 02:58:13 +03:00
|
|
|
}
|
2018-01-23 05:58:13 +03:00
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-01-23 05:58:13 +03:00
|
|
|
// Once the HTLC has cleared, all the nodes n our mini network should
|
|
|
|
// show that the HTLC has been locked in.
|
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, carol}
|
2018-08-14 10:09:02 +03:00
|
|
|
var predErr error
|
2018-01-23 05:58:13 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, numInvoices)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-01-23 07:37:22 +03:00
|
|
|
}, time.Second*15)
|
2018-01-23 05:58:13 +03:00
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
2016-10-15 16:47:09 +03:00
|
|
|
|
2017-10-07 02:58:13 +03:00
|
|
|
// Fetch starting height of this test so we can compute the block
|
|
|
|
// heights we expect certain events to take place.
|
|
|
|
_, curHeight, err := net.Miner.Node.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get best block height")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using the current height of the chain, derive the relevant heights
|
|
|
|
// for incubating two-stage htlcs.
|
|
|
|
var (
|
|
|
|
startHeight = uint32(curHeight)
|
|
|
|
commCsvMaturityHeight = startHeight + 1 + defaultCSV
|
|
|
|
htlcExpiryHeight = startHeight + defaultCLTV
|
|
|
|
htlcCsvMaturityHeight = startHeight + defaultCLTV + 1 + defaultCSV
|
|
|
|
)
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
aliceChan, err := getChanInfo(ctxt, net.Alice)
|
2017-10-07 02:58:13 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get alice's channel info: %v", err)
|
|
|
|
}
|
|
|
|
if aliceChan.NumUpdates == 0 {
|
|
|
|
t.Fatalf("alice should see at least one update to her channel")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the channel is open and we have unsettled htlcs, immediately
|
|
|
|
// execute a force closure of the channel. This will also assert that
|
|
|
|
// the commitment transaction was immediately broadcast in order to
|
|
|
|
// fulfill the force closure request.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
|
|
|
_, closingTxID, err := net.CloseChannel(ctxt, 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
|
2018-04-13 14:20:12 +03:00
|
|
|
// PendingChannels RPC under the waiting close section.
|
2018-01-04 23:20:25 +03:00
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
pendingChanResp, err := net.Alice.PendingChannels(ctxt, pendingChansRequest)
|
2017-05-05 03:38:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for pending channels: %v", err)
|
|
|
|
}
|
2018-05-23 20:54:12 +03:00
|
|
|
err = checkNumWaitingCloseChannels(pendingChanResp, 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
|
|
|
// Compute the outpoint of the channel, which we will use repeatedly to
|
|
|
|
// locate the pending channel information in the rpc responses.
|
2019-05-15 10:02:53 +03:00
|
|
|
txid, err := lnd.GetChanPointFundingTxid(chanPoint)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2017-05-05 03:38:16 +03:00
|
|
|
op := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
waitingClose, err := findWaitingCloseChannel(pendingChanResp, &op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-04-13 14:20:12 +03:00
|
|
|
// Immediately after force closing, all of the funds should be in limbo.
|
|
|
|
if waitingClose.LimboBalance == 0 {
|
2017-10-07 02:58:13 +03:00
|
|
|
t.Fatalf("all funds should still be in limbo")
|
2017-05-05 03:38:16 +03:00
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
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.
|
2018-11-29 14:31:40 +03:00
|
|
|
_, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to find commitment in miner mempool: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-09-14 05:00:47 +03:00
|
|
|
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
|
|
|
|
2018-04-13 14:20:12 +03:00
|
|
|
// Now that the commitment has been confirmed, the channel should be
|
|
|
|
// marked as force closed.
|
2018-05-23 20:54:12 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2018-05-23 20:54:12 +03:00
|
|
|
pendingChanResp, err := net.Alice.PendingChannels(
|
2018-08-30 04:00:54 +03:00
|
|
|
ctxt, pendingChansRequest,
|
2018-05-23 20:54:12 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
predErr = checkNumForceClosedChannels(pendingChanResp, 1)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
forceClose, predErr := findForceClosedChannel(
|
|
|
|
pendingChanResp, &op,
|
|
|
|
)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
// Now that the channel has been force closed, it should now
|
|
|
|
// have the height and number of blocks to confirm populated.
|
|
|
|
predErr = checkCommitmentMaturity(
|
|
|
|
forceClose, commCsvMaturityHeight, int32(defaultCSV),
|
|
|
|
)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// None of our outputs have been swept, so they should all be in
|
|
|
|
// limbo.
|
|
|
|
if forceClose.LimboBalance == 0 {
|
|
|
|
predErr = errors.New("all funds should still be in " +
|
|
|
|
"limbo")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if forceClose.RecoveredBalance != 0 {
|
|
|
|
predErr = errors.New("no funds should yet be shown " +
|
|
|
|
"as recovered")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, 15*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(predErr.Error())
|
2017-05-05 03:38:16 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-29 14:31:40 +03:00
|
|
|
// Carol's sweep tx should be in the mempool already, as her output is
|
|
|
|
// not timelocked.
|
|
|
|
_, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to find Carol's sweep in miner mempool: %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)
|
|
|
|
}
|
|
|
|
|
2017-10-07 02:58:13 +03:00
|
|
|
// 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.
|
2016-12-14 02:32:44 +03:00
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
// Alice should see the channel in her set of pending force closed
|
|
|
|
// channels with her funds still in limbo.
|
2019-01-22 15:38:05 +03:00
|
|
|
err = lntest.WaitNoError(func() error {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2018-05-23 20:54:12 +03:00
|
|
|
pendingChanResp, err := net.Alice.PendingChannels(
|
2018-08-30 04:00:54 +03:00
|
|
|
ctxt, pendingChansRequest,
|
2018-05-23 20:54:12 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
2019-01-22 15:38:05 +03:00
|
|
|
return fmt.Errorf("unable to query for pending "+
|
2018-05-23 20:54:12 +03:00
|
|
|
"channels: %v", err)
|
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2019-01-22 15:38:05 +03:00
|
|
|
err = checkNumForceClosedChannels(pendingChanResp, 1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-05-23 20:54:12 +03:00
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2019-01-22 15:38:05 +03:00
|
|
|
forceClose, err := findForceClosedChannel(
|
2018-05-23 20:54:12 +03:00
|
|
|
pendingChanResp, &op,
|
|
|
|
)
|
2019-01-22 15:38:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-05-23 20:54:12 +03:00
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
// At this point, the nursery should show that the commitment
|
|
|
|
// output has 1 block left before its CSV delay expires. In
|
|
|
|
// total, we have mined exactly defaultCSV blocks, so the htlc
|
|
|
|
// outputs should also reflect that this many blocks have
|
|
|
|
// passed.
|
2019-01-22 15:38:05 +03:00
|
|
|
err = checkCommitmentMaturity(
|
2018-05-23 20:54:12 +03:00
|
|
|
forceClose, commCsvMaturityHeight, 1,
|
|
|
|
)
|
2019-01-22 15:38:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-05-23 20:54:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// All funds should still be shown in limbo.
|
|
|
|
if forceClose.LimboBalance == 0 {
|
2019-01-22 15:38:05 +03:00
|
|
|
return errors.New("all funds should still be in " +
|
2018-05-23 20:54:12 +03:00
|
|
|
"limbo")
|
|
|
|
}
|
|
|
|
if forceClose.RecoveredBalance != 0 {
|
2019-01-22 15:38:05 +03:00
|
|
|
return errors.New("no funds should yet be shown " +
|
2018-05-23 20:54:12 +03:00
|
|
|
"as recovered")
|
|
|
|
}
|
|
|
|
|
2019-01-22 15:38:05 +03:00
|
|
|
return nil
|
2018-05-23 20:54:12 +03:00
|
|
|
}, 15*time.Second)
|
|
|
|
if err != nil {
|
2019-01-22 15:38:05 +03:00
|
|
|
t.Fatalf(err.Error())
|
2017-10-07 02:58:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate an additional block, which should cause the CSV delayed
|
|
|
|
// output from the commitment txn to expire.
|
2016-12-14 02:32:44 +03:00
|
|
|
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.
|
2018-11-27 11:12:16 +03:00
|
|
|
sweepingTXID, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2017-10-07 02:58:13 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get sweep tx from mempool: %v", err)
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 02:58:13 +03:00
|
|
|
// Restart Alice to ensure that she resumes watching the finalized
|
|
|
|
// commitment sweep txid.
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we mine an additional block which should include the sweep
|
2016-09-14 05:00:47 +03:00
|
|
|
// 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
|
|
|
|
2018-09-07 17:05:57 +03:00
|
|
|
// Update current height
|
|
|
|
_, curHeight, err = net.Miner.Node.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get best block height")
|
|
|
|
}
|
|
|
|
|
2018-08-30 04:00:54 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
// Now that the commit output has been fully swept, check to see
|
|
|
|
// that the channel remains open for the pending htlc outputs.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2018-08-30 04:00:54 +03:00
|
|
|
pendingChanResp, err := net.Alice.PendingChannels(
|
|
|
|
ctxt, pendingChansRequest,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2018-05-23 20:54:12 +03:00
|
|
|
|
2018-08-30 04:00:54 +03:00
|
|
|
err = checkNumForceClosedChannels(pendingChanResp, 1)
|
|
|
|
if err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-08-30 04:00:54 +03:00
|
|
|
// The commitment funds will have been recovered after the
|
|
|
|
// commit txn was included in the last block. The htlc funds
|
2018-09-07 17:05:57 +03:00
|
|
|
// will be shown in limbo.
|
2018-08-30 04:00:54 +03:00
|
|
|
forceClose, err := findForceClosedChannel(pendingChanResp, &op)
|
|
|
|
if err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
|
|
|
}
|
2018-09-07 17:05:57 +03:00
|
|
|
predErr = checkPendingChannelNumHtlcs(forceClose, numInvoices)
|
2018-08-30 04:00:54 +03:00
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
2018-09-07 17:05:57 +03:00
|
|
|
predErr = checkPendingHtlcStageAndMaturity(
|
|
|
|
forceClose, 1, htlcExpiryHeight,
|
|
|
|
int32(htlcExpiryHeight)-curHeight,
|
|
|
|
)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if forceClose.LimboBalance == 0 {
|
|
|
|
predErr = fmt.Errorf("expected funds in limbo, found 0")
|
2018-08-30 04:00:54 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, 15*time.Second)
|
2018-08-27 05:55:38 +03:00
|
|
|
if err != nil {
|
2018-08-30 04:00:54 +03:00
|
|
|
t.Fatalf(predErr.Error())
|
2017-10-07 02:58:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the height preceding that which will cause the htlc CLTV
|
|
|
|
// timeouts will expire. The outputs entered at the same height as the
|
|
|
|
// output spending from the commitment txn, so we must deduct the number
|
|
|
|
// of blocks we have generated since adding it to the nursery, and take
|
|
|
|
// an additional block off so that we end up one block shy of the expiry
|
|
|
|
// height.
|
|
|
|
cltvHeightDelta := defaultCLTV - defaultCSV - 2 - 1
|
|
|
|
|
|
|
|
// Advance the blockchain until just before the CLTV expires, nothing
|
|
|
|
// exciting should have happened during this time.
|
|
|
|
blockHash, err = net.Miner.Node.Generate(cltvHeightDelta)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We now restart Alice, to ensure that she will broadcast the presigned
|
2018-04-18 05:02:04 +03:00
|
|
|
// htlc timeout txns after the delay expires after experiencing a while
|
2017-10-07 02:58:13 +03:00
|
|
|
// waiting for the htlc outputs to incubate.
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
// Alice should now see the channel in her set of pending force closed
|
|
|
|
// channels with one pending HTLC.
|
2019-01-22 15:38:05 +03:00
|
|
|
err = lntest.WaitNoError(func() error {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2018-08-30 04:00:54 +03:00
|
|
|
pendingChanResp, err := net.Alice.PendingChannels(
|
|
|
|
ctxt, pendingChansRequest,
|
2018-05-23 20:54:12 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
2019-01-22 15:38:05 +03:00
|
|
|
return fmt.Errorf("unable to query for pending "+
|
2018-05-23 20:54:12 +03:00
|
|
|
"channels: %v", err)
|
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2019-01-22 15:38:05 +03:00
|
|
|
err = checkNumForceClosedChannels(pendingChanResp, 1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-05-23 20:54:12 +03:00
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2019-01-22 15:38:05 +03:00
|
|
|
forceClose, err := findForceClosedChannel(
|
2018-05-23 20:54:12 +03:00
|
|
|
pendingChanResp, &op,
|
|
|
|
)
|
2019-01-22 15:38:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-05-23 20:54:12 +03:00
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
// We should now be at the block just before the utxo nursery
|
|
|
|
// will attempt to broadcast the htlc timeout transactions.
|
2019-01-22 15:38:05 +03:00
|
|
|
err = checkPendingChannelNumHtlcs(forceClose, numInvoices)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-05-23 20:54:12 +03:00
|
|
|
}
|
2019-01-22 15:38:05 +03:00
|
|
|
err = checkPendingHtlcStageAndMaturity(
|
2018-05-23 20:54:12 +03:00
|
|
|
forceClose, 1, htlcExpiryHeight, 1,
|
|
|
|
)
|
2019-01-22 15:38:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-05-23 20:54:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that our commitment confirmation depth has been
|
|
|
|
// surpassed, we should now see a non-zero recovered balance.
|
|
|
|
// All htlc outputs are still left in limbo, so it should be
|
|
|
|
// non-zero as well.
|
|
|
|
if forceClose.LimboBalance == 0 {
|
2019-01-22 15:38:05 +03:00
|
|
|
return errors.New("htlc funds should still be in " +
|
2018-05-23 20:54:12 +03:00
|
|
|
"limbo")
|
|
|
|
}
|
|
|
|
|
2019-01-22 15:38:05 +03:00
|
|
|
return nil
|
2018-05-23 20:54:12 +03:00
|
|
|
}, 15*time.Second)
|
|
|
|
if err != nil {
|
2019-01-22 15:38:05 +03:00
|
|
|
t.Fatalf(err.Error())
|
2017-10-07 02:58:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now, generate the block which will cause Alice to broadcast the
|
|
|
|
// presigned htlc timeout txns.
|
|
|
|
blockHash, err = net.Miner.Node.Generate(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since Alice had numInvoices (6) htlcs extended to Carol before force
|
|
|
|
// closing, we expect Alice to broadcast an htlc timeout txn for each
|
|
|
|
// one. Wait for them all to show up in the mempool.
|
|
|
|
htlcTxIDs, err := waitForNTxsInMempool(net.Miner.Node, numInvoices,
|
2018-11-27 11:12:16 +03:00
|
|
|
minerMempoolTimeout)
|
2017-10-07 02:58:13 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find htlc timeout txns in mempool: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve each htlc timeout txn from the mempool, and ensure it is
|
|
|
|
// well-formed. This entails verifying that each only spends from
|
|
|
|
// output, and that that output is from the commitment txn.
|
|
|
|
for _, htlcTxID := range htlcTxIDs {
|
|
|
|
// Fetch the sweep transaction, all input it's spending should
|
|
|
|
// be from the commitment transaction which was broadcast
|
|
|
|
// on-chain.
|
|
|
|
htlcTx, err := net.Miner.Node.GetRawTransaction(htlcTxID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch sweep tx: %v", err)
|
|
|
|
}
|
|
|
|
// Ensure the htlc transaction only has one input.
|
|
|
|
if len(htlcTx.MsgTx().TxIn) != 1 {
|
|
|
|
t.Fatalf("htlc transaction should only have one txin, "+
|
|
|
|
"has %d", len(htlcTx.MsgTx().TxIn))
|
|
|
|
}
|
|
|
|
// Ensure the htlc transaction is spending from the commitment
|
|
|
|
// transaction.
|
|
|
|
txIn := htlcTx.MsgTx().TxIn[0]
|
|
|
|
if !closingTxID.IsEqual(&txIn.PreviousOutPoint.Hash) {
|
|
|
|
t.Fatalf("htlc transaction not spending from commit "+
|
|
|
|
"tx %v, instead spending %v",
|
|
|
|
closingTxID, txIn.PreviousOutPoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the htlc timeout txns still in the mempool, we restart Alice to
|
|
|
|
// verify that she can resume watching the htlc txns she broadcasted
|
|
|
|
// before crashing.
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a block that mines the htlc timeout txns. Doing so now
|
|
|
|
// activates the 2nd-stage CSV delayed outputs.
|
|
|
|
blockHash, err = net.Miner.Node.Generate(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice is restarted here to ensure that she promptly moved the crib
|
|
|
|
// outputs to the kindergarten bucket after the htlc timeout txns were
|
|
|
|
// confirmed.
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance the chain until just before the 2nd-layer CSV delays expire.
|
|
|
|
blockHash, err = net.Miner.Node.Generate(defaultCSV - 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restart Alice to ensure that she can recover from a failure before
|
|
|
|
// having graduated the htlc outputs in the kindergarten bucket.
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the channel has been fully swept, it should no longer show
|
|
|
|
// incubated, check to see that Alice's node still reports the channel
|
|
|
|
// as pending force closed.
|
2018-08-30 04:00:54 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2018-08-30 04:00:54 +03:00
|
|
|
pendingChanResp, err = net.Alice.PendingChannels(
|
|
|
|
ctxt, pendingChansRequest,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
err = checkNumForceClosedChannels(pendingChanResp, 1)
|
|
|
|
if err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-08-30 04:00:54 +03:00
|
|
|
forceClose, err := findForceClosedChannel(pendingChanResp, &op)
|
|
|
|
if err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-08-30 04:00:54 +03:00
|
|
|
if forceClose.LimboBalance == 0 {
|
|
|
|
predErr = fmt.Errorf("htlc funds should still be in limbo")
|
|
|
|
return false
|
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-08-30 04:00:54 +03:00
|
|
|
predErr = checkPendingChannelNumHtlcs(forceClose, numInvoices)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, 15*time.Second)
|
2018-05-23 20:54:12 +03:00
|
|
|
if err != nil {
|
2018-08-30 04:00:54 +03:00
|
|
|
t.Fatalf(predErr.Error())
|
2018-05-23 20:54:12 +03:00
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
|
|
|
// Generate a block that causes Alice to sweep the htlc outputs in the
|
|
|
|
// kindergarten bucket.
|
|
|
|
blockHash, err = net.Miner.Node.Generate(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the single sweep txn to appear in the mempool.
|
2019-02-06 23:47:40 +03:00
|
|
|
htlcSweepTxID, err := waitForTxInMempool(
|
|
|
|
net.Miner.Node, minerMempoolTimeout,
|
|
|
|
)
|
2017-10-07 02:58:13 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get sweep tx from mempool: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a map of the already confirmed htlc timeout txids, that
|
|
|
|
// will count the number of times each is spent by the sweep txn. We
|
|
|
|
// prepopulate it in this way so that we can later detect if we are
|
|
|
|
// spending from an output that was not a confirmed htlc timeout txn.
|
|
|
|
var htlcTxIDSet = make(map[chainhash.Hash]int)
|
|
|
|
for _, htlcTxID := range htlcTxIDs {
|
|
|
|
htlcTxIDSet[*htlcTxID] = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the htlc sweep transaction from the mempool.
|
|
|
|
htlcSweepTx, err := net.Miner.Node.GetRawTransaction(htlcSweepTxID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch sweep tx: %v", err)
|
|
|
|
}
|
|
|
|
// Ensure the htlc sweep transaction only has one input for each htlc
|
|
|
|
// Alice extended before force closing.
|
|
|
|
if len(htlcSweepTx.MsgTx().TxIn) != numInvoices {
|
|
|
|
t.Fatalf("htlc transaction should have %d txin, "+
|
|
|
|
"has %d", numInvoices, len(htlcSweepTx.MsgTx().TxIn))
|
|
|
|
}
|
|
|
|
// Ensure that each output spends from exactly one htlc timeout txn.
|
|
|
|
for _, txIn := range htlcSweepTx.MsgTx().TxIn {
|
|
|
|
outpoint := txIn.PreviousOutPoint.Hash
|
|
|
|
// Check that the input is a confirmed htlc timeout txn.
|
|
|
|
if _, ok := htlcTxIDSet[outpoint]; !ok {
|
|
|
|
t.Fatalf("htlc sweep output not spending from htlc "+
|
|
|
|
"tx, instead spending output %v", outpoint)
|
|
|
|
}
|
|
|
|
// Increment our count for how many times this output was spent.
|
|
|
|
htlcTxIDSet[outpoint]++
|
|
|
|
|
|
|
|
// Check that each is only spent once.
|
|
|
|
if htlcTxIDSet[outpoint] > 1 {
|
|
|
|
t.Fatalf("htlc sweep tx has multiple spends from "+
|
|
|
|
"outpoint %v", outpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following restart checks to ensure that the nursery store is
|
|
|
|
// storing the txid of the previously broadcast htlc sweep txn, and that
|
|
|
|
// it begins watching that txid after restarting.
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the channel has been fully swept, it should no longer show
|
|
|
|
// incubated, check to see that Alice's node still reports the channel
|
|
|
|
// as pending force closed.
|
2018-08-30 04:00:54 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2018-08-30 04:00:54 +03:00
|
|
|
pendingChanResp, err := net.Alice.PendingChannels(
|
|
|
|
ctxt, pendingChansRequest,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
err = checkNumForceClosedChannels(pendingChanResp, 1)
|
|
|
|
if err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-08-30 04:00:54 +03:00
|
|
|
// All htlcs should show zero blocks until maturity, as
|
|
|
|
// evidenced by having checked the sweep transaction in the
|
|
|
|
// mempool.
|
|
|
|
forceClose, err := findForceClosedChannel(pendingChanResp, &op)
|
|
|
|
if err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
predErr = checkPendingChannelNumHtlcs(forceClose, numInvoices)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
err = checkPendingHtlcStageAndMaturity(
|
|
|
|
forceClose, 2, htlcCsvMaturityHeight, 0,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, 15*time.Second)
|
2018-05-23 20:54:12 +03:00
|
|
|
if err != nil {
|
2018-08-30 04:00:54 +03:00
|
|
|
t.Fatalf(predErr.Error())
|
2018-05-23 20:54:12 +03:00
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
|
|
|
// Generate the final block that sweeps all htlc funds into the user's
|
2019-02-06 23:47:40 +03:00
|
|
|
// wallet, and make sure the sweep is in this block.
|
|
|
|
block = mineBlocks(t, net, 1, 1)[0]
|
|
|
|
assertTxInBlock(t, block, htlcSweepTxID)
|
2017-10-07 02:58:13 +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.
|
2018-05-23 20:54:12 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2018-05-23 20:54:12 +03:00
|
|
|
pendingChanResp, err := net.Alice.PendingChannels(
|
2018-08-30 04:00:54 +03:00
|
|
|
ctxt, pendingChansRequest,
|
2018-05-23 20:54:12 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
predErr = checkNumForceClosedChannels(pendingChanResp, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
// In addition to there being no pending channels, we verify
|
|
|
|
// that pending channels does not report any money still in
|
|
|
|
// limbo.
|
|
|
|
if pendingChanResp.TotalLimboBalance != 0 {
|
|
|
|
predErr = errors.New("no user funds should be left " +
|
|
|
|
"in limbo after incubation")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, 15*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(predErr.Error())
|
2017-05-05 03:38:16 +03:00
|
|
|
}
|
|
|
|
|
2018-07-17 10:13:05 +03:00
|
|
|
// At this point, Bob should now be aware of his new immediately
|
2017-02-08 07:28:50 +03:00
|
|
|
// spendable on-chain balance, as it was Alice who broadcast the
|
|
|
|
// commitment transaction.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
carolBalResp, err = net.Bob.WalletBalance(ctxt, carolBalReq)
|
2017-02-08 07:28:50 +03:00
|
|
|
if err != nil {
|
2017-10-07 02:58:13 +03:00
|
|
|
t.Fatalf("unable to get carol's balance: %v", err)
|
2017-02-08 07:28:50 +03:00
|
|
|
}
|
2018-03-26 05:16:39 +03:00
|
|
|
carolExpectedBalance := btcutil.Amount(carolStartingBalance) + pushAmt
|
|
|
|
if btcutil.Amount(carolBalResp.ConfirmedBalance) < carolExpectedBalance {
|
2017-10-07 02:58:13 +03:00
|
|
|
t.Fatalf("carol's balance is incorrect: expected %v got %v",
|
|
|
|
carolExpectedBalance,
|
2018-03-26 05:16:39 +03:00
|
|
|
carolBalResp.ConfirmedBalance)
|
2017-02-08 07:28:50 +03:00
|
|
|
}
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
// testSphinxReplayPersistence verifies that replayed onion packets are rejected
|
|
|
|
// by a remote peer after a restart. We use a combination of unsafe
|
|
|
|
// configuration arguments to force Carol to replay the same sphinx packet after
|
|
|
|
// reconnecting to Dave, and compare the returned failure message with what we
|
|
|
|
// expect for replayed onion packets.
|
|
|
|
func testSphinxReplayPersistence(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Carol and Dave with Carol being
|
|
|
|
// the sole funder of the channel.
|
|
|
|
chanAmt := btcutil.Amount(100000)
|
|
|
|
|
|
|
|
// First, we'll create Dave, the receiver, and start him in hodl mode.
|
2018-04-27 00:08:24 +03:00
|
|
|
dave, err := net.NewNode("Dave", []string{"--debughtlc", "--hodl.exit-settle"})
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-06-08 00:14:39 +03:00
|
|
|
// We must remember to shutdown the nodes we created for the duration
|
|
|
|
// of the tests, only leaving the two seed nodes (Alice and Bob) within
|
|
|
|
// our test network.
|
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
// Next, we'll create Carol and establish a channel to from her to
|
|
|
|
// Dave. Carol is started in both unsafe-replay and unsafe-disconnect,
|
|
|
|
// which will cause her to replay any pending Adds held in memory upon
|
|
|
|
// reconnection.
|
2018-04-27 11:42:04 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--unsafe-replay"})
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, dave); err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("unable to connect carol to dave: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, carol, dave,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
|
|
|
|
assertAmountSent := func(amt btcutil.Amount) {
|
|
|
|
// Both channels should also have properly accounted from the
|
|
|
|
// amount that has been sent/received over the channel.
|
|
|
|
listReq := &lnrpc.ListChannelsRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
carolListChannels, err := carol.ListChannels(ctxt, listReq)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for alice's channel list: %v", err)
|
|
|
|
}
|
|
|
|
carolSatoshisSent := carolListChannels.Channels[0].TotalSatoshisSent
|
|
|
|
if carolSatoshisSent != int64(amt) {
|
|
|
|
t.Fatalf("Carol's satoshis sent is incorrect got %v, expected %v",
|
|
|
|
carolSatoshisSent, amt)
|
|
|
|
}
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
daveListChannels, err := dave.ListChannels(ctxt, listReq)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for Dave's channel list: %v", err)
|
|
|
|
}
|
|
|
|
daveSatoshisReceived := daveListChannels.Channels[0].TotalSatoshisReceived
|
|
|
|
if daveSatoshisReceived != int64(amt) {
|
|
|
|
t.Fatalf("Dave's satoshis received is incorrect got %v, expected %v",
|
|
|
|
daveSatoshisReceived, amt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the channel is open, create an invoice for Dave which
|
|
|
|
// expects a payment of 1000 satoshis from Carol paid via a particular
|
|
|
|
// preimage.
|
|
|
|
const paymentAmt = 1000
|
|
|
|
preimage := bytes.Repeat([]byte("A"), 32)
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
invoiceResp, err := dave.AddInvoice(ctxt, invoice)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Carol to recognize and advertise the new channel generated
|
|
|
|
// above.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
err = dave.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the invoice for Dave added, send a payment from Carol paying
|
|
|
|
// to the above generated invoice.
|
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
payStream, err := carol.SendPayment(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to open payment stream: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sendReq := &lnrpc.SendRequest{PaymentRequest: invoiceResp.PaymentRequest}
|
|
|
|
err = payStream.Send(sendReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
|
|
|
// Dave's invoice should not be marked as settled.
|
|
|
|
payHash := &lnrpc.PaymentHash{
|
|
|
|
RHash: invoiceResp.RHash,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
dbInvoice, err := dave.LookupInvoice(ctxt, payHash)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to lookup invoice: %v", err)
|
|
|
|
}
|
|
|
|
if dbInvoice.Settled {
|
|
|
|
t.Fatalf("dave's invoice should not be marked as settled: %v",
|
|
|
|
spew.Sdump(dbInvoice))
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the payment sent but hedl, all balance related stats should not
|
|
|
|
// have changed.
|
|
|
|
time.Sleep(time.Millisecond * 200)
|
|
|
|
assertAmountSent(0)
|
|
|
|
|
|
|
|
// With the first payment sent, restart dave to make sure he is
|
|
|
|
// persisting the information required to detect replayed sphinx
|
|
|
|
// packets.
|
|
|
|
if err := net.RestartNode(dave, nil); err != nil {
|
|
|
|
t.Fatalf("unable to restart dave: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Carol should retransmit the Add hedl in her mailbox on startup. Dave
|
|
|
|
// should not accept the replayed Add, and actually fail back the
|
|
|
|
// pending payment. Even though he still holds the original settle, if
|
|
|
|
// he does fail, it is almost certainly caused by the sphinx replay
|
|
|
|
// protection, as it is the only validation we do in hodl mode.
|
|
|
|
resp, err := payStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to receive payment response: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the response we expect after sending a duplicate packet
|
|
|
|
// that fails due to sphinx replay detection.
|
2019-05-01 04:30:24 +03:00
|
|
|
replayErr := "TemporaryChannelFailure"
|
|
|
|
if !strings.Contains(resp.PaymentError, replayErr) {
|
|
|
|
t.Fatalf("received payment error: %v, expected %v",
|
|
|
|
resp.PaymentError, replayErr)
|
2018-02-23 01:29:06 +03:00
|
|
|
}
|
|
|
|
|
2018-03-31 00:53:05 +03:00
|
|
|
// Since the payment failed, the balance should still be left
|
|
|
|
// unaltered.
|
2018-02-23 01:29:06 +03:00
|
|
|
assertAmountSent(0)
|
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-03-31 00:53:05 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPoint, true)
|
2018-11-06 13:03:01 +03:00
|
|
|
|
|
|
|
// Cleanup by mining the force close and sweep transaction.
|
|
|
|
cleanupForceClose(t, net, carol, chanPoint)
|
2018-02-23 01:29:06 +03:00
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
func testSingleHopInvoice(net *lntest.NetworkHarness, t *harnessTest) {
|
2016-09-26 20:40:58 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice being
|
|
|
|
// the sole funder of the channel.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2016-09-26 20:40:58 +03:00
|
|
|
chanAmt := btcutil.Amount(100000)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
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{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
aliceListChannels, err := net.Alice.ListChannels(ctxt, listReq)
|
2017-01-07 23:46:10 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
bobListChannels, err := net.Bob.ListChannels(ctxt, listReq)
|
2017-01-07 23:46:10 +03:00
|
|
|
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,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
2016-09-26 20:40:58 +03:00
|
|
|
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.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-03-15 06:06:33 +03:00
|
|
|
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.
|
|
|
|
sendReq := &lnrpc.SendRequest{
|
2017-10-23 04:35:38 +03:00
|
|
|
PaymentRequest: invoiceResp.PaymentRequest,
|
2016-09-26 20:40:58 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-10-26 01:32:00 +03:00
|
|
|
resp, err := net.Alice.SendPaymentSync(ctxt, sendReq)
|
|
|
|
if 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-10-26 01:32:00 +03:00
|
|
|
if resp.PaymentError != "" {
|
2017-10-23 04:35:38 +03:00
|
|
|
t.Fatalf("error when attempting recv: %v", resp.PaymentError)
|
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,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
|
|
|
dbInvoice, err := net.Bob.LookupInvoice(ctxt, payHash)
|
2016-09-26 20:40:58 +03:00
|
|
|
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
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
|
|
|
invoiceResp, err = net.Bob.AddInvoice(ctxt, 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.
|
2017-10-26 01:32:00 +03:00
|
|
|
sendReq = &lnrpc.SendRequest{
|
2017-01-07 23:46:10 +03:00
|
|
|
PaymentRequest: invoiceResp.PaymentRequest,
|
2017-10-26 01:32:00 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-10-26 01:32:00 +03:00
|
|
|
resp, err = net.Alice.SendPaymentSync(ctxt, sendReq)
|
|
|
|
if err != nil {
|
2017-01-07 23:46:10 +03:00
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
2016-11-19 03:20:44 +03:00
|
|
|
}
|
2017-10-26 01:32:00 +03:00
|
|
|
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
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
2016-09-26 20:40:58 +03:00
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
func testListPayments(net *lntest.NetworkHarness, t *harnessTest) {
|
2016-12-05 14:59:36 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
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{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if _, err := net.Alice.DeleteAllPayments(ctxt, delPaymentsReq); err != nil {
|
2016-12-31 03:41:59 +03:00
|
|
|
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{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
|
|
|
paymentsRespInit, err := net.Alice.ListPayments(ctxt, 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)
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
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,
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
addInvoiceCtxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2016-12-21 12:19:01 +03:00
|
|
|
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.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-09-19 03:16:43 +03:00
|
|
|
if err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint); err != nil {
|
2017-03-15 06:06:33 +03:00
|
|
|
t.Fatalf("alice didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2017-09-19 03:16:43 +03:00
|
|
|
if err = net.Bob.WaitForNetworkChannelOpen(ctxt, chanPoint); err != nil {
|
|
|
|
t.Fatalf("bob didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2017-03-15 06:06:33 +03:00
|
|
|
|
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.
|
|
|
|
sendReq := &lnrpc.SendRequest{
|
2017-10-23 04:35:38 +03:00
|
|
|
PaymentRequest: invoiceResp.PaymentRequest,
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-10-26 01:32:00 +03:00
|
|
|
resp, err := net.Alice.SendPaymentSync(ctxt, sendReq)
|
|
|
|
if err != nil {
|
2016-12-05 14:59:36 +03:00
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
2017-10-26 01:32:00 +03:00
|
|
|
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{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
|
|
|
paymentsResp, err := net.Alice.ListPayments(ctxt, 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
|
|
|
}
|
|
|
|
|
2019-05-30 02:31:49 +03:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2019-05-30 02:31:49 +03:00
|
|
|
// Finally, verify that the payment request returned by the rpc matches
|
|
|
|
// the invoice that we paid.
|
|
|
|
if p.PaymentRequest != invoiceResp.PaymentRequest {
|
|
|
|
t.Fatalf("incorrect payreq, got: %v, want: %v",
|
|
|
|
p.PaymentRequest, invoiceResp.PaymentRequest)
|
|
|
|
}
|
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
// Delete all payments from Alice. DB should have no payments.
|
2016-12-31 03:41:59 +03:00
|
|
|
delReq := &lnrpc.DeleteAllPaymentsRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
|
|
|
_, err = net.Alice.DeleteAllPayments(ctxt, 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{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
|
|
|
paymentsResp, err = net.Alice.ListPayments(ctxt, 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
|
|
|
}
|
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
2017-12-19 02:18:40 +03:00
|
|
|
// assertAmountPaid checks that the ListChannels command of the provided
|
|
|
|
// node list the total amount sent and received as expected for the
|
|
|
|
// provided channel.
|
2018-11-28 11:00:19 +03:00
|
|
|
func assertAmountPaid(t *harnessTest, channelName string,
|
2017-12-19 02:18:40 +03:00
|
|
|
node *lntest.HarnessNode, chanPoint wire.OutPoint, amountSent,
|
|
|
|
amountReceived int64) {
|
2018-11-28 11:00:19 +03:00
|
|
|
ctxb := context.Background()
|
2017-12-19 02:18:40 +03:00
|
|
|
|
|
|
|
checkAmountPaid := func() error {
|
|
|
|
listReq := &lnrpc.ListChannelsRequest{}
|
2018-11-28 11:00:19 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := node.ListChannels(ctxt, listReq)
|
2017-12-19 02:18:40 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if channel.TotalSatoshisSent != amountSent {
|
|
|
|
return fmt.Errorf("%v: incorrect amount"+
|
|
|
|
" sent: %v != %v", channelName,
|
|
|
|
channel.TotalSatoshisSent,
|
|
|
|
amountSent)
|
|
|
|
}
|
|
|
|
if channel.TotalSatoshisReceived !=
|
|
|
|
amountReceived {
|
|
|
|
return fmt.Errorf("%v: incorrect amount"+
|
|
|
|
" received: %v != %v",
|
|
|
|
channelName,
|
|
|
|
channel.TotalSatoshisReceived,
|
|
|
|
amountReceived)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("channel not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if err := checkAmountPaid(); err != nil {
|
|
|
|
if isTimeover {
|
|
|
|
t.Fatalf("Check amount Paid failed: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 15:53:44 +03:00
|
|
|
// updateChannelPolicy updates the channel policy of node to the
|
|
|
|
// given fees and timelock delta. This function blocks until
|
|
|
|
// listenerNode has received the policy update.
|
|
|
|
func updateChannelPolicy(t *harnessTest, node *lntest.HarnessNode,
|
|
|
|
chanPoint *lnrpc.ChannelPoint, baseFee int64, feeRate int64,
|
|
|
|
timeLockDelta uint32, listenerNode *lntest.HarnessNode) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
expectedPolicy := &lnrpc.RoutingPolicy{
|
|
|
|
FeeBaseMsat: baseFee,
|
|
|
|
FeeRateMilliMsat: feeRate,
|
|
|
|
TimeLockDelta: timeLockDelta,
|
2018-08-22 10:32:43 +03:00
|
|
|
MinHtlc: 1000, // default value
|
2018-06-16 15:53:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
updateFeeReq := &lnrpc.PolicyUpdateRequest{
|
|
|
|
BaseFeeMsat: baseFee,
|
|
|
|
FeeRate: float64(feeRate) / testFeeBase,
|
|
|
|
TimeLockDelta: timeLockDelta,
|
|
|
|
Scope: &lnrpc.PolicyUpdateRequest_ChanPoint{
|
|
|
|
ChanPoint: chanPoint,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2018-06-16 15:53:44 +03:00
|
|
|
if _, err := node.UpdateChannelPolicy(ctxt, updateFeeReq); err != nil {
|
|
|
|
t.Fatalf("unable to update chan policy: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for listener node to receive the channel update from node.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-08-23 08:16:16 +03:00
|
|
|
graphSub := subscribeGraphNotifications(t, ctxt, listenerNode)
|
|
|
|
defer close(graphSub.quit)
|
2018-06-16 15:53:44 +03:00
|
|
|
|
|
|
|
waitForChannelUpdate(
|
2018-08-23 08:16:16 +03:00
|
|
|
t, graphSub,
|
2018-08-22 10:32:42 +03:00
|
|
|
[]expectedChanUpdate{
|
|
|
|
{node.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
},
|
2018-06-16 15:53:44 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
func testMultiHopPayments(net *lntest.NetworkHarness, t *harnessTest) {
|
2016-09-26 20:41:57 +03:00
|
|
|
ctxb := context.Background()
|
2018-11-28 11:25:46 +03:00
|
|
|
|
|
|
|
const chanAmt = btcutil.Amount(100000)
|
2017-11-14 04:37:20 +03:00
|
|
|
var networkChans []*lnrpc.ChannelPoint
|
2016-09-26 20:41:57 +03:00
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-11-14 04:37:20 +03:00
|
|
|
networkChans = append(networkChans, chanPointAlice)
|
2016-10-15 16:47:09 +03:00
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
aliceChanTXID, err := lnd.GetChanPointFundingTxid(chanPointAlice)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2016-09-26 20:41:57 +03:00
|
|
|
aliceFundPoint := wire.OutPoint{
|
|
|
|
Hash: *aliceChanTXID,
|
|
|
|
Index: chanPointAlice.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:33:55 +03:00
|
|
|
// As preliminary setup, we'll create two new nodes: Carol and Dave,
|
|
|
|
// such that we now have a 4 ndoe, 3 channel topology. Dave will make
|
|
|
|
// a channel with Alice, and Carol with Dave. After this setup, the
|
|
|
|
// network topology should now look like:
|
|
|
|
// Carol -> Dave -> Alice -> Bob
|
2016-09-26 20:41:57 +03:00
|
|
|
//
|
2017-10-25 04:33:55 +03:00
|
|
|
// First, we'll create Dave and establish a channel to Alice.
|
2018-04-27 11:42:04 +03:00
|
|
|
dave, err := net.NewNode("Dave", nil)
|
2017-10-25 04:33:55 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, dave, net.Alice); err != nil {
|
2017-10-25 04:33:55 +03:00
|
|
|
t.Fatalf("unable to connect dave to alice: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, dave)
|
2017-10-25 04:33:55 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to dave: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointDave := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, dave, net.Alice,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-11-14 04:37:20 +03:00
|
|
|
networkChans = append(networkChans, chanPointDave)
|
2019-05-15 10:02:53 +03:00
|
|
|
daveChanTXID, err := lnd.GetChanPointFundingTxid(chanPointDave)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2017-10-25 04:33:55 +03:00
|
|
|
daveFundPoint := wire.OutPoint{
|
|
|
|
Hash: *daveChanTXID,
|
|
|
|
Index: chanPointDave.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll create Carol and establish a channel to from her to
|
|
|
|
// Dave.
|
2018-04-27 11:42:04 +03:00
|
|
|
carol, err := net.NewNode("Carol", nil)
|
2016-09-26 20:41:57 +03:00
|
|
|
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
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, dave); err != nil {
|
2017-10-25 04:33:55 +03:00
|
|
|
t.Fatalf("unable to connect carol to dave: %v", err)
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
2016-09-26 20:41:57 +03:00
|
|
|
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
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointCarol := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, carol, dave,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-11-14 04:37:20 +03:00
|
|
|
networkChans = append(networkChans, chanPointCarol)
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
carolChanTXID, err := lnd.GetChanPointFundingTxid(chanPointCarol)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2016-09-26 20:41:57 +03:00
|
|
|
carolFundPoint := wire.OutPoint{
|
|
|
|
Hash: *carolChanTXID,
|
|
|
|
Index: chanPointCarol.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2017-11-14 04:37:20 +03:00
|
|
|
// Wait for all nodes to have seen all channels.
|
2018-01-21 16:57:30 +03:00
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, net.Bob, carol, dave}
|
|
|
|
nodeNames := []string{"Alice", "Bob", "Carol", "Dave"}
|
2017-11-14 04:37:20 +03:00
|
|
|
for _, chanPoint := range networkChans {
|
2018-01-21 16:57:30 +03:00
|
|
|
for i, node := range nodes {
|
2019-05-15 10:02:53 +03:00
|
|
|
txid, err := lnd.GetChanPointFundingTxid(chanPoint)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2018-01-21 16:57:30 +03:00
|
|
|
point := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-11-14 04:37:20 +03:00
|
|
|
err = node.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
2018-01-21 16:57:30 +03:00
|
|
|
t.Fatalf("%s(%d): timeout waiting for "+
|
|
|
|
"channel(%s) open: %v", nodeNames[i],
|
|
|
|
node.NodeID, point, err)
|
2017-11-14 04:37:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 20:41:57 +03:00
|
|
|
// 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
|
2018-11-20 17:09:44 +03:00
|
|
|
payReqs, _, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
net.Bob, paymentAmt, numPayments,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:33:55 +03:00
|
|
|
// We'll wait for all parties to recognize the new channels within the
|
|
|
|
// network.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-10-25 04:33:55 +03:00
|
|
|
err = dave.WaitForNetworkChannelOpen(ctxt, chanPointDave)
|
2017-03-15 06:06:33 +03:00
|
|
|
if err != nil {
|
2017-10-25 04:33:55 +03:00
|
|
|
t.Fatalf("dave didn't advertise his channel: %v", err)
|
2017-03-15 06:06:33 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-10-25 04:33:55 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPointCarol)
|
2017-03-15 06:06:33 +03:00
|
|
|
if err != nil {
|
2017-10-25 04:33:55 +03:00
|
|
|
t.Fatalf("carol didn't advertise her channel in time: %v",
|
|
|
|
err)
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
|
2017-12-03 05:42:31 +03:00
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
2018-06-15 22:33:49 +03:00
|
|
|
// Set the fee policies of the Alice -> Bob and the Dave -> Alice
|
|
|
|
// channel edges to relatively large non default values. This makes it
|
2018-06-16 15:53:44 +03:00
|
|
|
// possible to pick up more subtle fee calculation errors.
|
2019-03-12 04:43:37 +03:00
|
|
|
updateChannelPolicy(
|
|
|
|
t, net.Alice, chanPointAlice, 1000, 100000,
|
2019-05-15 10:02:53 +03:00
|
|
|
lnd.DefaultBitcoinTimeLockDelta, carol,
|
2019-03-12 04:43:37 +03:00
|
|
|
)
|
2018-06-16 15:53:44 +03:00
|
|
|
|
2019-03-12 04:43:37 +03:00
|
|
|
updateChannelPolicy(
|
|
|
|
t, dave, chanPointDave, 5000, 150000,
|
2019-05-15 10:02:53 +03:00
|
|
|
lnd.DefaultBitcoinTimeLockDelta, carol,
|
2019-03-12 04:43:37 +03:00
|
|
|
)
|
2018-06-16 15:53:44 +03:00
|
|
|
|
2017-12-03 05:42:31 +03:00
|
|
|
// Using Carol as the source, pay to the 5 invoices from Bob created
|
|
|
|
// above.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-10-26 01:32:00 +03:00
|
|
|
err = completePaymentRequests(ctxt, carol, payReqs, true)
|
2016-09-26 20:41:57 +03:00
|
|
|
if err != nil {
|
2017-10-26 01:32:00 +03:00
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
|
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-12-19 02:18:40 +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
|
|
|
|
// 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->David->Alice->Bob, order is Bob,
|
|
|
|
// Alice, David, Carol.
|
2018-06-16 15:53:44 +03:00
|
|
|
|
|
|
|
// The final node bob expects to get paid five times 1000 sat.
|
|
|
|
expectedAmountPaidAtoB := int64(5 * 1000)
|
|
|
|
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Bob(remote)", net.Bob,
|
2018-06-16 15:53:44 +03:00
|
|
|
aliceFundPoint, int64(0), expectedAmountPaidAtoB)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Bob(remote)", net.Alice,
|
2018-06-16 15:53:44 +03:00
|
|
|
aliceFundPoint, expectedAmountPaidAtoB, int64(0))
|
|
|
|
|
2018-06-15 22:33:49 +03:00
|
|
|
// To forward a payment of 1000 sat, Alice is charging a fee of
|
2018-06-16 15:53:44 +03:00
|
|
|
// 1 sat + 10% = 101 sat.
|
|
|
|
const expectedFeeAlice = 5 * 101
|
2018-06-15 22:33:49 +03:00
|
|
|
|
2018-06-16 15:53:44 +03:00
|
|
|
// Dave needs to pay what Alice pays plus Alice's fee.
|
|
|
|
expectedAmountPaidDtoA := expectedAmountPaidAtoB + expectedFeeAlice
|
|
|
|
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Alice(remote)", net.Alice,
|
2018-06-16 15:53:44 +03:00
|
|
|
daveFundPoint, int64(0), expectedAmountPaidDtoA)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Alice(remote)", dave,
|
2018-06-16 15:53:44 +03:00
|
|
|
daveFundPoint, expectedAmountPaidDtoA, int64(0))
|
|
|
|
|
2018-06-15 22:33:49 +03:00
|
|
|
// To forward a payment of 1101 sat, Dave is charging a fee of
|
2018-06-16 15:53:44 +03:00
|
|
|
// 5 sat + 15% = 170.15 sat. This is rounded down in rpcserver to 170.
|
|
|
|
const expectedFeeDave = 5 * 170
|
|
|
|
|
|
|
|
// Carol needs to pay what Dave pays plus Dave's fee.
|
|
|
|
expectedAmountPaidCtoD := expectedAmountPaidDtoA + expectedFeeDave
|
|
|
|
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Carol(local) => Dave(remote)", dave,
|
2018-06-16 15:53:44 +03:00
|
|
|
carolFundPoint, int64(0), expectedAmountPaidCtoD)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Carol(local) => Dave(remote)", carol,
|
2018-06-16 15:53:44 +03:00
|
|
|
carolFundPoint, expectedAmountPaidCtoD, int64(0))
|
2017-12-19 02:18:40 +03:00
|
|
|
|
2018-02-28 09:24:15 +03:00
|
|
|
// Now that we know all the balances have been settled out properly,
|
|
|
|
// we'll ensure that our internal record keeping for completed circuits
|
|
|
|
// was properly updated.
|
|
|
|
|
|
|
|
// First, check that the FeeReport response shows the proper fees
|
2018-06-16 15:53:44 +03:00
|
|
|
// accrued over each time range. Dave should've earned 170 satoshi for
|
2018-02-28 09:24:15 +03:00
|
|
|
// each of the forwarded payments.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
feeReport, err := dave.FeeReport(ctxt, &lnrpc.FeeReportRequest{})
|
2018-02-28 09:24:15 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for fee report: %v", err)
|
|
|
|
}
|
2018-06-16 15:53:44 +03:00
|
|
|
|
|
|
|
if feeReport.DayFeeSum != uint64(expectedFeeDave) {
|
|
|
|
t.Fatalf("fee mismatch: expected %v, got %v", expectedFeeDave,
|
2018-02-28 09:24:15 +03:00
|
|
|
feeReport.DayFeeSum)
|
|
|
|
}
|
2018-06-16 15:53:44 +03:00
|
|
|
if feeReport.WeekFeeSum != uint64(expectedFeeDave) {
|
|
|
|
t.Fatalf("fee mismatch: expected %v, got %v", expectedFeeDave,
|
2018-02-28 09:24:15 +03:00
|
|
|
feeReport.WeekFeeSum)
|
|
|
|
}
|
2018-06-16 15:53:44 +03:00
|
|
|
if feeReport.MonthFeeSum != uint64(expectedFeeDave) {
|
|
|
|
t.Fatalf("fee mismatch: expected %v, got %v", expectedFeeDave,
|
2018-02-28 09:24:15 +03:00
|
|
|
feeReport.MonthFeeSum)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, ensure that if we issue the vanilla query for the forwarding
|
|
|
|
// history, it returns 5 values, and each entry is formatted properly.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-28 09:24:15 +03:00
|
|
|
fwdingHistory, err := dave.ForwardingHistory(
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, &lnrpc.ForwardingHistoryRequest{},
|
2018-02-28 09:24:15 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for fee report: %v", err)
|
|
|
|
}
|
|
|
|
if len(fwdingHistory.ForwardingEvents) != 5 {
|
|
|
|
t.Fatalf("wrong number of forwarding event: expected %v, "+
|
|
|
|
"got %v", 5, len(fwdingHistory.ForwardingEvents))
|
|
|
|
}
|
2018-06-16 15:53:44 +03:00
|
|
|
expectedForwardingFee := uint64(expectedFeeDave / numPayments)
|
2018-02-28 09:24:15 +03:00
|
|
|
for _, event := range fwdingHistory.ForwardingEvents {
|
2018-06-16 15:53:44 +03:00
|
|
|
// Each event should show a fee of 170 satoshi.
|
|
|
|
if event.Fee != expectedForwardingFee {
|
|
|
|
t.Fatalf("fee mismatch: expected %v, got %v",
|
|
|
|
expectedForwardingFee, event.Fee)
|
2018-02-28 09:24:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-12-19 02:18:40 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-12-19 02:18:40 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, dave, chanPointDave, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-12-19 02:18:40 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPointCarol, false)
|
|
|
|
}
|
|
|
|
|
2018-02-03 13:21:16 +03:00
|
|
|
// testSingleHopSendToRoute tests that payments are properly processed
|
|
|
|
// through a provided route with a single hop. We'll create the
|
|
|
|
// following network topology:
|
|
|
|
// Alice --100k--> Bob
|
|
|
|
// We'll query the daemon for routes from Alice to Bob and then
|
|
|
|
// send payments through the route.
|
|
|
|
func testSingleHopSendToRoute(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
2018-11-28 11:25:46 +03:00
|
|
|
|
|
|
|
const chanAmt = btcutil.Amount(100000)
|
2018-02-03 13:21:16 +03:00
|
|
|
var networkChans []*lnrpc.ChannelPoint
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-08-22 10:32:42 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
2018-02-03 13:21:16 +03:00
|
|
|
networkChans = append(networkChans, chanPointAlice)
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
aliceChanTXID, err := lnd.GetChanPointFundingTxid(chanPointAlice)
|
2018-02-03 13:21:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
aliceFundPoint := wire.OutPoint{
|
|
|
|
Hash: *aliceChanTXID,
|
|
|
|
Index: chanPointAlice.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all nodes to have seen all channels.
|
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, net.Bob}
|
|
|
|
nodeNames := []string{"Alice", "Bob"}
|
|
|
|
for _, chanPoint := range networkChans {
|
|
|
|
for i, node := range nodes {
|
2019-05-15 10:02:53 +03:00
|
|
|
txid, err := lnd.GetChanPointFundingTxid(chanPoint)
|
2018-02-03 13:21:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
point := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
err = node.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s(%d): timeout waiting for "+
|
|
|
|
"channel(%s) open: %v", nodeNames[i],
|
|
|
|
node.NodeID, point, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query for routes to pay from Alice to Bob.
|
2019-03-12 04:43:37 +03:00
|
|
|
// We set FinalCltvDelta to 40 since by default QueryRoutes returns
|
2018-02-03 13:21:16 +03:00
|
|
|
// the last hop with a final cltv delta of 9 where as the default in
|
2019-03-12 04:43:37 +03:00
|
|
|
// htlcswitch is 40.
|
2018-02-03 13:21:16 +03:00
|
|
|
const paymentAmt = 1000
|
|
|
|
routesReq := &lnrpc.QueryRoutesRequest{
|
|
|
|
PubKey: net.Bob.PubKeyStr,
|
|
|
|
Amt: paymentAmt,
|
2019-05-15 10:02:53 +03:00
|
|
|
FinalCltvDelta: lnd.DefaultBitcoinTimeLockDelta,
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
routes, err := net.Alice.QueryRoutes(ctxt, routesReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get route: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create 5 invoices for Bob, which expect a payment from Alice for 1k
|
|
|
|
// satoshis with a different preimage each time.
|
|
|
|
const numPayments = 5
|
2018-11-20 17:09:44 +03:00
|
|
|
_, rHashes, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
net.Bob, paymentAmt, numPayments,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll wait for all parties to recognize the new channels within the
|
|
|
|
// network.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
err = net.Bob.WaitForNetworkChannelOpen(ctxt, chanPointAlice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't advertise her channel in time: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
|
|
|
// Using Alice as the source, pay to the 5 invoices from Carol created
|
|
|
|
// above.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
alicePayStream, err := net.Alice.SendToRoute(ctxt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rHash := range rHashes {
|
|
|
|
sendReq := &lnrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: rHash,
|
2018-08-08 12:09:30 +03:00
|
|
|
Route: routes.Routes[0],
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
|
|
|
err := alicePayStream.Send(sendReq)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for range rHashes {
|
|
|
|
resp, err := alicePayStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
if resp.PaymentError != "" {
|
|
|
|
t.Fatalf("received payment error: %v", resp.PaymentError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 02:31:49 +03:00
|
|
|
req := &lnrpc.ListPaymentsRequest{}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
|
|
|
paymentsResp, err := net.Alice.ListPayments(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error when obtaining Alice payments: %v", err)
|
|
|
|
}
|
|
|
|
if len(paymentsResp.Payments) != 5 {
|
|
|
|
t.Fatalf("incorrect number of payments, got %v, want %v",
|
|
|
|
len(paymentsResp.Payments), 5)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the ListPayments displays the payment without an invoice
|
|
|
|
// since the payment was completed with SendToRoute.
|
|
|
|
for _, p := range paymentsResp.Payments {
|
|
|
|
if p.PaymentRequest != "" {
|
|
|
|
t.Fatalf("incorrect payreq, want: \"\", got: %v",
|
|
|
|
p.PaymentRequest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-03 13:21:16 +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
|
|
|
|
// payment flow generated above. The order of asserts corresponds to
|
|
|
|
// increasing of time is needed to embed the HTLC in commitment
|
|
|
|
// transaction, in channel Alice->Bob, order is Bob and then Alice.
|
|
|
|
const amountPaid = int64(5000)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Bob(remote)", net.Bob,
|
2018-02-03 13:21:16 +03:00
|
|
|
aliceFundPoint, int64(0), amountPaid)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Bob(remote)", net.Alice,
|
2018-02-03 13:21:16 +03:00
|
|
|
aliceFundPoint, amountPaid, int64(0))
|
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testMultiHopSendToRoute tests that payments are properly processed
|
|
|
|
// through a provided route. We'll create the following network topology:
|
|
|
|
// Alice --100k--> Bob --100k--> Carol
|
|
|
|
// We'll query the daemon for routes from Alice to Carol and then
|
|
|
|
// send payments through the routes.
|
|
|
|
func testMultiHopSendToRoute(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
2018-11-28 11:25:46 +03:00
|
|
|
|
|
|
|
const chanAmt = btcutil.Amount(100000)
|
2018-02-03 13:21:16 +03:00
|
|
|
var networkChans []*lnrpc.ChannelPoint
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-08-22 10:32:42 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
2018-02-03 13:21:16 +03:00
|
|
|
networkChans = append(networkChans, chanPointAlice)
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
aliceChanTXID, err := lnd.GetChanPointFundingTxid(chanPointAlice)
|
2018-02-03 13:21:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
aliceFundPoint := wire.OutPoint{
|
|
|
|
Hash: *aliceChanTXID,
|
|
|
|
Index: chanPointAlice.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create Carol and establish a channel from Bob. Bob is the sole funder
|
|
|
|
// of the channel with 100k satoshis. The network topology should look like:
|
|
|
|
// Alice -> Bob -> Carol
|
|
|
|
carol, err := net.NewNode("Carol", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, net.Bob); err != nil {
|
2018-02-03 13:21:16 +03:00
|
|
|
t.Fatalf("unable to connect carol to alice: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, net.Bob)
|
2018-02-03 13:21:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to bob: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-08-22 10:32:42 +03:00
|
|
|
chanPointBob := openChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Bob, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
2018-02-03 13:21:16 +03:00
|
|
|
networkChans = append(networkChans, chanPointBob)
|
2019-05-15 10:02:53 +03:00
|
|
|
bobChanTXID, err := lnd.GetChanPointFundingTxid(chanPointBob)
|
2018-02-03 13:21:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
bobFundPoint := wire.OutPoint{
|
|
|
|
Hash: *bobChanTXID,
|
|
|
|
Index: chanPointBob.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all nodes to have seen all channels.
|
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, net.Bob, carol}
|
|
|
|
nodeNames := []string{"Alice", "Bob", "Carol"}
|
|
|
|
for _, chanPoint := range networkChans {
|
|
|
|
for i, node := range nodes {
|
2019-05-15 10:02:53 +03:00
|
|
|
txid, err := lnd.GetChanPointFundingTxid(chanPoint)
|
2018-02-03 13:21:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
point := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
err = node.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s(%d): timeout waiting for "+
|
|
|
|
"channel(%s) open: %v", nodeNames[i],
|
|
|
|
node.NodeID, point, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query for routes to pay from Alice to Carol.
|
2019-03-12 04:43:37 +03:00
|
|
|
// We set FinalCltvDelta to 40 since by default QueryRoutes returns
|
2018-02-03 13:21:16 +03:00
|
|
|
// the last hop with a final cltv delta of 9 where as the default in
|
2019-03-12 04:43:37 +03:00
|
|
|
// htlcswitch is 40.
|
2018-02-03 13:21:16 +03:00
|
|
|
const paymentAmt = 1000
|
|
|
|
routesReq := &lnrpc.QueryRoutesRequest{
|
|
|
|
PubKey: carol.PubKeyStr,
|
|
|
|
Amt: paymentAmt,
|
2019-05-15 10:02:53 +03:00
|
|
|
FinalCltvDelta: lnd.DefaultBitcoinTimeLockDelta,
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
routes, err := net.Alice.QueryRoutes(ctxt, routesReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get route: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create 5 invoices for Carol, which expect a payment from Alice for 1k
|
|
|
|
// satoshis with a different preimage each time.
|
|
|
|
const numPayments = 5
|
2018-11-20 17:09:44 +03:00
|
|
|
_, rHashes, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
carol, paymentAmt, numPayments,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll wait for all parties to recognize the new channels within the
|
|
|
|
// network.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPointBob)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't advertise his channel in time: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
|
|
|
// Using Alice as the source, pay to the 5 invoices from Carol created
|
|
|
|
// above.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
alicePayStream, err := net.Alice.SendToRoute(ctxt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rHash := range rHashes {
|
|
|
|
sendReq := &lnrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: rHash,
|
2018-08-08 12:09:30 +03:00
|
|
|
Route: routes.Routes[0],
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
|
|
|
err := alicePayStream.Send(sendReq)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for range rHashes {
|
|
|
|
resp, err := alicePayStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
if resp.PaymentError != "" {
|
|
|
|
t.Fatalf("received payment error: %v", resp.PaymentError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// At this point all the channels within our proto network should be
|
|
|
|
// shifted by 5k satoshis in the direction of Carol, the sink within the
|
|
|
|
// payment flow generated above. The order of asserts corresponds to
|
|
|
|
// increasing of time is needed to embed the HTLC in commitment
|
|
|
|
// transaction, in channel Alice->Bob->Carol, order is Carol, Bob,
|
|
|
|
// Alice.
|
|
|
|
const amountPaid = int64(5000)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Carol(remote)", carol,
|
2018-02-03 13:21:16 +03:00
|
|
|
bobFundPoint, int64(0), amountPaid)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Carol(remote)", net.Bob,
|
2018-02-03 13:21:16 +03:00
|
|
|
bobFundPoint, amountPaid, int64(0))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Bob(remote)", net.Bob,
|
2018-02-03 13:21:16 +03:00
|
|
|
aliceFundPoint, int64(0), amountPaid+(baseFee*numPayments))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Bob(remote)", net.Alice,
|
2018-02-03 13:21:16 +03:00
|
|
|
aliceFundPoint, amountPaid+(baseFee*numPayments), int64(0))
|
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPointBob, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testSendToRouteErrorPropagation tests propagation of errors that occur
|
|
|
|
// while processing a multi-hop payment through an unknown route.
|
|
|
|
func testSendToRouteErrorPropagation(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
const chanAmt = btcutil.Amount(100000)
|
|
|
|
|
2018-02-03 13:21:16 +03:00
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-08-22 10:32:42 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
2018-11-27 11:18:35 +03:00
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
err := net.Alice.WaitForNetworkChannelOpen(ctxt, chanPointAlice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't advertise her channel: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-06-30 04:07:55 +03:00
|
|
|
// Create a new nodes (Carol and Charlie), load her with some funds,
|
|
|
|
// then establish a connection between Carol and Charlie with a channel
|
|
|
|
// that has identical capacity to the one created above.Then we will
|
|
|
|
// get route via queryroutes call which will be fake route for Alice ->
|
|
|
|
// Bob graph.
|
2018-02-03 13:21:16 +03:00
|
|
|
//
|
|
|
|
// The network topology should now look like: Alice -> Bob; Carol -> Charlie.
|
|
|
|
carol, err := net.NewNode("Carol", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
2018-02-03 13:21:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
charlie, err := net.NewNode("Charlie", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, charlie)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, charlie)
|
2018-02-03 13:21:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to charlie: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, charlie); err != nil {
|
2018-02-03 13:21:16 +03:00
|
|
|
t.Fatalf("unable to connect carol to alice: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-08-22 10:32:42 +03:00
|
|
|
chanPointCarol := openChannelAndAssert(
|
|
|
|
ctxt, t, net, carol, charlie,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPointCarol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("carol didn't advertise her channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query routes from Carol to Charlie which will be an invalid route
|
|
|
|
// for Alice -> Bob.
|
|
|
|
fakeReq := &lnrpc.QueryRoutesRequest{
|
2019-05-07 18:01:01 +03:00
|
|
|
PubKey: charlie.PubKeyStr,
|
|
|
|
Amt: int64(1),
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
fakeRoute, err := carol.QueryRoutes(ctxt, fakeReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable get fake route: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create 1 invoices for Bob, which expect a payment from Alice for 1k
|
|
|
|
// satoshis
|
|
|
|
const paymentAmt = 1000
|
|
|
|
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := net.Bob.AddInvoice(ctxt, invoice)
|
2018-02-03 13:21:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rHash := resp.RHash
|
|
|
|
|
|
|
|
// Using Alice as the source, pay to the 5 invoices from Bob created above.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
alicePayStream, err := net.Alice.SendToRoute(ctxt)
|
2018-02-03 13:21:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sendReq := &lnrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: rHash,
|
2018-08-08 12:09:30 +03:00
|
|
|
Route: fakeRoute.Routes[0],
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := alicePayStream.Send(sendReq); err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this place we should get an rpc error with notification
|
|
|
|
// that edge is not found on hop(0)
|
|
|
|
if _, err := alicePayStream.Recv(); err != nil && strings.Contains(err.Error(),
|
|
|
|
"edge not found") {
|
|
|
|
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("payment stream has been closed but fake route has consumed: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-03 13:21:16 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPointCarol, false)
|
|
|
|
}
|
|
|
|
|
2018-09-26 18:30:09 +03:00
|
|
|
// testUnannouncedChannels checks unannounced channels are not returned by
|
2019-05-05 01:35:37 +03:00
|
|
|
// describeGraph RPC request unless explicitly asked for.
|
2018-09-26 18:30:09 +03:00
|
|
|
func testUnannouncedChannels(net *lntest.NetworkHarness, t *harnessTest) {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
amount := lnd.MaxBtcFundingAmount
|
2018-09-26 18:30:09 +03:00
|
|
|
|
|
|
|
// Open a channel between Alice and Bob, ensuring the
|
|
|
|
// channel has been opened properly.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-09-26 18:30:09 +03:00
|
|
|
chanOpenUpdate, err := net.OpenChannel(
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, net.Alice, net.Bob,
|
2018-09-26 18:30:09 +03:00
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: amount,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to open channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mine 2 blocks, and check that the channel is opened but not yet
|
|
|
|
// announced to the network.
|
2018-11-29 14:33:11 +03:00
|
|
|
mineBlocks(t, net, 2, 1)
|
2018-09-26 18:30:09 +03:00
|
|
|
|
|
|
|
// One block is enough to make the channel ready for use, since the
|
|
|
|
// nodes have defaultNumConfs=1 set.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
fundingChanPoint, err := net.WaitForChannelOpen(ctxt, chanOpenUpdate)
|
2018-09-26 18:30:09 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error while waiting for channel open: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice should have 1 edge in her graph.
|
|
|
|
req := &lnrpc.ChannelGraphRequest{
|
|
|
|
IncludeUnannounced: true,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
chanGraph, err := net.Alice.DescribeGraph(ctxt, req)
|
2018-09-26 18:30:09 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query alice's graph: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
numEdges := len(chanGraph.Edges)
|
|
|
|
if numEdges != 1 {
|
|
|
|
t.Fatalf("expected to find 1 edge in the graph, found %d", numEdges)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Channels should not be announced yet, hence Alice should have no
|
|
|
|
// announced edges in her graph.
|
|
|
|
req.IncludeUnannounced = false
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
chanGraph, err = net.Alice.DescribeGraph(ctxt, req)
|
2018-09-26 18:30:09 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query alice's graph: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
numEdges = len(chanGraph.Edges)
|
|
|
|
if numEdges != 0 {
|
|
|
|
t.Fatalf("expected to find 0 announced edges in the graph, found %d",
|
|
|
|
numEdges)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mine 4 more blocks, and check that the channel is now announced.
|
2018-11-29 14:33:11 +03:00
|
|
|
mineBlocks(t, net, 4, 0)
|
2018-09-26 18:30:09 +03:00
|
|
|
|
|
|
|
// Give the network a chance to learn that auth proof is confirmed.
|
|
|
|
var predErr error
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
// The channel should now be announced. Check that Alice has 1
|
|
|
|
// announced edge.
|
|
|
|
req.IncludeUnannounced = false
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
chanGraph, err = net.Alice.DescribeGraph(ctxt, req)
|
2018-09-26 18:30:09 +03:00
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query alice's graph: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
numEdges = len(chanGraph.Edges)
|
|
|
|
if numEdges != 1 {
|
|
|
|
predErr = fmt.Errorf("expected to find 1 announced edge in "+
|
|
|
|
"the graph, found %d", numEdges)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", predErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The channel should now be announced. Check that Alice has 1 announced
|
|
|
|
// edge.
|
|
|
|
req.IncludeUnannounced = false
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
chanGraph, err = net.Alice.DescribeGraph(ctxt, req)
|
2018-09-26 18:30:09 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query alice's graph: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
numEdges = len(chanGraph.Edges)
|
|
|
|
if numEdges != 1 {
|
|
|
|
t.Fatalf("expected to find 1 announced edge in the graph, found %d",
|
|
|
|
numEdges)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the channel used during the test.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, fundingChanPoint, false)
|
2018-09-26 18:30:09 +03:00
|
|
|
}
|
|
|
|
|
2017-12-19 02:18:40 +03:00
|
|
|
// testPrivateChannels tests that a private channel can be used for
|
|
|
|
// routing by the two endpoints of the channel, but is not known by
|
|
|
|
// the rest of the nodes in the graph.
|
|
|
|
func testPrivateChannels(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
2018-11-28 11:25:46 +03:00
|
|
|
|
|
|
|
const chanAmt = btcutil.Amount(100000)
|
2017-12-19 02:18:40 +03:00
|
|
|
var networkChans []*lnrpc.ChannelPoint
|
|
|
|
|
2018-04-18 05:03:27 +03:00
|
|
|
// We create the following topology:
|
2017-12-19 02:18:40 +03:00
|
|
|
//
|
|
|
|
// Dave --100k--> Alice --200k--> Bob
|
|
|
|
// ^ ^
|
|
|
|
// | |
|
|
|
|
// 100k 100k
|
|
|
|
// | |
|
|
|
|
// +---- Carol ----+
|
|
|
|
//
|
|
|
|
// where the 100k channel between Carol and Alice is private.
|
2016-10-30 17:55:25 +03:00
|
|
|
|
2017-12-19 02:18:40 +03:00
|
|
|
// Open a channel with 200k satoshis between Alice and Bob.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt * 2,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-12-19 02:18:40 +03:00
|
|
|
networkChans = append(networkChans, chanPointAlice)
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
aliceChanTXID, err := lnd.GetChanPointFundingTxid(chanPointAlice)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2017-12-19 02:18:40 +03:00
|
|
|
aliceFundPoint := wire.OutPoint{
|
|
|
|
Hash: *aliceChanTXID,
|
|
|
|
Index: chanPointAlice.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create Dave, and a channel to Alice of 100k.
|
2018-04-27 11:42:04 +03:00
|
|
|
dave, err := net.NewNode("Dave", nil)
|
2017-12-19 02:18:40 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, dave, net.Alice); err != nil {
|
2017-12-19 02:18:40 +03:00
|
|
|
t.Fatalf("unable to connect dave to alice: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, dave)
|
2017-12-19 02:18:40 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to dave: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointDave := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, dave, net.Alice,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-12-19 02:18:40 +03:00
|
|
|
networkChans = append(networkChans, chanPointDave)
|
2019-05-15 10:02:53 +03:00
|
|
|
daveChanTXID, err := lnd.GetChanPointFundingTxid(chanPointDave)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2017-12-19 02:18:40 +03:00
|
|
|
daveFundPoint := wire.OutPoint{
|
|
|
|
Hash: *daveChanTXID,
|
|
|
|
Index: chanPointDave.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll create Carol and establish a channel from her to
|
|
|
|
// Dave of 100k.
|
2018-04-27 11:42:04 +03:00
|
|
|
carol, err := net.NewNode("Carol", nil)
|
2017-12-19 02:18:40 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, dave); err != nil {
|
2017-12-19 02:18:40 +03:00
|
|
|
t.Fatalf("unable to connect carol to dave: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
2017-12-19 02:18:40 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointCarol := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, carol, dave,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-12-19 02:18:40 +03:00
|
|
|
networkChans = append(networkChans, chanPointCarol)
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
carolChanTXID, err := lnd.GetChanPointFundingTxid(chanPointCarol)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2017-12-19 02:18:40 +03:00
|
|
|
carolFundPoint := wire.OutPoint{
|
|
|
|
Hash: *carolChanTXID,
|
|
|
|
Index: chanPointCarol.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all nodes to have seen all these channels, as they
|
|
|
|
// are all public.
|
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, net.Bob, carol, dave}
|
2018-01-21 16:57:30 +03:00
|
|
|
nodeNames := []string{"Alice", "Bob", "Carol", "Dave"}
|
2017-12-19 02:18:40 +03:00
|
|
|
for _, chanPoint := range networkChans {
|
2018-01-21 16:57:30 +03:00
|
|
|
for i, node := range nodes {
|
2019-05-15 10:02:53 +03:00
|
|
|
txid, err := lnd.GetChanPointFundingTxid(chanPoint)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2018-01-21 16:57:30 +03:00
|
|
|
point := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-12-19 02:18:40 +03:00
|
|
|
err = node.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
2016-10-30 17:55:25 +03:00
|
|
|
if err != nil {
|
2018-01-21 16:57:30 +03:00
|
|
|
t.Fatalf("%s(%d): timeout waiting for "+
|
|
|
|
"channel(%s) open: %v", nodeNames[i],
|
|
|
|
node.NodeID, point, err)
|
2016-10-30 17:55:25 +03:00
|
|
|
}
|
2017-12-19 02:18:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now create a _private_ channel directly between Carol and
|
|
|
|
// Alice of 100k.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, net.Alice); err != nil {
|
2017-12-19 02:18:40 +03:00
|
|
|
t.Fatalf("unable to connect dave to alice: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-08-10 07:17:06 +03:00
|
|
|
chanOpenUpdate, err := net.OpenChannel(
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, carol, net.Alice,
|
2018-08-22 10:32:42 +03:00
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
Private: true,
|
|
|
|
},
|
2018-08-10 07:17:06 +03:00
|
|
|
)
|
2017-12-19 02:18:40 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to open channel: %v", err)
|
|
|
|
}
|
2016-10-30 17:55:25 +03:00
|
|
|
|
2017-12-19 02:18:40 +03:00
|
|
|
// One block is enough to make the channel ready for use, since the
|
|
|
|
// nodes have defaultNumConfs=1 set.
|
2018-11-29 14:33:11 +03:00
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
chanPointPrivate, err := net.WaitForChannelOpen(ctxt, chanOpenUpdate)
|
2017-12-19 02:18:40 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error while waiting for channel open: %v", err)
|
|
|
|
}
|
2019-05-15 10:02:53 +03:00
|
|
|
fundingTxID, err := lnd.GetChanPointFundingTxid(chanPointPrivate)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2017-12-19 02:18:40 +03:00
|
|
|
assertTxInBlock(t, block, fundingTxID)
|
|
|
|
|
|
|
|
// The channel should be listed in the peer information returned by
|
|
|
|
// both peers.
|
|
|
|
privateFundPoint := wire.OutPoint{
|
|
|
|
Hash: *fundingTxID,
|
|
|
|
Index: chanPointPrivate.OutputIndex,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.AssertChannelExists(ctxt, carol, &privateFundPoint)
|
2017-12-19 02:18:40 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to assert channel existence: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.AssertChannelExists(ctxt, net.Alice, &privateFundPoint)
|
2017-12-19 02:18:40 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to assert channel existence: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The channel should be available for payments between Carol and Alice.
|
|
|
|
// We check this by sending payments from Carol to Bob, that
|
|
|
|
// collectively would deplete at least one of Carol's channels.
|
|
|
|
|
|
|
|
// Create 2 invoices for Bob, each of 70k satoshis. Since each of
|
|
|
|
// Carol's channels is of size 100k, these payments cannot succeed
|
|
|
|
// by only using one of the channels.
|
|
|
|
const numPayments = 2
|
|
|
|
const paymentAmt = 70000
|
2018-11-20 17:09:44 +03:00
|
|
|
payReqs, _, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
net.Bob, paymentAmt, numPayments,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2017-12-19 02:18:40 +03:00
|
|
|
}
|
2016-10-30 17:55:25 +03:00
|
|
|
|
2017-12-19 02:18:40 +03:00
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
|
|
|
// Let Carol pay the invoices.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-12-19 02:18:40 +03:00
|
|
|
err = completePaymentRequests(ctxt, carol, payReqs, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// Bob should have received 140k satoshis from Alice.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Bob(remote)", net.Bob,
|
2017-12-19 02:18:40 +03:00
|
|
|
aliceFundPoint, int64(0), 2*paymentAmt)
|
|
|
|
|
|
|
|
// Alice sent 140k to Bob.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Bob(remote)", net.Alice,
|
2017-12-19 02:18:40 +03:00
|
|
|
aliceFundPoint, 2*paymentAmt, int64(0))
|
|
|
|
|
|
|
|
// Alice received 70k + fee from Dave.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Alice(remote)", net.Alice,
|
2017-12-19 02:18:40 +03:00
|
|
|
daveFundPoint, int64(0), paymentAmt+baseFee)
|
|
|
|
|
|
|
|
// Dave sent 70k+fee to Alice.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Alice(remote)", dave,
|
2017-12-19 02:18:40 +03:00
|
|
|
daveFundPoint, paymentAmt+baseFee, int64(0))
|
|
|
|
|
|
|
|
// Dave received 70k+fee of two hops from Carol.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Carol(local) => Dave(remote)", dave,
|
2017-12-19 02:18:40 +03:00
|
|
|
carolFundPoint, int64(0), paymentAmt+baseFee*2)
|
|
|
|
|
|
|
|
// Carol sent 70k+fee of two hops to Dave.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Carol(local) => Dave(remote)", carol,
|
2017-12-19 02:18:40 +03:00
|
|
|
carolFundPoint, paymentAmt+baseFee*2, int64(0))
|
|
|
|
|
|
|
|
// Alice received 70k+fee from Carol.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Carol(local) [private=>] Alice(remote)",
|
2017-12-19 02:18:40 +03:00
|
|
|
net.Alice, privateFundPoint, int64(0), paymentAmt+baseFee)
|
|
|
|
|
|
|
|
// Carol sent 70k+fee to Alice.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Carol(local) [private=>] Alice(remote)",
|
2017-12-19 02:18:40 +03:00
|
|
|
carol, privateFundPoint, paymentAmt+baseFee, int64(0))
|
|
|
|
|
|
|
|
// Alice should also be able to route payments using this channel,
|
|
|
|
// so send two payments of 60k back to Carol.
|
|
|
|
const paymentAmt60k = 60000
|
2018-11-20 17:09:44 +03:00
|
|
|
payReqs, _, _, err = createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
carol, paymentAmt60k, numPayments,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
|
2017-12-19 02:18:40 +03:00
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
|
|
|
// Let Bob pay the invoices.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-12-19 02:18:40 +03:00
|
|
|
err = completePaymentRequests(ctxt, net.Alice, payReqs, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we make sure Dave and Bob does not know about the
|
|
|
|
// private channel between Carol and Alice. We first mine
|
|
|
|
// plenty of blocks, such that the channel would have been
|
2018-02-07 06:11:11 +03:00
|
|
|
// announced in case it was public.
|
2018-11-29 14:33:11 +03:00
|
|
|
mineBlocks(t, net, 10, 0)
|
2017-12-19 02:18:40 +03:00
|
|
|
|
|
|
|
// We create a helper method to check how many edges each of the
|
|
|
|
// nodes know about. Carol and Alice should know about 4, while
|
|
|
|
// Bob and Dave should only know about 3, since one channel is
|
|
|
|
// private.
|
2018-09-26 18:30:09 +03:00
|
|
|
numChannels := func(node *lntest.HarnessNode, includeUnannounced bool) int {
|
|
|
|
req := &lnrpc.ChannelGraphRequest{
|
|
|
|
IncludeUnannounced: includeUnannounced,
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2017-12-19 02:18:40 +03:00
|
|
|
chanGraph, err := node.DescribeGraph(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable go describegraph: %v", err)
|
|
|
|
}
|
|
|
|
return len(chanGraph.Edges)
|
|
|
|
}
|
|
|
|
|
2018-04-15 19:55:55 +03:00
|
|
|
var predErr error
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-09-26 18:30:09 +03:00
|
|
|
aliceChans := numChannels(net.Alice, true)
|
2018-04-15 19:55:55 +03:00
|
|
|
if aliceChans != 4 {
|
|
|
|
predErr = fmt.Errorf("expected Alice to know 4 edges, "+
|
|
|
|
"had %v", aliceChans)
|
|
|
|
return false
|
|
|
|
}
|
2018-09-26 18:30:09 +03:00
|
|
|
alicePubChans := numChannels(net.Alice, false)
|
|
|
|
if alicePubChans != 3 {
|
|
|
|
predErr = fmt.Errorf("expected Alice to know 3 public edges, "+
|
|
|
|
"had %v", alicePubChans)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
bobChans := numChannels(net.Bob, true)
|
2018-04-15 19:55:55 +03:00
|
|
|
if bobChans != 3 {
|
|
|
|
predErr = fmt.Errorf("expected Bob to know 3 edges, "+
|
|
|
|
"had %v", bobChans)
|
|
|
|
return false
|
|
|
|
}
|
2018-09-26 18:30:09 +03:00
|
|
|
carolChans := numChannels(carol, true)
|
2018-04-15 19:55:55 +03:00
|
|
|
if carolChans != 4 {
|
|
|
|
predErr = fmt.Errorf("expected Carol to know 4 edges, "+
|
|
|
|
"had %v", carolChans)
|
|
|
|
return false
|
|
|
|
}
|
2018-09-26 18:30:09 +03:00
|
|
|
carolPubChans := numChannels(carol, false)
|
|
|
|
if carolPubChans != 3 {
|
|
|
|
predErr = fmt.Errorf("expected Carol to know 3 public edges, "+
|
|
|
|
"had %v", carolPubChans)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
daveChans := numChannels(dave, true)
|
2018-04-15 19:55:55 +03:00
|
|
|
if daveChans != 3 {
|
|
|
|
predErr = fmt.Errorf("expected Dave to know 3 edges, "+
|
|
|
|
"had %v", daveChans)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", predErr)
|
2017-12-19 02:18:40 +03:00
|
|
|
}
|
2016-09-26 20:41:57 +03:00
|
|
|
|
2017-12-19 02:18:40 +03:00
|
|
|
// Close all channels.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-10-25 04:33:55 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, dave, chanPointDave, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPointCarol, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-12-19 02:18:40 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPointPrivate, false)
|
2016-09-26 20:41:57 +03:00
|
|
|
}
|
|
|
|
|
2018-04-21 18:37:38 +03:00
|
|
|
// testInvoiceRoutingHints tests that the routing hints for an invoice are
|
|
|
|
// created properly.
|
|
|
|
func testInvoiceRoutingHints(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
2018-11-28 11:25:46 +03:00
|
|
|
|
2018-04-21 18:37:38 +03:00
|
|
|
const chanAmt = btcutil.Amount(100000)
|
|
|
|
|
2018-09-06 11:48:46 +03:00
|
|
|
// Throughout this test, we'll be opening a channel between Alice and
|
2018-04-21 18:37:38 +03:00
|
|
|
// several other parties.
|
|
|
|
//
|
|
|
|
// First, we'll create a private channel between Alice and Bob. This
|
|
|
|
// will be the only channel that will be considered as a routing hint
|
|
|
|
// throughout this test. We'll include a push amount since we currently
|
|
|
|
// require channels to have enough remote balance to cover the invoice's
|
|
|
|
// payment.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-04-21 18:37:38 +03:00
|
|
|
chanPointBob := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: chanAmt / 2,
|
|
|
|
Private: true,
|
|
|
|
},
|
2018-04-21 18:37:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Then, we'll create Carol's node and open a public channel between her
|
|
|
|
// and Alice. This channel will not be considered as a routing hint due
|
|
|
|
// to it being public.
|
2018-04-27 11:42:04 +03:00
|
|
|
carol, err := net.NewNode("Carol", nil)
|
2018-04-21 18:37:38 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create carol's node: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Alice, carol); err != nil {
|
2018-04-21 18:37:38 +03:00
|
|
|
t.Fatalf("unable to connect alice to carol: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-04-21 18:37:38 +03:00
|
|
|
chanPointCarol := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: chanAmt / 2,
|
|
|
|
},
|
2018-04-21 18:37:38 +03:00
|
|
|
)
|
|
|
|
|
2018-10-18 02:14:31 +03:00
|
|
|
// We'll also create a public channel between Bob and Carol to ensure
|
|
|
|
// that Bob gets selected as the only routing hint. We do this as
|
|
|
|
// we should only include routing hints for nodes that are publicly
|
|
|
|
// advertised, otherwise we'd end up leaking information about nodes
|
|
|
|
// that wish to stay unadvertised.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Bob, carol); err != nil {
|
2018-10-18 02:14:31 +03:00
|
|
|
t.Fatalf("unable to connect alice to carol: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-10-18 02:14:31 +03:00
|
|
|
chanPointBobCarol := openChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Bob, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: chanAmt / 2,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2018-04-21 18:37:38 +03:00
|
|
|
// Then, we'll create Dave's node and open a private channel between him
|
|
|
|
// and Alice. We will not include a push amount in order to not consider
|
|
|
|
// this channel as a routing hint as it will not have enough remote
|
|
|
|
// balance for the invoice's amount.
|
2018-04-27 11:42:04 +03:00
|
|
|
dave, err := net.NewNode("Dave", nil)
|
2018-04-21 18:37:38 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create dave's node: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Alice, dave); err != nil {
|
2018-04-21 18:37:38 +03:00
|
|
|
t.Fatalf("unable to connect alice to dave: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-04-21 18:37:38 +03:00
|
|
|
chanPointDave := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, dave,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
Private: true,
|
|
|
|
},
|
2018-04-21 18:37:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Finally, we'll create Eve's node and open a private channel between
|
|
|
|
// her and Alice. This time though, we'll take Eve's node down after the
|
|
|
|
// channel has been created to avoid populating routing hints for
|
|
|
|
// inactive channels.
|
2018-04-27 11:42:04 +03:00
|
|
|
eve, err := net.NewNode("Eve", nil)
|
2018-04-21 18:37:38 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create eve's node: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Alice, eve); err != nil {
|
2018-04-21 18:37:38 +03:00
|
|
|
t.Fatalf("unable to connect alice to eve: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-04-21 18:37:38 +03:00
|
|
|
chanPointEve := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, eve,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: chanAmt / 2,
|
|
|
|
Private: true,
|
|
|
|
},
|
2018-04-21 18:37:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Make sure all the channels have been opened.
|
|
|
|
nodeNames := []string{"bob", "carol", "dave", "eve"}
|
|
|
|
aliceChans := []*lnrpc.ChannelPoint{
|
2018-10-18 02:14:31 +03:00
|
|
|
chanPointBob, chanPointCarol, chanPointBobCarol, chanPointDave,
|
|
|
|
chanPointEve,
|
2018-04-21 18:37:38 +03:00
|
|
|
}
|
|
|
|
for i, chanPoint := range aliceChans {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-21 18:37:38 +03:00
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("timed out waiting for channel open between "+
|
|
|
|
"alice and %s: %v", nodeNames[i], err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the channels are open, we'll take down Eve's node.
|
2018-06-08 00:14:39 +03:00
|
|
|
shutdownAndAssert(net, t, eve)
|
2018-04-21 18:37:38 +03:00
|
|
|
|
|
|
|
// Create an invoice for Alice that will populate the routing hints.
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "routing hints",
|
|
|
|
Value: int64(chanAmt / 4),
|
|
|
|
Private: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Due to the way the channels were set up above, the channel between
|
|
|
|
// Alice and Bob should be the only channel used as a routing hint.
|
2018-05-04 07:07:06 +03:00
|
|
|
var predErr error
|
2018-05-09 10:20:40 +03:00
|
|
|
var decoded *lnrpc.PayReq
|
2018-05-04 07:07:06 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := net.Alice.AddInvoice(ctxt, invoice)
|
2018-05-09 10:20:40 +03:00
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to add invoice: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll decode the invoice's payment request to determine which
|
|
|
|
// channels were used as routing hints.
|
2018-07-31 11:29:12 +03:00
|
|
|
payReq := &lnrpc.PayReqString{
|
|
|
|
PayReq: resp.PaymentRequest,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
decoded, err = net.Alice.DecodePayReq(ctxt, payReq)
|
2018-05-09 10:20:40 +03:00
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to decode payment "+
|
|
|
|
"request: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-05-04 07:07:06 +03:00
|
|
|
if len(decoded.RouteHints) != 1 {
|
|
|
|
predErr = fmt.Errorf("expected one route hint, got %d",
|
|
|
|
len(decoded.RouteHints))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(predErr.Error())
|
2018-04-21 18:37:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
hops := decoded.RouteHints[0].HopHints
|
|
|
|
if len(hops) != 1 {
|
|
|
|
t.Fatalf("expected one hop in route hint, got %d", len(hops))
|
|
|
|
}
|
|
|
|
chanID := hops[0].ChanId
|
|
|
|
|
|
|
|
// We'll need the short channel ID of the channel between Alice and Bob
|
|
|
|
// to make sure the routing hint is for this channel.
|
|
|
|
listReq := &lnrpc.ListChannelsRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
listResp, err := net.Alice.ListChannels(ctxt, listReq)
|
2018-04-21 18:37:38 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to retrieve alice's channels: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var aliceBobChanID uint64
|
|
|
|
for _, channel := range listResp.Channels {
|
|
|
|
if channel.RemotePubkey == net.Bob.PubKeyStr {
|
|
|
|
aliceBobChanID = channel.ChanId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if aliceBobChanID == 0 {
|
|
|
|
t.Fatalf("channel between alice and bob not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if chanID != aliceBobChanID {
|
|
|
|
t.Fatalf("expected channel ID %d, got %d", aliceBobChanID,
|
|
|
|
chanID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we've confirmed the routing hints were added correctly, we
|
|
|
|
// can close all the channels and shut down all the nodes created.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-04-21 18:37:38 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointBob, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-04-21 18:37:38 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointCarol, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-10-18 02:14:31 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Bob, chanPointBobCarol, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-04-21 18:37:38 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointDave, false)
|
|
|
|
|
|
|
|
// The channel between Alice and Eve should be force closed since Eve
|
|
|
|
// is offline.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-04-21 18:37:38 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointEve, true)
|
2018-11-06 13:03:01 +03:00
|
|
|
|
|
|
|
// Cleanup by mining the force close and sweep transaction.
|
|
|
|
cleanupForceClose(t, net, net.Alice, chanPointEve)
|
2018-04-21 18:37:38 +03:00
|
|
|
}
|
|
|
|
|
2018-03-15 20:02:52 +03:00
|
|
|
// testMultiHopOverPrivateChannels tests that private channels can be used as
|
|
|
|
// intermediate hops in a route for payments.
|
|
|
|
func testMultiHopOverPrivateChannels(net *lntest.NetworkHarness, t *harnessTest) {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2018-03-15 20:02:52 +03:00
|
|
|
// We'll test that multi-hop payments over private channels work as
|
|
|
|
// intended. To do so, we'll create the following topology:
|
|
|
|
// private public private
|
|
|
|
// Alice <--100k--> Bob <--100k--> Carol <--100k--> Dave
|
|
|
|
const chanAmt = btcutil.Amount(100000)
|
|
|
|
|
|
|
|
// First, we'll open a private channel between Alice and Bob with Alice
|
|
|
|
// being the funder.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:52 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
Private: true,
|
|
|
|
},
|
2018-03-15 20:02:52 +03:00
|
|
|
)
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-28 11:25:46 +03:00
|
|
|
err := net.Alice.WaitForNetworkChannelOpen(ctxt, chanPointAlice)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't see the channel alice <-> bob before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-28 11:25:46 +03:00
|
|
|
err = net.Bob.WaitForNetworkChannelOpen(ctxt, chanPointAlice)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't see the channel alice <-> bob before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve Alice's funding outpoint.
|
2019-05-15 10:02:53 +03:00
|
|
|
aliceChanTXID, err := lnd.GetChanPointFundingTxid(chanPointAlice)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
aliceFundPoint := wire.OutPoint{
|
|
|
|
Hash: *aliceChanTXID,
|
|
|
|
Index: chanPointAlice.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll create Carol's node and open a public channel between
|
|
|
|
// her and Bob with Bob being the funder.
|
2018-04-27 11:42:04 +03:00
|
|
|
carol, err := net.NewNode("Carol", nil)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create carol's node: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Bob, carol); err != nil {
|
2018-03-15 20:02:52 +03:00
|
|
|
t.Fatalf("unable to connect bob to carol: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:52 +03:00
|
|
|
chanPointBob := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Bob, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:52 +03:00
|
|
|
)
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-28 11:25:46 +03:00
|
|
|
err = net.Bob.WaitForNetworkChannelOpen(ctxt, chanPointBob)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't see the channel bob <-> carol before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-28 11:25:46 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPointBob)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("carol didn't see the channel bob <-> carol before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-28 11:25:46 +03:00
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPointBob)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't see the channel bob <-> carol before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve Bob's funding outpoint.
|
2019-05-15 10:02:53 +03:00
|
|
|
bobChanTXID, err := lnd.GetChanPointFundingTxid(chanPointBob)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
bobFundPoint := wire.OutPoint{
|
|
|
|
Hash: *bobChanTXID,
|
|
|
|
Index: chanPointBob.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll create Dave's node and open a private channel between him
|
|
|
|
// and Carol with Carol being the funder.
|
2018-04-27 11:42:04 +03:00
|
|
|
dave, err := net.NewNode("Dave", nil)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create dave's node: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, dave); err != nil {
|
2018-03-15 20:02:52 +03:00
|
|
|
t.Fatalf("unable to connect carol to dave: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:52 +03:00
|
|
|
chanPointCarol := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, carol, dave,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
Private: true,
|
|
|
|
},
|
2018-03-15 20:02:52 +03:00
|
|
|
)
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-28 11:25:46 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPointCarol)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("carol didn't see the channel carol <-> dave before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-28 11:25:46 +03:00
|
|
|
err = dave.WaitForNetworkChannelOpen(ctxt, chanPointCarol)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("dave didn't see the channel carol <-> dave before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-28 11:25:46 +03:00
|
|
|
err = dave.WaitForNetworkChannelOpen(ctxt, chanPointBob)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("dave didn't see the channel bob <-> carol before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve Carol's funding point.
|
2019-05-15 10:02:53 +03:00
|
|
|
carolChanTXID, err := lnd.GetChanPointFundingTxid(chanPointCarol)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
carolFundPoint := wire.OutPoint{
|
|
|
|
Hash: *carolChanTXID,
|
|
|
|
Index: chanPointCarol.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that all the channels are set up according to the topology from
|
|
|
|
// above, we can proceed to test payments. We'll create an invoice for
|
|
|
|
// Dave of 20k satoshis and pay it with Alice. Since there is no public
|
|
|
|
// route from Alice to Dave, we'll need to use the private channel
|
|
|
|
// between Carol and Dave as a routing hint encoded in the invoice.
|
|
|
|
const paymentAmt = 20000
|
|
|
|
|
|
|
|
// Create the invoice for Dave.
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "two hopz!",
|
|
|
|
Value: paymentAmt,
|
|
|
|
Private: true,
|
|
|
|
}
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := dave.AddInvoice(ctxt, invoice)
|
2018-03-15 20:02:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice for dave: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let Alice pay the invoice.
|
|
|
|
payReqs := []string{resp.PaymentRequest}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-03-15 20:02:52 +03:00
|
|
|
err = completePaymentRequests(ctxt, net.Alice, payReqs, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments from alice to dave: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// When asserting the amount of satoshis moved, we'll factor in the
|
|
|
|
// default base fee, as we didn't modify the fee structure when opening
|
|
|
|
// the channels.
|
|
|
|
const baseFee = 1
|
|
|
|
|
|
|
|
// Dave should have received 20k satoshis from Carol.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Carol(local) [private=>] Dave(remote)",
|
2018-03-15 20:02:52 +03:00
|
|
|
dave, carolFundPoint, 0, paymentAmt)
|
|
|
|
|
|
|
|
// Carol should have sent 20k satoshis to Dave.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Carol(local) [private=>] Dave(remote)",
|
2018-03-15 20:02:52 +03:00
|
|
|
carol, carolFundPoint, paymentAmt, 0)
|
|
|
|
|
|
|
|
// Carol should have received 20k satoshis + fee for one hop from Bob.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Carol(remote)",
|
2018-03-15 20:02:52 +03:00
|
|
|
carol, bobFundPoint, 0, paymentAmt+baseFee)
|
|
|
|
|
|
|
|
// Bob should have sent 20k satoshis + fee for one hop to Carol.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Carol(remote)",
|
2018-03-15 20:02:52 +03:00
|
|
|
net.Bob, bobFundPoint, paymentAmt+baseFee, 0)
|
|
|
|
|
|
|
|
// Bob should have received 20k satoshis + fee for two hops from Alice.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) [private=>] Bob(remote)", net.Bob,
|
2018-03-15 20:02:52 +03:00
|
|
|
aliceFundPoint, 0, paymentAmt+baseFee*2)
|
|
|
|
|
|
|
|
// Alice should have sent 20k satoshis + fee for two hops to Bob.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) [private=>] Bob(remote)", net.Alice,
|
2018-03-15 20:02:52 +03:00
|
|
|
aliceFundPoint, paymentAmt+baseFee*2, 0)
|
|
|
|
|
|
|
|
// At this point, the payment was successful. We can now close all the
|
|
|
|
// channels and shutdown the nodes created throughout this test.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-03-15 20:02:52 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-03-15 20:02:52 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Bob, chanPointBob, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-03-15 20:02:52 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPointCarol, false)
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
func testInvoiceSubscriptions(net *lntest.NetworkHarness, t *harnessTest) {
|
2016-10-16 00:45:26 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
const chanAmt = btcutil.Amount(500000)
|
|
|
|
|
2016-10-16 00:45:26 +03:00
|
|
|
// Open a channel with 500k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
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
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
2018-08-12 16:23:08 +03:00
|
|
|
RPreimage: makeFakePayHash(t),
|
2016-10-16 00:45:26 +03:00
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
invoiceResp, err := net.Bob.AddInvoice(ctxt, invoice)
|
2016-10-16 00:45:26 +03:00
|
|
|
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
|
|
|
}
|
2018-06-30 04:07:55 +03:00
|
|
|
lastAddIndex := invoiceResp.AddIndex
|
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{}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctx, cancelInvoiceSubscription := context.WithCancel(ctxb)
|
2018-04-25 06:34:56 +03:00
|
|
|
bobInvoiceSubscription, err := net.Bob.SubscribeInvoices(ctx, req)
|
2016-10-16 00:45:26 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-06-30 04:07:55 +03:00
|
|
|
var settleIndex uint64
|
2017-12-14 03:14:24 +03:00
|
|
|
quit := make(chan struct{})
|
2016-10-16 00:45:26 +03:00
|
|
|
updateSent := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
invoiceUpdate, err := bobInvoiceSubscription.Recv()
|
2017-12-14 03:14:24 +03:00
|
|
|
select {
|
|
|
|
case <-quit:
|
|
|
|
// Received cancellation
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-10-16 00:45:26 +03:00
|
|
|
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
|
2017-12-05 09:18:55 +03:00
|
|
|
// above, but should now be settled and have SettleDate
|
2016-10-16 00:45:26 +03:00
|
|
|
if !invoiceUpdate.Settled {
|
2018-02-07 06:11:11 +03:00
|
|
|
t.Fatalf("invoice not settled but should be")
|
2016-10-16 00:45:26 +03:00
|
|
|
}
|
2017-12-05 09:18:55 +03:00
|
|
|
if invoiceUpdate.SettleDate == 0 {
|
|
|
|
t.Fatalf("invoice should have non zero settle date, but doesn't")
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-06-30 04:07:55 +03:00
|
|
|
if invoiceUpdate.SettleIndex == 0 {
|
|
|
|
t.Fatalf("invoice should have settle index")
|
|
|
|
}
|
|
|
|
|
|
|
|
settleIndex = invoiceUpdate.SettleIndex
|
|
|
|
|
2016-10-16 00:45:26 +03:00
|
|
|
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.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-03-15 06:06:33 +03:00
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(roasbeef): will need to make num blocks to advertise a
|
|
|
|
// node param
|
2017-12-14 03:14:24 +03:00
|
|
|
close(quit)
|
2017-03-15 06:06:33 +03:00
|
|
|
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.
|
|
|
|
sendReq := &lnrpc.SendRequest{
|
2017-10-23 04:35:38 +03:00
|
|
|
PaymentRequest: invoiceResp.PaymentRequest,
|
2016-10-16 00:45:26 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-10-26 01:32:00 +03:00
|
|
|
resp, err := net.Alice.SendPaymentSync(ctxt, sendReq)
|
|
|
|
if err != nil {
|
2017-12-14 03:14:24 +03:00
|
|
|
close(quit)
|
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-10-26 01:32:00 +03:00
|
|
|
if resp.PaymentError != "" {
|
2017-12-14 03:14:24 +03:00
|
|
|
close(quit)
|
2017-10-26 01:32:00 +03:00
|
|
|
t.Fatalf("error when attempting recv: %v", resp.PaymentError)
|
2016-10-16 00:45:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2017-12-14 03:14:24 +03:00
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
close(quit)
|
|
|
|
t.Fatalf("update not sent after 10 seconds")
|
2016-10-16 00:45:26 +03:00
|
|
|
case <-updateSent: // Fall through on success
|
|
|
|
}
|
|
|
|
|
2018-06-30 04:07:55 +03:00
|
|
|
// With the base case working, we'll now cancel Bob's current
|
|
|
|
// subscription in order to exercise the backlog fill behavior.
|
|
|
|
cancelInvoiceSubscription()
|
|
|
|
|
|
|
|
// We'll now add 3 more invoices to Bob's invoice registry.
|
|
|
|
const numInvoices = 3
|
2018-11-20 17:09:44 +03:00
|
|
|
payReqs, _, newInvoices, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
net.Bob, paymentAmt, numInvoices,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2018-06-30 04:07:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the set of invoices has been added, we'll re-register for
|
|
|
|
// streaming invoice notifications for Bob, this time specifying the
|
|
|
|
// add invoice of the last prior invoice.
|
|
|
|
req = &lnrpc.InvoiceSubscription{
|
|
|
|
AddIndex: lastAddIndex,
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctx, cancelInvoiceSubscription = context.WithCancel(ctxb)
|
2018-06-30 04:07:55 +03:00
|
|
|
bobInvoiceSubscription, err = net.Bob.SubscribeInvoices(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to subscribe to bob's invoice updates: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we specified a value of the prior add index above, we should
|
|
|
|
// now immediately get the invoices we just added as we should get the
|
|
|
|
// backlog of notifications.
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
invoiceUpdate, err := bobInvoiceSubscription.Recv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to receive subscription")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should now get the ith invoice we added, as they should
|
|
|
|
// be returned in order.
|
|
|
|
if invoiceUpdate.Settled {
|
|
|
|
t.Fatalf("should have only received add events")
|
|
|
|
}
|
|
|
|
originalInvoice := newInvoices[i]
|
|
|
|
rHash := sha256.Sum256(originalInvoice.RPreimage[:])
|
|
|
|
if !bytes.Equal(invoiceUpdate.RHash, rHash[:]) {
|
|
|
|
t.Fatalf("invoices have mismatched payment hashes: "+
|
|
|
|
"expected %x, got %x", rHash[:],
|
|
|
|
invoiceUpdate.RHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelInvoiceSubscription()
|
|
|
|
|
|
|
|
// We'll now have Bob settle out the remainder of these invoices so we
|
|
|
|
// can test that all settled invoices are properly notified.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-06-30 04:07:55 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, net.Alice, payReqs, true,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the set of invoices paid, we'll now cancel the old
|
|
|
|
// subscription, and create a new one for Bob, this time using the
|
|
|
|
// settle index to obtain the backlog of settled invoices.
|
|
|
|
req = &lnrpc.InvoiceSubscription{
|
|
|
|
SettleIndex: settleIndex,
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctx, cancelInvoiceSubscription = context.WithCancel(ctxb)
|
2018-06-30 04:07:55 +03:00
|
|
|
bobInvoiceSubscription, err = net.Bob.SubscribeInvoices(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to subscribe to bob's invoice updates: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer cancelInvoiceSubscription()
|
|
|
|
|
|
|
|
// As we specified the index of the past settle index, we should now
|
|
|
|
// receive notifications for the three HTLCs that we just settled. As
|
|
|
|
// the order that the HTLCs will be settled in is partially randomized,
|
|
|
|
// we'll use a map to assert that the proper set has been settled.
|
|
|
|
settledInvoices := make(map[[32]byte]struct{})
|
|
|
|
for _, invoice := range newInvoices {
|
|
|
|
rHash := sha256.Sum256(invoice.RPreimage[:])
|
|
|
|
settledInvoices[rHash] = struct{}{}
|
|
|
|
}
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
invoiceUpdate, err := bobInvoiceSubscription.Recv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to receive subscription")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should now get the ith invoice we added, as they should
|
|
|
|
// be returned in order.
|
|
|
|
if !invoiceUpdate.Settled {
|
|
|
|
t.Fatalf("should have only received settle events")
|
|
|
|
}
|
|
|
|
|
|
|
|
var rHash [32]byte
|
|
|
|
copy(rHash[:], invoiceUpdate.RHash)
|
|
|
|
if _, ok := settledInvoices[rHash]; !ok {
|
|
|
|
t.Fatalf("unknown invoice settled: %x", rHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(settledInvoices, rHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, all the invoices should be fully settled.
|
|
|
|
if len(settledInvoices) != 0 {
|
|
|
|
t.Fatalf("not all invoices settled")
|
|
|
|
}
|
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
2016-10-16 00:45:26 +03:00
|
|
|
}
|
|
|
|
|
2018-11-02 06:20:21 +03:00
|
|
|
// channelSubscription houses the proxied update and error chans for a node's
|
|
|
|
// channel subscriptions.
|
|
|
|
type channelSubscription struct {
|
|
|
|
updateChan chan *lnrpc.ChannelEventUpdate
|
|
|
|
errChan chan error
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// subscribeChannelNotifications subscribes to channel updates and launches a
|
|
|
|
// goroutine that forwards these to the returned channel.
|
|
|
|
func subscribeChannelNotifications(ctxb context.Context, t *harnessTest,
|
|
|
|
node *lntest.HarnessNode) channelSubscription {
|
|
|
|
|
|
|
|
// We'll first start by establishing a notification client which will
|
|
|
|
// send us notifications upon channels becoming active, inactive or
|
|
|
|
// closed.
|
|
|
|
req := &lnrpc.ChannelEventSubscription{}
|
|
|
|
ctx, cancelFunc := context.WithCancel(ctxb)
|
|
|
|
|
|
|
|
chanUpdateClient, err := node.SubscribeChannelEvents(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel update client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll launch a goroutine that will be responsible for proxying all
|
|
|
|
// notifications recv'd from the client into the channel below.
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
quit := make(chan struct{})
|
|
|
|
chanUpdates := make(chan *lnrpc.ChannelEventUpdate, 20)
|
|
|
|
go func() {
|
|
|
|
defer cancelFunc()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
chanUpdate, err := chanUpdateClient.Recv()
|
|
|
|
select {
|
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
select {
|
|
|
|
case errChan <- err:
|
|
|
|
case <-quit:
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case chanUpdates <- chanUpdate:
|
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return channelSubscription{
|
|
|
|
updateChan: chanUpdates,
|
|
|
|
errChan: errChan,
|
|
|
|
quit: quit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyCloseUpdate is used to verify that a closed channel update is of the
|
|
|
|
// expected type.
|
|
|
|
func verifyCloseUpdate(chanUpdate *lnrpc.ChannelEventUpdate,
|
|
|
|
force bool, forceType lnrpc.ChannelCloseSummary_ClosureType) error {
|
|
|
|
|
|
|
|
// We should receive one inactive and one closed notification
|
|
|
|
// for each channel.
|
|
|
|
switch update := chanUpdate.Channel.(type) {
|
|
|
|
case *lnrpc.ChannelEventUpdate_InactiveChannel:
|
|
|
|
if chanUpdate.Type != lnrpc.ChannelEventUpdate_INACTIVE_CHANNEL {
|
|
|
|
return fmt.Errorf("update type mismatch: expected %v, got %v",
|
|
|
|
lnrpc.ChannelEventUpdate_INACTIVE_CHANNEL,
|
|
|
|
chanUpdate.Type)
|
|
|
|
}
|
|
|
|
case *lnrpc.ChannelEventUpdate_ClosedChannel:
|
|
|
|
if chanUpdate.Type !=
|
|
|
|
lnrpc.ChannelEventUpdate_CLOSED_CHANNEL {
|
|
|
|
return fmt.Errorf("update type mismatch: expected %v, got %v",
|
|
|
|
lnrpc.ChannelEventUpdate_CLOSED_CHANNEL,
|
|
|
|
chanUpdate.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch force {
|
|
|
|
case true:
|
|
|
|
if update.ClosedChannel.CloseType != forceType {
|
|
|
|
return fmt.Errorf("channel closure type mismatch: "+
|
|
|
|
"expected %v, got %v",
|
|
|
|
forceType,
|
|
|
|
update.ClosedChannel.CloseType)
|
|
|
|
}
|
|
|
|
case false:
|
|
|
|
if update.ClosedChannel.CloseType !=
|
|
|
|
lnrpc.ChannelCloseSummary_COOPERATIVE_CLOSE {
|
|
|
|
return fmt.Errorf("channel closure type "+
|
|
|
|
"mismatch: expected %v, got %v",
|
|
|
|
lnrpc.ChannelCloseSummary_COOPERATIVE_CLOSE,
|
|
|
|
update.ClosedChannel.CloseType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("channel update channel of wrong type, "+
|
|
|
|
"expected closed channel, got %T",
|
|
|
|
update)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
|
2018-11-02 06:20:21 +03:00
|
|
|
// testBasicChannelCreationAndUpdates tests multiple channel opening and closing,
|
|
|
|
// and ensures that if a node is subscribed to channel updates they will be
|
|
|
|
// received correctly for both cooperative and force closed channels.
|
|
|
|
func testBasicChannelCreationAndUpdates(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
2016-10-24 05:00:09 +03:00
|
|
|
const (
|
|
|
|
numChannels = 2
|
2019-05-15 10:02:53 +03:00
|
|
|
amount = lnd.MaxBtcFundingAmount
|
2016-10-24 05:00:09 +03:00
|
|
|
)
|
2016-10-17 07:36:43 +03:00
|
|
|
|
2018-11-02 06:20:21 +03:00
|
|
|
// Let Bob subscribe to channel notifications.
|
|
|
|
bobChanSub := subscribeChannelNotifications(ctxb, t, net.Bob)
|
|
|
|
defer close(bobChanSub.quit)
|
|
|
|
|
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++ {
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoints[i] = openChannelAndAssert(
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
2018-08-22 10:32:42 +03:00
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: amount,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2016-10-17 07:36:43 +03:00
|
|
|
}
|
|
|
|
|
2018-11-02 06:20:21 +03:00
|
|
|
// Since each of the channels just became open, Bob should we receive an
|
|
|
|
// open and an active notification for each channel.
|
|
|
|
var numChannelUpds int
|
|
|
|
for numChannelUpds < 2*numChannels {
|
|
|
|
select {
|
|
|
|
case update := <-bobChanSub.updateChan:
|
|
|
|
switch update.Type {
|
|
|
|
case lnrpc.ChannelEventUpdate_ACTIVE_CHANNEL:
|
|
|
|
case lnrpc.ChannelEventUpdate_OPEN_CHANNEL:
|
|
|
|
default:
|
|
|
|
t.Fatalf("update type mismatch: expected open or active "+
|
|
|
|
"channel notification, got: %v", update.Type)
|
|
|
|
}
|
|
|
|
numChannelUpds++
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("timeout waiting for channel notifications, "+
|
|
|
|
"only received %d/%d chanupds", numChannelUpds,
|
|
|
|
numChannels)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe Alice to channel updates so we can test that both remote
|
|
|
|
// and local force close notifications are received correctly.
|
|
|
|
aliceChanSub := subscribeChannelNotifications(ctxb, t, net.Alice)
|
|
|
|
defer close(aliceChanSub.quit)
|
|
|
|
|
|
|
|
// Close the channel between Alice and Bob, asserting that the channel
|
|
|
|
// has been properly closed on-chain.
|
|
|
|
for i, chanPoint := range chanPoints {
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), defaultTimeout)
|
|
|
|
|
|
|
|
// Force close half of the channels.
|
|
|
|
force := i%2 == 0
|
|
|
|
closeChannelAndAssert(ctx, t, net, net.Alice, chanPoint, force)
|
|
|
|
if force {
|
|
|
|
cleanupForceClose(t, net, net.Alice, chanPoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyCloseUpdatesReceived is used to verify that Alice and Bob
|
|
|
|
// receive the correct channel updates in order.
|
|
|
|
verifyCloseUpdatesReceived := func(sub channelSubscription,
|
|
|
|
forceType lnrpc.ChannelCloseSummary_ClosureType) error {
|
|
|
|
|
|
|
|
// Ensure one inactive and one closed notification is received for each
|
|
|
|
// closed channel.
|
|
|
|
numChannelUpds := 0
|
|
|
|
for numChannelUpds < 2*numChannels {
|
|
|
|
// Every other channel should be force closed.
|
|
|
|
force := (numChannelUpds/2)%2 == 0
|
|
|
|
|
|
|
|
select {
|
|
|
|
case chanUpdate := <-sub.updateChan:
|
|
|
|
err := verifyCloseUpdate(chanUpdate, force, forceType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
numChannelUpds++
|
|
|
|
case err := <-sub.errChan:
|
|
|
|
return err
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
return fmt.Errorf("timeout waiting for channel "+
|
|
|
|
"notifications, only received %d/%d "+
|
|
|
|
"chanupds", numChannelUpds, 2*numChannels)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify Bob receives all closed channel notifications. He should
|
|
|
|
// receive a remote force close notification for force closed channels.
|
|
|
|
if err := verifyCloseUpdatesReceived(bobChanSub,
|
|
|
|
lnrpc.ChannelCloseSummary_REMOTE_FORCE_CLOSE); err != nil {
|
|
|
|
t.Fatalf("errored verifying close updates: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify Alice receives all closed channel notifications. She should
|
|
|
|
// receive a remote force close notification for force closed channels.
|
|
|
|
if err := verifyCloseUpdatesReceived(aliceChanSub,
|
|
|
|
lnrpc.ChannelCloseSummary_LOCAL_FORCE_CLOSE); err != nil {
|
|
|
|
t.Fatalf("errored verifying close updates: %v", err)
|
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.
|
2017-11-03 21:52:02 +03:00
|
|
|
func testMaxPendingChannels(net *lntest.NetworkHarness, t *harnessTest) {
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
maxPendingChannels := lnd.DefaultMaxPendingChannels + 1
|
|
|
|
amount := lnd.MaxBtcFundingAmount
|
2016-10-15 16:24:56 +03:00
|
|
|
|
|
|
|
// Create a new node (Carol) with greater number of max pending
|
|
|
|
// channels.
|
|
|
|
args := []string{
|
|
|
|
fmt.Sprintf("--maxpendingchannels=%v", maxPendingChannels),
|
|
|
|
}
|
2018-04-27 11:42:04 +03:00
|
|
|
carol, err := net.NewNode("Carol", args)
|
2016-10-15 16:24:56 +03:00
|
|
|
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
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
2016-10-24 05:00:09 +03:00
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-26 18:09:39 +03:00
|
|
|
if err := net.ConnectNodes(ctxt, 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
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2016-10-15 16:24:56 +03:00
|
|
|
carolBalance := btcutil.Amount(maxPendingChannels) * amount
|
2018-11-26 18:09:39 +03:00
|
|
|
if err := net.SendCoins(ctxt, 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++ {
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-08-10 07:17:06 +03:00
|
|
|
stream, err := net.OpenChannel(
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, net.Alice, carol,
|
2018-08-22 10:32:42 +03:00
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: amount,
|
|
|
|
},
|
2018-08-10 07:17:06 +03:00
|
|
|
)
|
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.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-08-10 07:17:06 +03:00
|
|
|
_, err = net.OpenChannel(
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, net.Alice, carol,
|
2018-08-22 10:32:42 +03:00
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: amount,
|
|
|
|
},
|
2018-08-10 07:17:06 +03:00
|
|
|
)
|
2018-08-22 10:32:42 +03:00
|
|
|
|
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-11-14 04:37:20 +03:00
|
|
|
// Mine 6 blocks, then wait for node's to notify us that the channel has
|
2017-03-15 06:06:33 +03:00
|
|
|
// been opened. The funding transactions should be found within the
|
2017-11-14 04:37:20 +03:00
|
|
|
// first newly mined block. 6 blocks make sure the funding transaction
|
2018-02-07 06:11:11 +03:00
|
|
|
// has enough confirmations to be announced publicly.
|
2018-11-29 14:33:11 +03:00
|
|
|
block := mineBlocks(t, net, 6, maxPendingChannels)[0]
|
2016-10-15 16:24:56 +03:00
|
|
|
|
|
|
|
chanPoints := make([]*lnrpc.ChannelPoint, maxPendingChannels)
|
|
|
|
for i, stream := range openStreams {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2016-12-08 09:38:46 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
fundingTxID, err := lnd.GetChanPointFundingTxid(fundingChanPoint)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %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)
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-03-15 06:06:33 +03:00
|
|
|
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,
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-26 18:09:39 +03:00
|
|
|
if err := net.AssertChannelExists(ctxt, 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 {
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 00:57:20 +03:00
|
|
|
// waitForTxInMempool polls until finding one transaction in the provided
|
|
|
|
// miner's mempool. An error is returned if *one* transaction isn't found within
|
|
|
|
// the given timeout.
|
2017-08-25 22:58:26 +03:00
|
|
|
func waitForTxInMempool(miner *rpcclient.Client,
|
2017-05-07 14:09:22 +03:00
|
|
|
timeout time.Duration) (*chainhash.Hash, error) {
|
|
|
|
|
2018-05-25 00:57:20 +03:00
|
|
|
txs, err := waitForNTxsInMempool(miner, 1, timeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-05-07 14:09:22 +03:00
|
|
|
}
|
2018-05-25 00:57:20 +03:00
|
|
|
|
|
|
|
return txs[0], err
|
2016-11-22 06:16:44 +03:00
|
|
|
}
|
|
|
|
|
2017-10-07 02:58:13 +03:00
|
|
|
// waitForNTxsInMempool polls until finding the desired number of transactions
|
2018-05-25 00:57:20 +03:00
|
|
|
// in the provided miner's mempool. An error is returned if this number is not
|
|
|
|
// met after the given timeout.
|
2017-10-07 02:58:13 +03:00
|
|
|
func waitForNTxsInMempool(miner *rpcclient.Client, n int,
|
|
|
|
timeout time.Duration) ([]*chainhash.Hash, error) {
|
|
|
|
|
|
|
|
breakTimeout := time.After(timeout)
|
|
|
|
ticker := time.NewTicker(50 * time.Millisecond)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var mempool []*chainhash.Hash
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-breakTimeout:
|
2018-04-13 14:18:35 +03:00
|
|
|
return nil, fmt.Errorf("wanted %v, found %v txs "+
|
2018-05-25 00:57:20 +03:00
|
|
|
"in mempool: %v", n, len(mempool), mempool)
|
2017-10-07 02:58:13 +03:00
|
|
|
case <-ticker.C:
|
|
|
|
mempool, err = miner.GetRawMempool()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mempool) == n {
|
|
|
|
return mempool, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 15:04:05 +03:00
|
|
|
// testFailingChannel tests that we will fail the channel by force closing ii
|
|
|
|
// in the case where a counterparty tries to settle an HTLC with the wrong
|
|
|
|
// preimage.
|
|
|
|
func testFailingChannel(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
2018-11-28 11:25:46 +03:00
|
|
|
|
2018-05-18 15:04:05 +03:00
|
|
|
const (
|
|
|
|
paymentAmt = 10000
|
|
|
|
)
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
chanAmt := lnd.MaxFundingAmount
|
2018-05-26 05:57:30 +03:00
|
|
|
|
2018-05-18 15:04:05 +03:00
|
|
|
// We'll introduce Carol, which will settle any incoming invoice with a
|
|
|
|
// totally unrelated preimage.
|
|
|
|
carol, err := net.NewNode("Carol",
|
|
|
|
[]string{"--debughtlc", "--hodl.bogus-settle"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
2018-05-18 15:04:05 +03:00
|
|
|
|
|
|
|
// Let Alice connect and open a channel to Carol,
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Alice, carol); err != nil {
|
2018-05-18 15:04:05 +03:00
|
|
|
t.Fatalf("unable to connect alice to carol: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-05-18 15:04:05 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-05-18 15:04:05 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// With the channel open, we'll create a invoice for Carol that Alice
|
|
|
|
// will attempt to pay.
|
|
|
|
preimage := bytes.Repeat([]byte{byte(192)}, 32)
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := carol.AddInvoice(ctxt, invoice)
|
2018-05-18 15:04:05 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
carolPayReqs := []string{resp.PaymentRequest}
|
|
|
|
|
|
|
|
// Wait for Alice to receive the channel edge from the funding manager.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-05-18 15:04:05 +03:00
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't see the alice->carol channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the payment from Alice to Carol. We expect Carol to attempt to
|
|
|
|
// settle this payment with the wrong preimage.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = completePaymentRequests(ctxt, net.Alice, carolPayReqs, false)
|
2018-05-18 15:04:05 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since Alice detects that Carol is trying to trick her by providing a
|
|
|
|
// fake preimage, she should fail and force close the channel.
|
|
|
|
var predErr error
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
pendingChanResp, err := net.Alice.PendingChannels(ctxt,
|
2018-05-18 15:04:05 +03:00
|
|
|
pendingChansRequest)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
n := len(pendingChanResp.WaitingCloseChannels)
|
|
|
|
if n != 1 {
|
|
|
|
predErr = fmt.Errorf("Expected to find %d channels "+
|
|
|
|
"waiting close, found %d", 1, n)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", predErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mine a block to confirm the broadcasted commitment.
|
2018-11-29 14:33:11 +03:00
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
2018-05-18 15:04:05 +03:00
|
|
|
if len(block.Transactions) != 2 {
|
|
|
|
t.Fatalf("transaction wasn't mined")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The channel should now show up as force closed both for Alice and
|
|
|
|
// Carol.
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
pendingChanResp, err := net.Alice.PendingChannels(ctxt,
|
2018-05-18 15:04:05 +03:00
|
|
|
pendingChansRequest)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
n := len(pendingChanResp.WaitingCloseChannels)
|
|
|
|
if n != 0 {
|
|
|
|
predErr = fmt.Errorf("Expected to find %d channels "+
|
|
|
|
"waiting close, found %d", 0, n)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
n = len(pendingChanResp.PendingForceClosingChannels)
|
|
|
|
if n != 1 {
|
|
|
|
predErr = fmt.Errorf("expected to find %d channel "+
|
|
|
|
"pending force close, found %d", 1, n)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", predErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
pendingChanResp, err := carol.PendingChannels(ctxt,
|
2018-05-18 15:04:05 +03:00
|
|
|
pendingChansRequest)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
n := len(pendingChanResp.PendingForceClosingChannels)
|
|
|
|
if n != 1 {
|
|
|
|
predErr = fmt.Errorf("expected to find %d channel "+
|
|
|
|
"pending force close, found %d", 1, n)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", predErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Carol will use the correct preimage to resolve the HTLC on-chain.
|
2018-11-27 11:12:16 +03:00
|
|
|
_, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2018-05-18 15:04:05 +03:00
|
|
|
if err != nil {
|
2018-11-28 11:25:46 +03:00
|
|
|
t.Fatalf("unable to find Carol's resolve tx in mempool: %v", err)
|
2018-05-18 15:04:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mine enough blocks for Alice to sweep her funds from the force
|
|
|
|
// closed channel.
|
|
|
|
_, err = net.Miner.Node.Generate(defaultCSV)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the sweeping tx to be broadcast.
|
2018-11-27 11:12:16 +03:00
|
|
|
_, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2018-05-18 15:04:05 +03:00
|
|
|
if err != nil {
|
2018-11-28 11:25:46 +03:00
|
|
|
t.Fatalf("unable to find Alice's sweep tx in mempool: %v", err)
|
2018-05-18 15:04:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mine the sweep.
|
|
|
|
_, err = net.Miner.Node.Generate(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// No pending channels should be left.
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
pendingChanResp, err := net.Alice.PendingChannels(ctxt,
|
2018-05-18 15:04:05 +03:00
|
|
|
pendingChansRequest)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
n := len(pendingChanResp.PendingForceClosingChannels)
|
|
|
|
if n != 0 {
|
|
|
|
predErr = fmt.Errorf("expected to find %d channel "+
|
|
|
|
"pending force close, found %d", 0, n)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", predErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 22:33:49 +03:00
|
|
|
// testGarbageCollectLinkNodes tests that we properly garbase collect link nodes
|
|
|
|
// from the database and the set of persistent connections within the server.
|
|
|
|
func testGarbageCollectLinkNodes(net *lntest.NetworkHarness, t *harnessTest) {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2018-06-15 22:33:49 +03:00
|
|
|
const (
|
|
|
|
chanAmt = 1000000
|
|
|
|
)
|
|
|
|
|
|
|
|
// Open a channel between Alice and Bob which will later be
|
|
|
|
// cooperatively closed.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-06-15 22:33:49 +03:00
|
|
|
coopChanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-06-15 22:33:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Create Carol's node and connect Alice to her.
|
|
|
|
carol, err := net.NewNode("Carol", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create carol's node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-06-15 22:33:49 +03:00
|
|
|
if err := net.ConnectNodes(ctxt, net.Alice, carol); err != nil {
|
|
|
|
t.Fatalf("unable to connect alice and carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open a channel between Alice and Carol which will later be force
|
|
|
|
// closed.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-06-15 22:33:49 +03:00
|
|
|
forceCloseChanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-06-15 22:33:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Now, create Dave's a node and also open a channel between Alice and
|
|
|
|
// him. This link will serve as the only persistent link throughout
|
|
|
|
// restarts in this test.
|
|
|
|
dave, err := net.NewNode("Dave", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create dave's node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Alice, dave); err != nil {
|
|
|
|
t.Fatalf("unable to connect alice to dave: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-06-15 22:33:49 +03:00
|
|
|
persistentChanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, dave,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-06-15 22:33:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// isConnected is a helper closure that checks if a peer is connected to
|
|
|
|
// Alice.
|
|
|
|
isConnected := func(pubKey string) bool {
|
|
|
|
req := &lnrpc.ListPeersRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := net.Alice.ListPeers(ctxt, req)
|
2018-06-15 22:33:49 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to retrieve alice's peers: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, peer := range resp.Peers {
|
|
|
|
if peer.PubKey == pubKey {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restart both Bob and Carol to ensure Alice is able to reconnect to
|
|
|
|
// them.
|
|
|
|
if err := net.RestartNode(net.Bob, nil); err != nil {
|
|
|
|
t.Fatalf("unable to restart bob's node: %v", err)
|
|
|
|
}
|
|
|
|
if err := net.RestartNode(carol, nil); err != nil {
|
|
|
|
t.Fatalf("unable to restart carol's node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
return isConnected(net.Bob.PubKeyStr)
|
|
|
|
}, 15*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice did not reconnect to bob")
|
|
|
|
}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
return isConnected(carol.PubKeyStr)
|
|
|
|
}, 15*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice did not reconnect to carol")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll also restart Alice to ensure she can reconnect to her peers
|
|
|
|
// with open channels.
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("unable to restart alice's node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
return isConnected(net.Bob.PubKeyStr)
|
|
|
|
}, 15*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice did not reconnect to bob")
|
|
|
|
}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
return isConnected(carol.PubKeyStr)
|
|
|
|
}, 15*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice did not reconnect to carol")
|
|
|
|
}
|
2018-09-11 11:25:48 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
return isConnected(dave.PubKeyStr)
|
|
|
|
}, 15*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice did not reconnect to dave")
|
|
|
|
}
|
2018-06-15 22:33:49 +03:00
|
|
|
|
|
|
|
// testReconnection is a helper closure that restarts the nodes at both
|
|
|
|
// ends of a channel to ensure they do not reconnect after restarting.
|
|
|
|
// When restarting Alice, we'll first need to ensure she has
|
|
|
|
// reestablished her connection with Dave, as they still have an open
|
|
|
|
// channel together.
|
|
|
|
testReconnection := func(node *lntest.HarnessNode) {
|
2018-09-11 11:25:48 +03:00
|
|
|
// Restart both nodes, to trigger the pruning logic.
|
|
|
|
if err := net.RestartNode(node, nil); err != nil {
|
|
|
|
t.Fatalf("unable to restart %v's node: %v",
|
|
|
|
node.Name(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("unable to restart alice's node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now restart both nodes and make sure they don't reconnect.
|
2018-06-15 22:33:49 +03:00
|
|
|
if err := net.RestartNode(node, nil); err != nil {
|
|
|
|
t.Fatalf("unable to restart %v's node: %v", node.Name(),
|
|
|
|
err)
|
|
|
|
}
|
2018-09-11 11:25:48 +03:00
|
|
|
err = lntest.WaitInvariant(func() bool {
|
2018-06-15 22:33:49 +03:00
|
|
|
return !isConnected(node.PubKeyStr)
|
2018-09-11 11:25:48 +03:00
|
|
|
}, 5*time.Second)
|
2018-06-15 22:33:49 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice reconnected to %v", node.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("unable to restart alice's node: %v", err)
|
|
|
|
}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-09-11 11:25:48 +03:00
|
|
|
return isConnected(dave.PubKeyStr)
|
2018-06-15 22:33:49 +03:00
|
|
|
}, 20*time.Second)
|
2018-09-11 11:25:48 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't reconnect to Dave")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = lntest.WaitInvariant(func() bool {
|
|
|
|
return !isConnected(node.PubKeyStr)
|
|
|
|
}, 5*time.Second)
|
2018-06-15 22:33:49 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice reconnected to %v", node.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, we'll close the channel between Alice and Bob and ensure there
|
|
|
|
// is no reconnection logic between the both once the channel is fully
|
|
|
|
// closed.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-06-15 22:33:49 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, coopChanPoint, false)
|
|
|
|
|
|
|
|
testReconnection(net.Bob)
|
|
|
|
|
|
|
|
// We'll do the same with Alice and Carol, but this time we'll force
|
|
|
|
// close the channel instead.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-06-15 22:33:49 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, forceCloseChanPoint, true)
|
|
|
|
|
2018-11-06 13:03:01 +03:00
|
|
|
// Cleanup by mining the force close and sweep transaction.
|
|
|
|
cleanupForceClose(t, net, net.Alice, forceCloseChanPoint)
|
|
|
|
|
2018-06-15 22:33:49 +03:00
|
|
|
// We'll need to mine some blocks in order to mark the channel fully
|
|
|
|
// closed.
|
2019-05-15 10:02:53 +03:00
|
|
|
_, err = net.Miner.Node.Generate(lnd.DefaultBitcoinTimeLockDelta - defaultCSV)
|
2018-06-15 22:33:49 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-07 02:57:28 +03:00
|
|
|
// Before we test reconnection, we'll ensure that the channel has been
|
|
|
|
// fully cleaned up for both Carol and Alice.
|
|
|
|
var predErr error
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-09-07 02:57:28 +03:00
|
|
|
pendingChanResp, err := net.Alice.PendingChannels(
|
|
|
|
ctxt, pendingChansRequest,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
predErr = checkNumForceClosedChannels(pendingChanResp, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-09-07 02:57:28 +03:00
|
|
|
pendingChanResp, err = carol.PendingChannels(
|
|
|
|
ctxt, pendingChansRequest,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
predErr = checkNumForceClosedChannels(pendingChanResp, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("channels not marked as fully resolved: %v", predErr)
|
|
|
|
}
|
|
|
|
|
2018-06-15 22:33:49 +03:00
|
|
|
testReconnection(carol)
|
|
|
|
|
|
|
|
// Finally, we'll ensure that Bob and Carol no longer show in Alice's
|
|
|
|
// channel graph.
|
2018-09-26 18:30:09 +03:00
|
|
|
describeGraphReq := &lnrpc.ChannelGraphRequest{
|
|
|
|
IncludeUnannounced: true,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
channelGraph, err := net.Alice.DescribeGraph(ctxt, describeGraphReq)
|
2018-06-15 22:33:49 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for alice's channel graph: %v", err)
|
|
|
|
}
|
|
|
|
for _, node := range channelGraph.Nodes {
|
|
|
|
if node.PubKey == net.Bob.PubKeyStr {
|
|
|
|
t.Fatalf("did not expect to find bob in the channel " +
|
|
|
|
"graph, but did")
|
|
|
|
}
|
|
|
|
if node.PubKey == carol.PubKeyStr {
|
|
|
|
t.Fatalf("did not expect to find carol in the channel " +
|
|
|
|
"graph, but did")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the test is done, we can also close the persistent link.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-06-15 22:33:49 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, persistentChanPoint, false)
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// testRevokedCloseRetribution tests that Carol is able carry out
|
2017-08-01 09:17:13 +03:00
|
|
|
// retribution in the event that she fails immediately after detecting Bob's
|
|
|
|
// breach txn in the mempool.
|
2017-11-03 21:52:02 +03:00
|
|
|
func testRevokedCloseRetribution(net *lntest.NetworkHarness, t *harnessTest) {
|
2016-11-22 06:16:44 +03:00
|
|
|
ctxb := context.Background()
|
2018-11-28 11:25:46 +03:00
|
|
|
|
2016-11-22 06:16:44 +03:00
|
|
|
const (
|
2019-05-15 10:02:53 +03:00
|
|
|
chanAmt = lnd.MaxBtcFundingAmount
|
2016-11-22 06:16:44 +03:00
|
|
|
paymentAmt = 10000
|
|
|
|
numInvoices = 6
|
|
|
|
)
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// Carol will be the breached party. We set --nolisten to ensure Bob
|
|
|
|
// won't be able to connect to her and trigger the channel data
|
|
|
|
// protection logic automatically.
|
|
|
|
carol, err := net.NewNode(
|
|
|
|
"Carol",
|
|
|
|
[]string{"--debughtlc", "--hodl.exit-settle", "--nolisten"},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new carol node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
|
|
|
// We must let Bob communicate with Carol before they are able to open
|
|
|
|
// channel, so we connect Bob and Carol,
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, net.Bob); err != nil {
|
2018-07-12 12:02:51 +03:00
|
|
|
t.Fatalf("unable to connect dave to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we make a channel, we'll load up Carol with some coins sent
|
|
|
|
// directly from the miner.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
2018-07-12 12:02:51 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to test Carol's response to an uncooperative channel
|
2016-11-22 06:16:44 +03:00
|
|
|
// closure by Bob, we'll first open up a channel between them with a
|
|
|
|
// 0.5 BTC value.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, carol, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2016-11-22 06:16:44 +03:00
|
|
|
|
|
|
|
// With the channel open, we'll create a few invoices for Bob that
|
2018-07-12 12:02:51 +03:00
|
|
|
// Carol will pay to in order to advance the state of the channel.
|
2018-11-20 17:09:44 +03:00
|
|
|
bobPayReqs, _, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
net.Bob, paymentAmt, numInvoices,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2016-11-22 06:16:44 +03:00
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// Wait for Carol to receive the channel edge from the funding manager.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-07-12 12:02:51 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
2017-03-16 13:46:58 +03:00
|
|
|
if err != nil {
|
2018-07-12 12:02:51 +03:00
|
|
|
t.Fatalf("carol didn't see the carol->bob channel before "+
|
2017-03-16 13:46:58 +03:00
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// Send payments from Carol to Bob using 3 of Bob's payment hashes
|
2016-11-22 06:16:44 +03:00
|
|
|
// generated above.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-07-12 12:02:51 +03:00
|
|
|
err = completePaymentRequests(ctxt, carol, bobPayReqs[:numInvoices/2],
|
2017-10-26 01:32:00 +03:00
|
|
|
true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
2016-11-22 06:16:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-03-13 22:11:30 +03:00
|
|
|
var bobChan *lnrpc.Channel
|
2018-01-24 13:12:52 +03:00
|
|
|
var predErr error
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
bChan, err := getChanInfo(ctxt, net.Bob)
|
2018-01-24 13:12:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get bob's channel info: %v", err)
|
|
|
|
}
|
2018-01-28 09:11:18 +03:00
|
|
|
if bChan.LocalBalance != 30000 {
|
2018-01-24 13:12:52 +03:00
|
|
|
predErr = fmt.Errorf("bob's balance is incorrect, "+
|
2018-01-28 09:11:18 +03:00
|
|
|
"got %v, expected %v", bChan.LocalBalance,
|
2018-01-24 13:12:52 +03:00
|
|
|
30000)
|
|
|
|
return false
|
|
|
|
}
|
2018-01-28 09:11:18 +03:00
|
|
|
|
|
|
|
bobChan = bChan
|
2018-01-24 13:12:52 +03:00
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
2016-11-22 06:16:44 +03:00
|
|
|
if err != nil {
|
2018-01-24 13:12:52 +03:00
|
|
|
t.Fatalf("%v", predErr)
|
2016-11-22 06:16:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-10-10 13:55:14 +03:00
|
|
|
if err := lntest.CopyFile(bobTempDbFile, net.Bob.DBPath()); err != nil {
|
2016-11-22 06:16:44 +03:00
|
|
|
t.Fatalf("unable to copy database files: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// Finally, send payments from Carol to Bob, consuming Bob's remaining
|
2016-11-22 06:16:44 +03:00
|
|
|
// payment hashes.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-07-12 12:02:51 +03:00
|
|
|
err = completePaymentRequests(ctxt, carol, bobPayReqs[numInvoices/2:],
|
2017-10-26 01:32:00 +03:00
|
|
|
true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
2016-11-22 06:16:44 +03:00
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
bobChan, err = getChanInfo(ctxt, net.Bob)
|
2016-11-22 06:16:44 +03:00
|
|
|
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 {
|
2017-11-03 21:52:02 +03:00
|
|
|
return os.Rename(bobTempDbFile, net.Bob.DBPath())
|
2016-11-22 06:16:44 +03:00
|
|
|
}); 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.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
bobChan, err = getChanInfo(ctxt, net.Bob)
|
2016-11-22 06:16:44 +03:00
|
|
|
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
|
2018-07-12 12:02:51 +03:00
|
|
|
// feel the wrath of Carol's retribution.
|
2018-05-04 06:49:15 +03:00
|
|
|
var closeUpdates lnrpc.Lightning_CloseChannelClient
|
2017-05-07 14:09:22 +03:00
|
|
|
force := true
|
2018-05-04 06:49:15 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelCloseTimeout)
|
|
|
|
closeUpdates, _, err = net.CloseChannel(ctxt, net.Bob, chanPoint, force)
|
2018-05-04 06:49:15 +03:00
|
|
|
if err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, time.Second*10)
|
2017-05-07 14:09:22 +03:00
|
|
|
if err != nil {
|
2018-05-04 06:49:15 +03:00
|
|
|
t.Fatalf("unable to close channel: %v", predErr)
|
2017-05-07 14:09:22 +03:00
|
|
|
}
|
2016-11-22 06:16:44 +03:00
|
|
|
|
2017-05-07 14:09:22 +03:00
|
|
|
// Wait for Bob's breach transaction to show up in the mempool to ensure
|
2018-07-12 12:02:51 +03:00
|
|
|
// that Carol's node has started waiting for confirmations.
|
2018-11-27 11:12:16 +03:00
|
|
|
_, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2017-05-07 14:09:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find Bob's breach tx in mempool: %v", err)
|
|
|
|
}
|
2016-11-22 06:16:44 +03:00
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// Here, Carol sees Bob's breach transaction in the mempool, but is waiting
|
|
|
|
// for it to confirm before continuing her retribution. We restart Carol to
|
2017-05-07 14:09:22 +03:00
|
|
|
// ensure that she is persisting her retribution state and continues
|
|
|
|
// watching for the breach transaction to confirm even after her node
|
|
|
|
// restarts.
|
2018-07-12 12:02:51 +03:00
|
|
|
if err := net.RestartNode(carol, nil); err != nil {
|
|
|
|
t.Fatalf("unable to restart Carol's node: %v", err)
|
2017-05-07 14:09:22 +03:00
|
|
|
}
|
2016-11-22 06:16:44 +03:00
|
|
|
|
2017-05-07 14:09:22 +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.
|
2018-11-29 14:33:11 +03:00
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
2016-11-29 06:43:57 +03:00
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
|
|
|
breachTXID, err := net.WaitForChannelClose(ctxt, closeUpdates)
|
2017-05-07 14:09:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error while waiting for channel close: %v", err)
|
2016-11-22 06:16:44 +03:00
|
|
|
}
|
2017-05-07 14:09:22 +03:00
|
|
|
assertTxInBlock(t, block, breachTXID)
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// Query the mempool for Carol's justice transaction, this should be
|
2017-05-07 14:09:22 +03:00
|
|
|
// broadcast as Bob's contract breaching transaction gets confirmed
|
|
|
|
// above.
|
2018-11-27 11:12:16 +03:00
|
|
|
justiceTXID, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2017-05-07 14:09:22 +03:00
|
|
|
if err != nil {
|
2018-07-12 12:02:51 +03:00
|
|
|
t.Fatalf("unable to find Carol's justice tx in mempool: %v", err)
|
2017-05-07 14:09:22 +03:00
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// We restart Carol here to ensure that she persists her retribution state
|
2017-05-07 14:09:22 +03:00
|
|
|
// and successfully continues exacting retribution after restarting. At
|
2018-07-12 12:02:51 +03:00
|
|
|
// this point, Carol has broadcast the justice transaction, but it hasn't
|
|
|
|
// been confirmed yet; when Carol restarts, she should start waiting for
|
2017-05-07 14:09:22 +03:00
|
|
|
// the justice transaction to confirm again.
|
2018-07-12 12:02:51 +03:00
|
|
|
if err := net.RestartNode(carol, nil); err != nil {
|
|
|
|
t.Fatalf("unable to restart Carol's node: %v", err)
|
2017-05-07 14:09:22 +03:00
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// Now mine a block, this transaction should include Carol's justice
|
2016-11-22 06:16:44 +03:00
|
|
|
// transaction which was just accepted into the mempool.
|
2018-11-29 14:33:11 +03:00
|
|
|
block = mineBlocks(t, net, 1, 1)[0]
|
2016-11-22 06:16:44 +03:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2018-11-28 11:25:27 +03:00
|
|
|
assertNodeNumChannels(t, carol, 0)
|
2016-11-22 06:16:44 +03:00
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// testRevokedCloseRetributionZeroValueRemoteOutput tests that Dave is able
|
2017-09-19 02:32:48 +03:00
|
|
|
// carry out retribution in the event that she fails in state where the remote
|
|
|
|
// commitment output has zero-value.
|
2017-11-03 21:52:02 +03:00
|
|
|
func testRevokedCloseRetributionZeroValueRemoteOutput(net *lntest.NetworkHarness,
|
2017-09-19 02:32:48 +03:00
|
|
|
t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
2018-11-28 11:25:46 +03:00
|
|
|
|
2017-09-19 02:32:48 +03:00
|
|
|
const (
|
2019-05-15 10:02:53 +03:00
|
|
|
chanAmt = lnd.MaxBtcFundingAmount
|
2017-09-19 02:32:48 +03:00
|
|
|
paymentAmt = 10000
|
|
|
|
numInvoices = 6
|
|
|
|
)
|
|
|
|
|
|
|
|
// Since we'd like to test some multi-hop failure scenarios, we'll
|
|
|
|
// introduce another node into our test network: Carol.
|
2018-04-27 00:08:24 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--debughtlc", "--hodl.exit-settle"})
|
2017-09-19 02:32:48 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:22:22 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
2017-09-19 02:32:48 +03:00
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// Dave will be the breached party. We set --nolisten to ensure Carol
|
|
|
|
// won't be able to connect to him and trigger the channel data
|
|
|
|
// protection logic automatically.
|
|
|
|
dave, err := net.NewNode(
|
|
|
|
"Dave",
|
|
|
|
[]string{"--debughtlc", "--hodl.exit-settle", "--nolisten"},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
|
|
|
// We must let Dave have an open channel before she can send a node
|
2017-09-19 02:32:48 +03:00
|
|
|
// announcement, so we open a channel with Carol,
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, dave, carol); err != nil {
|
2018-07-12 12:02:51 +03:00
|
|
|
t.Fatalf("unable to connect dave to carol: %v", err)
|
2017-09-19 02:32:48 +03:00
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// Before we make a channel, we'll load up Dave with some coins sent
|
|
|
|
// directly from the miner.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, dave)
|
2018-07-12 12:02:51 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to dave: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to test Dave's response to an uncooperative channel
|
2017-09-19 02:32:48 +03:00
|
|
|
// closure by Carol, we'll first open up a channel between them with a
|
|
|
|
// 0.5 BTC value.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, dave, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-09-19 02:32:48 +03:00
|
|
|
|
|
|
|
// With the channel open, we'll create a few invoices for Carol that
|
2018-07-12 12:02:51 +03:00
|
|
|
// Dave will pay to in order to advance the state of the channel.
|
2018-11-20 17:09:44 +03:00
|
|
|
carolPayReqs, _, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
carol, paymentAmt, numInvoices,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2017-09-19 02:32:48 +03:00
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// Wait for Dave to receive the channel edge from the funding manager.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-07-12 12:02:51 +03:00
|
|
|
err = dave.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
2017-09-19 02:32:48 +03:00
|
|
|
if err != nil {
|
2018-07-12 12:02:51 +03:00
|
|
|
t.Fatalf("dave didn't see the dave->carol channel before "+
|
2017-09-19 02:32:48 +03:00
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next query for Carol's channel state, as we sent 0 payments, Carol
|
|
|
|
// should now see her balance as being 0 satoshis.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
carolChan, err := getChanInfo(ctxt, carol)
|
2017-09-19 02:32:48 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get carol's channel info: %v", err)
|
|
|
|
}
|
|
|
|
if carolChan.LocalBalance != 0 {
|
|
|
|
t.Fatalf("carol's balance is incorrect, got %v, expected %v",
|
|
|
|
carolChan.LocalBalance, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab Carol's current commitment height (update number), we'll later
|
|
|
|
// revert her to this state after additional updates to force him to
|
|
|
|
// broadcast this soon to be revoked state.
|
|
|
|
carolStateNumPreCopy := carolChan.NumUpdates
|
|
|
|
|
|
|
|
// Create a temporary file to house Carol's database state at this
|
|
|
|
// particular point in history.
|
|
|
|
carolTempDbPath, err := ioutil.TempDir("", "carol-past-state")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp db folder: %v", err)
|
|
|
|
}
|
|
|
|
carolTempDbFile := filepath.Join(carolTempDbPath, "channel.db")
|
|
|
|
defer os.Remove(carolTempDbPath)
|
|
|
|
|
|
|
|
// With the temporary file created, copy Carol's current state into the
|
|
|
|
// temporary file we created above. Later after more updates, we'll
|
|
|
|
// restore this state.
|
2018-10-10 13:55:14 +03:00
|
|
|
if err := lntest.CopyFile(carolTempDbFile, carol.DBPath()); err != nil {
|
2017-09-19 02:32:48 +03:00
|
|
|
t.Fatalf("unable to copy database files: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// Finally, send payments from Dave to Carol, consuming Carol's remaining
|
2017-09-19 02:32:48 +03:00
|
|
|
// payment hashes.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = completePaymentRequests(ctxt, dave, carolPayReqs, false)
|
2017-10-26 01:32:00 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
2017-09-19 02:32:48 +03:00
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
carolChan, err = getChanInfo(ctxt, carol)
|
2017-09-19 02:32:48 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get carol chan info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we shutdown Carol, 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 Carol to travel back in time
|
|
|
|
// within the channel's history.
|
|
|
|
if err = net.RestartNode(carol, func() error {
|
2017-11-03 21:52:02 +03:00
|
|
|
return os.Rename(carolTempDbFile, carol.DBPath())
|
2017-09-19 02:32:48 +03:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("unable to restart node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now query for Carol's channel state, it should show that he's at a
|
|
|
|
// state number in the past, not the *latest* state.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
carolChan, err = getChanInfo(ctxt, carol)
|
2017-09-19 02:32:48 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get carol chan info: %v", err)
|
|
|
|
}
|
|
|
|
if carolChan.NumUpdates != carolStateNumPreCopy {
|
|
|
|
t.Fatalf("db copy failed: %v", carolChan.NumUpdates)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now force Carol 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
|
2018-07-12 12:02:51 +03:00
|
|
|
// feel the wrath of Dave's retribution.
|
2018-06-11 08:32:25 +03:00
|
|
|
var (
|
|
|
|
closeUpdates lnrpc.Lightning_CloseChannelClient
|
|
|
|
closeTxId *chainhash.Hash
|
|
|
|
closeErr error
|
|
|
|
force bool = true
|
|
|
|
)
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-06-11 08:32:25 +03:00
|
|
|
closeUpdates, closeTxId, closeErr = net.CloseChannel(
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, carol, chanPoint, force,
|
2018-06-11 08:32:25 +03:00
|
|
|
)
|
|
|
|
return closeErr == nil
|
|
|
|
}, time.Second*15)
|
2017-09-19 02:32:48 +03:00
|
|
|
if err != nil {
|
2018-06-11 08:32:25 +03:00
|
|
|
t.Fatalf("unable to close channel: %v", closeErr)
|
2017-09-19 02:32:48 +03:00
|
|
|
}
|
|
|
|
|
2018-04-13 14:20:12 +03:00
|
|
|
// Query the mempool for the breaching closing transaction, this should
|
|
|
|
// be broadcast by Carol when she force closes the channel above.
|
2018-11-27 11:12:16 +03:00
|
|
|
txid, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2018-04-13 14:20:12 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find Carol's force close tx in mempool: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
if *txid != *closeTxId {
|
|
|
|
t.Fatalf("expected closeTx(%v) in mempool, instead found %v",
|
|
|
|
closeTxId, txid)
|
|
|
|
}
|
|
|
|
|
2017-09-19 02:32:48 +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.
|
2018-11-29 14:33:11 +03:00
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
2017-09-19 02:32:48 +03:00
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// Here, Dave receives a confirmation of Carol's breach transaction.
|
|
|
|
// We restart Dave to ensure that she is persisting her retribution
|
2017-09-19 02:32:48 +03:00
|
|
|
// state and continues exacting justice after her node restarts.
|
2018-07-12 12:02:51 +03:00
|
|
|
if err := net.RestartNode(dave, nil); err != nil {
|
|
|
|
t.Fatalf("unable to stop Dave's node: %v", err)
|
2017-09-19 02:32:48 +03:00
|
|
|
}
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
|
|
|
breachTXID, err := net.WaitForChannelClose(ctxt, closeUpdates)
|
2017-09-19 02:32:48 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error while waiting for channel close: %v", err)
|
|
|
|
}
|
|
|
|
assertTxInBlock(t, block, breachTXID)
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// Query the mempool for Dave's justice transaction, this should be
|
2017-09-19 02:32:48 +03:00
|
|
|
// broadcast as Carol's contract breaching transaction gets confirmed
|
|
|
|
// above.
|
2018-11-27 11:12:16 +03:00
|
|
|
justiceTXID, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2017-09-19 02:32:48 +03:00
|
|
|
if err != nil {
|
2018-07-12 12:02:51 +03:00
|
|
|
t.Fatalf("unable to find Dave's justice tx in mempool: %v",
|
2017-09-19 02:32:48 +03:00
|
|
|
err)
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// Query for the mempool transaction found above. Then assert that all
|
|
|
|
// the inputs of this transaction are spending outputs generated by
|
|
|
|
// Carol'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[:]) {
|
|
|
|
t.Fatalf("justice tx not spending commitment utxo "+
|
|
|
|
"instead is: %v", txIn.PreviousOutPoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// We restart Dave here to ensure that he persists her retribution state
|
2017-09-19 02:32:48 +03:00
|
|
|
// and successfully continues exacting retribution after restarting. At
|
2018-07-12 12:02:51 +03:00
|
|
|
// this point, Dave has broadcast the justice transaction, but it hasn't
|
|
|
|
// been confirmed yet; when Dave restarts, she should start waiting for
|
2017-09-19 02:32:48 +03:00
|
|
|
// the justice transaction to confirm again.
|
2018-07-12 12:02:51 +03:00
|
|
|
if err := net.RestartNode(dave, nil); err != nil {
|
|
|
|
t.Fatalf("unable to restart Dave's node: %v", err)
|
2017-09-19 02:32:48 +03:00
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:51 +03:00
|
|
|
// Now mine a block, this transaction should include Dave's justice
|
2017-09-19 02:32:48 +03:00
|
|
|
// transaction which was just accepted into the mempool.
|
2018-11-29 14:33:11 +03:00
|
|
|
block = mineBlocks(t, net, 1, 1)[0]
|
2017-09-19 02:32:48 +03:00
|
|
|
|
|
|
|
// The block should have exactly *two* transactions, one of which is
|
|
|
|
// the justice transaction.
|
|
|
|
if len(block.Transactions) != 2 {
|
|
|
|
t.Fatalf("transaction wasn't mined")
|
|
|
|
}
|
|
|
|
justiceSha := block.Transactions[1].TxHash()
|
|
|
|
if !bytes.Equal(justiceTx.Hash()[:], justiceSha[:]) {
|
|
|
|
t.Fatalf("justice tx wasn't mined")
|
|
|
|
}
|
|
|
|
|
2018-11-28 11:25:27 +03:00
|
|
|
assertNodeNumChannels(t, dave, 0)
|
2017-09-19 02:32:48 +03:00
|
|
|
}
|
|
|
|
|
2018-04-05 03:02:56 +03:00
|
|
|
// testRevokedCloseRetributionRemoteHodl tests that Dave properly responds to a
|
2017-08-30 10:43:38 +03:00
|
|
|
// channel breach made by the remote party, specifically in the case that the
|
|
|
|
// remote party breaches before settling extended HTLCs.
|
2017-11-03 21:52:02 +03:00
|
|
|
func testRevokedCloseRetributionRemoteHodl(net *lntest.NetworkHarness,
|
2017-08-01 09:17:13 +03:00
|
|
|
t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
2018-11-28 11:25:46 +03:00
|
|
|
|
2017-08-01 09:17:13 +03:00
|
|
|
const (
|
2019-05-15 10:02:53 +03:00
|
|
|
chanAmt = lnd.MaxBtcFundingAmount
|
2018-04-05 03:02:56 +03:00
|
|
|
pushAmt = 200000
|
2017-08-01 09:17:13 +03:00
|
|
|
paymentAmt = 10000
|
|
|
|
numInvoices = 6
|
|
|
|
)
|
|
|
|
|
2018-04-05 03:02:56 +03:00
|
|
|
// Since this test will result in the counterparty being left in a
|
|
|
|
// weird state, we will introduce another node into our test network:
|
|
|
|
// Carol.
|
2018-04-27 00:08:24 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--debughtlc", "--hodl.exit-settle"})
|
2017-08-26 03:12:49 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:22:22 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
2017-08-26 03:12:49 +03:00
|
|
|
|
2018-04-05 03:02:56 +03:00
|
|
|
// We'll also create a new node Dave, who will have a channel with
|
|
|
|
// Carol, and also use similar settings so we can broadcast a commit
|
2018-07-12 12:02:51 +03:00
|
|
|
// with active HTLCs. Dave will be the breached party. We set
|
|
|
|
// --nolisten to ensure Carol won't be able to connect to him and
|
|
|
|
// trigger the channel data protection logic automatically.
|
|
|
|
dave, err := net.NewNode(
|
|
|
|
"Dave",
|
|
|
|
[]string{"--debughtlc", "--hodl.exit-settle", "--nolisten"},
|
|
|
|
)
|
2018-04-05 03:02:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new dave node: %v", err)
|
2017-08-26 03:12:49 +03:00
|
|
|
}
|
2018-06-08 00:22:22 +03:00
|
|
|
defer shutdownAndAssert(net, t, dave)
|
2017-08-26 03:12:49 +03:00
|
|
|
|
2018-04-05 03:02:56 +03:00
|
|
|
// We must let Dave communicate with Carol before they are able to open
|
|
|
|
// channel, so we connect Dave and Carol,
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, dave, carol); err != nil {
|
2018-04-05 03:02:56 +03:00
|
|
|
t.Fatalf("unable to connect dave to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we make a channel, we'll load up Dave with some coins sent
|
|
|
|
// directly from the miner.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, dave)
|
2018-04-05 03:02:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to dave: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to test Dave's response to an uncooperative channel closure
|
|
|
|
// by Carol, we'll first open up a channel between them with a
|
2019-05-15 10:02:53 +03:00
|
|
|
// lnd.MaxBtcFundingAmount (2^24) satoshis value.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-04-05 03:02:56 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, dave, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-04-05 03:02:56 +03:00
|
|
|
)
|
2017-08-01 09:17:13 +03:00
|
|
|
|
2017-08-30 10:43:38 +03:00
|
|
|
// With the channel open, we'll create a few invoices for Carol that
|
2018-04-05 03:02:56 +03:00
|
|
|
// Dave will pay to in order to advance the state of the channel.
|
2018-11-20 17:09:44 +03:00
|
|
|
carolPayReqs, _, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
carol, paymentAmt, numInvoices,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2017-08-01 09:17:13 +03:00
|
|
|
}
|
|
|
|
|
2017-08-30 10:43:38 +03:00
|
|
|
// We'll introduce a closure to validate that Carol's current balance
|
|
|
|
// matches the given expected amount.
|
|
|
|
checkCarolBalance := func(expectedAmt int64) {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
carolChan, err := getChanInfo(ctxt, carol)
|
2017-08-30 10:43:38 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get carol's channel info: %v", err)
|
|
|
|
}
|
|
|
|
if carolChan.LocalBalance != expectedAmt {
|
|
|
|
t.Fatalf("carol's balance is incorrect, "+
|
|
|
|
"got %v, expected %v", carolChan.LocalBalance,
|
|
|
|
expectedAmt)
|
|
|
|
}
|
|
|
|
}
|
2018-04-05 03:02:56 +03:00
|
|
|
|
2017-08-30 10:43:38 +03:00
|
|
|
// We'll introduce another closure to validate that Carol's current
|
|
|
|
// number of updates is at least as large as the provided minimum
|
|
|
|
// number.
|
2018-02-07 06:11:11 +03:00
|
|
|
checkCarolNumUpdatesAtLeast := func(minimum uint64) {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
carolChan, err := getChanInfo(ctxt, carol)
|
2017-08-30 10:43:38 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get carol's channel info: %v", err)
|
|
|
|
}
|
|
|
|
if carolChan.NumUpdates < minimum {
|
|
|
|
t.Fatalf("carol's numupdates is incorrect, want %v "+
|
2017-10-26 01:32:00 +03:00
|
|
|
"to be at least %v", carolChan.NumUpdates,
|
2017-08-30 10:43:38 +03:00
|
|
|
minimum)
|
|
|
|
}
|
2017-08-01 09:17:13 +03:00
|
|
|
}
|
|
|
|
|
2018-04-05 03:02:56 +03:00
|
|
|
// Wait for Dave to receive the channel edge from the funding manager.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-05 03:02:56 +03:00
|
|
|
err = dave.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
2017-08-01 09:17:13 +03:00
|
|
|
if err != nil {
|
2018-04-05 03:02:56 +03:00
|
|
|
t.Fatalf("dave didn't see the dave->carol channel before "+
|
2017-08-01 09:17:13 +03:00
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-30 10:43:38 +03:00
|
|
|
// Ensure that carol's balance starts with the amount we pushed to her.
|
|
|
|
checkCarolBalance(pushAmt)
|
|
|
|
|
2018-04-05 03:02:56 +03:00
|
|
|
// Send payments from Dave to Carol using 3 of Carol's payment hashes
|
|
|
|
// generated above.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-05 03:02:56 +03:00
|
|
|
err = completePaymentRequests(
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, dave, carolPayReqs[:numInvoices/2], false,
|
2018-04-05 03:02:56 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we'll also send over a set of HTLC's from Carol to
|
|
|
|
// Dave. This ensures that the final revoked transaction has HTLC's in
|
|
|
|
// both directions.
|
2018-11-20 17:09:44 +03:00
|
|
|
davePayReqs, _, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
dave, paymentAmt, numInvoices,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2018-04-05 03:02:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send payments from Carol to Dave using 3 of Dave's payment hashes
|
2017-08-01 09:17:13 +03:00
|
|
|
// generated above.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-05 03:02:56 +03:00
|
|
|
err = completePaymentRequests(
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, carol, davePayReqs[:numInvoices/2], false,
|
2018-04-05 03:02:56 +03:00
|
|
|
)
|
2017-10-26 01:32:00 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
2017-08-01 09:17:13 +03:00
|
|
|
}
|
|
|
|
|
2017-08-26 03:12:49 +03:00
|
|
|
// Next query for Carol's channel state, as we sent 3 payments of 10k
|
2017-08-30 10:43:38 +03:00
|
|
|
// satoshis each, however Carol should now see her balance as being
|
|
|
|
// equal to the push amount in satoshis since she has not settled.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
carolChan, err := getChanInfo(ctxt, carol)
|
2017-08-01 09:17:13 +03:00
|
|
|
if err != nil {
|
2017-08-30 10:43:38 +03:00
|
|
|
t.Fatalf("unable to get carol's channel info: %v", err)
|
2017-08-01 09:17:13 +03:00
|
|
|
}
|
2018-04-05 03:02:56 +03:00
|
|
|
|
2017-08-26 03:12:49 +03:00
|
|
|
// Grab Carol's current commitment height (update number), we'll later
|
2017-08-30 10:43:38 +03:00
|
|
|
// revert her to this state after additional updates to force her to
|
2017-08-01 09:17:13 +03:00
|
|
|
// broadcast this soon to be revoked state.
|
2017-08-30 10:43:38 +03:00
|
|
|
carolStateNumPreCopy := carolChan.NumUpdates
|
|
|
|
|
|
|
|
// Ensure that carol's balance still reflects the original amount we
|
2018-04-05 03:02:56 +03:00
|
|
|
// pushed to her, minus the HTLCs she just sent to Dave.
|
|
|
|
checkCarolBalance(pushAmt - 3*paymentAmt)
|
|
|
|
|
2017-08-30 10:43:38 +03:00
|
|
|
// Since Carol has not settled, she should only see at least one update
|
|
|
|
// to her channel.
|
2018-02-07 06:11:11 +03:00
|
|
|
checkCarolNumUpdatesAtLeast(1)
|
2017-08-01 09:17:13 +03:00
|
|
|
|
2017-08-26 03:12:49 +03:00
|
|
|
// Create a temporary file to house Carol's database state at this
|
2017-08-01 09:17:13 +03:00
|
|
|
// particular point in history.
|
2017-08-30 10:43:38 +03:00
|
|
|
carolTempDbPath, err := ioutil.TempDir("", "carol-past-state")
|
2017-08-01 09:17:13 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp db folder: %v", err)
|
|
|
|
}
|
2017-08-30 10:43:38 +03:00
|
|
|
carolTempDbFile := filepath.Join(carolTempDbPath, "channel.db")
|
|
|
|
defer os.Remove(carolTempDbPath)
|
2017-08-01 09:17:13 +03:00
|
|
|
|
2017-08-26 03:12:49 +03:00
|
|
|
// With the temporary file created, copy Carol's current state into the
|
2017-08-01 09:17:13 +03:00
|
|
|
// temporary file we created above. Later after more updates, we'll
|
|
|
|
// restore this state.
|
2018-10-10 13:55:14 +03:00
|
|
|
if err := lntest.CopyFile(carolTempDbFile, carol.DBPath()); err != nil {
|
2017-08-01 09:17:13 +03:00
|
|
|
t.Fatalf("unable to copy database files: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-04-05 03:02:56 +03:00
|
|
|
// Finally, send payments from Dave to Carol, consuming Carol's
|
|
|
|
// remaining payment hashes.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-05 03:02:56 +03:00
|
|
|
err = completePaymentRequests(
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, dave, carolPayReqs[numInvoices/2:], false,
|
2018-04-05 03:02:56 +03:00
|
|
|
)
|
2017-10-26 01:32:00 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
2017-08-01 09:17:13 +03:00
|
|
|
}
|
|
|
|
|
2017-08-30 10:43:38 +03:00
|
|
|
// Ensure that carol's balance still shows the amount we originally
|
2018-04-05 03:02:56 +03:00
|
|
|
// pushed to her (minus the HTLCs she sent to Bob), and that at least
|
|
|
|
// one more update has occurred.
|
2017-12-21 13:44:40 +03:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
2018-04-05 03:02:56 +03:00
|
|
|
checkCarolBalance(pushAmt - 3*paymentAmt)
|
2018-02-07 06:11:11 +03:00
|
|
|
checkCarolNumUpdatesAtLeast(carolStateNumPreCopy + 1)
|
2017-08-01 09:17:13 +03:00
|
|
|
|
2017-08-30 10:43:38 +03:00
|
|
|
// Now we shutdown Carol, copying over the her temporary database state
|
|
|
|
// which has the *prior* channel state over her current most up to date
|
2017-08-26 03:12:49 +03:00
|
|
|
// state. With this, we essentially force Carol to travel back in time
|
2017-08-01 09:17:13 +03:00
|
|
|
// within the channel's history.
|
2017-08-26 03:12:49 +03:00
|
|
|
if err = net.RestartNode(carol, func() error {
|
2017-11-03 21:52:02 +03:00
|
|
|
return os.Rename(carolTempDbFile, carol.DBPath())
|
2017-08-01 09:17:13 +03:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("unable to restart node: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-30 10:43:38 +03:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
2018-04-05 03:02:56 +03:00
|
|
|
// Ensure that Carol's view of the channel is consistent with the state
|
|
|
|
// of the channel just before it was snapshotted.
|
|
|
|
checkCarolBalance(pushAmt - 3*paymentAmt)
|
2018-02-07 06:11:11 +03:00
|
|
|
checkCarolNumUpdatesAtLeast(1)
|
2017-08-30 10:43:38 +03:00
|
|
|
|
|
|
|
// Now query for Carol's channel state, it should show that she's at a
|
|
|
|
// state number in the past, *not* the latest state.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
carolChan, err = getChanInfo(ctxt, carol)
|
2017-08-01 09:17:13 +03:00
|
|
|
if err != nil {
|
2017-08-30 10:43:38 +03:00
|
|
|
t.Fatalf("unable to get carol chan info: %v", err)
|
2017-08-01 09:17:13 +03:00
|
|
|
}
|
2017-08-30 10:43:38 +03:00
|
|
|
if carolChan.NumUpdates != carolStateNumPreCopy {
|
|
|
|
t.Fatalf("db copy failed: %v", carolChan.NumUpdates)
|
2017-08-01 09:17:13 +03:00
|
|
|
}
|
|
|
|
|
2017-08-26 03:12:49 +03:00
|
|
|
// Now force Carol to execute a *force* channel closure by unilaterally
|
2017-08-30 10:43:38 +03:00
|
|
|
// broadcasting her current channel state. This is actually the
|
|
|
|
// commitment transaction of a prior *revoked* state, so she'll soon
|
2018-04-05 03:02:56 +03:00
|
|
|
// feel the wrath of Dave's retribution.
|
2017-08-01 09:17:13 +03:00
|
|
|
force := true
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
|
|
|
closeUpdates, closeTxId, err := net.CloseChannel(ctxt, carol,
|
2018-04-13 14:20:12 +03:00
|
|
|
chanPoint, force)
|
2017-08-01 09:17:13 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to close channel: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-04-13 14:20:12 +03:00
|
|
|
// Query the mempool for the breaching closing transaction, this should
|
|
|
|
// be broadcast by Carol when she force closes the channel above.
|
2018-11-27 11:12:16 +03:00
|
|
|
txid, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2017-08-30 10:43:38 +03:00
|
|
|
if err != nil {
|
2018-04-13 14:20:12 +03:00
|
|
|
t.Fatalf("unable to find Carol's force close tx in mempool: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
if *txid != *closeTxId {
|
|
|
|
t.Fatalf("expected closeTx(%v) in mempool, instead found %v",
|
|
|
|
closeTxId, txid)
|
2017-08-30 10:43:38 +03:00
|
|
|
}
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
|
|
|
// Generate a single block to mine the breach transaction.
|
2018-11-29 14:33:11 +03:00
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
2017-08-01 09:17:13 +03:00
|
|
|
|
2018-04-05 03:02:56 +03:00
|
|
|
// Wait so Dave receives a confirmation of Carol's breach transaction.
|
2017-08-30 10:43:38 +03:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
2018-04-05 03:02:56 +03:00
|
|
|
// We restart Dave to ensure that he is persisting his retribution
|
2017-08-30 10:43:38 +03:00
|
|
|
// state and continues exacting justice after her node restarts.
|
2018-04-05 03:02:56 +03:00
|
|
|
if err := net.RestartNode(dave, nil); err != nil {
|
|
|
|
t.Fatalf("unable to stop Dave's node: %v", err)
|
2017-08-01 09:17:13 +03:00
|
|
|
}
|
|
|
|
|
2018-04-05 03:02:56 +03:00
|
|
|
// Finally, wait for the final close status update, then ensure that
|
|
|
|
// the closing transaction was included in the block.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
|
|
|
breachTXID, err := net.WaitForChannelClose(ctxt, closeUpdates)
|
2017-08-01 09:17:13 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error while waiting for channel close: %v", err)
|
|
|
|
}
|
2018-04-13 14:20:12 +03:00
|
|
|
if *breachTXID != *closeTxId {
|
|
|
|
t.Fatalf("expected breach ID(%v) to be equal to close ID (%v)",
|
|
|
|
breachTXID, closeTxId)
|
|
|
|
}
|
2017-08-01 09:17:13 +03:00
|
|
|
assertTxInBlock(t, block, breachTXID)
|
|
|
|
|
2018-04-05 03:02:56 +03:00
|
|
|
// Query the mempool for Dave's justice transaction, this should be
|
2017-08-26 03:12:49 +03:00
|
|
|
// broadcast as Carol's contract breaching transaction gets confirmed
|
2018-04-13 14:20:12 +03:00
|
|
|
// above. Since Carol might have had the time to take some of the HTLC
|
2018-07-17 10:13:06 +03:00
|
|
|
// outputs to the second level before Dave broadcasts his justice tx,
|
2018-04-13 14:20:12 +03:00
|
|
|
// we'll search through the mempool for a tx that matches the number of
|
|
|
|
// expected inputs in the justice tx.
|
|
|
|
var predErr error
|
|
|
|
var justiceTxid *chainhash.Hash
|
2018-07-17 10:13:06 +03:00
|
|
|
errNotFound := errors.New("justice tx not found")
|
|
|
|
findJusticeTx := func() (*chainhash.Hash, error) {
|
2018-04-13 14:20:12 +03:00
|
|
|
mempool, err := net.Miner.Node.GetRawMempool()
|
|
|
|
if err != nil {
|
2018-07-17 10:13:06 +03:00
|
|
|
return nil, fmt.Errorf("unable to get mempool from "+
|
|
|
|
"miner: %v", err)
|
2018-04-13 14:20:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, txid := range mempool {
|
|
|
|
// Check that the justice tx has the appropriate number
|
|
|
|
// of inputs.
|
|
|
|
tx, err := net.Miner.Node.GetRawTransaction(txid)
|
|
|
|
if err != nil {
|
2018-07-17 10:13:06 +03:00
|
|
|
return nil, fmt.Errorf("unable to query for "+
|
2018-04-13 14:20:12 +03:00
|
|
|
"txs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exNumInputs := 2 + numInvoices
|
|
|
|
if len(tx.MsgTx().TxIn) == exNumInputs {
|
2018-07-17 10:13:06 +03:00
|
|
|
return txid, nil
|
2018-04-13 14:20:12 +03:00
|
|
|
}
|
2018-07-17 10:13:06 +03:00
|
|
|
}
|
|
|
|
return nil, errNotFound
|
|
|
|
}
|
2018-04-13 14:20:12 +03:00
|
|
|
|
2018-07-17 10:13:06 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
txid, err := findJusticeTx()
|
|
|
|
if err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
2018-04-13 14:20:12 +03:00
|
|
|
}
|
|
|
|
|
2018-07-17 10:13:06 +03:00
|
|
|
justiceTxid = txid
|
|
|
|
return true
|
|
|
|
}, time.Second*10)
|
|
|
|
if err != nil && predErr == errNotFound {
|
|
|
|
// If Dave is unable to broadcast his justice tx on first
|
|
|
|
// attempt because of the second layer transactions, he will
|
|
|
|
// wait until the next block epoch before trying again. Because
|
|
|
|
// of this, we'll mine a block if we cannot find the justice tx
|
2019-02-06 23:47:41 +03:00
|
|
|
// immediately. Since we cannot tell for sure how many
|
|
|
|
// transactions will be in the mempool at this point, we pass 0
|
|
|
|
// as the last argument, indicating we don't care what's in the
|
|
|
|
// mempool.
|
|
|
|
mineBlocks(t, net, 1, 0)
|
2018-07-17 10:13:06 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
txid, err := findJusticeTx()
|
|
|
|
if err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
justiceTxid = txid
|
|
|
|
return true
|
|
|
|
}, time.Second*10)
|
|
|
|
}
|
2017-08-01 09:17:13 +03:00
|
|
|
if err != nil {
|
2018-04-13 14:20:12 +03:00
|
|
|
t.Fatalf(predErr.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
justiceTx, err := net.Miner.Node.GetRawTransaction(justiceTxid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for justice tx: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// isSecondLevelSpend checks that the passed secondLevelTxid is a
|
|
|
|
// potentitial second level spend spending from the commit tx.
|
|
|
|
isSecondLevelSpend := func(commitTxid, secondLevelTxid *chainhash.Hash) bool {
|
|
|
|
secondLevel, err := net.Miner.Node.GetRawTransaction(
|
|
|
|
secondLevelTxid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for tx: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A second level spend should have only one input, and one
|
|
|
|
// output.
|
|
|
|
if len(secondLevel.MsgTx().TxIn) != 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(secondLevel.MsgTx().TxOut) != 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// The sole input should be spending from the commit tx.
|
|
|
|
txIn := secondLevel.MsgTx().TxIn[0]
|
|
|
|
if !bytes.Equal(txIn.PreviousOutPoint.Hash[:], commitTxid[:]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that all the inputs of this transaction are spending outputs
|
|
|
|
// generated by Carol's breach transaction above.
|
|
|
|
for _, txIn := range justiceTx.MsgTx().TxIn {
|
|
|
|
if bytes.Equal(txIn.PreviousOutPoint.Hash[:], breachTXID[:]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the justice tx is spending from an output that was not on
|
|
|
|
// the breach tx, Carol might have had the time to take an
|
|
|
|
// output to the second level. In that case, check that the
|
|
|
|
// justice tx is spending this second level output.
|
|
|
|
if isSecondLevelSpend(breachTXID, &txIn.PreviousOutPoint.Hash) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatalf("justice tx not spending commitment utxo "+
|
|
|
|
"instead is: %v", txIn.PreviousOutPoint)
|
2017-08-01 09:17:13 +03:00
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
2018-04-05 03:02:56 +03:00
|
|
|
// We restart Dave here to ensure that he persists he retribution state
|
2017-08-30 10:43:38 +03:00
|
|
|
// and successfully continues exacting retribution after restarting. At
|
2018-04-05 03:02:56 +03:00
|
|
|
// this point, Dave has broadcast the justice transaction, but it
|
|
|
|
// hasn't been confirmed yet; when Dave restarts, he should start
|
|
|
|
// waiting for the justice transaction to confirm again.
|
|
|
|
if err := net.RestartNode(dave, nil); err != nil {
|
|
|
|
t.Fatalf("unable to restart Dave's node: %v", err)
|
2017-08-30 10:43:38 +03:00
|
|
|
}
|
|
|
|
|
2018-04-05 03:02:56 +03:00
|
|
|
// Now mine a block, this transaction should include Dave's justice
|
2017-08-01 09:17:13 +03:00
|
|
|
// transaction which was just accepted into the mempool.
|
2018-11-29 14:33:11 +03:00
|
|
|
block = mineBlocks(t, net, 1, 1)[0]
|
2018-04-13 14:20:12 +03:00
|
|
|
assertTxInBlock(t, block, justiceTxid)
|
2017-08-01 09:17:13 +03:00
|
|
|
|
2018-04-13 14:20:12 +03:00
|
|
|
// Dave should have no open channels.
|
2018-11-28 11:25:27 +03:00
|
|
|
assertNodeNumChannels(t, dave, 0)
|
2017-12-15 00:56:10 +03:00
|
|
|
}
|
|
|
|
|
2019-06-14 03:32:29 +03:00
|
|
|
// testRevokedCloseRetributionAltruistWatchtower establishes a channel between
|
|
|
|
// Carol and Dave, where Carol is using a third node Willy as her watchtower.
|
|
|
|
// After sending some payments, Dave reverts his state and force closes to
|
|
|
|
// trigger a breach. Carol is kept offline throughout the process and the test
|
|
|
|
// asserts that Willy responds by broadcasting the justice transaction on
|
|
|
|
// Carol's behalf sweeping her funds without a reward.
|
|
|
|
func testRevokedCloseRetributionAltruistWatchtower(net *lntest.NetworkHarness,
|
|
|
|
t *harnessTest) {
|
|
|
|
|
|
|
|
ctxb := context.Background()
|
|
|
|
const (
|
|
|
|
chanAmt = lnd.MaxBtcFundingAmount
|
|
|
|
paymentAmt = 10000
|
|
|
|
numInvoices = 6
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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("Carol", []string{
|
|
|
|
"--debughtlc", "--hodl.exit-settle",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
|
|
|
// Willy the watchtower will protect Dave from Carol's breach. He will
|
|
|
|
// remain online in order to punish Carol on Dave's behalf, since the
|
|
|
|
// breach will happen while Dave is offline.
|
|
|
|
willy, err := net.NewNode("Willy", []string{"--watchtower.active"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, willy)
|
|
|
|
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
willyInfo, err := willy.GetInfo(ctxt, &lnrpc.GetInfoRequest{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to getinfo from willy: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
willyAddr := willyInfo.Uris[0]
|
|
|
|
parts := strings.Split(willyAddr, ":")
|
|
|
|
willyTowerAddr := parts[0]
|
|
|
|
|
|
|
|
// Dave will be the breached party. We set --nolisten to ensure Carol
|
|
|
|
// won't be able to connect to him and trigger the channel data
|
|
|
|
// protection logic automatically.
|
|
|
|
dave, err := net.NewNode("Dave", []string{
|
|
|
|
"--nolisten",
|
|
|
|
"--wtclient.private-tower-uris=" + willyTowerAddr,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
|
|
|
// We must let Dave have an open channel before she can send a node
|
|
|
|
// announcement, so we open a channel with Carol,
|
|
|
|
if err := net.ConnectNodes(ctxb, dave, carol); err != nil {
|
|
|
|
t.Fatalf("unable to connect dave to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we make a channel, we'll load up Dave with some coins sent
|
|
|
|
// directly from the miner.
|
|
|
|
err = net.SendCoins(ctxb, btcutil.SatoshiPerBitcoin, dave)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to dave: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to test Dave's response to an uncooperative channel
|
|
|
|
// closure by Carol, we'll first open up a channel between them with a
|
|
|
|
// 0.5 BTC value.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
|
|
|
chanPoint := openChannelAndAssert(
|
|
|
|
ctxt, t, net, dave, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: 3 * (chanAmt / 4),
|
|
|
|
PushAmt: chanAmt / 4,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// With the channel open, we'll create a few invoices for Carol that
|
|
|
|
// Dave will pay to in order to advance the state of the channel.
|
|
|
|
carolPayReqs, _, _, err := createPayReqs(
|
|
|
|
carol, paymentAmt, numInvoices,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Dave to receive the channel edge from the funding manager.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = dave.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("dave didn't see the dave->carol channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next query for Carol's channel state, as we sent 0 payments, Carol
|
|
|
|
// should still see her balance as the push amount, which is 1/4 of the
|
|
|
|
// capacity.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
carolChan, err := getChanInfo(ctxt, carol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get carol's channel info: %v", err)
|
|
|
|
}
|
|
|
|
if carolChan.LocalBalance != int64(chanAmt/4) {
|
|
|
|
t.Fatalf("carol's balance is incorrect, got %v, expected %v",
|
|
|
|
carolChan.LocalBalance, chanAmt/4)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab Carol's current commitment height (update number), we'll later
|
|
|
|
// revert her to this state after additional updates to force him to
|
|
|
|
// broadcast this soon to be revoked state.
|
|
|
|
carolStateNumPreCopy := carolChan.NumUpdates
|
|
|
|
|
|
|
|
// Create a temporary file to house Carol's database state at this
|
|
|
|
// particular point in history.
|
|
|
|
carolTempDbPath, err := ioutil.TempDir("", "carol-past-state")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp db folder: %v", err)
|
|
|
|
}
|
|
|
|
carolTempDbFile := filepath.Join(carolTempDbPath, "channel.db")
|
|
|
|
defer os.Remove(carolTempDbPath)
|
|
|
|
|
|
|
|
// With the temporary file created, copy Carol's current state into the
|
|
|
|
// temporary file we created above. Later after more updates, we'll
|
|
|
|
// restore this state.
|
|
|
|
if err := lntest.CopyFile(carolTempDbFile, carol.DBPath()); err != nil {
|
|
|
|
t.Fatalf("unable to copy database files: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, send payments from Dave to Carol, consuming Carol's remaining
|
|
|
|
// payment hashes.
|
|
|
|
err = completePaymentRequests(ctxb, dave, carolPayReqs, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
daveBalReq := &lnrpc.WalletBalanceRequest{}
|
|
|
|
daveBalResp, err := dave.WalletBalance(ctxt, daveBalReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get dave's balance: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
davePreSweepBalance := daveBalResp.ConfirmedBalance
|
|
|
|
|
|
|
|
// Shutdown Dave to simulate going offline for an extended period of
|
|
|
|
// time. Once he's not watching, Carol will try to breach the channel.
|
|
|
|
restart, err := net.SuspendNode(dave)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to suspend Dave: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we shutdown Carol, 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 Carol to travel back in time
|
|
|
|
// within the channel's history.
|
|
|
|
if err = net.RestartNode(carol, func() error {
|
|
|
|
return os.Rename(carolTempDbFile, carol.DBPath())
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("unable to restart node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now query for Carol's channel state, it should show that he's at a
|
|
|
|
// state number in the past, not the *latest* state.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
carolChan, err = getChanInfo(ctxt, carol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get carol chan info: %v", err)
|
|
|
|
}
|
|
|
|
if carolChan.NumUpdates != carolStateNumPreCopy {
|
|
|
|
t.Fatalf("db copy failed: %v", carolChan.NumUpdates)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(conner): add hook for backup completion
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
|
|
|
// Now force Carol 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 Dave's retribution.
|
|
|
|
closeUpdates, closeTxId, err := net.CloseChannel(
|
|
|
|
ctxb, carol, chanPoint, true,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to close channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the mempool for the breaching closing transaction, this should
|
|
|
|
// be broadcast by Carol when she force closes the channel above.
|
|
|
|
txid, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find Carol's force close tx in mempool: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
if *txid != *closeTxId {
|
|
|
|
t.Fatalf("expected closeTx(%v) in mempool, instead found %v",
|
|
|
|
closeTxId, txid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, generate a single block, wait for the final close status
|
|
|
|
// update, then ensure that the closing transaction was included in the
|
|
|
|
// block.
|
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
breachTXID, err := net.WaitForChannelClose(ctxt, closeUpdates)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error while waiting for channel close: %v", err)
|
|
|
|
}
|
|
|
|
assertTxInBlock(t, block, breachTXID)
|
|
|
|
|
|
|
|
// Query the mempool for Dave's justice transaction, this should be
|
|
|
|
// broadcast as Carol's contract breaching transaction gets confirmed
|
|
|
|
// above.
|
|
|
|
justiceTXID, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find Dave's justice tx in mempool: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// Query for the mempool transaction found above. Then assert that all
|
|
|
|
// the inputs of this transaction are spending outputs generated by
|
|
|
|
// Carol'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[:]) {
|
|
|
|
t.Fatalf("justice tx not spending commitment utxo "+
|
|
|
|
"instead is: %v", txIn.PreviousOutPoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
willyBalReq := &lnrpc.WalletBalanceRequest{}
|
|
|
|
willyBalResp, err := willy.WalletBalance(ctxt, willyBalReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get willy's balance: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if willyBalResp.ConfirmedBalance != 0 {
|
|
|
|
t.Fatalf("willy should have 0 balance before mining "+
|
|
|
|
"justice transaction, instead has %d",
|
|
|
|
willyBalResp.ConfirmedBalance)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now mine a block, this transaction should include Dave's justice
|
|
|
|
// transaction which was just accepted into the mempool.
|
|
|
|
block = mineBlocks(t, net, 1, 1)[0]
|
|
|
|
|
|
|
|
// The block should have exactly *two* transactions, one of which is
|
|
|
|
// the justice transaction.
|
|
|
|
if len(block.Transactions) != 2 {
|
|
|
|
t.Fatalf("transaction wasn't mined")
|
|
|
|
}
|
|
|
|
justiceSha := block.Transactions[1].TxHash()
|
|
|
|
if !bytes.Equal(justiceTx.Hash()[:], justiceSha[:]) {
|
|
|
|
t.Fatalf("justice tx wasn't mined")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that Willy doesn't get any funds, as he is acting as an
|
|
|
|
// altruist watchtower.
|
|
|
|
var predErr error
|
|
|
|
err = lntest.WaitInvariant(func() bool {
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
willyBalReq := &lnrpc.WalletBalanceRequest{}
|
|
|
|
willyBalResp, err := willy.WalletBalance(ctxt, willyBalReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get willy's balance: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if willyBalResp.ConfirmedBalance != 0 {
|
|
|
|
predErr = fmt.Errorf("Expected Willy to have no funds "+
|
|
|
|
"after justice transaction was mined, found %v",
|
|
|
|
willyBalResp)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, time.Second*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", predErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restart Dave, who will still think his channel with Carol is open.
|
|
|
|
// We should him to detect the breach, but realize that the funds have
|
|
|
|
// then been swept to his wallet by Willy.
|
|
|
|
err = restart()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to restart dave: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
daveBalReq := &lnrpc.ChannelBalanceRequest{}
|
|
|
|
daveBalResp, err := dave.ChannelBalance(ctxt, daveBalReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get dave's balance: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if daveBalResp.Balance != 0 {
|
|
|
|
predErr = fmt.Errorf("Dave should end up with zero "+
|
|
|
|
"channel balance, instead has %d",
|
|
|
|
daveBalResp.Balance)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", predErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNumPendingChannels(t, dave, 0, 0)
|
|
|
|
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
daveBalReq := &lnrpc.WalletBalanceRequest{}
|
|
|
|
daveBalResp, err := dave.WalletBalance(ctxt, daveBalReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get dave's balance: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if daveBalResp.ConfirmedBalance <= davePreSweepBalance {
|
|
|
|
predErr = fmt.Errorf("Dave should have more than %d "+
|
|
|
|
"after sweep, instead has %d",
|
|
|
|
davePreSweepBalance,
|
|
|
|
daveBalResp.ConfirmedBalance)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", predErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dave should have no open channels.
|
|
|
|
assertNodeNumChannels(t, dave, 0)
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:54 +03:00
|
|
|
// assertNumPendingChannels checks that a PendingChannels response from the
|
|
|
|
// node reports the expected number of pending channels.
|
|
|
|
func assertNumPendingChannels(t *harnessTest, node *lntest.HarnessNode,
|
|
|
|
expWaitingClose, expPendingForceClose int) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
var predErr error
|
|
|
|
err := lntest.WaitPredicate(func() bool {
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
pendingChanResp, err := node.PendingChannels(ctxt,
|
2018-07-12 12:02:54 +03:00
|
|
|
pendingChansRequest)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
n := len(pendingChanResp.WaitingCloseChannels)
|
|
|
|
if n != expWaitingClose {
|
|
|
|
predErr = fmt.Errorf("Expected to find %d channels "+
|
|
|
|
"waiting close, found %d", expWaitingClose, n)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
n = len(pendingChanResp.PendingForceClosingChannels)
|
|
|
|
if n != expPendingForceClose {
|
|
|
|
predErr = fmt.Errorf("expected to find %d channel "+
|
|
|
|
"pending force close, found %d", expPendingForceClose, n)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", predErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 02:28:47 +03:00
|
|
|
// assertDLPExecuted asserts that Dave is a node that has recovered their state
|
|
|
|
// form scratch. Carol should then force close on chain, with Dave sweeping his
|
|
|
|
// funds immediately, and Carol sweeping her fund after her CSV delay is up. If
|
|
|
|
// the blankSlate value is true, then this means that Dave won't need to sweep
|
|
|
|
// on chain as he has no funds in the channel.
|
|
|
|
func assertDLPExecuted(net *lntest.NetworkHarness, t *harnessTest,
|
|
|
|
carol *lntest.HarnessNode, carolStartingBalance int64,
|
|
|
|
dave *lntest.HarnessNode, daveStartingBalance int64) {
|
|
|
|
|
|
|
|
// Upon reconnection, the nodes should detect that Dave is out of sync.
|
|
|
|
// Carol should force close the channel using her latest commitment.
|
|
|
|
ctxb := context.Background()
|
|
|
|
forceClose, err := waitForTxInMempool(
|
|
|
|
net.Miner.Node, minerMempoolTimeout,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find Carol's force close tx in mempool: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Channel should be in the state "waiting close" for Carol since she
|
|
|
|
// broadcasted the force close tx.
|
|
|
|
assertNumPendingChannels(t, carol, 1, 0)
|
|
|
|
|
|
|
|
// Dave should also consider the channel "waiting close", as he noticed
|
|
|
|
// the channel was out of sync, and is now waiting for a force close to
|
|
|
|
// hit the chain.
|
|
|
|
assertNumPendingChannels(t, dave, 1, 0)
|
|
|
|
|
|
|
|
// Restart Dave to make sure he is able to sweep the funds after
|
|
|
|
// shutdown.
|
|
|
|
if err := net.RestartNode(dave, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a single block, which should confirm the closing tx.
|
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
|
|
|
assertTxInBlock(t, block, forceClose)
|
|
|
|
|
|
|
|
// Dave should sweep his funds immediately, as they are not timelocked.
|
|
|
|
daveSweep, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find Dave's sweep tx in mempool: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dave should consider the channel pending force close (since he is
|
|
|
|
// waiting for his sweep to confirm).
|
|
|
|
assertNumPendingChannels(t, dave, 0, 1)
|
|
|
|
|
|
|
|
// Carol is considering it "pending force close", as we must wait
|
|
|
|
// before she can sweep her outputs.
|
|
|
|
assertNumPendingChannels(t, carol, 0, 1)
|
|
|
|
|
|
|
|
// Mine the sweep tx.
|
|
|
|
block = mineBlocks(t, net, 1, 1)[0]
|
|
|
|
assertTxInBlock(t, block, daveSweep)
|
|
|
|
|
|
|
|
// Now Dave should consider the channel fully closed.
|
|
|
|
assertNumPendingChannels(t, dave, 0, 0)
|
|
|
|
|
|
|
|
// We query Dave's balance to make sure it increased after the channel
|
|
|
|
// closed. This checks that he was able to sweep the funds he had in
|
|
|
|
// the channel.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
balReq := &lnrpc.WalletBalanceRequest{}
|
|
|
|
daveBalResp, err := dave.WalletBalance(ctxt, balReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get dave's balance: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
daveBalance := daveBalResp.ConfirmedBalance
|
|
|
|
if daveBalance <= daveStartingBalance {
|
|
|
|
t.Fatalf("expected dave to have balance above %d, "+
|
|
|
|
"instead had %v", daveStartingBalance, daveBalance)
|
|
|
|
}
|
|
|
|
|
|
|
|
// After the Carol's output matures, she should also reclaim her funds.
|
|
|
|
mineBlocks(t, net, defaultCSV-1, 0)
|
|
|
|
carolSweep, err := waitForTxInMempool(
|
|
|
|
net.Miner.Node, minerMempoolTimeout,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find Carol's sweep tx in mempool: %v", err)
|
|
|
|
}
|
|
|
|
block = mineBlocks(t, net, 1, 1)[0]
|
|
|
|
assertTxInBlock(t, block, carolSweep)
|
|
|
|
|
|
|
|
// Now the channel should be fully closed also from Carol's POV.
|
|
|
|
assertNumPendingChannels(t, carol, 0, 0)
|
|
|
|
|
|
|
|
// Make sure Carol got her balance back.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
carolBalResp, err := carol.WalletBalance(ctxt, balReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get carol's balance: %v", err)
|
|
|
|
}
|
|
|
|
carolBalance := carolBalResp.ConfirmedBalance
|
|
|
|
if carolBalance <= carolStartingBalance {
|
|
|
|
t.Fatalf("expected carol to have balance above %d, "+
|
|
|
|
"instead had %v", carolStartingBalance,
|
|
|
|
carolBalance)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNodeNumChannels(t, dave, 0)
|
|
|
|
assertNodeNumChannels(t, carol, 0)
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:54 +03:00
|
|
|
// testDataLossProtection tests that if one of the nodes in a channel
|
|
|
|
// relationship lost state, they will detect this during channel sync, and the
|
|
|
|
// up-to-date party will force close the channel, giving the outdated party the
|
2019-03-12 02:26:26 +03:00
|
|
|
// opportunity to sweep its output.
|
2018-07-12 12:02:54 +03:00
|
|
|
func testDataLossProtection(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
const (
|
2019-05-15 10:02:53 +03:00
|
|
|
chanAmt = lnd.MaxBtcFundingAmount
|
2018-07-12 12:02:54 +03:00
|
|
|
paymentAmt = 10000
|
|
|
|
numInvoices = 6
|
|
|
|
)
|
|
|
|
|
|
|
|
// Carol will be the up-to-date party. We set --nolisten to ensure Dave
|
|
|
|
// won't be able to connect to her and trigger the channel data
|
|
|
|
// protection logic automatically.
|
|
|
|
carol, err := net.NewNode("Carol", []string{"--nolisten"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new carol node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
|
|
|
// Dave will be the party losing his state.
|
|
|
|
dave, err := net.NewNode("Dave", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
|
|
|
// Before we make a channel, we'll load up Carol with some coins sent
|
|
|
|
// directly from the miner.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
2018-07-12 12:02:54 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// timeTravel is a method that will make Carol open a channel to the
|
|
|
|
// passed node, settle a series of payments, then reset the node back
|
|
|
|
// to the state before the payments happened. When this method returns
|
|
|
|
// the node will be unaware of the new state updates. The returned
|
|
|
|
// function can be used to restart the node in this state.
|
|
|
|
timeTravel := func(node *lntest.HarnessNode) (func() error,
|
|
|
|
*lnrpc.ChannelPoint, int64, error) {
|
2018-07-12 12:02:54 +03:00
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// We must let the node communicate with Carol before they are
|
|
|
|
// able to open channel, so we connect them.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.EnsureConnected(ctxt, carol, node); err != nil {
|
2018-11-20 17:09:44 +03:00
|
|
|
t.Fatalf("unable to connect %v to carol: %v",
|
|
|
|
node.Name(), err)
|
|
|
|
}
|
2018-07-12 12:02:54 +03:00
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// We'll first open up a channel between them with a 0.5 BTC
|
|
|
|
// value.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
|
|
|
ctxt, t, net, carol, node,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
2018-07-12 12:02:54 +03:00
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// With the channel open, we'll create a few invoices for the
|
|
|
|
// node that Carol will pay to in order to advance the state of
|
|
|
|
// the channel.
|
|
|
|
// TODO(halseth): have dangling HTLCs on the commitment, able to
|
|
|
|
// retrive funds?
|
|
|
|
payReqs, _, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
node, paymentAmt, numInvoices,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
2018-07-12 12:02:54 +03:00
|
|
|
if err != nil {
|
2018-11-20 17:09:44 +03:00
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2018-07-12 12:02:54 +03:00
|
|
|
}
|
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// Wait for Carol to receive the channel edge from the funding
|
|
|
|
// manager.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("carol didn't see the carol->%s channel "+
|
|
|
|
"before timeout: %v", node.Name(), err)
|
|
|
|
}
|
2018-07-12 12:02:54 +03:00
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// Send payments from Carol using 3 of the payment hashes
|
|
|
|
// generated above.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
err = completePaymentRequests(ctxt, carol,
|
|
|
|
payReqs[:numInvoices/2], true)
|
2018-07-12 12:02:54 +03:00
|
|
|
if err != nil {
|
2018-11-20 17:09:44 +03:00
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
2018-07-12 12:02:54 +03:00
|
|
|
}
|
2018-11-20 17:09:44 +03:00
|
|
|
|
|
|
|
// Next query for the node's channel state, as we sent 3
|
|
|
|
// payments of 10k satoshis each, it should now see his balance
|
|
|
|
// as being 30k satoshis.
|
|
|
|
var nodeChan *lnrpc.Channel
|
|
|
|
var predErr error
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
bChan, err := getChanInfo(ctxt, node)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get channel info: %v", err)
|
|
|
|
}
|
|
|
|
if bChan.LocalBalance != 30000 {
|
|
|
|
predErr = fmt.Errorf("balance is incorrect, "+
|
|
|
|
"got %v, expected %v",
|
|
|
|
bChan.LocalBalance, 30000)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeChan = bChan
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", predErr)
|
2018-07-12 12:02:54 +03:00
|
|
|
}
|
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// Grab the current commitment height (update number), we'll
|
|
|
|
// later revert him to this state after additional updates to
|
|
|
|
// revoke this state.
|
|
|
|
stateNumPreCopy := nodeChan.NumUpdates
|
2018-07-12 12:02:54 +03:00
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// Create a temporary file to house the database state at this
|
|
|
|
// particular point in history.
|
|
|
|
tempDbPath, err := ioutil.TempDir("", node.Name()+"-past-state")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp db folder: %v", err)
|
|
|
|
}
|
|
|
|
tempDbFile := filepath.Join(tempDbPath, "channel.db")
|
|
|
|
defer os.Remove(tempDbPath)
|
2018-07-12 12:02:54 +03:00
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// With the temporary file created, copy the current state into
|
|
|
|
// the temporary file we created above. Later after more
|
|
|
|
// updates, we'll restore this state.
|
2018-10-10 13:55:14 +03:00
|
|
|
if err := lntest.CopyFile(tempDbFile, node.DBPath()); err != nil {
|
2018-11-20 17:09:44 +03:00
|
|
|
t.Fatalf("unable to copy database files: %v", err)
|
|
|
|
}
|
2018-07-12 12:02:54 +03:00
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// Finally, send more payments from , using the remaining
|
|
|
|
// payment hashes.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
err = completePaymentRequests(ctxt, carol,
|
|
|
|
payReqs[numInvoices/2:], true)
|
2018-07-12 12:02:54 +03:00
|
|
|
if err != nil {
|
2018-11-20 17:09:44 +03:00
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
2018-07-12 12:02:54 +03:00
|
|
|
}
|
2018-11-20 17:09:44 +03:00
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
nodeChan, err = getChanInfo(ctxt, node)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get dave chan info: %v", err)
|
2018-07-12 12:02:54 +03:00
|
|
|
}
|
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// Now we shutdown the node, copying over the its temporary
|
|
|
|
// database state which has the *prior* channel state over his
|
|
|
|
// current most up to date state. With this, we essentially
|
|
|
|
// force the node to travel back in time within the channel's
|
|
|
|
// history.
|
|
|
|
if err = net.RestartNode(node, func() error {
|
|
|
|
return os.Rename(tempDbFile, node.DBPath())
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("unable to restart node: %v", err)
|
|
|
|
}
|
2018-07-12 12:02:54 +03:00
|
|
|
|
2019-02-06 23:47:41 +03:00
|
|
|
// Make sure the channel is still there from the PoV of the
|
|
|
|
// node.
|
|
|
|
assertNodeNumChannels(t, node, 1)
|
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// Now query for the channel state, it should show that it's at
|
|
|
|
// a state number in the past, not the *latest* state.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
nodeChan, err = getChanInfo(ctxt, node)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get dave chan info: %v", err)
|
|
|
|
}
|
|
|
|
if nodeChan.NumUpdates != stateNumPreCopy {
|
|
|
|
t.Fatalf("db copy failed: %v", nodeChan.NumUpdates)
|
|
|
|
}
|
2018-07-12 12:02:54 +03:00
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
balReq := &lnrpc.WalletBalanceRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
balResp, err := node.WalletBalance(ctxt, balReq)
|
2018-11-20 17:09:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get dave's balance: %v", err)
|
|
|
|
}
|
2018-07-12 12:02:54 +03:00
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
restart, err := net.SuspendNode(node)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to suspend node: %v", err)
|
|
|
|
}
|
|
|
|
return restart, chanPoint, balResp.ConfirmedBalance, nil
|
2018-07-12 12:02:54 +03:00
|
|
|
}
|
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// Reset Dave to a state where he has an outdated channel state.
|
|
|
|
restartDave, _, daveStartingBalance, err := timeTravel(dave)
|
2018-07-12 12:02:54 +03:00
|
|
|
if err != nil {
|
2018-11-20 17:09:44 +03:00
|
|
|
t.Fatalf("unable to time travel dave: %v", err)
|
2018-07-12 12:02:54 +03:00
|
|
|
}
|
|
|
|
|
2019-03-12 02:26:26 +03:00
|
|
|
// We make a note of the nodes' current on-chain balances, to make sure
|
|
|
|
// they are able to retrieve the channel funds eventually,
|
2018-11-20 17:09:44 +03:00
|
|
|
balReq := &lnrpc.WalletBalanceRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
carolBalResp, err := carol.WalletBalance(ctxt, balReq)
|
2018-07-12 12:02:54 +03:00
|
|
|
if err != nil {
|
2018-11-20 17:09:44 +03:00
|
|
|
t.Fatalf("unable to get carol's balance: %v", err)
|
2018-07-12 12:02:54 +03:00
|
|
|
}
|
2018-11-20 17:09:44 +03:00
|
|
|
carolStartingBalance := carolBalResp.ConfirmedBalance
|
2018-07-12 12:02:54 +03:00
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
// Restart Dave to trigger a channel resync.
|
|
|
|
if err := restartDave(); err != nil {
|
|
|
|
t.Fatalf("unable to restart dave: %v", err)
|
2018-07-12 12:02:54 +03:00
|
|
|
}
|
|
|
|
|
2019-03-12 02:28:47 +03:00
|
|
|
// Assert that once Dave comes up, they reconnect, Carol force closes
|
|
|
|
// on chain, and both of them properly carry out the DLP protocol.
|
|
|
|
assertDLPExecuted(
|
|
|
|
net, t, carol, carolStartingBalance, dave, daveStartingBalance,
|
|
|
|
)
|
2018-11-20 17:09:46 +03:00
|
|
|
|
2019-03-12 02:28:47 +03:00
|
|
|
// As a second part of this test, we will test the scenario where a
|
2018-11-20 17:09:46 +03:00
|
|
|
// channel is closed while Dave is offline, loses his state and comes
|
|
|
|
// back online. In this case the node should attempt to resync the
|
|
|
|
// channel, and the peer should resend a channel sync message for the
|
|
|
|
// closed channel, such that Dave can retrieve his funds.
|
|
|
|
//
|
|
|
|
// We start by letting Dave time travel back to an outdated state.
|
|
|
|
restartDave, chanPoint2, daveStartingBalance, err := timeTravel(dave)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to time travel eve: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
carolBalResp, err = carol.WalletBalance(ctxt, balReq)
|
2018-11-20 17:09:46 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get carol's balance: %v", err)
|
|
|
|
}
|
|
|
|
carolStartingBalance = carolBalResp.ConfirmedBalance
|
|
|
|
|
|
|
|
// Now let Carol force close the channel while Dave is offline.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-11-20 17:09:46 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPoint2, true)
|
|
|
|
|
|
|
|
// Wait for the channel to be marked pending force close.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:46 +03:00
|
|
|
err = waitForChannelPendingForceClose(ctxt, carol, chanPoint2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("channel not pending force close: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mine enough blocks for Carol to sweep her funds.
|
2018-11-29 14:33:11 +03:00
|
|
|
mineBlocks(t, net, defaultCSV, 0)
|
2018-11-20 17:09:46 +03:00
|
|
|
|
2019-03-12 02:28:47 +03:00
|
|
|
carolSweep, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2018-11-20 17:09:46 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find Carol's sweep tx in mempool: %v", err)
|
|
|
|
}
|
2019-03-12 02:28:47 +03:00
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
2018-11-20 17:09:46 +03:00
|
|
|
assertTxInBlock(t, block, carolSweep)
|
|
|
|
|
|
|
|
// Now the channel should be fully closed also from Carol's POV.
|
|
|
|
assertNumPendingChannels(t, carol, 0, 0)
|
|
|
|
|
|
|
|
// Make sure Carol got her balance back.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
carolBalResp, err = carol.WalletBalance(ctxt, balReq)
|
2018-11-20 17:09:46 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get carol's balance: %v", err)
|
|
|
|
}
|
2019-03-12 02:28:47 +03:00
|
|
|
carolBalance := carolBalResp.ConfirmedBalance
|
2018-11-20 17:09:46 +03:00
|
|
|
if carolBalance <= carolStartingBalance {
|
|
|
|
t.Fatalf("expected carol to have balance above %d, "+
|
|
|
|
"instead had %v", carolStartingBalance,
|
|
|
|
carolBalance)
|
|
|
|
}
|
|
|
|
|
2018-11-28 11:25:27 +03:00
|
|
|
assertNodeNumChannels(t, carol, 0)
|
2018-11-20 17:09:46 +03:00
|
|
|
|
|
|
|
// When Dave comes online, he will reconnect to Carol, try to resync
|
|
|
|
// the channel, but it will already be closed. Carol should resend the
|
|
|
|
// information Dave needs to sweep his funds.
|
|
|
|
if err := restartDave(); err != nil {
|
2019-05-05 01:35:37 +03:00
|
|
|
t.Fatalf("unable to restart Eve: %v", err)
|
2018-11-20 17:09:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dave should sweep his funds.
|
2018-11-27 11:12:16 +03:00
|
|
|
_, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2018-11-20 17:09:46 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find Dave's sweep tx in mempool: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mine a block to confirm the sweep, and make sure Dave got his
|
|
|
|
// balance back.
|
2018-11-29 14:33:11 +03:00
|
|
|
mineBlocks(t, net, 1, 1)
|
2018-11-28 11:25:27 +03:00
|
|
|
assertNodeNumChannels(t, dave, 0)
|
2018-11-20 17:09:46 +03:00
|
|
|
|
2019-04-23 14:07:43 +03:00
|
|
|
err = lntest.WaitNoError(func() error {
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
daveBalResp, err := dave.WalletBalance(ctxt, balReq)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get dave's balance: %v",
|
|
|
|
err)
|
|
|
|
}
|
2018-11-20 17:09:46 +03:00
|
|
|
|
2019-04-23 14:07:43 +03:00
|
|
|
daveBalance := daveBalResp.ConfirmedBalance
|
|
|
|
if daveBalance <= daveStartingBalance {
|
|
|
|
return fmt.Errorf("expected dave to have balance "+
|
|
|
|
"above %d, intead had %v", daveStartingBalance,
|
|
|
|
daveBalance)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", err)
|
2018-11-20 17:09:46 +03:00
|
|
|
}
|
2018-07-12 12:02:54 +03:00
|
|
|
}
|
|
|
|
|
2018-01-23 05:59:44 +03:00
|
|
|
// assertNodeNumChannels polls the provided node's list channels rpc until it
|
2017-12-15 00:56:10 +03:00
|
|
|
// reaches the desired number of total channels.
|
2018-11-28 11:25:27 +03:00
|
|
|
func assertNodeNumChannels(t *harnessTest, node *lntest.HarnessNode,
|
|
|
|
numChannels int) {
|
|
|
|
ctxb := context.Background()
|
2017-12-15 00:56:10 +03:00
|
|
|
|
2018-07-12 12:02:54 +03:00
|
|
|
// Poll node for its list of channels.
|
2017-08-01 09:17:13 +03:00
|
|
|
req := &lnrpc.ListChannelsRequest{}
|
2017-12-15 00:56:10 +03:00
|
|
|
|
|
|
|
var predErr error
|
|
|
|
pred := func() bool {
|
2018-11-28 11:25:27 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
chanInfo, err := node.ListChannels(ctxt, req)
|
2017-12-15 00:56:10 +03:00
|
|
|
if err != nil {
|
2018-07-12 12:02:54 +03:00
|
|
|
predErr = fmt.Errorf("unable to query for node's "+
|
2017-12-15 00:56:10 +03:00
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the query returned the expected number of
|
|
|
|
// channels.
|
2018-04-13 14:18:35 +03:00
|
|
|
num := len(chanInfo.Channels)
|
|
|
|
if num != numChannels {
|
|
|
|
predErr = fmt.Errorf("expected %v channels, got %v",
|
|
|
|
numChannels, num)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2017-08-01 09:17:13 +03:00
|
|
|
}
|
2017-12-15 00:56:10 +03:00
|
|
|
|
|
|
|
if err := lntest.WaitPredicate(pred, time.Second*15); err != nil {
|
|
|
|
t.Fatalf("node has incorrect number of channels: %v", predErr)
|
2017-08-01 09:17:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
func testHtlcErrorPropagation(net *lntest.NetworkHarness, t *harnessTest) {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2017-01-08 07:25:45 +03:00
|
|
|
// 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.
|
2019-05-15 10:02:53 +03:00
|
|
|
const chanAmt = lnd.MaxBtcFundingAmount
|
2017-01-08 07:25:45 +03:00
|
|
|
|
|
|
|
// First establish a channel with a capacity of 0.5 BTC between Alice
|
|
|
|
// and Bob.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-03-15 06:06:33 +03:00
|
|
|
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{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
aliceBal, err := net.Alice.ChannelBalance(ctxt, balReq)
|
2017-01-08 07:25:45 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get channel balance: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
bobBal, err := net.Bob.ChannelBalance(ctxt, balReq)
|
2017-01-08 07:25:45 +03:00
|
|
|
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.
|
2018-04-27 11:42:04 +03:00
|
|
|
carol, err := net.NewNode("Carol", nil)
|
2017-01-08 07:25:45 +03:00
|
|
|
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.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Bob, carol); err != nil {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("unable to connect bob to carol: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2019-05-15 10:02:53 +03:00
|
|
|
const bobChanAmt = lnd.MaxBtcFundingAmount
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointBob := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Bob, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
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:
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
_, err := net.Alice.GetNodeInfo(ctxt, nodeInfoReq)
|
2017-01-30 04:16:49 +03:00
|
|
|
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,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
carolInvoice, err := carol.AddInvoice(ctxt, invoiceReq)
|
2017-01-08 07:25:45 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate carol invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-03-26 14:05:43 +03:00
|
|
|
carolPayReq, err := carol.DecodePayReq(ctxb,
|
|
|
|
&lnrpc.PayReqString{
|
|
|
|
PayReq: carolInvoice.PaymentRequest,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to decode generated payment request: %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.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-03-15 06:06:33 +03:00
|
|
|
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
|
|
|
// For the first scenario, we'll test the cancellation of an HTLC with
|
|
|
|
// an unknown payment hash.
|
2017-10-26 01:32:00 +03:00
|
|
|
// TODO(roasbeef): return failure response rather than failing entire
|
|
|
|
// stream on payment error.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-01-08 07:25:45 +03:00
|
|
|
sendReq := &lnrpc.SendRequest{
|
2018-08-12 16:23:08 +03:00
|
|
|
PaymentHashString: hex.EncodeToString(makeFakePayHash(t)),
|
2017-10-26 01:32:00 +03:00
|
|
|
DestString: hex.EncodeToString(carol.PubKey[:]),
|
|
|
|
Amt: payAmt,
|
2019-03-26 14:05:43 +03:00
|
|
|
FinalCltvDelta: int32(carolPayReq.CltvExpiry),
|
2017-01-08 07:25:45 +03:00
|
|
|
}
|
2017-10-26 01:32:00 +03:00
|
|
|
resp, err := net.Alice.SendPaymentSync(ctxt, sendReq)
|
|
|
|
if err != nil {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// The payment should have resulted in an error since we sent it with the
|
2017-01-08 07:25:45 +03:00
|
|
|
// wrong payment hash.
|
2017-10-26 01:32:00 +03:00
|
|
|
if resp.PaymentError == "" {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("payment should have been rejected due to invalid " +
|
|
|
|
"payment hash")
|
2017-10-26 01:32:00 +03:00
|
|
|
}
|
|
|
|
expectedErrorCode := lnwire.CodeUnknownPaymentHash.String()
|
|
|
|
if !strings.Contains(resp.PaymentError, expectedErrorCode) {
|
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()
|
|
|
|
|
|
|
|
// Next, we'll test the case of a recognized payHash but, an incorrect
|
|
|
|
// value on the extended HTLC.
|
2019-01-29 04:41:56 +03:00
|
|
|
htlcAmt := lnwire.NewMSatFromSatoshis(1000)
|
2017-01-08 07:25:45 +03:00
|
|
|
sendReq = &lnrpc.SendRequest{
|
2017-10-26 01:32:00 +03:00
|
|
|
PaymentHashString: hex.EncodeToString(carolInvoice.RHash),
|
|
|
|
DestString: hex.EncodeToString(carol.PubKey[:]),
|
2019-01-29 04:41:56 +03:00
|
|
|
Amt: int64(htlcAmt.ToSatoshis()), // 10k satoshis are expected.
|
2019-03-26 14:05:43 +03:00
|
|
|
FinalCltvDelta: int32(carolPayReq.CltvExpiry),
|
2017-01-08 07:25:45 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-10-26 01:32:00 +03:00
|
|
|
resp, err = net.Alice.SendPaymentSync(ctxt, sendReq)
|
|
|
|
if err != nil {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-26 01:32:00 +03:00
|
|
|
// The payment should fail with an error since we sent 1k satoshis isn't of
|
|
|
|
// 10k as was requested.
|
|
|
|
if resp.PaymentError == "" {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("payment should have been rejected due to wrong " +
|
|
|
|
"HTLC amount")
|
2017-10-26 01:32:00 +03:00
|
|
|
}
|
2019-01-29 04:41:56 +03:00
|
|
|
expectedErrorCode = lnwire.CodeUnknownPaymentHash.String()
|
2017-10-26 01:32:00 +03:00
|
|
|
if !strings.Contains(resp.PaymentError, expectedErrorCode) {
|
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)
|
2019-01-29 04:41:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll also ensure that the encoded error includes the invlaid HTLC
|
|
|
|
// amount.
|
|
|
|
if !strings.Contains(resp.PaymentError, htlcAmt.String()) {
|
|
|
|
t.Fatalf("error didn't include expected payment amt of %v: "+
|
|
|
|
"%v", htlcAmt, 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.
|
2018-11-28 11:15:58 +03:00
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
bobPayStream, err := net.Bob.SendPayment(ctx)
|
2017-01-08 07:25:45 +03:00
|
|
|
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.
|
2017-11-29 16:03:50 +03:00
|
|
|
chanReserve := int64(chanAmt / 100)
|
|
|
|
amtToSend := int64(chanAmt) - chanReserve - 20000
|
2017-08-22 10:34:11 +03:00
|
|
|
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.
|
2019-05-15 10:02:53 +03:00
|
|
|
toSend := int64(lnd.MaxPaymentMSat.ToSatoshis())
|
2017-08-22 10:34:11 +03:00
|
|
|
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,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
carolInvoice2, err := carol.AddInvoice(ctxt, invoiceReq)
|
2017-08-22 10:34:11 +03:00
|
|
|
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.
|
|
|
|
invoiceReq = &lnrpc.Invoice{
|
|
|
|
Value: 100000,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
carolInvoice3, err := carol.AddInvoice(ctxt, invoiceReq)
|
2017-01-08 07:25:45 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate carol invoice: %v", err)
|
|
|
|
}
|
2017-10-26 01:32:00 +03:00
|
|
|
|
|
|
|
sendReq = &lnrpc.SendRequest{
|
2017-01-08 07:25:45 +03:00
|
|
|
PaymentRequest: carolInvoice3.PaymentRequest,
|
2017-10-26 01:32:00 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-10-26 01:32:00 +03:00
|
|
|
resp, err = net.Alice.SendPaymentSync(ctxt, sendReq)
|
|
|
|
if err != nil {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
2017-10-26 01:32:00 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-12 16:23:08 +03:00
|
|
|
// Generate new invoice to not pay same invoice twice.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
carolInvoice, err = carol.AddInvoice(ctxt, invoiceReq)
|
2018-08-12 16:23:08 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate carol invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
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.
|
2018-06-08 00:14:39 +03:00
|
|
|
shutdownAndAssert(net, t, carol)
|
|
|
|
|
2019-03-19 19:09:27 +03:00
|
|
|
// Reset mission control to forget the temporary channel failure above.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
_, err = net.Alice.RouterClient.ResetMissionControl(
|
|
|
|
ctxt, &routerrpc.ResetMissionControlRequest{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to reset mission control: %v", err)
|
|
|
|
}
|
2017-10-26 01:32:00 +03:00
|
|
|
|
|
|
|
sendReq = &lnrpc.SendRequest{
|
2017-01-08 07:25:45 +03:00
|
|
|
PaymentRequest: carolInvoice.PaymentRequest,
|
2017-10-26 01:32:00 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-10-26 01:32:00 +03:00
|
|
|
resp, err = net.Alice.SendPaymentSync(ctxt, sendReq)
|
|
|
|
if err != nil {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
2017-05-19 15:18:21 +03:00
|
|
|
|
2017-10-26 01:32:00 +03:00
|
|
|
if resp.PaymentError == "" {
|
2017-01-08 07:25:45 +03:00
|
|
|
t.Fatalf("payment should have failed")
|
2017-10-26 01:32:00 +03:00
|
|
|
}
|
|
|
|
expectedErrorCode = lnwire.CodeUnknownNextPeer.String()
|
|
|
|
if !strings.Contains(resp.PaymentError, expectedErrorCode) {
|
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.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
2017-01-08 07:25:45 +03:00
|
|
|
|
2018-11-06 13:03:01 +03:00
|
|
|
// Force close Bob's final channel.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-02-23 01:49:04 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Bob, chanPointBob, true)
|
2018-11-06 13:03:01 +03:00
|
|
|
|
|
|
|
// Cleanup by mining the force close and sweep transaction.
|
|
|
|
cleanupForceClose(t, net, net.Bob, chanPointBob)
|
2017-01-08 07:25:45 +03:00
|
|
|
}
|
|
|
|
|
2018-08-23 08:16:16 +03:00
|
|
|
// graphSubscription houses the proxied update and error chans for a node's
|
|
|
|
// graph subscriptions.
|
|
|
|
type graphSubscription struct {
|
|
|
|
updateChan chan *lnrpc.GraphTopologyUpdate
|
|
|
|
errChan chan error
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
2017-12-22 15:49:49 +03:00
|
|
|
// subscribeGraphNotifications subscribes to channel graph updates and launches
|
|
|
|
// a goroutine that forwards these to the returned channel.
|
|
|
|
func subscribeGraphNotifications(t *harnessTest, ctxb context.Context,
|
2018-08-23 08:16:16 +03:00
|
|
|
node *lntest.HarnessNode) graphSubscription {
|
|
|
|
|
2017-12-22 15:49:49 +03:00
|
|
|
// We'll first start by establishing a notification client which will
|
|
|
|
// send us notifications upon detected changes in the channel graph.
|
2017-03-14 08:23:19 +03:00
|
|
|
req := &lnrpc.GraphTopologySubscription{}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctx, cancelFunc := context.WithCancel(ctxb)
|
2018-04-25 06:34:56 +03:00
|
|
|
topologyClient, err := node.SubscribeChannelGraph(ctx, req)
|
2017-03-14 08:23:19 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create topology client: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// We'll launch a goroutine that will be responsible for proxying all
|
2017-03-14 08:23:19 +03:00
|
|
|
// notifications recv'd from the client into the channel below.
|
2018-08-23 08:16:16 +03:00
|
|
|
errChan := make(chan error, 1)
|
2017-03-14 08:23:19 +03:00
|
|
|
quit := make(chan struct{})
|
2017-12-22 15:49:49 +03:00
|
|
|
graphUpdates := make(chan *lnrpc.GraphTopologyUpdate, 20)
|
2017-03-14 08:23:19 +03:00
|
|
|
go func() {
|
|
|
|
for {
|
2018-04-25 06:34:56 +03:00
|
|
|
defer cancelFunc()
|
|
|
|
|
2017-03-14 08:23:19 +03:00
|
|
|
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 {
|
2018-08-23 08:16:16 +03:00
|
|
|
select {
|
|
|
|
case errChan <- err:
|
|
|
|
case <-quit:
|
|
|
|
}
|
|
|
|
return
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
|
|
|
|
2017-08-05 04:34:10 +03:00
|
|
|
select {
|
|
|
|
case graphUpdates <- graphUpdate:
|
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
}
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2018-08-23 08:16:16 +03:00
|
|
|
|
|
|
|
return graphSubscription{
|
|
|
|
updateChan: graphUpdates,
|
|
|
|
errChan: errChan,
|
|
|
|
quit: quit,
|
|
|
|
}
|
2017-12-22 15:49:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func testGraphTopologyNotifications(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
const chanAmt = lnd.MaxBtcFundingAmount
|
2018-11-28 11:25:46 +03:00
|
|
|
|
2017-12-22 15:49:49 +03:00
|
|
|
// Let Alice subscribe to graph notifications.
|
2018-08-23 08:16:16 +03:00
|
|
|
graphSub := subscribeGraphNotifications(
|
|
|
|
t, ctxb, net.Alice,
|
|
|
|
)
|
|
|
|
defer close(graphSub.quit)
|
2017-12-22 15:49:49 +03:00
|
|
|
|
|
|
|
// Open a new channel between Alice and Bob.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-03-14 08:23:19 +03:00
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// The channel opening above should have triggered a few notifications
|
2017-07-14 22:47:52 +03:00
|
|
|
// sent to the notification client. We'll expect two channel updates,
|
|
|
|
// and two node announcements.
|
2018-10-02 02:45:11 +03:00
|
|
|
var numChannelUpds int
|
|
|
|
var numNodeAnns int
|
|
|
|
for numChannelUpds < 2 && numNodeAnns < 2 {
|
2017-03-14 08:23:19 +03:00
|
|
|
select {
|
|
|
|
// Ensure that a new update for both created edges is properly
|
|
|
|
// dispatched to our registered client.
|
2018-08-23 08:16:16 +03:00
|
|
|
case graphUpdate := <-graphSub.updateChan:
|
2018-10-02 02:45:11 +03:00
|
|
|
// Process all channel updates prsented in this update
|
|
|
|
// message.
|
|
|
|
for _, chanUpdate := range graphUpdate.ChannelUpdates {
|
2017-07-14 22:47:52 +03:00
|
|
|
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)
|
|
|
|
}
|
2018-10-02 02:45:11 +03:00
|
|
|
|
|
|
|
if chanUpdate.Capacity != int64(chanAmt) {
|
|
|
|
t.Fatalf("channel capacities mismatch:"+
|
|
|
|
" expected %v, got %v", chanAmt,
|
|
|
|
btcutil.Amount(chanUpdate.Capacity))
|
|
|
|
}
|
|
|
|
numChannelUpds++
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
|
|
|
|
2018-10-02 02:45:11 +03:00
|
|
|
for _, nodeUpdate := range graphUpdate.NodeUpdates {
|
2017-07-14 22:47:52 +03:00
|
|
|
switch nodeUpdate.IdentityKey {
|
|
|
|
case net.Alice.PubKeyStr:
|
|
|
|
case net.Bob.PubKeyStr:
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown node: %v",
|
|
|
|
nodeUpdate.IdentityKey)
|
|
|
|
}
|
2018-10-02 02:45:11 +03:00
|
|
|
numNodeAnns++
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
2018-08-23 08:16:16 +03:00
|
|
|
case err := <-graphSub.errChan:
|
|
|
|
t.Fatalf("unable to recv graph update: %v", err)
|
2017-03-15 06:06:33 +03:00
|
|
|
case <-time.After(time.Second * 10):
|
2018-10-02 02:45:11 +03:00
|
|
|
t.Fatalf("timeout waiting for graph notifications, "+
|
|
|
|
"only received %d/2 chanupds and %d/2 nodeanns",
|
|
|
|
numChannelUpds, numNodeAnns)
|
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)
|
|
|
|
}
|
|
|
|
|
2017-10-26 01:32:00 +03:00
|
|
|
// Now we'll test that updates are properly sent after channels are closed
|
|
|
|
// within the network.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-03-14 08:23:19 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
|
|
|
|
2018-06-15 04:16:28 +03:00
|
|
|
// Now that the channel has been closed, we should receive a
|
|
|
|
// notification indicating so.
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
2018-08-23 08:16:16 +03:00
|
|
|
case graphUpdate := <-graphSub.updateChan:
|
2018-06-15 04:16:28 +03:00
|
|
|
if len(graphUpdate.ClosedChans) != 1 {
|
|
|
|
continue
|
|
|
|
}
|
2017-03-14 08:23:19 +03:00
|
|
|
|
2018-06-15 04:16:28 +03:00
|
|
|
closedChan := graphUpdate.ClosedChans[0]
|
|
|
|
if closedChan.ClosedHeight != uint32(blockHeight+1) {
|
|
|
|
t.Fatalf("close heights of channel mismatch: "+
|
|
|
|
"expected %v, got %v", blockHeight+1,
|
|
|
|
closedChan.ClosedHeight)
|
|
|
|
}
|
2019-05-15 10:02:53 +03:00
|
|
|
chanPointTxid, err := lnd.GetChanPointFundingTxid(chanPoint)
|
2018-06-15 04:16:28 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2019-05-15 10:02:53 +03:00
|
|
|
closedChanTxid, err := lnd.GetChanPointFundingTxid(
|
2018-06-15 04:16:28 +03:00
|
|
|
closedChan.ChanPoint,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2019-03-12 02:28:47 +03:00
|
|
|
if !bytes.Equal(closedChanTxid[:], chanPointTxid[:]) {
|
2018-06-15 04:16:28 +03:00
|
|
|
t.Fatalf("channel point hash mismatch: "+
|
|
|
|
"expected %v, got %v", chanPointTxid,
|
|
|
|
closedChanTxid)
|
|
|
|
}
|
|
|
|
if closedChan.ChanPoint.OutputIndex != chanPoint.OutputIndex {
|
|
|
|
t.Fatalf("output index mismatch: expected %v, "+
|
|
|
|
"got %v", chanPoint.OutputIndex,
|
|
|
|
closedChan.ChanPoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
break out
|
2018-08-23 08:16:16 +03:00
|
|
|
|
|
|
|
case err := <-graphSub.errChan:
|
|
|
|
t.Fatalf("unable to recv graph update: %v", err)
|
2018-06-15 04:16:28 +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.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-07-14 22:47:52 +03:00
|
|
|
if err := net.DisconnectNodes(ctxt, net.Alice, net.Bob); err != nil {
|
|
|
|
t.Fatalf("unable to disconnect alice and bob: %v", err)
|
|
|
|
}
|
2018-04-27 11:42:04 +03:00
|
|
|
carol, err := net.NewNode("Carol", nil)
|
2017-03-14 08:23:19 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
2017-03-14 08:23:19 +03:00
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Bob, carol); err != nil {
|
2017-07-14 22:47:52 +03:00
|
|
|
t.Fatalf("unable to connect bob to carol: %v", err)
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint = openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Bob, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-03-14 08:23:19 +03:00
|
|
|
|
2017-07-14 22:47:52 +03:00
|
|
|
// 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.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.EnsureConnected(ctxt, net.Alice, net.Bob); err != nil {
|
2017-07-14 22:47:52 +03:00
|
|
|
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.
|
2018-10-02 02:45:11 +03:00
|
|
|
numNodeAnns = 0
|
|
|
|
numChannelUpds = 0
|
|
|
|
for numChannelUpds < 2 && numNodeAnns < 1 {
|
2017-07-14 22:47:52 +03:00
|
|
|
select {
|
2018-08-23 08:16:16 +03:00
|
|
|
case graphUpdate := <-graphSub.updateChan:
|
2018-10-02 02:45:11 +03:00
|
|
|
for _, nodeUpdate := range graphUpdate.NodeUpdates {
|
2017-07-14 22:47:52 +03:00
|
|
|
switch nodeUpdate.IdentityKey {
|
|
|
|
case carol.PubKeyStr:
|
|
|
|
case net.Bob.PubKeyStr:
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown node update pubey: %v",
|
|
|
|
nodeUpdate.IdentityKey)
|
|
|
|
}
|
2018-10-02 02:45:11 +03:00
|
|
|
numNodeAnns++
|
2017-07-14 22:47:52 +03:00
|
|
|
}
|
|
|
|
|
2018-10-02 02:45:11 +03:00
|
|
|
for _, chanUpdate := range graphUpdate.ChannelUpdates {
|
2017-07-14 22:47:52 +03:00
|
|
|
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)
|
|
|
|
}
|
2018-10-02 02:45:11 +03:00
|
|
|
|
|
|
|
if chanUpdate.Capacity != int64(chanAmt) {
|
|
|
|
t.Fatalf("channel capacities mismatch:"+
|
|
|
|
" expected %v, got %v", chanAmt,
|
|
|
|
btcutil.Amount(chanUpdate.Capacity))
|
|
|
|
}
|
|
|
|
numChannelUpds++
|
2017-07-14 22:47:52 +03:00
|
|
|
}
|
2018-08-23 08:16:16 +03:00
|
|
|
case err := <-graphSub.errChan:
|
|
|
|
t.Fatalf("unable to recv graph update: %v", err)
|
2017-07-14 22:47:52 +03:00
|
|
|
case <-time.After(time.Second * 10):
|
2018-10-02 02:45:11 +03:00
|
|
|
t.Fatalf("timeout waiting for graph notifications, "+
|
|
|
|
"only received %d/2 chanupds and %d/2 nodeanns",
|
|
|
|
numChannelUpds, numNodeAnns)
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 22:47:52 +03:00
|
|
|
// Close the channel between Bob and Carol.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-07-14 22:47:52 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Bob, chanPoint, false)
|
2017-03-14 08:23:19 +03:00
|
|
|
}
|
|
|
|
|
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.
|
2017-11-03 21:52:02 +03:00
|
|
|
func testNodeAnnouncement(net *lntest.NetworkHarness, t *harnessTest) {
|
2017-02-23 03:24:22 +03:00
|
|
|
ctxb := context.Background()
|
2018-11-28 11:25:46 +03:00
|
|
|
|
2018-08-23 08:16:16 +03:00
|
|
|
aliceSub := subscribeGraphNotifications(t, ctxb, net.Alice)
|
|
|
|
defer close(aliceSub.quit)
|
2017-02-23 03:24:22 +03:00
|
|
|
|
2018-05-24 09:54:12 +03:00
|
|
|
advertisedAddrs := []string{
|
|
|
|
"192.168.1.1:8333",
|
|
|
|
"[2001:db8:85a3:8d3:1319:8a2e:370:7348]:8337",
|
2018-09-21 05:23:34 +03:00
|
|
|
"bkb6azqggsaiskzi.onion:9735",
|
|
|
|
"fomvuglh6h6vcag73xo5t5gv56ombih3zr2xvplkpbfd7wrog4swjwid.onion:1234",
|
2017-02-23 03:24:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var lndArgs []string
|
2018-05-24 09:54:12 +03:00
|
|
|
for _, addr := range advertisedAddrs {
|
|
|
|
lndArgs = append(lndArgs, "--externalip="+addr)
|
2017-02-23 03:24:22 +03:00
|
|
|
}
|
|
|
|
|
2018-04-27 11:42:04 +03:00
|
|
|
dave, err := net.NewNode("Dave", lndArgs)
|
2017-02-23 03:24:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, dave)
|
2017-02-23 03:24:22 +03:00
|
|
|
|
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,
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Bob, dave); err != nil {
|
2017-07-14 22:47:52 +03:00
|
|
|
t.Fatalf("unable to connect bob to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Bob, dave,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: 1000000,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-07-14 22:47:52 +03:00
|
|
|
|
2018-05-24 09:54:12 +03:00
|
|
|
// When Alice now connects with Dave, Alice will get his node
|
|
|
|
// announcement.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Alice, dave); err != nil {
|
2017-02-23 03:24:22 +03:00
|
|
|
t.Fatalf("unable to connect bob to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-05-24 09:54:12 +03:00
|
|
|
assertAddrs := func(addrsFound []string, targetAddrs ...string) {
|
|
|
|
addrs := make(map[string]struct{}, len(addrsFound))
|
|
|
|
for _, addr := range addrsFound {
|
|
|
|
addrs[addr] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range targetAddrs {
|
|
|
|
if _, ok := addrs[addr]; !ok {
|
|
|
|
t.Fatalf("address %v not found in node "+
|
|
|
|
"announcement", addr)
|
|
|
|
}
|
|
|
|
}
|
2017-02-23 03:24:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-23 08:16:16 +03:00
|
|
|
waitForAddrsInUpdate := func(graphSub graphSubscription,
|
2018-05-24 09:54:12 +03:00
|
|
|
nodePubKey string, targetAddrs ...string) {
|
2017-02-23 03:24:22 +03:00
|
|
|
|
2018-05-24 09:54:12 +03:00
|
|
|
for {
|
|
|
|
select {
|
2018-08-23 08:16:16 +03:00
|
|
|
case graphUpdate := <-graphSub.updateChan:
|
2018-05-24 09:54:12 +03:00
|
|
|
for _, update := range graphUpdate.NodeUpdates {
|
|
|
|
if update.IdentityKey == nodePubKey {
|
|
|
|
assertAddrs(
|
|
|
|
update.Addresses,
|
|
|
|
targetAddrs...,
|
|
|
|
)
|
|
|
|
return
|
2017-03-25 11:40:33 +03:00
|
|
|
}
|
2017-02-23 03:24:22 +03:00
|
|
|
}
|
2018-08-23 08:16:16 +03:00
|
|
|
case err := <-graphSub.errChan:
|
|
|
|
t.Fatalf("unable to recv graph update: %v", err)
|
2018-05-24 09:54:12 +03:00
|
|
|
case <-time.After(20 * time.Second):
|
|
|
|
t.Fatalf("did not receive node ann update")
|
2017-02-23 03:24:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-24 09:54:12 +03:00
|
|
|
|
2018-08-23 08:16:16 +03:00
|
|
|
waitForAddrsInUpdate(
|
|
|
|
aliceSub, dave.PubKeyStr, advertisedAddrs...,
|
|
|
|
)
|
2017-04-17 01:27:28 +03:00
|
|
|
|
2017-07-14 22:47:52 +03:00
|
|
|
// Close the channel between Bob and Dave.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-07-14 22:47:52 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Bob, chanPoint, false)
|
2017-02-23 03:24:22 +03:00
|
|
|
}
|
|
|
|
|
2017-11-03 21:52:02 +03:00
|
|
|
func testNodeSignVerify(net *lntest.NetworkHarness, t *harnessTest) {
|
2017-04-20 05:28:10 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
chanAmt := lnd.MaxBtcFundingAmount
|
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.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
aliceBobCh := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
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}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
sigResp, err := net.Alice.SignMessage(ctxt, sigReq)
|
2017-04-20 05:28:10 +03:00
|
|
|
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}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
verifyResp, err := net.Bob.VerifyMessage(ctxt, verifyReq)
|
2017-04-20 05:28:10 +03:00
|
|
|
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.
|
2018-04-27 11:42:04 +03:00
|
|
|
carol, err := net.NewNode("Carol", nil)
|
2017-04-20 05:28:10 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
2017-04-20 05:28:10 +03:00
|
|
|
|
|
|
|
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}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
sigResp, err = carol.SignMessage(ctxt, sigReq)
|
2017-04-20 05:28:10 +03:00
|
|
|
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}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
verifyResp, err = net.Bob.VerifyMessage(ctxt, verifyReq)
|
2017-04-20 05:28:10 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the channel between alice and bob.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-04-29 14:44:29 +03:00
|
|
|
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.
|
2017-11-03 21:52:02 +03:00
|
|
|
func testAsyncPayments(net *lntest.NetworkHarness, t *harnessTest) {
|
2017-05-17 19:06:52 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
const (
|
|
|
|
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.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-02-22 13:14:41 +03:00
|
|
|
channelCapacity := btcutil.Amount(paymentAmt * 2000)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: channelCapacity,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-05-17 19:06:52 +03:00
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
info, err := getChanInfo(ctxt, net.Alice)
|
2017-05-17 19:06:52 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get alice channel info: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-29 16:03:50 +03:00
|
|
|
// Calculate the number of invoices. We will deplete the channel
|
|
|
|
// all the way down to the channel reserve.
|
2018-02-22 13:14:41 +03:00
|
|
|
chanReserve := channelCapacity / 100
|
|
|
|
availableBalance := btcutil.Amount(info.LocalBalance) - chanReserve
|
|
|
|
numInvoices := int(availableBalance / paymentAmt)
|
|
|
|
|
2017-05-17 19:06:52 +03:00
|
|
|
bobAmt := int64(numInvoices * paymentAmt)
|
|
|
|
aliceAmt := info.LocalBalance - bobAmt
|
|
|
|
|
|
|
|
// Send one more payment in order to cause insufficient capacity error.
|
|
|
|
numInvoices++
|
|
|
|
|
2018-04-18 05:02:04 +03:00
|
|
|
// With the channel open, we'll create invoices for Bob that Alice
|
2017-10-23 04:35:38 +03:00
|
|
|
// will pay to in order to advance the state of the channel.
|
2018-11-20 17:09:44 +03:00
|
|
|
bobPayReqs, _, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
net.Bob, paymentAmt, numInvoices,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2017-05-17 19:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Alice to receive the channel edge from the funding manager.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2017-05-17 19:06:52 +03:00
|
|
|
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.
|
2019-03-28 05:00:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, lntest.AsyncBenchmarkTimeout)
|
2017-12-05 04:28:16 +03:00
|
|
|
alicePayStream, err := net.Alice.SendPayment(ctxt)
|
2017-05-17 19:06:52 +03:00
|
|
|
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{
|
2017-10-23 04:35:38 +03:00
|
|
|
PaymentRequest: bobPayReqs[i],
|
2017-05-17 19:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-26 02:09:48 +03:00
|
|
|
// We should receive one insufficient capacity error, because we sent
|
|
|
|
// one more payment than we can actually handle with the current
|
|
|
|
// channel capacity.
|
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 != "" {
|
2017-09-26 02:09:48 +03:00
|
|
|
if errorReceived {
|
|
|
|
t.Fatalf("redundant payment error: %v",
|
|
|
|
resp.PaymentError)
|
2017-05-17 19:06:52 +03:00
|
|
|
}
|
|
|
|
|
2017-09-26 02:09:48 +03:00
|
|
|
errorReceived = true
|
|
|
|
continue
|
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.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
aliceChan, err := getChanInfo(ctxt, net.Alice)
|
2017-05-17 19:06:52 +03:00
|
|
|
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)
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
bobChan, err := getChanInfo(ctxt, net.Bob)
|
2017-05-17 19:06:52 +03:00
|
|
|
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.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-05-17 19:06:52 +03:00
|
|
|
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.
|
2017-11-03 21:52:02 +03:00
|
|
|
func testBidirectionalAsyncPayments(net *lntest.NetworkHarness, t *harnessTest) {
|
2017-05-18 17:36:55 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
const (
|
2017-10-07 02:58:13 +03:00
|
|
|
paymentAmt = 1000
|
2017-05-18 17:36:55 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: paymentAmt * 2000,
|
|
|
|
PushAmt: paymentAmt * 1000,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2017-05-18 17:36:55 +03:00
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
info, err := getChanInfo(ctxt, net.Alice)
|
2017-05-18 17:36:55 +03:00
|
|
|
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
|
|
|
|
|
2018-04-18 05:02:04 +03:00
|
|
|
// With the channel open, we'll create invoices for Bob that Alice
|
2017-10-23 04:35:38 +03:00
|
|
|
// will pay to in order to advance the state of the channel.
|
2018-11-20 17:09:44 +03:00
|
|
|
bobPayReqs, _, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
net.Bob, paymentAmt, numInvoices,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2017-05-18 17:36:55 +03:00
|
|
|
}
|
|
|
|
|
2018-04-18 05:02:04 +03:00
|
|
|
// With the channel open, we'll create invoices for Alice that Bob
|
2017-10-23 04:35:38 +03:00
|
|
|
// will pay to in order to advance the state of the channel.
|
2018-11-20 17:09:44 +03:00
|
|
|
alicePayReqs, _, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
net.Alice, paymentAmt, numInvoices,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2017-05-18 17:36:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Alice to receive the channel edge from the funding manager.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
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.
|
2019-04-09 02:42:02 +03:00
|
|
|
ctx, cancel := context.WithTimeout(ctxb, lntest.AsyncBenchmarkTimeout)
|
2018-11-28 11:15:58 +03:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
alicePayStream, err := net.Alice.SendPayment(ctx)
|
2017-05-18 17:36:55 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for alice: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-04-09 02:42:02 +03:00
|
|
|
ctx, cancel = context.WithTimeout(ctxb, lntest.AsyncBenchmarkTimeout)
|
2018-11-28 11:15:58 +03:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
bobPayStream, err := net.Bob.SendPayment(ctx)
|
2017-05-18 17:36:55 +03:00
|
|
|
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{
|
2017-10-23 04:35:38 +03:00
|
|
|
PaymentRequest: bobPayReqs[i],
|
2017-05-18 17:36:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bobSendReq := &lnrpc.SendRequest{
|
2017-10-23 04:35:38 +03:00
|
|
|
PaymentRequest: alicePayReqs[i],
|
2017-05-18 17:36:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2017-10-26 01:32:00 +03:00
|
|
|
return
|
2017-05-18 17:36:55 +03:00
|
|
|
} else if resp.PaymentError != "" {
|
|
|
|
errChan <- errors.Errorf("unable to send "+
|
|
|
|
"payment from alice to bob: %v",
|
|
|
|
resp.PaymentError)
|
2017-10-26 01:32:00 +03:00
|
|
|
return
|
2017-05-18 17:36:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
2017-10-26 01:32:00 +03:00
|
|
|
return
|
2017-05-18 17:36:55 +03:00
|
|
|
} else if resp.PaymentError != "" {
|
|
|
|
errChan <- errors.Errorf("unable to send "+
|
|
|
|
"payment from bob to alice: %v",
|
|
|
|
resp.PaymentError)
|
2017-10-26 01:32:00 +03:00
|
|
|
return
|
2017-05-18 17:36:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
errChan <- nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for Alice and Bob receive their payments, and throw and error
|
|
|
|
// if something goes wrong.
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
2019-04-12 02:26:11 +03:00
|
|
|
case <-time.After(lntest.AsyncBenchmarkTimeout):
|
2017-06-19 16:53:52 +03:00
|
|
|
t.Fatalf("waiting for payments to finish too long "+
|
2019-04-12 02:26:11 +03:00
|
|
|
"(%v)", lntest.AsyncBenchmarkTimeout)
|
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)
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
aliceInfo, err := getChanInfo(ctxt, net.Alice)
|
2017-05-18 17:36:55 +03:00
|
|
|
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.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
bobInfo, err := getChanInfo(ctxt, net.Bob)
|
2017-05-18 17:36:55 +03:00
|
|
|
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.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2017-05-18 17:36:55 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:14:42 +03:00
|
|
|
// assertActiveHtlcs makes sure all the passed nodes have the _exact_ HTLCs
|
|
|
|
// matching payHashes on _all_ their channels.
|
2018-01-23 05:59:44 +03:00
|
|
|
func assertActiveHtlcs(nodes []*lntest.HarnessNode, payHashes ...[]byte) error {
|
|
|
|
ctxb := context.Background()
|
2018-11-28 11:25:46 +03:00
|
|
|
|
|
|
|
req := &lnrpc.ListChannelsRequest{}
|
2018-01-23 05:59:44 +03:00
|
|
|
for _, node := range nodes {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
nodeChans, err := node.ListChannels(ctxt, req)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get node chans: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, channel := range nodeChans.Channels {
|
2018-04-13 18:14:42 +03:00
|
|
|
// Record all payment hashes active for this channel.
|
|
|
|
htlcHashes := make(map[string]struct{})
|
2018-01-23 05:59:44 +03:00
|
|
|
for _, htlc := range channel.PendingHtlcs {
|
2018-04-13 18:14:42 +03:00
|
|
|
_, ok := htlcHashes[string(htlc.HashLock)]
|
|
|
|
if ok {
|
|
|
|
return fmt.Errorf("duplicate HashLock")
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
2018-04-13 18:14:42 +03:00
|
|
|
htlcHashes[string(htlc.HashLock)] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Channel should have exactly the payHashes active.
|
|
|
|
if len(payHashes) != len(htlcHashes) {
|
|
|
|
return fmt.Errorf("node %x had %v htlcs active, "+
|
|
|
|
"expected %v", node.PubKey[:],
|
|
|
|
len(htlcHashes), len(payHashes))
|
|
|
|
}
|
2018-01-23 05:59:44 +03:00
|
|
|
|
2018-04-13 18:14:42 +03:00
|
|
|
// Make sure all the payHashes are active.
|
|
|
|
for _, payHash := range payHashes {
|
|
|
|
if _, ok := htlcHashes[string(payHash)]; ok {
|
2018-01-23 05:59:44 +03:00
|
|
|
continue
|
|
|
|
}
|
2018-04-13 18:14:42 +03:00
|
|
|
return fmt.Errorf("node %x didn't have the "+
|
|
|
|
"payHash %v active", node.PubKey[:],
|
|
|
|
payHash)
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-10-26 15:28:05 +03:00
|
|
|
}
|
|
|
|
|
2018-06-09 06:42:54 +03:00
|
|
|
func assertNumActiveHtlcsChanPoint(node *lntest.HarnessNode,
|
2018-08-14 10:09:02 +03:00
|
|
|
chanPoint wire.OutPoint, numHtlcs int) error {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxb := context.Background()
|
2018-06-09 06:42:54 +03:00
|
|
|
|
|
|
|
req := &lnrpc.ListChannelsRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
nodeChans, err := node.ListChannels(ctxt, req)
|
2018-06-09 06:42:54 +03:00
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
return err
|
2018-06-09 06:42:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, channel := range nodeChans.Channels {
|
|
|
|
if channel.ChannelPoint != chanPoint.String() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-14 10:09:02 +03:00
|
|
|
if len(channel.PendingHtlcs) != numHtlcs {
|
|
|
|
return fmt.Errorf("expected %v active HTLCs, got %v",
|
|
|
|
numHtlcs, len(channel.PendingHtlcs))
|
|
|
|
}
|
|
|
|
return nil
|
2018-06-09 06:42:54 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 10:09:02 +03:00
|
|
|
return fmt.Errorf("channel point %v not found", chanPoint)
|
2018-06-09 06:42:54 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 10:09:02 +03:00
|
|
|
func assertNumActiveHtlcs(nodes []*lntest.HarnessNode, numHtlcs int) error {
|
2018-01-23 05:59:44 +03:00
|
|
|
ctxb := context.Background()
|
2018-11-28 11:25:46 +03:00
|
|
|
|
|
|
|
req := &lnrpc.ListChannelsRequest{}
|
2018-01-23 05:59:44 +03:00
|
|
|
for _, node := range nodes {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
nodeChans, err := node.ListChannels(ctxt, req)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
return err
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, channel := range nodeChans.Channels {
|
|
|
|
if len(channel.PendingHtlcs) != numHtlcs {
|
2018-08-14 10:09:02 +03:00
|
|
|
return fmt.Errorf("expected %v HTLCs, got %v",
|
|
|
|
numHtlcs, len(channel.PendingHtlcs))
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 10:09:02 +03:00
|
|
|
return nil
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func assertSpendingTxInMempool(t *harnessTest, miner *rpcclient.Client,
|
|
|
|
timeout time.Duration, chanPoint wire.OutPoint) {
|
|
|
|
|
|
|
|
breakTimeout := time.After(timeout)
|
|
|
|
ticker := time.NewTicker(50 * time.Millisecond)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-breakTimeout:
|
|
|
|
t.Fatalf("didn't find tx in mempool")
|
|
|
|
case <-ticker.C:
|
|
|
|
mempool, err := miner.GetRawMempool()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get mempool: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mempool) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, txid := range mempool {
|
|
|
|
tx, err := miner.GetRawTransaction(txid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch tx: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
|
|
|
if txIn.PreviousOutPoint == chanPoint {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 12:22:58 +03:00
|
|
|
func createThreeHopNetwork(t *harnessTest, net *lntest.NetworkHarness,
|
|
|
|
carolHodl bool) (*lnrpc.ChannelPoint, *lnrpc.ChannelPoint,
|
|
|
|
*lntest.HarnessNode) {
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxb := context.Background()
|
2018-01-23 05:59:44 +03:00
|
|
|
|
|
|
|
// We'll start the test by creating a channel between Alice and Bob,
|
|
|
|
// which will act as the first leg for out multi-hop HTLC.
|
|
|
|
const chanAmt = 1000000
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
aliceChanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-01-23 05:59:44 +03:00
|
|
|
)
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
err := net.Alice.WaitForNetworkChannelOpen(ctxt, aliceChanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't report channel: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
err = net.Bob.WaitForNetworkChannelOpen(ctxt, aliceChanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't report channel: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-08 12:22:58 +03:00
|
|
|
// Next, we'll create a new node "carol" and have Bob connect to her. If
|
|
|
|
// the carolHodl flag is set, we'll make carol always hold onto the
|
|
|
|
// HTLC, this way it'll force Bob to go to chain to resolve the HTLC.
|
|
|
|
carolFlags := []string{"--debughtlc"}
|
|
|
|
if carolHodl {
|
|
|
|
carolFlags = append(carolFlags, "--hodl.exit-settle")
|
|
|
|
}
|
|
|
|
carol, err := net.NewNode("Carol", carolFlags)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Bob, carol); err != nil {
|
2018-01-23 05:59:44 +03:00
|
|
|
t.Fatalf("unable to connect bob to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll then create a channel from Bob to Carol. After this channel is
|
|
|
|
// open, our topology looks like: A -> B -> C.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
bobChanPoint := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Bob, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-01-23 05:59:44 +03:00
|
|
|
)
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
err = net.Bob.WaitForNetworkChannelOpen(ctxt, bobChanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't report channel: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, bobChanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't report channel: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, bobChanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't report channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return aliceChanPoint, bobChanPoint, carol
|
|
|
|
}
|
|
|
|
|
|
|
|
// testMultiHopHtlcLocalTimeout tests that in a multi-hop HTLC scenario, if the
|
|
|
|
// outgoing HTLC is about to time out, then we'll go to chain in order to claim
|
|
|
|
// it. Any dust HTLC's should be immediately cancelled backwards. Once the
|
|
|
|
// timeout has been reached, then we should sweep it on-chain, and cancel the
|
|
|
|
// HTLC backwards.
|
|
|
|
func testMultiHopHtlcLocalTimeout(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// First, we'll create a three hop network: Alice -> Bob -> Carol, with
|
|
|
|
// Carol refusing to actually settle or directly cancel any HTLC's
|
|
|
|
// self.
|
2019-05-08 12:22:58 +03:00
|
|
|
aliceChanPoint, bobChanPoint, carol :=
|
|
|
|
createThreeHopNetwork(t, net, true)
|
2018-01-23 05:59:44 +03:00
|
|
|
|
2018-06-08 00:14:39 +03:00
|
|
|
// Clean up carol's node when the test finishes.
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-01-23 05:59:44 +03:00
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
|
|
|
|
// Now that our channels are set up, we'll send two HTLC's from Alice
|
|
|
|
// to Carol. The first HTLC will be universally considered "dust",
|
|
|
|
// while the second will be a proper fully valued HTLC.
|
|
|
|
const (
|
|
|
|
dustHtlcAmt = btcutil.Amount(100)
|
|
|
|
htlcAmt = btcutil.Amount(30000)
|
|
|
|
finalCltvDelta = 40
|
|
|
|
)
|
2018-11-28 11:15:58 +03:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
alicePayStream, err := net.Alice.SendPayment(ctx)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll create two random payment hashes unknown to carol, then send
|
|
|
|
// each of them by manually specifying the HTLC details.
|
|
|
|
carolPubKey := carol.PubKey[:]
|
2018-08-12 16:23:08 +03:00
|
|
|
dustPayHash := makeFakePayHash(t)
|
|
|
|
payHash := makeFakePayHash(t)
|
2018-01-23 05:59:44 +03:00
|
|
|
err = alicePayStream.Send(&lnrpc.SendRequest{
|
|
|
|
Dest: carolPubKey,
|
|
|
|
Amt: int64(dustHtlcAmt),
|
|
|
|
PaymentHash: dustPayHash,
|
|
|
|
FinalCltvDelta: finalCltvDelta,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send alice htlc: %v", err)
|
|
|
|
}
|
|
|
|
err = alicePayStream.Send(&lnrpc.SendRequest{
|
|
|
|
Dest: carolPubKey,
|
|
|
|
Amt: int64(htlcAmt),
|
|
|
|
PaymentHash: payHash,
|
|
|
|
FinalCltvDelta: finalCltvDelta,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send alice htlc: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that all nodes in the path now have two HTLC's with the
|
|
|
|
// proper parameters.
|
|
|
|
var predErr error
|
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, net.Bob, carol}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
predErr = assertActiveHtlcs(nodes, dustPayHash, payHash)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-01-23 06:58:13 +03:00
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-01-24 01:16:13 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now mine enough blocks to trigger Bob's broadcast of his
|
|
|
|
// commitment transaction due to the fact that the HTLC is about to
|
2019-04-04 15:48:55 +03:00
|
|
|
// timeout. With the default outgoing broadcast delta of zero, this will
|
|
|
|
// be the same height as the htlc expiry height.
|
2019-05-15 10:02:53 +03:00
|
|
|
numBlocks := uint32(finalCltvDelta - lnd.DefaultOutgoingBroadcastDelta)
|
2018-01-23 05:59:44 +03:00
|
|
|
if _, err := net.Miner.Node.Generate(numBlocks); err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob's force close transaction should now be found in the mempool.
|
2019-05-15 10:02:53 +03:00
|
|
|
bobFundingTxid, err := lnd.GetChanPointFundingTxid(bobChanPoint)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2018-01-23 05:59:44 +03:00
|
|
|
assertSpendingTxInMempool(
|
2018-11-27 11:12:16 +03:00
|
|
|
t, net.Miner.Node, minerMempoolTimeout, wire.OutPoint{
|
2018-01-23 05:59:44 +03:00
|
|
|
Hash: *bobFundingTxid,
|
|
|
|
Index: bobChanPoint.OutputIndex,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2018-04-13 14:20:12 +03:00
|
|
|
// Mine a block to confirm the closing transaction.
|
2018-11-29 14:33:11 +03:00
|
|
|
mineBlocks(t, net, 1, 1)
|
2018-04-13 14:20:12 +03:00
|
|
|
|
2018-01-23 05:59:44 +03:00
|
|
|
// At this point, Bob should have cancelled backwards the dust HTLC
|
|
|
|
// that we sent earlier. This means Alice should now only have a single
|
|
|
|
// HTLC on her channel.
|
|
|
|
nodes = []*lntest.HarnessNode{net.Alice}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
predErr = assertActiveHtlcs(nodes, payHash)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-01-23 06:58:13 +03:00
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-04-13 14:18:35 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
2019-04-04 15:48:55 +03:00
|
|
|
// We'll mine defaultCSV blocks in order to generate the sweep
|
|
|
|
// transaction of Bob's funding output. This will also bring us to the
|
|
|
|
// maturity height of the htlc tx output.
|
2018-10-29 07:52:09 +03:00
|
|
|
if _, err := net.Miner.Node.Generate(defaultCSV); err != nil {
|
2018-05-25 05:40:56 +03:00
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:12:16 +03:00
|
|
|
_, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2018-05-25 05:40:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find bob's funding output sweep tx: %v", err)
|
|
|
|
}
|
2018-01-23 05:59:44 +03:00
|
|
|
|
|
|
|
// The second layer HTLC timeout transaction should now have been
|
|
|
|
// broadcast on-chain.
|
2018-11-27 11:12:16 +03:00
|
|
|
secondLayerHash, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find bob's second layer transaction")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob's pending channel report should show that he has a commitment
|
|
|
|
// output awaiting sweeping, and also that there's an outgoing HTLC
|
|
|
|
// output pending.
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
pendingChanResp, err := net.Bob.PendingChannels(ctxt, pendingChansRequest)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for pending channels: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pendingChanResp.PendingForceClosingChannels) == 0 {
|
|
|
|
t.Fatalf("bob should have pending for close chan but doesn't")
|
|
|
|
}
|
|
|
|
forceCloseChan := pendingChanResp.PendingForceClosingChannels[0]
|
|
|
|
if forceCloseChan.LimboBalance == 0 {
|
|
|
|
t.Fatalf("bob should have nonzero limbo balance instead "+
|
|
|
|
"has: %v", forceCloseChan.LimboBalance)
|
|
|
|
}
|
|
|
|
if len(forceCloseChan.PendingHtlcs) == 0 {
|
|
|
|
t.Fatalf("bob should have pending htlc but doesn't")
|
|
|
|
}
|
|
|
|
|
2018-11-29 14:33:11 +03:00
|
|
|
// Now we'll mine an additional block, which should include the second
|
|
|
|
// layer sweep tx.
|
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
2018-01-23 05:59:44 +03:00
|
|
|
|
|
|
|
// The block should have confirmed Bob's second layer sweeping
|
|
|
|
// transaction. Therefore, at this point, there should be no active
|
|
|
|
// HTLC's on the commitment transaction from Alice -> Bob.
|
2018-04-13 14:20:12 +03:00
|
|
|
assertTxInBlock(t, block, secondLayerHash)
|
2018-01-23 05:59:44 +03:00
|
|
|
nodes = []*lntest.HarnessNode{net.Alice}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-01-23 06:58:13 +03:00
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("alice's channel still has active htlc's: %v", predErr)
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, Bob should show that the pending HTLC has advanced to
|
|
|
|
// the second stage and is to be swept.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
pendingChanResp, err = net.Bob.PendingChannels(ctxt, pendingChansRequest)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for pending channels: %v", err)
|
|
|
|
}
|
|
|
|
forceCloseChan = pendingChanResp.PendingForceClosingChannels[0]
|
|
|
|
if forceCloseChan.PendingHtlcs[0].Stage != 2 {
|
|
|
|
t.Fatalf("bob's htlc should have advanced to the second stage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now mine four more blocks. After the 4th block, a transaction
|
|
|
|
// sweeping the HTLC output should be broadcast.
|
|
|
|
if _, err := net.Miner.Node.Generate(4); err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
2018-11-27 11:12:16 +03:00
|
|
|
_, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-05-25 00:57:20 +03:00
|
|
|
t.Fatalf("unable to find bob's sweeping transaction: %v", err)
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll mine a final block that should confirm the second-layer
|
|
|
|
// sweeping transaction.
|
|
|
|
if _, err := net.Miner.Node.Generate(1); err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once this transaction has been confirmed, Bob should detect that he
|
|
|
|
// no longer has any pending channels.
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
pendingChanResp, err = net.Bob.PendingChannels(ctxt, pendingChansRequest)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(pendingChanResp.PendingForceClosingChannels) != 0 {
|
|
|
|
predErr = fmt.Errorf("bob still has pending "+
|
|
|
|
"channels but shouldn't: %v",
|
|
|
|
spew.Sdump(pendingChanResp))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
2018-01-23 06:58:13 +03:00
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(predErr.Error())
|
|
|
|
}
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, aliceChanPoint, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testMultiHopLocalForceCloseOnChainHtlcTimeout tests that in a multi-hop HTLC
|
|
|
|
// scenario, if the node that extended the HTLC to the final node closes their
|
|
|
|
// commitment on-chain early, then it eventually recognizes this HTLC as one
|
|
|
|
// that's timed out. At this point, the node should timeout the HTLC, then
|
|
|
|
// cancel it backwards as normal.
|
|
|
|
func testMultiHopLocalForceCloseOnChainHtlcTimeout(net *lntest.NetworkHarness,
|
|
|
|
t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// First, we'll create a three hop network: Alice -> Bob -> Carol, with
|
|
|
|
// Carol refusing to actually settle or directly cancel any HTLC's
|
|
|
|
// self.
|
2019-05-08 12:22:58 +03:00
|
|
|
aliceChanPoint, bobChanPoint, carol :=
|
|
|
|
createThreeHopNetwork(t, net, true)
|
2018-01-23 05:59:44 +03:00
|
|
|
|
2018-06-08 00:14:39 +03:00
|
|
|
// Clean up carol's node when the test finishes.
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-01-23 05:59:44 +03:00
|
|
|
// With our channels set up, we'll then send a single HTLC from Alice
|
|
|
|
// to Carol. As Carol is in hodl mode, she won't settle this HTLC which
|
|
|
|
// opens up the base for out tests.
|
|
|
|
const (
|
|
|
|
finalCltvDelta = 40
|
|
|
|
htlcAmt = btcutil.Amount(30000)
|
|
|
|
)
|
2018-11-28 11:15:58 +03:00
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
alicePayStream, err := net.Alice.SendPayment(ctx)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now send a single HTLC across our multi-hop network.
|
|
|
|
carolPubKey := carol.PubKey[:]
|
2018-08-12 16:23:08 +03:00
|
|
|
payHash := makeFakePayHash(t)
|
2018-01-23 05:59:44 +03:00
|
|
|
err = alicePayStream.Send(&lnrpc.SendRequest{
|
|
|
|
Dest: carolPubKey,
|
|
|
|
Amt: int64(htlcAmt),
|
|
|
|
PaymentHash: payHash,
|
|
|
|
FinalCltvDelta: finalCltvDelta,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send alice htlc: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once the HTLC has cleared, all channels in our mini network should
|
|
|
|
// have the it locked in.
|
|
|
|
var predErr error
|
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, net.Bob, carol}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
predErr = assertActiveHtlcs(nodes, payHash)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-01-23 06:58:13 +03:00
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("htlc mismatch: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that all parties have the HTLC locked in, we'll immediately
|
|
|
|
// force close the Bob -> Carol channel. This should trigger contract
|
|
|
|
// resolution mode for both of them.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Bob, bobChanPoint, true)
|
|
|
|
|
|
|
|
// At this point, Bob should have a pending force close channel as he
|
|
|
|
// just went to chain.
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
2018-04-13 14:20:12 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
pendingChanResp, err := net.Bob.PendingChannels(ctxt,
|
2018-04-13 14:20:12 +03:00
|
|
|
pendingChansRequest)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(pendingChanResp.PendingForceClosingChannels) == 0 {
|
|
|
|
predErr = fmt.Errorf("bob should have pending for " +
|
|
|
|
"close chan but doesn't")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
forceCloseChan := pendingChanResp.PendingForceClosingChannels[0]
|
|
|
|
if forceCloseChan.LimboBalance == 0 {
|
|
|
|
predErr = fmt.Errorf("bob should have nonzero limbo "+
|
|
|
|
"balance instead has: %v",
|
|
|
|
forceCloseChan.LimboBalance)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-04-13 14:20:12 +03:00
|
|
|
t.Fatalf(predErr.Error())
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
2018-10-29 07:52:09 +03:00
|
|
|
// We'll mine defaultCSV blocks in order to generate the sweep transaction
|
2018-05-25 05:40:56 +03:00
|
|
|
// of Bob's funding output.
|
2018-10-29 07:52:09 +03:00
|
|
|
if _, err := net.Miner.Node.Generate(defaultCSV); err != nil {
|
2018-05-25 05:40:56 +03:00
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:12:16 +03:00
|
|
|
_, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2018-05-25 05:40:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find bob's funding output sweep tx: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:59:44 +03:00
|
|
|
// We'll now mine enough blocks for the HTLC to expire. After this, Bob
|
|
|
|
// should hand off the now expired HTLC output to the utxo nursery.
|
2018-10-29 07:52:09 +03:00
|
|
|
if _, err := net.Miner.Node.Generate(finalCltvDelta - defaultCSV - 1); err != nil {
|
2018-01-23 05:59:44 +03:00
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob's pending channel report should show that he has a single HTLC
|
|
|
|
// that's now in stage one.
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
pendingChanResp, err := net.Bob.PendingChannels(
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, pendingChansRequest,
|
2018-01-23 05:59:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pendingChanResp.PendingForceClosingChannels) == 0 {
|
2018-04-13 14:20:12 +03:00
|
|
|
predErr = fmt.Errorf("bob should have pending force " +
|
2018-01-23 05:59:44 +03:00
|
|
|
"close chan but doesn't")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-04-13 14:20:12 +03:00
|
|
|
forceCloseChan := pendingChanResp.PendingForceClosingChannels[0]
|
2018-01-23 05:59:44 +03:00
|
|
|
if len(forceCloseChan.PendingHtlcs) != 1 {
|
|
|
|
predErr = fmt.Errorf("bob should have pending htlc " +
|
|
|
|
"but doesn't")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if forceCloseChan.PendingHtlcs[0].Stage != 1 {
|
|
|
|
predErr = fmt.Errorf("bob's htlc should have "+
|
|
|
|
"advanced to the first stage: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-01-23 06:58:13 +03:00
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't hand off time-locked HTLC: %v", predErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should also now find a transaction in the mempool, as Bob should
|
|
|
|
// have broadcast his second layer timeout transaction.
|
2018-11-27 11:12:16 +03:00
|
|
|
timeoutTx, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-05-25 05:40:56 +03:00
|
|
|
t.Fatalf("unable to find bob's htlc timeout tx: %v", err)
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll mine an additional block. This should serve to confirm
|
|
|
|
// the second layer timeout transaction.
|
2018-11-29 14:33:11 +03:00
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
2018-05-25 05:40:56 +03:00
|
|
|
assertTxInBlock(t, block, timeoutTx)
|
2018-01-23 05:59:44 +03:00
|
|
|
|
|
|
|
// With the second layer timeout transaction confirmed, Bob should have
|
|
|
|
// cancelled backwards the HTLC that carol sent.
|
|
|
|
nodes = []*lntest.HarnessNode{net.Alice}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-01-23 06:58:13 +03:00
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("alice's channel still has active htlc's: %v", predErr)
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Additionally, Bob should now show that HTLC as being advanced to the
|
|
|
|
// second stage.
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
pendingChanResp, err := net.Bob.PendingChannels(
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, pendingChansRequest,
|
2018-01-23 05:59:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pendingChanResp.PendingForceClosingChannels) == 0 {
|
|
|
|
predErr = fmt.Errorf("bob should have pending for " +
|
|
|
|
"close chan but doesn't")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-04-13 14:20:12 +03:00
|
|
|
forceCloseChan := pendingChanResp.PendingForceClosingChannels[0]
|
2018-01-23 05:59:44 +03:00
|
|
|
if len(forceCloseChan.PendingHtlcs) != 1 {
|
|
|
|
predErr = fmt.Errorf("bob should have pending htlc " +
|
|
|
|
"but doesn't")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if forceCloseChan.PendingHtlcs[0].Stage != 2 {
|
|
|
|
predErr = fmt.Errorf("bob's htlc should have "+
|
|
|
|
"advanced to the second stage: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-01-23 06:58:13 +03:00
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't hand off time-locked HTLC: %v", predErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now mine 4 additional blocks. This should be enough for Bob's
|
2018-05-25 05:40:56 +03:00
|
|
|
// CSV timelock to expire and the sweeping transaction of the HTLC to be
|
|
|
|
// broadcast.
|
2018-10-29 07:52:09 +03:00
|
|
|
if _, err := net.Miner.Node.Generate(defaultCSV); err != nil {
|
2018-01-23 05:59:44 +03:00
|
|
|
t.Fatalf("unable to mine blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:12:16 +03:00
|
|
|
sweepTx, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2018-05-25 05:40:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find bob's htlc sweep tx: %v", err)
|
|
|
|
}
|
2018-01-23 05:59:44 +03:00
|
|
|
|
|
|
|
// We'll then mine a final block which should confirm this second layer
|
|
|
|
// sweep transaction.
|
2018-11-29 14:33:11 +03:00
|
|
|
block = mineBlocks(t, net, 1, 1)[0]
|
2018-05-25 05:40:56 +03:00
|
|
|
assertTxInBlock(t, block, sweepTx)
|
2018-01-23 05:59:44 +03:00
|
|
|
|
|
|
|
// At this point, Bob should no longer show any channels as pending
|
|
|
|
// close.
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-13 14:20:12 +03:00
|
|
|
pendingChanResp, err := net.Bob.PendingChannels(
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, pendingChansRequest,
|
2018-01-23 05:59:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(pendingChanResp.PendingForceClosingChannels) != 0 {
|
|
|
|
predErr = fmt.Errorf("bob still has pending channels "+
|
|
|
|
"but shouldn't: %v", spew.Sdump(pendingChanResp))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-01-23 06:58:13 +03:00
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(predErr.Error())
|
|
|
|
}
|
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, aliceChanPoint, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testMultiHopRemoteForceCloseOnChainHtlcTimeout tests that if we extend a
|
|
|
|
// multi-hop HTLC, and the final destination of the HTLC force closes the
|
|
|
|
// channel, then we properly timeout the HTLC on *their* commitment transaction
|
|
|
|
// once the timeout has expired. Once we sweep the transaction, we should also
|
|
|
|
// cancel back the initial HTLC.
|
2018-05-25 05:40:56 +03:00
|
|
|
func testMultiHopRemoteForceCloseOnChainHtlcTimeout(net *lntest.NetworkHarness,
|
2018-01-23 05:59:44 +03:00
|
|
|
t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// First, we'll create a three hop network: Alice -> Bob -> Carol, with
|
|
|
|
// Carol refusing to actually settle or directly cancel any HTLC's
|
|
|
|
// self.
|
2019-05-08 12:22:58 +03:00
|
|
|
aliceChanPoint, bobChanPoint, carol :=
|
|
|
|
createThreeHopNetwork(t, net, true)
|
2018-01-23 05:59:44 +03:00
|
|
|
|
2018-06-08 00:14:39 +03:00
|
|
|
// Clean up carol's node when the test finishes.
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-01-23 05:59:44 +03:00
|
|
|
// With our channels set up, we'll then send a single HTLC from Alice
|
|
|
|
// to Carol. As Carol is in hodl mode, she won't settle this HTLC which
|
|
|
|
// opens up the base for out tests.
|
|
|
|
const (
|
|
|
|
finalCltvDelta = 40
|
|
|
|
htlcAmt = btcutil.Amount(30000)
|
|
|
|
)
|
2018-11-28 11:15:58 +03:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
alicePayStream, err := net.Alice.SendPayment(ctx)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment stream for alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now send a single HTLC across our multi-hop network.
|
|
|
|
carolPubKey := carol.PubKey[:]
|
2018-08-12 16:23:08 +03:00
|
|
|
payHash := makeFakePayHash(t)
|
2018-01-23 05:59:44 +03:00
|
|
|
err = alicePayStream.Send(&lnrpc.SendRequest{
|
|
|
|
Dest: carolPubKey,
|
|
|
|
Amt: int64(htlcAmt),
|
|
|
|
PaymentHash: payHash,
|
|
|
|
FinalCltvDelta: finalCltvDelta,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send alice htlc: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-04-27 10:54:36 +03:00
|
|
|
// Once the HTLC has cleared, all the nodes in our mini network should
|
2018-01-23 05:59:44 +03:00
|
|
|
// show that the HTLC has been locked in.
|
|
|
|
var predErr error
|
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, net.Bob, carol}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
|
|
|
predErr = assertActiveHtlcs(nodes, payHash)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-01-23 06:58:13 +03:00
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-04-13 14:18:35 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we'll now instruct Carol to force close the
|
|
|
|
// transaction. This will let us exercise that Bob is able to sweep the
|
|
|
|
// expired HTLC on Carol's version of the commitment transaction.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, bobChanPoint, true)
|
|
|
|
|
|
|
|
// At this point, Bob should have a pending force close channel as
|
|
|
|
// Carol has gone directly to chain.
|
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
2018-04-13 14:20:12 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-13 14:20:12 +03:00
|
|
|
pendingChanResp, err := net.Bob.PendingChannels(
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, pendingChansRequest,
|
2018-04-13 14:20:12 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for "+
|
|
|
|
"pending channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(pendingChanResp.PendingForceClosingChannels) == 0 {
|
|
|
|
predErr = fmt.Errorf("bob should have pending " +
|
|
|
|
"force close channels but doesn't")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-04-13 14:20:12 +03:00
|
|
|
t.Fatalf(predErr.Error())
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
2018-07-18 16:55:59 +03:00
|
|
|
// Bob can sweep his output immediately.
|
2018-11-27 11:12:16 +03:00
|
|
|
_, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2018-05-25 05:40:56 +03:00
|
|
|
if err != nil {
|
2018-07-18 16:55:59 +03:00
|
|
|
t.Fatalf("unable to find bob's funding output sweep tx: %v",
|
|
|
|
err)
|
2018-05-25 05:40:56 +03:00
|
|
|
}
|
|
|
|
|
2018-01-23 05:59:44 +03:00
|
|
|
// Next, we'll mine enough blocks for the HTLC to expire. At this
|
2018-04-27 10:54:36 +03:00
|
|
|
// point, Bob should hand off the output to his internal utxo nursery,
|
|
|
|
// which will broadcast a sweep transaction.
|
2018-07-18 16:55:59 +03:00
|
|
|
if _, err := net.Miner.Node.Generate(finalCltvDelta - 1); err != nil {
|
2018-01-23 05:59:44 +03:00
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we check Bob's pending channel report, it should show that he has
|
|
|
|
// a single HTLC that's now in the second stage, as skip the initial
|
|
|
|
// first stage since this is a direct HTLC.
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
pendingChanResp, err := net.Bob.PendingChannels(
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, pendingChansRequest,
|
2018-01-23 05:59:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pendingChanResp.PendingForceClosingChannels) == 0 {
|
|
|
|
predErr = fmt.Errorf("bob should have pending for " +
|
|
|
|
"close chan but doesn't")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
forceCloseChan := pendingChanResp.PendingForceClosingChannels[0]
|
|
|
|
if len(forceCloseChan.PendingHtlcs) != 1 {
|
|
|
|
predErr = fmt.Errorf("bob should have pending htlc " +
|
|
|
|
"but doesn't")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if forceCloseChan.PendingHtlcs[0].Stage != 2 {
|
|
|
|
predErr = fmt.Errorf("bob's htlc should have "+
|
|
|
|
"advanced to the second stage: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-01-23 06:58:13 +03:00
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't hand off time-locked HTLC: %v", predErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob's sweeping transaction should now be found in the mempool at
|
|
|
|
// this point.
|
2018-11-27 11:12:16 +03:00
|
|
|
sweepTx, err := waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-05-04 02:38:54 +03:00
|
|
|
// If Bob's transaction isn't yet in the mempool, then due to
|
|
|
|
// internal message passing and the low period between blocks
|
|
|
|
// being mined, it may have been detected as a late
|
|
|
|
// registration. As a result, we'll mine another block and
|
|
|
|
// repeat the check. If it doesn't go through this time, then
|
|
|
|
// we'll fail.
|
2018-11-06 13:03:01 +03:00
|
|
|
// TODO(halseth): can we use waitForChannelPendingForceClose to
|
|
|
|
// avoid this hack?
|
2018-05-04 02:38:54 +03:00
|
|
|
if _, err := net.Miner.Node.Generate(1); err != nil {
|
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
|
|
|
}
|
2018-11-27 11:12:16 +03:00
|
|
|
sweepTx, err = waitForTxInMempool(net.Miner.Node, minerMempoolTimeout)
|
2018-05-04 02:38:54 +03:00
|
|
|
if err != nil {
|
2018-05-25 05:40:56 +03:00
|
|
|
t.Fatalf("unable to find bob's sweeping transaction: "+
|
|
|
|
"%v", err)
|
2018-05-04 02:38:54 +03:00
|
|
|
}
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we mine an additional block, then this should confirm Bob's
|
|
|
|
// transaction which sweeps the direct HTLC output.
|
2018-11-29 14:33:11 +03:00
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
2018-05-25 05:40:56 +03:00
|
|
|
assertTxInBlock(t, block, sweepTx)
|
2018-01-23 05:59:44 +03:00
|
|
|
|
|
|
|
// Now that the sweeping transaction has been confirmed, Bob should
|
|
|
|
// cancel back that HTLC. As a result, Alice should not know of any
|
|
|
|
// active HTLC's.
|
|
|
|
nodes = []*lntest.HarnessNode{net.Alice}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-05-25 05:55:13 +03:00
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("alice's channel still has active htlc's: %v", predErr)
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now we'll check Bob's pending channel report. Since this was Carol's
|
|
|
|
// commitment, he doesn't have to wait for any CSV delays. As a result,
|
|
|
|
// he should show no additional pending transactions.
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-13 14:20:12 +03:00
|
|
|
pendingChanResp, err := net.Bob.PendingChannels(
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, pendingChansRequest,
|
2018-01-23 05:59:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query for pending "+
|
|
|
|
"channels: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(pendingChanResp.PendingForceClosingChannels) != 0 {
|
|
|
|
predErr = fmt.Errorf("bob still has pending channels "+
|
|
|
|
"but shouldn't: %v", spew.Sdump(pendingChanResp))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-01-23 06:58:13 +03:00
|
|
|
}, time.Second*15)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(predErr.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll close out the test by closing the channel from Alice to Bob,
|
|
|
|
// and then shutting down the new node we created as its no longer
|
|
|
|
// needed.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-01-23 05:59:44 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, aliceChanPoint, false)
|
|
|
|
}
|
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
// testSwitchCircuitPersistence creates a multihop network to ensure the sender
|
|
|
|
// and intermediaries are persisting their open payment circuits. After
|
|
|
|
// forwarding a packet via an outgoing link, all are restarted, and expected to
|
|
|
|
// forward a response back from the receiver once back online.
|
|
|
|
//
|
|
|
|
// The general flow of this test:
|
|
|
|
// 1. Carol --> Dave --> Alice --> Bob forward payment
|
|
|
|
// 2. X X X Bob restart sender and intermediaries
|
|
|
|
// 3. Carol <-- Dave <-- Alice <-- Bob expect settle to propagate
|
|
|
|
func testSwitchCircuitPersistence(net *lntest.NetworkHarness, t *harnessTest) {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
const chanAmt = btcutil.Amount(1000000)
|
|
|
|
const pushAmt = btcutil.Amount(900000)
|
|
|
|
var networkChans []*lnrpc.ChannelPoint
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
networkChans = append(networkChans, chanPointAlice)
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
aliceChanTXID, err := lnd.GetChanPointFundingTxid(chanPointAlice)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
aliceFundPoint := wire.OutPoint{
|
|
|
|
Hash: *aliceChanTXID,
|
|
|
|
Index: chanPointAlice.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// As preliminary setup, we'll create two new nodes: Carol and Dave,
|
|
|
|
// such that we now have a 4 ndoe, 3 channel topology. Dave will make
|
|
|
|
// a channel with Alice, and Carol with Dave. After this setup, the
|
|
|
|
// network topology should now look like:
|
|
|
|
// Carol -> Dave -> Alice -> Bob
|
|
|
|
//
|
|
|
|
// First, we'll create Dave and establish a channel to Alice.
|
2018-04-27 11:42:04 +03:00
|
|
|
dave, err := net.NewNode("Dave", nil)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, dave, net.Alice); err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("unable to connect dave to alice: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, dave)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to dave: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointDave := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, dave, net.Alice,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
networkChans = append(networkChans, chanPointDave)
|
2019-05-15 10:02:53 +03:00
|
|
|
daveChanTXID, err := lnd.GetChanPointFundingTxid(chanPointDave)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
daveFundPoint := wire.OutPoint{
|
|
|
|
Hash: *daveChanTXID,
|
|
|
|
Index: chanPointDave.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll create Carol and establish a channel to from her to
|
|
|
|
// Dave. Carol is started in htlchodl mode so that we can disconnect the
|
|
|
|
// intermediary hops before starting the settle.
|
2018-04-27 00:08:24 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--debughtlc", "--hodl.exit-settle"})
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, dave); err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("unable to connect carol to dave: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointCarol := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, carol, dave,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
networkChans = append(networkChans, chanPointCarol)
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
carolChanTXID, err := lnd.GetChanPointFundingTxid(chanPointCarol)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
carolFundPoint := wire.OutPoint{
|
|
|
|
Hash: *carolChanTXID,
|
|
|
|
Index: chanPointCarol.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all nodes to have seen all channels.
|
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, net.Bob, carol, dave}
|
|
|
|
nodeNames := []string{"Alice", "Bob", "Carol", "Dave"}
|
|
|
|
for _, chanPoint := range networkChans {
|
|
|
|
for i, node := range nodes {
|
2019-05-15 10:02:53 +03:00
|
|
|
txid, err := lnd.GetChanPointFundingTxid(chanPoint)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
point := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = node.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s(%d): timeout waiting for "+
|
|
|
|
"channel(%s) open: %v", nodeNames[i],
|
|
|
|
node.NodeID, point, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create 5 invoices for Carol, which expect a payment from Bob for 1k
|
|
|
|
// satoshis with a different preimage each time.
|
|
|
|
const numPayments = 5
|
|
|
|
const paymentAmt = 1000
|
2018-11-20 17:09:44 +03:00
|
|
|
payReqs, _, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
carol, paymentAmt, numPayments,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2018-02-23 01:29:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll wait for all parties to recognize the new channels within the
|
|
|
|
// network.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = dave.WaitForNetworkChannelOpen(ctxt, chanPointDave)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("dave didn't advertise his channel: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPointCarol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("carol didn't advertise her channel in time: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
|
|
|
// Using Carol as the source, pay to the 5 invoices from Bob created
|
|
|
|
// above.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = completePaymentRequests(ctxt, net.Bob, payReqs, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// Wait until all nodes in the network have 5 outstanding htlcs.
|
2018-08-14 10:09:02 +03:00
|
|
|
var predErr error
|
2018-03-13 10:13:57 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, numPayments)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-03-13 10:13:57 +03:00
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-03-13 10:13:57 +03:00
|
|
|
}
|
2018-02-23 01:29:06 +03:00
|
|
|
|
|
|
|
// Restart the intermediaries and the sender.
|
|
|
|
if err := net.RestartNode(dave, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := net.RestartNode(net.Bob, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// Ensure all of the intermediate links are reconnected.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
err = net.EnsureConnected(ctxt, net.Alice, dave)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to reconnect alice and dave: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
err = net.EnsureConnected(ctxt, net.Bob, net.Alice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to reconnect bob and alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure all nodes in the network still have 5 outstanding htlcs.
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, numPayments)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-03-13 10:13:57 +03:00
|
|
|
}
|
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
// Now restart carol without hodl mode, to settle back the outstanding
|
|
|
|
// payments.
|
|
|
|
carol.SetExtraArgs(nil)
|
|
|
|
if err := net.RestartNode(carol, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
err = net.EnsureConnected(ctxt, dave, carol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to reconnect dave and carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// After the payments settle, there should be no active htlcs on any of
|
|
|
|
// the nodes in the network.
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-03-13 10:13:57 +03:00
|
|
|
}
|
2018-02-23 01:29:06 +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
|
|
|
|
|
|
|
|
// At this point all the channels within our proto network should be
|
|
|
|
// shifted by 5k satoshis in the direction of Carol, the sink within the
|
|
|
|
// payment flow generated above. The order of asserts corresponds to
|
|
|
|
// increasing of time is needed to embed the HTLC in commitment
|
|
|
|
// transaction, in channel Bob->Alice->David->Carol, order is Carol,
|
|
|
|
// David, Alice, Bob.
|
|
|
|
var amountPaid = int64(5000)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Carol(remote)", carol,
|
2018-02-23 01:29:06 +03:00
|
|
|
carolFundPoint, int64(0), amountPaid)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Carol(remote)", dave,
|
2018-02-23 01:29:06 +03:00
|
|
|
carolFundPoint, amountPaid, int64(0))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Dave(remote)", dave,
|
2018-02-23 01:29:06 +03:00
|
|
|
daveFundPoint, int64(0), amountPaid+(baseFee*numPayments))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Dave(remote)", net.Alice,
|
2018-02-23 01:29:06 +03:00
|
|
|
daveFundPoint, amountPaid+(baseFee*numPayments), int64(0))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Alice(remote)", net.Alice,
|
2018-02-23 01:29:06 +03:00
|
|
|
aliceFundPoint, int64(0), amountPaid+((baseFee*numPayments)*2))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Alice(remote)", net.Bob,
|
2018-02-23 01:29:06 +03:00
|
|
|
aliceFundPoint, amountPaid+(baseFee*numPayments)*2, int64(0))
|
|
|
|
|
|
|
|
// Lastly, we will send one more payment to ensure all channels are
|
|
|
|
// still functioning properly.
|
|
|
|
finalInvoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := carol.AddInvoice(ctxt, finalInvoice)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
payReqs = []string{resp.PaymentRequest}
|
|
|
|
|
|
|
|
// Using Carol as the source, pay to the 5 invoices from Bob created
|
|
|
|
// above.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = completePaymentRequests(ctxt, net.Bob, payReqs, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
amountPaid = int64(6000)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Carol(remote)", carol,
|
2018-02-23 01:29:06 +03:00
|
|
|
carolFundPoint, int64(0), amountPaid)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Carol(remote)", dave,
|
2018-02-23 01:29:06 +03:00
|
|
|
carolFundPoint, amountPaid, int64(0))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Dave(remote)", dave,
|
2018-02-23 01:29:06 +03:00
|
|
|
daveFundPoint, int64(0), amountPaid+(baseFee*(numPayments+1)))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Dave(remote)", net.Alice,
|
2018-02-23 01:29:06 +03:00
|
|
|
daveFundPoint, amountPaid+(baseFee*(numPayments+1)), int64(0))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Alice(remote)", net.Alice,
|
2018-02-23 01:29:06 +03:00
|
|
|
aliceFundPoint, int64(0), amountPaid+((baseFee*(numPayments+1))*2))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Alice(remote)", net.Bob,
|
2018-02-23 01:29:06 +03:00
|
|
|
aliceFundPoint, amountPaid+(baseFee*(numPayments+1))*2, int64(0))
|
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, dave, chanPointDave, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPointCarol, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testSwitchOfflineDelivery constructs a set of multihop payments, and tests
|
|
|
|
// that the returning payments are not lost if a peer on the backwards path is
|
|
|
|
// offline when the settle/fails are received. We expect the payments to be
|
|
|
|
// buffered in memory, and transmitted as soon as the disconnect link comes back
|
|
|
|
// online.
|
|
|
|
//
|
|
|
|
// The general flow of this test:
|
|
|
|
// 1. Carol --> Dave --> Alice --> Bob forward payment
|
|
|
|
// 2. Carol --- Dave X Alice --- Bob disconnect intermediaries
|
|
|
|
// 3. Carol --- Dave X Alice <-- Bob settle last hop
|
|
|
|
// 4. Carol <-- Dave <-- Alice --- Bob reconnect, expect settle to propagate
|
|
|
|
func testSwitchOfflineDelivery(net *lntest.NetworkHarness, t *harnessTest) {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
const chanAmt = btcutil.Amount(1000000)
|
|
|
|
const pushAmt = btcutil.Amount(900000)
|
|
|
|
var networkChans []*lnrpc.ChannelPoint
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
networkChans = append(networkChans, chanPointAlice)
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
aliceChanTXID, err := lnd.GetChanPointFundingTxid(chanPointAlice)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
aliceFundPoint := wire.OutPoint{
|
|
|
|
Hash: *aliceChanTXID,
|
|
|
|
Index: chanPointAlice.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// As preliminary setup, we'll create two new nodes: Carol and Dave,
|
|
|
|
// such that we now have a 4 ndoe, 3 channel topology. Dave will make
|
|
|
|
// a channel with Alice, and Carol with Dave. After this setup, the
|
|
|
|
// network topology should now look like:
|
|
|
|
// Carol -> Dave -> Alice -> Bob
|
|
|
|
//
|
|
|
|
// First, we'll create Dave and establish a channel to Alice.
|
2018-04-27 11:42:04 +03:00
|
|
|
dave, err := net.NewNode("Dave", []string{"--unsafe-disconnect"})
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, dave, net.Alice); err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("unable to connect dave to alice: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, dave)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to dave: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointDave := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, dave, net.Alice,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
networkChans = append(networkChans, chanPointDave)
|
2019-05-15 10:02:53 +03:00
|
|
|
daveChanTXID, err := lnd.GetChanPointFundingTxid(chanPointDave)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
daveFundPoint := wire.OutPoint{
|
|
|
|
Hash: *daveChanTXID,
|
|
|
|
Index: chanPointDave.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll create Carol and establish a channel to from her to
|
|
|
|
// Dave. Carol is started in htlchodl mode so that we can disconnect the
|
|
|
|
// intermediary hops before starting the settle.
|
2018-04-27 00:08:24 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--debughtlc", "--hodl.exit-settle"})
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, dave); err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("unable to connect carol to dave: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointCarol := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, carol, dave,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
networkChans = append(networkChans, chanPointCarol)
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
carolChanTXID, err := lnd.GetChanPointFundingTxid(chanPointCarol)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
carolFundPoint := wire.OutPoint{
|
|
|
|
Hash: *carolChanTXID,
|
|
|
|
Index: chanPointCarol.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all nodes to have seen all channels.
|
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, net.Bob, carol, dave}
|
|
|
|
nodeNames := []string{"Alice", "Bob", "Carol", "Dave"}
|
|
|
|
for _, chanPoint := range networkChans {
|
|
|
|
for i, node := range nodes {
|
2019-05-15 10:02:53 +03:00
|
|
|
txid, err := lnd.GetChanPointFundingTxid(chanPoint)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
point := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = node.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s(%d): timeout waiting for "+
|
|
|
|
"channel(%s) open: %v", nodeNames[i],
|
|
|
|
node.NodeID, point, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create 5 invoices for Carol, which expect a payment from Bob for 1k
|
|
|
|
// satoshis with a different preimage each time.
|
|
|
|
const numPayments = 5
|
|
|
|
const paymentAmt = 1000
|
2018-11-20 17:09:44 +03:00
|
|
|
payReqs, _, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
carol, paymentAmt, numPayments,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2018-02-23 01:29:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll wait for all parties to recognize the new channels within the
|
|
|
|
// network.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = dave.WaitForNetworkChannelOpen(ctxt, chanPointDave)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("dave didn't advertise his channel: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPointCarol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("carol didn't advertise her channel in time: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
|
|
|
// Using Carol as the source, pay to the 5 invoices from Bob created
|
|
|
|
// above.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = completePaymentRequests(ctxt, net.Bob, payReqs, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// Wait for all of the payments to reach Carol.
|
2018-08-14 10:09:02 +03:00
|
|
|
var predErr error
|
2018-03-13 10:13:57 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, numPayments)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-03-13 10:13:57 +03:00
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-03-13 10:13:57 +03:00
|
|
|
}
|
2018-02-23 01:29:06 +03:00
|
|
|
|
|
|
|
// First, disconnect Dave and Alice so that their link is broken.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err := net.DisconnectNodes(ctxt, dave, net.Alice); err != nil {
|
|
|
|
t.Fatalf("unable to disconnect alice from dave: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then, reconnect them to ensure Dave doesn't just fail back the htlc.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err := net.ConnectNodes(ctxt, dave, net.Alice); err != nil {
|
|
|
|
t.Fatalf("unable to reconnect alice to dave: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// Wait to ensure that the payment remain are not failed back after
|
|
|
|
// reconnecting. All node should report the number payments initiated
|
|
|
|
// for the duration of the interval.
|
|
|
|
err = lntest.WaitInvariant(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, numPayments)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-03-13 10:13:57 +03:00
|
|
|
}, time.Second*2)
|
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("htlc change: %v", predErr)
|
2018-03-13 10:13:57 +03:00
|
|
|
}
|
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
// Now, disconnect Dave from Alice again before settling back the
|
|
|
|
// payment.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err := net.DisconnectNodes(ctxt, dave, net.Alice); err != nil {
|
|
|
|
t.Fatalf("unable to disconnect alice from dave: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now restart carol without hodl mode, to settle back the outstanding
|
|
|
|
// payments.
|
|
|
|
carol.SetExtraArgs(nil)
|
|
|
|
if err := net.RestartNode(carol, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// Wait for Carol to report no outstanding htlcs.
|
|
|
|
carolNode := []*lntest.HarnessNode{carol}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(carolNode, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-03-13 10:13:57 +03:00
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-03-13 10:13:57 +03:00
|
|
|
}
|
2018-02-23 01:29:06 +03:00
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// Now that the settles have reached Dave, reconnect him with Alice,
|
|
|
|
// allowing the settles to return to the sender.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-03-31 05:59:58 +03:00
|
|
|
if err := net.EnsureConnected(ctxt, dave, net.Alice); err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("unable to reconnect alice to dave: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// Wait until all outstanding htlcs in the network have been settled.
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-03-13 10:13:57 +03:00
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-03-13 10:13:57 +03:00
|
|
|
}
|
2018-02-23 01:29:06 +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
|
|
|
|
|
|
|
|
// At this point all the channels within our proto network should be
|
|
|
|
// shifted by 5k satoshis in the direction of Carol, the sink within the
|
|
|
|
// payment flow generated above. The order of asserts corresponds to
|
|
|
|
// increasing of time is needed to embed the HTLC in commitment
|
|
|
|
// transaction, in channel Bob->Alice->David->Carol, order is Carol,
|
|
|
|
// David, Alice, Bob.
|
|
|
|
var amountPaid = int64(5000)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Carol(remote)", carol,
|
2018-02-23 01:29:06 +03:00
|
|
|
carolFundPoint, int64(0), amountPaid)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Carol(remote)", dave,
|
2018-02-23 01:29:06 +03:00
|
|
|
carolFundPoint, amountPaid, int64(0))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Dave(remote)", dave,
|
2018-02-23 01:29:06 +03:00
|
|
|
daveFundPoint, int64(0), amountPaid+(baseFee*numPayments))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Dave(remote)", net.Alice,
|
2018-02-23 01:29:06 +03:00
|
|
|
daveFundPoint, amountPaid+(baseFee*numPayments), int64(0))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Alice(remote)", net.Alice,
|
2018-02-23 01:29:06 +03:00
|
|
|
aliceFundPoint, int64(0), amountPaid+((baseFee*numPayments)*2))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Alice(remote)", net.Bob,
|
2018-02-23 01:29:06 +03:00
|
|
|
aliceFundPoint, amountPaid+(baseFee*numPayments)*2, int64(0))
|
|
|
|
|
|
|
|
// Lastly, we will send one more payment to ensure all channels are
|
|
|
|
// still functioning properly.
|
|
|
|
finalInvoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := carol.AddInvoice(ctxt, finalInvoice)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
payReqs = []string{resp.PaymentRequest}
|
|
|
|
|
|
|
|
// Using Carol as the source, pay to the 5 invoices from Bob created
|
|
|
|
// above.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = completePaymentRequests(ctxt, net.Bob, payReqs, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
amountPaid = int64(6000)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Carol(remote)", carol,
|
2018-02-23 01:29:06 +03:00
|
|
|
carolFundPoint, int64(0), amountPaid)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Carol(remote)", dave,
|
2018-02-23 01:29:06 +03:00
|
|
|
carolFundPoint, amountPaid, int64(0))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Dave(remote)", dave,
|
2018-02-23 01:29:06 +03:00
|
|
|
daveFundPoint, int64(0), amountPaid+(baseFee*(numPayments+1)))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Dave(remote)", net.Alice,
|
2018-02-23 01:29:06 +03:00
|
|
|
daveFundPoint, amountPaid+(baseFee*(numPayments+1)), int64(0))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Alice(remote)", net.Alice,
|
2018-02-23 01:29:06 +03:00
|
|
|
aliceFundPoint, int64(0), amountPaid+((baseFee*(numPayments+1))*2))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Alice(remote)", net.Bob,
|
2018-02-23 01:29:06 +03:00
|
|
|
aliceFundPoint, amountPaid+(baseFee*(numPayments+1))*2, int64(0))
|
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, dave, chanPointDave, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPointCarol, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testSwitchOfflineDeliveryPersistence constructs a set of multihop payments,
|
|
|
|
// and tests that the returning payments are not lost if a peer on the backwards
|
|
|
|
// path is offline when the settle/fails are received AND the peer buffering the
|
|
|
|
// responses is completely restarts. We expect the payments to be reloaded from
|
|
|
|
// disk, and transmitted as soon as the intermediaries are reconnected.
|
|
|
|
//
|
|
|
|
// The general flow of this test:
|
|
|
|
// 1. Carol --> Dave --> Alice --> Bob forward payment
|
|
|
|
// 2. Carol --- Dave X Alice --- Bob disconnect intermediaries
|
|
|
|
// 3. Carol --- Dave X Alice <-- Bob settle last hop
|
|
|
|
// 4. Carol --- Dave X X Bob restart Alice
|
|
|
|
// 5. Carol <-- Dave <-- Alice --- Bob expect settle to propagate
|
|
|
|
func testSwitchOfflineDeliveryPersistence(net *lntest.NetworkHarness, t *harnessTest) {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
const chanAmt = btcutil.Amount(1000000)
|
|
|
|
const pushAmt = btcutil.Amount(900000)
|
|
|
|
var networkChans []*lnrpc.ChannelPoint
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
networkChans = append(networkChans, chanPointAlice)
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
aliceChanTXID, err := lnd.GetChanPointFundingTxid(chanPointAlice)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
aliceFundPoint := wire.OutPoint{
|
|
|
|
Hash: *aliceChanTXID,
|
|
|
|
Index: chanPointAlice.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// As preliminary setup, we'll create two new nodes: Carol and Dave,
|
|
|
|
// such that we now have a 4 ndoe, 3 channel topology. Dave will make
|
|
|
|
// a channel with Alice, and Carol with Dave. After this setup, the
|
|
|
|
// network topology should now look like:
|
|
|
|
// Carol -> Dave -> Alice -> Bob
|
|
|
|
//
|
|
|
|
// First, we'll create Dave and establish a channel to Alice.
|
2018-04-27 11:42:04 +03:00
|
|
|
dave, err := net.NewNode("Dave", []string{"--unsafe-disconnect"})
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, dave, net.Alice); err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("unable to connect dave to alice: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, dave)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to dave: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointDave := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, dave, net.Alice,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-08-22 10:32:42 +03:00
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
networkChans = append(networkChans, chanPointDave)
|
2019-05-15 10:02:53 +03:00
|
|
|
daveChanTXID, err := lnd.GetChanPointFundingTxid(chanPointDave)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
daveFundPoint := wire.OutPoint{
|
|
|
|
Hash: *daveChanTXID,
|
|
|
|
Index: chanPointDave.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll create Carol and establish a channel to from her to
|
|
|
|
// Dave. Carol is started in htlchodl mode so that we can disconnect the
|
|
|
|
// intermediary hops before starting the settle.
|
2018-04-27 00:08:24 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--debughtlc", "--hodl.exit-settle"})
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, dave); err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("unable to connect carol to dave: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointCarol := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, carol, dave,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
networkChans = append(networkChans, chanPointCarol)
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
carolChanTXID, err := lnd.GetChanPointFundingTxid(chanPointCarol)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
carolFundPoint := wire.OutPoint{
|
|
|
|
Hash: *carolChanTXID,
|
|
|
|
Index: chanPointCarol.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all nodes to have seen all channels.
|
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, net.Bob, carol, dave}
|
|
|
|
nodeNames := []string{"Alice", "Bob", "Carol", "Dave"}
|
|
|
|
for _, chanPoint := range networkChans {
|
|
|
|
for i, node := range nodes {
|
2019-05-15 10:02:53 +03:00
|
|
|
txid, err := lnd.GetChanPointFundingTxid(chanPoint)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
point := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = node.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s(%d): timeout waiting for "+
|
|
|
|
"channel(%s) open: %v", nodeNames[i],
|
|
|
|
node.NodeID, point, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create 5 invoices for Carol, which expect a payment from Bob for 1k
|
|
|
|
// satoshis with a different preimage each time.
|
|
|
|
const numPayments = 5
|
|
|
|
const paymentAmt = 1000
|
2018-11-20 17:09:44 +03:00
|
|
|
payReqs, _, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
carol, paymentAmt, numPayments,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2018-02-23 01:29:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll wait for all parties to recognize the new channels within the
|
|
|
|
// network.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = dave.WaitForNetworkChannelOpen(ctxt, chanPointDave)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("dave didn't advertise his channel: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPointCarol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("carol didn't advertise her channel in time: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using Carol as the source, pay to the 5 invoices from Bob created
|
|
|
|
// above.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = completePaymentRequests(ctxt, net.Bob, payReqs, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-14 10:09:02 +03:00
|
|
|
var predErr error
|
2018-03-13 10:13:57 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, numPayments)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-03-13 10:13:57 +03:00
|
|
|
}
|
2018-02-23 01:29:06 +03:00
|
|
|
|
2018-06-09 06:42:54 +03:00
|
|
|
// Disconnect the two intermediaries, Alice and Dave, by shutting down
|
|
|
|
// Alice.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-06-09 06:42:54 +03:00
|
|
|
if err := net.StopNode(net.Alice); err != nil {
|
|
|
|
t.Fatalf("unable to shutdown alice: %v", err)
|
2018-02-23 01:29:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now restart carol without hodl mode, to settle back the outstanding
|
|
|
|
// payments.
|
|
|
|
carol.SetExtraArgs(nil)
|
|
|
|
if err := net.RestartNode(carol, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// Make Carol and Dave are reconnected before waiting for the htlcs to
|
|
|
|
// clear.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
err = net.EnsureConnected(ctxt, dave, carol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to reconnect dave and carol: %v", err)
|
|
|
|
}
|
2018-02-23 01:29:06 +03:00
|
|
|
|
2018-06-09 06:42:54 +03:00
|
|
|
// Wait for Carol to report no outstanding htlcs, and also for Dav to
|
|
|
|
// receive all the settles from Carol.
|
2018-03-13 10:13:57 +03:00
|
|
|
carolNode := []*lntest.HarnessNode{carol}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(carolNode, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
predErr = assertNumActiveHtlcsChanPoint(dave, carolFundPoint, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-03-13 10:13:57 +03:00
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-03-13 10:13:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, restart dave who received the settles, but was unable to
|
|
|
|
// deliver them to Alice since they were disconnected.
|
2018-02-23 01:29:06 +03:00
|
|
|
if err := net.RestartNode(dave, nil); err != nil {
|
2018-06-09 06:42:54 +03:00
|
|
|
t.Fatalf("unable to restart dave: %v", err)
|
|
|
|
}
|
|
|
|
if err = net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("unable to restart alice: %v", err)
|
2018-02-23 01:29:06 +03:00
|
|
|
}
|
|
|
|
|
2018-03-31 05:59:58 +03:00
|
|
|
// Force Dave and Alice to reconnect before waiting for the htlcs to
|
|
|
|
// clear.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-03-31 05:59:58 +03:00
|
|
|
err = net.EnsureConnected(ctxt, dave, net.Alice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to reconnect dave and carol: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-06-09 06:42:54 +03:00
|
|
|
// After reconnection succeeds, the settles should be propagated all
|
|
|
|
// the way back to the sender. All nodes should report no active htlcs.
|
2018-03-13 10:13:57 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-03-13 10:13:57 +03:00
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-03-13 10:13:57 +03:00
|
|
|
}
|
2018-02-23 01:29:06 +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
|
|
|
|
|
|
|
|
// At this point all the channels within our proto network should be
|
|
|
|
// shifted by 5k satoshis in the direction of Carol, the sink within the
|
|
|
|
// payment flow generated above. The order of asserts corresponds to
|
|
|
|
// increasing of time is needed to embed the HTLC in commitment
|
|
|
|
// transaction, in channel Bob->Alice->David->Carol, order is Carol,
|
|
|
|
// David, Alice, Bob.
|
|
|
|
var amountPaid = int64(5000)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Carol(remote)", carol,
|
2018-02-23 01:29:06 +03:00
|
|
|
carolFundPoint, int64(0), amountPaid)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Carol(remote)", dave,
|
2018-02-23 01:29:06 +03:00
|
|
|
carolFundPoint, amountPaid, int64(0))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Dave(remote)", dave,
|
2018-02-23 01:29:06 +03:00
|
|
|
daveFundPoint, int64(0), amountPaid+(baseFee*numPayments))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Dave(remote)", net.Alice,
|
2018-02-23 01:29:06 +03:00
|
|
|
daveFundPoint, amountPaid+(baseFee*numPayments), int64(0))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Alice(remote)", net.Alice,
|
2018-02-23 01:29:06 +03:00
|
|
|
aliceFundPoint, int64(0), amountPaid+((baseFee*numPayments)*2))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Alice(remote)", net.Bob,
|
2018-02-23 01:29:06 +03:00
|
|
|
aliceFundPoint, amountPaid+(baseFee*numPayments)*2, int64(0))
|
|
|
|
|
|
|
|
// Lastly, we will send one more payment to ensure all channels are
|
|
|
|
// still functioning properly.
|
|
|
|
finalInvoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := carol.AddInvoice(ctxt, finalInvoice)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
payReqs = []string{resp.PaymentRequest}
|
|
|
|
|
2018-03-31 05:59:58 +03:00
|
|
|
// Before completing the final payment request, ensure that the
|
|
|
|
// connection between Dave and Carol has been healed.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-03-31 05:59:58 +03:00
|
|
|
err = net.EnsureConnected(ctxt, dave, carol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to reconnect dave and carol: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
// Using Carol as the source, pay to the 5 invoices from Bob created
|
|
|
|
// above.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
err = completePaymentRequests(ctxt, net.Bob, payReqs, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
amountPaid = int64(6000)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Carol(remote)", carol,
|
2018-02-23 01:29:06 +03:00
|
|
|
carolFundPoint, int64(0), amountPaid)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Carol(remote)", dave,
|
2018-02-23 01:29:06 +03:00
|
|
|
carolFundPoint, amountPaid, int64(0))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Dave(remote)", dave,
|
2018-02-23 01:29:06 +03:00
|
|
|
daveFundPoint, int64(0), amountPaid+(baseFee*(numPayments+1)))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Dave(remote)", net.Alice,
|
2018-02-23 01:29:06 +03:00
|
|
|
daveFundPoint, amountPaid+(baseFee*(numPayments+1)), int64(0))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Alice(remote)", net.Alice,
|
2018-02-23 01:29:06 +03:00
|
|
|
aliceFundPoint, int64(0), amountPaid+((baseFee*(numPayments+1))*2))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Alice(remote)", net.Bob,
|
2018-02-23 01:29:06 +03:00
|
|
|
aliceFundPoint, amountPaid+(baseFee*(numPayments+1))*2, int64(0))
|
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, dave, chanPointDave, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-23 01:29:06 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPointCarol, false)
|
|
|
|
}
|
|
|
|
|
2018-02-24 03:45:50 +03:00
|
|
|
// testSwitchOfflineDeliveryOutgoingOffline constructs a set of multihop payments,
|
|
|
|
// and tests that the returning payments are not lost if a peer on the backwards
|
|
|
|
// path is offline when the settle/fails are received AND the peer buffering the
|
|
|
|
// responses is completely restarts. We expect the payments to be reloaded from
|
|
|
|
// disk, and transmitted as soon as the intermediaries are reconnected.
|
|
|
|
//
|
|
|
|
// The general flow of this test:
|
|
|
|
// 1. Carol --> Dave --> Alice --> Bob forward payment
|
|
|
|
// 2. Carol --- Dave X Alice --- Bob disconnect intermediaries
|
|
|
|
// 3. Carol --- Dave X Alice <-- Bob settle last hop
|
2018-03-04 11:20:26 +03:00
|
|
|
// 4. Carol --- Dave X X shutdown Bob, restart Alice
|
|
|
|
// 5. Carol <-- Dave <-- Alice X expect settle to propagate
|
2018-02-24 03:45:50 +03:00
|
|
|
func testSwitchOfflineDeliveryOutgoingOffline(
|
|
|
|
net *lntest.NetworkHarness, t *harnessTest) {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxb := context.Background()
|
2018-02-24 03:45:50 +03:00
|
|
|
|
2018-03-04 11:20:26 +03:00
|
|
|
const chanAmt = btcutil.Amount(1000000)
|
|
|
|
const pushAmt = btcutil.Amount(900000)
|
2018-02-24 03:45:50 +03:00
|
|
|
var networkChans []*lnrpc.ChannelPoint
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-02-24 03:45:50 +03:00
|
|
|
networkChans = append(networkChans, chanPointAlice)
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
aliceChanTXID, err := lnd.GetChanPointFundingTxid(chanPointAlice)
|
2018-02-24 03:45:50 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
aliceFundPoint := wire.OutPoint{
|
|
|
|
Hash: *aliceChanTXID,
|
|
|
|
Index: chanPointAlice.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// As preliminary setup, we'll create two new nodes: Carol and Dave,
|
|
|
|
// such that we now have a 4 ndoe, 3 channel topology. Dave will make
|
|
|
|
// a channel with Alice, and Carol with Dave. After this setup, the
|
|
|
|
// network topology should now look like:
|
|
|
|
// Carol -> Dave -> Alice -> Bob
|
|
|
|
//
|
|
|
|
// First, we'll create Dave and establish a channel to Alice.
|
2018-04-27 11:42:04 +03:00
|
|
|
dave, err := net.NewNode("Dave", []string{"--unsafe-disconnect"})
|
2018-02-24 03:45:50 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, dave, net.Alice); err != nil {
|
2018-02-24 03:45:50 +03:00
|
|
|
t.Fatalf("unable to connect dave to alice: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, dave)
|
2018-02-24 03:45:50 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to dave: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointDave := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, dave, net.Alice,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-02-24 03:45:50 +03:00
|
|
|
networkChans = append(networkChans, chanPointDave)
|
2019-05-15 10:02:53 +03:00
|
|
|
daveChanTXID, err := lnd.GetChanPointFundingTxid(chanPointDave)
|
2018-02-24 03:45:50 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
daveFundPoint := wire.OutPoint{
|
|
|
|
Hash: *daveChanTXID,
|
|
|
|
Index: chanPointDave.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll create Carol and establish a channel to from her to
|
|
|
|
// Dave. Carol is started in htlchodl mode so that we can disconnect the
|
|
|
|
// intermediary hops before starting the settle.
|
2018-04-27 00:08:24 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--debughtlc", "--hodl.exit-settle"})
|
2018-02-24 03:45:50 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, dave); err != nil {
|
2018-02-24 03:45:50 +03:00
|
|
|
t.Fatalf("unable to connect carol to dave: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
2018-02-24 03:45:50 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointCarol := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, carol, dave,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-02-24 03:45:50 +03:00
|
|
|
networkChans = append(networkChans, chanPointCarol)
|
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
carolChanTXID, err := lnd.GetChanPointFundingTxid(chanPointCarol)
|
2018-02-24 03:45:50 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
carolFundPoint := wire.OutPoint{
|
|
|
|
Hash: *carolChanTXID,
|
|
|
|
Index: chanPointCarol.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all nodes to have seen all channels.
|
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, net.Bob, carol, dave}
|
|
|
|
nodeNames := []string{"Alice", "Bob", "Carol", "Dave"}
|
|
|
|
for _, chanPoint := range networkChans {
|
|
|
|
for i, node := range nodes {
|
2019-05-15 10:02:53 +03:00
|
|
|
txid, err := lnd.GetChanPointFundingTxid(chanPoint)
|
2018-02-24 03:45:50 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
point := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-24 03:45:50 +03:00
|
|
|
err = node.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s(%d): timeout waiting for "+
|
|
|
|
"channel(%s) open: %v", nodeNames[i],
|
|
|
|
node.NodeID, point, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create 5 invoices for Carol, which expect a payment from Bob for 1k
|
|
|
|
// satoshis with a different preimage each time.
|
|
|
|
const numPayments = 5
|
|
|
|
const paymentAmt = 1000
|
2018-11-20 17:09:44 +03:00
|
|
|
payReqs, _, _, err := createPayReqs(
|
2019-03-28 05:00:27 +03:00
|
|
|
carol, paymentAmt, numPayments,
|
2018-11-20 17:09:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2018-02-24 03:45:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll wait for all parties to recognize the new channels within the
|
|
|
|
// network.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-24 03:45:50 +03:00
|
|
|
err = dave.WaitForNetworkChannelOpen(ctxt, chanPointDave)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("dave didn't advertise his channel: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-24 03:45:50 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPointCarol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("carol didn't advertise her channel in time: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using Carol as the source, pay to the 5 invoices from Bob created
|
|
|
|
// above.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-02-24 03:45:50 +03:00
|
|
|
err = completePaymentRequests(ctxt, net.Bob, payReqs, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// Wait for all payments to reach Carol.
|
2018-08-14 10:09:02 +03:00
|
|
|
var predErr error
|
2018-03-13 10:13:57 +03:00
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, numPayments)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-03-13 10:13:57 +03:00
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-03-13 10:13:57 +03:00
|
|
|
}
|
2018-02-24 03:45:50 +03:00
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// Disconnect the two intermediaries, Alice and Dave, so that when carol
|
|
|
|
// restarts, the response will be held by Dave.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-06-09 06:42:54 +03:00
|
|
|
if err := net.StopNode(net.Alice); err != nil {
|
|
|
|
t.Fatalf("unable to shutdown alice: %v", err)
|
2018-02-24 03:45:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now restart carol without hodl mode, to settle back the outstanding
|
|
|
|
// payments.
|
|
|
|
carol.SetExtraArgs(nil)
|
|
|
|
if err := net.RestartNode(carol, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// Wait for Carol to report no outstanding htlcs.
|
|
|
|
carolNode := []*lntest.HarnessNode{carol}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(carolNode, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
predErr = assertNumActiveHtlcsChanPoint(dave, carolFundPoint, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-03-13 10:13:57 +03:00
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-03-13 10:13:57 +03:00
|
|
|
}
|
2018-02-24 03:45:50 +03:00
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// Now check that the total amount was transferred from Dave to Carol.
|
|
|
|
// The amount transferred should be exactly equal to the invoice total
|
|
|
|
// payment amount, 5k satsohis.
|
2018-02-24 03:45:50 +03:00
|
|
|
const amountPaid = int64(5000)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Carol(remote)", carol,
|
2018-02-24 03:45:50 +03:00
|
|
|
carolFundPoint, int64(0), amountPaid)
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Dave(local) => Carol(remote)", dave,
|
2018-02-24 03:45:50 +03:00
|
|
|
carolFundPoint, amountPaid, int64(0))
|
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// Shutdown carol and leave her offline for the rest of the test. This
|
|
|
|
// is critical, as we wish to see if Dave can propragate settles even if
|
|
|
|
// the outgoing link is never revived.
|
2018-06-08 00:14:39 +03:00
|
|
|
shutdownAndAssert(net, t, carol)
|
2018-02-24 03:45:50 +03:00
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// Now restart Dave, ensuring he is both persisting the settles, and is
|
|
|
|
// able to reforward them to Alice after recovering from a restart.
|
2018-02-24 03:45:50 +03:00
|
|
|
if err := net.RestartNode(dave, nil); err != nil {
|
2018-03-13 10:13:57 +03:00
|
|
|
t.Fatalf("unable to restart dave: %v", err)
|
2018-02-24 03:45:50 +03:00
|
|
|
}
|
2018-06-09 06:42:54 +03:00
|
|
|
if err = net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("unable to restart alice: %v", err)
|
|
|
|
}
|
2018-02-24 03:45:50 +03:00
|
|
|
|
2019-03-12 02:28:47 +03:00
|
|
|
// Ensure that Dave is reconnected to Alice before waiting for the
|
|
|
|
// htlcs to clear.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
err = net.EnsureConnected(ctxt, dave, net.Alice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to reconnect alice and dave: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since Carol has been shutdown permanently, we will wait until all
|
|
|
|
// other nodes in the network report no active htlcs.
|
|
|
|
nodesMinusCarol := []*lntest.HarnessNode{net.Bob, net.Alice, dave}
|
|
|
|
err = lntest.WaitPredicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodesMinusCarol, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-03-13 10:13:57 +03:00
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
2018-08-14 10:09:02 +03:00
|
|
|
t.Fatalf("htlc mismatch: %v", predErr)
|
2018-03-13 10:13:57 +03:00
|
|
|
}
|
2018-02-24 03:45:50 +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
|
|
|
|
|
2018-03-13 10:13:57 +03:00
|
|
|
// At this point, all channels (minus Carol, who is shutdown) should
|
|
|
|
// show a shift of 5k satoshis towards Carol. The order of asserts
|
|
|
|
// corresponds to increasing of time is needed to embed the HTLC in
|
2019-03-12 02:28:47 +03:00
|
|
|
// commitment transaction, in channel Bob->Alice->David, order is
|
|
|
|
// David, Alice, Bob.
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Dave(remote)", dave,
|
2018-02-24 03:45:50 +03:00
|
|
|
daveFundPoint, int64(0), amountPaid+(baseFee*numPayments))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Alice(local) => Dave(remote)", net.Alice,
|
2018-02-24 03:45:50 +03:00
|
|
|
daveFundPoint, amountPaid+(baseFee*numPayments), int64(0))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Alice(remote)", net.Alice,
|
2018-02-24 03:45:50 +03:00
|
|
|
aliceFundPoint, int64(0), amountPaid+((baseFee*numPayments)*2))
|
2018-11-28 11:00:19 +03:00
|
|
|
assertAmountPaid(t, "Bob(local) => Alice(remote)", net.Bob,
|
2018-02-24 03:45:50 +03:00
|
|
|
aliceFundPoint, amountPaid+(baseFee*numPayments)*2, int64(0))
|
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-24 03:45:50 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-02-24 03:45:50 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, dave, chanPointDave, false)
|
|
|
|
}
|
|
|
|
|
2018-04-11 12:18:58 +03:00
|
|
|
// computeFee calculates the payment fee as specified in BOLT07
|
|
|
|
func computeFee(baseFee, feeRate, amt lnwire.MilliSatoshi) lnwire.MilliSatoshi {
|
|
|
|
return baseFee + amt*feeRate/1000000
|
|
|
|
}
|
|
|
|
|
|
|
|
// testQueryRoutes checks the response of queryroutes.
|
|
|
|
// We'll create the following network topology:
|
|
|
|
// Alice --> Bob --> Carol --> Dave
|
|
|
|
// and query the daemon for routes from Alice to Dave.
|
|
|
|
func testQueryRoutes(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
2018-11-28 11:25:46 +03:00
|
|
|
|
|
|
|
const chanAmt = btcutil.Amount(100000)
|
2018-04-11 12:18:58 +03:00
|
|
|
var networkChans []*lnrpc.ChannelPoint
|
|
|
|
|
|
|
|
// Open a channel between Alice and Bob.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-04-11 12:18:58 +03:00
|
|
|
networkChans = append(networkChans, chanPointAlice)
|
|
|
|
|
|
|
|
// Create Carol and establish a channel from Bob.
|
2018-04-27 11:42:04 +03:00
|
|
|
carol, err := net.NewNode("Carol", nil)
|
2018-04-11 12:18:58 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, net.Bob); err != nil {
|
2018-04-11 12:18:58 +03:00
|
|
|
t.Fatalf("unable to connect carol to bob: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, net.Bob)
|
2018-04-11 12:18:58 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to bob: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointBob := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Bob, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-04-11 12:18:58 +03:00
|
|
|
networkChans = append(networkChans, chanPointBob)
|
|
|
|
|
|
|
|
// Create Dave and establish a channel from Carol.
|
2018-04-27 11:42:04 +03:00
|
|
|
dave, err := net.NewNode("Dave", nil)
|
2018-04-11 12:18:58 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, dave, carol); err != nil {
|
2018-04-11 12:18:58 +03:00
|
|
|
t.Fatalf("unable to connect dave to carol: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
2018-04-11 12:18:58 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPointCarol := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, carol, dave,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-04-11 12:18:58 +03:00
|
|
|
networkChans = append(networkChans, chanPointCarol)
|
|
|
|
|
|
|
|
// Wait for all nodes to have seen all channels.
|
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, net.Bob, carol, dave}
|
|
|
|
nodeNames := []string{"Alice", "Bob", "Carol", "Dave"}
|
|
|
|
for _, chanPoint := range networkChans {
|
|
|
|
for i, node := range nodes {
|
2019-05-15 10:02:53 +03:00
|
|
|
txid, err := lnd.GetChanPointFundingTxid(chanPoint)
|
2018-04-11 12:18:58 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
point := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-11 12:18:58 +03:00
|
|
|
err = node.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s(%d): timeout waiting for "+
|
|
|
|
"channel(%s) open: %v", nodeNames[i],
|
|
|
|
node.NodeID, point, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query for routes to pay from Alice to Dave.
|
|
|
|
const paymentAmt = 1000
|
|
|
|
routesReq := &lnrpc.QueryRoutesRequest{
|
2019-05-07 18:01:01 +03:00
|
|
|
PubKey: dave.PubKeyStr,
|
|
|
|
Amt: paymentAmt,
|
2018-04-11 12:18:58 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-11 12:18:58 +03:00
|
|
|
routesRes, err := net.Alice.QueryRoutes(ctxt, routesReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get route: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const mSat = 1000
|
|
|
|
feePerHopMSat := computeFee(1000, 1, paymentAmt*mSat)
|
|
|
|
|
|
|
|
for i, route := range routesRes.Routes {
|
|
|
|
expectedTotalFeesMSat :=
|
|
|
|
lnwire.MilliSatoshi(len(route.Hops)-1) * feePerHopMSat
|
|
|
|
expectedTotalAmtMSat := (paymentAmt * mSat) + expectedTotalFeesMSat
|
|
|
|
|
|
|
|
if route.TotalFees != route.TotalFeesMsat/mSat {
|
|
|
|
t.Fatalf("route %v: total fees %v (msat) does not "+
|
|
|
|
"round down to %v (sat)",
|
|
|
|
i, route.TotalFeesMsat, route.TotalFees)
|
|
|
|
}
|
|
|
|
if route.TotalFeesMsat != int64(expectedTotalFeesMSat) {
|
|
|
|
t.Fatalf("route %v: total fees in msat expected %v got %v",
|
|
|
|
i, expectedTotalFeesMSat, route.TotalFeesMsat)
|
|
|
|
}
|
|
|
|
|
|
|
|
if route.TotalAmt != route.TotalAmtMsat/mSat {
|
|
|
|
t.Fatalf("route %v: total amt %v (msat) does not "+
|
|
|
|
"round down to %v (sat)",
|
|
|
|
i, route.TotalAmtMsat, route.TotalAmt)
|
|
|
|
}
|
|
|
|
if route.TotalAmtMsat != int64(expectedTotalAmtMSat) {
|
|
|
|
t.Fatalf("route %v: total amt in msat expected %v got %v",
|
|
|
|
i, expectedTotalAmtMSat, route.TotalAmtMsat)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For all hops except the last, we check that fee equals feePerHop
|
2018-09-06 11:48:46 +03:00
|
|
|
// and amount to forward deducts feePerHop on each hop.
|
2018-04-11 12:18:58 +03:00
|
|
|
expectedAmtToForwardMSat := expectedTotalAmtMSat
|
|
|
|
for j, hop := range route.Hops[:len(route.Hops)-1] {
|
|
|
|
expectedAmtToForwardMSat -= feePerHopMSat
|
|
|
|
|
|
|
|
if hop.Fee != hop.FeeMsat/mSat {
|
|
|
|
t.Fatalf("route %v hop %v: fee %v (msat) does not "+
|
|
|
|
"round down to %v (sat)",
|
|
|
|
i, j, hop.FeeMsat, hop.Fee)
|
|
|
|
}
|
|
|
|
if hop.FeeMsat != int64(feePerHopMSat) {
|
|
|
|
t.Fatalf("route %v hop %v: fee in msat expected %v got %v",
|
|
|
|
i, j, feePerHopMSat, hop.FeeMsat)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hop.AmtToForward != hop.AmtToForwardMsat/mSat {
|
|
|
|
t.Fatalf("route %v hop %v: amt to forward %v (msat) does not "+
|
|
|
|
"round down to %v (sat)",
|
|
|
|
i, j, hop.AmtToForwardMsat, hop.AmtToForward)
|
|
|
|
}
|
|
|
|
if hop.AmtToForwardMsat != int64(expectedAmtToForwardMSat) {
|
|
|
|
t.Fatalf("route %v hop %v: amt to forward in msat "+
|
|
|
|
"expected %v got %v",
|
|
|
|
i, j, expectedAmtToForwardMSat, hop.AmtToForwardMsat)
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 11:48:46 +03:00
|
|
|
// Last hop should have zero fee and amount to forward should equal
|
2018-04-11 12:18:58 +03:00
|
|
|
// payment amount.
|
|
|
|
hop := route.Hops[len(route.Hops)-1]
|
|
|
|
|
|
|
|
if hop.Fee != 0 || hop.FeeMsat != 0 {
|
|
|
|
t.Fatalf("route %v hop %v: fee expected 0 got %v (sat) %v (msat)",
|
|
|
|
i, len(route.Hops)-1, hop.Fee, hop.FeeMsat)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hop.AmtToForward != hop.AmtToForwardMsat/mSat {
|
|
|
|
t.Fatalf("route %v hop %v: amt to forward %v (msat) does not "+
|
|
|
|
"round down to %v (sat)",
|
|
|
|
i, len(route.Hops)-1, hop.AmtToForwardMsat, hop.AmtToForward)
|
|
|
|
}
|
|
|
|
if hop.AmtToForwardMsat != paymentAmt*mSat {
|
|
|
|
t.Fatalf("route %v hop %v: amt to forward in msat "+
|
|
|
|
"expected %v got %v",
|
|
|
|
i, len(route.Hops)-1, paymentAmt*mSat, hop.AmtToForwardMsat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We clean up the test case by closing channels that were created for
|
|
|
|
// the duration of the tests.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-04-11 12:18:58 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-04-11 12:18:58 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Bob, chanPointBob, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-04-11 12:18:58 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPointCarol, false)
|
|
|
|
}
|
|
|
|
|
2018-04-30 23:56:02 +03:00
|
|
|
// testRouteFeeCutoff tests that we are able to prevent querying routes and
|
|
|
|
// sending payments that incur a fee higher than the fee limit.
|
|
|
|
func testRouteFeeCutoff(net *lntest.NetworkHarness, t *harnessTest) {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2018-04-30 23:56:02 +03:00
|
|
|
// For this test, we'll create the following topology:
|
|
|
|
//
|
|
|
|
// --- Bob ---
|
|
|
|
// / \
|
|
|
|
// Alice ---- ---- Dave
|
|
|
|
// \ /
|
|
|
|
// -- Carol --
|
|
|
|
//
|
|
|
|
// Alice will attempt to send payments to Dave that should not incur a
|
|
|
|
// fee greater than the fee limit expressed as a percentage of the
|
|
|
|
// amount and as a fixed amount of satoshis.
|
|
|
|
const chanAmt = btcutil.Amount(100000)
|
|
|
|
|
|
|
|
// Open a channel between Alice and Bob.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
chanPointAliceBob := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-04-30 23:56:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Create Carol's node and open a channel between her and Alice with
|
|
|
|
// Alice being the funder.
|
|
|
|
carol, err := net.NewNode("Carol", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create carol's node: %v", err)
|
|
|
|
}
|
2018-08-09 11:03:15 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
if err := net.ConnectNodes(ctxt, carol, net.Alice); err != nil {
|
|
|
|
t.Fatalf("unable to connect carol to alice: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
chanPointAliceCarol := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-04-30 23:56:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Create Dave's node and open a channel between him and Bob with Bob
|
|
|
|
// being the funder.
|
|
|
|
dave, err := net.NewNode("Dave", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create dave's node: %v", err)
|
|
|
|
}
|
2018-08-09 11:03:15 +03:00
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
if err := net.ConnectNodes(ctxt, dave, net.Bob); err != nil {
|
|
|
|
t.Fatalf("unable to connect dave to bob: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
chanPointBobDave := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Bob, dave,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-04-30 23:56:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Open a channel between Carol and Dave.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
if err := net.ConnectNodes(ctxt, carol, dave); err != nil {
|
|
|
|
t.Fatalf("unable to connect carol to dave: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
chanPointCarolDave := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, carol, dave,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-04-30 23:56:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Now that all the channels were set up, we'll wait for all the nodes
|
|
|
|
// to have seen all the channels.
|
|
|
|
nodes := []*lntest.HarnessNode{net.Alice, net.Bob, carol, dave}
|
|
|
|
nodeNames := []string{"alice", "bob", "carol", "dave"}
|
|
|
|
networkChans := []*lnrpc.ChannelPoint{
|
|
|
|
chanPointAliceBob, chanPointAliceCarol, chanPointBobDave,
|
|
|
|
chanPointCarolDave,
|
|
|
|
}
|
|
|
|
for _, chanPoint := range networkChans {
|
|
|
|
for i, node := range nodes {
|
2019-05-15 10:02:53 +03:00
|
|
|
txid, err := lnd.GetChanPointFundingTxid(chanPoint)
|
2018-04-30 23:56:02 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
outpoint := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
err = node.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s(%d) timed out waiting for "+
|
|
|
|
"channel(%s) open: %v", nodeNames[i],
|
|
|
|
node.NodeID, outpoint, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 11:48:46 +03:00
|
|
|
// The payments should only be successful across the route:
|
2018-04-30 23:56:02 +03:00
|
|
|
// Alice -> Bob -> Dave
|
|
|
|
// Therefore, we'll update the fee policy on Carol's side for the
|
|
|
|
// channel between her and Dave to invalidate the route:
|
|
|
|
// Alice -> Carol -> Dave
|
|
|
|
baseFee := int64(10000)
|
|
|
|
feeRate := int64(5)
|
2019-05-15 10:02:53 +03:00
|
|
|
timeLockDelta := uint32(lnd.DefaultBitcoinTimeLockDelta)
|
2018-04-30 23:56:02 +03:00
|
|
|
|
|
|
|
expectedPolicy := &lnrpc.RoutingPolicy{
|
|
|
|
FeeBaseMsat: baseFee,
|
2018-06-16 15:53:44 +03:00
|
|
|
FeeRateMilliMsat: testFeeBase * feeRate,
|
2018-04-30 23:56:02 +03:00
|
|
|
TimeLockDelta: timeLockDelta,
|
2018-08-22 10:32:43 +03:00
|
|
|
MinHtlc: 1000, // default value
|
2018-04-30 23:56:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
updateFeeReq := &lnrpc.PolicyUpdateRequest{
|
|
|
|
BaseFeeMsat: baseFee,
|
|
|
|
FeeRate: float64(feeRate),
|
|
|
|
TimeLockDelta: timeLockDelta,
|
|
|
|
Scope: &lnrpc.PolicyUpdateRequest_ChanPoint{
|
|
|
|
ChanPoint: chanPointCarolDave,
|
|
|
|
},
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
if _, err := carol.UpdateChannelPolicy(ctxt, updateFeeReq); err != nil {
|
|
|
|
t.Fatalf("unable to update chan policy: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Alice to receive the channel update from Carol.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-08-23 08:16:16 +03:00
|
|
|
aliceSub := subscribeGraphNotifications(t, ctxt, net.Alice)
|
|
|
|
defer close(aliceSub.quit)
|
2018-08-22 10:32:43 +03:00
|
|
|
|
2018-04-30 23:56:02 +03:00
|
|
|
waitForChannelUpdate(
|
2018-08-23 08:16:16 +03:00
|
|
|
t, aliceSub,
|
2018-08-22 10:32:42 +03:00
|
|
|
[]expectedChanUpdate{
|
|
|
|
{carol.PubKeyStr, expectedPolicy, chanPointCarolDave},
|
|
|
|
},
|
2018-04-30 23:56:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// We'll also need the channel IDs for Bob's channels in order to
|
|
|
|
// confirm the route of the payments.
|
|
|
|
listReq := &lnrpc.ListChannelsRequest{}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
listResp, err := net.Bob.ListChannels(ctxt, listReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to retrieve bob's channels: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var aliceBobChanID, bobDaveChanID uint64
|
|
|
|
for _, channel := range listResp.Channels {
|
|
|
|
switch channel.RemotePubkey {
|
|
|
|
case net.Alice.PubKeyStr:
|
|
|
|
aliceBobChanID = channel.ChanId
|
|
|
|
case dave.PubKeyStr:
|
|
|
|
bobDaveChanID = channel.ChanId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if aliceBobChanID == 0 {
|
|
|
|
t.Fatalf("channel between alice and bob not found")
|
|
|
|
}
|
|
|
|
if bobDaveChanID == 0 {
|
|
|
|
t.Fatalf("channel between bob and dave not found")
|
|
|
|
}
|
|
|
|
hopChanIDs := []uint64{aliceBobChanID, bobDaveChanID}
|
|
|
|
|
|
|
|
// checkRoute is a helper closure to ensure the route contains the
|
|
|
|
// correct intermediate hops.
|
|
|
|
checkRoute := func(route *lnrpc.Route) {
|
|
|
|
if len(route.Hops) != 2 {
|
|
|
|
t.Fatalf("expected two hops, got %d", len(route.Hops))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, hop := range route.Hops {
|
|
|
|
if hop.ChanId != hopChanIDs[i] {
|
|
|
|
t.Fatalf("expected chan id %d, got %d",
|
2018-07-31 11:29:12 +03:00
|
|
|
hopChanIDs[i], hop.ChanId)
|
2018-04-30 23:56:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll be attempting to send two payments from Alice to Dave. One will
|
|
|
|
// have a fee cutoff expressed as a percentage of the amount and the
|
|
|
|
// other will have it expressed as a fixed amount of satoshis.
|
|
|
|
const paymentAmt = 100
|
|
|
|
carolFee := computeFee(lnwire.MilliSatoshi(baseFee), 1, paymentAmt)
|
|
|
|
|
|
|
|
// testFeeCutoff is a helper closure that will ensure the different
|
|
|
|
// types of fee limits work as intended when querying routes and sending
|
|
|
|
// payments.
|
|
|
|
testFeeCutoff := func(feeLimit *lnrpc.FeeLimit) {
|
|
|
|
queryRoutesReq := &lnrpc.QueryRoutesRequest{
|
2019-05-07 18:01:01 +03:00
|
|
|
PubKey: dave.PubKeyStr,
|
|
|
|
Amt: paymentAmt,
|
|
|
|
FeeLimit: feeLimit,
|
2018-04-30 23:56:02 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
routesResp, err := net.Alice.QueryRoutes(ctxt, queryRoutesReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get routes: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checkRoute(routesResp.Routes[0])
|
|
|
|
|
|
|
|
invoice := &lnrpc.Invoice{Value: paymentAmt}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
invoiceResp, err := dave.AddInvoice(ctxt, invoice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sendReq := &lnrpc.SendRequest{
|
|
|
|
PaymentRequest: invoiceResp.PaymentRequest,
|
|
|
|
FeeLimit: feeLimit,
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
paymentResp, err := net.Alice.SendPaymentSync(ctxt, sendReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
if paymentResp.PaymentError != "" {
|
|
|
|
t.Fatalf("unable to send payment: %v",
|
|
|
|
paymentResp.PaymentError)
|
|
|
|
}
|
|
|
|
|
|
|
|
checkRoute(paymentResp.PaymentRoute)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll start off using percentages first. Since the fee along the
|
|
|
|
// route using Carol as an intermediate hop is 10% of the payment's
|
|
|
|
// amount, we'll use a lower percentage in order to invalid that route.
|
|
|
|
feeLimitPercent := &lnrpc.FeeLimit{
|
2019-02-21 15:47:58 +03:00
|
|
|
Limit: &lnrpc.FeeLimit_Percent{
|
|
|
|
Percent: baseFee/1000 - 1,
|
|
|
|
},
|
2018-04-30 23:56:02 +03:00
|
|
|
}
|
|
|
|
testFeeCutoff(feeLimitPercent)
|
|
|
|
|
|
|
|
// Now we'll test using fixed fee limit amounts. Since we computed the
|
|
|
|
// fee for the route using Carol as an intermediate hop earlier, we can
|
|
|
|
// use a smaller value in order to invalidate that route.
|
|
|
|
feeLimitFixed := &lnrpc.FeeLimit{
|
2019-02-21 15:47:58 +03:00
|
|
|
Limit: &lnrpc.FeeLimit_Fixed{
|
|
|
|
Fixed: int64(carolFee.ToSatoshis()) - 1,
|
|
|
|
},
|
2018-04-30 23:56:02 +03:00
|
|
|
}
|
|
|
|
testFeeCutoff(feeLimitFixed)
|
|
|
|
|
|
|
|
// Once we're done, close the channels and shut down the nodes created
|
|
|
|
// throughout this test.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAliceBob, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAliceCarol, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Bob, chanPointBobDave, false)
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-04-30 23:56:02 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPointCarolDave, false)
|
|
|
|
}
|
|
|
|
|
2018-06-14 05:36:08 +03:00
|
|
|
// testSendUpdateDisableChannel ensures that a channel update with the disable
|
|
|
|
// flag set is sent once a channel has been either unilaterally or cooperatively
|
|
|
|
// closed.
|
|
|
|
func testSendUpdateDisableChannel(net *lntest.NetworkHarness, t *harnessTest) {
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2018-06-14 05:36:08 +03:00
|
|
|
const (
|
|
|
|
chanAmt = 100000
|
|
|
|
)
|
|
|
|
|
|
|
|
// Open a channel between Alice and Bob and Alice and Carol. These will
|
|
|
|
// be closed later on in order to trigger channel update messages
|
|
|
|
// marking the channels as disabled.
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-06-14 05:36:08 +03:00
|
|
|
chanPointAliceBob := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-06-14 05:36:08 +03:00
|
|
|
)
|
|
|
|
|
2019-02-15 04:13:20 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{
|
|
|
|
"--minbackoff=10s",
|
|
|
|
"--unsafe-disconnect",
|
|
|
|
"--chan-enable-timeout=1.5s",
|
|
|
|
"--chan-disable-timeout=3s",
|
|
|
|
"--chan-status-sample-interval=.5s",
|
|
|
|
})
|
2018-06-14 05:36:08 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create carol's node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
2018-08-09 11:03:15 +03:00
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Alice, carol); err != nil {
|
2018-06-14 05:36:08 +03:00
|
|
|
t.Fatalf("unable to connect alice to carol: %v", err)
|
|
|
|
}
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-06-14 05:36:08 +03:00
|
|
|
chanPointAliceCarol := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, net.Alice, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-06-14 05:36:08 +03:00
|
|
|
)
|
|
|
|
|
2018-08-09 11:03:15 +03:00
|
|
|
// We create a new node Eve that has an inactive channel timeout of
|
|
|
|
// just 2 seconds (down from the default 20m). It will be used to test
|
|
|
|
// channel updates for channels going inactive.
|
2019-02-15 04:13:20 +03:00
|
|
|
eve, err := net.NewNode("Eve", []string{
|
|
|
|
"--minbackoff=10s",
|
|
|
|
"--chan-enable-timeout=1.5s",
|
|
|
|
"--chan-disable-timeout=3s",
|
|
|
|
"--chan-status-sample-interval=.5s",
|
|
|
|
})
|
2018-08-09 11:03:15 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create eve's node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, eve)
|
|
|
|
|
|
|
|
// Give Eve some coins.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, eve)
|
2018-08-09 11:03:15 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to eve: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect Eve to Carol and Bob, and open a channel to carol.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, eve, carol); err != nil {
|
2018-08-09 11:03:15 +03:00
|
|
|
t.Fatalf("unable to connect alice to carol: %v", err)
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, eve, net.Bob); err != nil {
|
2018-08-09 11:03:15 +03:00
|
|
|
t.Fatalf("unable to connect eve to bob: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-08-09 11:03:15 +03:00
|
|
|
chanPointEveCarol := openChannelAndAssert(
|
2018-08-22 10:32:42 +03:00
|
|
|
ctxt, t, net, eve, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
2018-08-09 11:03:15 +03:00
|
|
|
)
|
|
|
|
|
2018-06-14 05:36:08 +03:00
|
|
|
// Launch a node for Dave which will connect to Bob in order to receive
|
|
|
|
// graph updates from. This will ensure that the channel updates are
|
|
|
|
// propagated throughout the network.
|
|
|
|
dave, err := net.NewNode("Dave", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create dave's node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, dave)
|
2018-11-28 11:25:46 +03:00
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, net.Bob, dave); err != nil {
|
2018-06-14 05:36:08 +03:00
|
|
|
t.Fatalf("unable to connect bob to dave: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-23 08:16:16 +03:00
|
|
|
daveSub := subscribeGraphNotifications(t, ctxb, dave)
|
|
|
|
defer close(daveSub.quit)
|
2018-06-14 05:36:08 +03:00
|
|
|
|
|
|
|
// We should expect to see a channel update with the default routing
|
|
|
|
// policy, except that it should indicate the channel is disabled.
|
|
|
|
expectedPolicy := &lnrpc.RoutingPolicy{
|
2019-05-15 10:02:53 +03:00
|
|
|
FeeBaseMsat: int64(lnd.DefaultBitcoinBaseFeeMSat),
|
|
|
|
FeeRateMilliMsat: int64(lnd.DefaultBitcoinFeeRate),
|
|
|
|
TimeLockDelta: lnd.DefaultBitcoinTimeLockDelta,
|
2018-08-22 10:32:43 +03:00
|
|
|
MinHtlc: 1000, // default value
|
2018-06-14 05:36:08 +03:00
|
|
|
Disabled: true,
|
|
|
|
}
|
|
|
|
|
2018-08-09 11:03:15 +03:00
|
|
|
// Let Carol go offline. Since Eve has an inactive timeout of 2s, we
|
|
|
|
// expect her to send an update disabling the channel.
|
|
|
|
restartCarol, err := net.SuspendNode(carol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to suspend carol: %v", err)
|
|
|
|
}
|
|
|
|
waitForChannelUpdate(
|
2018-08-23 08:16:16 +03:00
|
|
|
t, daveSub,
|
2018-08-22 10:32:42 +03:00
|
|
|
[]expectedChanUpdate{
|
|
|
|
{eve.PubKeyStr, expectedPolicy, chanPointEveCarol},
|
|
|
|
},
|
2018-08-09 11:03:15 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// We restart Carol. Since the channel now becomes active again, Eve
|
|
|
|
// should send a ChannelUpdate setting the channel no longer disabled.
|
|
|
|
if err := restartCarol(); err != nil {
|
|
|
|
t.Fatalf("unable to restart carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedPolicy.Disabled = false
|
|
|
|
waitForChannelUpdate(
|
2018-08-23 08:16:16 +03:00
|
|
|
t, daveSub,
|
2018-08-22 10:32:42 +03:00
|
|
|
[]expectedChanUpdate{
|
|
|
|
{eve.PubKeyStr, expectedPolicy, chanPointEveCarol},
|
|
|
|
},
|
2018-08-09 11:03:15 +03:00
|
|
|
)
|
|
|
|
|
2019-02-15 04:13:20 +03:00
|
|
|
// Now we'll test a long disconnection. Disconnect Carol and Eve and
|
|
|
|
// ensure they both detect each other as disabled. Their min backoffs
|
|
|
|
// are high enough to not interfere with disabling logic.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.DisconnectNodes(ctxt, carol, eve); err != nil {
|
|
|
|
t.Fatalf("unable to disconnect Carol from Eve: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for a disable from both Carol and Eve to come through.
|
|
|
|
expectedPolicy.Disabled = true
|
|
|
|
waitForChannelUpdate(
|
|
|
|
t, daveSub,
|
|
|
|
[]expectedChanUpdate{
|
|
|
|
{eve.PubKeyStr, expectedPolicy, chanPointEveCarol},
|
|
|
|
{carol.PubKeyStr, expectedPolicy, chanPointEveCarol},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Reconnect Carol and Eve, this should cause them to reenable the
|
|
|
|
// channel from both ends after a short delay.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.EnsureConnected(ctxt, carol, eve); err != nil {
|
|
|
|
t.Fatalf("unable to reconnect Carol to Eve: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedPolicy.Disabled = false
|
|
|
|
waitForChannelUpdate(
|
|
|
|
t, daveSub,
|
|
|
|
[]expectedChanUpdate{
|
|
|
|
{eve.PubKeyStr, expectedPolicy, chanPointEveCarol},
|
|
|
|
{carol.PubKeyStr, expectedPolicy, chanPointEveCarol},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Now we'll test a short disconnection. Disconnect Carol and Eve, then
|
|
|
|
// reconnect them after one second so that their scheduled disables are
|
|
|
|
// aborted. One second is twice the status sample interval, so this
|
|
|
|
// should allow for the disconnect to be detected, but still leave time
|
|
|
|
// to cancel the announcement before the 3 second inactive timeout is
|
|
|
|
// hit.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.DisconnectNodes(ctxt, carol, eve); err != nil {
|
|
|
|
t.Fatalf("unable to disconnect Carol from Eve: %v", err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.EnsureConnected(ctxt, eve, carol); err != nil {
|
|
|
|
t.Fatalf("unable to reconnect Carol to Eve: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since the disable should have been canceled by both Carol and Eve, we
|
|
|
|
// expect no channel updates to appear on the network.
|
|
|
|
assertNoChannelUpdates(t, daveSub, 4*time.Second)
|
|
|
|
|
2018-06-14 05:36:08 +03:00
|
|
|
// Close Alice's channels with Bob and Carol cooperatively and
|
|
|
|
// unilaterally respectively.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-08-09 11:03:13 +03:00
|
|
|
_, _, err = net.CloseChannel(ctxt, net.Alice, chanPointAliceBob, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to close channel: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-08-09 11:03:13 +03:00
|
|
|
_, _, err = net.CloseChannel(ctxt, net.Alice, chanPointAliceCarol, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to close channel: %v", err)
|
|
|
|
}
|
2018-06-14 05:36:08 +03:00
|
|
|
|
2018-08-09 11:03:13 +03:00
|
|
|
// Now that the channel close processes have been started, we should
|
|
|
|
// receive an update marking each as disabled.
|
2018-08-09 11:03:15 +03:00
|
|
|
expectedPolicy.Disabled = true
|
2018-06-14 05:36:08 +03:00
|
|
|
waitForChannelUpdate(
|
2018-08-23 08:16:16 +03:00
|
|
|
t, daveSub,
|
2018-08-22 10:32:42 +03:00
|
|
|
[]expectedChanUpdate{
|
|
|
|
{net.Alice.PubKeyStr, expectedPolicy, chanPointAliceBob},
|
|
|
|
{net.Alice.PubKeyStr, expectedPolicy, chanPointAliceCarol},
|
|
|
|
},
|
2018-06-14 05:36:08 +03:00
|
|
|
)
|
2018-08-09 11:03:13 +03:00
|
|
|
|
|
|
|
// Finally, close the channels by mining the closing transactions.
|
2018-11-29 14:33:11 +03:00
|
|
|
mineBlocks(t, net, 1, 2)
|
2018-08-09 11:03:15 +03:00
|
|
|
|
|
|
|
// Also do this check for Eve's channel with Carol.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-08-09 11:03:15 +03:00
|
|
|
_, _, err = net.CloseChannel(ctxt, eve, chanPointEveCarol, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to close channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
waitForChannelUpdate(
|
2018-08-23 08:16:16 +03:00
|
|
|
t, daveSub,
|
2018-08-22 10:32:42 +03:00
|
|
|
[]expectedChanUpdate{
|
|
|
|
{eve.PubKeyStr, expectedPolicy, chanPointEveCarol},
|
|
|
|
},
|
2018-08-09 11:03:15 +03:00
|
|
|
)
|
2018-11-29 14:33:11 +03:00
|
|
|
mineBlocks(t, net, 1, 1)
|
2018-06-14 05:36:08 +03:00
|
|
|
}
|
|
|
|
|
2018-06-20 10:09:02 +03:00
|
|
|
// testAbandonChannel abandones a channel and asserts that it is no
|
|
|
|
// longer open and not in one of the pending closure states. It also
|
|
|
|
// verifies that the abandoned channel is reported as closed with close
|
|
|
|
// type 'abandoned'.
|
|
|
|
func testAbandonChannel(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// First establish a channel between Alice and Bob.
|
|
|
|
channelParam := lntest.OpenChannelParams{
|
2019-05-15 10:02:53 +03:00
|
|
|
Amt: lnd.MaxBtcFundingAmount,
|
2018-06-20 10:09:02 +03:00
|
|
|
PushAmt: btcutil.Amount(100000),
|
|
|
|
}
|
|
|
|
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-06-20 10:09:02 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Alice, net.Bob, channelParam)
|
|
|
|
|
|
|
|
// Wait for channel to be confirmed open.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-06-20 10:09:02 +03:00
|
|
|
err := net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't report channel: %v", err)
|
|
|
|
}
|
|
|
|
err = net.Bob.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't report channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send request to abandon channel.
|
|
|
|
abandonChannelRequest := &lnrpc.AbandonChannelRequest{
|
|
|
|
ChannelPoint: chanPoint,
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-06-20 10:09:02 +03:00
|
|
|
_, err = net.Alice.AbandonChannel(ctxt, abandonChannelRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to abandon channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that channel in no longer open.
|
|
|
|
listReq := &lnrpc.ListChannelsRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
aliceChannelList, err := net.Alice.ListChannels(ctxt, listReq)
|
2018-06-20 10:09:02 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to list channels: %v", err)
|
|
|
|
}
|
|
|
|
if len(aliceChannelList.Channels) != 0 {
|
|
|
|
t.Fatalf("alice should only have no channels open, "+
|
|
|
|
"instead she has %v",
|
|
|
|
len(aliceChannelList.Channels))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that channel is not pending closure.
|
|
|
|
pendingReq := &lnrpc.PendingChannelsRequest{}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
alicePendingList, err := net.Alice.PendingChannels(ctxt, pendingReq)
|
2018-06-20 10:09:02 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to list pending channels: %v", err)
|
|
|
|
}
|
|
|
|
if len(alicePendingList.PendingClosingChannels) != 0 {
|
|
|
|
t.Fatalf("alice should only have no pending closing channels, "+
|
|
|
|
"instead she has %v",
|
|
|
|
len(alicePendingList.PendingClosingChannels))
|
|
|
|
}
|
|
|
|
if len(alicePendingList.PendingForceClosingChannels) != 0 {
|
|
|
|
t.Fatalf("alice should only have no pending force closing "+
|
|
|
|
"channels instead she has %v",
|
|
|
|
len(alicePendingList.PendingForceClosingChannels))
|
|
|
|
}
|
|
|
|
if len(alicePendingList.WaitingCloseChannels) != 0 {
|
|
|
|
t.Fatalf("alice should only have no waiting close "+
|
|
|
|
"channels instead she has %v",
|
|
|
|
len(alicePendingList.WaitingCloseChannels))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that channel is listed as abandoned.
|
|
|
|
closedReq := &lnrpc.ClosedChannelsRequest{
|
|
|
|
Abandoned: true,
|
|
|
|
}
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
aliceClosedList, err := net.Alice.ClosedChannels(ctxt, closedReq)
|
2018-06-20 10:09:02 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to list closed channels: %v", err)
|
|
|
|
}
|
|
|
|
if len(aliceClosedList.Channels) != 1 {
|
|
|
|
t.Fatalf("alice should only have a single abandoned channel, "+
|
|
|
|
"instead she has %v",
|
|
|
|
len(aliceClosedList.Channels))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we're done with the test, the channel can be closed. This is
|
|
|
|
// necessary to avoid unexpected outcomes of other tests that use Bob's
|
|
|
|
// lnd instance.
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-06-20 10:09:02 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Bob, chanPoint, true)
|
2018-11-06 13:03:01 +03:00
|
|
|
|
|
|
|
// Cleanup by mining the force close and sweep transaction.
|
|
|
|
cleanupForceClose(t, net, net.Bob, chanPoint)
|
2018-06-20 10:09:02 +03:00
|
|
|
}
|
|
|
|
|
2018-12-25 01:05:48 +03:00
|
|
|
// testSweepAllCoins tests that we're able to properly sweep all coins from the
|
|
|
|
// wallet into a single target address at the specified fee rate.
|
|
|
|
func testSweepAllCoins(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// First, we'll make a new node, ainz who'll we'll use to test wallet
|
|
|
|
// sweeping.
|
|
|
|
ainz, err := net.NewNode("Ainz", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, ainz)
|
|
|
|
|
|
|
|
// Next, we'll give Ainz exactly 2 utxos of 1 BTC each, with one of
|
|
|
|
// them being p2wkh and the other being a n2wpkh address.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, ainz)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to eve: %v", err)
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoinsNP2WKH(ctxt, btcutil.SatoshiPerBitcoin, ainz)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to eve: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-04-10 05:45:55 +03:00
|
|
|
// Ensure that we can't send coins to our own Pubkey.
|
|
|
|
info, err := ainz.GetInfo(ctxt, &lnrpc.GetInfoRequest{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get node info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sweepReq := &lnrpc.SendCoinsRequest{
|
|
|
|
Addr: info.IdentityPubkey,
|
|
|
|
SendAll: true,
|
|
|
|
}
|
|
|
|
_, err = ainz.SendCoins(ctxt, sweepReq)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected SendCoins to users own pubkey to fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that we can't send coins to another users Pubkey.
|
|
|
|
info, err = net.Alice.GetInfo(ctxt, &lnrpc.GetInfoRequest{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get node info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sweepReq = &lnrpc.SendCoinsRequest{
|
|
|
|
Addr: info.IdentityPubkey,
|
|
|
|
SendAll: true,
|
|
|
|
}
|
|
|
|
_, err = ainz.SendCoins(ctxt, sweepReq)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected SendCoins to Alices pubkey to fail")
|
|
|
|
}
|
|
|
|
|
2018-12-25 01:05:48 +03:00
|
|
|
// With the two coins above mined, we'll now instruct ainz to sweep all
|
|
|
|
// the coins to an external address not under its control.
|
2019-02-27 22:16:34 +03:00
|
|
|
// We will first attempt to send the coins to addresses that are not
|
|
|
|
// compatible with the current network. This is to test that the wallet
|
|
|
|
// will prevent any onchain transactions to addresses that are not on the
|
|
|
|
// same network as the user.
|
|
|
|
|
|
|
|
// Send coins to a testnet3 address.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2019-04-10 05:45:55 +03:00
|
|
|
sweepReq = &lnrpc.SendCoinsRequest{
|
2019-02-27 22:16:34 +03:00
|
|
|
Addr: "tb1qfc8fusa98jx8uvnhzavxccqlzvg749tvjw82tg",
|
|
|
|
SendAll: true,
|
|
|
|
}
|
|
|
|
_, err = ainz.SendCoins(ctxt, sweepReq)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected SendCoins to different network to fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send coins to a mainnet address.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
sweepReq = &lnrpc.SendCoinsRequest{
|
|
|
|
Addr: "1MPaXKp5HhsLNjVSqaL7fChE3TVyrTMRT3",
|
|
|
|
SendAll: true,
|
|
|
|
}
|
|
|
|
_, err = ainz.SendCoins(ctxt, sweepReq)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected SendCoins to different network to fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send coins to a compatible address.
|
2018-12-25 01:05:48 +03:00
|
|
|
minerAddr, err := net.Miner.NewAddress()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new miner addr: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:16:34 +03:00
|
|
|
sweepReq = &lnrpc.SendCoinsRequest{
|
2018-12-25 01:05:48 +03:00
|
|
|
Addr: minerAddr.String(),
|
|
|
|
SendAll: true,
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
_, err = ainz.SendCoins(ctxt, sweepReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to sweep coins: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:16:34 +03:00
|
|
|
// We'll mine a block which should include the sweep transaction we
|
2018-12-25 01:05:48 +03:00
|
|
|
// generated above.
|
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
|
|
|
|
|
|
|
// The sweep transaction should have exactly two inputs as we only had
|
|
|
|
// two UTXOs in the wallet.
|
|
|
|
sweepTx := block.Transactions[1]
|
|
|
|
if len(sweepTx.TxIn) != 2 {
|
|
|
|
t.Fatalf("expected 2 inputs instead have %v", len(sweepTx.TxIn))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, Ainz should now have no coins at all within his wallet.
|
|
|
|
balReq := &lnrpc.WalletBalanceRequest{}
|
|
|
|
resp, err := ainz.WalletBalance(ctxt, balReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get ainz's balance: %v", err)
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case resp.ConfirmedBalance != 0:
|
|
|
|
t.Fatalf("expected no confirmed balance, instead have %v",
|
|
|
|
resp.ConfirmedBalance)
|
|
|
|
|
|
|
|
case resp.UnconfirmedBalance != 0:
|
|
|
|
t.Fatalf("expected no unconfirmed balance, instead have %v",
|
|
|
|
resp.UnconfirmedBalance)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we try again, but this time specifying an amount, then the call
|
|
|
|
// should fail.
|
|
|
|
sweepReq.Amount = 10000
|
|
|
|
_, err = ainz.SendCoins(ctxt, sweepReq)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("sweep attempt should fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 03:38:38 +03:00
|
|
|
// testChannelBackupUpdates tests that both the streaming channel update RPC,
|
|
|
|
// and the on-disk channels.backup are updated each time a channel is
|
|
|
|
// opened/closed.
|
|
|
|
func testChannelBackupUpdates(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// First, we'll make a temp directory that we'll use to store our
|
|
|
|
// backup file, so we can check in on it during the test easily.
|
|
|
|
backupDir, err := ioutil.TempDir("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create backup dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(backupDir)
|
|
|
|
|
|
|
|
// First, we'll create a new node, Carol. We'll also create a temporary
|
|
|
|
// file that Carol will use to store her channel backups.
|
|
|
|
backupFilePath := filepath.Join(
|
|
|
|
backupDir, chanbackup.DefaultBackupFileName,
|
|
|
|
)
|
|
|
|
carolArgs := fmt.Sprintf("--backupfilepath=%v", backupFilePath)
|
|
|
|
carol, err := net.NewNode("carol", []string{carolArgs})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
|
|
|
// Next, we'll register for streaming notifications for changes to the
|
|
|
|
// backup file.
|
|
|
|
backupStream, err := carol.SubscribeChannelBackups(
|
|
|
|
ctxb, &lnrpc.ChannelBackupSubscription{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create backup stream: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll use this goroutine to proxy any updates to a channel we can
|
|
|
|
// easily use below.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
backupUpdates := make(chan *lnrpc.ChanBackupSnapshot)
|
|
|
|
streamErr := make(chan error)
|
|
|
|
streamQuit := make(chan struct{})
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
snapshot, err := backupStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case streamErr <- err:
|
|
|
|
case <-streamQuit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case backupUpdates <- snapshot:
|
|
|
|
case <-streamQuit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer close(streamQuit)
|
|
|
|
|
|
|
|
// With Carol up, we'll now connect her to Alice, and open a channel
|
|
|
|
// between them.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, net.Alice); err != nil {
|
|
|
|
t.Fatalf("unable to connect carol to alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll open two channels between Alice and Carol back to back.
|
|
|
|
var chanPoints []*lnrpc.ChannelPoint
|
|
|
|
numChans := 2
|
|
|
|
chanAmt := btcutil.Amount(1000000)
|
|
|
|
for i := 0; i < numChans; i++ {
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
chanPoint := openChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Alice, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
chanPoints = append(chanPoints, chanPoint)
|
|
|
|
}
|
|
|
|
|
2019-03-12 03:41:01 +03:00
|
|
|
// Using this helper function, we'll maintain a pointer to the latest
|
2019-03-12 03:38:38 +03:00
|
|
|
// channel backup so we can compare it to the on disk state.
|
|
|
|
var currentBackup *lnrpc.ChanBackupSnapshot
|
|
|
|
assertBackupNtfns := func(numNtfns int) {
|
|
|
|
for i := 0; i < numNtfns; i++ {
|
|
|
|
select {
|
|
|
|
case err := <-streamErr:
|
|
|
|
t.Fatalf("error with backup stream: %v", err)
|
|
|
|
|
|
|
|
case currentBackup = <-backupUpdates:
|
|
|
|
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("didn't receive channel backup "+
|
|
|
|
"notification %v", i+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertBackupFileState is a helper function that we'll use to compare
|
|
|
|
// the on disk back up file to our currentBackup pointer above.
|
|
|
|
assertBackupFileState := func() {
|
|
|
|
err := lntest.WaitNoError(func() error {
|
|
|
|
packedBackup, err := ioutil.ReadFile(backupFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to read backup "+
|
|
|
|
"file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// As each back up file will be encrypted with a fresh
|
|
|
|
// nonce, we can't compare them directly, so instead
|
|
|
|
// we'll compare the length which is a proxy for the
|
|
|
|
// number of channels that the multi-backup contains.
|
|
|
|
rawBackup := currentBackup.MultiChanBackup.MultiChanBackup
|
|
|
|
if len(rawBackup) != len(packedBackup) {
|
|
|
|
return fmt.Errorf("backup files don't match: "+
|
|
|
|
"expected %x got %x", rawBackup, packedBackup)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Additionally, we'll assert that both backups up
|
|
|
|
// returned are valid.
|
|
|
|
for i, backup := range [][]byte{rawBackup, packedBackup} {
|
|
|
|
snapshot := &lnrpc.ChanBackupSnapshot{
|
|
|
|
MultiChanBackup: &lnrpc.MultiChanBackup{
|
|
|
|
MultiChanBackup: backup,
|
|
|
|
},
|
|
|
|
}
|
2019-04-05 01:52:31 +03:00
|
|
|
_, err := carol.VerifyChanBackup(ctxb, snapshot)
|
2019-03-12 03:38:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to verify "+
|
2019-04-05 01:52:31 +03:00
|
|
|
"backup #%d: %v", i, err)
|
2019-03-12 03:38:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("backup state invalid: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// As these two channels were just open, we should've got two
|
|
|
|
// notifications for channel backups.
|
|
|
|
assertBackupNtfns(2)
|
|
|
|
|
|
|
|
// The on disk file should also exactly match the latest backup that we
|
|
|
|
// have.
|
|
|
|
assertBackupFileState()
|
|
|
|
|
|
|
|
// Next, we'll close the channels one by one. After each channel
|
|
|
|
// closure, we should get a notification, and the on-disk state should
|
|
|
|
// match this state as well.
|
|
|
|
for i := 0; i < numChans; i++ {
|
|
|
|
// To ensure force closes also trigger an update, we'll force
|
|
|
|
// close half of the channels.
|
|
|
|
forceClose := i%2 == 0
|
|
|
|
|
|
|
|
chanPoint := chanPoints[i]
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
|
|
|
closeChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Alice, chanPoint, forceClose,
|
|
|
|
)
|
|
|
|
|
|
|
|
// We should get a single notification after closing, and the
|
|
|
|
// on-disk state should match this latest notifications.
|
|
|
|
assertBackupNtfns(1)
|
|
|
|
assertBackupFileState()
|
|
|
|
|
|
|
|
// If we force closed the channel, then we'll mine enough
|
|
|
|
// blocks to ensure all outputs have been swept.
|
|
|
|
if forceClose {
|
|
|
|
cleanupForceClose(t, net, net.Alice, chanPoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 03:39:00 +03:00
|
|
|
// testExportChannelBackup tests that we're able to properly export either a
|
|
|
|
// targeted channel's backup, or export backups of all the currents open
|
|
|
|
// channels.
|
|
|
|
func testExportChannelBackup(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// First, we'll create our primary test node: Carol. We'll use Carol to
|
|
|
|
// open channels and also export backups that we'll examine throughout
|
|
|
|
// the test.
|
|
|
|
carol, err := net.NewNode("carol", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
|
|
|
// With Carol up, we'll now connect her to Alice, and open a channel
|
|
|
|
// between them.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, net.Alice); err != nil {
|
|
|
|
t.Fatalf("unable to connect carol to alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll open two channels between Alice and Carol back to back.
|
|
|
|
var chanPoints []*lnrpc.ChannelPoint
|
|
|
|
numChans := 2
|
|
|
|
chanAmt := btcutil.Amount(1000000)
|
|
|
|
for i := 0; i < numChans; i++ {
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
chanPoint := openChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Alice, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
chanPoints = append(chanPoints, chanPoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the channels are open, we should be able to fetch the
|
|
|
|
// backups of each of the channels.
|
|
|
|
for _, chanPoint := range chanPoints {
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2019-03-12 03:41:01 +03:00
|
|
|
req := &lnrpc.ExportChannelBackupRequest{
|
|
|
|
ChanPoint: chanPoint,
|
|
|
|
}
|
|
|
|
chanBackup, err := carol.ExportChannelBackup(ctxt, req)
|
2019-03-12 03:39:00 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch backup for channel %v: %v",
|
|
|
|
chanPoint, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The returned backup should be full populated. Since it's
|
|
|
|
// encrypted, we can't assert any more than that atm.
|
|
|
|
if len(chanBackup.ChanBackup) == 0 {
|
|
|
|
t.Fatalf("obtained empty backup for channel: %v", chanPoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The specified chanPoint in the response should match our
|
|
|
|
// requested chanPoint.
|
|
|
|
if chanBackup.ChanPoint.String() != chanPoint.String() {
|
|
|
|
t.Fatalf("chanPoint mismatched: expected %v, got %v",
|
|
|
|
chanPoint.String(),
|
|
|
|
chanBackup.ChanPoint.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we proceed, we'll make two utility methods we'll use below
|
|
|
|
// for our primary assertions.
|
|
|
|
assertNumSingleBackups := func(numSingles int) {
|
2019-04-11 05:23:08 +03:00
|
|
|
err := lntest.WaitNoError(func() error {
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
req := &lnrpc.ChanBackupExportRequest{}
|
|
|
|
chanSnapshot, err := carol.ExportAllChannelBackups(
|
|
|
|
ctxt, req,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to export channel "+
|
|
|
|
"backup: %v", err)
|
|
|
|
}
|
2019-03-12 03:39:00 +03:00
|
|
|
|
2019-04-11 05:23:08 +03:00
|
|
|
if chanSnapshot.SingleChanBackups == nil {
|
|
|
|
return fmt.Errorf("single chan backups not " +
|
|
|
|
"populated")
|
|
|
|
}
|
2019-03-12 03:39:00 +03:00
|
|
|
|
2019-04-11 05:23:08 +03:00
|
|
|
backups := chanSnapshot.SingleChanBackups.ChanBackups
|
|
|
|
if len(backups) != numSingles {
|
|
|
|
return fmt.Errorf("expected %v singles, "+
|
|
|
|
"got %v", len(backups), numSingles)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, defaultTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
2019-03-12 03:39:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assertMultiBackupFound := func() func(bool, map[wire.OutPoint]struct{}) {
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
req := &lnrpc.ChanBackupExportRequest{}
|
|
|
|
chanSnapshot, err := carol.ExportAllChannelBackups(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to export channel backup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(found bool, chanPoints map[wire.OutPoint]struct{}) {
|
|
|
|
switch {
|
|
|
|
case found && chanSnapshot.MultiChanBackup == nil:
|
|
|
|
t.Fatalf("multi-backup not present")
|
|
|
|
|
|
|
|
case !found && chanSnapshot.MultiChanBackup != nil &&
|
|
|
|
(len(chanSnapshot.MultiChanBackup.MultiChanBackup) !=
|
|
|
|
chanbackup.NilMultiSizePacked):
|
|
|
|
|
|
|
|
t.Fatalf("found multi-backup when non should " +
|
|
|
|
"be found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
backedUpChans := chanSnapshot.MultiChanBackup.ChanPoints
|
|
|
|
if len(chanPoints) != len(backedUpChans) {
|
|
|
|
t.Fatalf("expected %v chans got %v", len(chanPoints),
|
|
|
|
len(backedUpChans))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, chanPoint := range backedUpChans {
|
|
|
|
wirePoint := rpcPointToWirePoint(t, chanPoint)
|
|
|
|
if _, ok := chanPoints[wirePoint]; !ok {
|
|
|
|
t.Fatalf("unexpected backup: %v", wirePoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chans := make(map[wire.OutPoint]struct{})
|
|
|
|
for _, chanPoint := range chanPoints {
|
|
|
|
chans[rpcPointToWirePoint(t, chanPoint)] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should have exactly two single channel backups contained, and we
|
|
|
|
// should also have a multi-channel backup.
|
|
|
|
assertNumSingleBackups(2)
|
|
|
|
assertMultiBackupFound()(true, chans)
|
|
|
|
|
|
|
|
// We'll now close each channel on by one. After we close a channel, we
|
|
|
|
// shouldn't be able to find that channel as a backup still. We should
|
|
|
|
// also have one less single written to disk.
|
|
|
|
for i, chanPoint := range chanPoints {
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
|
|
|
closeChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Alice, chanPoint, false,
|
|
|
|
)
|
|
|
|
|
|
|
|
assertNumSingleBackups(len(chanPoints) - i - 1)
|
|
|
|
|
|
|
|
delete(chans, rpcPointToWirePoint(t, chanPoint))
|
|
|
|
assertMultiBackupFound()(true, chans)
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point we shouldn't have any single or multi-chan backups at
|
|
|
|
// all.
|
|
|
|
assertNumSingleBackups(0)
|
|
|
|
assertMultiBackupFound()(false, nil)
|
|
|
|
}
|
2019-03-12 03:41:01 +03:00
|
|
|
|
|
|
|
// nodeRestorer is a function closure that allows each chanRestoreTestCase to
|
|
|
|
// control exactly *how* the prior node is restored. This might be using an
|
|
|
|
// backup obtained over RPC, or the file system, etc.
|
|
|
|
type nodeRestorer func() (*lntest.HarnessNode, error)
|
|
|
|
|
|
|
|
// chanRestoreTestCase describes a test case for an end to end SCB restoration
|
|
|
|
// work flow. One node will start from scratch using an existing SCB. At the
|
|
|
|
// end of the est, both nodes should be made whole via the DLP protocol.
|
|
|
|
type chanRestoreTestCase struct {
|
|
|
|
// name is the name of the target test case.
|
|
|
|
name string
|
|
|
|
|
|
|
|
// channelsUpdated is false then this means that no updates
|
|
|
|
// have taken place within the channel before restore.
|
|
|
|
// Otherwise, HTLCs will be settled between the two parties
|
|
|
|
// before restoration modifying the balance beyond the initial
|
|
|
|
// allocation.
|
|
|
|
channelsUpdated bool
|
|
|
|
|
|
|
|
// initiator signals if Dave should be the one that opens the
|
|
|
|
// channel to Alice, or if it should be the other way around.
|
|
|
|
initiator bool
|
|
|
|
|
|
|
|
// private signals if the channel from Dave to Carol should be
|
|
|
|
// private or not.
|
|
|
|
private bool
|
|
|
|
|
|
|
|
// restoreMethod takes an old node, then returns a function
|
|
|
|
// closure that'll return the same node, but with its state
|
|
|
|
// restored via a custom method. We use this to abstract away
|
|
|
|
// _how_ a node is restored from our assertions once the node
|
|
|
|
// has been fully restored itself.
|
|
|
|
restoreMethod func(oldNode *lntest.HarnessNode,
|
|
|
|
backupFilePath string,
|
|
|
|
mnemonic []string) (nodeRestorer, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testChanRestoreScenario executes a chanRestoreTestCase from end to end,
|
|
|
|
// ensuring that after Dave restores his channel state according to the
|
|
|
|
// testCase, the DLP protocol is executed properly and both nodes are made
|
|
|
|
// whole.
|
|
|
|
func testChanRestoreScenario(t *harnessTest, net *lntest.NetworkHarness,
|
|
|
|
testCase *chanRestoreTestCase, password []byte) {
|
|
|
|
|
|
|
|
const (
|
|
|
|
chanAmt = btcutil.Amount(10000000)
|
|
|
|
pushAmt = btcutil.Amount(5000000)
|
|
|
|
)
|
|
|
|
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// First, we'll create a brand new node we'll use within the test. If
|
|
|
|
// we have a custom backup file specified, then we'll also create that
|
|
|
|
// for use.
|
|
|
|
dave, mnemonic, err := net.NewNodeWithSeed(
|
|
|
|
"dave", nil, password,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
carol, err := net.NewNode("carol", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
|
|
|
// Now that our new node is created, we'll give him some coins it can
|
|
|
|
// use to open channels with Carol.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, dave)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to dave: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var from, to *lntest.HarnessNode
|
|
|
|
if testCase.initiator {
|
|
|
|
from, to = dave, carol
|
|
|
|
} else {
|
|
|
|
from, to = carol, dave
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll connect Dave to Carol, and open a new channel to her
|
|
|
|
// with a portion pushed.
|
|
|
|
if err := net.ConnectNodes(ctxt, dave, carol); err != nil {
|
|
|
|
t.Fatalf("unable to connect dave to carol: %v", err)
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
|
|
|
chanPoint := openChannelAndAssert(
|
|
|
|
ctxt, t, net, from, to,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
Private: testCase.private,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Wait for both sides to see the opened channel.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = dave.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("dave didn't report channel: %v", err)
|
|
|
|
}
|
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("carol didn't report channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If both parties should start with existing channel updates, then
|
|
|
|
// we'll send+settle an HTLC between 'from' and 'to' now.
|
|
|
|
if testCase.channelsUpdated {
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
Value: 10000,
|
|
|
|
}
|
|
|
|
invoiceResp, err := to.AddInvoice(ctxt, invoice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, from, []string{invoiceResp.PaymentRequest},
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to complete payments: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we start the recovery, we'll record the balances of both
|
|
|
|
// Carol and Dave to ensure they both sweep their coins at the end.
|
|
|
|
balReq := &lnrpc.WalletBalanceRequest{}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
carolBalResp, err := carol.WalletBalance(ctxt, balReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get carol's balance: %v", err)
|
|
|
|
}
|
|
|
|
carolStartingBalance := carolBalResp.ConfirmedBalance
|
|
|
|
|
|
|
|
daveBalance, err := dave.WalletBalance(ctxt, balReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get carol's balance: %v", err)
|
|
|
|
}
|
|
|
|
daveStartingBalance := daveBalance.ConfirmedBalance
|
|
|
|
|
|
|
|
// At this point, we'll now execute the restore method to give us the
|
|
|
|
// new node we should attempt our assertions against.
|
|
|
|
backupFilePath := dave.ChanBackupPath()
|
|
|
|
restoredNodeFunc, err := testCase.restoreMethod(
|
|
|
|
dave, backupFilePath, mnemonic,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to prep node restoration: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): assert recovery state in channel
|
|
|
|
|
|
|
|
// Now that we're able to make our restored now, we'll shutdown the old
|
|
|
|
// Dave node as we'll be storing it shortly below.
|
|
|
|
shutdownAndAssert(net, t, dave)
|
|
|
|
|
|
|
|
// Next, we'll make a new Dave and start the bulk of our recovery
|
|
|
|
// workflow.
|
|
|
|
dave, err = restoredNodeFunc()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to restore node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have our new node up, we expect that it'll re-connect to
|
|
|
|
// Carol automatically based on the restored backup.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.EnsureConnected(ctxt, dave, carol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("node didn't connect after recovery: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): move dave restarts?
|
|
|
|
|
|
|
|
// Now we'll assert that both sides properly execute the DLP protocol.
|
|
|
|
// We grab their balances now to ensure that they're made whole at the
|
|
|
|
// end of the protocol.
|
|
|
|
assertDLPExecuted(
|
|
|
|
net, t, carol, carolStartingBalance, dave, daveStartingBalance,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// chanRestoreViaRPC is a helper test method that returns a nodeRestorer
|
|
|
|
// instance which will restore the target node from a password+seed, then
|
|
|
|
// trigger a SCB restore using the RPC interface.
|
|
|
|
func chanRestoreViaRPC(net *lntest.NetworkHarness,
|
|
|
|
password []byte, mnemonic []string,
|
|
|
|
multi []byte) (nodeRestorer, error) {
|
|
|
|
|
|
|
|
backup := &lnrpc.RestoreChanBackupRequest_MultiChanBackup{
|
|
|
|
MultiChanBackup: multi,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
return func() (*lntest.HarnessNode, error) {
|
|
|
|
newNode, err := net.RestoreNodeWithSeed(
|
|
|
|
"dave", nil, password, mnemonic, 1000, nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to "+
|
|
|
|
"restore node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = newNode.RestoreChannelBackups(
|
|
|
|
ctxb, &lnrpc.RestoreChanBackupRequest{
|
|
|
|
Backup: backup,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable "+
|
|
|
|
"to restore backups: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newNode, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// testChannelBackupRestore tests that we're able to recover from, and initiate
|
|
|
|
// the DLP protocol via: the RPC restore command, restoring on unlock, and
|
|
|
|
// restoring from initial wallet creation. We'll also alternate between
|
|
|
|
// restoring form the on disk file, and restoring from the exported RPC command
|
|
|
|
// as well.
|
|
|
|
func testChannelBackupRestore(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
password := []byte("El Psy Kongroo")
|
|
|
|
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
var testCases = []chanRestoreTestCase{
|
|
|
|
// Restore from backups obtained via the RPC interface. Dave
|
|
|
|
// was the initiator, of the non-advertised channel.
|
|
|
|
{
|
|
|
|
name: "restore from RPC backup",
|
|
|
|
channelsUpdated: false,
|
|
|
|
initiator: true,
|
|
|
|
private: false,
|
|
|
|
restoreMethod: func(oldNode *lntest.HarnessNode,
|
|
|
|
backupFilePath string,
|
|
|
|
mnemonic []string) (nodeRestorer, error) {
|
|
|
|
|
|
|
|
// For this restoration method, we'll grab the
|
|
|
|
// current multi-channel backup from the old
|
|
|
|
// node, and use it to restore a new node
|
|
|
|
// within the closure.
|
|
|
|
req := &lnrpc.ChanBackupExportRequest{}
|
|
|
|
chanBackup, err := oldNode.ExportAllChannelBackups(
|
|
|
|
ctxb, req,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to obtain "+
|
|
|
|
"channel backup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
multi := chanBackup.MultiChanBackup.MultiChanBackup
|
|
|
|
|
|
|
|
// In our nodeRestorer function, we'll restore
|
|
|
|
// the node from seed, then manually recover
|
|
|
|
// the channel backup.
|
|
|
|
return chanRestoreViaRPC(
|
|
|
|
net, password, mnemonic, multi,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Restore the backup from the on-disk file, using the RPC
|
|
|
|
// interface.
|
|
|
|
{
|
|
|
|
name: "restore from backup file",
|
|
|
|
initiator: true,
|
|
|
|
private: false,
|
|
|
|
restoreMethod: func(oldNode *lntest.HarnessNode,
|
|
|
|
backupFilePath string,
|
|
|
|
mnemonic []string) (nodeRestorer, error) {
|
|
|
|
|
|
|
|
// Read the entire Multi backup stored within
|
|
|
|
// this node's chaannels.backup file.
|
|
|
|
multi, err := ioutil.ReadFile(backupFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have Dave's backup file, we'll
|
|
|
|
// create a new nodeRestorer that will restore
|
|
|
|
// using the on-disk channels.backup.
|
|
|
|
return chanRestoreViaRPC(
|
|
|
|
net, password, mnemonic, multi,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Restore the backup as part of node initialization with the
|
|
|
|
// prior mnemonic and new backup seed.
|
|
|
|
{
|
|
|
|
name: "restore during creation",
|
|
|
|
initiator: true,
|
|
|
|
private: false,
|
|
|
|
restoreMethod: func(oldNode *lntest.HarnessNode,
|
|
|
|
backupFilePath string,
|
|
|
|
mnemonic []string) (nodeRestorer, error) {
|
|
|
|
|
|
|
|
// First, fetch the current backup state as is,
|
|
|
|
// to obtain our latest Multi.
|
|
|
|
chanBackup, err := oldNode.ExportAllChannelBackups(
|
|
|
|
ctxb, &lnrpc.ChanBackupExportRequest{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to obtain "+
|
|
|
|
"channel backup: %v", err)
|
|
|
|
}
|
|
|
|
backupSnapshot := &lnrpc.ChanBackupSnapshot{
|
|
|
|
MultiChanBackup: chanBackup.MultiChanBackup,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new nodeRestorer that will restore
|
|
|
|
// the node using the Multi backup we just
|
|
|
|
// obtained above.
|
|
|
|
return func() (*lntest.HarnessNode, error) {
|
|
|
|
return net.RestoreNodeWithSeed(
|
|
|
|
"dave", nil, password,
|
|
|
|
mnemonic, 1000, backupSnapshot,
|
|
|
|
)
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Restore the backup once the node has already been
|
|
|
|
// re-created, using the Unlock call.
|
|
|
|
{
|
|
|
|
name: "restore during unlock",
|
|
|
|
initiator: true,
|
|
|
|
private: false,
|
|
|
|
restoreMethod: func(oldNode *lntest.HarnessNode,
|
|
|
|
backupFilePath string,
|
|
|
|
mnemonic []string) (nodeRestorer, error) {
|
|
|
|
|
|
|
|
// First, fetch the current backup state as is,
|
|
|
|
// to obtain our latest Multi.
|
|
|
|
chanBackup, err := oldNode.ExportAllChannelBackups(
|
|
|
|
ctxb, &lnrpc.ChanBackupExportRequest{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to obtain "+
|
|
|
|
"channel backup: %v", err)
|
|
|
|
}
|
|
|
|
backupSnapshot := &lnrpc.ChanBackupSnapshot{
|
|
|
|
MultiChanBackup: chanBackup.MultiChanBackup,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new nodeRestorer that will restore
|
|
|
|
// the node with its seed, but no channel
|
|
|
|
// backup, shutdown this initialized node, then
|
|
|
|
// restart it again using Unlock.
|
|
|
|
return func() (*lntest.HarnessNode, error) {
|
|
|
|
newNode, err := net.RestoreNodeWithSeed(
|
|
|
|
"dave", nil, password,
|
|
|
|
mnemonic, 1000, nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = net.RestartNode(
|
|
|
|
newNode, nil, backupSnapshot,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newNode, nil
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): online vs offline close?
|
|
|
|
|
|
|
|
// TODO(roasbeef): need to re-trigger the on-disk file once the node
|
|
|
|
// ann is updated?
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
2019-04-04 03:23:12 +03:00
|
|
|
success := t.t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
h := newHarnessTest(t)
|
|
|
|
testChanRestoreScenario(h, net, &testCase, password)
|
2019-03-12 03:41:01 +03:00
|
|
|
})
|
|
|
|
if !success {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 17:42:26 +03:00
|
|
|
// testHoldInvoicePersistence tests that a sender to a hold-invoice, can be
|
|
|
|
// restarted before the payment gets settled, and still be able to receive the
|
|
|
|
// preimage.
|
|
|
|
func testHoldInvoicePersistence(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
const (
|
|
|
|
chanAmt = btcutil.Amount(1000000)
|
|
|
|
numPayments = 10
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create carol, and clean up when the test finishes.
|
|
|
|
carol, err := net.NewNode("Carol", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
|
|
|
// Connect Alice to Carol.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxb, net.Alice, carol); err != nil {
|
|
|
|
t.Fatalf("unable to connect alice to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open a channel between Alice and Carol.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
|
|
|
chanPointAlice := openChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Alice, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Wait for Alice and Carol to receive the channel edge from the
|
|
|
|
// funding manager.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPointAlice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't see the alice->carol channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPointAlice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't see the alice->carol channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create preimages for all payments we are going to initiate.
|
|
|
|
var preimages []lntypes.Preimage
|
|
|
|
for i := 0; i < numPayments; i++ {
|
|
|
|
var preimage lntypes.Preimage
|
|
|
|
_, err = rand.Read(preimage[:])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate preimage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
preimages = append(preimages, preimage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let Carol create hold-invoices for all the payments.
|
|
|
|
var (
|
|
|
|
payAmt = btcutil.Amount(4)
|
|
|
|
payReqs []string
|
|
|
|
invoiceStreams []invoicesrpc.Invoices_SubscribeSingleInvoiceClient
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, preimage := range preimages {
|
|
|
|
payHash := preimage.Hash()
|
|
|
|
invoiceReq := &invoicesrpc.AddHoldInvoiceRequest{
|
|
|
|
Memo: "testing",
|
|
|
|
Value: int64(payAmt),
|
|
|
|
Hash: payHash[:],
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := carol.AddHoldInvoice(ctxt, invoiceReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
stream, err := carol.SubscribeSingleInvoice(
|
|
|
|
ctx,
|
|
|
|
&invoicesrpc.SubscribeSingleInvoiceRequest{
|
|
|
|
RHash: payHash[:],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to subscribe to invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
invoiceStreams = append(invoiceStreams, stream)
|
|
|
|
payReqs = append(payReqs, resp.PaymentRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all the invoices to reach the OPEN state.
|
|
|
|
for _, stream := range invoiceStreams {
|
|
|
|
invoice, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if invoice.State != lnrpc.Invoice_OPEN {
|
|
|
|
t.Fatalf("expected OPEN, got state: %v", invoice.State)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let Alice initiate payments for all the created invoices.
|
|
|
|
var paymentStreams []routerrpc.Router_SendPaymentClient
|
|
|
|
for _, payReq := range payReqs {
|
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
payStream, err := net.Alice.RouterClient.SendPayment(
|
|
|
|
ctx, &routerrpc.SendPaymentRequest{
|
|
|
|
PaymentRequest: payReq,
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitSat: 1000000,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send alice htlc: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
paymentStreams = append(paymentStreams, payStream)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for inlight status update.
|
|
|
|
for _, payStream := range paymentStreams {
|
|
|
|
status, err := payStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed receiving status update: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status.State != routerrpc.PaymentState_IN_FLIGHT {
|
|
|
|
t.Fatalf("state not in flight: %v", status.State)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The payments should now show up in Alice's ListInvoices, with a zero
|
|
|
|
// preimage, indicating they are not yet settled.
|
|
|
|
err = lntest.WaitNoError(func() error {
|
2019-06-11 15:59:05 +03:00
|
|
|
req := &lnrpc.ListPaymentsRequest{
|
2019-06-14 02:16:18 +03:00
|
|
|
IncludeIncomplete: true,
|
2019-06-11 15:59:05 +03:00
|
|
|
}
|
2019-06-07 17:42:26 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
|
|
|
paymentsResp, err := net.Alice.ListPayments(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error when obtaining payments: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather the payment hashes we are looking for in the
|
|
|
|
// response.
|
|
|
|
payHashes := make(map[string]struct{})
|
|
|
|
for _, preimg := range preimages {
|
|
|
|
payHashes[preimg.Hash().String()] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var zeroPreimg lntypes.Preimage
|
|
|
|
for _, payment := range paymentsResp.Payments {
|
|
|
|
_, ok := payHashes[payment.PaymentHash]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// The preimage should NEVER be non-zero at this point.
|
|
|
|
if payment.PaymentPreimage != zeroPreimg.String() {
|
|
|
|
t.Fatalf("expected zero preimage, got %v",
|
|
|
|
payment.PaymentPreimage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We wait for the payment attempt to have been
|
|
|
|
// properly recorded in the DB.
|
|
|
|
if len(payment.Path) == 0 {
|
|
|
|
return fmt.Errorf("path is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(payHashes, payment.PaymentHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(payHashes) != 0 {
|
|
|
|
return fmt.Errorf("payhash not found in response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, time.Second*15)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("predicate not satisfied: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all invoices to be accepted.
|
|
|
|
for _, stream := range invoiceStreams {
|
|
|
|
invoice, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if invoice.State != lnrpc.Invoice_ACCEPTED {
|
|
|
|
t.Fatalf("expected ACCEPTED, got state: %v",
|
|
|
|
invoice.State)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restart alice. This to ensure she will still be able to handle
|
|
|
|
// settling the invoices after a restart.
|
|
|
|
if err := net.RestartNode(net.Alice, nil); err != nil {
|
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now after a restart, we must re-track the payments. We set up a
|
|
|
|
// goroutine for each to track thir status updates.
|
|
|
|
var (
|
|
|
|
statusUpdates []chan *routerrpc.PaymentStatus
|
|
|
|
wg sync.WaitGroup
|
|
|
|
quit = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
|
|
|
defer close(quit)
|
|
|
|
for _, preimg := range preimages {
|
|
|
|
hash := preimg.Hash()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
payStream, err := net.Alice.RouterClient.TrackPayment(
|
|
|
|
ctx, &routerrpc.TrackPaymentRequest{
|
|
|
|
PaymentHash: hash[:],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send track payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We set up a channel where we'll forward any status update.
|
|
|
|
upd := make(chan *routerrpc.PaymentStatus)
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
for {
|
|
|
|
status, err := payStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
close(upd)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case upd <- status:
|
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
statusUpdates = append(statusUpdates, upd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the infligt status update.
|
|
|
|
for _, upd := range statusUpdates {
|
|
|
|
select {
|
|
|
|
case status, ok := <-upd:
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("failed getting status update")
|
|
|
|
}
|
|
|
|
|
|
|
|
if status.State != routerrpc.PaymentState_IN_FLIGHT {
|
|
|
|
t.Fatalf("state not in in flight: %v",
|
|
|
|
status.State)
|
|
|
|
}
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("in flight status not recevied")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settle invoices half the invoices, cancel the rest.
|
|
|
|
for i, preimage := range preimages {
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if i%2 == 0 {
|
|
|
|
settle := &invoicesrpc.SettleInvoiceMsg{
|
|
|
|
Preimage: preimage[:],
|
|
|
|
}
|
|
|
|
_, err = carol.SettleInvoice(ctxt, settle)
|
|
|
|
} else {
|
|
|
|
hash := preimage.Hash()
|
|
|
|
settle := &invoicesrpc.CancelInvoiceMsg{
|
|
|
|
PaymentHash: hash[:],
|
|
|
|
}
|
|
|
|
_, err = carol.CancelInvoice(ctxt, settle)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to cancel/settle invoice: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we get the expected status update.
|
|
|
|
for i, upd := range statusUpdates {
|
|
|
|
select {
|
|
|
|
case status, ok := <-upd:
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("failed getting status update")
|
|
|
|
}
|
|
|
|
|
|
|
|
if i%2 == 0 {
|
|
|
|
if status.State != routerrpc.PaymentState_SUCCEEDED {
|
|
|
|
t.Fatalf("state not suceeded : %v",
|
|
|
|
status.State)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if status.State != routerrpc.PaymentState_FAILED_NO_ROUTE {
|
|
|
|
t.Fatalf("state not failed: %v",
|
|
|
|
status.State)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("in flight status not recevied")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that Alice's invoices to be shown as settled and failed
|
|
|
|
// accordingly, and preimages matching up.
|
2019-06-11 15:59:05 +03:00
|
|
|
req := &lnrpc.ListPaymentsRequest{
|
2019-06-14 02:16:18 +03:00
|
|
|
IncludeIncomplete: true,
|
2019-06-11 15:59:05 +03:00
|
|
|
}
|
2019-06-07 17:42:26 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
|
|
|
paymentsResp, err := net.Alice.ListPayments(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error when obtaining Alice payments: %v", err)
|
|
|
|
}
|
|
|
|
for i, preimage := range preimages {
|
|
|
|
paymentHash := preimage.Hash()
|
|
|
|
var p string
|
|
|
|
for _, resp := range paymentsResp.Payments {
|
|
|
|
if resp.PaymentHash == paymentHash.String() {
|
|
|
|
p = resp.PaymentPreimage
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p == "" {
|
|
|
|
t.Fatalf("payment not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if i%2 == 0 {
|
|
|
|
if p != preimage.String() {
|
|
|
|
t.Fatalf("preimage doesn't match: %v vs %v",
|
|
|
|
p, preimage.String())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if p != lntypes.ZeroHash.String() {
|
|
|
|
t.Fatalf("preimage not zero: %v", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 10:20:13 +03:00
|
|
|
// testTLSAutoRegeneration creates an expired TLS certificate, to test that a
|
|
|
|
// new TLS certificate pair is regenerated when the old pair expires. This is
|
|
|
|
// necessary because the pair expires after a little over a year.
|
|
|
|
func testTLSAutoRegeneration(lnNet *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
certPath := lnNet.Alice.TLSCertStr()
|
|
|
|
keyPath := lnNet.Alice.TLSKeyStr()
|
|
|
|
|
|
|
|
// Create an expired certificate.
|
|
|
|
expiredCert := genExpiredCertPair(
|
|
|
|
t, lnNet, certPath, keyPath,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Restart the node to test that the cert is automatically regenerated.
|
|
|
|
lnNet.RestartNode(lnNet.Alice, nil, nil)
|
|
|
|
|
|
|
|
// Grab the newly generated certificate.
|
|
|
|
newCertData, err := tls.LoadX509KeyPair(certPath, keyPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't grab new certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
newCert, err := x509.ParseCertificate(newCertData.Certificate[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't parse new certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the expired certificate was successfully deleted and
|
|
|
|
// replaced with a new one.
|
|
|
|
if !newCert.NotAfter.After(expiredCert.NotAfter) {
|
|
|
|
t.Fatalf("New certificate expiration is too old")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// genExpiredCertPair generates an expired key/cert pair to the paths
|
|
|
|
// provided to test that expired certificates are being regenerated correctly.
|
|
|
|
func genExpiredCertPair(t *harnessTest, lnNet *lntest.NetworkHarness, certPath,
|
|
|
|
keyPath string) *x509.Certificate {
|
|
|
|
// Max serial number.
|
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
|
|
|
|
|
|
// Generate a serial number that's below the serialNumberLimit.
|
|
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to generate serial number: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
host := "lightning"
|
|
|
|
|
|
|
|
// Create a simple ip address for the fake certificate.
|
|
|
|
ipAddresses := []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::1")}
|
|
|
|
|
|
|
|
dnsNames := []string{host, "unix", "unixpacket"}
|
|
|
|
|
|
|
|
// Construct the certificate template.
|
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: serialNumber,
|
|
|
|
Subject: pkix.Name{
|
|
|
|
Organization: []string{"lnd autogenerated cert"},
|
|
|
|
CommonName: host,
|
|
|
|
},
|
|
|
|
NotBefore: time.Now().Add(-time.Hour * 24),
|
|
|
|
NotAfter: time.Now(),
|
|
|
|
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment |
|
|
|
|
x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
|
|
|
|
IsCA: true, // so can sign self.
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
|
|
|
|
DNSNames: dnsNames,
|
|
|
|
IPAddresses: ipAddresses,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a private key for the certificate.
|
|
|
|
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to generate a private key")
|
|
|
|
}
|
|
|
|
|
|
|
|
derBytes, err := x509.CreateCertificate(
|
|
|
|
rand.Reader, &template, &template, &priv.PublicKey, priv)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create certificate: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expiredCert, err := x509.ParseCertificate(derBytes)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse certificate: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
certBuf := bytes.Buffer{}
|
|
|
|
err = pem.Encode(
|
|
|
|
&certBuf, &pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: derBytes,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to encode certificate: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keybytes, err := x509.MarshalECPrivateKey(priv)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to encode privkey: %v", err)
|
|
|
|
}
|
|
|
|
keyBuf := bytes.Buffer{}
|
|
|
|
err = pem.Encode(
|
|
|
|
&keyBuf, &pem.Block{
|
|
|
|
Type: "EC PRIVATE KEY",
|
|
|
|
Bytes: keybytes,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to encode private key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write cert and key files.
|
|
|
|
if err = ioutil.WriteFile(certPath, certBuf.Bytes(), 0644); err != nil {
|
|
|
|
t.Fatalf("failed to write cert file: %v", err)
|
|
|
|
}
|
|
|
|
if err = ioutil.WriteFile(keyPath, keyBuf.Bytes(), 0600); err != nil {
|
|
|
|
os.Remove(certPath)
|
|
|
|
t.Fatalf("failed to write key file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return expiredCert
|
|
|
|
}
|
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
test func(net *lntest.NetworkHarness, t *harnessTest)
|
|
|
|
}
|
|
|
|
|
|
|
|
var testsCases = []*testCase{
|
2018-12-25 01:05:48 +03:00
|
|
|
{
|
|
|
|
name: "sweep coins",
|
|
|
|
test: testSweepAllCoins,
|
|
|
|
},
|
2018-04-03 02:57:58 +03:00
|
|
|
{
|
|
|
|
name: "onchain fund recovery",
|
|
|
|
test: testOnchainFundRecovery,
|
|
|
|
},
|
2018-02-23 01:29:06 +03:00
|
|
|
{
|
|
|
|
name: "basic funding flow",
|
|
|
|
test: testBasicChannelFunding,
|
|
|
|
},
|
2018-08-10 07:14:31 +03:00
|
|
|
{
|
|
|
|
name: "unconfirmed channel funding",
|
|
|
|
test: testUnconfirmedChannelFunding,
|
|
|
|
},
|
2018-02-23 01:29:06 +03:00
|
|
|
{
|
|
|
|
name: "update channel policy",
|
|
|
|
test: testUpdateChannelPolicy,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "open channel reorg test",
|
|
|
|
test: testOpenChannelAfterReorg,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "disconnecting target peer",
|
|
|
|
test: testDisconnectingTargetPeer,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "graph topology notifications",
|
|
|
|
test: testGraphTopologyNotifications,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "funding flow persistence",
|
|
|
|
test: testChannelFundingPersistence,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "channel force closure",
|
|
|
|
test: testChannelForceClosure,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "channel balance",
|
|
|
|
test: testChannelBalance,
|
|
|
|
},
|
2018-12-07 03:49:05 +03:00
|
|
|
{
|
|
|
|
name: "channel unsettled balance",
|
|
|
|
test: testChannelUnsettledBalance,
|
|
|
|
},
|
2018-02-23 01:29:06 +03:00
|
|
|
{
|
|
|
|
name: "single hop invoice",
|
|
|
|
test: testSingleHopInvoice,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sphinx replay persistence",
|
|
|
|
test: testSphinxReplayPersistence,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "list outgoing payments",
|
|
|
|
test: testListPayments,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "max pending channel",
|
|
|
|
test: testMaxPendingChannels,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi-hop payments",
|
|
|
|
test: testMultiHopPayments,
|
|
|
|
},
|
2018-02-03 13:21:16 +03:00
|
|
|
{
|
|
|
|
name: "single-hop send to route",
|
|
|
|
test: testSingleHopSendToRoute,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi-hop send to route",
|
|
|
|
test: testMultiHopSendToRoute,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "send to route error propagation",
|
|
|
|
test: testSendToRouteErrorPropagation,
|
|
|
|
},
|
2018-09-26 18:30:09 +03:00
|
|
|
{
|
|
|
|
name: "unannounced channels",
|
|
|
|
test: testUnannouncedChannels,
|
|
|
|
},
|
2018-02-23 01:29:06 +03:00
|
|
|
{
|
|
|
|
name: "private channels",
|
|
|
|
test: testPrivateChannels,
|
|
|
|
},
|
2018-04-21 18:37:38 +03:00
|
|
|
{
|
|
|
|
name: "invoice routing hints",
|
|
|
|
test: testInvoiceRoutingHints,
|
|
|
|
},
|
2018-03-15 20:02:52 +03:00
|
|
|
{
|
|
|
|
name: "multi-hop payments over private channels",
|
|
|
|
test: testMultiHopOverPrivateChannels,
|
|
|
|
},
|
2018-02-23 01:29:06 +03:00
|
|
|
{
|
2018-11-02 06:20:21 +03:00
|
|
|
name: "multiple channel creation and update subscription",
|
|
|
|
test: testBasicChannelCreationAndUpdates,
|
2018-02-23 01:29:06 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invoice update subscription",
|
|
|
|
test: testInvoiceSubscriptions,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi-hop htlc error propagation",
|
|
|
|
test: testHtlcErrorPropagation,
|
|
|
|
},
|
|
|
|
// TODO(roasbeef): multi-path integration test
|
|
|
|
{
|
|
|
|
name: "node announcement",
|
|
|
|
test: testNodeAnnouncement,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "node sign verify",
|
|
|
|
test: testNodeSignVerify,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "async payments benchmark",
|
|
|
|
test: testAsyncPayments,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "async bidirectional payments",
|
|
|
|
test: testBidirectionalAsyncPayments,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// bob: outgoing our commit timeout
|
|
|
|
// carol: incoming their commit watch and see timeout
|
|
|
|
name: "test multi-hop htlc local force close immediate expiry",
|
|
|
|
test: testMultiHopHtlcLocalTimeout,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// bob: outgoing watch and see, they sweep on chain
|
|
|
|
// carol: incoming our commit, know preimage
|
|
|
|
name: "test multi-hop htlc receiver chain claim",
|
|
|
|
test: testMultiHopReceiverChainClaim,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// bob: outgoing our commit watch and see timeout
|
|
|
|
// carol: incoming their commit watch and see timeout
|
|
|
|
name: "test multi-hop local force close on-chain htlc timeout",
|
|
|
|
test: testMultiHopLocalForceCloseOnChainHtlcTimeout,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// bob: outgoing their commit watch and see timeout
|
|
|
|
// carol: incoming our commit watch and see timeout
|
|
|
|
name: "test multi-hop remote force close on-chain htlc timeout",
|
2018-05-25 05:40:56 +03:00
|
|
|
test: testMultiHopRemoteForceCloseOnChainHtlcTimeout,
|
2018-02-23 01:29:06 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// bob: outgoing our commit watch and see, they sweep on chain
|
|
|
|
// bob: incoming our commit watch and learn preimage
|
|
|
|
// carol: incoming their commit know preimage
|
|
|
|
name: "test multi-hop htlc local chain claim",
|
|
|
|
test: testMultiHopHtlcLocalChainClaim,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// bob: outgoing their commit watch and see, they sweep on chain
|
|
|
|
// bob: incoming their commit watch and learn preimage
|
|
|
|
// carol: incoming our commit know preimage
|
|
|
|
name: "test multi-hop htlc remote chain claim",
|
|
|
|
test: testMultiHopHtlcRemoteChainClaim,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "switch circuit persistence",
|
|
|
|
test: testSwitchCircuitPersistence,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "switch offline delivery",
|
|
|
|
test: testSwitchOfflineDelivery,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "switch offline delivery persistence",
|
|
|
|
test: testSwitchOfflineDeliveryPersistence,
|
2017-08-01 09:17:13 +03:00
|
|
|
},
|
2018-02-24 03:45:50 +03:00
|
|
|
{
|
|
|
|
name: "switch offline delivery outgoing offline",
|
|
|
|
test: testSwitchOfflineDeliveryOutgoingOffline,
|
|
|
|
},
|
2018-03-13 00:46:29 +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,
|
|
|
|
},
|
2018-05-18 15:04:05 +03:00
|
|
|
{
|
|
|
|
name: "failing link",
|
|
|
|
test: testFailingChannel,
|
|
|
|
},
|
2018-06-15 22:33:49 +03:00
|
|
|
{
|
|
|
|
name: "garbage collect link nodes",
|
|
|
|
test: testGarbageCollectLinkNodes,
|
|
|
|
},
|
2018-06-20 10:09:02 +03:00
|
|
|
{
|
|
|
|
name: "abandonchannel",
|
|
|
|
test: testAbandonChannel,
|
|
|
|
},
|
2018-03-13 00:46:29 +03:00
|
|
|
{
|
|
|
|
name: "revoked uncooperative close retribution zero value remote output",
|
|
|
|
test: testRevokedCloseRetributionZeroValueRemoteOutput,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "revoked uncooperative close retribution remote hodl",
|
|
|
|
test: testRevokedCloseRetributionRemoteHodl,
|
|
|
|
},
|
2019-06-14 03:32:29 +03:00
|
|
|
{
|
|
|
|
name: "revoked uncooperative close retribution altruist watchtower",
|
|
|
|
test: testRevokedCloseRetributionAltruistWatchtower,
|
|
|
|
},
|
2018-07-12 12:02:54 +03:00
|
|
|
{
|
|
|
|
name: "data loss protection",
|
|
|
|
test: testDataLossProtection,
|
|
|
|
},
|
2018-04-11 12:18:58 +03:00
|
|
|
{
|
|
|
|
name: "query routes",
|
|
|
|
test: testQueryRoutes,
|
|
|
|
},
|
2018-04-30 23:56:02 +03:00
|
|
|
{
|
|
|
|
name: "route fee cutoff",
|
|
|
|
test: testRouteFeeCutoff,
|
|
|
|
},
|
2018-06-14 05:36:08 +03:00
|
|
|
{
|
|
|
|
name: "send update disable channel",
|
|
|
|
test: testSendUpdateDisableChannel,
|
|
|
|
},
|
2019-03-12 03:38:38 +03:00
|
|
|
{
|
|
|
|
name: "streaming channel backup update",
|
|
|
|
test: testChannelBackupUpdates,
|
|
|
|
},
|
2019-03-12 03:39:00 +03:00
|
|
|
{
|
|
|
|
name: "export channel backup",
|
|
|
|
test: testExportChannelBackup,
|
|
|
|
},
|
|
|
|
{
|
2019-03-12 03:41:01 +03:00
|
|
|
name: "channel backup restore",
|
|
|
|
test: testChannelBackupRestore,
|
|
|
|
},
|
2019-06-07 17:42:26 +03:00
|
|
|
{
|
|
|
|
name: "hold invoice sender persistence",
|
|
|
|
test: testHoldInvoicePersistence,
|
|
|
|
},
|
2019-06-04 02:12:20 +03:00
|
|
|
{
|
|
|
|
name: "cpfp",
|
|
|
|
test: testCPFP,
|
|
|
|
},
|
2019-04-25 10:20:13 +03:00
|
|
|
{
|
|
|
|
name: "automatic certificate regeneration",
|
|
|
|
test: testTLSAutoRegeneration,
|
|
|
|
},
|
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
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// Declare the network harness here to gain access to its
|
|
|
|
// 'OnTxAccepted' call back.
|
2017-11-17 04:31:41 +03:00
|
|
|
var lndHarness *lntest.NetworkHarness
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// Create an instance of the btcd's rpctest.Harness that will act as
|
|
|
|
// the miner for all tests. This will be used to fund the wallets of
|
|
|
|
// the nodes within the test network and to 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.
|
|
|
|
//
|
|
|
|
// We will also connect it to our chain backend.
|
|
|
|
minerLogDir := "./.minerlogs"
|
2018-10-10 14:04:13 +03:00
|
|
|
args := []string{
|
|
|
|
"--rejectnonstd",
|
|
|
|
"--txindex",
|
|
|
|
"--debuglevel=debug",
|
2018-12-14 11:24:00 +03:00
|
|
|
"--logdir=" + minerLogDir,
|
|
|
|
"--trickleinterval=100ms",
|
2018-10-10 14:04:13 +03:00
|
|
|
}
|
2017-11-17 04:31:41 +03:00
|
|
|
handlers := &rpcclient.NotificationHandlers{
|
|
|
|
OnTxAccepted: func(hash *chainhash.Hash, amt btcutil.Amount) {
|
|
|
|
lndHarness.OnTxAccepted(hash)
|
|
|
|
},
|
|
|
|
}
|
2018-12-14 11:24:00 +03:00
|
|
|
|
|
|
|
miner, err := rpctest.New(harnessNetParams, handlers, args)
|
2017-11-17 04:31:41 +03:00
|
|
|
if err != nil {
|
|
|
|
ht.Fatalf("unable to create mining node: %v", err)
|
|
|
|
}
|
2018-10-10 14:04:13 +03:00
|
|
|
defer func() {
|
2018-12-14 11:24:00 +03:00
|
|
|
miner.TearDown()
|
2018-10-10 14:04:13 +03:00
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// After shutting down the miner, we'll make a copy of the log
|
|
|
|
// file before deleting the temporary log dir.
|
|
|
|
logFile := fmt.Sprintf(
|
|
|
|
"%s/%s/btcd.log", minerLogDir, harnessNetParams.Name,
|
|
|
|
)
|
|
|
|
err := lntest.CopyFile("./output_btcd_miner.log", logFile)
|
2018-10-10 14:04:13 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("unable to copy file: %v\n", err)
|
|
|
|
}
|
2018-12-14 11:24:00 +03:00
|
|
|
if err = os.RemoveAll(minerLogDir); err != nil {
|
|
|
|
fmt.Printf("Cannot remove dir %s: %v\n",
|
|
|
|
minerLogDir, err)
|
2018-10-10 14:04:13 +03:00
|
|
|
}
|
|
|
|
}()
|
2017-11-17 04:31:41 +03:00
|
|
|
|
2019-05-24 15:17:48 +03:00
|
|
|
// Start a chain backend.
|
|
|
|
chainBackend, cleanUp, err := lntest.NewBackend(miner.P2PAddress())
|
2019-05-24 15:17:48 +03:00
|
|
|
if err != nil {
|
2019-05-24 15:17:48 +03:00
|
|
|
ht.Fatalf("unable to start backend: %v", err)
|
2019-05-24 15:17:48 +03:00
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
if err := miner.SetUp(true, 50); err != nil {
|
|
|
|
ht.Fatalf("unable to set up mining node: %v", err)
|
|
|
|
}
|
|
|
|
if err := miner.Node.NotifyNewTransactions(false); err != nil {
|
|
|
|
ht.Fatalf("unable to request transaction notifications: %v", err)
|
2018-12-14 11:24:00 +03:00
|
|
|
}
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// Now we can set up our test harness (LND instance), with the chain
|
|
|
|
// backend we just created.
|
|
|
|
lndHarness, err = lntest.NewNetworkHarness(miner, chainBackend)
|
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-11-17 04:31:41 +03:00
|
|
|
defer lndHarness.TearDownAll()
|
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
|
2017-10-25 05:35:54 +03:00
|
|
|
// case should naturally as a result and we log the server error here to
|
|
|
|
// help debug.
|
2016-11-24 11:49:18 +03:00
|
|
|
go func() {
|
2017-10-25 05:35:54 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err, more := <-lndHarness.ProcessErrors():
|
|
|
|
if !more {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ht.Logf("lnd finished with error (stderr):\n%v", err)
|
|
|
|
}
|
2016-11-24 11:49:18 +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
|
2018-12-14 11:24:00 +03:00
|
|
|
if _, err := miner.Node.Generate(numBlocks); err != nil {
|
2017-01-06 00:17:49 +03:00
|
|
|
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?
|
2017-11-17 04:31:41 +03:00
|
|
|
if err = lndHarness.SetUp(nil); 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 {
|
2018-01-09 14:59:32 +03:00
|
|
|
logLine := fmt.Sprintf("STARTING ============ %v ============\n",
|
|
|
|
testCase.name)
|
2018-06-15 22:35:12 +03:00
|
|
|
|
|
|
|
err := lndHarness.EnsureConnected(
|
|
|
|
context.Background(), lndHarness.Alice, lndHarness.Bob,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to connect alice to bob: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-01-09 14:59:32 +03:00
|
|
|
if err := lndHarness.Alice.AddToLog(logLine); err != nil {
|
|
|
|
t.Fatalf("unable to add to log: %v", err)
|
|
|
|
}
|
|
|
|
if err := lndHarness.Bob.AddToLog(logLine); err != nil {
|
|
|
|
t.Fatalf("unable to add to log: %v", err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|