2019-05-15 10:02:53 +03:00
|
|
|
package itest
|
2016-08-30 08:07:54 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-09-29 01:43:42 +03:00
|
|
|
"context"
|
2019-01-23 07:45:38 +03:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2020-11-04 13:03:29 +03:00
|
|
|
"flag"
|
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"
|
2016-11-22 06:16:44 +03:00
|
|
|
"os"
|
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
|
|
|
|
2020-09-04 12:19:27 +03:00
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
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/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/integration/rpctest"
|
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2020-05-25 09:38:25 +03:00
|
|
|
"github.com/btcsuite/btcwallet/wallet"
|
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"
|
2020-10-06 21:56:10 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainreg"
|
2019-12-05 00:31:03 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2020-11-17 02:28:56 +03:00
|
|
|
"github.com/lightningnetwork/lnd/funding"
|
2019-12-11 07:02:49 +03:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2020-07-29 10:27:22 +03:00
|
|
|
"github.com/lightningnetwork/lnd/labels"
|
2020-05-13 16:47:45 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lncfg"
|
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"
|
2019-12-11 07:02:49 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
|
2020-05-05 22:46:26 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
|
2019-06-21 02:55:52 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/watchtowerrpc"
|
2019-07-04 05:54:28 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/wtclientrpc"
|
2018-06-15 22:33:49 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntest"
|
2019-09-19 22:46:29 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
2019-06-07 17:42:26 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2020-06-10 15:21:13 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2020-03-27 09:55:13 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2018-06-15 22:33:49 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-07-24 04:00:30 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing"
|
2020-07-07 11:32:13 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-08-30 08:07:54 +03:00
|
|
|
)
|
|
|
|
|
2020-11-04 13:03:29 +03:00
|
|
|
const (
|
|
|
|
// defaultSplitTranches is the default number of tranches we split the
|
|
|
|
// test cases into.
|
|
|
|
defaultSplitTranches uint = 1
|
|
|
|
|
|
|
|
// defaultRunTranche is the default index of the test cases tranche that
|
|
|
|
// we run.
|
|
|
|
defaultRunTranche uint = 0
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// testCasesSplitParts is the number of tranches the test cases should
|
|
|
|
// be split into. By default this is set to 1, so no splitting happens.
|
|
|
|
// If this value is increased, then the -runtranche flag must be
|
|
|
|
// specified as well to indicate which part should be run in the current
|
|
|
|
// invocation.
|
|
|
|
testCasesSplitTranches = flag.Uint(
|
|
|
|
"splittranches", defaultSplitTranches, "split the test cases "+
|
|
|
|
"in this many tranches and run the tranche at "+
|
|
|
|
"0-based index specified by the -runtranche flag",
|
|
|
|
)
|
|
|
|
|
|
|
|
// testCasesRunTranche is the 0-based index of the split test cases
|
|
|
|
// tranche to run in the current invocation.
|
|
|
|
testCasesRunTranche = flag.Uint(
|
|
|
|
"runtranche", defaultRunTranche, "run the tranche of the "+
|
|
|
|
"split test cases with the given (0-based) index",
|
|
|
|
)
|
2020-06-22 20:36:12 +03:00
|
|
|
|
|
|
|
// useEtcd test LND nodes use (embedded) etcd as remote db.
|
|
|
|
useEtcd = flag.Bool("etcd", false, "Use etcd backend for lnd.")
|
2020-11-04 13:03:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// getTestCaseSplitTranche returns the sub slice of the test cases that should
|
|
|
|
// be run as the current split tranche as well as the index and slice offset of
|
|
|
|
// the tranche.
|
|
|
|
func getTestCaseSplitTranche() ([]*testCase, uint, uint) {
|
|
|
|
numTranches := defaultSplitTranches
|
|
|
|
if testCasesSplitTranches != nil {
|
|
|
|
numTranches = *testCasesSplitTranches
|
|
|
|
}
|
|
|
|
runTranche := defaultRunTranche
|
|
|
|
if testCasesRunTranche != nil {
|
|
|
|
runTranche = *testCasesRunTranche
|
|
|
|
}
|
|
|
|
|
2020-11-07 15:23:30 +03:00
|
|
|
// There's a special flake-hunt mode where we run the same test multiple
|
|
|
|
// times in parallel. In that case the tranche index is equal to the
|
|
|
|
// thread ID, but we need to actually run all tests for the regex
|
|
|
|
// selection to work.
|
|
|
|
threadID := runTranche
|
|
|
|
if numTranches == 1 {
|
|
|
|
runTranche = 0
|
|
|
|
}
|
|
|
|
|
2020-11-04 13:03:29 +03:00
|
|
|
numCases := uint(len(allTestCases))
|
|
|
|
testsPerTranche := numCases / numTranches
|
|
|
|
trancheOffset := runTranche * testsPerTranche
|
|
|
|
trancheEnd := trancheOffset + testsPerTranche
|
|
|
|
if trancheEnd > numCases || runTranche == numTranches-1 {
|
|
|
|
trancheEnd = numCases
|
|
|
|
}
|
|
|
|
|
2020-11-07 15:23:30 +03:00
|
|
|
return allTestCases[trancheOffset:trancheEnd], threadID, trancheOffset
|
2020-11-04 13:03:29 +03:00
|
|
|
}
|
|
|
|
|
2019-03-12 02:24:45 +03:00
|
|
|
func rpcPointToWirePoint(t *harnessTest, chanPoint *lnrpc.ChannelPoint) wire.OutPoint {
|
2021-02-13 11:05:33 +03:00
|
|
|
txid, err := lnrpc.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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 00:29:50 +03:00
|
|
|
// openChannelStream blocks until an OpenChannel request for a channel funding
|
|
|
|
// by alice succeeds. If it does, a stream client is returned to receive events
|
|
|
|
// about the opening channel.
|
|
|
|
func openChannelStream(ctx context.Context, t *harnessTest,
|
2017-11-03 21:52:02 +03:00
|
|
|
net *lntest.NetworkHarness, alice, bob *lntest.HarnessNode,
|
2020-04-03 00:29:50 +03:00
|
|
|
p lntest.OpenChannelParams) lnrpc.Lightning_OpenChannelClient {
|
|
|
|
|
|
|
|
t.t.Helper()
|
2016-09-18 03:34:39 +03:00
|
|
|
|
2020-03-31 02:55:16 +03:00
|
|
|
// Wait until we are able to fund a channel successfully. This wait
|
|
|
|
// prevents us from erroring out when trying to create a channel while
|
|
|
|
// the node is starting up.
|
|
|
|
var chanOpenUpdate lnrpc.Lightning_OpenChannelClient
|
|
|
|
err := wait.NoError(func() error {
|
|
|
|
var err error
|
|
|
|
chanOpenUpdate, err = net.OpenChannel(ctx, alice, bob, p)
|
|
|
|
return err
|
|
|
|
}, defaultTimeout)
|
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
|
|
|
|
2020-04-03 00:29:50 +03:00
|
|
|
return chanOpenUpdate
|
|
|
|
}
|
|
|
|
|
|
|
|
// openChannelAndAssert attempts to open a channel with the specified
|
|
|
|
// parameters extended from Alice to Bob. Additionally, two items are asserted
|
|
|
|
// after the channel is considered open: the funding transaction should be
|
|
|
|
// found within a block, and that Alice can report the status of the new
|
|
|
|
// channel.
|
|
|
|
func openChannelAndAssert(ctx context.Context, t *harnessTest,
|
|
|
|
net *lntest.NetworkHarness, alice, bob *lntest.HarnessNode,
|
|
|
|
p lntest.OpenChannelParams) *lnrpc.ChannelPoint {
|
|
|
|
|
|
|
|
t.t.Helper()
|
|
|
|
|
|
|
|
chanOpenUpdate := openChannelStream(ctx, t, net, alice, bob, p)
|
|
|
|
|
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
|
|
|
}
|
2021-02-13 11:05:33 +03:00
|
|
|
fundingTxID, err := lnrpc.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
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
return closeChannelAndAssertType(ctx, t, net, node, fundingChanPoint, false, force)
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeChannelAndAssertType(ctx context.Context, t *harnessTest,
|
|
|
|
net *lntest.NetworkHarness, node *lntest.HarnessNode,
|
|
|
|
fundingChanPoint *lnrpc.ChannelPoint, anchors, force bool) *chainhash.Hash {
|
|
|
|
|
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},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
return assertChannelClosed(
|
|
|
|
ctx, t, net, node, fundingChanPoint, anchors, closeUpdates,
|
|
|
|
)
|
2019-03-13 03:34:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
return assertChannelClosed(
|
|
|
|
ctx, t, net, node, fundingChanPoint, false, closeUpdates,
|
|
|
|
)
|
2019-03-13 03:34:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2020-03-04 15:21:29 +03:00
|
|
|
fundingChanPoint *lnrpc.ChannelPoint, anchors bool,
|
2019-03-13 03:34:42 +03:00
|
|
|
closeUpdates lnrpc.Lightning_CloseChannelClient) *chainhash.Hash {
|
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
txid, err := lnrpc.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)
|
|
|
|
|
2019-12-05 00:31:03 +03:00
|
|
|
// If the channel appears in list channels, ensure that its state
|
|
|
|
// contains ChanStatusCoopBroadcasted.
|
|
|
|
ctxt, _ := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
listChansRequest := &lnrpc.ListChannelsRequest{}
|
|
|
|
listChansResp, err := node.ListChannels(ctxt, listChansRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for list channels: %v", err)
|
|
|
|
}
|
|
|
|
for _, channel := range listChansResp.Channels {
|
|
|
|
// Skip other channels.
|
|
|
|
if channel.ChannelPoint != chanPointStr {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that the channel is in coop broadcasted.
|
|
|
|
if !strings.Contains(channel.ChanStatusFlags,
|
|
|
|
channeldb.ChanStatusCoopBroadcasted.String()) {
|
|
|
|
t.Fatalf("channel not coop broadcasted, "+
|
|
|
|
"got: %v", channel.ChanStatusFlags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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".
|
2019-12-05 00:31:03 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctx, defaultTimeout)
|
2018-07-18 14:37:53 +03:00
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
2019-12-05 00:31:03 +03:00
|
|
|
pendingChanResp, err := node.PendingChannels(ctxt, pendingChansRequest)
|
2018-07-18 14:37:53 +03:00
|
|
|
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
|
2020-03-04 15:21:29 +03:00
|
|
|
// block. If there are anchors, we also expect an anchor sweep.
|
|
|
|
expectedTxes := 1
|
|
|
|
if anchors {
|
|
|
|
expectedTxes = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
block := mineBlocks(t, net, 1, expectedTxes)[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.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-07-18 14:37:53 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-07-18 14:37:53 +03:00
|
|
|
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 {
|
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
txid, err := lnrpc.GetChanPointFundingTxid(fundingChanPoint)
|
2018-11-06 13:03:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
op := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: fundingChanPoint.OutputIndex,
|
|
|
|
}
|
|
|
|
|
2020-10-26 16:21:09 +03:00
|
|
|
return wait.NoError(func() error {
|
2018-11-06 13:03:01 +03:00
|
|
|
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
|
|
|
pendingChanResp, err := node.PendingChannels(
|
|
|
|
ctx, pendingChansRequest,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2020-10-26 16:21:09 +03:00
|
|
|
return fmt.Errorf("unable to get pending channels: %v",
|
|
|
|
err)
|
2018-11-06 13:03:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
forceClose, err := findForceClosedChannel(pendingChanResp, &op)
|
|
|
|
if err != nil {
|
2020-10-26 16:21:09 +03:00
|
|
|
return err
|
2018-11-06 13:03:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We must wait until the UTXO nursery has received the channel
|
|
|
|
// and is aware of its maturity height.
|
|
|
|
if forceClose.MaturityHeight == 0 {
|
2020-10-26 16:21:09 +03:00
|
|
|
return fmt.Errorf("channel had maturity height of 0")
|
2018-11-06 13:03:01 +03:00
|
|
|
}
|
|
|
|
|
2020-10-26 16:21:09 +03:00
|
|
|
return nil
|
|
|
|
}, defaultTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lnrpcForceCloseChannel is a short type alias for a ridiculously long type
|
|
|
|
// name in the lnrpc package.
|
|
|
|
type lnrpcForceCloseChannel = lnrpc.PendingChannelsResponse_ForceClosedChannel
|
|
|
|
|
|
|
|
// waitForNumChannelPendingForceClose waits for the node to report a certain
|
|
|
|
// number of channels in state pending force close.
|
|
|
|
func waitForNumChannelPendingForceClose(ctx context.Context,
|
|
|
|
node *lntest.HarnessNode, expectedNum int,
|
|
|
|
perChanCheck func(channel *lnrpcForceCloseChannel) error) error {
|
|
|
|
|
|
|
|
return wait.NoError(func() error {
|
|
|
|
resp, err := node.PendingChannels(
|
|
|
|
ctx, &lnrpc.PendingChannelsRequest{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get pending channels: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
forceCloseChans := resp.PendingForceClosingChannels
|
|
|
|
if len(forceCloseChans) != expectedNum {
|
|
|
|
return fmt.Errorf("bob should have %d pending "+
|
|
|
|
"force close channels but has %d", expectedNum,
|
|
|
|
len(forceCloseChans))
|
|
|
|
}
|
|
|
|
|
|
|
|
if perChanCheck != nil {
|
|
|
|
for _, forceCloseChan := range forceCloseChans {
|
|
|
|
err := perChanCheck(forceCloseChan)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, defaultTimeout)
|
2018-11-06 13:03:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2020-03-09 20:43:47 +03:00
|
|
|
//
|
|
|
|
// The commit sweep resolver is able to broadcast the sweep tx up to
|
|
|
|
// one block before the CSV elapses, so wait until defaulCSV-1.
|
|
|
|
_, err = net.Miner.Node.Generate(defaultCSV - 1)
|
2018-11-06 13:03:01 +03:00
|
|
|
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-09-19 22:46:29 +03:00
|
|
|
err := wait.NoError(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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2019-02-06 23:47:40 +03:00
|
|
|
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-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,
|
2020-03-31 13:44:18 +03:00
|
|
|
routerClient routerrpc.RouterClient, paymentRequests []string,
|
|
|
|
awaitResponse bool) error {
|
2017-10-26 01:32:00 +03:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
// send sends a payment and returns an error if it doesn't succeeded.
|
|
|
|
send := func(payReq string) error {
|
|
|
|
ctxc, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
2017-10-26 01:32:00 +03:00
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
payStream, err := routerClient.SendPaymentV2(
|
|
|
|
ctxc,
|
|
|
|
&routerrpc.SendPaymentRequest{
|
|
|
|
PaymentRequest: payReq,
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-03-12 02:24:45 +03:00
|
|
|
}
|
2020-03-31 13:44:18 +03:00
|
|
|
|
|
|
|
resp, err := getPaymentResult(payStream)
|
2017-10-26 01:32:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-31 13:44:18 +03:00
|
|
|
if resp.Status != lnrpc.Payment_SUCCEEDED {
|
|
|
|
return errors.New(resp.FailureReason)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch all payments simultaneously.
|
|
|
|
results := make(chan error)
|
|
|
|
for _, payReq := range paymentRequests {
|
|
|
|
payReqCopy := payReq
|
|
|
|
go func() {
|
|
|
|
err := send(payReqCopy)
|
|
|
|
if awaitResponse {
|
|
|
|
results <- err
|
|
|
|
}
|
|
|
|
}()
|
2017-10-26 01:32:00 +03:00
|
|
|
}
|
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
// If awaiting a response, verify that all payments succeeded.
|
2017-10-26 01:32:00 +03:00
|
|
|
if awaitResponse {
|
|
|
|
for range paymentRequests {
|
2020-03-31 13:44:18 +03:00
|
|
|
err := <-results
|
2017-10-26 01:32:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
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.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2019-04-15 16:00:51 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
newListResp, err := client.ListChannels(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-10-16 22:11:12 +03:00
|
|
|
// If the number of open channels is now lower than before
|
|
|
|
// attempting the payments, it means one of the payments
|
|
|
|
// triggered a force closure (for example, due to an incorrect
|
|
|
|
// preimage). Return early since it's clear the payment was
|
|
|
|
// attempted.
|
|
|
|
if len(newListResp.Channels) < len(listResp.Channels) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-04-15 16:00:51 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2019-04-15 16:00:51 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:05:32 +03:00
|
|
|
// Set the payment address in the invoice so the caller can
|
|
|
|
// properly use it.
|
|
|
|
invoice.PaymentAddr = resp.PaymentAddr
|
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-09 09:47:17 +03:00
|
|
|
// testGetRecoveryInfo checks whether lnd gives the right information about
|
|
|
|
// the wallet recovery process.
|
|
|
|
func testGetRecoveryInfo(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")
|
2020-10-06 18:23:40 +03:00
|
|
|
carol, mnemonic, _, err := net.NewNodeWithSeed(
|
|
|
|
"Carol", nil, password, false,
|
|
|
|
)
|
2020-06-09 09:47:17 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create node with seed; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
shutdownAndAssert(net, t, carol)
|
|
|
|
|
|
|
|
checkInfo := func(expectedRecoveryMode, expectedRecoveryFinished bool,
|
|
|
|
expectedProgress float64, recoveryWindow int32) {
|
|
|
|
|
|
|
|
// Restore Carol, passing in the password, mnemonic, and
|
|
|
|
// desired recovery window.
|
|
|
|
node, err := net.RestoreNodeWithSeed(
|
|
|
|
"Carol", nil, password, mnemonic, recoveryWindow, nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to restore node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Carol to sync to the chain.
|
|
|
|
_, minerHeight, err := net.Miner.Node.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get current blockheight %v", err)
|
|
|
|
}
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = waitForNodeBlockHeight(ctxt, node, minerHeight)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to sync to chain: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query carol for her current wallet recovery progress.
|
|
|
|
var (
|
|
|
|
recoveryMode bool
|
|
|
|
recoveryFinished bool
|
|
|
|
progress float64
|
|
|
|
)
|
|
|
|
|
|
|
|
err = wait.Predicate(func() bool {
|
|
|
|
// Verify that recovery info gives the right response.
|
|
|
|
req := &lnrpc.GetRecoveryInfoRequest{}
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := node.GetRecoveryInfo(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query recovery info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
recoveryMode = resp.RecoveryMode
|
|
|
|
recoveryFinished = resp.RecoveryFinished
|
|
|
|
progress = resp.Progress
|
|
|
|
|
|
|
|
if recoveryMode != expectedRecoveryMode ||
|
|
|
|
recoveryFinished != expectedRecoveryFinished ||
|
|
|
|
progress != expectedProgress {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2020-06-09 09:47:17 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected recovery mode to be %v, got %v, "+
|
|
|
|
"expected recovery finished to be %v, got %v, "+
|
|
|
|
"expected progress %v, got %v",
|
|
|
|
expectedRecoveryMode, recoveryMode,
|
|
|
|
expectedRecoveryFinished, recoveryFinished,
|
|
|
|
expectedProgress, progress,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lastly, shutdown this Carol so we can move on to the next
|
|
|
|
// restoration.
|
|
|
|
shutdownAndAssert(net, t, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore Carol with a recovery window of 0. Since it's not in recovery
|
|
|
|
// mode, the recovery info will give a response with recoveryMode=false,
|
|
|
|
// recoveryFinished=false, and progress=0
|
|
|
|
checkInfo(false, false, 0, 0)
|
|
|
|
|
|
|
|
// Change the recovery windown to be 1 to turn on recovery mode. Since the
|
|
|
|
// current chain height is the same as the birthday height, it should
|
|
|
|
// indicate the recovery process is finished.
|
|
|
|
checkInfo(true, true, 1, 1)
|
|
|
|
|
|
|
|
// We now go ahead 5 blocks. Because the wallet's syncing process is
|
|
|
|
// controlled by a goroutine in the background, it will catch up quickly.
|
|
|
|
// This makes the recovery progress back to 1.
|
|
|
|
mineBlocks(t, net, 5, 0)
|
|
|
|
checkInfo(true, true, 1, 1)
|
|
|
|
}
|
|
|
|
|
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")
|
2020-10-06 18:23:40 +03:00
|
|
|
carol, mnemonic, _, err := net.NewNodeWithSeed(
|
|
|
|
"Carol", nil, password, false,
|
|
|
|
)
|
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.
|
2020-03-31 19:36:37 +03:00
|
|
|
restoreCheckBalance := func(expAmount int64, expectedNumUTXOs uint32,
|
2019-02-12 00:02:00 +03:00
|
|
|
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
|
|
|
|
)
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-04-03 02:57:58 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
currBalance = resp.ConfirmedBalance
|
|
|
|
|
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)
|
|
|
|
}
|
2020-03-31 19:36:37 +03:00
|
|
|
currNumUTXOs = uint32(len(utxoResp.Utxos))
|
2019-02-12 00:02:00 +03:00
|
|
|
|
2020-03-31 19:36:37 +03:00
|
|
|
// Verify that Carol's balance and number of UTXOs
|
|
|
|
// matches what's expected.
|
|
|
|
if expAmount != currBalance {
|
|
|
|
return false
|
|
|
|
}
|
2019-02-12 00:02:00 +03:00
|
|
|
if currNumUTXOs != expectedNumUTXOs {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-04-03 02:57:58 +03:00
|
|
|
return true
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-04-03 02:57:58 +03:00
|
|
|
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.
|
2020-03-31 19:36:37 +03:00
|
|
|
|
|
|
|
// After these checks are done, we'll want to make sure we can also
|
|
|
|
// recover change address outputs. This is mainly motivated by a now
|
|
|
|
// fixed bug in the wallet in which change addresses could at times be
|
|
|
|
// created outside of the default key scopes. Recovery only used to be
|
|
|
|
// performed on the default key scopes, so ideally this test case
|
|
|
|
// would've caught the bug earlier. Carol has received 6 BTC so far from
|
|
|
|
// the miner, we'll send 5 back to ensure all of her UTXOs get spent to
|
|
|
|
// avoid fee discrepancies and a change output is formed.
|
|
|
|
const minerAmt = 5 * btcutil.SatoshiPerBitcoin
|
|
|
|
const finalBalance = 6 * btcutil.SatoshiPerBitcoin
|
|
|
|
promptChangeAddr := func(node *lntest.HarnessNode) {
|
|
|
|
minerAddr, err := net.Miner.NewAddress()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new miner address: %v", err)
|
|
|
|
}
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := node.SendCoins(ctxt, &lnrpc.SendCoinsRequest{
|
|
|
|
Addr: minerAddr.String(),
|
|
|
|
Amount: minerAmt,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to miner: %v", err)
|
|
|
|
}
|
|
|
|
txid, err := waitForTxInMempool(
|
|
|
|
net.Miner.Node, minerMempoolTimeout,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("transaction not found in mempool: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Txid != txid.String() {
|
|
|
|
t.Fatalf("txid mismatch: %v vs %v", resp.Txid,
|
|
|
|
txid.String())
|
|
|
|
}
|
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
|
|
|
assertTxInBlock(t, block, txid)
|
|
|
|
}
|
|
|
|
restoreCheckBalance(finalBalance, 6, 20, promptChangeAddr)
|
|
|
|
|
|
|
|
// We should expect a static fee of 27750 satoshis for spending 6 inputs
|
|
|
|
// (3 P2WPKH, 3 NP2WPKH) to two P2WPKH outputs. Carol should therefore
|
|
|
|
// only have one UTXO present (the change output) of 6 - 5 - fee BTC.
|
|
|
|
const fee = 27750
|
|
|
|
restoreCheckBalance(finalBalance-minerAmt-fee, 1, 21, nil)
|
2018-04-03 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
2020-03-06 18:11:48 +03:00
|
|
|
// commitType is a simple enum used to run though the basic funding flow with
|
|
|
|
// different commitment formats.
|
|
|
|
type commitType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
// commitTypeLegacy is the old school commitment type.
|
2020-03-14 04:52:33 +03:00
|
|
|
commitTypeLegacy commitType = iota
|
2020-03-06 18:11:48 +03:00
|
|
|
|
|
|
|
// commiTypeTweakless is the commitment type where the remote key is
|
|
|
|
// static (non-tweaked).
|
|
|
|
commitTypeTweakless
|
2020-03-14 04:52:33 +03:00
|
|
|
|
|
|
|
// commitTypeAnchors is the kind of commitment that has extra outputs
|
|
|
|
// used for anchoring down to commitment using CPFP.
|
|
|
|
commitTypeAnchors
|
2020-03-06 18:11:48 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// String returns that name of the commitment type.
|
|
|
|
func (c commitType) String() string {
|
|
|
|
switch c {
|
|
|
|
case commitTypeLegacy:
|
|
|
|
return "legacy"
|
|
|
|
case commitTypeTweakless:
|
|
|
|
return "tweakless"
|
2020-03-14 04:52:33 +03:00
|
|
|
case commitTypeAnchors:
|
|
|
|
return "anchors"
|
2020-03-06 18:11:48 +03:00
|
|
|
default:
|
|
|
|
return "invalid"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Args returns the command line flag to supply to enable this commitment type.
|
|
|
|
func (c commitType) Args() []string {
|
|
|
|
switch c {
|
|
|
|
case commitTypeLegacy:
|
2020-07-09 00:26:05 +03:00
|
|
|
return []string{"--protocol.legacy.committweak"}
|
2020-03-06 18:11:48 +03:00
|
|
|
case commitTypeTweakless:
|
|
|
|
return []string{}
|
2020-03-14 04:52:33 +03:00
|
|
|
case commitTypeAnchors:
|
|
|
|
return []string{"--protocol.anchors"}
|
2020-03-06 18:11:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-04 15:21:28 +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.
|
|
|
|
func (c commitType) calcStaticFee(numHTLCs int) btcutil.Amount {
|
|
|
|
const htlcWeight = input.HTLCWeight
|
|
|
|
var (
|
|
|
|
feePerKw = chainfee.SatPerKVByte(50000).FeePerKWeight()
|
|
|
|
commitWeight = input.CommitWeight
|
|
|
|
anchors = btcutil.Amount(0)
|
|
|
|
)
|
|
|
|
|
|
|
|
// The anchor commitment type is slightly heavier, and we must also add
|
|
|
|
// the value of the two anchors to the resulting fee the initiator
|
2020-12-15 13:22:14 +03:00
|
|
|
// pays. In addition the fee rate is capped at 10 sat/vbyte for anchor
|
|
|
|
// channels.
|
2020-03-04 15:21:28 +03:00
|
|
|
if c == commitTypeAnchors {
|
2020-12-15 13:22:14 +03:00
|
|
|
feePerKw = chainfee.SatPerKVByte(
|
|
|
|
lnwallet.DefaultAnchorsCommitMaxFeeRateSatPerVByte * 1000,
|
|
|
|
).FeePerKWeight()
|
2020-03-04 15:21:28 +03:00
|
|
|
commitWeight = input.AnchorCommitWeight
|
|
|
|
anchors = 2 * anchorSize
|
|
|
|
}
|
|
|
|
|
|
|
|
return feePerKw.FeeForWeight(int64(commitWeight+htlcWeight*numHTLCs)) +
|
|
|
|
anchors
|
|
|
|
}
|
|
|
|
|
|
|
|
// channelCommitType retrieves the active channel commitment type for the given
|
|
|
|
// chan point.
|
|
|
|
func channelCommitType(node *lntest.HarnessNode,
|
|
|
|
chanPoint *lnrpc.ChannelPoint) (commitType, error) {
|
|
|
|
|
|
|
|
ctxb := context.Background()
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
|
|
|
|
req := &lnrpc.ListChannelsRequest{}
|
|
|
|
channels, err := node.ListChannels(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("listchannels failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range channels.Channels {
|
|
|
|
if c.ChannelPoint == txStr(chanPoint) {
|
|
|
|
switch c.CommitmentType {
|
|
|
|
|
|
|
|
// If the anchor output size is non-zero, we are
|
|
|
|
// dealing with the anchor type.
|
|
|
|
case lnrpc.CommitmentType_ANCHORS:
|
|
|
|
return commitTypeAnchors, nil
|
|
|
|
|
|
|
|
// StaticRemoteKey means it is tweakless,
|
|
|
|
case lnrpc.CommitmentType_STATIC_REMOTE_KEY:
|
|
|
|
return commitTypeTweakless, nil
|
|
|
|
|
|
|
|
// Otherwise legacy.
|
|
|
|
default:
|
|
|
|
return commitTypeLegacy, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, fmt.Errorf("channel point %v not found", chanPoint)
|
|
|
|
}
|
|
|
|
|
2020-08-06 13:08:44 +03:00
|
|
|
// assertChannelBalanceResp makes a ChannelBalance request and checks the
|
2020-09-24 15:51:58 +03:00
|
|
|
// returned response matches the expected.
|
2020-08-06 13:08:44 +03:00
|
|
|
func assertChannelBalanceResp(t *harnessTest,
|
|
|
|
node *lntest.HarnessNode, expected *lnrpc.ChannelBalanceResponse) {
|
|
|
|
|
2020-08-10 22:47:27 +03:00
|
|
|
resp := getChannelBalance(t, node)
|
|
|
|
require.Equal(
|
|
|
|
t.t, expected, resp, "balance is incorrect",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getChannelBalance gets the channel balance.
|
|
|
|
func getChannelBalance(t *harnessTest,
|
|
|
|
node *lntest.HarnessNode) *lnrpc.ChannelBalanceResponse {
|
|
|
|
|
2020-08-06 13:08:44 +03:00
|
|
|
t.t.Helper()
|
|
|
|
|
|
|
|
ctxt, _ := context.WithTimeout(context.Background(), defaultTimeout)
|
|
|
|
req := &lnrpc.ChannelBalanceRequest{}
|
|
|
|
resp, err := node.ChannelBalance(ctxt, req)
|
|
|
|
|
|
|
|
require.NoError(t.t, err, "unable to get node's balance")
|
2020-08-10 22:47:27 +03:00
|
|
|
return resp
|
2020-08-06 13:08:44 +03:00
|
|
|
}
|
|
|
|
|
2019-09-11 15:49:08 +03:00
|
|
|
// basicChannelFundingTest is a sub-test of the main testBasicChannelFunding
|
|
|
|
// test. Given two nodes: Alice and Bob, it'll assert proper channel creation,
|
|
|
|
// then return a function closure that should be called to assert proper
|
|
|
|
// channel closure.
|
|
|
|
func basicChannelFundingTest(t *harnessTest, net *lntest.NetworkHarness,
|
2019-12-11 07:02:49 +03:00
|
|
|
alice *lntest.HarnessNode, bob *lntest.HarnessNode,
|
|
|
|
fundingShim *lnrpc.FundingShim) (*lnrpc.Channel, *lnrpc.Channel, func(), error) {
|
2016-09-15 22:24:52 +03:00
|
|
|
|
2020-11-17 02:28:56 +03:00
|
|
|
chanAmt := funding.MaxBtcFundingAmount
|
2017-01-10 06:34:22 +03:00
|
|
|
pushAmt := btcutil.Amount(100000)
|
2016-08-31 05:36:33 +03:00
|
|
|
|
2020-08-10 22:47:27 +03:00
|
|
|
// Record nodes' channel balance before testing.
|
|
|
|
aliceChannelBalance := getChannelBalance(t, alice)
|
|
|
|
bobChannelBalance := getChannelBalance(t, bob)
|
|
|
|
|
|
|
|
// Creates a helper closure to be used below which asserts the proper
|
|
|
|
// response to a channel balance RPC.
|
|
|
|
checkChannelBalance := func(node *lntest.HarnessNode,
|
|
|
|
oldChannelBalance *lnrpc.ChannelBalanceResponse,
|
|
|
|
local, remote btcutil.Amount) {
|
|
|
|
|
|
|
|
newResp := oldChannelBalance
|
|
|
|
|
|
|
|
newResp.LocalBalance.Sat += uint64(local)
|
|
|
|
newResp.LocalBalance.Msat += uint64(
|
|
|
|
lnwire.NewMSatFromSatoshis(local),
|
|
|
|
)
|
|
|
|
newResp.RemoteBalance.Sat += uint64(remote)
|
|
|
|
newResp.RemoteBalance.Msat += uint64(
|
|
|
|
lnwire.NewMSatFromSatoshis(remote),
|
|
|
|
)
|
|
|
|
// Deprecated fields.
|
|
|
|
newResp.Balance += int64(local)
|
|
|
|
assertChannelBalanceResp(t, node, newResp)
|
|
|
|
}
|
|
|
|
|
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.
|
2019-09-11 15:49:08 +03:00
|
|
|
ctxb := context.Background()
|
2018-11-26 18:09:39 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2019-09-11 15:49:08 +03:00
|
|
|
ctxt, t, net, alice, bob,
|
2018-08-22 10:32:42 +03:00
|
|
|
lntest.OpenChannelParams{
|
2019-12-11 07:02:49 +03:00
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
FundingShim: fundingShim,
|
2018-08-22 10:32:42 +03:00
|
|
|
},
|
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)
|
2019-09-11 15:49:08 +03:00
|
|
|
err := alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
2017-03-15 06:06:33 +03:00
|
|
|
if err != nil {
|
2019-09-11 15:49:08 +03:00
|
|
|
return nil, nil, nil, fmt.Errorf("alice didn't report "+
|
|
|
|
"channel: %v", err)
|
2017-03-15 06:06:33 +03:00
|
|
|
}
|
2019-09-11 15:49:08 +03:00
|
|
|
err = bob.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
2017-10-19 01:13:36 +03:00
|
|
|
if err != nil {
|
2019-09-11 15:49:08 +03:00
|
|
|
return nil, nil, nil, fmt.Errorf("bob didn't report "+
|
|
|
|
"channel: %v", err)
|
2017-10-19 01:13:36 +03:00
|
|
|
}
|
2017-03-15 06:06:33 +03:00
|
|
|
|
2020-03-04 15:21:28 +03:00
|
|
|
cType, err := channelCommitType(alice, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("unable to get channel "+
|
|
|
|
"type: %v", err)
|
|
|
|
}
|
|
|
|
|
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.
|
2020-08-06 13:08:44 +03:00
|
|
|
aliceLocalBalance := chanAmt - pushAmt - cType.calcStaticFee(0)
|
2020-08-10 22:47:27 +03:00
|
|
|
checkChannelBalance(
|
|
|
|
alice, aliceChannelBalance, aliceLocalBalance, pushAmt,
|
|
|
|
)
|
|
|
|
checkChannelBalance(
|
|
|
|
bob, bobChannelBalance, pushAmt, aliceLocalBalance,
|
|
|
|
)
|
2016-09-15 22:24:52 +03:00
|
|
|
|
2019-09-11 15:49:08 +03:00
|
|
|
req := &lnrpc.ListChannelsRequest{}
|
|
|
|
aliceChannel, err := alice.ListChannels(context.Background(), req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("unable to obtain chan: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bobChannel, err := bob.ListChannels(context.Background(), req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("unable to obtain chan: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
closeChan := func() {
|
|
|
|
// 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.
|
2020-01-10 17:27:50 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelCloseTimeout)
|
2019-09-11 15:49:08 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, alice, chanPoint, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return aliceChannel.Channels[0], bobChannel.Channels[0], closeChan, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// testBasicChannelFunding performs a test exercising expected behavior from a
|
|
|
|
// basic funding workflow. The test creates a new channel between Alice and
|
|
|
|
// Bob, then immediately closes the channel after asserting some expected post
|
|
|
|
// conditions. Finally, the chain itself is checked to ensure the closing
|
|
|
|
// transaction was mined.
|
|
|
|
func testBasicChannelFunding(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
2020-03-06 18:11:48 +03:00
|
|
|
// Run through the test with combinations of all the different
|
|
|
|
// commitment types.
|
|
|
|
allTypes := []commitType{
|
|
|
|
commitTypeLegacy,
|
|
|
|
commitTypeTweakless,
|
2020-03-24 12:18:30 +03:00
|
|
|
commitTypeAnchors,
|
2020-03-06 18:11:48 +03:00
|
|
|
}
|
|
|
|
|
2019-09-11 15:49:08 +03:00
|
|
|
test:
|
|
|
|
// We'll test all possible combinations of the feature bit presence
|
|
|
|
// that both nodes can signal for this new channel type. We'll make a
|
|
|
|
// new Carol+Dave for each test instance as well.
|
2020-03-06 18:11:48 +03:00
|
|
|
for _, carolCommitType := range allTypes {
|
|
|
|
for _, daveCommitType := range allTypes {
|
2019-09-11 15:49:08 +03:00
|
|
|
// Based on the current tweak variable for Carol, we'll
|
|
|
|
// preferentially signal the legacy commitment format.
|
|
|
|
// We do the same for Dave shortly below.
|
2020-03-06 18:11:48 +03:00
|
|
|
carolArgs := carolCommitType.Args()
|
2019-09-11 15:49:08 +03:00
|
|
|
carol, err := net.NewNode("Carol", carolArgs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Each time, we'll send Carol a new set of coins in
|
|
|
|
// order to fund the channel.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-06 18:11:48 +03:00
|
|
|
daveArgs := daveCommitType.Args()
|
2019-09-11 15:49:08 +03:00
|
|
|
dave, err := net.NewNode("Dave", daveArgs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we start the test, we'll ensure both sides
|
|
|
|
// are connected to the funding flow can properly be
|
|
|
|
// executed.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.EnsureConnected(ctxt, carol, dave)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to connect peers: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-06 18:11:48 +03:00
|
|
|
testName := fmt.Sprintf("carol_commit=%v,dave_commit=%v",
|
|
|
|
carolCommitType, daveCommitType)
|
2019-09-11 15:49:08 +03:00
|
|
|
|
|
|
|
ht := t
|
2020-01-10 17:27:50 +03:00
|
|
|
carolCommitType := carolCommitType
|
|
|
|
daveCommitType := daveCommitType
|
2019-09-11 15:49:08 +03:00
|
|
|
success := t.t.Run(testName, func(t *testing.T) {
|
|
|
|
carolChannel, daveChannel, closeChan, err := basicChannelFundingTest(
|
2019-12-11 07:02:49 +03:00
|
|
|
ht, net, carol, dave, nil,
|
2019-09-11 15:49:08 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed funding flow: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-24 12:18:30 +03:00
|
|
|
// Both nodes should report the same commitment
|
|
|
|
// type.
|
|
|
|
chansCommitType := carolChannel.CommitmentType
|
|
|
|
if daveChannel.CommitmentType != chansCommitType {
|
|
|
|
t.Fatalf("commit types don't match, "+
|
|
|
|
"carol got %v, dave got %v",
|
|
|
|
carolChannel.CommitmentType,
|
|
|
|
daveChannel.CommitmentType,
|
|
|
|
)
|
|
|
|
}
|
2020-03-06 18:11:48 +03:00
|
|
|
|
2020-03-24 12:18:30 +03:00
|
|
|
// Now check that the commitment type reported
|
|
|
|
// by both nodes is what we expect. It will be
|
|
|
|
// the minimum of the two nodes' preference, in
|
|
|
|
// the order Legacy, Tweakless, Anchors.
|
|
|
|
expType := carolCommitType
|
2020-03-06 18:11:48 +03:00
|
|
|
|
2020-03-24 12:18:30 +03:00
|
|
|
switch daveCommitType {
|
2020-03-06 18:11:48 +03:00
|
|
|
|
2020-03-24 12:18:30 +03:00
|
|
|
// Dave supports anchors, type will be what
|
|
|
|
// Carol supports.
|
|
|
|
case commitTypeAnchors:
|
|
|
|
|
|
|
|
// Dave only supports tweakless, channel will
|
|
|
|
// be downgraded to this type if Carol supports
|
|
|
|
// anchors.
|
|
|
|
case commitTypeTweakless:
|
|
|
|
if expType == commitTypeAnchors {
|
|
|
|
expType = commitTypeTweakless
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dave only supoprts legacy type, channel will
|
|
|
|
// be downgraded to this type.
|
|
|
|
case commitTypeLegacy:
|
|
|
|
expType = commitTypeLegacy
|
|
|
|
|
|
|
|
default:
|
|
|
|
t.Fatalf("invalid commit type %v",
|
|
|
|
daveCommitType)
|
|
|
|
}
|
2020-03-06 18:11:48 +03:00
|
|
|
|
2020-03-24 12:18:30 +03:00
|
|
|
// Check that the signalled type matches what we
|
|
|
|
// expect.
|
2019-09-11 15:49:08 +03:00
|
|
|
switch {
|
2020-07-02 07:06:31 +03:00
|
|
|
case expType == commitTypeAnchors &&
|
|
|
|
chansCommitType == lnrpc.CommitmentType_ANCHORS:
|
|
|
|
|
|
|
|
case expType == commitTypeTweakless &&
|
|
|
|
chansCommitType == lnrpc.CommitmentType_STATIC_REMOTE_KEY:
|
|
|
|
|
|
|
|
case expType == commitTypeLegacy &&
|
|
|
|
chansCommitType == lnrpc.CommitmentType_LEGACY:
|
2020-03-24 12:18:30 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
t.Fatalf("expected nodes to signal "+
|
|
|
|
"commit type %v, instead got "+
|
|
|
|
"%v", expType, chansCommitType)
|
2019-09-11 15:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// As we've concluded this sub-test case we'll
|
|
|
|
// now close out the channel for both sides.
|
|
|
|
closeChan()
|
|
|
|
})
|
|
|
|
if !success {
|
|
|
|
break test
|
|
|
|
}
|
|
|
|
|
|
|
|
shutdownAndAssert(net, t, carol)
|
|
|
|
shutdownAndAssert(net, t, dave)
|
|
|
|
}
|
|
|
|
}
|
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 (
|
2020-11-17 02:28:56 +03:00
|
|
|
chanAmt = funding.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.
|
2020-04-03 00:29:50 +03:00
|
|
|
|
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)
|
|
|
|
}
|
2020-04-03 00:29:50 +03:00
|
|
|
|
|
|
|
chanOpenUpdate := openChannelStream(
|
|
|
|
ctxt, t, net, carol, net.Alice,
|
2018-08-22 10:32:42 +03:00
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
SpendUnconfirmed: true,
|
|
|
|
},
|
2018-08-10 07:14:31 +03:00
|
|
|
)
|
|
|
|
|
2020-08-10 22:47:27 +03:00
|
|
|
// Creates a helper closure to be used below which asserts the proper
|
|
|
|
// response to a channel balance RPC.
|
|
|
|
checkChannelBalance := func(node *lntest.HarnessNode,
|
|
|
|
local, remote, pendingLocal, pendingRemote btcutil.Amount) {
|
|
|
|
expectedResponse := &lnrpc.ChannelBalanceResponse{
|
|
|
|
LocalBalance: &lnrpc.Amount{
|
|
|
|
Sat: uint64(local),
|
|
|
|
Msat: uint64(lnwire.NewMSatFromSatoshis(
|
|
|
|
local,
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
RemoteBalance: &lnrpc.Amount{
|
|
|
|
Sat: uint64(remote),
|
|
|
|
Msat: uint64(lnwire.NewMSatFromSatoshis(
|
|
|
|
remote,
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
PendingOpenLocalBalance: &lnrpc.Amount{
|
|
|
|
Sat: uint64(pendingLocal),
|
|
|
|
Msat: uint64(lnwire.NewMSatFromSatoshis(
|
|
|
|
pendingLocal,
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
PendingOpenRemoteBalance: &lnrpc.Amount{
|
|
|
|
Sat: uint64(pendingRemote),
|
|
|
|
Msat: uint64(lnwire.NewMSatFromSatoshis(
|
|
|
|
pendingRemote,
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
UnsettledLocalBalance: &lnrpc.Amount{},
|
|
|
|
UnsettledRemoteBalance: &lnrpc.Amount{},
|
|
|
|
// Deprecated fields.
|
|
|
|
Balance: int64(local),
|
|
|
|
PendingOpenBalance: int64(pendingLocal),
|
|
|
|
}
|
|
|
|
assertChannelBalanceResp(t, node, expectedResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
// As the channel is pending open, it's expected Carol has both zero
|
|
|
|
// local and remote balances, and pending local/remote should not be
|
|
|
|
// zero.
|
|
|
|
//
|
|
|
|
// Note that atm we haven't obtained the chanPoint yet, so we use the
|
|
|
|
// type directly.
|
|
|
|
cType := commitTypeTweakless
|
|
|
|
carolLocalBalance := chanAmt - pushAmt - cType.calcStaticFee(0)
|
|
|
|
checkChannelBalance(carol, 0, 0, carolLocalBalance, pushAmt)
|
|
|
|
|
|
|
|
// For Alice, her local/remote balances should be zero, and the
|
|
|
|
// local/remote balances are the mirror of Carol's.
|
|
|
|
checkChannelBalance(net.Alice, 0, 0, pushAmt, carolLocalBalance)
|
|
|
|
|
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.
|
2020-08-10 22:47:27 +03:00
|
|
|
checkChannelBalance(carol, carolLocalBalance, pushAmt, 0, 0)
|
|
|
|
checkChannelBalance(net.Alice, pushAmt, carolLocalBalance, 0, 0)
|
2018-08-10 07:14:31 +03:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2019-11-05 10:57:38 +03:00
|
|
|
// testPaymentFollowingChannelOpen tests that the channel transition from
|
|
|
|
// 'pending' to 'open' state does not cause any inconsistencies within other
|
2020-10-26 16:21:05 +03:00
|
|
|
// subsystems trying to update the channel state in the db. We follow this
|
2019-11-28 00:20:06 +03:00
|
|
|
// transition with a payment that updates the commitment state and verify that
|
|
|
|
// the pending state is up to date.
|
2019-11-05 10:57:38 +03:00
|
|
|
func testPaymentFollowingChannelOpen(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
const paymentAmt = btcutil.Amount(100)
|
2020-01-10 17:27:50 +03:00
|
|
|
channelCapacity := paymentAmt * 1000
|
2019-11-28 00:20:06 +03:00
|
|
|
|
|
|
|
// We first establish a channel between Alice and Bob.
|
|
|
|
ctxt, cancel := context.WithTimeout(ctxb, channelOpenTimeout)
|
|
|
|
defer cancel()
|
|
|
|
pendingUpdate, err := net.OpenPendingChannel(
|
|
|
|
ctxt, net.Alice, net.Bob, channelCapacity, 0,
|
|
|
|
)
|
2019-11-05 10:57:38 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to open channel: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-28 00:20:06 +03:00
|
|
|
// At this point, the channel's funding transaction will have been
|
|
|
|
// broadcast, but not confirmed. Alice and Bob's nodes
|
2019-11-05 10:57:38 +03:00
|
|
|
// should reflect this when queried via RPC.
|
2019-11-28 00:20:06 +03:00
|
|
|
ctxt, cancel = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
defer cancel()
|
2019-11-05 10:57:38 +03:00
|
|
|
assertNumOpenChannelsPending(ctxt, t, net.Alice, net.Bob, 1)
|
|
|
|
|
2019-11-28 00:20:06 +03:00
|
|
|
// We are restarting Bob's node to let the link be created for the
|
|
|
|
// pending channel.
|
2019-11-05 10:57:38 +03:00
|
|
|
if err := net.RestartNode(net.Bob, nil); err != nil {
|
|
|
|
t.Fatalf("Bob restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-10-26 16:21:05 +03:00
|
|
|
// We ensure that Bob reconnects to Alice.
|
2019-11-05 10:57:38 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.EnsureConnected(ctxt, net.Bob, net.Alice); err != nil {
|
|
|
|
t.Fatalf("peers unable to reconnect after restart: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We mine one block for the channel to be confirmed.
|
|
|
|
_ = mineBlocks(t, net, 6, 1)[0]
|
|
|
|
|
2020-10-26 16:21:05 +03:00
|
|
|
// We verify that the channel is open from both nodes point of view.
|
2019-11-28 00:20:06 +03:00
|
|
|
ctxt, cancel = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
defer cancel()
|
2019-11-05 10:57:38 +03:00
|
|
|
assertNumOpenChannelsPending(ctxt, t, net.Alice, net.Bob, 0)
|
|
|
|
|
2019-11-28 00:20:06 +03:00
|
|
|
// With the channel open, we'll create invoices for Bob that Alice will
|
|
|
|
// pay to in order to advance the state of the channel.
|
2019-11-05 10:57:38 +03:00
|
|
|
bobPayReqs, _, _, err := createPayReqs(
|
|
|
|
net.Bob, paymentAmt, 1,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:59:18 +03:00
|
|
|
// Send payment to Bob so that a channel update to disk will be
|
2019-11-28 00:20:06 +03:00
|
|
|
// executed.
|
2020-10-26 16:21:05 +03:00
|
|
|
sendAndAssertSuccess(t, net.Alice, &routerrpc.SendPaymentRequest{
|
|
|
|
PaymentRequest: bobPayReqs[0],
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitSat: 1000000,
|
|
|
|
})
|
2019-11-05 10:57:38 +03:00
|
|
|
|
2019-11-28 00:20:06 +03:00
|
|
|
// At this point we want to make sure the channel is opened and not
|
|
|
|
// pending.
|
|
|
|
ctxt, cancel = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
defer cancel()
|
2019-11-05 10:57:38 +03:00
|
|
|
res, err := net.Bob.ListChannels(ctxt, &lnrpc.ListChannelsRequest{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to list bob channels: %v", err)
|
|
|
|
}
|
|
|
|
if len(res.Channels) == 0 {
|
|
|
|
t.Fatalf("bob list of channels is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, immediately close the channel. This function will also
|
|
|
|
// block until the channel is closed and will additionally assert the
|
|
|
|
// relevant channel closing post conditions.
|
|
|
|
chanPoint := &lnrpc.ChannelPoint{
|
|
|
|
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
|
|
|
|
FundingTxidBytes: pendingUpdate.Txid,
|
|
|
|
},
|
|
|
|
OutputIndex: pendingUpdate.OutputIndex,
|
|
|
|
}
|
2019-11-28 00:20:06 +03:00
|
|
|
ctxt, cancel = context.WithTimeout(ctxb, channelCloseTimeout)
|
|
|
|
defer cancel()
|
2019-11-05 10:57:38 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, 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 {
|
2021-02-13 11:05:33 +03:00
|
|
|
fundingTxID, err := lnrpc.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
|
|
|
|
2019-08-20 03:55:30 +03:00
|
|
|
// calculateMaxHtlc re-implements the RequiredRemoteChannelReserve of the
|
|
|
|
// funding manager's config, which corresponds to the maximum MaxHTLC value we
|
|
|
|
// allow users to set when updating a channel policy.
|
|
|
|
func calculateMaxHtlc(chanCap btcutil.Amount) uint64 {
|
|
|
|
reserve := lnwire.NewMSatFromSatoshis(chanCap / 100)
|
|
|
|
max := lnwire.NewMSatFromSatoshis(chanCap) - reserve
|
|
|
|
return uint64(max)
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-03-04 02:15:02 +03:00
|
|
|
if len(expUpdates) == 0 {
|
|
|
|
t.Fatalf("received unexpected channel "+
|
|
|
|
"update from %v for channel %v",
|
|
|
|
update.AdvertisingNode,
|
|
|
|
update.ChanId)
|
|
|
|
}
|
|
|
|
|
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)
|
2020-12-08 18:27:01 +03:00
|
|
|
case <-time.After(defaultTimeout):
|
2021-03-04 02:15:02 +03:00
|
|
|
if len(expUpdates) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2018-04-30 23:55:31 +03:00
|
|
|
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)
|
2020-11-25 03:40:54 +03:00
|
|
|
require.NoError(t.t, err, "unable to query for alice's graph")
|
2018-04-30 23:55:31 +03:00
|
|
|
|
2019-03-13 03:34:42 +03:00
|
|
|
var policies []*lnrpc.RoutingPolicy
|
2020-11-25 03:40:54 +03:00
|
|
|
err = wait.NoError(func() error {
|
|
|
|
out:
|
|
|
|
for _, chanPoint := range chanPoints {
|
|
|
|
for _, e := range chanGraph.Edges {
|
|
|
|
if e.ChanPoint != txStr(chanPoint) {
|
|
|
|
continue
|
|
|
|
}
|
2018-04-30 23:55:31 +03:00
|
|
|
|
2020-11-25 03:40:54 +03:00
|
|
|
if e.Node1Pub == advertisingNode {
|
|
|
|
policies = append(policies,
|
|
|
|
e.Node1Policy)
|
|
|
|
} else {
|
|
|
|
policies = append(policies,
|
|
|
|
e.Node2Policy)
|
|
|
|
}
|
|
|
|
|
|
|
|
continue out
|
2018-04-30 23:55:31 +03:00
|
|
|
}
|
|
|
|
|
2020-11-25 03:40:54 +03:00
|
|
|
// If we've iterated over all the known edges and we weren't
|
|
|
|
// able to find this specific one, then we'll fail.
|
|
|
|
return fmt.Errorf("did not find edge %v", txStr(chanPoint))
|
2018-04-30 23:55:31 +03:00
|
|
|
}
|
|
|
|
|
2020-11-25 03:40:54 +03:00
|
|
|
return nil
|
|
|
|
}, defaultTimeout)
|
|
|
|
require.NoError(t.t, err)
|
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)
|
|
|
|
}
|
2019-08-20 03:55:30 +03:00
|
|
|
if policy.MaxHtlcMsat != expectedPolicy.MaxHtlcMsat {
|
|
|
|
return fmt.Errorf("expected max htlc %v, got %v",
|
|
|
|
expectedPolicy.MaxHtlcMsat, policy.MaxHtlcMsat)
|
|
|
|
}
|
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
|
2020-10-06 21:56:10 +03:00
|
|
|
defaultTimeLockDelta = chainreg.DefaultBitcoinTimeLockDelta
|
2018-08-22 10:32:43 +03:00
|
|
|
defaultMinHtlc = 1000
|
|
|
|
)
|
2020-11-17 02:28:56 +03:00
|
|
|
defaultMaxHtlc := calculateMaxHtlc(funding.MaxBtcFundingAmount)
|
2018-08-22 10:32:43 +03:00
|
|
|
|
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
|
|
|
|
2020-11-17 02:28:56 +03:00
|
|
|
chanAmt := funding.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,
|
2019-08-20 03:55:30 +03:00
|
|
|
MaxHtlcMsat: defaultMaxHtlc,
|
2018-08-22 10:32:43 +03:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-03-04 02:15:02 +03:00
|
|
|
// Create Carol with options to rate limit channel updates up to 2 per
|
|
|
|
// day, and create a new channel Bob->Carol.
|
|
|
|
carol, err := net.NewNode(
|
|
|
|
"Carol", []string{
|
|
|
|
"--gossip.max-channel-update-burst=2",
|
|
|
|
"--gossip.channel-update-interval=24h",
|
|
|
|
},
|
|
|
|
)
|
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,
|
2019-08-20 03:55:30 +03:00
|
|
|
MaxHtlcMsat: defaultMaxHtlc,
|
2018-08-22 10:32:43 +03:00
|
|
|
}
|
|
|
|
expectedPolicyCarol := &lnrpc.RoutingPolicy{
|
|
|
|
FeeBaseMsat: defaultFeeBase,
|
|
|
|
FeeRateMilliMsat: defaultFeeRate,
|
|
|
|
TimeLockDelta: defaultTimeLockDelta,
|
|
|
|
MinHtlc: defaultMinHtlc,
|
2019-08-20 03:55:30 +03:00
|
|
|
MaxHtlcMsat: defaultMaxHtlc,
|
2018-08-22 10:32:43 +03:00
|
|
|
}
|
|
|
|
|
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(
|
2020-03-31 13:44:18 +03:00
|
|
|
ctxt, net.Alice, net.Alice.RouterClient,
|
|
|
|
[]string{resp.PaymentRequest}, true,
|
2018-08-22 10:32:44 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Alice knows about the channel policy of Carol and should therefore
|
|
|
|
// not be able to find a path during routing.
|
2020-03-31 13:44:18 +03:00
|
|
|
expErr := lnrpc.PaymentFailureReason_FAILURE_REASON_NO_ROUTE
|
|
|
|
if err.Error() != expErr.String() {
|
|
|
|
t.Fatalf("expected %v, instead got %v", expErr, err)
|
2018-08-22 10:32:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2020-11-25 07:31:11 +03:00
|
|
|
// Manually set the MPP payload a new for each payment since
|
|
|
|
// the payment addr will change with each invoice, although we
|
|
|
|
// can re-use the route itself.
|
|
|
|
route := routes.Routes[0]
|
|
|
|
route.Hops[len(route.Hops)-1].TlvPayload = true
|
|
|
|
route.Hops[len(route.Hops)-1].MppRecord = &lnrpc.MPPRecord{
|
|
|
|
PaymentAddr: resp.PaymentAddr,
|
|
|
|
TotalAmtMsat: amtMSat,
|
|
|
|
}
|
|
|
|
|
2018-08-22 10:32:44 +03:00
|
|
|
sendReq = &lnrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: resp.RHash,
|
2020-11-25 07:31:11 +03:00
|
|
|
Route: route,
|
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)
|
|
|
|
}
|
|
|
|
|
2019-08-20 03:55:30 +03:00
|
|
|
// With our little cluster set up, we'll update the fees and the max htlc
|
|
|
|
// size for the 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)
|
2019-08-20 03:55:30 +03:00
|
|
|
maxHtlc := uint64(500000)
|
2017-12-19 19:10:00 +03:00
|
|
|
|
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,
|
2019-08-20 03:55:30 +03:00
|
|
|
MaxHtlcMsat: maxHtlc,
|
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,
|
2019-08-20 03:55:30 +03:00
|
|
|
MaxHtlcMsat: maxHtlc,
|
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(
|
2020-03-31 13:44:18 +03:00
|
|
|
ctxt, net.Alice, net.Alice.RouterClient,
|
|
|
|
[]string{resp.PaymentRequest}, true,
|
2018-04-04 06:18:42 +03:00
|
|
|
)
|
|
|
|
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)
|
2020-01-10 17:27:50 +03:00
|
|
|
maxHtlc *= 2
|
2017-12-19 19:10:00 +03:00
|
|
|
|
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
|
2019-08-20 03:55:30 +03:00
|
|
|
expectedPolicy.MaxHtlcMsat = maxHtlc
|
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,
|
2019-08-20 03:55:30 +03:00
|
|
|
MaxHtlcMsat: maxHtlc,
|
2017-12-19 19:10:00 +03:00
|
|
|
}
|
|
|
|
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 {
|
2019-08-20 03:55:30 +03:00
|
|
|
t.Fatalf("unable to update alice's channel policy: %v", err)
|
2017-12-19 19:10:00 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-03-04 02:15:02 +03:00
|
|
|
// Now, to test that Carol is properly rate limiting incoming updates,
|
|
|
|
// we'll send two more update from Alice. Carol should accept the first,
|
|
|
|
// but not the second, as she only allows two updates per day and a day
|
|
|
|
// has yet to elapse from the previous update.
|
|
|
|
const numUpdatesTilRateLimit = 2
|
|
|
|
for i := 0; i < numUpdatesTilRateLimit; i++ {
|
|
|
|
prevAlicePolicy := *expectedPolicy
|
|
|
|
baseFee *= 2
|
|
|
|
expectedPolicy.FeeBaseMsat = baseFee
|
|
|
|
req.BaseFeeMsat = baseFee
|
|
|
|
|
|
|
|
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
_, err = net.Alice.UpdateChannelPolicy(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to update alice's channel policy: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all nodes to have seen the policy updates for both
|
|
|
|
// of Alice's channels. Carol will not see the last update as
|
|
|
|
// the limit has been reached.
|
|
|
|
for idx, graphSub := range graphSubs {
|
|
|
|
expUpdates := []expectedChanUpdate{
|
|
|
|
{net.Alice.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
{net.Alice.PubKeyStr, expectedPolicy, chanPoint3},
|
|
|
|
}
|
|
|
|
// Carol was added last, which is why we check the last
|
|
|
|
// index.
|
|
|
|
if i == numUpdatesTilRateLimit-1 && idx == len(graphSubs)-1 {
|
|
|
|
expUpdates = nil
|
|
|
|
}
|
|
|
|
waitForChannelUpdate(t, graphSub, expUpdates)
|
|
|
|
}
|
|
|
|
|
|
|
|
// And finally check that all nodes remembers the policy update
|
|
|
|
// they received. Since Carol didn't receive the last update,
|
|
|
|
// she still has Alice's old policy.
|
|
|
|
for idx, node := range nodes {
|
|
|
|
policy := expectedPolicy
|
|
|
|
// Carol was added last, which is why we check the last
|
|
|
|
// index.
|
|
|
|
if i == numUpdatesTilRateLimit-1 && idx == len(nodes)-1 {
|
|
|
|
policy = &prevAlicePolicy
|
|
|
|
}
|
|
|
|
assertChannelPolicy(
|
|
|
|
t, node, net.Alice.PubKeyStr, policy, 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
|
2019-09-19 22:46:29 +03:00
|
|
|
err := wait.Predicate(func() bool {
|
2020-12-08 18:27:01 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctx, defaultTimeout)
|
2018-12-14 11:24:00 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-12-14 11:24:00 +03:00
|
|
|
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
|
2019-09-19 22:46:29 +03:00
|
|
|
err := wait.Predicate(func() bool {
|
2018-12-14 11:24:00 +03:00
|
|
|
_, 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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-12-14 11:24:00 +03:00
|
|
|
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.
|
2020-11-04 13:03:30 +03:00
|
|
|
tempLogDir := fmt.Sprintf("%s/.tempminerlogs", lntest.GetLogDir())
|
2020-09-30 01:15:05 +03:00
|
|
|
logFilename := "output-open_channel_reorg-temp_miner.log"
|
|
|
|
tempMiner, tempMinerCleanUp, err := lntest.NewMiner(
|
2020-12-03 13:30:22 +03:00
|
|
|
tempLogDir, logFilename, harnessNetParams,
|
|
|
|
&rpcclient.NotificationHandlers{}, lntest.GetBtcdBinary(),
|
2020-07-31 11:32:30 +03:00
|
|
|
)
|
2020-09-30 01:15:05 +03:00
|
|
|
require.NoError(t.t, err, "failed to create temp miner")
|
2020-09-02 21:38:54 +03:00
|
|
|
defer func() {
|
|
|
|
require.NoError(
|
2020-09-30 01:15:05 +03:00
|
|
|
t.t, tempMinerCleanUp(),
|
|
|
|
"failed to clean up temp miner",
|
2020-09-02 21:38:54 +03:00
|
|
|
)
|
|
|
|
}()
|
2017-10-02 18:56:03 +03:00
|
|
|
|
2020-09-30 01:15:05 +03:00
|
|
|
// Setup the temp miner
|
|
|
|
require.NoError(
|
|
|
|
t.t, tempMiner.SetUp(false, 0), "unable to set up mining node",
|
|
|
|
)
|
|
|
|
|
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
|
2020-11-17 02:28:56 +03:00
|
|
|
chanAmt := funding.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
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-12-14 11:24:00 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-12-14 11:24:00 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-20 07:12:29 +03:00
|
|
|
// testDisconnectingTargetPeer performs a test which disconnects Alice-peer from
|
2019-11-20 07:13:04 +03:00
|
|
|
// Bob-peer and then re-connects them again. We expect Alice to be able to
|
|
|
|
// disconnect at any point.
|
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()
|
|
|
|
|
2019-11-20 07:13:04 +03:00
|
|
|
// We'll start both nodes with a high backoff so that they don't
|
|
|
|
// reconnect automatically during our test.
|
|
|
|
args := []string{
|
|
|
|
"--minbackoff=1m",
|
|
|
|
"--maxbackoff=1m",
|
|
|
|
}
|
|
|
|
|
|
|
|
alice, err := net.NewNode("Alice", args)
|
2019-11-20 07:12:29 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, alice)
|
|
|
|
|
2019-11-20 07:13:04 +03:00
|
|
|
bob, err := net.NewNode("Bob", args)
|
2019-11-20 07:12:29 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, bob)
|
|
|
|
|
|
|
|
// Start by connecting Alice and Bob with no channels.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, alice, bob); err != nil {
|
|
|
|
t.Fatalf("unable to connect Alice's peer to Bob's: err %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-05 10:00:39 +03:00
|
|
|
// Check existing connection.
|
2019-11-20 07:12:29 +03:00
|
|
|
assertNumConnections(t, alice, bob, 1)
|
|
|
|
|
|
|
|
// Give Alice some coins so she can fund a channel.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, alice)
|
|
|
|
if err != nil {
|
2020-06-10 15:21:13 +03:00
|
|
|
t.Fatalf("unable to send coins to alice: %v", err)
|
2019-11-20 07:12:29 +03:00
|
|
|
}
|
2017-05-05 10:00:39 +03:00
|
|
|
|
2020-11-17 02:28:56 +03:00
|
|
|
chanAmt := funding.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
|
2019-11-20 07:12:29 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
|
|
|
pendingUpdate, err := net.OpenPendingChannel(
|
|
|
|
ctxt, alice, bob, chanAmt, pushAmt,
|
|
|
|
)
|
2017-05-05 10:00:39 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to open channel: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-20 07:12:29 +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)
|
2019-11-20 07:12:29 +03:00
|
|
|
assertNumOpenChannelsPending(ctxt, t, alice, bob, 1)
|
2017-05-05 10:00:39 +03:00
|
|
|
|
2019-11-20 07:12:29 +03:00
|
|
|
// Disconnect Alice-peer from Bob-peer and get error causes by one
|
|
|
|
// pending channel with detach node is existing.
|
2019-11-20 07:13:04 +03:00
|
|
|
if err := net.DisconnectNodes(ctxt, alice, bob); err != nil {
|
2017-05-05 10:00:39 +03:00
|
|
|
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)
|
|
|
|
|
2019-11-20 07:13:04 +03:00
|
|
|
// Assert that the connection was torn down.
|
|
|
|
assertNumConnections(t, alice, bob, 0)
|
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)
|
|
|
|
|
2019-11-20 07:12:29 +03:00
|
|
|
// 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
|
|
|
|
2019-11-20 07:12:29 +03:00
|
|
|
assertNumOpenChannelsPending(ctxt, t, alice, bob, 0)
|
2017-05-05 10:00:39 +03:00
|
|
|
|
2019-11-20 07:13:04 +03:00
|
|
|
// Reconnect the nodes so that the channel can become active.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, alice, bob); err != nil {
|
|
|
|
t.Fatalf("unable to connect Alice's peer to Bob's: err %v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-20 07:12:29 +03:00
|
|
|
// The channel should be listed in the peer information returned by both
|
|
|
|
// peers.
|
2017-05-05 10:00:39 +03:00
|
|
|
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)
|
2019-11-20 07:12:29 +03:00
|
|
|
if err := net.AssertChannelExists(ctxt, alice, &outPoint); err != nil {
|
2017-05-05 10:00:39 +03:00
|
|
|
t.Fatalf("unable to assert channel existence: %v", err)
|
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2019-11-20 07:12:29 +03:00
|
|
|
if err := net.AssertChannelExists(ctxt, bob, &outPoint); err != nil {
|
2017-05-05 10:00:39 +03:00
|
|
|
t.Fatalf("unable to assert channel existence: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-20 07:12:07 +03:00
|
|
|
// Disconnect Alice-peer from Bob-peer and get error causes by one
|
|
|
|
// active channel with detach node is existing.
|
2019-11-20 07:13:04 +03:00
|
|
|
if err := net.DisconnectNodes(ctxt, alice, bob); err != nil {
|
2019-11-20 07:12:07 +03:00
|
|
|
t.Fatalf("Bob's peer was disconnected from Alice's"+
|
|
|
|
" while one active channel is existing: err %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check existing connection.
|
2019-11-20 07:13:04 +03:00
|
|
|
assertNumConnections(t, alice, bob, 0)
|
|
|
|
|
|
|
|
// Reconnect both nodes before force closing the channel.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, alice, bob); err != nil {
|
|
|
|
t.Fatalf("unable to connect Alice's peer to Bob's: err %v", err)
|
|
|
|
}
|
2019-11-20 07:12:07 +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.
|
2017-05-05 10:00:39 +03:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2019-11-20 07:12:29 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, alice, chanPoint, true)
|
2017-05-05 10:00:39 +03:00
|
|
|
|
2019-11-20 07:12:29 +03:00
|
|
|
// Disconnect Alice-peer from Bob-peer without getting error about
|
|
|
|
// existing channels.
|
2019-11-20 07:13:04 +03:00
|
|
|
if err := net.DisconnectNodes(ctxt, alice, bob); err != nil {
|
2018-04-13 14:20:12 +03:00
|
|
|
t.Fatalf("unable to disconnect Bob's peer from Alice's: err %v",
|
2019-11-20 07:13:04 +03:00
|
|
|
err)
|
2017-05-05 10:00:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check zero peer connections.
|
2019-11-20 07:12:29 +03:00
|
|
|
assertNumConnections(t, alice, 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)
|
2019-11-20 07:12:29 +03:00
|
|
|
if err := net.ConnectNodes(ctxt, alice, bob); err != nil {
|
2017-05-05 10:00:39 +03:00
|
|
|
t.Fatalf("unable to connect Alice's peer to Bob's: err %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check existing connection.
|
2019-11-20 07:12:29 +03:00
|
|
|
assertNumConnections(t, 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.
|
2019-11-20 07:12:29 +03:00
|
|
|
cleanupForceClose(t, 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()
|
|
|
|
|
2020-11-17 02:28:56 +03:00
|
|
|
chanAmt := funding.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)
|
|
|
|
}
|
2020-07-29 10:27:22 +03:00
|
|
|
fundingTxStr := fundingTxID.String()
|
2017-01-31 07:21:52 +03:00
|
|
|
|
|
|
|
// Mine a block, then wait for Alice's node to notify us that the
|
|
|
|
// channel has been opened. The funding transaction should be found
|
|
|
|
// within the newly mined block.
|
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)
|
|
|
|
|
2020-07-29 10:27:22 +03:00
|
|
|
// Get the height that our transaction confirmed at.
|
|
|
|
_, height, err := net.Miner.Node.GetBestBlock()
|
|
|
|
require.NoError(t.t, err, "could not get best block")
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-07-29 10:27:22 +03:00
|
|
|
// Assert that our wallet has our opening transaction with a label
|
|
|
|
// that does not have a channel ID set yet, because we have not
|
|
|
|
// reached our required confirmations.
|
2020-01-10 17:27:50 +03:00
|
|
|
tx := findTxAtHeight(ctxt, t, height, fundingTxStr, net.Alice)
|
2020-07-29 10:27:22 +03:00
|
|
|
|
|
|
|
// At this stage, we expect the transaction to be labelled, but not with
|
|
|
|
// our channel ID because our transaction has not yet confirmed.
|
|
|
|
label := labels.MakeLabel(labels.LabelTypeChannelOpen, nil)
|
|
|
|
require.Equal(t.t, label, tx.Label, "open channel label wrong")
|
|
|
|
|
2017-02-25 03:30:58 +03:00
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
|
2020-07-29 10:27:22 +03:00
|
|
|
// Re-lookup our transaction in the block that it confirmed in.
|
2020-01-10 17:27:50 +03:00
|
|
|
tx = findTxAtHeight(ctxt, t, height, fundingTxStr, net.Alice)
|
2020-07-29 10:27:22 +03:00
|
|
|
|
|
|
|
// Create an additional check for our channel assertion that will
|
|
|
|
// check that our label is as expected.
|
|
|
|
check := func(channel *lnrpc.Channel) {
|
|
|
|
shortChanID := lnwire.NewShortChanIDFromInt(
|
|
|
|
channel.ChanId,
|
|
|
|
)
|
|
|
|
|
|
|
|
label := labels.MakeLabel(
|
|
|
|
labels.LabelTypeChannelOpen, &shortChanID,
|
|
|
|
)
|
|
|
|
require.Equal(t.t, label, tx.Label,
|
|
|
|
"open channel label not updated")
|
|
|
|
}
|
|
|
|
|
2017-01-31 07:21:52 +03:00
|
|
|
// 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)
|
2020-07-29 10:27:22 +03:00
|
|
|
err = net.AssertChannelExists(ctxt, net.Alice, &outPoint, check)
|
|
|
|
if err != nil {
|
2017-01-31 07:21:52 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-29 10:27:22 +03:00
|
|
|
// findTxAtHeight gets all of the transactions that a node's wallet has a record
|
|
|
|
// of at the target height, and finds and returns the tx with the target txid,
|
|
|
|
// failing if it is not found.
|
|
|
|
func findTxAtHeight(ctx context.Context, t *harnessTest, height int32,
|
2020-01-10 17:27:50 +03:00
|
|
|
target string, node *lntest.HarnessNode) *lnrpc.Transaction {
|
2020-07-29 10:27:22 +03:00
|
|
|
|
|
|
|
txns, err := node.LightningClient.GetTransactions(
|
|
|
|
ctx, &lnrpc.GetTransactionsRequest{
|
|
|
|
StartHeight: height,
|
|
|
|
EndHeight: height,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t.t, err, "could not get transactions")
|
|
|
|
|
|
|
|
for _, tx := range txns.Transactions {
|
|
|
|
if tx.TxHash == target {
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-06 13:08:44 +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.
|
2020-11-17 02:28:56 +03:00
|
|
|
amount := funding.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.
|
2020-08-06 13:08:44 +03:00
|
|
|
checkChannelBalance := func(node *lntest.HarnessNode,
|
|
|
|
local, remote btcutil.Amount) {
|
2016-08-31 05:36:33 +03:00
|
|
|
|
2020-08-06 13:08:44 +03:00
|
|
|
expectedResponse := &lnrpc.ChannelBalanceResponse{
|
|
|
|
LocalBalance: &lnrpc.Amount{
|
|
|
|
Sat: uint64(local),
|
|
|
|
Msat: uint64(lnwire.NewMSatFromSatoshis(local)),
|
|
|
|
},
|
|
|
|
RemoteBalance: &lnrpc.Amount{
|
|
|
|
Sat: uint64(remote),
|
|
|
|
Msat: uint64(lnwire.NewMSatFromSatoshis(
|
|
|
|
remote,
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
UnsettledLocalBalance: &lnrpc.Amount{},
|
|
|
|
UnsettledRemoteBalance: &lnrpc.Amount{},
|
|
|
|
PendingOpenLocalBalance: &lnrpc.Amount{},
|
|
|
|
PendingOpenRemoteBalance: &lnrpc.Amount{},
|
|
|
|
// Deprecated fields.
|
|
|
|
Balance: int64(local),
|
2016-09-15 21:59:51 +03:00
|
|
|
}
|
2020-08-06 13:08:44 +03:00
|
|
|
assertChannelBalanceResp(t, node, expectedResponse)
|
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)
|
|
|
|
}
|
|
|
|
|
2020-03-04 15:21:28 +03:00
|
|
|
cType, err := channelCommitType(net.Alice, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get channel type: %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.
|
2020-08-06 13:08:44 +03:00
|
|
|
checkChannelBalance(net.Alice, amount-cType.calcStaticFee(0), 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.
|
2020-08-06 13:08:44 +03:00
|
|
|
checkChannelBalance(net.Bob, 0, amount-cType.calcStaticFee(0))
|
2016-09-15 21:59:51 +03:00
|
|
|
|
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()
|
|
|
|
|
2020-08-10 22:47:27 +03:00
|
|
|
// Creates a helper closure to be used below which asserts the proper
|
|
|
|
// response to a channel balance RPC.
|
|
|
|
checkChannelBalance := func(node *lntest.HarnessNode,
|
|
|
|
local, remote, unsettledLocal, unsettledRemote btcutil.Amount) {
|
|
|
|
|
|
|
|
expectedResponse := &lnrpc.ChannelBalanceResponse{
|
|
|
|
LocalBalance: &lnrpc.Amount{
|
|
|
|
Sat: uint64(local),
|
|
|
|
Msat: uint64(lnwire.NewMSatFromSatoshis(
|
|
|
|
local,
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
RemoteBalance: &lnrpc.Amount{
|
|
|
|
Sat: uint64(remote),
|
|
|
|
Msat: uint64(lnwire.NewMSatFromSatoshis(
|
|
|
|
remote,
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
UnsettledLocalBalance: &lnrpc.Amount{
|
|
|
|
Sat: uint64(unsettledLocal),
|
|
|
|
Msat: uint64(lnwire.NewMSatFromSatoshis(
|
|
|
|
unsettledLocal,
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
UnsettledRemoteBalance: &lnrpc.Amount{
|
|
|
|
Sat: uint64(unsettledRemote),
|
|
|
|
Msat: uint64(lnwire.NewMSatFromSatoshis(
|
|
|
|
unsettledRemote,
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
PendingOpenLocalBalance: &lnrpc.Amount{},
|
|
|
|
PendingOpenRemoteBalance: &lnrpc.Amount{},
|
|
|
|
// Deprecated fields.
|
|
|
|
Balance: int64(local),
|
|
|
|
}
|
|
|
|
assertChannelBalanceResp(t, node, expectedResponse)
|
|
|
|
}
|
|
|
|
|
2018-12-07 03:49:05 +03:00
|
|
|
// Create carol in hodl mode.
|
2019-08-14 20:57:31 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--hodl.exit-settle"})
|
2018-12-07 03:49:05 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-08-10 22:47:27 +03:00
|
|
|
cType, err := channelCommitType(net.Alice, chanPointAlice)
|
|
|
|
require.NoError(t.t, err, "unable to get channel type")
|
|
|
|
|
|
|
|
// Check alice's channel balance, which should have zero remote and zero
|
|
|
|
// pending balance.
|
|
|
|
checkChannelBalance(net.Alice, chanAmt-cType.calcStaticFee(0), 0, 0, 0)
|
|
|
|
|
|
|
|
// Check carol's channel balance, which should have zero local and zero
|
|
|
|
// pending balance.
|
|
|
|
checkChannelBalance(carol, 0, chanAmt-cType.calcStaticFee(0), 0, 0)
|
|
|
|
|
2018-12-07 03:49:05 +03:00
|
|
|
// Channel should be ready for payments.
|
|
|
|
const (
|
|
|
|
payAmt = 100
|
|
|
|
numInvoices = 6
|
|
|
|
)
|
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
// Simulateneously send numInvoices payments from Alice to Carol.
|
2018-12-07 03:49:05 +03:00
|
|
|
carolPubKey := carol.PubKey[:]
|
2020-03-31 13:44:18 +03:00
|
|
|
errChan := make(chan error)
|
2018-12-07 03:49:05 +03:00
|
|
|
for i := 0; i < numInvoices; i++ {
|
2020-03-31 13:44:18 +03:00
|
|
|
go func() {
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
_, err := net.Alice.RouterClient.SendPaymentV2(ctxt,
|
|
|
|
&routerrpc.SendPaymentRequest{
|
|
|
|
Dest: carolPubKey,
|
|
|
|
Amt: int64(payAmt),
|
|
|
|
PaymentHash: makeFakePayHash(t),
|
2020-10-06 21:56:10 +03:00
|
|
|
FinalCltvDelta: chainreg.DefaultBitcoinTimeLockDelta,
|
2020-03-31 13:44:18 +03:00
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}()
|
2018-12-07 03:49:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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}
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-12-07 03:49:05 +03:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
// Check for payment errors.
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
t.Fatalf("payment error: %v", err)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2020-08-10 22:47:27 +03:00
|
|
|
// Check alice's channel balance, which should have a remote unsettled
|
|
|
|
// balance that equals to the amount of invoices * payAmt. The remote
|
|
|
|
// balance remains zero.
|
|
|
|
aliceLocal := chanAmt - cType.calcStaticFee(0) - numInvoices*payAmt
|
|
|
|
checkChannelBalance(net.Alice, aliceLocal, 0, 0, numInvoices*payAmt)
|
|
|
|
|
|
|
|
// Check carol's channel balance, which should have a local unsettled
|
|
|
|
// balance that equals to the amount of invoices * payAmt. The local
|
|
|
|
// balance remains zero.
|
|
|
|
checkChannelBalance(carol, 0, aliceLocal, numInvoices*payAmt, 0)
|
|
|
|
|
2018-12-07 03:49:05 +03:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2019-07-24 04:00:30 +03:00
|
|
|
// padCLTV is a small helper function that pads a cltv value with a block
|
|
|
|
// padding.
|
|
|
|
func padCLTV(cltv uint32) uint32 {
|
|
|
|
return cltv + uint32(routing.BlockPadding)
|
|
|
|
}
|
|
|
|
|
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
|
2020-03-04 15:21:28 +03:00
|
|
|
// htlc timeout transactions, where n is the number of payments Alice attempted
|
2017-10-07 02:58:13 +03:00
|
|
|
// 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) {
|
2020-03-04 15:21:27 +03:00
|
|
|
// We'll test the scenario for some of the commitment types, to ensure
|
|
|
|
// outputs can be swept.
|
|
|
|
commitTypes := []commitType{
|
|
|
|
commitTypeLegacy,
|
2020-03-04 15:21:29 +03:00
|
|
|
commitTypeAnchors,
|
2020-03-04 15:21:27 +03:00
|
|
|
}
|
2020-03-04 15:21:27 +03:00
|
|
|
|
2020-03-04 15:21:27 +03:00
|
|
|
for _, channelType := range commitTypes {
|
|
|
|
testName := fmt.Sprintf("committype=%v", channelType)
|
|
|
|
|
2020-01-10 17:27:50 +03:00
|
|
|
channelType := channelType
|
2020-03-04 15:21:27 +03:00
|
|
|
success := t.t.Run(testName, func(t *testing.T) {
|
|
|
|
ht := newHarnessTest(t, net)
|
|
|
|
|
|
|
|
args := channelType.Args()
|
|
|
|
alice, err := net.NewNode("Alice", args)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, ht, alice)
|
|
|
|
|
|
|
|
// Since we'd like to test failure scenarios with
|
|
|
|
// outstanding htlcs, we'll introduce another node into
|
|
|
|
// our test network: Carol.
|
|
|
|
carolArgs := []string{"--hodl.exit-settle"}
|
|
|
|
carolArgs = append(carolArgs, args...)
|
|
|
|
carol, err := net.NewNode("Carol", carolArgs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, ht, carol)
|
2020-03-04 15:21:27 +03:00
|
|
|
|
2020-03-04 15:21:27 +03:00
|
|
|
// Each time, we'll send Alice new set of coins in
|
|
|
|
// order to fund the channel.
|
|
|
|
ctxt, _ := context.WithTimeout(
|
|
|
|
context.Background(), defaultTimeout,
|
|
|
|
)
|
|
|
|
err = net.SendCoins(
|
|
|
|
ctxt, btcutil.SatoshiPerBitcoin, alice,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to Alice: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2020-03-04 15:21:29 +03:00
|
|
|
// Also give Carol some coins to allow her to sweep her
|
|
|
|
// anchor.
|
|
|
|
err = net.SendCoins(
|
|
|
|
ctxt, btcutil.SatoshiPerBitcoin, carol,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to Alice: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
channelForceClosureTest(
|
|
|
|
net, ht, alice, carol, channelType,
|
|
|
|
)
|
2020-03-04 15:21:27 +03:00
|
|
|
})
|
|
|
|
if !success {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 15:21:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func channelForceClosureTest(net *lntest.NetworkHarness, t *harnessTest,
|
2020-03-04 15:21:29 +03:00
|
|
|
alice, carol *lntest.HarnessNode, channelType commitType) {
|
2020-03-04 15:21:27 +03:00
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2020-09-04 12:19:27 +03:00
|
|
|
const commitFeeRate = 20000
|
|
|
|
net.SetFeeEstimate(commitFeeRate)
|
|
|
|
|
2017-10-07 02:58:13 +03:00
|
|
|
// TODO(roasbeef): should check default value in config here
|
|
|
|
// instead, or make delay a param
|
2020-10-06 21:56:10 +03:00
|
|
|
defaultCLTV := uint32(chainreg.DefaultBitcoinTimeLockDelta)
|
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)
|
2020-03-04 15:21:27 +03:00
|
|
|
if err := net.ConnectNodes(ctxt, 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(
|
2020-03-04 15:21:27 +03:00
|
|
|
ctxt, t, net, alice, carol,
|
2018-08-22 10:32:42 +03:00
|
|
|
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)
|
2020-03-04 15:21:27 +03:00
|
|
|
err = 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.
|
|
|
|
carolPubKey := carol.PubKey[:]
|
2017-10-07 02:58:13 +03:00
|
|
|
for i := 0; i < numInvoices; i++ {
|
2020-03-31 13:44:18 +03:00
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, err := alice.RouterClient.SendPaymentV2(
|
|
|
|
ctx,
|
|
|
|
&routerrpc.SendPaymentRequest{
|
|
|
|
Dest: carolPubKey,
|
|
|
|
Amt: int64(paymentAmt),
|
|
|
|
PaymentHash: makeFakePayHash(t),
|
2020-10-06 21:56:10 +03:00
|
|
|
FinalCltvDelta: chainreg.DefaultBitcoinTimeLockDelta,
|
2020-03-31 13:44:18 +03:00
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
|
|
|
},
|
|
|
|
)
|
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.
|
2020-03-04 15:21:27 +03:00
|
|
|
nodes := []*lntest.HarnessNode{alice, carol}
|
2018-08-14 10:09:02 +03:00
|
|
|
var predErr error
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, numInvoices)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
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
|
2019-07-24 04:00:30 +03:00
|
|
|
htlcExpiryHeight = padCLTV(startHeight + defaultCLTV)
|
|
|
|
htlcCsvMaturityHeight = padCLTV(startHeight + defaultCLTV + 1 + defaultCSV)
|
2017-10-07 02:58:13 +03:00
|
|
|
)
|
|
|
|
|
2020-12-09 14:24:04 +03:00
|
|
|
// If we are dealing with an anchor channel type, the sweeper will
|
|
|
|
// sweep the HTLC second level output one block earlier (than the
|
|
|
|
// nursery that waits an additional block, and handles non-anchor
|
|
|
|
// channels). So we set a maturity height that is one less.
|
|
|
|
if channelType == commitTypeAnchors {
|
|
|
|
htlcCsvMaturityHeight = padCLTV(
|
|
|
|
startHeight + defaultCLTV + defaultCSV,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2020-03-04 15:21:27 +03:00
|
|
|
aliceChan, err := getChanInfo(ctxt, 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.
|
2020-09-04 12:19:27 +03:00
|
|
|
const actualFeeRate = 30000
|
|
|
|
net.SetFeeEstimate(actualFeeRate)
|
|
|
|
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2020-03-04 15:21:27 +03:00
|
|
|
_, closingTxID, err := net.CloseChannel(ctxt, 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)
|
2020-03-04 15:21:27 +03:00
|
|
|
pendingChanResp, err := 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.
|
2021-02-13 11:05:33 +03:00
|
|
|
txid, err := lnrpc.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
|
|
|
|
2020-07-07 11:32:13 +03:00
|
|
|
// Create a map of outpoints to expected resolutions for alice and carol
|
|
|
|
// which we will add reports to as we sweep outputs.
|
|
|
|
var (
|
|
|
|
aliceReports = make(map[string]*lnrpc.Resolution)
|
|
|
|
carolReports = make(map[string]*lnrpc.Resolution)
|
|
|
|
)
|
|
|
|
|
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.
|
2020-03-04 15:21:27 +03:00
|
|
|
if err := net.RestartNode(alice, nil); err != nil {
|
2016-12-14 02:32:44 +03:00
|
|
|
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
|
2020-03-04 15:21:29 +03:00
|
|
|
// broadcast as a result of the force closure. If there are anchors, we
|
|
|
|
// also expect the anchor sweep tx to be in the mempool.
|
|
|
|
expectedTxes := 1
|
2020-09-04 12:19:27 +03:00
|
|
|
expectedFeeRate := commitFeeRate
|
2020-03-04 15:21:29 +03:00
|
|
|
if channelType == commitTypeAnchors {
|
|
|
|
expectedTxes = 2
|
2020-09-04 12:19:27 +03:00
|
|
|
expectedFeeRate = actualFeeRate
|
2020-03-04 15:21:29 +03:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:19:27 +03:00
|
|
|
sweepTxns, err := getNTxsFromMempool(
|
2020-03-04 15:21:29 +03:00
|
|
|
net.Miner.Node, expectedTxes, minerMempoolTimeout,
|
|
|
|
)
|
2018-11-29 14:31:40 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to find commitment in miner mempool: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-09-04 12:19:27 +03:00
|
|
|
// Verify fee rate of the commitment tx plus anchor if present.
|
|
|
|
var totalWeight, totalFee int64
|
|
|
|
for _, tx := range sweepTxns {
|
|
|
|
utx := btcutil.NewTx(tx)
|
|
|
|
totalWeight += blockchain.GetTransactionWeight(utx)
|
|
|
|
|
|
|
|
fee, err := getTxFee(net.Miner.Node, tx)
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
totalFee += int64(fee)
|
|
|
|
}
|
|
|
|
feeRate := totalFee * 1000 / totalWeight
|
|
|
|
|
|
|
|
// Allow some deviation because weight estimates during tx generation
|
|
|
|
// are estimates.
|
|
|
|
require.InEpsilon(t.t, expectedFeeRate, feeRate, 0.005)
|
|
|
|
|
2020-07-07 11:32:13 +03:00
|
|
|
// Find alice's commit sweep and anchor sweep (if present) in the
|
|
|
|
// mempool.
|
|
|
|
aliceCloseTx := waitingClose.Commitments.LocalTxid
|
|
|
|
_, aliceAnchor := findCommitAndAnchor(
|
|
|
|
t, net, sweepTxns, aliceCloseTx,
|
|
|
|
)
|
|
|
|
|
|
|
|
// If we expect anchors, add alice's anchor to our expected set of
|
|
|
|
// reports.
|
|
|
|
if channelType == commitTypeAnchors {
|
|
|
|
aliceReports[aliceAnchor.OutPoint.String()] = &lnrpc.Resolution{
|
|
|
|
ResolutionType: lnrpc.ResolutionType_ANCHOR,
|
|
|
|
Outcome: lnrpc.ResolutionOutcome_CLAIMED,
|
|
|
|
SweepTxid: aliceAnchor.SweepTx,
|
|
|
|
Outpoint: &lnrpc.OutPoint{
|
|
|
|
TxidBytes: aliceAnchor.OutPoint.Hash[:],
|
|
|
|
TxidStr: aliceAnchor.OutPoint.Hash.String(),
|
|
|
|
OutputIndex: aliceAnchor.OutPoint.Index,
|
|
|
|
},
|
|
|
|
AmountSat: uint64(anchorSize),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2020-03-12 12:11:08 +03:00
|
|
|
err = wait.NoError(func() error {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2020-03-04 15:21:27 +03:00
|
|
|
pendingChanResp, err := alice.PendingChannels(
|
2018-08-30 04:00:54 +03:00
|
|
|
ctxt, pendingChansRequest,
|
2018-05-23 20:54:12 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
2020-03-12 12:11:08 +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
|
|
|
|
2020-03-12 12:11:08 +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
|
|
|
|
2020-03-12 12:11:08 +03:00
|
|
|
forceClose, err := findForceClosedChannel(pendingChanResp, &op)
|
|
|
|
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
|
|
|
// Now that the channel has been force closed, it should now
|
|
|
|
// have the height and number of blocks to confirm populated.
|
2020-03-12 12:11:08 +03:00
|
|
|
err = checkCommitmentMaturity(
|
2018-05-23 20:54:12 +03:00
|
|
|
forceClose, commCsvMaturityHeight, int32(defaultCSV),
|
|
|
|
)
|
2020-03-12 12:11:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-05-23 20:54:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// None of our outputs have been swept, so they should all be in
|
2020-03-04 15:21:29 +03:00
|
|
|
// limbo. For anchors, we expect the anchor amount to be
|
|
|
|
// recovered.
|
2018-05-23 20:54:12 +03:00
|
|
|
if forceClose.LimboBalance == 0 {
|
2020-03-12 12:11:08 +03:00
|
|
|
return errors.New("all funds should still be in " +
|
2018-05-23 20:54:12 +03:00
|
|
|
"limbo")
|
|
|
|
}
|
2020-03-04 15:21:29 +03:00
|
|
|
expectedRecoveredBalance := int64(0)
|
|
|
|
if channelType == commitTypeAnchors {
|
|
|
|
expectedRecoveredBalance = anchorSize
|
|
|
|
}
|
|
|
|
if forceClose.RecoveredBalance != expectedRecoveredBalance {
|
2020-03-12 12:11:08 +03:00
|
|
|
return errors.New("no funds should yet be shown " +
|
2018-05-23 20:54:12 +03:00
|
|
|
"as recovered")
|
|
|
|
}
|
|
|
|
|
2020-03-12 12:11:08 +03:00
|
|
|
return nil
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-05-23 20:54:12 +03:00
|
|
|
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.)
|
2020-03-04 15:21:27 +03:00
|
|
|
if err := net.RestartNode(alice, nil); err != nil {
|
2016-12-14 02:32:44 +03:00
|
|
|
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
|
2020-03-04 15:21:29 +03:00
|
|
|
// not timelocked. If there are anchors, we also expect Carol's anchor
|
|
|
|
// sweep now.
|
2020-09-04 12:19:27 +03:00
|
|
|
sweepTxns, err = getNTxsFromMempool(
|
2020-03-04 15:21:29 +03:00
|
|
|
net.Miner.Node, expectedTxes, minerMempoolTimeout,
|
|
|
|
)
|
2018-11-29 14:31:40 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to find Carol's sweep in miner mempool: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2020-07-07 11:32:13 +03:00
|
|
|
// We look up the sweep txns we have found in mempool and create
|
|
|
|
// expected resolutions for carol.
|
|
|
|
carolCommit, carolAnchor := findCommitAndAnchor(
|
|
|
|
t, net, sweepTxns, aliceCloseTx,
|
|
|
|
)
|
|
|
|
|
|
|
|
// If we have anchors, add an anchor resolution for carol.
|
|
|
|
if channelType == commitTypeAnchors {
|
|
|
|
carolReports[carolAnchor.OutPoint.String()] = &lnrpc.Resolution{
|
|
|
|
ResolutionType: lnrpc.ResolutionType_ANCHOR,
|
|
|
|
Outcome: lnrpc.ResolutionOutcome_CLAIMED,
|
|
|
|
SweepTxid: carolAnchor.SweepTx,
|
|
|
|
AmountSat: anchorSize,
|
|
|
|
Outpoint: &lnrpc.OutPoint{
|
|
|
|
TxidBytes: carolAnchor.OutPoint.Hash[:],
|
|
|
|
TxidStr: carolAnchor.OutPoint.Hash.String(),
|
|
|
|
OutputIndex: carolAnchor.OutPoint.Index,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 05:00:47 +03:00
|
|
|
// Currently within the codebase, the default CSV is 4 relative blocks.
|
2019-12-17 17:28:44 +03:00
|
|
|
// For the persistence test, we generate two blocks, then trigger
|
2016-12-14 02:32:44 +03:00
|
|
|
// a restart and then generate the final block that should trigger
|
|
|
|
// the creation of the sweep transaction.
|
2019-12-17 17:28:44 +03:00
|
|
|
if _, err := net.Miner.Node.Generate(defaultCSV - 2); err != nil {
|
2016-12-14 02:32:44 +03:00
|
|
|
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.
|
2020-03-04 15:21:27 +03:00
|
|
|
if err := net.RestartNode(alice, nil); err != nil {
|
2016-12-14 02:32:44 +03:00
|
|
|
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.
|
2020-07-07 11:32:13 +03:00
|
|
|
var aliceBalance int64
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.NoError(func() error {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2020-03-04 15:21:27 +03:00
|
|
|
pendingChanResp, err := 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
|
|
|
|
2020-07-07 11:32:13 +03:00
|
|
|
// Make a record of the balances we expect for alice and carol.
|
|
|
|
aliceBalance = forceClose.Channel.LocalBalance
|
|
|
|
|
2018-05-23 20:54:12 +03:00
|
|
|
// At this point, the nursery should show that the commitment
|
2019-12-17 17:28:44 +03:00
|
|
|
// output has 2 block left before its CSV delay expires. In
|
2018-05-23 20:54:12 +03:00
|
|
|
// 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(
|
2019-12-17 17:28:44 +03:00
|
|
|
forceClose, commCsvMaturityHeight, 2,
|
2018-05-23 20:54:12 +03:00
|
|
|
)
|
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")
|
|
|
|
}
|
2020-03-04 15:21:29 +03:00
|
|
|
expectedRecoveredBalance := int64(0)
|
|
|
|
if channelType == commitTypeAnchors {
|
|
|
|
expectedRecoveredBalance = anchorSize
|
|
|
|
}
|
|
|
|
if forceClose.RecoveredBalance != expectedRecoveredBalance {
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-05-23 20:54:12 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-17 17:28:44 +03:00
|
|
|
// At this point, the CSV will expire in the next block, meaning that
|
|
|
|
// the sweeping transaction should now be broadcast. So we fetch the
|
|
|
|
// node's mempool to ensure it has been properly 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 11:32:13 +03:00
|
|
|
// We expect a resolution which spends our commit output.
|
|
|
|
output := sweepTx.MsgTx().TxIn[0].PreviousOutPoint
|
|
|
|
aliceReports[output.String()] = &lnrpc.Resolution{
|
|
|
|
ResolutionType: lnrpc.ResolutionType_COMMIT,
|
|
|
|
Outcome: lnrpc.ResolutionOutcome_CLAIMED,
|
|
|
|
SweepTxid: sweepingTXID.String(),
|
|
|
|
Outpoint: &lnrpc.OutPoint{
|
|
|
|
TxidBytes: output.Hash[:],
|
|
|
|
TxidStr: output.Hash.String(),
|
|
|
|
OutputIndex: output.Index,
|
|
|
|
},
|
|
|
|
AmountSat: uint64(aliceBalance),
|
|
|
|
}
|
|
|
|
|
|
|
|
carolReports[carolCommit.OutPoint.String()] = &lnrpc.Resolution{
|
|
|
|
ResolutionType: lnrpc.ResolutionType_COMMIT,
|
|
|
|
Outcome: lnrpc.ResolutionOutcome_CLAIMED,
|
|
|
|
Outpoint: &lnrpc.OutPoint{
|
|
|
|
TxidBytes: carolCommit.OutPoint.Hash[:],
|
|
|
|
TxidStr: carolCommit.OutPoint.Hash.String(),
|
|
|
|
OutputIndex: carolCommit.OutPoint.Index,
|
|
|
|
},
|
|
|
|
AmountSat: uint64(pushAmt),
|
|
|
|
SweepTxid: carolCommit.SweepTx,
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:46:26 +03:00
|
|
|
// Check that we can find the commitment sweep in our set of known
|
2020-11-13 14:59:52 +03:00
|
|
|
// sweeps, using the simple transaction id ListSweeps output.
|
|
|
|
assertSweepFound(ctxb, t.t, alice, sweepingTXID.String(), false)
|
2020-05-05 22:46:26 +03:00
|
|
|
|
2017-10-07 02:58:13 +03:00
|
|
|
// Restart Alice to ensure that she resumes watching the finalized
|
|
|
|
// commitment sweep txid.
|
2020-03-04 15:21:27 +03:00
|
|
|
if err := net.RestartNode(alice, nil); err != nil {
|
2017-10-07 02:58:13 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-08-30 04:00:54 +03:00
|
|
|
// 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)
|
2020-03-04 15:21:27 +03:00
|
|
|
pendingChanResp, err := alice.PendingChannels(
|
2018-08-30 04:00:54 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
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
|
2019-07-24 04:00:30 +03:00
|
|
|
// height, and add the block padding.
|
2019-12-17 17:28:44 +03:00
|
|
|
cltvHeightDelta := padCLTV(defaultCLTV - defaultCSV - 1 - 1)
|
2017-10-07 02:58:13 +03:00
|
|
|
|
|
|
|
// 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.
|
2020-03-04 15:21:27 +03:00
|
|
|
if err := net.RestartNode(alice, nil); err != nil {
|
2017-10-07 02:58:13 +03:00
|
|
|
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-09-19 22:46:29 +03:00
|
|
|
err = wait.NoError(func() error {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2020-03-04 15:21:27 +03:00
|
|
|
pendingChanResp, err := 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
|
|
|
// 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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-05-23 20:54:12 +03:00
|
|
|
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
|
2020-12-09 14:24:04 +03:00
|
|
|
// one.
|
|
|
|
expectedTxes = numInvoices
|
|
|
|
|
|
|
|
// In case of anchors, the timeout txs will be aggregated into one.
|
|
|
|
if channelType == commitTypeAnchors {
|
|
|
|
expectedTxes = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for them all to show up in the mempool.
|
|
|
|
htlcTxIDs, err := waitForNTxsInMempool(
|
|
|
|
net.Miner.Node, expectedTxes, 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
|
2020-12-09 14:24:04 +03:00
|
|
|
// output, and that that output is from the commitment txn. In case
|
|
|
|
// this is an anchor channel, the transactions are aggregated by the
|
|
|
|
// sweeper into one.
|
|
|
|
numInputs := 1
|
|
|
|
if channelType == commitTypeAnchors {
|
|
|
|
numInputs = numInvoices + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a map of the already confirmed htlc timeout outpoints,
|
|
|
|
// 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 htlcTxOutpointSet = make(map[wire.OutPoint]int)
|
|
|
|
|
2020-07-07 11:32:13 +03:00
|
|
|
var htlcLessFees uint64
|
2017-10-07 02:58:13 +03:00
|
|
|
for _, htlcTxID := range htlcTxIDs {
|
|
|
|
// Fetch the sweep transaction, all input it's spending should
|
|
|
|
// be from the commitment transaction which was broadcast
|
2020-12-09 14:24:04 +03:00
|
|
|
// on-chain. In case of an anchor type channel, we expect one
|
|
|
|
// extra input that is not spending from the commitment, that
|
|
|
|
// is added for fees.
|
2017-10-07 02:58:13 +03:00
|
|
|
htlcTx, err := net.Miner.Node.GetRawTransaction(htlcTxID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch sweep tx: %v", err)
|
|
|
|
}
|
2020-12-09 14:24:04 +03:00
|
|
|
|
|
|
|
// Ensure the htlc transaction has the expected number of
|
|
|
|
// inputs.
|
2020-07-07 11:32:13 +03:00
|
|
|
inputs := htlcTx.MsgTx().TxIn
|
2020-12-09 14:24:04 +03:00
|
|
|
if len(inputs) != numInputs {
|
|
|
|
t.Fatalf("htlc transaction should only have %d txin, "+
|
|
|
|
"has %d", numInputs, len(htlcTx.MsgTx().TxIn))
|
2017-10-07 02:58:13 +03:00
|
|
|
}
|
2020-07-07 11:32:13 +03:00
|
|
|
|
2020-12-09 14:24:04 +03:00
|
|
|
// The number of outputs should be the same.
|
2020-07-07 11:32:13 +03:00
|
|
|
outputs := htlcTx.MsgTx().TxOut
|
2020-12-09 14:24:04 +03:00
|
|
|
if len(outputs) != numInputs {
|
|
|
|
t.Fatalf("htlc transaction should only have %d"+
|
|
|
|
"txout, has: %v", numInputs, len(outputs))
|
2020-07-07 11:32:13 +03:00
|
|
|
}
|
|
|
|
|
2020-12-09 14:24:04 +03:00
|
|
|
// Ensure all the htlc transaction inputs are spending from the
|
|
|
|
// commitment transaction, except if this is an extra input
|
|
|
|
// added to pay for fees for anchor channels.
|
|
|
|
nonCommitmentInputs := 0
|
|
|
|
for i, txIn := range inputs {
|
|
|
|
if !closingTxID.IsEqual(&txIn.PreviousOutPoint.Hash) {
|
|
|
|
nonCommitmentInputs++
|
|
|
|
|
|
|
|
if nonCommitmentInputs > 1 {
|
|
|
|
t.Fatalf("htlc transaction not "+
|
|
|
|
"spending from commit "+
|
|
|
|
"tx %v, instead spending %v",
|
|
|
|
closingTxID,
|
|
|
|
txIn.PreviousOutPoint)
|
|
|
|
}
|
2020-07-07 11:32:13 +03:00
|
|
|
|
2020-12-09 14:24:04 +03:00
|
|
|
// This was an extra input added to pay fees,
|
|
|
|
// continue to the next one.
|
|
|
|
continue
|
|
|
|
}
|
2020-07-07 11:32:13 +03:00
|
|
|
|
2020-12-09 14:24:04 +03:00
|
|
|
// For each htlc timeout transaction, we expect a
|
|
|
|
// resolver report recording this on chain resolution
|
|
|
|
// for both alice and carol.
|
|
|
|
outpoint := txIn.PreviousOutPoint
|
|
|
|
resolutionOutpoint := &lnrpc.OutPoint{
|
|
|
|
TxidBytes: outpoint.Hash[:],
|
|
|
|
TxidStr: outpoint.Hash.String(),
|
|
|
|
OutputIndex: outpoint.Index,
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect alice to have a timeout tx resolution with
|
|
|
|
// an amount equal to the payment amount.
|
|
|
|
aliceReports[outpoint.String()] = &lnrpc.Resolution{
|
|
|
|
ResolutionType: lnrpc.ResolutionType_OUTGOING_HTLC,
|
|
|
|
Outcome: lnrpc.ResolutionOutcome_FIRST_STAGE,
|
|
|
|
SweepTxid: htlcTx.Hash().String(),
|
|
|
|
Outpoint: resolutionOutpoint,
|
|
|
|
AmountSat: uint64(paymentAmt),
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect carol to have a resolution with an
|
|
|
|
// incoming htlc timeout which reflects the full amount
|
|
|
|
// of the htlc. It has no spend tx, because carol stops
|
|
|
|
// monitoring the htlc once it has timed out.
|
|
|
|
carolReports[outpoint.String()] = &lnrpc.Resolution{
|
|
|
|
ResolutionType: lnrpc.ResolutionType_INCOMING_HTLC,
|
|
|
|
Outcome: lnrpc.ResolutionOutcome_TIMEOUT,
|
|
|
|
SweepTxid: "",
|
|
|
|
Outpoint: resolutionOutpoint,
|
|
|
|
AmountSat: uint64(paymentAmt),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recorf the HTLC outpoint, such that we can later
|
|
|
|
// check whether it gets swept
|
|
|
|
op := wire.OutPoint{
|
|
|
|
Hash: *htlcTxID,
|
|
|
|
Index: uint32(i),
|
|
|
|
}
|
|
|
|
htlcTxOutpointSet[op] = 0
|
2020-07-07 11:32:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We record the htlc amount less fees here, so that we know
|
|
|
|
// what value to expect for the second stage of our htlc
|
|
|
|
// htlc resolution.
|
|
|
|
htlcLessFees = uint64(outputs[0].Value)
|
2017-10-07 02:58:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2020-03-04 15:21:27 +03:00
|
|
|
if err := net.RestartNode(alice, nil); err != nil {
|
2017-10-07 02:58:13 +03:00
|
|
|
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.
|
2020-03-04 15:21:27 +03:00
|
|
|
if err := net.RestartNode(alice, nil); err != nil {
|
2017-10-07 02:58:13 +03:00
|
|
|
t.Fatalf("Node restart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance the chain until just before the 2nd-layer CSV delays expire.
|
2020-12-09 14:24:04 +03:00
|
|
|
// For anchor channels thhis is one block earlier.
|
|
|
|
numBlocks := uint32(defaultCSV - 1)
|
|
|
|
if channelType == commitTypeAnchors {
|
|
|
|
numBlocks = defaultCSV - 2
|
|
|
|
|
|
|
|
}
|
|
|
|
_, err = net.Miner.Node.Generate(numBlocks)
|
2017-10-07 02:58:13 +03:00
|
|
|
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.
|
2020-03-04 15:21:27 +03:00
|
|
|
if err := net.RestartNode(alice, nil); err != nil {
|
2017-10-07 02:58:13 +03:00
|
|
|
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.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2020-03-04 15:21:27 +03:00
|
|
|
pendingChanResp, err = alice.PendingChannels(
|
2018-08-30 04:00:54 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|
2020-07-07 11:32:13 +03:00
|
|
|
outputCount := len(htlcSweepTx.MsgTx().TxOut)
|
|
|
|
if outputCount != 1 {
|
|
|
|
t.Fatalf("htlc sweep transaction should have one output, has: "+
|
|
|
|
"%v", outputCount)
|
|
|
|
}
|
|
|
|
|
2020-12-09 14:24:04 +03:00
|
|
|
// Ensure that each output spends from exactly one htlc timeout output.
|
2017-10-07 02:58:13 +03:00
|
|
|
for _, txIn := range htlcSweepTx.MsgTx().TxIn {
|
2020-12-09 14:24:04 +03:00
|
|
|
outpoint := txIn.PreviousOutPoint
|
2017-10-07 02:58:13 +03:00
|
|
|
// Check that the input is a confirmed htlc timeout txn.
|
2020-12-09 14:24:04 +03:00
|
|
|
if _, ok := htlcTxOutpointSet[outpoint]; !ok {
|
2017-10-07 02:58:13 +03:00
|
|
|
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.
|
2020-12-09 14:24:04 +03:00
|
|
|
htlcTxOutpointSet[outpoint]++
|
2017-10-07 02:58:13 +03:00
|
|
|
|
|
|
|
// Check that each is only spent once.
|
2020-12-09 14:24:04 +03:00
|
|
|
if htlcTxOutpointSet[outpoint] > 1 {
|
2017-10-07 02:58:13 +03:00
|
|
|
t.Fatalf("htlc sweep tx has multiple spends from "+
|
|
|
|
"outpoint %v", outpoint)
|
|
|
|
}
|
2020-07-07 11:32:13 +03:00
|
|
|
|
|
|
|
// Since we have now swept our htlc timeout tx, we expect to
|
|
|
|
// have timeout resolutions for each of our htlcs.
|
|
|
|
output := txIn.PreviousOutPoint
|
|
|
|
aliceReports[output.String()] = &lnrpc.Resolution{
|
|
|
|
ResolutionType: lnrpc.ResolutionType_OUTGOING_HTLC,
|
|
|
|
Outcome: lnrpc.ResolutionOutcome_TIMEOUT,
|
|
|
|
SweepTxid: htlcSweepTx.Hash().String(),
|
|
|
|
Outpoint: &lnrpc.OutPoint{
|
|
|
|
TxidBytes: output.Hash[:],
|
|
|
|
TxidStr: output.Hash.String(),
|
|
|
|
OutputIndex: output.Index,
|
|
|
|
},
|
2020-01-10 17:27:50 +03:00
|
|
|
AmountSat: htlcLessFees,
|
2020-07-07 11:32:13 +03:00
|
|
|
}
|
2017-10-07 02:58:13 +03:00
|
|
|
}
|
|
|
|
|
2020-12-09 14:24:04 +03:00
|
|
|
// Check that each HTLC output was spent exactly onece.
|
|
|
|
for op, num := range htlcTxOutpointSet {
|
|
|
|
if num != 1 {
|
|
|
|
t.Fatalf("HTLC outpoint %v was spent %v times", op, num)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 14:59:52 +03:00
|
|
|
// Check that we can find the htlc sweep in our set of sweeps using
|
|
|
|
// the verbose output of the listsweeps output.
|
|
|
|
assertSweepFound(ctxb, t.t, alice, htlcSweepTx.Hash().String(), true)
|
2020-05-05 22:46:26 +03:00
|
|
|
|
2017-10-07 02:58:13 +03:00
|
|
|
// 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.
|
2020-03-04 15:21:27 +03:00
|
|
|
if err := net.RestartNode(alice, nil); err != nil {
|
2017-10-07 02:58:13 +03:00
|
|
|
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.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2020-03-04 15:21:27 +03:00
|
|
|
pendingChanResp, err := alice.PendingChannels(
|
2018-08-30 04:00:54 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
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.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2020-03-04 15:21:27 +03:00
|
|
|
pendingChanResp, err := 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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-05-23 20:54:12 +03:00
|
|
|
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
|
|
|
}
|
2020-07-07 11:32:13 +03:00
|
|
|
|
|
|
|
// Finally, we check that alice and carol have the set of resolutions
|
|
|
|
// we expect.
|
|
|
|
assertReports(ctxb, t, alice, op, aliceReports)
|
|
|
|
assertReports(ctxb, t, carol, op, carolReports)
|
|
|
|
}
|
|
|
|
|
|
|
|
type sweptOutput struct {
|
|
|
|
OutPoint wire.OutPoint
|
|
|
|
SweepTx string
|
|
|
|
}
|
|
|
|
|
|
|
|
// findCommitAndAnchor looks for a commitment sweep and anchor sweep in the
|
|
|
|
// mempool. Our anchor output is identified by having multiple inputs, because
|
|
|
|
// we have to bring another input to add fees to the anchor. Note that the
|
|
|
|
// anchor swept output may be nil if the channel did not have anchors.
|
|
|
|
func findCommitAndAnchor(t *harnessTest, net *lntest.NetworkHarness,
|
2020-09-04 12:19:27 +03:00
|
|
|
sweepTxns []*wire.MsgTx, closeTx string) (*sweptOutput, *sweptOutput) {
|
2020-07-07 11:32:13 +03:00
|
|
|
|
|
|
|
var commitSweep, anchorSweep *sweptOutput
|
|
|
|
|
|
|
|
for _, tx := range sweepTxns {
|
2020-09-04 12:19:27 +03:00
|
|
|
txHash := tx.TxHash()
|
|
|
|
sweepTx, err := net.Miner.Node.GetRawTransaction(&txHash)
|
2020-07-07 11:32:13 +03:00
|
|
|
require.NoError(t.t, err)
|
|
|
|
|
|
|
|
// We expect our commitment sweep to have a single input, and,
|
|
|
|
// our anchor sweep to have more inputs (because the wallet
|
|
|
|
// needs to add balance to the anchor amount). We find their
|
|
|
|
// sweep txids here to setup appropriate resolutions. We also
|
|
|
|
// need to find the outpoint for our resolution, which we do by
|
|
|
|
// matching the inputs to the sweep to the close transaction.
|
|
|
|
inputs := sweepTx.MsgTx().TxIn
|
|
|
|
if len(inputs) == 1 {
|
|
|
|
commitSweep = &sweptOutput{
|
|
|
|
OutPoint: inputs[0].PreviousOutPoint,
|
2020-09-04 12:19:27 +03:00
|
|
|
SweepTx: txHash.String(),
|
2020-07-07 11:32:13 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Since we have more than one input, we run through
|
|
|
|
// them to find the outpoint that spends from the close
|
|
|
|
// tx. This will be our anchor output.
|
|
|
|
for _, txin := range inputs {
|
|
|
|
outpointStr := txin.PreviousOutPoint.Hash.String()
|
|
|
|
if outpointStr == closeTx {
|
|
|
|
anchorSweep = &sweptOutput{
|
|
|
|
OutPoint: txin.PreviousOutPoint,
|
2020-09-04 12:19:27 +03:00
|
|
|
SweepTx: txHash.String(),
|
2020-07-07 11:32:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return commitSweep, anchorSweep
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertReports checks that the count of resolutions we have present per
|
|
|
|
// type matches a set of expected resolutions.
|
|
|
|
func assertReports(ctxb context.Context, t *harnessTest,
|
|
|
|
node *lntest.HarnessNode, channelPoint wire.OutPoint,
|
|
|
|
expected map[string]*lnrpc.Resolution) {
|
|
|
|
|
|
|
|
// Get our node's closed channels.
|
|
|
|
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
closed, err := node.ClosedChannels(
|
|
|
|
ctxt, &lnrpc.ClosedChannelsRequest{},
|
|
|
|
)
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
|
|
|
|
var resolutions []*lnrpc.Resolution
|
|
|
|
for _, close := range closed.Channels {
|
|
|
|
if close.ChannelPoint == channelPoint.String() {
|
|
|
|
resolutions = close.Resolutions
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NotNil(t.t, resolutions)
|
|
|
|
require.Equal(t.t, len(expected), len(resolutions))
|
|
|
|
|
|
|
|
for _, res := range resolutions {
|
|
|
|
outPointStr := fmt.Sprintf("%v:%v", res.Outpoint.TxidStr,
|
|
|
|
res.Outpoint.OutputIndex)
|
|
|
|
|
|
|
|
expected, ok := expected[outPointStr]
|
|
|
|
require.True(t.t, ok)
|
|
|
|
require.Equal(t.t, expected, res)
|
|
|
|
}
|
2016-09-14 05:00:47 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 14:59:52 +03:00
|
|
|
// assertSweepFound looks up a sweep in a nodes list of broadcast sweeps.
|
|
|
|
func assertSweepFound(ctx context.Context, t *testing.T, node *lntest.HarnessNode,
|
|
|
|
sweep string, verbose bool) {
|
2020-05-05 22:46:26 +03:00
|
|
|
|
|
|
|
// List all sweeps that alice's node had broadcast.
|
|
|
|
ctx, _ = context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
sweepResp, err := node.WalletKitClient.ListSweeps(
|
|
|
|
ctx, &walletrpc.ListSweepsRequest{
|
2020-11-13 14:59:52 +03:00
|
|
|
Verbose: verbose,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2020-05-05 22:46:26 +03:00
|
|
|
|
2020-11-13 14:59:52 +03:00
|
|
|
var found bool
|
|
|
|
if verbose {
|
|
|
|
found = findSweepInDetails(t, sweep, sweepResp)
|
|
|
|
} else {
|
|
|
|
found = findSweepInTxids(t, sweep, sweepResp)
|
2020-05-05 22:46:26 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 14:59:52 +03:00
|
|
|
require.True(t, found, "sweep: %v not found", sweep)
|
|
|
|
}
|
|
|
|
|
|
|
|
func findSweepInTxids(t *testing.T, sweepTxid string,
|
|
|
|
sweepResp *walletrpc.ListSweepsResponse) bool {
|
|
|
|
|
|
|
|
sweepTxIDs := sweepResp.GetTransactionIds()
|
|
|
|
require.NotNil(t, sweepTxIDs, "expected transaction ids")
|
|
|
|
require.Nil(t, sweepResp.GetTransactionDetails())
|
|
|
|
|
2020-05-05 22:46:26 +03:00
|
|
|
// Check that the sweep tx we have just produced is present.
|
2020-11-13 14:59:52 +03:00
|
|
|
for _, tx := range sweepTxIDs.TransactionIds {
|
|
|
|
if tx == sweepTxid {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func findSweepInDetails(t *testing.T, sweepTxid string,
|
|
|
|
sweepResp *walletrpc.ListSweepsResponse) bool {
|
|
|
|
|
|
|
|
sweepDetails := sweepResp.GetTransactionDetails()
|
|
|
|
require.NotNil(t, sweepDetails, "expected transaction details")
|
|
|
|
require.Nil(t, sweepResp.GetTransactionIds())
|
|
|
|
|
|
|
|
for _, tx := range sweepDetails.Transactions {
|
|
|
|
if tx.TxHash == sweepTxid {
|
|
|
|
return true
|
2020-05-05 22:46:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 14:59:52 +03:00
|
|
|
return false
|
2020-05-05 22:46:26 +03:00
|
|
|
}
|
|
|
|
|
2019-12-19 10:57:12 +03:00
|
|
|
// assertAmountSent generates a closure which queries listchannels for sndr and
|
|
|
|
// rcvr, and asserts that sndr sent amt satoshis, and that rcvr received amt
|
|
|
|
// satoshis.
|
|
|
|
//
|
|
|
|
// NOTE: This method assumes that each node only has one channel, and it is the
|
|
|
|
// channel used to send the payment.
|
|
|
|
func assertAmountSent(amt btcutil.Amount, sndr, rcvr *lntest.HarnessNode) func() error {
|
|
|
|
return func() error {
|
|
|
|
// Both channels should also have properly accounted from the
|
|
|
|
// amount that has been sent/received over the channel.
|
|
|
|
listReq := &lnrpc.ListChannelsRequest{}
|
|
|
|
ctxb := context.Background()
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
sndrListChannels, err := sndr.ListChannels(ctxt, listReq)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to query for %s's channel "+
|
|
|
|
"list: %v", sndr.Name(), err)
|
|
|
|
}
|
|
|
|
sndrSatoshisSent := sndrListChannels.Channels[0].TotalSatoshisSent
|
|
|
|
if sndrSatoshisSent != int64(amt) {
|
|
|
|
return fmt.Errorf("%s's satoshis sent is incorrect "+
|
|
|
|
"got %v, expected %v", sndr.Name(),
|
|
|
|
sndrSatoshisSent, amt)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
rcvrListChannels, err := rcvr.ListChannels(ctxt, listReq)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to query for %s's channel "+
|
|
|
|
"list: %v", rcvr.Name(), err)
|
|
|
|
}
|
|
|
|
rcvrSatoshisReceived := rcvrListChannels.Channels[0].TotalSatoshisReceived
|
|
|
|
if rcvrSatoshisReceived != int64(amt) {
|
|
|
|
return fmt.Errorf("%s's satoshis received is "+
|
|
|
|
"incorrect got %v, expected %v", rcvr.Name(),
|
|
|
|
rcvrSatoshisReceived, amt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:13:25 +03:00
|
|
|
// assertLastHTLCError checks that the last sent HTLC of the last payment sent
|
|
|
|
// by the given node failed with the expected failure code.
|
|
|
|
func assertLastHTLCError(t *harnessTest, node *lntest.HarnessNode,
|
|
|
|
code lnrpc.Failure_FailureCode) {
|
|
|
|
|
|
|
|
req := &lnrpc.ListPaymentsRequest{
|
|
|
|
IncludeIncomplete: true,
|
|
|
|
}
|
|
|
|
ctxt, _ := context.WithTimeout(context.Background(), defaultTimeout)
|
|
|
|
paymentsResp, err := node.ListPayments(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error when obtaining payments: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
payments := paymentsResp.Payments
|
|
|
|
if len(payments) == 0 {
|
|
|
|
t.Fatalf("no payments found")
|
|
|
|
}
|
|
|
|
|
|
|
|
payment := payments[len(payments)-1]
|
|
|
|
htlcs := payment.Htlcs
|
|
|
|
if len(htlcs) == 0 {
|
|
|
|
t.Fatalf("no htlcs")
|
|
|
|
}
|
|
|
|
|
|
|
|
htlc := htlcs[len(htlcs)-1]
|
|
|
|
if htlc.Failure == nil {
|
|
|
|
t.Fatalf("expected failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
if htlc.Failure.Code != code {
|
|
|
|
t.Fatalf("expected failure %v, got %v", code, htlc.Failure.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2019-08-14 20:57:31 +03:00
|
|
|
dave, err := net.NewNode("Dave", []string{"--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
|
2019-11-20 07:13:04 +03:00
|
|
|
// Dave. Carol is started in both unsafe-replay 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
|
|
|
|
2020-04-13 18:29:52 +03:00
|
|
|
// Next, we'll create Fred who is going to initiate the payment and
|
|
|
|
// establish a channel to from him to Carol. We can't perform this test
|
|
|
|
// by paying from Carol directly to Dave, because the '--unsafe-replay'
|
|
|
|
// setup doesn't apply to locally added htlcs. In that case, the
|
|
|
|
// mailbox, that is responsible for generating the replay, is bypassed.
|
|
|
|
fred, err := net.NewNode("Fred", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, fred)
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, fred, carol); err != nil {
|
|
|
|
t.Fatalf("unable to connect fred to carol: %v", err)
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, fred)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to fred: %v", err)
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
|
|
|
chanPointFC := openChannelAndAssert(
|
|
|
|
ctxt, t, net, fred, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:29:52 +03:00
|
|
|
// Wait for all channels to be recognized and advertized.
|
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)
|
|
|
|
}
|
2020-04-13 18:29:52 +03:00
|
|
|
err = carol.WaitForNetworkChannelOpen(ctxt, chanPointFC)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
err = fred.WaitForNetworkChannelOpen(ctxt, chanPointFC)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2018-02-23 01:29:06 +03:00
|
|
|
|
2020-04-13 18:29:52 +03:00
|
|
|
// With the invoice for Dave added, send a payment from Fred paying
|
2018-02-23 01:29:06 +03:00
|
|
|
// to the above generated invoice.
|
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
payStream, err := fred.RouterClient.SendPaymentV2(
|
|
|
|
ctx,
|
|
|
|
&routerrpc.SendPaymentRequest{
|
|
|
|
PaymentRequest: invoiceResp.PaymentRequest,
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
|
|
|
},
|
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to open payment stream: %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.
|
2019-12-19 10:57:12 +03:00
|
|
|
err = wait.InvariantNoError(
|
|
|
|
assertAmountSent(0, carol, dave), 3*time.Second,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
2018-02-23 01:29:06 +03:00
|
|
|
|
|
|
|
// 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.
|
2020-03-31 13:44:18 +03:00
|
|
|
result, err := getPaymentResult(payStream)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to receive payment response: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:29:52 +03:00
|
|
|
// Assert that Fred receives the expected failure after Carol sent a
|
|
|
|
// duplicate packet that fails due to sphinx replay detection.
|
2020-03-31 13:44:18 +03:00
|
|
|
if result.Status == lnrpc.Payment_SUCCEEDED {
|
2020-04-01 01:13:25 +03:00
|
|
|
t.Fatalf("expected payment error")
|
2018-02-23 01:29:06 +03:00
|
|
|
}
|
2020-04-13 18:29:52 +03:00
|
|
|
assertLastHTLCError(t, fred, lnrpc.Failure_INVALID_ONION_KEY)
|
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.
|
2019-12-19 10:57:12 +03:00
|
|
|
err = wait.InvariantNoError(
|
|
|
|
assertAmountSent(0, carol, dave), 3*time.Second,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
2018-02-23 01:29:06 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-10 15:21:13 +03:00
|
|
|
func assertChannelConstraintsEqual(
|
|
|
|
t *harnessTest, want, got *lnrpc.ChannelConstraints) {
|
|
|
|
|
2020-08-13 01:45:20 +03:00
|
|
|
t.t.Helper()
|
|
|
|
|
2020-06-10 15:21:13 +03:00
|
|
|
if want.CsvDelay != got.CsvDelay {
|
|
|
|
t.Fatalf("CsvDelay mismatched, want: %v, got: %v",
|
|
|
|
want.CsvDelay, got.CsvDelay,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if want.ChanReserveSat != got.ChanReserveSat {
|
|
|
|
t.Fatalf("ChanReserveSat mismatched, want: %v, got: %v",
|
|
|
|
want.ChanReserveSat, got.ChanReserveSat,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if want.DustLimitSat != got.DustLimitSat {
|
|
|
|
t.Fatalf("DustLimitSat mismatched, want: %v, got: %v",
|
|
|
|
want.DustLimitSat, got.DustLimitSat,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if want.MaxPendingAmtMsat != got.MaxPendingAmtMsat {
|
|
|
|
t.Fatalf("MaxPendingAmtMsat mismatched, want: %v, got: %v",
|
|
|
|
want.MaxPendingAmtMsat, got.MaxPendingAmtMsat,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if want.MinHtlcMsat != got.MinHtlcMsat {
|
|
|
|
t.Fatalf("MinHtlcMsat mismatched, want: %v, got: %v",
|
|
|
|
want.MinHtlcMsat, got.MinHtlcMsat,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if want.MaxAcceptedHtlcs != got.MaxAcceptedHtlcs {
|
|
|
|
t.Fatalf("MaxAcceptedHtlcs mismatched, want: %v, got: %v",
|
|
|
|
want.MaxAcceptedHtlcs, got.MaxAcceptedHtlcs,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// testListChannels checks that the response from ListChannels is correct. It
|
|
|
|
// tests the values in all ChannelConstraints are returned as expected. Once
|
|
|
|
// ListChannels becomes mature, a test against all fields in ListChannels should
|
|
|
|
// be performed.
|
|
|
|
func testListChannels(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
2020-08-13 01:45:57 +03:00
|
|
|
const aliceRemoteMaxHtlcs = 50
|
|
|
|
const bobRemoteMaxHtlcs = 100
|
|
|
|
|
2020-06-10 15:21:13 +03:00
|
|
|
// Create two fresh nodes and open a channel between them.
|
2020-08-13 01:46:17 +03:00
|
|
|
alice, err := net.NewNode("Alice", nil)
|
2020-06-10 15:21:13 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, alice)
|
|
|
|
|
2020-08-13 01:45:57 +03:00
|
|
|
bob, err := net.NewNode("Bob", []string{
|
|
|
|
fmt.Sprintf("--default-remote-max-htlcs=%v", bobRemoteMaxHtlcs),
|
|
|
|
})
|
2020-06-10 15:21:13 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, bob)
|
|
|
|
|
|
|
|
// Connect Alice to Bob.
|
|
|
|
if err := net.ConnectNodes(ctxb, alice, bob); err != nil {
|
|
|
|
t.Fatalf("unable to connect alice to bob: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give Alice some coins so she can fund a channel.
|
2020-01-10 17:27:50 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2020-06-10 15:21:13 +03:00
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, alice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel. The minial HTLC amount is set to
|
|
|
|
// 4200 msats.
|
|
|
|
const customizedMinHtlc = 4200
|
|
|
|
|
|
|
|
chanAmt := btcutil.Amount(100000)
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
|
|
|
chanPoint := openChannelAndAssert(
|
|
|
|
ctxt, t, net, alice, bob,
|
|
|
|
lntest.OpenChannelParams{
|
2020-08-13 01:46:17 +03:00
|
|
|
Amt: chanAmt,
|
|
|
|
MinHtlc: customizedMinHtlc,
|
|
|
|
RemoteMaxHtlcs: aliceRemoteMaxHtlcs,
|
2020-06-10 15:21:13 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Wait for Alice and Bob to receive the channel edge from the
|
|
|
|
// funding manager.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't see the alice->bob channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = bob.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't see the bob->alice channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice should have one channel opened with Bob.
|
|
|
|
assertNodeNumChannels(t, alice, 1)
|
|
|
|
// Bob should have one channel opened with Alice.
|
|
|
|
assertNodeNumChannels(t, bob, 1)
|
|
|
|
|
|
|
|
// Get the ListChannel response from Alice.
|
|
|
|
listReq := &lnrpc.ListChannelsRequest{}
|
|
|
|
ctxb = context.Background()
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := alice.ListChannels(ctxt, listReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for %s's channel list: %v",
|
|
|
|
alice.Name(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the returned response is correct.
|
|
|
|
aliceChannel := resp.Channels[0]
|
|
|
|
|
|
|
|
// defaultConstraints is a ChannelConstraints with default values. It is
|
|
|
|
// used to test against Alice's local channel constraints.
|
|
|
|
defaultConstraints := &lnrpc.ChannelConstraints{
|
|
|
|
CsvDelay: 4,
|
|
|
|
ChanReserveSat: 1000,
|
|
|
|
DustLimitSat: uint64(lnwallet.DefaultDustLimit()),
|
|
|
|
MaxPendingAmtMsat: 99000000,
|
|
|
|
MinHtlcMsat: 1,
|
2020-08-13 01:45:57 +03:00
|
|
|
MaxAcceptedHtlcs: bobRemoteMaxHtlcs,
|
2020-06-10 15:21:13 +03:00
|
|
|
}
|
|
|
|
assertChannelConstraintsEqual(
|
2020-08-13 01:45:29 +03:00
|
|
|
t, defaultConstraints, aliceChannel.LocalConstraints,
|
2020-06-10 15:21:13 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// customizedConstraints is a ChannelConstraints with customized values.
|
|
|
|
// Ideally, all these values can be passed in when creating the channel.
|
|
|
|
// Currently, only the MinHtlcMsat is customized. It is used to check
|
|
|
|
// against Alice's remote channel constratins.
|
|
|
|
customizedConstraints := &lnrpc.ChannelConstraints{
|
|
|
|
CsvDelay: 4,
|
|
|
|
ChanReserveSat: 1000,
|
|
|
|
DustLimitSat: uint64(lnwallet.DefaultDustLimit()),
|
|
|
|
MaxPendingAmtMsat: 99000000,
|
|
|
|
MinHtlcMsat: customizedMinHtlc,
|
2020-08-13 01:45:57 +03:00
|
|
|
MaxAcceptedHtlcs: aliceRemoteMaxHtlcs,
|
2020-06-10 15:21:13 +03:00
|
|
|
}
|
|
|
|
assertChannelConstraintsEqual(
|
2020-08-13 01:45:29 +03:00
|
|
|
t, customizedConstraints, aliceChannel.RemoteConstraints,
|
2020-06-10 15:21:13 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Get the ListChannel response for Bob.
|
|
|
|
listReq = &lnrpc.ListChannelsRequest{}
|
|
|
|
ctxb = context.Background()
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err = bob.ListChannels(ctxt, listReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for %s's channel "+
|
|
|
|
"list: %v", bob.Name(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bobChannel := resp.Channels[0]
|
|
|
|
if bobChannel.ChannelPoint != aliceChannel.ChannelPoint {
|
|
|
|
t.Fatalf("Bob's channel point mismatched, want: %s, got: %s",
|
|
|
|
chanPoint.String(), bobChannel.ChannelPoint,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check channel constraints match. Alice's local channel constraint should
|
|
|
|
// be equal to Bob's remote channel constraint, and her remote one should
|
|
|
|
// be equal to Bob's local one.
|
|
|
|
assertChannelConstraintsEqual(
|
|
|
|
t, aliceChannel.LocalConstraints, bobChannel.RemoteConstraints,
|
|
|
|
)
|
|
|
|
assertChannelConstraintsEqual(
|
|
|
|
t, aliceChannel.RemoteConstraints, bobChannel.LocalConstraints,
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-17 01:06:54 +03:00
|
|
|
// testUpdateChanStatus checks that calls to the UpdateChanStatus RPC update
|
|
|
|
// the channel graph as expected, and that channel state is properly updated
|
|
|
|
// in the presence of interleaved node disconnects / reconnects.
|
|
|
|
func testUpdateChanStatus(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// Create two fresh nodes and open a channel between them.
|
|
|
|
alice, err := net.NewNode("Alice", []string{
|
|
|
|
"--minbackoff=10s",
|
|
|
|
"--chan-enable-timeout=1.5s",
|
|
|
|
"--chan-disable-timeout=3s",
|
|
|
|
"--chan-status-sample-interval=.5s",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, alice)
|
|
|
|
|
|
|
|
bob, err := net.NewNode("Bob", []string{
|
|
|
|
"--minbackoff=10s",
|
|
|
|
"--chan-enable-timeout=1.5s",
|
|
|
|
"--chan-disable-timeout=3s",
|
|
|
|
"--chan-status-sample-interval=.5s",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, bob)
|
|
|
|
|
|
|
|
// Connect Alice to Bob.
|
|
|
|
if err := net.ConnectNodes(ctxb, alice, bob); err != nil {
|
|
|
|
t.Fatalf("unable to connect alice to bob: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give Alice some coins so she can fund a channel.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, alice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
|
|
|
chanAmt := btcutil.Amount(100000)
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
|
|
|
chanPoint := openChannelAndAssert(
|
|
|
|
ctxt, t, net, alice, bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Wait for Alice and Bob to receive the channel edge from the
|
|
|
|
// funding manager.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't see the alice->bob channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = bob.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't see the bob->alice channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch a node for Carol which will connect to Alice and Bob in
|
|
|
|
// order to receive graph updates. This will ensure that the
|
|
|
|
// channel updates are propagated throughout the network.
|
|
|
|
carol, err := net.NewNode("Carol", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create Carol's node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, alice, carol); err != nil {
|
|
|
|
t.Fatalf("unable to connect alice to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, bob, carol); err != nil {
|
|
|
|
t.Fatalf("unable to connect bob to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
carolSub := subscribeGraphNotifications(t, ctxb, carol)
|
|
|
|
defer close(carolSub.quit)
|
|
|
|
|
|
|
|
// sendReq sends an UpdateChanStatus request to the given node.
|
|
|
|
sendReq := func(node *lntest.HarnessNode, chanPoint *lnrpc.ChannelPoint,
|
|
|
|
action routerrpc.ChanStatusAction) {
|
|
|
|
|
|
|
|
req := &routerrpc.UpdateChanStatusRequest{
|
|
|
|
ChanPoint: chanPoint,
|
|
|
|
Action: action,
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
_, err = node.RouterClient.UpdateChanStatus(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to call UpdateChanStatus for %s's node: %v",
|
|
|
|
node.Name(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertEdgeDisabled ensures that a given node has the correct
|
|
|
|
// Disabled state for a channel.
|
|
|
|
assertEdgeDisabled := func(node *lntest.HarnessNode,
|
|
|
|
chanPoint *lnrpc.ChannelPoint, disabled bool) {
|
|
|
|
|
|
|
|
var predErr error
|
|
|
|
err = wait.Predicate(func() bool {
|
|
|
|
req := &lnrpc.ChannelGraphRequest{
|
|
|
|
IncludeUnannounced: true,
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
chanGraph, err := node.DescribeGraph(ctxt, req)
|
|
|
|
if err != nil {
|
|
|
|
predErr = fmt.Errorf("unable to query node %v's graph: %v", node, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
numEdges := len(chanGraph.Edges)
|
|
|
|
if numEdges != 1 {
|
|
|
|
predErr = fmt.Errorf("expected to find 1 edge in the graph, found %d", numEdges)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
edge := chanGraph.Edges[0]
|
|
|
|
if edge.ChanPoint != chanPoint.GetFundingTxidStr() {
|
|
|
|
predErr = fmt.Errorf("expected chan_point %v, got %v",
|
|
|
|
chanPoint.GetFundingTxidStr(), edge.ChanPoint)
|
|
|
|
}
|
|
|
|
var policy *lnrpc.RoutingPolicy
|
|
|
|
if node.PubKeyStr == edge.Node1Pub {
|
|
|
|
policy = edge.Node1Policy
|
|
|
|
} else {
|
|
|
|
policy = edge.Node2Policy
|
|
|
|
}
|
|
|
|
if disabled != policy.Disabled {
|
|
|
|
predErr = fmt.Errorf("expected policy.Disabled to be %v, "+
|
|
|
|
"but policy was %v", disabled, policy)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, defaultTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", predErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When updating the state of the channel between Alice and Bob, we
|
|
|
|
// should expect to see channel updates with the default routing
|
|
|
|
// policy. The value of "Disabled" will depend on the specific
|
|
|
|
// scenario being tested.
|
|
|
|
expectedPolicy := &lnrpc.RoutingPolicy{
|
|
|
|
FeeBaseMsat: int64(chainreg.DefaultBitcoinBaseFeeMSat),
|
|
|
|
FeeRateMilliMsat: int64(chainreg.DefaultBitcoinFeeRate),
|
|
|
|
TimeLockDelta: chainreg.DefaultBitcoinTimeLockDelta,
|
|
|
|
MinHtlc: 1000, // default value
|
|
|
|
MaxHtlcMsat: calculateMaxHtlc(chanAmt),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initially, the channel between Alice and Bob should not be
|
|
|
|
// disabled.
|
|
|
|
assertEdgeDisabled(alice, chanPoint, false)
|
|
|
|
|
|
|
|
// Manually disable the channel and ensure that a "Disabled = true"
|
|
|
|
// update is propagated.
|
|
|
|
sendReq(alice, chanPoint, routerrpc.ChanStatusAction_DISABLE)
|
|
|
|
expectedPolicy.Disabled = true
|
|
|
|
waitForChannelUpdate(
|
|
|
|
t, carolSub,
|
|
|
|
[]expectedChanUpdate{
|
|
|
|
{alice.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Re-enable the channel and ensure that a "Disabled = false" update
|
|
|
|
// is propagated.
|
|
|
|
sendReq(alice, chanPoint, routerrpc.ChanStatusAction_ENABLE)
|
|
|
|
expectedPolicy.Disabled = false
|
|
|
|
waitForChannelUpdate(
|
|
|
|
t, carolSub,
|
|
|
|
[]expectedChanUpdate{
|
|
|
|
{alice.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Manually enabling a channel should NOT prevent subsequent
|
|
|
|
// disconnections from automatically disabling the channel again
|
|
|
|
// (we don't want to clutter the network with channels that are
|
|
|
|
// falsely advertised as enabled when they don't work).
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.DisconnectNodes(ctxt, alice, bob); err != nil {
|
|
|
|
t.Fatalf("unable to disconnect Alice from Bob: %v", err)
|
|
|
|
}
|
|
|
|
expectedPolicy.Disabled = true
|
|
|
|
waitForChannelUpdate(
|
|
|
|
t, carolSub,
|
|
|
|
[]expectedChanUpdate{
|
|
|
|
{alice.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
{bob.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Reconnecting the nodes should propagate a "Disabled = false" update.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.EnsureConnected(ctxt, alice, bob); err != nil {
|
|
|
|
t.Fatalf("unable to reconnect Alice to Bob: %v", err)
|
|
|
|
}
|
|
|
|
expectedPolicy.Disabled = false
|
|
|
|
waitForChannelUpdate(
|
|
|
|
t, carolSub,
|
|
|
|
[]expectedChanUpdate{
|
|
|
|
{alice.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
{bob.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Manually disabling the channel should prevent a subsequent
|
|
|
|
// disconnect / reconnect from re-enabling the channel on
|
|
|
|
// Alice's end. Note the asymmetry between manual enable and
|
|
|
|
// manual disable!
|
|
|
|
sendReq(alice, chanPoint, routerrpc.ChanStatusAction_DISABLE)
|
|
|
|
|
|
|
|
// Alice sends out the "Disabled = true" update in response to
|
|
|
|
// the ChanStatusAction_DISABLE request.
|
|
|
|
expectedPolicy.Disabled = true
|
|
|
|
waitForChannelUpdate(
|
|
|
|
t, carolSub,
|
|
|
|
[]expectedChanUpdate{
|
|
|
|
{alice.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.DisconnectNodes(ctxt, alice, bob); err != nil {
|
|
|
|
t.Fatalf("unable to disconnect Alice from Bob: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob sends a "Disabled = true" update upon detecting the
|
|
|
|
// disconnect.
|
|
|
|
expectedPolicy.Disabled = true
|
|
|
|
waitForChannelUpdate(
|
|
|
|
t, carolSub,
|
|
|
|
[]expectedChanUpdate{
|
|
|
|
{bob.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Bob sends a "Disabled = false" update upon detecting the
|
|
|
|
// reconnect.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.EnsureConnected(ctxt, alice, bob); err != nil {
|
|
|
|
t.Fatalf("unable to reconnect Alice to Bob: %v", err)
|
|
|
|
}
|
|
|
|
expectedPolicy.Disabled = false
|
|
|
|
waitForChannelUpdate(
|
|
|
|
t, carolSub,
|
|
|
|
[]expectedChanUpdate{
|
|
|
|
{bob.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// However, since we manually disabled the channel on Alice's end,
|
|
|
|
// the policy on Alice's end should still be "Disabled = true". Again,
|
|
|
|
// note the asymmetry between manual enable and manual disable!
|
|
|
|
assertEdgeDisabled(alice, chanPoint, true)
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.DisconnectNodes(ctxt, alice, bob); err != nil {
|
|
|
|
t.Fatalf("unable to disconnect Alice from Bob: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob sends a "Disabled = true" update upon detecting the
|
|
|
|
// disconnect.
|
|
|
|
expectedPolicy.Disabled = true
|
|
|
|
waitForChannelUpdate(
|
|
|
|
t, carolSub,
|
|
|
|
[]expectedChanUpdate{
|
|
|
|
{bob.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// After restoring automatic channel state management on Alice's end,
|
|
|
|
// BOTH Alice and Bob should set the channel state back to "enabled"
|
|
|
|
// on reconnect.
|
|
|
|
sendReq(alice, chanPoint, routerrpc.ChanStatusAction_AUTO)
|
|
|
|
if err := net.EnsureConnected(ctxt, alice, bob); err != nil {
|
|
|
|
t.Fatalf("unable to reconnect Alice to Bob: %v", err)
|
|
|
|
}
|
|
|
|
expectedPolicy.Disabled = false
|
|
|
|
waitForChannelUpdate(
|
|
|
|
t, carolSub,
|
|
|
|
[]expectedChanUpdate{
|
|
|
|
{alice.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
{bob.PubKeyStr, expectedPolicy, chanPoint},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assertEdgeDisabled(alice, chanPoint, false)
|
|
|
|
}
|
|
|
|
|
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.
|
2020-03-31 13:44:18 +03:00
|
|
|
sendAndAssertSuccess(
|
|
|
|
t, net.Alice,
|
|
|
|
&routerrpc.SendPaymentRequest{
|
|
|
|
PaymentRequest: invoiceResp.PaymentRequest,
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitSat: 1000000,
|
|
|
|
},
|
|
|
|
)
|
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]
|
2020-04-09 17:28:34 +03:00
|
|
|
path := p.Htlcs[len(p.Htlcs)-1].Route.Hops
|
2016-12-05 14:59:36 +03:00
|
|
|
|
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.
|
2020-04-09 17:28:34 +03:00
|
|
|
if len(path) != 1 || path[0].PubKey != net.Bob.PubKeyStr {
|
|
|
|
t.Fatalf("incorrect path")
|
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)
|
|
|
|
}
|
|
|
|
|
2020-01-17 09:49:13 +03:00
|
|
|
// Check that there are no payments after 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() {
|
2020-12-08 18:27:01 +03:00
|
|
|
<-time.After(defaultTimeout)
|
2017-12-19 02:18:40 +03:00
|
|
|
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,
|
2019-08-20 03:55:30 +03:00
|
|
|
timeLockDelta uint32, maxHtlc uint64, listenerNode *lntest.HarnessNode) {
|
|
|
|
|
2018-06-16 15:53:44 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
expectedPolicy := &lnrpc.RoutingPolicy{
|
|
|
|
FeeBaseMsat: baseFee,
|
|
|
|
FeeRateMilliMsat: feeRate,
|
|
|
|
TimeLockDelta: timeLockDelta,
|
2018-08-22 10:32:43 +03:00
|
|
|
MinHtlc: 1000, // default value
|
2019-08-20 03:55:30 +03:00
|
|
|
MaxHtlcMsat: maxHtlc,
|
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,
|
|
|
|
},
|
2019-08-20 03:55:30 +03:00
|
|
|
MaxHtlcMsat: maxHtlc,
|
2018-06-16 15:53:44 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-11-05 02:11:55 +03:00
|
|
|
type singleHopSendToRouteCase struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
// streaming tests streaming SendToRoute if true, otherwise tests
|
|
|
|
// synchronous SenToRoute.
|
|
|
|
streaming bool
|
|
|
|
|
|
|
|
// routerrpc submits the request to the routerrpc subserver if true,
|
|
|
|
// otherwise submits to the main rpc server.
|
|
|
|
routerrpc bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var singleHopSendToRouteCases = []singleHopSendToRouteCase{
|
|
|
|
{
|
|
|
|
name: "regular main sync",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular main stream",
|
|
|
|
streaming: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular routerrpc sync",
|
|
|
|
routerrpc: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mpp main sync",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mpp main stream",
|
|
|
|
streaming: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mpp routerrpc sync",
|
|
|
|
routerrpc: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// testSingleHopSendToRoute tests that payments are properly processed through a
|
|
|
|
// provided route with a single hop. We'll create the following network
|
|
|
|
// topology:
|
|
|
|
// Carol --100k--> Dave
|
|
|
|
// We'll query the daemon for routes from Carol to Dave and then send payments
|
|
|
|
// by feeding the route back into the various SendToRoute RPC methods. Here we
|
|
|
|
// test all three SendToRoute endpoints, forcing each to perform both a regular
|
|
|
|
// payment and an MPP payment.
|
2018-02-03 13:21:16 +03:00
|
|
|
func testSingleHopSendToRoute(net *lntest.NetworkHarness, t *harnessTest) {
|
2019-11-05 02:11:55 +03:00
|
|
|
for _, test := range singleHopSendToRouteCases {
|
|
|
|
test := test
|
|
|
|
|
|
|
|
t.t.Run(test.name, func(t1 *testing.T) {
|
|
|
|
ht := newHarnessTest(t1, t.lndHarness)
|
|
|
|
ht.RunTestCase(&testCase{
|
|
|
|
name: test.name,
|
|
|
|
test: func(_ *lntest.NetworkHarness, tt *harnessTest) {
|
|
|
|
testSingleHopSendToRouteCase(net, tt, test)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSingleHopSendToRouteCase(net *lntest.NetworkHarness, t *harnessTest,
|
|
|
|
test singleHopSendToRouteCase) {
|
2018-11-28 11:25:46 +03:00
|
|
|
|
|
|
|
const chanAmt = btcutil.Amount(100000)
|
2019-11-05 02:11:55 +03:00
|
|
|
const paymentAmtSat = 1000
|
|
|
|
const numPayments = 5
|
|
|
|
const amountPaid = int64(numPayments * paymentAmtSat)
|
|
|
|
|
|
|
|
ctxb := context.Background()
|
2018-02-03 13:21:16 +03:00
|
|
|
var networkChans []*lnrpc.ChannelPoint
|
|
|
|
|
2019-11-05 02:11:55 +03:00
|
|
|
// Create Carol and Dave, then establish a channel between them. Carol
|
|
|
|
// is the sole funder of the channel with 100k satoshis. The network
|
|
|
|
// topology should look like:
|
|
|
|
// Carol -> 100k -> Dave
|
|
|
|
carol, err := net.NewNode("Carol", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
|
|
|
dave, err := net.NewNode("Dave", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
if err := net.ConnectNodes(ctxt, carol, dave); err != nil {
|
|
|
|
t.Fatalf("unable to connect carol to dave: %v", err)
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Carol and Dave with Carol
|
2018-02-03 13:21:16 +03:00
|
|
|
// being the sole funder of the channel.
|
2019-11-05 02:11:55 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
|
|
|
chanPointCarol := openChannelAndAssert(
|
|
|
|
ctxt, t, net, carol, dave,
|
2018-08-22 10:32:42 +03:00
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
2019-11-05 02:11:55 +03:00
|
|
|
networkChans = append(networkChans, chanPointCarol)
|
2018-02-03 13:21:16 +03:00
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
carolChanTXID, err := lnrpc.GetChanPointFundingTxid(chanPointCarol)
|
2018-02-03 13:21:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2019-11-05 02:11:55 +03:00
|
|
|
carolFundPoint := wire.OutPoint{
|
|
|
|
Hash: *carolChanTXID,
|
|
|
|
Index: chanPointCarol.OutputIndex,
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all nodes to have seen all channels.
|
2019-11-05 02:11:55 +03:00
|
|
|
nodes := []*lntest.HarnessNode{carol, dave}
|
2018-02-03 13:21:16 +03:00
|
|
|
for _, chanPoint := range networkChans {
|
2019-11-05 02:11:55 +03:00
|
|
|
for _, node := range nodes {
|
2021-02-13 11:05:33 +03:00
|
|
|
txid, err := lnrpc.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 "+
|
2019-11-05 02:11:55 +03:00
|
|
|
"channel(%s) open: %v", node.Name(),
|
2018-02-03 13:21:16 +03:00
|
|
|
node.NodeID, point, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 02:11:55 +03:00
|
|
|
// Create invoices for Dave, which expect a payment from Carol.
|
2019-09-03 13:23:39 +03:00
|
|
|
payReqs, rHashes, _, err := createPayReqs(
|
2019-11-05 02:11:55 +03:00
|
|
|
dave, paymentAmtSat, numPayments,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-09-03 13:23:39 +03:00
|
|
|
// Reconstruct payment addresses.
|
|
|
|
var payAddrs [][]byte
|
|
|
|
for _, payReq := range payReqs {
|
|
|
|
ctx, _ := context.WithTimeout(
|
|
|
|
context.Background(), defaultTimeout,
|
|
|
|
)
|
|
|
|
resp, err := dave.DecodePayReq(
|
|
|
|
ctx,
|
|
|
|
&lnrpc.PayReqString{PayReq: payReq},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("decode pay req: %v", err)
|
|
|
|
}
|
|
|
|
payAddrs = append(payAddrs, resp.PaymentAddr)
|
|
|
|
}
|
|
|
|
|
2020-10-26 22:25:25 +03:00
|
|
|
// Assert Carol and Dave are synced to the chain before proceeding, to
|
|
|
|
// ensure the queried route will have a valid final CLTV once the HTLC
|
|
|
|
// reaches Dave.
|
|
|
|
_, minerHeight, err := net.Miner.Node.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get best height: %v", err)
|
|
|
|
}
|
|
|
|
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
require.NoError(t.t, waitForNodeBlockHeight(ctxt, carol, minerHeight))
|
|
|
|
require.NoError(t.t, waitForNodeBlockHeight(ctxt, dave, minerHeight))
|
|
|
|
|
|
|
|
// Query for routes to pay from Carol to Dave using the default CLTV
|
|
|
|
// config.
|
2018-02-03 13:21:16 +03:00
|
|
|
routesReq := &lnrpc.QueryRoutesRequest{
|
2020-10-26 22:25:25 +03:00
|
|
|
PubKey: dave.PubKeyStr,
|
|
|
|
Amt: paymentAmtSat,
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2019-11-05 02:11:55 +03:00
|
|
|
routes, err := carol.QueryRoutes(ctxt, routesReq)
|
2018-02-03 13:21:16 +03:00
|
|
|
if err != nil {
|
2019-11-05 02:11:55 +03:00
|
|
|
t.Fatalf("unable to get route from %s: %v",
|
|
|
|
carol.Name(), err)
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
|
|
|
|
2019-11-05 02:11:55 +03:00
|
|
|
// There should only be one route to try, so take the first item.
|
|
|
|
r := routes.Routes[0]
|
2018-02-03 13:21:16 +03:00
|
|
|
|
2019-11-05 02:11:55 +03:00
|
|
|
// Construct a closure that will set MPP fields on the route, which
|
|
|
|
// allows us to test MPP payments.
|
|
|
|
setMPPFields := func(i int) {
|
|
|
|
hop := r.Hops[len(r.Hops)-1]
|
|
|
|
hop.TlvPayload = true
|
|
|
|
hop.MppRecord = &lnrpc.MPPRecord{
|
2019-09-03 13:23:39 +03:00
|
|
|
PaymentAddr: payAddrs[i],
|
2019-11-05 02:11:55 +03:00
|
|
|
TotalAmtMsat: paymentAmtSat * 1000,
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
2019-11-05 02:11:55 +03:00
|
|
|
}
|
2018-02-03 13:21:16 +03:00
|
|
|
|
2019-11-05 02:11:55 +03:00
|
|
|
// Construct closures for each of the payment types covered:
|
|
|
|
// - main rpc server sync
|
|
|
|
// - main rpc server streaming
|
|
|
|
// - routerrpc server sync
|
|
|
|
sendToRouteSync := func() {
|
|
|
|
for i, rHash := range rHashes {
|
2020-11-25 06:05:32 +03:00
|
|
|
setMPPFields(i)
|
2019-11-05 02:11:55 +03:00
|
|
|
|
|
|
|
sendReq := &lnrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: rHash,
|
|
|
|
Route: r,
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := carol.SendToRouteSync(
|
|
|
|
ctxt, sendReq,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send to route for "+
|
|
|
|
"%s: %v", carol.Name(), err)
|
|
|
|
}
|
|
|
|
if resp.PaymentError != "" {
|
|
|
|
t.Fatalf("received payment error from %s: %v",
|
|
|
|
carol.Name(), resp.PaymentError)
|
|
|
|
}
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
|
|
|
}
|
2019-11-05 02:11:55 +03:00
|
|
|
sendToRouteStream := func() {
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
alicePayStream, err := carol.SendToRoute(ctxt)
|
2018-02-03 13:21:16 +03:00
|
|
|
if err != nil {
|
2019-11-05 02:11:55 +03:00
|
|
|
t.Fatalf("unable to create payment stream for "+
|
|
|
|
"carol: %v", err)
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
2019-11-05 02:11:55 +03:00
|
|
|
|
|
|
|
for i, rHash := range rHashes {
|
2020-11-25 06:05:32 +03:00
|
|
|
setMPPFields(i)
|
2019-11-05 02:11:55 +03:00
|
|
|
|
|
|
|
sendReq := &lnrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: rHash,
|
|
|
|
Route: routes.Routes[0],
|
|
|
|
}
|
|
|
|
err := alicePayStream.Send(sendReq)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
|
|
|
}
|
2019-11-05 02:11:55 +03:00
|
|
|
sendToRouteRouterRPC := func() {
|
|
|
|
for i, rHash := range rHashes {
|
2020-11-25 06:05:32 +03:00
|
|
|
setMPPFields(i)
|
2018-02-03 13:21:16 +03:00
|
|
|
|
2019-11-05 02:11:55 +03:00
|
|
|
sendReq := &routerrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: rHash,
|
|
|
|
Route: r,
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2020-05-06 16:28:41 +03:00
|
|
|
resp, err := carol.RouterClient.SendToRouteV2(
|
2019-11-05 02:11:55 +03:00
|
|
|
ctxt, sendReq,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send to route for "+
|
|
|
|
"%s: %v", carol.Name(), err)
|
|
|
|
}
|
|
|
|
if resp.Failure != nil {
|
|
|
|
t.Fatalf("received payment error from %s: %v",
|
|
|
|
carol.Name(), resp.Failure)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using Carol as the node as the source, send the payments
|
|
|
|
// synchronously via the the routerrpc's SendToRoute, or via the main RPC
|
|
|
|
// server's SendToRoute streaming or sync calls.
|
|
|
|
switch {
|
|
|
|
case !test.routerrpc && test.streaming:
|
|
|
|
sendToRouteStream()
|
|
|
|
case !test.routerrpc && !test.streaming:
|
|
|
|
sendToRouteSync()
|
|
|
|
case test.routerrpc && !test.streaming:
|
|
|
|
sendToRouteRouterRPC()
|
|
|
|
default:
|
|
|
|
t.Fatalf("routerrpc does not support streaming send_to_route")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the payment's from Carol's PoV have the correct payment
|
|
|
|
// hash and amount.
|
2019-05-30 02:31:49 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
2019-11-05 02:11:55 +03:00
|
|
|
paymentsResp, err := carol.ListPayments(
|
|
|
|
ctxt, &lnrpc.ListPaymentsRequest{},
|
|
|
|
)
|
2019-05-30 02:31:49 +03:00
|
|
|
if err != nil {
|
2019-11-05 02:11:55 +03:00
|
|
|
t.Fatalf("error when obtaining %s payments: %v",
|
|
|
|
carol.Name(), err)
|
2019-05-30 02:31:49 +03:00
|
|
|
}
|
2019-11-05 02:11:55 +03:00
|
|
|
if len(paymentsResp.Payments) != numPayments {
|
2019-05-30 02:31:49 +03:00
|
|
|
t.Fatalf("incorrect number of payments, got %v, want %v",
|
2019-11-05 02:11:55 +03:00
|
|
|
len(paymentsResp.Payments), numPayments)
|
2019-05-30 02:31:49 +03:00
|
|
|
}
|
|
|
|
|
2019-11-05 02:11:55 +03:00
|
|
|
for i, p := range paymentsResp.Payments {
|
|
|
|
// Assert that the payment hashes for each payment match up.
|
|
|
|
rHashHex := hex.EncodeToString(rHashes[i])
|
|
|
|
if p.PaymentHash != rHashHex {
|
|
|
|
t.Fatalf("incorrect payment hash for payment %d, "+
|
|
|
|
"want: %s got: %s",
|
|
|
|
i, rHashHex, p.PaymentHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that each payment has no invoice since the payment was
|
|
|
|
// completed using SendToRoute.
|
2019-05-30 02:31:49 +03:00
|
|
|
if p.PaymentRequest != "" {
|
2019-11-05 02:11:55 +03:00
|
|
|
t.Fatalf("incorrect payment request for payment: %d, "+
|
|
|
|
"want: \"\", got: %s",
|
|
|
|
i, p.PaymentRequest)
|
|
|
|
}
|
|
|
|
|
2020-01-10 17:27:50 +03:00
|
|
|
// Assert the payment amount is correct.
|
2019-11-05 02:11:55 +03:00
|
|
|
if p.ValueSat != paymentAmtSat {
|
|
|
|
t.Fatalf("incorrect payment amt for payment %d, "+
|
|
|
|
"want: %d, got: %d",
|
|
|
|
i, paymentAmtSat, p.ValueSat)
|
|
|
|
}
|
2019-11-20 07:41:58 +03:00
|
|
|
|
|
|
|
// Assert exactly one htlc was made.
|
|
|
|
if len(p.Htlcs) != 1 {
|
|
|
|
t.Fatalf("expected 1 htlc for payment %d, got: %d",
|
|
|
|
i, len(p.Htlcs))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert the htlc's route is populated.
|
|
|
|
htlc := p.Htlcs[0]
|
|
|
|
if htlc.Route == nil {
|
|
|
|
t.Fatalf("expected route for payment %d", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert the hop has exactly one hop.
|
|
|
|
if len(htlc.Route.Hops) != 1 {
|
|
|
|
t.Fatalf("expected 1 hop for payment %d, got: %d",
|
|
|
|
i, len(htlc.Route.Hops))
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is an MPP test, assert the MPP record's fields are
|
|
|
|
// properly populated. Otherwise the hop should not have an MPP
|
|
|
|
// record.
|
|
|
|
hop := htlc.Route.Hops[0]
|
2020-11-25 06:05:32 +03:00
|
|
|
if hop.MppRecord == nil {
|
|
|
|
t.Fatalf("expected mpp record for mpp payment")
|
|
|
|
}
|
2019-11-20 07:41:58 +03:00
|
|
|
|
2020-11-25 06:05:32 +03:00
|
|
|
if hop.MppRecord.TotalAmtMsat != paymentAmtSat*1000 {
|
|
|
|
t.Fatalf("incorrect mpp total msat for payment %d "+
|
|
|
|
"want: %d, got: %d",
|
|
|
|
i, paymentAmtSat*1000,
|
|
|
|
hop.MppRecord.TotalAmtMsat)
|
|
|
|
}
|
2019-11-20 07:41:58 +03:00
|
|
|
|
2020-11-25 06:05:32 +03:00
|
|
|
expAddr := payAddrs[i]
|
|
|
|
if !bytes.Equal(hop.MppRecord.PaymentAddr, expAddr) {
|
|
|
|
t.Fatalf("incorrect mpp payment addr for payment %d "+
|
|
|
|
"want: %x, got: %x",
|
|
|
|
i, expAddr, hop.MppRecord.PaymentAddr)
|
2019-11-20 07:41:58 +03:00
|
|
|
}
|
2019-11-05 02:11:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the invoices's from Dave's PoV have the correct payment
|
|
|
|
// hash and amount.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
|
|
|
invoicesResp, err := dave.ListInvoices(
|
|
|
|
ctxt, &lnrpc.ListInvoiceRequest{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error when obtaining %s payments: %v",
|
|
|
|
dave.Name(), err)
|
|
|
|
}
|
|
|
|
if len(invoicesResp.Invoices) != numPayments {
|
|
|
|
t.Fatalf("incorrect number of invoices, got %v, want %v",
|
|
|
|
len(invoicesResp.Invoices), numPayments)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, inv := range invoicesResp.Invoices {
|
|
|
|
// Assert that the payment hashes match up.
|
|
|
|
if !bytes.Equal(inv.RHash, rHashes[i]) {
|
|
|
|
t.Fatalf("incorrect payment hash for invoice %d, "+
|
|
|
|
"want: %x got: %x",
|
|
|
|
i, rHashes[i], inv.RHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that the amount paid to the invoice is correct.
|
|
|
|
if inv.AmtPaidSat != paymentAmtSat {
|
|
|
|
t.Fatalf("incorrect payment amt for invoice %d, "+
|
|
|
|
"want: %d, got %d",
|
|
|
|
i, paymentAmtSat, inv.AmtPaidSat)
|
2019-05-30 02:31:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-03 13:21:16 +03:00
|
|
|
// At this point all the channels within our proto network should be
|
2019-11-05 02:11:55 +03:00
|
|
|
// shifted by 5k satoshis in the direction of Dave, the sink within the
|
2018-02-03 13:21:16 +03:00
|
|
|
// payment flow generated above. The order of asserts corresponds to
|
|
|
|
// increasing of time is needed to embed the HTLC in commitment
|
2019-11-05 02:11:55 +03:00
|
|
|
// transaction, in channel Carol->Dave, order is Dave and then Carol.
|
|
|
|
assertAmountPaid(t, "Carol(local) => Dave(remote)", dave,
|
|
|
|
carolFundPoint, int64(0), amountPaid)
|
|
|
|
assertAmountPaid(t, "Carol(local) => Dave(remote)", carol,
|
|
|
|
carolFundPoint, amountPaid, int64(0))
|
2018-02-03 13:21:16 +03:00
|
|
|
|
2018-11-27 10:57:32 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2019-11-05 02:11:55 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPointCarol, false)
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
aliceChanTXID, err := lnrpc.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)
|
2021-02-13 11:05:33 +03:00
|
|
|
bobChanTXID, err := lnrpc.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 {
|
2021-02-13 11:05:33 +03:00
|
|
|
txid, err := lnrpc.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:05:32 +03:00
|
|
|
// Create 5 invoices for Carol, which expect a payment from Alice for 1k
|
|
|
|
// satoshis with a different preimage each time.
|
|
|
|
const (
|
|
|
|
numPayments = 5
|
|
|
|
paymentAmt = 1000
|
|
|
|
)
|
|
|
|
_, rHashes, invoices, err := createPayReqs(
|
|
|
|
carol, paymentAmt, numPayments,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a route from Alice to Carol for each of the invoices
|
|
|
|
// created above. We set FinalCltvDelta to 40 since by default
|
|
|
|
// QueryRoutes returns the last hop with a final cltv delta of 9 where
|
|
|
|
// as the default in htlcswitch is 40.
|
2018-02-03 13:21:16 +03:00
|
|
|
routesReq := &lnrpc.QueryRoutesRequest{
|
|
|
|
PubKey: carol.PubKeyStr,
|
|
|
|
Amt: paymentAmt,
|
2020-10-06 21:56:10 +03:00
|
|
|
FinalCltvDelta: chainreg.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-11-25 06:05:32 +03:00
|
|
|
for i, rHash := range rHashes {
|
|
|
|
// Manually set the MPP payload a new for each payment since
|
|
|
|
// the payment addr will change with each invoice, although we
|
|
|
|
// can re-use the route itself.
|
|
|
|
route := *routes.Routes[0]
|
|
|
|
route.Hops[len(route.Hops)-1].TlvPayload = true
|
|
|
|
route.Hops[len(route.Hops)-1].MppRecord = &lnrpc.MPPRecord{
|
|
|
|
PaymentAddr: invoices[i].PaymentAddr,
|
|
|
|
TotalAmtMsat: int64(
|
|
|
|
lnwire.NewMSatFromSatoshis(paymentAmt),
|
|
|
|
),
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
|
|
|
|
2020-11-25 06:05:32 +03:00
|
|
|
sendReq := &routerrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: rHash,
|
|
|
|
Route: &route,
|
2018-02-03 13:21:16 +03:00
|
|
|
}
|
2020-11-25 06:05:32 +03:00
|
|
|
resp, err := net.Alice.RouterClient.SendToRouteV2(ctxt, sendReq)
|
2018-02-03 13:21:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
2020-11-25 06:05:32 +03:00
|
|
|
|
|
|
|
if resp.Failure != nil {
|
|
|
|
t.Fatalf("received payment error: %v", resp.Failure)
|
2018-02-03 13:21:16 +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 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()
|
|
|
|
|
2020-11-17 02:28:56 +03:00
|
|
|
amount := funding.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)
|
2020-04-03 00:29:50 +03:00
|
|
|
chanOpenUpdate := openChannelStream(
|
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
2018-09-26 18:30:09 +03:00
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: amount,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-09-26 18:30:09 +03:00
|
|
|
// 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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-09-26 18:30:09 +03:00
|
|
|
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)
|
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
aliceChanTXID, err := lnrpc.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)
|
2021-02-13 11:05:33 +03:00
|
|
|
daveChanTXID, err := lnrpc.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)
|
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
carolChanTXID, err := lnrpc.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 {
|
2021-02-13 11:05:33 +03:00
|
|
|
txid, err := lnrpc.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)
|
2020-04-03 00:29:50 +03:00
|
|
|
chanOpenUpdate := openChannelStream(
|
|
|
|
ctxt, t, net, 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)
|
|
|
|
}
|
2021-02-13 11:05:33 +03:00
|
|
|
fundingTxID, err := lnrpc.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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, carol, carol.RouterClient, payReqs, true,
|
|
|
|
)
|
2017-12-19 02:18:40 +03:00
|
|
|
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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, net.Alice, net.Alice.RouterClient, payReqs, true,
|
|
|
|
)
|
2017-12-19 02:18:40 +03:00
|
|
|
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
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-04-15 19:55:55 +03:00
|
|
|
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.
|
2021-01-14 15:58:03 +03:00
|
|
|
chanNames := []string{
|
|
|
|
"alice-bob", "alice-carol", "bob-carol", "alice-dave",
|
|
|
|
"alice-eve",
|
|
|
|
}
|
2018-04-21 18:37:38 +03:00
|
|
|
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 {
|
2021-01-14 15:58:03 +03:00
|
|
|
t.Fatalf("timed out waiting for channel open %s: %v",
|
|
|
|
chanNames[i], err)
|
2018-04-21 18:37:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-05-04 07:07:06 +03:00
|
|
|
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.
|
2021-02-13 11:05:33 +03:00
|
|
|
aliceChanTXID, err := lnrpc.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.
|
2021-02-13 11:05:33 +03:00
|
|
|
bobChanTXID, err := lnrpc.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.
|
2021-02-13 11:05:33 +03:00
|
|
|
carolChanTXID, err := lnrpc.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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, net.Alice, net.Alice.RouterClient, payReqs, true,
|
|
|
|
)
|
2018-03-15 20:02:52 +03:00
|
|
|
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.
|
2020-03-31 13:44:18 +03:00
|
|
|
sendReq := &routerrpc.SendPaymentRequest{
|
2017-10-23 04:35:38 +03:00
|
|
|
PaymentRequest: invoiceResp.PaymentRequest,
|
2020-03-31 13:44:18 +03:00
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
2016-10-16 00:45:26 +03:00
|
|
|
}
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2020-03-31 13:44:18 +03:00
|
|
|
stream, err := net.Alice.RouterClient.SendPaymentV2(ctxt, sendReq)
|
2017-10-26 01:32:00 +03:00
|
|
|
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
|
|
|
}
|
2020-03-31 13:44:18 +03:00
|
|
|
result, err := getPaymentResult(stream)
|
|
|
|
if err != nil {
|
2017-12-14 03:14:24 +03:00
|
|
|
close(quit)
|
2020-03-31 13:44:18 +03:00
|
|
|
t.Fatalf("cannot get payment result: %v", err)
|
|
|
|
}
|
|
|
|
if result.Status != lnrpc.Payment_SUCCEEDED {
|
|
|
|
close(quit)
|
|
|
|
t.Fatalf("error when attempting recv: %v", result.Status)
|
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(
|
2020-03-31 13:44:18 +03:00
|
|
|
ctxt, net.Alice, net.Alice.RouterClient, payReqs, true,
|
2018-06-30 04:07:55 +03:00
|
|
|
)
|
|
|
|
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,
|
2020-02-21 14:24:24 +03:00
|
|
|
closeType lnrpc.ChannelCloseSummary_ClosureType,
|
2020-04-03 10:09:39 +03:00
|
|
|
closeInitiator lnrpc.Initiator) error {
|
2018-11-02 06:20:21 +03:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:24:24 +03:00
|
|
|
if update.ClosedChannel.CloseType != closeType {
|
|
|
|
return fmt.Errorf("channel closure type "+
|
|
|
|
"mismatch: expected %v, got %v",
|
|
|
|
closeType,
|
|
|
|
update.ClosedChannel.CloseType)
|
2018-11-02 06:20:21 +03:00
|
|
|
}
|
2020-02-21 14:24:24 +03:00
|
|
|
|
|
|
|
if update.ClosedChannel.CloseInitiator != closeInitiator {
|
|
|
|
return fmt.Errorf("expected close intiator: %v, got: %v",
|
|
|
|
closeInitiator,
|
|
|
|
update.ClosedChannel.CloseInitiator)
|
|
|
|
}
|
|
|
|
|
2018-11-02 06:20:21 +03:00
|
|
|
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
|
2020-11-17 02:28:56 +03:00
|
|
|
amount = funding.MaxBtcFundingAmount
|
2016-10-24 05:00:09 +03:00
|
|
|
)
|
2016-10-17 07:36:43 +03:00
|
|
|
|
2019-02-12 04:23:47 +03:00
|
|
|
// Subscribe Bob and Alice to channel event notifications.
|
2018-11-02 06:20:21 +03:00
|
|
|
bobChanSub := subscribeChannelNotifications(ctxb, t, net.Bob)
|
|
|
|
defer close(bobChanSub.quit)
|
|
|
|
|
2019-02-12 04:23:47 +03:00
|
|
|
aliceChanSub := subscribeChannelNotifications(ctxb, t, net.Alice)
|
|
|
|
defer close(aliceChanSub.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
|
|
|
}
|
|
|
|
|
2019-02-12 04:23:47 +03:00
|
|
|
// Since each of the channels just became open, Bob and Alice should
|
|
|
|
// each receive an open and an active notification for each channel.
|
2018-11-02 06:20:21 +03:00
|
|
|
var numChannelUpds int
|
2019-09-29 13:13:01 +03:00
|
|
|
const totalNtfns = 3 * numChannels
|
2019-02-12 04:23:47 +03:00
|
|
|
verifyOpenUpdatesReceived := func(sub channelSubscription) error {
|
|
|
|
numChannelUpds = 0
|
|
|
|
for numChannelUpds < totalNtfns {
|
|
|
|
select {
|
|
|
|
case update := <-sub.updateChan:
|
|
|
|
switch update.Type {
|
2019-09-29 13:13:01 +03:00
|
|
|
case lnrpc.ChannelEventUpdate_PENDING_OPEN_CHANNEL:
|
|
|
|
if numChannelUpds%3 != 0 {
|
|
|
|
return fmt.Errorf("expected " +
|
|
|
|
"open or active" +
|
|
|
|
"channel ntfn, got pending open " +
|
2019-02-12 04:23:47 +03:00
|
|
|
"channel ntfn instead")
|
|
|
|
}
|
|
|
|
case lnrpc.ChannelEventUpdate_OPEN_CHANNEL:
|
2019-09-29 13:13:01 +03:00
|
|
|
if numChannelUpds%3 != 1 {
|
|
|
|
return fmt.Errorf("expected " +
|
|
|
|
"pending open or active" +
|
2019-02-12 04:23:47 +03:00
|
|
|
"channel ntfn, got open" +
|
|
|
|
"channel ntfn instead")
|
|
|
|
}
|
2019-09-29 13:13:01 +03:00
|
|
|
case lnrpc.ChannelEventUpdate_ACTIVE_CHANNEL:
|
|
|
|
if numChannelUpds%3 != 2 {
|
|
|
|
return fmt.Errorf("expected " +
|
|
|
|
"pending open or open" +
|
|
|
|
"channel ntfn, got active " +
|
|
|
|
"channel ntfn instead")
|
|
|
|
}
|
2019-02-12 04:23:47 +03:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("update type mismatch: "+
|
|
|
|
"expected open or active channel "+
|
|
|
|
"notification, got: %v",
|
|
|
|
update.Type)
|
|
|
|
}
|
|
|
|
numChannelUpds++
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
return fmt.Errorf("timeout waiting for channel "+
|
|
|
|
"notifications, only received %d/%d "+
|
|
|
|
"chanupds", numChannelUpds,
|
|
|
|
totalNtfns)
|
2018-11-02 06:20:21 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-12 04:23:47 +03:00
|
|
|
|
|
|
|
return nil
|
2018-11-02 06:20:21 +03:00
|
|
|
}
|
|
|
|
|
2019-02-12 04:23:47 +03:00
|
|
|
if err := verifyOpenUpdatesReceived(bobChanSub); err != nil {
|
|
|
|
t.Fatalf("error verifying open updates: %v", err)
|
|
|
|
}
|
|
|
|
if err := verifyOpenUpdatesReceived(aliceChanSub); err != nil {
|
|
|
|
t.Fatalf("error verifying open updates: %v", err)
|
|
|
|
}
|
2018-11-02 06:20:21 +03:00
|
|
|
|
|
|
|
// 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,
|
2020-02-21 14:24:24 +03:00
|
|
|
forceType lnrpc.ChannelCloseSummary_ClosureType,
|
2020-04-03 10:09:39 +03:00
|
|
|
closeInitiator lnrpc.Initiator) error {
|
2018-11-02 06:20:21 +03:00
|
|
|
|
|
|
|
// Ensure one inactive and one closed notification is received for each
|
|
|
|
// closed channel.
|
|
|
|
numChannelUpds := 0
|
|
|
|
for numChannelUpds < 2*numChannels {
|
2020-02-21 14:24:24 +03:00
|
|
|
expectedCloseType := lnrpc.ChannelCloseSummary_COOPERATIVE_CLOSE
|
|
|
|
|
|
|
|
// Every other channel should be force closed. If this
|
|
|
|
// channel was force closed, set the expected close type
|
|
|
|
// the the type passed in.
|
2018-11-02 06:20:21 +03:00
|
|
|
force := (numChannelUpds/2)%2 == 0
|
2020-02-21 14:24:24 +03:00
|
|
|
if force {
|
|
|
|
expectedCloseType = forceType
|
|
|
|
}
|
2018-11-02 06:20:21 +03:00
|
|
|
|
|
|
|
select {
|
|
|
|
case chanUpdate := <-sub.updateChan:
|
2020-02-21 14:24:24 +03:00
|
|
|
err := verifyCloseUpdate(
|
|
|
|
chanUpdate, expectedCloseType,
|
|
|
|
closeInitiator,
|
|
|
|
)
|
2018-11-02 06:20:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
numChannelUpds++
|
|
|
|
case err := <-sub.errChan:
|
|
|
|
return err
|
|
|
|
case <-time.After(time.Second * 10):
|
2020-02-21 14:24:24 +03:00
|
|
|
return fmt.Errorf("timeout waiting "+
|
|
|
|
"for channel notifications, only "+
|
|
|
|
"received %d/%d chanupds",
|
|
|
|
numChannelUpds, 2*numChannels)
|
2018-11-02 06:20:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify Bob receives all closed channel notifications. He should
|
|
|
|
// receive a remote force close notification for force closed channels.
|
2020-02-21 14:24:24 +03:00
|
|
|
// All channels (cooperatively and force closed) should have a remote
|
|
|
|
// close initiator because Alice closed the channels.
|
2018-11-02 06:20:21 +03:00
|
|
|
if err := verifyCloseUpdatesReceived(bobChanSub,
|
2020-02-21 14:24:24 +03:00
|
|
|
lnrpc.ChannelCloseSummary_REMOTE_FORCE_CLOSE,
|
2020-04-03 10:09:39 +03:00
|
|
|
lnrpc.Initiator_INITIATOR_REMOTE); err != nil {
|
2018-11-02 06:20:21 +03:00
|
|
|
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.
|
2020-02-21 14:24:24 +03:00
|
|
|
// All channels (cooperatively and force closed) should have a local
|
|
|
|
// close initiator because Alice closed the channels.
|
2018-11-02 06:20:21 +03:00
|
|
|
if err := verifyCloseUpdatesReceived(aliceChanSub,
|
2020-02-21 14:24:24 +03:00
|
|
|
lnrpc.ChannelCloseSummary_LOCAL_FORCE_CLOSE,
|
2020-04-03 10:09:39 +03:00
|
|
|
lnrpc.Initiator_INITIATOR_LOCAL); err != nil {
|
2018-11-02 06:20:21 +03:00
|
|
|
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()
|
|
|
|
|
2020-05-13 16:47:45 +03:00
|
|
|
maxPendingChannels := lncfg.DefaultMaxPendingChannels + 1
|
2020-11-17 02:28:56 +03:00
|
|
|
amount := funding.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)
|
2020-04-03 00:29:50 +03:00
|
|
|
stream := openChannelStream(
|
|
|
|
ctxt, t, net, 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
|
|
|
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")
|
2019-09-20 11:55:20 +03:00
|
|
|
} else if !strings.Contains(
|
|
|
|
err.Error(), lnwire.ErrMaxPendingChannels.Error(),
|
|
|
|
) {
|
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
|
|
|
}
|
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
fundingTxID, err := lnrpc.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 13:01:42 +03:00
|
|
|
// getNTxsFromMempool polls until finding the desired number of transactions in
|
|
|
|
// the provided miner's mempool and returns the full transactions to the caller.
|
|
|
|
func getNTxsFromMempool(miner *rpcclient.Client, n int,
|
|
|
|
timeout time.Duration) ([]*wire.MsgTx, error) {
|
|
|
|
|
|
|
|
txids, err := waitForNTxsInMempool(miner, n, timeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var txes []*wire.MsgTx
|
|
|
|
for _, txid := range txids {
|
|
|
|
tx, err := miner.GetRawTransaction(txid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
txes = append(txes, tx.MsgTx())
|
|
|
|
}
|
|
|
|
return txes, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 12:19:27 +03:00
|
|
|
// getTxFee retrieves parent transactions and reconstructs the fee paid.
|
|
|
|
func getTxFee(miner *rpcclient.Client, tx *wire.MsgTx) (btcutil.Amount, error) {
|
|
|
|
var balance btcutil.Amount
|
|
|
|
for _, in := range tx.TxIn {
|
|
|
|
parentHash := in.PreviousOutPoint.Hash
|
|
|
|
rawTx, err := miner.GetRawTransaction(&parentHash)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
parent := rawTx.MsgTx()
|
|
|
|
balance += btcutil.Amount(
|
|
|
|
parent.TxOut[in.PreviousOutPoint.Index].Value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, out := range tx.TxOut {
|
|
|
|
balance -= btcutil.Amount(out.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return balance, 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.
|
2019-08-14 20:57:31 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--hodl.bogus-settle"})
|
2018-05-18 15:04:05 +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-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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, net.Alice, net.Alice.RouterClient, 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
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-05-18 15:04:05 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-05-18 15:04:05 +03:00
|
|
|
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.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-05-18 15:04:05 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-05-18 15:04:05 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", predErr)
|
|
|
|
}
|
|
|
|
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-05-18 15:04:05 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-05-18 15:04:05 +03:00
|
|
|
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.
|
2020-03-09 20:43:47 +03:00
|
|
|
_, err = net.Miner.Node.Generate(defaultCSV - 1)
|
2018-05-18 15:04:05 +03:00
|
|
|
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.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-05-18 15:04:05 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-05-18 15:04:05 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-12-08 20:54:18 +03:00
|
|
|
require.Eventually(t.t, func() bool {
|
2018-06-15 22:33:49 +03:00
|
|
|
return isConnected(net.Bob.PubKeyStr)
|
2020-12-08 20:54:18 +03:00
|
|
|
}, defaultTimeout, 20*time.Millisecond)
|
|
|
|
require.Eventually(t.t, func() bool {
|
2018-06-15 22:33:49 +03:00
|
|
|
return isConnected(carol.PubKeyStr)
|
2020-12-08 20:54:18 +03:00
|
|
|
}, defaultTimeout, 20*time.Millisecond)
|
2018-06-15 22:33:49 +03:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-12-08 20:54:18 +03:00
|
|
|
require.Eventually(t.t, func() bool {
|
2018-06-15 22:33:49 +03:00
|
|
|
return isConnected(net.Bob.PubKeyStr)
|
2020-12-08 20:54:18 +03:00
|
|
|
}, defaultTimeout, 20*time.Millisecond)
|
|
|
|
require.Eventually(t.t, func() bool {
|
2018-06-15 22:33:49 +03:00
|
|
|
return isConnected(carol.PubKeyStr)
|
2020-12-08 20:54:18 +03:00
|
|
|
}, defaultTimeout, 20*time.Millisecond)
|
|
|
|
require.Eventually(t.t, func() bool {
|
|
|
|
return isConnected(dave.PubKeyStr)
|
|
|
|
}, defaultTimeout, 20*time.Millisecond)
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-09-11 11:25:48 +03:00
|
|
|
return isConnected(dave.PubKeyStr)
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
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)
|
|
|
|
}
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Invariant(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)
|
|
|
|
}
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-09-11 11:25:48 +03:00
|
|
|
return isConnected(dave.PubKeyStr)
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-09-11 11:25:48 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't reconnect to Dave")
|
|
|
|
}
|
|
|
|
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Invariant(func() bool {
|
2018-09-11 11:25:48 +03:00
|
|
|
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.
|
2020-10-06 21:56:10 +03:00
|
|
|
_, err = net.Miner.Node.Generate(chainreg.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{}
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-09-07 02:57:28 +03:00
|
|
|
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 (
|
2020-11-17 02:28:56 +03:00
|
|
|
chanAmt = funding.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
|
2020-01-18 01:19:56 +03:00
|
|
|
// protection logic automatically. We also can't have Carol
|
|
|
|
// automatically re-connect too early, otherwise DLP would be initiated
|
|
|
|
// instead of the breach we want to provoke.
|
2018-07-12 12:02:51 +03:00
|
|
|
carol, err := net.NewNode(
|
|
|
|
"Carol",
|
2020-01-18 01:19:56 +03:00
|
|
|
[]string{"--hodl.exit-settle", "--nolisten", "--minbackoff=1h"},
|
2018-07-12 12:02:51 +03:00
|
|
|
)
|
|
|
|
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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, carol, carol.RouterClient, bobPayReqs[:numInvoices/2],
|
|
|
|
true,
|
|
|
|
)
|
2017-10-26 01:32:00 +03:00
|
|
|
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
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
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)
|
|
|
|
}
|
|
|
|
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.
|
2020-10-26 18:36:09 +03:00
|
|
|
if err := lntest.CopyAll(bobTempDbPath, net.Bob.DBDir()); 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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, carol, carol.RouterClient, bobPayReqs[numInvoices/2:],
|
|
|
|
true,
|
|
|
|
)
|
2017-10-26 01:32:00 +03:00
|
|
|
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 {
|
2020-10-26 18:36:09 +03:00
|
|
|
return lntest.CopyAll(net.Bob.DBDir(), bobTempDbPath)
|
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
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
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)
|
2020-01-08 15:06:59 +03:00
|
|
|
|
|
|
|
// Mine enough blocks for Bob's channel arbitrator to wrap up the
|
|
|
|
// references to the breached channel. The chanarb waits for commitment
|
|
|
|
// tx's confHeight+CSV-1 blocks and since we've already mined one that
|
|
|
|
// included the justice tx we only need to mine extra DefaultCSV-2
|
|
|
|
// blocks to unlock it.
|
|
|
|
mineBlocks(t, net, lntest.DefaultCSV-2, 0)
|
|
|
|
|
|
|
|
assertNumPendingChannels(t, net.Bob, 0, 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 (
|
2020-11-17 02:28:56 +03:00
|
|
|
chanAmt = funding.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.
|
2019-08-14 20:57:31 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--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
|
2020-01-18 01:19:56 +03:00
|
|
|
// protection logic automatically. We also can't have Dave automatically
|
|
|
|
// re-connect too early, otherwise DLP would be initiated instead of the
|
|
|
|
// breach we want to provoke.
|
2018-07-12 12:02:51 +03:00
|
|
|
dave, err := net.NewNode(
|
|
|
|
"Dave",
|
2020-01-18 01:19:56 +03:00
|
|
|
[]string{"--hodl.exit-settle", "--nolisten", "--minbackoff=1h"},
|
2018-07-12 12:02:51 +03:00
|
|
|
)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
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.
|
2020-10-26 18:36:09 +03:00
|
|
|
if err := lntest.CopyAll(carolTempDbPath, carol.DBDir()); 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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, dave, dave.RouterClient, 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 {
|
2020-10-26 18:36:09 +03:00
|
|
|
return lntest.CopyAll(carol.DBDir(), carolTempDbPath)
|
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
|
|
|
|
)
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
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 (
|
2020-11-17 02:28:56 +03:00
|
|
|
chanAmt = funding.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.
|
2019-08-14 20:57:31 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--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",
|
2019-08-14 20:57:31 +03:00
|
|
|
[]string{"--hodl.exit-settle", "--nolisten"},
|
2018-07-12 12:02:51 +03:00
|
|
|
)
|
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
|
2020-11-17 02:28:56 +03:00
|
|
|
// funding.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(
|
2020-03-31 13:44:18 +03:00
|
|
|
ctxt, dave, dave.RouterClient, 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(
|
2020-03-31 13:44:18 +03:00
|
|
|
ctxt, carol, carol.RouterClient, 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
|
|
|
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.
|
2020-10-26 18:36:09 +03:00
|
|
|
if err := lntest.CopyAll(carolTempDbPath, carol.DBDir()); 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(
|
2020-03-31 13:44:18 +03:00
|
|
|
ctxt, dave, dave.RouterClient, 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
|
|
|
|
2019-11-21 15:36:13 +03:00
|
|
|
// Suspend Dave, such that Carol won't reconnect at startup, triggering
|
|
|
|
// the data loss protection.
|
|
|
|
restartDave, err := net.SuspendNode(dave)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to suspend Dave: %v", err)
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-10-26 18:36:09 +03:00
|
|
|
return lntest.CopyAll(carol.DBDir(), carolTempDbPath)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2019-11-21 15:36:13 +03:00
|
|
|
// We resurrect Dave to ensure he will be exacting justice after his
|
|
|
|
// node restarts.
|
|
|
|
if err := restartDave(); err != nil {
|
2018-04-05 03:02:56 +03:00
|
|
|
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
|
|
|
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-07-17 10:13:06 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-07-17 10:13:06 +03:00
|
|
|
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)
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-07-17 10:13:06 +03:00
|
|
|
txid, err := findJusticeTx()
|
|
|
|
if err != nil {
|
|
|
|
predErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
justiceTxid = txid
|
|
|
|
return true
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-07-17 10:13:06 +03:00
|
|
|
}
|
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) {
|
|
|
|
|
2020-12-01 15:52:49 +03:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
anchors bool
|
|
|
|
}{{
|
|
|
|
name: "anchors",
|
|
|
|
anchors: true,
|
|
|
|
}, {
|
|
|
|
name: "legacy",
|
|
|
|
anchors: false,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
success := t.t.Run(tc.name, func(tt *testing.T) {
|
|
|
|
ht := newHarnessTest(tt, net)
|
|
|
|
ht.RunTestCase(&testCase{
|
|
|
|
name: tc.name,
|
|
|
|
test: func(net1 *lntest.NetworkHarness, t1 *harnessTest) {
|
|
|
|
testRevokedCloseRetributionAltruistWatchtowerCase(
|
|
|
|
net1, t1, tc.anchors,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if !success {
|
|
|
|
// Log failure time to help relate the lnd logs to the
|
|
|
|
// failure.
|
|
|
|
t.Logf("Failure time: %v", time.Now().Format(
|
|
|
|
"2006-01-02 15:04:05.000",
|
|
|
|
))
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 02:08:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func testRevokedCloseRetributionAltruistWatchtowerCase(
|
|
|
|
net *lntest.NetworkHarness, t *harnessTest, anchors bool) {
|
|
|
|
|
2019-06-14 03:32:29 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
const (
|
2020-11-17 02:28:56 +03:00
|
|
|
chanAmt = funding.MaxBtcFundingAmount
|
2019-06-14 03:32:29 +03:00
|
|
|
paymentAmt = 10000
|
|
|
|
numInvoices = 6
|
2019-06-21 02:55:52 +03:00
|
|
|
externalIP = "1.2.3.4"
|
2019-06-14 03:32:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Since we'd like to test some multi-hop failure scenarios, we'll
|
|
|
|
// introduce another node into our test network: Carol.
|
2020-11-26 02:08:30 +03:00
|
|
|
carolArgs := []string{"--hodl.exit-settle"}
|
|
|
|
if anchors {
|
|
|
|
carolArgs = append(carolArgs, "--protocol.anchors")
|
|
|
|
}
|
|
|
|
carol, err := net.NewNode("Carol", carolArgs)
|
2019-06-14 03:32:29 +03:00
|
|
|
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.
|
2019-06-21 02:55:52 +03:00
|
|
|
willy, err := net.NewNode("Willy", []string{
|
|
|
|
"--watchtower.active",
|
|
|
|
"--watchtower.externalip=" + externalIP,
|
|
|
|
})
|
2019-06-14 03:32:29 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, willy)
|
|
|
|
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2019-06-08 03:46:31 +03:00
|
|
|
willyInfo, err := willy.Watchtower.GetInfo(
|
2019-06-21 02:55:52 +03:00
|
|
|
ctxt, &watchtowerrpc.GetInfoRequest{},
|
|
|
|
)
|
2019-06-14 03:32:29 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to getinfo from willy: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-06-21 02:55:52 +03:00
|
|
|
// Assert that Willy has one listener and it is 0.0.0.0:9911 or
|
|
|
|
// [::]:9911. Since no listener is explicitly specified, one of these
|
|
|
|
// should be the default depending on whether the host supports IPv6 or
|
|
|
|
// not.
|
|
|
|
if len(willyInfo.Listeners) != 1 {
|
|
|
|
t.Fatalf("Willy should have 1 listener, has %d",
|
|
|
|
len(willyInfo.Listeners))
|
|
|
|
}
|
|
|
|
listener := willyInfo.Listeners[0]
|
|
|
|
if listener != "0.0.0.0:9911" && listener != "[::]:9911" {
|
|
|
|
t.Fatalf("expected listener on 0.0.0.0:9911 or [::]:9911, "+
|
|
|
|
"got %v", listener)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert the Willy's URIs properly display the chosen external IP.
|
|
|
|
if len(willyInfo.Uris) != 1 {
|
|
|
|
t.Fatalf("Willy should have 1 uri, has %d",
|
|
|
|
len(willyInfo.Uris))
|
|
|
|
}
|
|
|
|
if !strings.Contains(willyInfo.Uris[0], externalIP) {
|
|
|
|
t.Fatalf("expected uri with %v, got %v",
|
|
|
|
externalIP, willyInfo.Uris[0])
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:32:29 +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.
|
2020-11-26 02:08:30 +03:00
|
|
|
daveArgs := []string{
|
2019-06-14 03:32:29 +03:00
|
|
|
"--nolisten",
|
2019-07-04 05:54:28 +03:00
|
|
|
"--wtclient.active",
|
2020-11-26 02:08:30 +03:00
|
|
|
}
|
|
|
|
if anchors {
|
|
|
|
daveArgs = append(daveArgs, "--protocol.anchors")
|
|
|
|
}
|
|
|
|
dave, err := net.NewNode("Dave", daveArgs)
|
2019-06-14 03:32:29 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
2019-07-04 05:54:28 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
addTowerReq := &wtclientrpc.AddTowerRequest{
|
|
|
|
Pubkey: willyInfo.Pubkey,
|
|
|
|
Address: listener,
|
|
|
|
}
|
|
|
|
if _, err := dave.WatchtowerClient.AddTower(ctxt, addTowerReq); err != nil {
|
|
|
|
t.Fatalf("unable to add willy's watchtower: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:32:29 +03:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
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.
|
2020-10-26 18:36:09 +03:00
|
|
|
if err := lntest.CopyAll(carolTempDbPath, carol.DBDir()); err != nil {
|
2019-06-14 03:32:29 +03:00
|
|
|
t.Fatalf("unable to copy database files: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, send payments from Dave to Carol, consuming Carol's remaining
|
|
|
|
// payment hashes.
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxb, dave, dave.RouterClient, carolPayReqs, false,
|
|
|
|
)
|
2019-06-14 03:32:29 +03:00
|
|
|
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
|
|
|
|
|
2020-06-02 19:55:55 +03:00
|
|
|
// Wait until the backup has been accepted by the watchtower before
|
|
|
|
// shutting down Dave.
|
|
|
|
err = wait.NoError(func() error {
|
|
|
|
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
defer cancel()
|
2020-11-26 02:08:30 +03:00
|
|
|
bkpStats, err := dave.WatchtowerClient.Stats(ctxt,
|
|
|
|
&wtclientrpc.StatsRequest{},
|
|
|
|
)
|
2020-06-02 19:55:55 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
if bkpStats == nil {
|
|
|
|
return errors.New("no active backup sessions")
|
|
|
|
}
|
|
|
|
if bkpStats.NumBackups == 0 {
|
|
|
|
return errors.New("no backups accepted")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, defaultTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to verify backup task completed: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:32:29 +03:00
|
|
|
// 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 {
|
2020-10-26 18:36:09 +03:00
|
|
|
return lntest.CopyAll(carol.DBDir(), carolTempDbPath)
|
2019-06-14 03:32:29 +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.
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Invariant(func() bool {
|
2019-06-14 03:32:29 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2019-06-14 03:32:29 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-08-06 13:08:44 +03:00
|
|
|
if daveBalResp.LocalBalance.Sat != 0 {
|
2019-06-14 03:32:29 +03:00
|
|
|
predErr = fmt.Errorf("Dave should end up with zero "+
|
|
|
|
"channel balance, instead has %d",
|
2020-08-06 13:08:44 +03:00
|
|
|
daveBalResp.LocalBalance.Sat)
|
2019-06-14 03:32:29 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2019-06-14 03:32:29 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", predErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNumPendingChannels(t, dave, 0, 0)
|
|
|
|
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2019-06-14 03:32:29 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2019-06-14 03:32:29 +03:00
|
|
|
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
|
2019-09-19 22:46:29 +03:00
|
|
|
err := wait.Predicate(func() bool {
|
2018-07-12 12:02:54 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-07-12 12:02:54 +03:00
|
|
|
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,
|
2020-03-10 16:01:39 +03:00
|
|
|
dave *lntest.HarnessNode, daveStartingBalance int64,
|
|
|
|
anchors bool) {
|
2019-03-12 02:28:47 +03:00
|
|
|
|
2020-09-04 12:19:27 +03:00
|
|
|
// Increase the fee estimate so that the following force close tx will
|
|
|
|
// be cpfp'ed.
|
|
|
|
net.SetFeeEstimate(30000)
|
|
|
|
|
2020-01-18 01:19:56 +03:00
|
|
|
// We disabled auto-reconnect for some tests to avoid timing issues.
|
|
|
|
// To make sure the nodes are initiating DLP now, we have to manually
|
|
|
|
// re-connect them.
|
|
|
|
ctxb := context.Background()
|
|
|
|
err := net.ConnectNodes(ctxb, carol, dave)
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "already connected") {
|
|
|
|
t.Fatalf("unable to connect Carol to Dave to initiate DLP: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2019-03-12 02:28:47 +03:00
|
|
|
// Upon reconnection, the nodes should detect that Dave is out of sync.
|
|
|
|
// Carol should force close the channel using her latest commitment.
|
2020-03-10 16:01:39 +03:00
|
|
|
expectedTxes := 1
|
|
|
|
if anchors {
|
|
|
|
expectedTxes = 2
|
|
|
|
}
|
2020-01-18 01:19:56 +03:00
|
|
|
_, err = waitForNTxsInMempool(
|
2020-03-10 16:01:39 +03:00
|
|
|
net.Miner.Node, expectedTxes, minerMempoolTimeout,
|
2019-03-12 02:28:47 +03:00
|
|
|
)
|
|
|
|
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.
|
2020-01-10 17:27:50 +03:00
|
|
|
_ = mineBlocks(t, net, 1, expectedTxes)[0]
|
2019-03-12 02:28:47 +03:00
|
|
|
|
|
|
|
// Dave should sweep his funds immediately, as they are not timelocked.
|
2020-03-10 16:01:39 +03:00
|
|
|
// We also expect Dave to sweep his anchor, if present.
|
|
|
|
|
|
|
|
_, err = waitForNTxsInMempool(
|
|
|
|
net.Miner.Node, expectedTxes, minerMempoolTimeout,
|
2019-09-18 13:46:44 +03:00
|
|
|
)
|
2019-03-12 02:28:47 +03:00
|
|
|
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.
|
2020-01-10 17:27:50 +03:00
|
|
|
_ = mineBlocks(t, net, 1, expectedTxes)[0]
|
2019-03-12 02:28:47 +03:00
|
|
|
|
|
|
|
// 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.
|
2020-03-09 20:43:47 +03:00
|
|
|
//
|
|
|
|
// The commit sweep resolver publishes the sweep tx at defaultCSV-1 and
|
|
|
|
// we already mined one block after the commitmment was published, so
|
|
|
|
// take that into account.
|
|
|
|
mineBlocks(t, net, defaultCSV-1-1, 0)
|
2019-03-12 02:28:47 +03:00
|
|
|
carolSweep, err := waitForTxInMempool(
|
|
|
|
net.Miner.Node, minerMempoolTimeout,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find Carol's sweep tx in mempool: %v", err)
|
|
|
|
}
|
2020-01-10 17:27:50 +03:00
|
|
|
block := mineBlocks(t, net, 1, 1)[0]
|
2019-03-12 02:28:47 +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.
|
|
|
|
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 (
|
2020-11-17 02:28:56 +03:00
|
|
|
chanAmt = funding.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
|
2020-01-18 01:19:56 +03:00
|
|
|
// protection logic automatically. We also can't have Carol
|
|
|
|
// automatically re-connect too early, otherwise DLP would be initiated
|
|
|
|
// at the wrong moment.
|
|
|
|
carol, err := net.NewNode(
|
|
|
|
"Carol", []string{"--nolisten", "--minbackoff=1h"},
|
|
|
|
)
|
2018-07-12 12:02:54 +03:00
|
|
|
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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, carol, carol.RouterClient,
|
|
|
|
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
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-11-20 17:09:44 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
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.
|
2020-10-26 18:36:09 +03:00
|
|
|
if err := lntest.CopyAll(tempDbPath, node.DBDir()); 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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, carol, carol.RouterClient,
|
|
|
|
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 {
|
2020-10-26 18:36:09 +03:00
|
|
|
return lntest.CopyAll(node.DBDir(), tempDbPath)
|
2018-11-20 17:09:44 +03:00
|
|
|
}); 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)
|
|
|
|
}
|
2020-03-14 04:52:33 +03:00
|
|
|
|
2018-11-20 17:09:44 +03:00
|
|
|
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,
|
2020-03-10 16:01:39 +03:00
|
|
|
false,
|
2019-03-12 02:28:47 +03:00
|
|
|
)
|
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.
|
2020-03-09 20:43:47 +03:00
|
|
|
mineBlocks(t, net, defaultCSV-1, 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-09-19 22:46:29 +03:00
|
|
|
err = wait.NoError(func() error {
|
2019-04-23 14:07:43 +03:00
|
|
|
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
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2019-04-23 14:07:43 +03:00
|
|
|
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
|
|
|
|
2020-12-08 18:27:01 +03:00
|
|
|
if err := wait.Predicate(pred, defaultTimeout); err != nil {
|
2017-12-15 00:56:10 +03:00
|
|
|
t.Fatalf("node has incorrect number of channels: %v", predErr)
|
2017-08-01 09:17:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 20:37:50 +03:00
|
|
|
// testRejectHTLC tests that a node can be created with the flag --rejecthtlc.
|
|
|
|
// This means that the node will reject all forwarded HTLCs but can still
|
|
|
|
// accept direct HTLCs as well as send HTLCs.
|
|
|
|
func testRejectHTLC(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
// RejectHTLC
|
|
|
|
// Alice ------> Carol ------> Bob
|
|
|
|
//
|
|
|
|
const chanAmt = btcutil.Amount(1000000)
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// Create Carol with reject htlc flag.
|
|
|
|
carol, err := net.NewNode("Carol", []string{"--rejecthtlc"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new node: %v", err)
|
|
|
|
}
|
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
|
|
|
// Connect Alice to Carol.
|
|
|
|
if err := net.ConnectNodes(ctxb, net.Alice, carol); err != nil {
|
|
|
|
t.Fatalf("unable to connect alice to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect Carol to Bob.
|
|
|
|
if err := net.ConnectNodes(ctxb, carol, net.Bob); err != nil {
|
|
|
|
t.Fatalf("unable to conenct carol to net.Bob: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send coins to Carol.
|
|
|
|
err = net.SendCoins(ctxb, btcutil.SatoshiPerBitcoin, carol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send coins to Alice.
|
|
|
|
err = net.SendCoins(ctxb, btcutil.SatoshiPerBitcent, net.Alice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coins to alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open a channel between Alice and Carol.
|
2019-09-02 13:05:51 +03:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-11-19 20:37:50 +03:00
|
|
|
chanPointAlice := openChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Alice, carol,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Open a channel between Carol and Bob.
|
2019-09-02 13:05:51 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-11-19 20:37:50 +03:00
|
|
|
chanPointCarol := openChannelAndAssert(
|
|
|
|
ctxt, t, net, carol, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Channel should be ready for payments.
|
|
|
|
const payAmt = 100
|
|
|
|
|
|
|
|
// Helper closure to generate a random pre image.
|
|
|
|
genPreImage := func() []byte {
|
|
|
|
preimage := make([]byte, 32)
|
|
|
|
|
|
|
|
_, err = rand.Read(preimage)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate preimage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return preimage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an invoice from Carol of 100 satoshis.
|
|
|
|
// We expect Alice to be able to pay this invoice.
|
|
|
|
preimage := genPreImage()
|
|
|
|
|
|
|
|
carolInvoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing - alice should pay carol",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: payAmt,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Carol adds the invoice to her database.
|
|
|
|
resp, err := carol.AddInvoice(ctxb, carolInvoice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice pays Carols invoice.
|
2019-09-02 13:05:51 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-19 20:37:50 +03:00
|
|
|
err = completePaymentRequests(
|
2020-03-31 13:44:18 +03:00
|
|
|
ctxt, net.Alice, net.Alice.RouterClient,
|
|
|
|
[]string{resp.PaymentRequest}, true,
|
2018-11-19 20:37:50 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments from alice to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an invoice from Bob of 100 satoshis.
|
|
|
|
// We expect Carol to be able to pay this invoice.
|
|
|
|
preimage = genPreImage()
|
|
|
|
|
|
|
|
bobInvoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing - carol should pay bob",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: payAmt,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob adds the invoice to his database.
|
|
|
|
resp, err = net.Bob.AddInvoice(ctxb, bobInvoice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Carol pays Bobs invoice.
|
2019-09-02 13:05:51 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-19 20:37:50 +03:00
|
|
|
err = completePaymentRequests(
|
2020-03-31 13:44:18 +03:00
|
|
|
ctxt, carol, carol.RouterClient,
|
|
|
|
[]string{resp.PaymentRequest}, true,
|
2018-11-19 20:37:50 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments from carol to bob: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an invoice from Bob of 100 satoshis.
|
|
|
|
// Alice attempts to pay Bob but this should fail, since we are
|
|
|
|
// using Carol as a hop and her node will reject onward HTLCs.
|
|
|
|
preimage = genPreImage()
|
|
|
|
|
|
|
|
bobInvoice = &lnrpc.Invoice{
|
|
|
|
Memo: "testing - alice tries to pay bob",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: payAmt,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob adds the invoice to his database.
|
|
|
|
resp, err = net.Bob.AddInvoice(ctxb, bobInvoice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice attempts to pay Bobs invoice. This payment should be rejected since
|
|
|
|
// we are using Carol as an intermediary hop, Carol is running lnd with
|
|
|
|
// --rejecthtlc.
|
2019-09-02 13:05:51 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2018-11-19 20:37:50 +03:00
|
|
|
err = completePaymentRequests(
|
2020-03-31 13:44:18 +03:00
|
|
|
ctxt, net.Alice, net.Alice.RouterClient,
|
|
|
|
[]string{resp.PaymentRequest}, true,
|
2018-11-19 20:37:50 +03:00
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf(
|
|
|
|
"should have been rejected, carol will not accept forwarded htlcs",
|
|
|
|
)
|
|
|
|
}
|
2020-04-01 01:13:25 +03:00
|
|
|
|
|
|
|
assertLastHTLCError(t, net.Alice, lnrpc.Failure_CHANNEL_DISABLED)
|
2018-11-19 20:37:50 +03:00
|
|
|
|
|
|
|
// Close all channels.
|
2019-09-02 13:05:51 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-11-19 20:37:50 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false)
|
2019-09-02 13:05:51 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2018-11-19 20:37:50 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, carol, chanPointCarol, false)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-29 11:14:38 +03:00
|
|
|
func assertSyncType(t *harnessTest, node *lntest.HarnessNode,
|
|
|
|
peer string, syncType lnrpc.Peer_SyncType) {
|
|
|
|
|
|
|
|
t.t.Helper()
|
|
|
|
|
|
|
|
ctxb := context.Background()
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := node.ListPeers(ctxt, &lnrpc.ListPeersRequest{})
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
|
|
|
|
for _, rpcPeer := range resp.Peers {
|
|
|
|
if rpcPeer.PubKey != peer {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t.t, syncType, rpcPeer.SyncType)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.t.Fatalf("unable to find peer: %s", peer)
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:14:50 +03:00
|
|
|
func waitForGraphSync(t *harnessTest, node *lntest.HarnessNode) {
|
|
|
|
t.t.Helper()
|
|
|
|
|
|
|
|
err := wait.Predicate(func() bool {
|
|
|
|
ctxb := context.Background()
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := node.GetInfo(ctxt, &lnrpc.GetInfoRequest{})
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
|
|
|
|
return resp.SyncedToGraph
|
|
|
|
}, defaultTimeout)
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
}
|
|
|
|
|
2017-12-22 15:49:49 +03:00
|
|
|
func testGraphTopologyNotifications(net *lntest.NetworkHarness, t *harnessTest) {
|
2021-01-29 11:14:38 +03:00
|
|
|
t.t.Run("pinned", func(t *testing.T) {
|
|
|
|
ht := newHarnessTest(t, net)
|
|
|
|
testGraphTopologyNtfns(net, ht, true)
|
|
|
|
})
|
|
|
|
t.t.Run("unpinned", func(t *testing.T) {
|
|
|
|
ht := newHarnessTest(t, net)
|
|
|
|
testGraphTopologyNtfns(net, ht, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGraphTopologyNtfns(net *lntest.NetworkHarness, t *harnessTest, pinned bool) {
|
2017-12-22 15:49:49 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
2020-11-17 02:28:56 +03:00
|
|
|
const chanAmt = funding.MaxBtcFundingAmount
|
2018-11-28 11:25:46 +03:00
|
|
|
|
2021-01-29 11:14:38 +03:00
|
|
|
// Spin up Bob first, since we will need to grab his pubkey when
|
|
|
|
// starting Alice to test pinned syncing.
|
2021-01-29 11:14:29 +03:00
|
|
|
bob, err := net.NewNode("bob", nil)
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
defer shutdownAndAssert(net, t, bob)
|
|
|
|
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2021-01-29 11:14:38 +03:00
|
|
|
bobInfo, err := bob.GetInfo(ctxt, &lnrpc.GetInfoRequest{})
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
bobPubkey := bobInfo.IdentityPubkey
|
|
|
|
|
|
|
|
// For unpinned syncing, start Alice as usual. Otherwise grab Bob's
|
|
|
|
// pubkey to include in his pinned syncer set.
|
|
|
|
var aliceArgs []string
|
|
|
|
if pinned {
|
|
|
|
aliceArgs = []string{
|
2021-01-23 00:01:29 +03:00
|
|
|
"--numgraphsyncpeers=0",
|
2021-01-29 11:14:38 +03:00
|
|
|
fmt.Sprintf("--gossip.pinned-syncers=%s", bobPubkey),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
alice, err := net.NewNode("alice", aliceArgs)
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
defer shutdownAndAssert(net, t, alice)
|
|
|
|
|
|
|
|
// Connect Alice and Bob.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2021-01-29 11:14:29 +03:00
|
|
|
err = net.EnsureConnected(ctxt, alice, bob)
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
|
|
|
|
// Alice stimmy.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, alice)
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
|
|
|
|
// Bob stimmy.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, bob)
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
|
2021-01-29 11:14:38 +03:00
|
|
|
// Assert that Bob has the correct sync type before proceeeding.
|
|
|
|
if pinned {
|
|
|
|
assertSyncType(t, alice, bobPubkey, lnrpc.Peer_PINNED_SYNC)
|
|
|
|
} else {
|
|
|
|
assertSyncType(t, alice, bobPubkey, lnrpc.Peer_ACTIVE_SYNC)
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:14:50 +03:00
|
|
|
// Regardless of syncer type, ensure that both peers report having
|
|
|
|
// completed their initial sync before continuing to make a channel.
|
|
|
|
waitForGraphSync(t, alice)
|
|
|
|
|
2017-12-22 15:49:49 +03:00
|
|
|
// Let Alice subscribe to graph notifications.
|
2018-08-23 08:16:16 +03:00
|
|
|
graphSub := subscribeGraphNotifications(
|
2021-01-29 11:14:29 +03:00
|
|
|
t, ctxb, alice,
|
2018-08-23 08:16:16 +03:00
|
|
|
)
|
|
|
|
defer close(graphSub.quit)
|
2017-12-22 15:49:49 +03:00
|
|
|
|
|
|
|
// Open a new channel between Alice and Bob.
|
2021-01-29 11:14:29 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
2018-03-15 20:02:40 +03:00
|
|
|
chanPoint := openChannelAndAssert(
|
2021-01-29 11:14:29 +03:00
|
|
|
ctxt, t, net, alice, bob,
|
2018-08-22 10:32:42 +03:00
|
|
|
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 {
|
2021-01-29 11:14:29 +03:00
|
|
|
case alice.PubKeyStr:
|
|
|
|
case bob.PubKeyStr:
|
2017-07-14 22:47:52 +03:00
|
|
|
default:
|
|
|
|
t.Fatalf("unknown advertising node: %v",
|
|
|
|
chanUpdate.AdvertisingNode)
|
|
|
|
}
|
|
|
|
switch chanUpdate.ConnectingNode {
|
2021-01-29 11:14:29 +03:00
|
|
|
case alice.PubKeyStr:
|
|
|
|
case bob.PubKeyStr:
|
2017-07-14 22:47:52 +03:00
|
|
|
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 {
|
2021-01-29 11:14:29 +03:00
|
|
|
case alice.PubKeyStr:
|
|
|
|
case bob.PubKeyStr:
|
2017-07-14 22:47:52 +03:00
|
|
|
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)
|
2021-01-29 11:14:29 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, alice, chanPoint, false)
|
2017-03-14 08:23:19 +03:00
|
|
|
|
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)
|
|
|
|
}
|
2021-02-13 11:05:33 +03:00
|
|
|
chanPointTxid, err := lnrpc.GetChanPointFundingTxid(chanPoint)
|
2018-06-15 04:16:28 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
2021-02-13 11:05:33 +03:00
|
|
|
closedChanTxid, err := lnrpc.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)
|
2021-01-29 11:14:29 +03:00
|
|
|
if err := net.DisconnectNodes(ctxt, alice, bob); err != nil {
|
2017-07-14 22:47:52 +03:00
|
|
|
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)
|
2021-01-29 11:14:29 +03:00
|
|
|
if err := net.ConnectNodes(ctxt, 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(
|
2021-01-29 11:14:29 +03:00
|
|
|
ctxt, t, net, bob, carol,
|
2018-08-22 10:32:42 +03:00
|
|
|
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)
|
2021-01-29 11:14:29 +03:00
|
|
|
if err := net.EnsureConnected(ctxt, alice, 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:
|
2021-01-29 11:14:29 +03:00
|
|
|
case bob.PubKeyStr:
|
2017-07-14 22:47:52 +03:00
|
|
|
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:
|
2021-01-29 11:14:29 +03:00
|
|
|
case bob.PubKeyStr:
|
2017-07-14 22:47:52 +03:00
|
|
|
default:
|
|
|
|
t.Fatalf("unknown advertising node: %v",
|
|
|
|
chanUpdate.AdvertisingNode)
|
|
|
|
}
|
|
|
|
switch chanUpdate.ConnectingNode {
|
|
|
|
case carol.PubKeyStr:
|
2021-01-29 11:14:29 +03:00
|
|
|
case bob.PubKeyStr:
|
2017-07-14 22:47:52 +03:00
|
|
|
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)
|
2021-01-29 11:14:29 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, 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)
|
|
|
|
}
|
|
|
|
|
2019-04-25 02:06:14 +03:00
|
|
|
// Alice shouldn't receive any new updates yet since the channel has yet
|
|
|
|
// to be opened.
|
|
|
|
select {
|
|
|
|
case <-aliceSub.updateChan:
|
|
|
|
t.Fatalf("received unexpected update from dave")
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll then go ahead and open a channel between Bob and Dave. This
|
|
|
|
// ensures that Alice receives the node announcement from Bob as part of
|
|
|
|
// the announcement broadcast.
|
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
|
|
|
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)
|
2020-12-08 18:27:01 +03:00
|
|
|
case <-time.After(defaultTimeout):
|
2018-05-24 09:54:12 +03:00
|
|
|
t.Fatalf("did not receive node ann update")
|
2017-02-23 03:24:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-24 09:54:12 +03:00
|
|
|
|
2019-04-25 02:06:14 +03:00
|
|
|
// We'll then wait for Alice to receive Dave's node announcement
|
|
|
|
// including the expected advertised addresses from Bob since they
|
|
|
|
// should already be connected.
|
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()
|
|
|
|
|
2020-11-17 02:28:56 +03:00
|
|
|
chanAmt := funding.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
|
|
|
}
|
|
|
|
|
2020-04-07 21:56:47 +03:00
|
|
|
// testAsyncPayments tests the performance of the async payments.
|
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)
|
|
|
|
}
|
|
|
|
|
2020-04-07 21:56:47 +03:00
|
|
|
// We'll create a number of invoices equal the max number of HTLCs that
|
|
|
|
// can be carried in one direction. The number on the commitment will
|
|
|
|
// likely be lower, but we can't guarantee that any more HTLCs will
|
|
|
|
// succeed due to the limited path diversity and inability of the router
|
|
|
|
// to retry via another path.
|
|
|
|
numInvoices := int(input.MaxHTLCNumber / 2)
|
2018-02-22 13:14:41 +03:00
|
|
|
|
2017-05-17 19:06:52 +03:00
|
|
|
bobAmt := int64(numInvoices * paymentAmt)
|
|
|
|
aliceAmt := info.LocalBalance - bobAmt
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
// Simultaneously send payments from Alice to Bob using of Bob's payment
|
|
|
|
// hashes generated above.
|
2017-05-17 19:06:52 +03:00
|
|
|
now := time.Now()
|
2020-03-31 13:44:18 +03:00
|
|
|
errChan := make(chan error)
|
|
|
|
statusChan := make(chan *lnrpc.Payment)
|
2017-05-17 19:06:52 +03:00
|
|
|
for i := 0; i < numInvoices; i++ {
|
2020-03-31 13:44:18 +03:00
|
|
|
payReq := bobPayReqs[i]
|
|
|
|
go func() {
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, lntest.AsyncBenchmarkTimeout)
|
|
|
|
stream, err := net.Alice.RouterClient.SendPaymentV2(
|
|
|
|
ctxt,
|
|
|
|
&routerrpc.SendPaymentRequest{
|
|
|
|
PaymentRequest: payReq,
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
result, err := getPaymentResult(stream)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
2017-05-17 19:06:52 +03:00
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
statusChan <- result
|
|
|
|
}()
|
2017-05-17 19:06:52 +03:00
|
|
|
}
|
|
|
|
|
2020-04-07 21:56:47 +03:00
|
|
|
// Wait until all the payments have settled.
|
2017-05-17 19:06:52 +03:00
|
|
|
for i := 0; i < numInvoices; i++ {
|
2020-03-31 13:44:18 +03:00
|
|
|
select {
|
|
|
|
case result := <-statusChan:
|
|
|
|
if result.Status == lnrpc.Payment_SUCCEEDED {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
case err := <-errChan:
|
|
|
|
t.Fatalf("payment error: %v", err)
|
2017-05-17 19:06:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2019-05-30 19:26:24 +03:00
|
|
|
|
|
|
|
// Wait for the revocation to be received so alice no longer has pending
|
|
|
|
// htlcs listed and has correct balances. This is needed due to the fact
|
|
|
|
// that we now pipeline the settles.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2019-05-30 19:26:24 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
aliceChan, err := getChanInfo(ctxt, net.Alice)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(aliceChan.PendingHtlcs) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if aliceChan.RemoteBalance != bobAmt {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if aliceChan.LocalBalance != aliceAmt {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, time.Second*5)
|
2017-05-17 19:06:52 +03:00
|
|
|
if err != nil {
|
2019-05-30 19:26:24 +03:00
|
|
|
t.Fatalf("failed to assert alice's pending htlcs and/or remote/local balance")
|
2017-05-17 19:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-04-07 21:56:47 +03:00
|
|
|
// We'll create a number of invoices equal the max number of HTLCs that
|
|
|
|
// can be carried in one direction. The number on the commitment will
|
|
|
|
// likely be lower, but we can't guarantee that any more HTLCs will
|
|
|
|
// succeed due to the limited path diversity and inability of the router
|
|
|
|
// to retry via another path.
|
|
|
|
numInvoices := int(input.MaxHTLCNumber / 2)
|
2017-05-18 17:36:55 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2019-07-29 16:10:58 +03:00
|
|
|
// Reset mission control to prevent previous payment results from
|
|
|
|
// interfering with this test. A new channel has been opened, but
|
|
|
|
// mission control operates on node pairs.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
_, err = net.Alice.RouterClient.ResetMissionControl(
|
|
|
|
ctxt, &routerrpc.ResetMissionControlRequest{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to reset mc for alice: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-18 17:36:55 +03:00
|
|
|
// Send payments from Alice to Bob and from Bob to Alice in async
|
|
|
|
// manner.
|
2020-03-31 13:44:18 +03:00
|
|
|
errChan := make(chan error)
|
|
|
|
statusChan := make(chan *lnrpc.Payment)
|
2017-05-18 17:36:55 +03:00
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
send := func(node *lntest.HarnessNode, payReq string) {
|
|
|
|
go func() {
|
|
|
|
ctxt, _ = context.WithTimeout(
|
|
|
|
ctxb, lntest.AsyncBenchmarkTimeout,
|
|
|
|
)
|
|
|
|
stream, err := node.RouterClient.SendPaymentV2(
|
|
|
|
ctxt,
|
|
|
|
&routerrpc.SendPaymentRequest{
|
|
|
|
PaymentRequest: payReq,
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
result, err := getPaymentResult(stream)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
2017-05-18 17:36:55 +03:00
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
statusChan <- result
|
|
|
|
}()
|
2017-05-18 17:36:55 +03:00
|
|
|
}
|
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
send(net.Bob, alicePayReqs[i])
|
|
|
|
send(net.Alice, bobPayReqs[i])
|
|
|
|
}
|
2017-05-18 17:36:55 +03:00
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
// Expect all payments to succeed.
|
|
|
|
for i := 0; i < 2*numInvoices; i++ {
|
|
|
|
select {
|
|
|
|
case result := <-statusChan:
|
|
|
|
if result.Status != lnrpc.Payment_SUCCEEDED {
|
|
|
|
t.Fatalf("payment error: %v", result.Status)
|
2017-05-18 17:36:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case err := <-errChan:
|
2020-03-31 13:44:18 +03:00
|
|
|
t.Fatalf("payment error: %v", err)
|
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 {
|
2020-12-09 14:24:02 +03:00
|
|
|
h := hex.EncodeToString(htlc.HashLock)
|
|
|
|
_, ok := htlcHashes[h]
|
2018-04-13 18:14:42 +03:00
|
|
|
if ok {
|
|
|
|
return fmt.Errorf("duplicate HashLock")
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
2020-12-09 14:24:02 +03:00
|
|
|
htlcHashes[h] = struct{}{}
|
2018-04-13 18:14:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2020-12-09 14:24:02 +03:00
|
|
|
h := hex.EncodeToString(payHash)
|
|
|
|
if _, ok := htlcHashes[h]; 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[:],
|
2020-12-09 14:24:02 +03:00
|
|
|
h)
|
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,
|
2020-03-19 13:01:42 +03:00
|
|
|
timeout time.Duration, chanPoint wire.OutPoint) chainhash.Hash {
|
|
|
|
|
|
|
|
tx := getSpendingTxInMempool(t, miner, timeout, chanPoint)
|
|
|
|
return tx.TxHash()
|
|
|
|
}
|
|
|
|
|
|
|
|
// getSpendingTxInMempool waits for a transaction spending the given outpoint to
|
|
|
|
// appear in the mempool and returns that tx in full.
|
|
|
|
func getSpendingTxInMempool(t *harnessTest, miner *rpcclient.Client,
|
|
|
|
timeout time.Duration, chanPoint wire.OutPoint) *wire.MsgTx {
|
2018-01-23 05:59:44 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-03-19 13:01:42 +03:00
|
|
|
msgTx := tx.MsgTx()
|
|
|
|
for _, txIn := range msgTx.TxIn {
|
2018-01-23 05:59:44 +03:00
|
|
|
if txIn.PreviousOutPoint == chanPoint {
|
2020-03-19 13:01:42 +03:00
|
|
|
return msgTx
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-23 05:59:44 +03:00
|
|
|
|
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-01-23 05:59:44 +03:00
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
networkChans = append(networkChans, chanPointAlice)
|
2018-01-23 05:59:44 +03:00
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
aliceChanTXID, err := lnrpc.GetChanPointFundingTxid(chanPointAlice)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
aliceFundPoint := wire.OutPoint{
|
|
|
|
Hash: *aliceChanTXID,
|
|
|
|
Index: chanPointAlice.OutputIndex,
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
2018-02-23 01:29:06 +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
|
|
|
|
//
|
|
|
|
// 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-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
2018-06-08 00:14:39 +03:00
|
|
|
defer shutdownAndAssert(net, t, dave)
|
2018-01-23 05:59:44 +03:00
|
|
|
|
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-01-23 05:59:44 +03:00
|
|
|
}
|
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-01-23 05:59:44 +03:00
|
|
|
}
|
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-01-23 05:59:44 +03:00
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
networkChans = append(networkChans, chanPointDave)
|
2021-02-13 11:05:33 +03:00
|
|
|
daveChanTXID, err := lnrpc.GetChanPointFundingTxid(chanPointDave)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
2018-02-23 01:29:06 +03:00
|
|
|
daveFundPoint := wire.OutPoint{
|
|
|
|
Hash: *daveChanTXID,
|
|
|
|
Index: chanPointDave.OutputIndex,
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
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 htlchodl mode so that we can disconnect the
|
|
|
|
// intermediary hops before starting the settle.
|
2019-08-14 20:57:31 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--hodl.exit-settle"})
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("unable to create new nodes: %v", err)
|
2018-01-23 05:59:44 +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 {
|
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-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("unable to send coins to carol: %v", err)
|
2018-01-23 05:59:44 +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,
|
|
|
|
PushAmt: pushAmt,
|
|
|
|
},
|
2018-03-15 20:02:40 +03:00
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
networkChans = append(networkChans, chanPointCarol)
|
2018-01-23 05:59:44 +03:00
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
carolChanTXID, err := lnrpc.GetChanPointFundingTxid(chanPointCarol)
|
2018-01-23 05:59:44 +03:00
|
|
|
if err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
2018-02-23 01:29:06 +03:00
|
|
|
carolFundPoint := wire.OutPoint{
|
|
|
|
Hash: *carolChanTXID,
|
|
|
|
Index: chanPointCarol.OutputIndex,
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
// 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 {
|
2021-02-13 11:05:33 +03:00
|
|
|
txid, err := lnrpc.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-01-23 05:59:44 +03:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
// 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,
|
2019-07-24 04:00:30 +03:00
|
|
|
)
|
2018-11-20 17:09:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
2018-01-23 05:59:44 +03:00
|
|
|
}
|
|
|
|
|
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)
|
2018-01-11 07:59:30 +03:00
|
|
|
if err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("dave didn't advertise his channel: %v", err)
|
2018-01-11 07:59:30 +03:00
|
|
|
}
|
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)
|
2019-08-27 04:17:30 +03:00
|
|
|
if err != nil {
|
2018-02-23 01:29:06 +03:00
|
|
|
t.Fatalf("carol didn't advertise her channel in time: %v",
|
|
|
|
err)
|
2019-08-27 04:17:30 +03:00
|
|
|
}
|
2018-01-23 05:59:44 +03:00
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, net.Bob, net.Bob.RouterClient, payReqs, false,
|
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
|
|
|
}
|
2018-04-13 14:20:12 +03:00
|
|
|
|
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
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, numPayments)
|
2018-01-23 05:59:44 +03:00
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
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
|
|
|
}
|
|
|
|
|
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)
|
2018-05-25 05:40:56 +03:00
|
|
|
}
|
2018-01-23 05:59:44 +03:00
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
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.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, numPayments)
|
2020-01-10 17:27:50 +03:00
|
|
|
return predErr == nil
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
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.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, 0)
|
2020-09-24 15:51:58 +03:00
|
|
|
return predErr == nil
|
2018-08-14 10:09:02 +03:00
|
|
|
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, net.Bob, net.Bob.RouterClient, payReqs, true,
|
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
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)
|
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
aliceChanTXID, err := lnrpc.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.
|
2019-11-20 07:13: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)
|
2021-02-13 11:05:33 +03:00
|
|
|
daveChanTXID, err := lnrpc.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.
|
2019-08-14 20:57:31 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--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)
|
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
carolChanTXID, err := lnrpc.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 {
|
2021-02-13 11:05:33 +03:00
|
|
|
txid, err := lnrpc.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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, net.Bob, net.Bob.RouterClient, payReqs, false,
|
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
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
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, numPayments)
|
2020-11-18 17:26:27 +03:00
|
|
|
return predErr == nil
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
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.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Invariant(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, numPayments)
|
2020-01-10 17:27:50 +03:00
|
|
|
return predErr == nil
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
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}
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(carolNode, 0)
|
2020-01-10 17:27:50 +03:00
|
|
|
return predErr == nil
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
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.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2021-01-29 12:37:37 +03:00
|
|
|
return assertNumActiveHtlcs(nodes, 0) == nil
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, net.Bob, net.Bob.RouterClient, payReqs, true,
|
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
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)
|
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
aliceChanTXID, err := lnrpc.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.
|
2019-11-20 07:13: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-08-22 10:32:42 +03:00
|
|
|
|
2018-02-23 01:29:06 +03:00
|
|
|
networkChans = append(networkChans, chanPointDave)
|
2021-02-13 11:05:33 +03:00
|
|
|
daveChanTXID, err := lnrpc.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.
|
2019-08-14 20:57:31 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--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)
|
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
carolChanTXID, err := lnrpc.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 {
|
2021-02-13 11:05:33 +03:00
|
|
|
txid, err := lnrpc.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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, net.Bob, net.Bob.RouterClient, payReqs, false,
|
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payments: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-14 10:09:02 +03:00
|
|
|
var predErr error
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodes, numPayments)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
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.
|
|
|
|
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}
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(carolNode, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
predErr = assertNumActiveHtlcsChanPoint(dave, carolFundPoint, 0)
|
2020-01-10 17:27:50 +03:00
|
|
|
return predErr == nil
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
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.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2021-01-29 12:37:37 +03:00
|
|
|
return assertNumActiveHtlcs(nodes, 0) == nil
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, net.Bob, net.Bob.RouterClient, payReqs, true,
|
|
|
|
)
|
2018-02-23 01:29:06 +03:00
|
|
|
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)
|
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
aliceChanTXID, err := lnrpc.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.
|
2019-11-20 07:13:04 +03:00
|
|
|
dave, err := net.NewNode("Dave", nil)
|
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)
|
2021-02-13 11:05:33 +03:00
|
|
|
daveChanTXID, err := lnrpc.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.
|
2019-08-14 20:57:31 +03:00
|
|
|
carol, err := net.NewNode("Carol", []string{"--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)
|
|
|
|
|
2021-02-13 11:05:33 +03:00
|
|
|
carolChanTXID, err := lnrpc.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 {
|
2021-02-13 11:05:33 +03:00
|
|
|
txid, err := lnrpc.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)
|
2020-03-31 13:44:18 +03:00
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, net.Bob, net.Bob.RouterClient, payReqs, false,
|
|
|
|
)
|
2018-02-24 03:45:50 +03:00
|
|
|
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
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2021-01-29 12:37:37 +03:00
|
|
|
return assertNumActiveHtlcs(nodes, numPayments) == nil
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
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-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}
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(carolNode, 0)
|
|
|
|
if predErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
predErr = assertNumActiveHtlcsChanPoint(dave, carolFundPoint, 0)
|
2020-01-10 17:27:50 +03:00
|
|
|
return predErr == nil
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
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}
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
2018-08-14 10:09:02 +03:00
|
|
|
predErr = assertNumActiveHtlcs(nodesMinusCarol, 0)
|
2021-03-04 02:15:02 +03:00
|
|
|
return predErr == nil
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2018-03-13 10:13:57 +03:00
|
|
|
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 {
|
2021-02-13 11:05:33 +03:00
|
|
|
txid, err := lnrpc.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 11:57:15 +03:00
|
|
|
// While we're here, we test updating mission control's config values
|
|
|
|
// and assert that they are correctly updated.
|
|
|
|
testMissionControlCfg(t.t, net.Alice)
|
|
|
|
|
2018-04-11 12:18:58 +03:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2021-01-19 11:57:15 +03:00
|
|
|
// testMissionControlCfg tests getting and setting of a node's mission control
|
|
|
|
// config, resetting to the original values after testing so that no other
|
|
|
|
// tests are affected.
|
|
|
|
func testMissionControlCfg(t *testing.T, node *lntest.HarnessNode) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
startCfg, err := node.RouterClient.GetMissionControlConfig(
|
|
|
|
ctxb, &routerrpc.GetMissionControlConfigRequest{},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cfg := &routerrpc.MissionControlConfig{
|
|
|
|
HalfLifeSeconds: 8000,
|
|
|
|
HopProbability: 0.8,
|
|
|
|
Weight: 0.3,
|
|
|
|
MaximumPaymentResults: 30,
|
|
|
|
MinimumFailureRelaxInterval: 60,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = node.RouterClient.SetMissionControlConfig(
|
|
|
|
ctxb, &routerrpc.SetMissionControlConfigRequest{
|
|
|
|
Config: cfg,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp, err := node.RouterClient.GetMissionControlConfig(
|
|
|
|
ctxb, &routerrpc.GetMissionControlConfigRequest{},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Set the hidden fields on the cfg we set so that we can use require
|
|
|
|
// equal rather than comparing field by field.
|
|
|
|
cfg.XXX_sizecache = resp.XXX_sizecache
|
|
|
|
cfg.XXX_NoUnkeyedLiteral = resp.XXX_NoUnkeyedLiteral
|
|
|
|
cfg.XXX_unrecognized = resp.XXX_unrecognized
|
|
|
|
require.Equal(t, cfg, resp.Config)
|
|
|
|
|
|
|
|
_, err = node.RouterClient.SetMissionControlConfig(
|
|
|
|
ctxb, &routerrpc.SetMissionControlConfigRequest{
|
|
|
|
Config: startCfg.Config,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-02-13 11:05:33 +03:00
|
|
|
txid, err := lnrpc.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)
|
2020-10-06 21:56:10 +03:00
|
|
|
timeLockDelta := uint32(chainreg.DefaultBitcoinTimeLockDelta)
|
2019-08-20 03:55:30 +03:00
|
|
|
maxHtlc := calculateMaxHtlc(chanAmt)
|
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
|
2019-08-20 03:55:30 +03:00
|
|
|
MaxHtlcMsat: maxHtlc,
|
2018-04-30 23:56:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
updateFeeReq := &lnrpc.PolicyUpdateRequest{
|
|
|
|
BaseFeeMsat: baseFee,
|
|
|
|
FeeRate: float64(feeRate),
|
|
|
|
TimeLockDelta: timeLockDelta,
|
2019-08-20 03:55:30 +03:00
|
|
|
MaxHtlcMsat: maxHtlc,
|
2018-04-30 23:56:02 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
sendReq := &routerrpc.SendPaymentRequest{
|
2018-04-30 23:56:02 +03:00
|
|
|
PaymentRequest: invoiceResp.PaymentRequest,
|
2020-03-31 13:44:18 +03:00
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
2018-04-30 23:56:02 +03:00
|
|
|
}
|
2020-03-31 13:44:18 +03:00
|
|
|
switch limit := feeLimit.Limit.(type) {
|
|
|
|
case *lnrpc.FeeLimit_Fixed:
|
|
|
|
sendReq.FeeLimitMsat = 1000 * limit.Fixed
|
|
|
|
case *lnrpc.FeeLimit_Percent:
|
|
|
|
sendReq.FeeLimitMsat = 1000 * paymentAmt * limit.Percent / 100
|
2018-04-30 23:56:02 +03:00
|
|
|
}
|
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
result := sendAndAssertSuccess(t, net.Alice, sendReq)
|
|
|
|
|
|
|
|
checkRoute(result.Htlcs[0].Route)
|
2018-04-30 23:56:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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",
|
|
|
|
"--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{
|
2020-10-06 21:56:10 +03:00
|
|
|
FeeBaseMsat: int64(chainreg.DefaultBitcoinBaseFeeMSat),
|
|
|
|
FeeRateMilliMsat: int64(chainreg.DefaultBitcoinFeeRate),
|
|
|
|
TimeLockDelta: chainreg.DefaultBitcoinTimeLockDelta,
|
2018-08-22 10:32:43 +03:00
|
|
|
MinHtlc: 1000, // default value
|
2019-08-20 03:55:30 +03:00
|
|
|
MaxHtlcMsat: calculateMaxHtlc(chanAmt),
|
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)
|
2019-08-07 14:12:32 +03:00
|
|
|
|
|
|
|
// And finally, clean up the force closed channel by mining the
|
|
|
|
// sweeping transaction.
|
|
|
|
cleanupForceClose(t, net, net.Alice, chanPointAliceCarol)
|
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{
|
2020-11-17 02:28:56 +03:00
|
|
|
Amt: funding.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(
|
2019-11-14 03:09:20 +03:00
|
|
|
ctxt, t, net, net.Alice, net.Bob, channelParam,
|
|
|
|
)
|
2021-02-13 11:05:33 +03:00
|
|
|
txid, err := lnrpc.GetChanPointFundingTxid(chanPoint)
|
2019-11-14 03:09:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get txid: %v", err)
|
|
|
|
}
|
|
|
|
chanPointStr := fmt.Sprintf("%v:%v", txid, chanPoint.OutputIndex)
|
2018-06-20 10:09:02 +03:00
|
|
|
|
|
|
|
// Wait for channel to be confirmed open.
|
2018-11-27 11:18:35 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2019-11-14 03:09:20 +03:00
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
2018-06-20 10:09:02 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-11-14 03:09:20 +03:00
|
|
|
// Now that the channel is open, we'll obtain its channel ID real quick
|
|
|
|
// so we can use it to query the graph below.
|
|
|
|
listReq := &lnrpc.ListChannelsRequest{}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
aliceChannelList, err := net.Alice.ListChannels(ctxt, listReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch alice's channels: %v", err)
|
|
|
|
}
|
|
|
|
var chanID uint64
|
|
|
|
for _, channel := range aliceChannelList.Channels {
|
|
|
|
if channel.ChannelPoint == chanPointStr {
|
|
|
|
chanID = channel.ChanId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if chanID == 0 {
|
|
|
|
t.Fatalf("unable to find channel")
|
|
|
|
}
|
|
|
|
|
2020-03-04 12:57:06 +03:00
|
|
|
// To make sure the channel is removed from the backup file as well when
|
|
|
|
// being abandoned, grab a backup snapshot so we can compare it with the
|
|
|
|
// later state.
|
|
|
|
bkupBefore, err := ioutil.ReadFile(net.Alice.ChanBackupPath())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not get channel backup before abandoning "+
|
|
|
|
"channel: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-06-20 10:09:02 +03:00
|
|
|
// 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.
|
2018-11-28 11:25:46 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2019-11-14 03:09:20 +03:00
|
|
|
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)
|
|
|
|
}
|
2020-01-10 17:27:50 +03:00
|
|
|
if len(alicePendingList.PendingClosingChannels) != 0 { //nolint:staticcheck
|
2018-06-20 10:09:02 +03:00
|
|
|
t.Fatalf("alice should only have no pending closing channels, "+
|
|
|
|
"instead she has %v",
|
2020-01-10 17:27:50 +03:00
|
|
|
len(alicePendingList.PendingClosingChannels)) //nolint:staticcheck
|
2018-06-20 10:09:02 +03:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2019-11-14 03:09:20 +03:00
|
|
|
// Ensure that the channel can no longer be found in the channel graph.
|
|
|
|
_, err = net.Alice.GetChanInfo(ctxb, &lnrpc.ChanInfoRequest{
|
|
|
|
ChanId: chanID,
|
|
|
|
})
|
|
|
|
if !strings.Contains(err.Error(), "marked as zombie") {
|
|
|
|
t.Fatalf("channel shouldn't be found in the channel " +
|
|
|
|
"graph!")
|
|
|
|
}
|
|
|
|
|
2020-03-04 12:57:06 +03:00
|
|
|
// Make sure the channel is no longer in the channel backup list.
|
2020-03-24 14:18:42 +03:00
|
|
|
err = wait.Predicate(func() bool {
|
|
|
|
bkupAfter, err := ioutil.ReadFile(net.Alice.ChanBackupPath())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not get channel backup before "+
|
|
|
|
"abandoning channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(bkupAfter) < len(bkupBefore)
|
|
|
|
}, defaultTimeout)
|
2020-03-04 12:57:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("channel wasn't removed from channel backup file")
|
|
|
|
}
|
|
|
|
|
2019-11-14 03:09:20 +03:00
|
|
|
// Calling AbandonChannel again, should result in no new errors, as the
|
|
|
|
// channel has already been removed.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
_, err = net.Alice.AbandonChannel(ctxt, abandonChannelRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to abandon channel a second time: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-05-18 15:13:24 +03:00
|
|
|
// Create a label that we will used to label the transaction with.
|
|
|
|
sendCoinsLabel := "send all coins"
|
|
|
|
|
2019-04-10 05:45:55 +03:00
|
|
|
sweepReq := &lnrpc.SendCoinsRequest{
|
|
|
|
Addr: info.IdentityPubkey,
|
|
|
|
SendAll: true,
|
2020-05-18 15:13:24 +03:00
|
|
|
Label: sendCoinsLabel,
|
2019-04-10 05:45:55 +03:00
|
|
|
}
|
|
|
|
_, 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,
|
2020-05-18 15:13:24 +03:00
|
|
|
Label: sendCoinsLabel,
|
2019-04-10 05:45:55 +03:00
|
|
|
}
|
|
|
|
_, 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,
|
2020-05-18 15:13:24 +03:00
|
|
|
Label: sendCoinsLabel,
|
2019-02-27 22:16:34 +03:00
|
|
|
}
|
|
|
|
_, 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,
|
2020-05-18 15:13:24 +03:00
|
|
|
Label: sendCoinsLabel,
|
2019-02-27 22:16:34 +03:00
|
|
|
}
|
|
|
|
_, 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,
|
2020-05-18 15:13:24 +03:00
|
|
|
Label: sendCoinsLabel,
|
2018-12-25 01:05:48 +03:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2020-05-25 09:38:25 +03:00
|
|
|
sweepTxStr := sweepTx.TxHash().String()
|
|
|
|
assertTxLabel(ctxb, t, ainz, sweepTxStr, sendCoinsLabel)
|
|
|
|
|
|
|
|
// While we are looking at labels, we test our label transaction command
|
|
|
|
// to make sure it is behaving as expected. First, we try to label our
|
|
|
|
// transaction with an empty label, and check that we fail as expected.
|
|
|
|
sweepHash := sweepTx.TxHash()
|
|
|
|
_, err = ainz.WalletKitClient.LabelTransaction(
|
|
|
|
ctxt, &walletrpc.LabelTransactionRequest{
|
|
|
|
Txid: sweepHash[:],
|
|
|
|
Label: "",
|
|
|
|
Overwrite: false,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error for zero transaction label")
|
2020-05-18 15:13:24 +03:00
|
|
|
}
|
|
|
|
|
2020-05-25 09:38:25 +03:00
|
|
|
// Our error will be wrapped in a rpc error, so we check that it
|
|
|
|
// contains the error we expect.
|
2020-09-04 14:47:56 +03:00
|
|
|
errZeroLabel := "cannot label transaction with empty label"
|
|
|
|
if !strings.Contains(err.Error(), errZeroLabel) {
|
2020-05-25 09:38:25 +03:00
|
|
|
t.Fatalf("expected: zero label error, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we try to relabel our transaction without setting the overwrite
|
|
|
|
// boolean. We expect this to fail, because the wallet requires setting
|
|
|
|
// of this param to prevent accidental overwrite of labels.
|
|
|
|
_, err = ainz.WalletKitClient.LabelTransaction(
|
|
|
|
ctxt, &walletrpc.LabelTransactionRequest{
|
|
|
|
Txid: sweepHash[:],
|
|
|
|
Label: "label that will not work",
|
|
|
|
Overwrite: false,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error for tx already labelled")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our error will be wrapped in a rpc error, so we check that it
|
|
|
|
// contains the error we expect.
|
|
|
|
if !strings.Contains(err.Error(), wallet.ErrTxLabelExists.Error()) {
|
|
|
|
t.Fatalf("expected: label exists, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we overwrite our label with a new label, which should not
|
|
|
|
// fail.
|
|
|
|
newLabel := "new sweep tx label"
|
|
|
|
_, err = ainz.WalletKitClient.LabelTransaction(
|
|
|
|
ctxt, &walletrpc.LabelTransactionRequest{
|
|
|
|
Txid: sweepHash[:],
|
|
|
|
Label: newLabel,
|
|
|
|
Overwrite: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not label tx: %v", err)
|
2020-05-18 15:13:24 +03:00
|
|
|
}
|
|
|
|
|
2020-05-25 09:38:25 +03:00
|
|
|
assertTxLabel(ctxb, t, ainz, sweepTxStr, newLabel)
|
|
|
|
|
2018-12-25 01:05:48 +03:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 09:38:25 +03:00
|
|
|
// assertTxLabel is a helper function which finds a target tx in our set
|
|
|
|
// of transactions and checks that it has the desired label.
|
|
|
|
func assertTxLabel(ctx context.Context, t *harnessTest,
|
|
|
|
node *lntest.HarnessNode, targetTx, label string) {
|
|
|
|
|
|
|
|
// List all transactions relevant to our wallet, and find the tx so that
|
|
|
|
// we can check the correct label has been set.
|
|
|
|
ctxt, cancel := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
txResp, err := node.GetTransactions(
|
|
|
|
ctxt, &lnrpc.GetTransactionsRequest{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not get transactions: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find our transaction in the set of transactions returned and check
|
|
|
|
// its label.
|
|
|
|
for _, txn := range txResp.Transactions {
|
|
|
|
if txn.TxHash == targetTx {
|
|
|
|
if txn.Label != label {
|
|
|
|
t.Fatalf("expected label: %v, got: %v",
|
|
|
|
label, txn.Label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:19:21 +03:00
|
|
|
// Open a channel between Alice and Carol which is private so that we
|
|
|
|
// cover the addition of hop hints for hold invoices.
|
2019-06-07 17:42:26 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelOpenTimeout)
|
|
|
|
chanPointAlice := openChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Alice, carol,
|
|
|
|
lntest.OpenChannelParams{
|
2021-02-08 10:19:21 +03:00
|
|
|
Amt: chanAmt,
|
|
|
|
Private: true,
|
2019-06-07 17:42:26 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2020-06-10 15:21:13 +03:00
|
|
|
t.Fatalf("carol didn't see the carol->alice channel before "+
|
2019-06-07 17:42:26 +03:00
|
|
|
"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()
|
2021-02-08 10:19:21 +03:00
|
|
|
|
|
|
|
// Make our invoices private so that we get coverage for adding
|
|
|
|
// hop hints.
|
2019-06-07 17:42:26 +03:00
|
|
|
invoiceReq := &invoicesrpc.AddHoldInvoiceRequest{
|
2021-02-08 10:19:21 +03:00
|
|
|
Memo: "testing",
|
|
|
|
Value: int64(payAmt),
|
|
|
|
Hash: payHash[:],
|
|
|
|
Private: true,
|
2019-06-07 17:42:26 +03:00
|
|
|
}
|
|
|
|
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.
|
2020-04-13 11:07:53 +03:00
|
|
|
var paymentStreams []routerrpc.Router_SendPaymentV2Client
|
2019-06-07 17:42:26 +03:00
|
|
|
for _, payReq := range payReqs {
|
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-04-13 11:07:53 +03:00
|
|
|
payStream, err := net.Alice.RouterClient.SendPaymentV2(
|
2019-06-07 17:42:26 +03:00
|
|
|
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 {
|
2020-04-03 18:05:05 +03:00
|
|
|
payment, err := payStream.Recv()
|
2019-06-07 17:42:26 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed receiving status update: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:05:05 +03:00
|
|
|
if payment.Status != lnrpc.Payment_IN_FLIGHT {
|
|
|
|
t.Fatalf("state not in flight: %v", payment.Status)
|
2019-06-07 17:42:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The payments should now show up in Alice's ListInvoices, with a zero
|
|
|
|
// preimage, indicating they are not yet settled.
|
2019-09-19 22:46:29 +03:00
|
|
|
err = wait.NoError(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.
|
2020-04-09 17:28:34 +03:00
|
|
|
if len(payment.Htlcs) == 0 {
|
|
|
|
return fmt.Errorf("no attempt recorded")
|
2019-06-07 17:42:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(payHashes, payment.PaymentHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(payHashes) != 0 {
|
|
|
|
return fmt.Errorf("payhash not found in response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-12-08 18:27:01 +03:00
|
|
|
}, defaultTimeout)
|
2019-06-07 17:42:26 +03:00
|
|
|
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 (
|
2020-04-03 18:05:05 +03:00
|
|
|
statusUpdates []chan *lnrpc.Payment
|
2019-06-07 17:42:26 +03:00
|
|
|
wg sync.WaitGroup
|
|
|
|
quit = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
|
|
|
defer close(quit)
|
|
|
|
for _, preimg := range preimages {
|
|
|
|
hash := preimg.Hash()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctxb)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-04-13 11:07:53 +03:00
|
|
|
payStream, err := net.Alice.RouterClient.TrackPaymentV2(
|
2019-06-07 17:42:26 +03:00
|
|
|
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.
|
2020-04-03 18:05:05 +03:00
|
|
|
upd := make(chan *lnrpc.Payment)
|
2019-06-07 17:42:26 +03:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
for {
|
2020-04-03 18:05:05 +03:00
|
|
|
payment, err := payStream.Recv()
|
2019-06-07 17:42:26 +03:00
|
|
|
if err != nil {
|
|
|
|
close(upd)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2020-04-03 18:05:05 +03:00
|
|
|
case upd <- payment:
|
2019-06-07 17:42:26 +03:00
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
statusUpdates = append(statusUpdates, upd)
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:05:05 +03:00
|
|
|
// Wait for the in-flight status update.
|
2019-06-07 17:42:26 +03:00
|
|
|
for _, upd := range statusUpdates {
|
|
|
|
select {
|
2020-04-03 18:05:05 +03:00
|
|
|
case payment, ok := <-upd:
|
2019-06-07 17:42:26 +03:00
|
|
|
if !ok {
|
2020-04-03 18:05:05 +03:00
|
|
|
t.Fatalf("failed getting payment update")
|
2019-06-07 17:42:26 +03:00
|
|
|
}
|
|
|
|
|
2020-04-03 18:05:05 +03:00
|
|
|
if payment.Status != lnrpc.Payment_IN_FLIGHT {
|
2019-06-07 17:42:26 +03:00
|
|
|
t.Fatalf("state not in in flight: %v",
|
2020-04-03 18:05:05 +03:00
|
|
|
payment.Status)
|
2019-06-07 17:42:26 +03:00
|
|
|
}
|
|
|
|
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 {
|
2020-04-03 18:05:05 +03:00
|
|
|
// Read until the payment is in a terminal state.
|
|
|
|
var payment *lnrpc.Payment
|
|
|
|
for payment == nil {
|
|
|
|
select {
|
|
|
|
case p, ok := <-upd:
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("failed getting payment update")
|
2019-06-07 17:42:26 +03:00
|
|
|
}
|
2020-04-03 18:05:05 +03:00
|
|
|
|
|
|
|
if p.Status == lnrpc.Payment_IN_FLIGHT {
|
|
|
|
continue
|
2019-06-07 17:42:26 +03:00
|
|
|
}
|
2020-04-03 18:05:05 +03:00
|
|
|
|
|
|
|
payment = p
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("in flight status not recevied")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert terminal payment state.
|
|
|
|
if i%2 == 0 {
|
|
|
|
if payment.Status != lnrpc.Payment_SUCCEEDED {
|
2020-01-10 17:27:50 +03:00
|
|
|
t.Fatalf("state not succeeded : %v",
|
2020-04-03 18:05:05 +03:00
|
|
|
payment.Status)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if payment.FailureReason !=
|
|
|
|
lnrpc.PaymentFailureReason_FAILURE_REASON_INCORRECT_PAYMENT_DETAILS {
|
|
|
|
|
|
|
|
t.Fatalf("state not failed: %v",
|
|
|
|
payment.FailureReason)
|
2019-06-07 17:42:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-12-11 07:02:49 +03:00
|
|
|
// testExternalFundingChanPoint tests that we're able to carry out a normal
|
|
|
|
// channel funding workflow given a channel point that was constructed outside
|
|
|
|
// the main daemon.
|
|
|
|
func testExternalFundingChanPoint(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// First, we'll create two new nodes that we'll use to open channel
|
|
|
|
// between for this test.
|
|
|
|
carol, err := net.NewNode("carol", nil)
|
2020-08-21 14:47:18 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-12-11 07:02:49 +03:00
|
|
|
defer shutdownAndAssert(net, t, carol)
|
|
|
|
|
|
|
|
dave, err := net.NewNode("dave", nil)
|
2020-08-21 14:47:18 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-12-11 07:02:49 +03:00
|
|
|
defer shutdownAndAssert(net, t, dave)
|
|
|
|
|
|
|
|
// Carol will be funding the channel, so we'll send some coins over to
|
|
|
|
// her and ensure they have enough confirmations before we proceed.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.SendCoins(ctxt, btcutil.SatoshiPerBitcoin, carol)
|
2020-08-21 14:47:18 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-12-11 07:02:49 +03:00
|
|
|
|
|
|
|
// Before we start the test, we'll ensure both sides are connected to
|
|
|
|
// the funding flow can properly be executed.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.EnsureConnected(ctxt, carol, dave)
|
2020-08-21 14:47:18 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-12-11 07:02:49 +03:00
|
|
|
|
2020-08-21 14:44:25 +03:00
|
|
|
// At this point, we're ready to simulate our external channel funding
|
2020-08-21 15:12:30 +03:00
|
|
|
// flow. To start with, we'll create a pending channel with a shim for
|
|
|
|
// a transaction that will never be published.
|
|
|
|
const thawHeight uint32 = 10
|
2020-11-17 02:28:56 +03:00
|
|
|
const chanSize = funding.MaxBtcFundingAmount
|
2020-08-21 15:12:30 +03:00
|
|
|
fundingShim1, chanPoint1, _ := deriveFundingShim(
|
|
|
|
net, t, carol, dave, chanSize, thawHeight, 1, false,
|
|
|
|
)
|
|
|
|
_ = openChannelStream(
|
|
|
|
ctxb, t, net, carol, dave, lntest.OpenChannelParams{
|
|
|
|
Amt: chanSize,
|
|
|
|
FundingShim: fundingShim1,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
assertNumOpenChannelsPending(ctxt, t, carol, dave, 1)
|
|
|
|
|
|
|
|
// That channel is now pending forever and normally would saturate the
|
|
|
|
// max pending channel limit for both nodes. But because the channel is
|
|
|
|
// externally funded, we should still be able to open another one. Let's
|
|
|
|
// do exactly that now. For this one we publish the transaction so we
|
|
|
|
// can mine it later.
|
|
|
|
fundingShim2, chanPoint2, _ := deriveFundingShim(
|
|
|
|
net, t, carol, dave, chanSize, thawHeight, 2, true,
|
2020-08-21 14:44:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// At this point, we'll now carry out the normal basic channel funding
|
|
|
|
// test as everything should now proceed as normal (a regular channel
|
|
|
|
// funding flow).
|
|
|
|
carolChan, daveChan, _, err := basicChannelFundingTest(
|
2020-08-21 15:12:30 +03:00
|
|
|
t, net, carol, dave, fundingShim2,
|
2020-08-21 14:44:25 +03:00
|
|
|
)
|
2020-08-21 14:47:18 +03:00
|
|
|
require.NoError(t.t, err)
|
2020-08-21 14:44:25 +03:00
|
|
|
|
|
|
|
// Both channels should be marked as frozen with the proper thaw
|
|
|
|
// height.
|
|
|
|
if carolChan.ThawHeight != thawHeight {
|
|
|
|
t.Fatalf("expected thaw height of %v, got %v",
|
|
|
|
carolChan.ThawHeight, thawHeight)
|
|
|
|
}
|
|
|
|
if daveChan.ThawHeight != thawHeight {
|
|
|
|
t.Fatalf("expected thaw height of %v, got %v",
|
|
|
|
daveChan.ThawHeight, thawHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, to make sure the channel functions as normal, we'll make some
|
|
|
|
// payments within the channel.
|
|
|
|
payAmt := btcutil.Amount(100000)
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "new chans",
|
|
|
|
Value: int64(payAmt),
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
resp, err := dave.AddInvoice(ctxt, invoice)
|
2020-08-21 14:47:18 +03:00
|
|
|
require.NoError(t.t, err)
|
2020-08-21 14:44:25 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = completePaymentRequests(
|
|
|
|
ctxt, carol, carol.RouterClient, []string{resp.PaymentRequest},
|
|
|
|
true,
|
|
|
|
)
|
2020-08-21 14:47:18 +03:00
|
|
|
require.NoError(t.t, err)
|
2020-08-21 14:44:25 +03:00
|
|
|
|
|
|
|
// Now that the channels are open, and we've confirmed that they're
|
|
|
|
// operational, we'll now ensure that the channels are frozen as
|
|
|
|
// intended (if requested).
|
|
|
|
//
|
|
|
|
// First, we'll try to close the channel as Carol, the initiator. This
|
|
|
|
// should fail as a frozen channel only allows the responder to
|
|
|
|
// initiate a channel close.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2020-08-21 15:12:30 +03:00
|
|
|
_, _, err = net.CloseChannel(ctxt, carol, chanPoint2, false)
|
2020-08-21 14:44:25 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("carol wasn't denied a co-op close attempt for a " +
|
|
|
|
"frozen channel")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next we'll try but this time with Dave (the responder) as the
|
|
|
|
// initiator. This time the channel should be closed as normal.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
2020-08-21 15:12:30 +03:00
|
|
|
closeChannelAndAssert(ctxt, t, net, dave, chanPoint2, false)
|
|
|
|
|
|
|
|
// As a last step, we check if we still have the pending channel hanging
|
|
|
|
// around because we never published the funding TX.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
assertNumOpenChannelsPending(ctxt, t, carol, dave, 1)
|
|
|
|
|
|
|
|
// Let's make sure we can abandon it.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
_, err = carol.AbandonChannel(ctxt, &lnrpc.AbandonChannelRequest{
|
|
|
|
ChannelPoint: chanPoint1,
|
|
|
|
PendingFundingShimOnly: true,
|
|
|
|
})
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
_, err = dave.AbandonChannel(ctxt, &lnrpc.AbandonChannelRequest{
|
|
|
|
ChannelPoint: chanPoint1,
|
|
|
|
PendingFundingShimOnly: true,
|
|
|
|
})
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
|
|
|
|
// It should now not appear in the pending channels anymore.
|
|
|
|
assertNumOpenChannelsPending(ctxt, t, carol, dave, 0)
|
2020-08-21 14:44:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// deriveFundingShim creates a channel funding shim by deriving the necessary
|
|
|
|
// keys on both sides.
|
|
|
|
func deriveFundingShim(net *lntest.NetworkHarness, t *harnessTest,
|
2020-08-21 15:12:30 +03:00
|
|
|
carol, dave *lntest.HarnessNode, chanSize btcutil.Amount,
|
|
|
|
thawHeight uint32, keyIndex int32, publish bool) (*lnrpc.FundingShim,
|
|
|
|
*lnrpc.ChannelPoint, *chainhash.Hash) {
|
2020-08-21 14:44:25 +03:00
|
|
|
|
|
|
|
ctxb := context.Background()
|
2019-12-11 07:02:49 +03:00
|
|
|
keyLoc := &signrpc.KeyLocator{
|
|
|
|
KeyFamily: 9999,
|
2020-08-21 14:44:25 +03:00
|
|
|
KeyIndex: keyIndex,
|
2019-12-11 07:02:49 +03:00
|
|
|
}
|
|
|
|
carolFundingKey, err := carol.WalletKitClient.DeriveKey(ctxb, keyLoc)
|
2020-08-21 14:47:18 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-12-11 07:02:49 +03:00
|
|
|
daveFundingKey, err := dave.WalletKitClient.DeriveKey(ctxb, keyLoc)
|
2020-08-21 14:47:18 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-12-11 07:02:49 +03:00
|
|
|
|
|
|
|
// Now that we have the multi-sig keys for each party, we can manually
|
|
|
|
// construct the funding transaction. We'll instruct the backend to
|
|
|
|
// immediately create and broadcast a transaction paying out an exact
|
|
|
|
// amount. Normally this would reside in the mempool, but we just
|
|
|
|
// confirm it now for simplicity.
|
|
|
|
_, fundingOutput, err := input.GenFundingPkScript(
|
|
|
|
carolFundingKey.RawKeyBytes, daveFundingKey.RawKeyBytes,
|
|
|
|
int64(chanSize),
|
|
|
|
)
|
2020-08-21 14:47:18 +03:00
|
|
|
require.NoError(t.t, err)
|
2020-08-21 15:12:30 +03:00
|
|
|
|
|
|
|
var txid *chainhash.Hash
|
|
|
|
targetOutputs := []*wire.TxOut{fundingOutput}
|
|
|
|
if publish {
|
|
|
|
txid, err = net.Miner.SendOutputsWithoutChange(
|
|
|
|
targetOutputs, 5,
|
|
|
|
)
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
} else {
|
|
|
|
tx, err := net.Miner.CreateTransaction(targetOutputs, 5, false)
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
|
|
|
|
txHash := tx.TxHash()
|
|
|
|
txid = &txHash
|
|
|
|
}
|
2019-12-11 07:02:49 +03:00
|
|
|
|
|
|
|
// At this point, we can being our external channel funding workflow.
|
|
|
|
// We'll start by generating a pending channel ID externally that will
|
|
|
|
// be used to track this new funding type.
|
|
|
|
var pendingChanID [32]byte
|
2020-08-21 14:47:18 +03:00
|
|
|
_, err = rand.Read(pendingChanID[:])
|
|
|
|
require.NoError(t.t, err)
|
2019-12-11 07:02:49 +03:00
|
|
|
|
|
|
|
// Now that we have the pending channel ID, Dave (our responder) will
|
|
|
|
// register the intent to receive a new channel funding workflow using
|
|
|
|
// the pending channel ID.
|
2020-03-14 03:14:30 +03:00
|
|
|
chanPoint := &lnrpc.ChannelPoint{
|
|
|
|
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
|
|
|
|
FundingTxidBytes: txid[:],
|
2019-12-11 07:02:49 +03:00
|
|
|
},
|
2020-03-14 03:14:30 +03:00
|
|
|
}
|
|
|
|
chanPointShim := &lnrpc.ChanPointShim{
|
|
|
|
Amt: int64(chanSize),
|
|
|
|
ChanPoint: chanPoint,
|
2019-12-11 07:02:49 +03:00
|
|
|
LocalKey: &lnrpc.KeyDescriptor{
|
|
|
|
RawKeyBytes: daveFundingKey.RawKeyBytes,
|
|
|
|
KeyLoc: &lnrpc.KeyLocator{
|
|
|
|
KeyFamily: daveFundingKey.KeyLoc.KeyFamily,
|
|
|
|
KeyIndex: daveFundingKey.KeyLoc.KeyIndex,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RemoteKey: carolFundingKey.RawKeyBytes,
|
|
|
|
PendingChanId: pendingChanID[:],
|
2020-03-14 03:14:30 +03:00
|
|
|
ThawHeight: thawHeight,
|
2019-12-11 07:02:49 +03:00
|
|
|
}
|
|
|
|
fundingShim := &lnrpc.FundingShim{
|
|
|
|
Shim: &lnrpc.FundingShim_ChanPointShim{
|
|
|
|
ChanPointShim: chanPointShim,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err = dave.FundingStateStep(ctxb, &lnrpc.FundingTransitionMsg{
|
|
|
|
Trigger: &lnrpc.FundingTransitionMsg_ShimRegister{
|
|
|
|
ShimRegister: fundingShim,
|
|
|
|
},
|
|
|
|
})
|
2020-08-21 14:47:18 +03:00
|
|
|
require.NoError(t.t, err)
|
2019-12-11 07:02:49 +03:00
|
|
|
|
|
|
|
// If we attempt to register the same shim (has the same pending chan
|
|
|
|
// ID), then we should get an error.
|
|
|
|
_, err = dave.FundingStateStep(ctxb, &lnrpc.FundingTransitionMsg{
|
|
|
|
Trigger: &lnrpc.FundingTransitionMsg_ShimRegister{
|
|
|
|
ShimRegister: fundingShim,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("duplicate pending channel ID funding shim " +
|
|
|
|
"registration should trigger an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll take the chan point shim we just registered for Dave (the
|
|
|
|
// responder), and swap the local/remote keys before we feed it in as
|
|
|
|
// Carol's funding shim as the initiator.
|
|
|
|
fundingShim.GetChanPointShim().LocalKey = &lnrpc.KeyDescriptor{
|
|
|
|
RawKeyBytes: carolFundingKey.RawKeyBytes,
|
|
|
|
KeyLoc: &lnrpc.KeyLocator{
|
|
|
|
KeyFamily: carolFundingKey.KeyLoc.KeyFamily,
|
|
|
|
KeyIndex: carolFundingKey.KeyLoc.KeyIndex,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fundingShim.GetChanPointShim().RemoteKey = daveFundingKey.RawKeyBytes
|
|
|
|
|
2020-08-21 15:12:30 +03:00
|
|
|
return fundingShim, chanPoint, txid
|
2019-12-11 07:02:49 +03:00
|
|
|
}
|
|
|
|
|
2020-03-26 17:05:11 +03:00
|
|
|
// sendAndAssertSuccess sends the given payment requests and asserts that the
|
|
|
|
// payment completes successfully.
|
|
|
|
func sendAndAssertSuccess(t *harnessTest, node *lntest.HarnessNode,
|
|
|
|
req *routerrpc.SendPaymentRequest) *lnrpc.Payment {
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-10-26 16:21:07 +03:00
|
|
|
var result *lnrpc.Payment
|
|
|
|
err := wait.NoError(func() error {
|
|
|
|
stream, err := node.RouterClient.SendPaymentV2(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to send payment: %v", err)
|
|
|
|
}
|
2020-03-26 17:05:11 +03:00
|
|
|
|
2020-10-26 16:21:07 +03:00
|
|
|
result, err = getPaymentResult(stream)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get payment result: %v",
|
|
|
|
err)
|
|
|
|
}
|
2020-03-26 17:05:11 +03:00
|
|
|
|
2020-10-26 16:21:07 +03:00
|
|
|
if result.Status != lnrpc.Payment_SUCCEEDED {
|
|
|
|
return fmt.Errorf("payment failed: %v", result.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, defaultTimeout)
|
|
|
|
require.NoError(t.t, err)
|
2020-03-26 17:05:11 +03:00
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-03-31 13:44:18 +03:00
|
|
|
// sendAndAssertFailure sends the given payment requests and asserts that the
|
|
|
|
// payment fails with the expected reason.
|
|
|
|
func sendAndAssertFailure(t *harnessTest, node *lntest.HarnessNode,
|
|
|
|
req *routerrpc.SendPaymentRequest,
|
|
|
|
failureReason lnrpc.PaymentFailureReason) *lnrpc.Payment {
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
stream, err := node.RouterClient.SendPaymentV2(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := getPaymentResult(stream)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get payment result: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Status != lnrpc.Payment_FAILED {
|
|
|
|
t.Fatalf("payment was expected to fail, but succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.FailureReason != failureReason {
|
|
|
|
t.Fatalf("payment should have been rejected due to "+
|
|
|
|
"%v, but got %v", failureReason, result.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-03-26 17:05:11 +03:00
|
|
|
// getPaymentResult reads a final result from the stream and returns it.
|
2020-04-13 11:07:53 +03:00
|
|
|
func getPaymentResult(stream routerrpc.Router_SendPaymentV2Client) (
|
2020-03-26 17:05:11 +03:00
|
|
|
*lnrpc.Payment, error) {
|
|
|
|
|
|
|
|
for {
|
|
|
|
payment, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if payment.Status != lnrpc.Payment_IN_FLIGHT {
|
|
|
|
return payment, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-10-26 16:21:05 +03:00
|
|
|
// If no tests are registered, then we can exit early.
|
2020-11-04 13:03:29 +03:00
|
|
|
if len(allTestCases) == 0 {
|
2020-01-10 17:27:48 +03:00
|
|
|
t.Skip("integration tests not selected with flag 'rpctest'")
|
|
|
|
}
|
|
|
|
|
2020-11-04 13:03:29 +03:00
|
|
|
// Parse testing flags that influence our test execution.
|
2020-11-04 13:03:30 +03:00
|
|
|
logDir := lntest.GetLogDir()
|
|
|
|
require.NoError(t, os.MkdirAll(logDir, 0700))
|
2020-11-04 13:03:29 +03:00
|
|
|
testCases, trancheIndex, trancheOffset := getTestCaseSplitTranche()
|
|
|
|
lntest.ApplyPortOffset(uint32(trancheIndex) * 1000)
|
|
|
|
|
2020-12-03 13:30:23 +03:00
|
|
|
// Before we start any node, we need to make sure that any btcd node
|
|
|
|
// that is started through the RPC harness uses a unique port as well to
|
|
|
|
// avoid any port collisions.
|
|
|
|
rpctest.ListenAddressGenerator = lntest.GenerateBtcdListenerAddresses
|
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.
|
2020-11-04 13:03:30 +03:00
|
|
|
minerLogDir := fmt.Sprintf("%s/.minerlogs", logDir)
|
2020-09-30 01:15:05 +03:00
|
|
|
miner, minerCleanUp, err := lntest.NewMiner(
|
2020-12-03 13:30:22 +03:00
|
|
|
minerLogDir, "output_btcd_miner.log", harnessNetParams,
|
|
|
|
&rpcclient.NotificationHandlers{}, lntest.GetBtcdBinary(),
|
2020-09-30 01:15:05 +03:00
|
|
|
)
|
|
|
|
require.NoError(t, err, "failed to create new miner")
|
2018-10-10 14:04:13 +03:00
|
|
|
defer func() {
|
2020-09-30 01:15:05 +03:00
|
|
|
require.NoError(t, minerCleanUp(), "failed to clean up miner")
|
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.
|
2019-07-11 14:51:22 +03:00
|
|
|
chainBackend, cleanUp, err := lntest.NewBackend(
|
|
|
|
miner.P2PAddress(), harnessNetParams,
|
|
|
|
)
|
2020-12-03 13:30:23 +03:00
|
|
|
require.NoError(t, err, "new backend")
|
2020-09-02 22:15:04 +03:00
|
|
|
defer func() {
|
2020-12-03 13:30:23 +03:00
|
|
|
require.NoError(t, cleanUp(), "cleanup")
|
2020-09-02 22:15:04 +03:00
|
|
|
}()
|
2019-05-24 15:17:48 +03:00
|
|
|
|
2020-12-03 13:30:23 +03:00
|
|
|
// Before we start anything, we want to overwrite some of the connection
|
|
|
|
// settings to make the tests more robust. We might need to restart the
|
|
|
|
// miner while there are already blocks present, which will take a bit
|
|
|
|
// longer than the 1 second the default settings amount to. Doubling
|
|
|
|
// both values will give us retries up to 4 seconds.
|
|
|
|
miner.MaxConnRetries = rpctest.DefaultMaxConnectionRetries * 2
|
|
|
|
miner.ConnectionRetryTimeout = rpctest.DefaultConnectionRetryTimeout * 2
|
2018-12-14 11:24:00 +03:00
|
|
|
|
2020-12-03 13:30:23 +03:00
|
|
|
// Set up miner and connect chain backend to it.
|
|
|
|
require.NoError(t, miner.SetUp(true, 50))
|
|
|
|
require.NoError(t, miner.Node.NotifyNewTransactions(false))
|
|
|
|
require.NoError(t, chainBackend.ConnectMiner(), "connect miner")
|
2020-09-03 13:59:08 +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.
|
2020-12-03 13:30:23 +03:00
|
|
|
ht := newHarnessTest(t, nil)
|
2020-11-04 13:03:31 +03:00
|
|
|
binary := ht.getLndBinary()
|
2020-06-22 20:36:12 +03:00
|
|
|
lndHarness, err = lntest.NewNetworkHarness(
|
|
|
|
miner, chainBackend, binary, *useEtcd,
|
|
|
|
)
|
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
|
|
|
}
|
2020-10-29 17:37:17 +03:00
|
|
|
defer lndHarness.Stop()
|
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
|
|
|
|
}
|
2020-11-04 13:03:29 +03:00
|
|
|
ht.Logf("lnd finished with error (stderr):\n%v",
|
|
|
|
err)
|
2017-10-25 05:35:54 +03:00
|
|
|
}
|
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.
|
2019-07-11 14:51:22 +03:00
|
|
|
numBlocks := harnessNetParams.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?
|
2020-08-13 01:45:57 +03:00
|
|
|
aliceBobArgs := []string{
|
|
|
|
"--default-remote-max-htlcs=483",
|
|
|
|
}
|
2016-08-30 08:07:54 +03:00
|
|
|
|
2020-11-04 13:03:29 +03:00
|
|
|
// Run the subset of the test cases selected in this tranche.
|
|
|
|
for idx, testCase := range testCases {
|
|
|
|
testCase := testCase
|
2020-10-29 17:37:17 +03:00
|
|
|
name := fmt.Sprintf("%02d-of-%d/%s/%s",
|
|
|
|
trancheOffset+uint(idx)+1, len(allTestCases),
|
|
|
|
chainBackend.Name(), testCase.name)
|
2018-06-15 22:35:12 +03:00
|
|
|
|
2020-10-29 17:37:17 +03:00
|
|
|
success := t.Run(name, func(t1 *testing.T) {
|
|
|
|
cleanTestCaseName := strings.ReplaceAll(
|
|
|
|
testCase.name, " ", "_",
|
|
|
|
)
|
|
|
|
|
|
|
|
err = lndHarness.SetUp(cleanTestCaseName, aliceBobArgs)
|
|
|
|
require.NoError(t1,
|
|
|
|
err, "unable to set up test lightning network",
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t1, lndHarness.TearDown())
|
|
|
|
}()
|
2018-06-15 22:35:12 +03:00
|
|
|
|
2020-10-29 17:37:17 +03:00
|
|
|
err = lndHarness.EnsureConnected(
|
|
|
|
context.Background(), lndHarness.Alice,
|
|
|
|
lndHarness.Bob,
|
|
|
|
)
|
|
|
|
require.NoError(t1,
|
|
|
|
err, "unable to connect alice to bob",
|
|
|
|
)
|
2020-10-29 17:35:02 +03:00
|
|
|
|
2020-10-29 17:37:17 +03:00
|
|
|
logLine := fmt.Sprintf(
|
|
|
|
"STARTING ============ %v ============\n",
|
|
|
|
testCase.name,
|
|
|
|
)
|
2018-01-09 14:59:32 +03:00
|
|
|
|
2020-10-29 17:37:17 +03:00
|
|
|
err = lndHarness.Alice.AddToLog(logLine)
|
|
|
|
require.NoError(t1, err, "unable to add to log")
|
2020-09-04 12:19:27 +03:00
|
|
|
|
2020-10-29 17:37:17 +03:00
|
|
|
err = lndHarness.Bob.AddToLog(logLine)
|
|
|
|
require.NoError(t1, err, "unable to add to log")
|
|
|
|
|
|
|
|
// Start every test with the default static fee estimate.
|
|
|
|
lndHarness.SetFeeEstimate(12500)
|
|
|
|
|
|
|
|
// Create a separate harness test for the testcase to
|
|
|
|
// avoid overwriting the external harness test that is
|
|
|
|
// tied to the parent test.
|
2019-04-15 17:19:47 +03:00
|
|
|
ht := newHarnessTest(t1, lndHarness)
|
|
|
|
ht.RunTestCase(testCase)
|
2017-06-19 16:53:52 +03:00
|
|
|
})
|
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 {
|
2020-04-06 22:32:26 +03:00
|
|
|
// Log failure time to help relate the lnd logs to the
|
|
|
|
// failure.
|
2020-11-04 13:03:29 +03:00
|
|
|
t.Logf("Failure time: %v", time.Now().Format(
|
|
|
|
"2006-01-02 15:04:05.000",
|
|
|
|
))
|
2017-06-19 16:53:52 +03:00
|
|
|
break
|
|
|
|
}
|
2016-08-30 08:07:54 +03:00
|
|
|
}
|
|
|
|
}
|