2017-03-21 02:25:54 +01:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-07-14 21:32:00 +02:00
|
|
|
"image/color"
|
2019-10-11 21:46:10 +02:00
|
|
|
"math"
|
2017-10-02 17:54:29 +02:00
|
|
|
"math/rand"
|
2019-05-16 15:27:28 +02:00
|
|
|
"sync/atomic"
|
2017-03-21 02:25:54 +01:00
|
|
|
"testing"
|
2017-07-14 21:32:00 +02:00
|
|
|
"time"
|
2017-03-21 02:25:54 +01:00
|
|
|
|
2018-07-30 22:40:20 +02:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2018-08-18 20:05:58 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2019-01-16 15:47:43 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2018-12-09 03:42:46 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2017-03-30 03:01:28 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2021-05-21 13:02:49 +02:00
|
|
|
"github.com/stretchr/testify/mock"
|
2020-11-24 05:17:16 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-01-16 15:47:43 +01:00
|
|
|
|
2021-03-18 03:32:26 +01:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2019-01-16 15:47:43 +01:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2020-03-05 13:54:03 +01:00
|
|
|
"github.com/lightningnetwork/lnd/clock"
|
2019-01-16 15:47:43 +01:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
2021-03-18 03:32:26 +01:00
|
|
|
lnmock "github.com/lightningnetwork/lnd/lntest/mock"
|
2021-09-17 00:21:00 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
2018-08-08 11:09:30 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2017-03-21 02:25:54 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2020-04-01 00:13:27 +02:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2019-04-05 17:36:11 +02:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2018-12-22 20:38:03 +01:00
|
|
|
"github.com/lightningnetwork/lnd/zpay32"
|
2017-03-21 02:25:54 +01:00
|
|
|
)
|
|
|
|
|
2019-05-16 15:27:28 +02:00
|
|
|
var uniquePaymentID uint64 = 1 // to be used atomically
|
|
|
|
|
2017-03-21 02:25:54 +01:00
|
|
|
type testCtx struct {
|
|
|
|
router *ChannelRouter
|
|
|
|
|
|
|
|
graph *channeldb.ChannelGraph
|
|
|
|
|
2019-04-05 17:36:11 +02:00
|
|
|
aliases map[string]route.Vertex
|
2017-03-21 02:25:54 +01:00
|
|
|
|
2018-12-10 13:32:21 +01:00
|
|
|
privKeys map[string]*btcec.PrivateKey
|
|
|
|
|
2021-04-10 12:39:25 +02:00
|
|
|
channelIDs map[route.Vertex]map[route.Vertex]uint64
|
|
|
|
|
2017-03-21 02:25:54 +01:00
|
|
|
chain *mockChain
|
|
|
|
|
2017-05-11 02:22:26 +02:00
|
|
|
chainView *mockChainView
|
2021-03-18 03:32:26 +01:00
|
|
|
|
|
|
|
notifier *lnmock.ChainNotifier
|
2017-03-21 02:25:54 +01:00
|
|
|
}
|
|
|
|
|
2021-04-10 12:39:25 +02:00
|
|
|
func (c *testCtx) getChannelIDFromAlias(t *testing.T, a, b string) uint64 {
|
|
|
|
vertexA, ok := c.aliases[a]
|
|
|
|
require.True(t, ok, "cannot find aliases for %s", a)
|
|
|
|
|
|
|
|
vertexB, ok := c.aliases[b]
|
|
|
|
require.True(t, ok, "cannot find aliases for %s", b)
|
|
|
|
|
|
|
|
channelIDMap, ok := c.channelIDs[vertexA]
|
|
|
|
require.True(t, ok, "cannot find channelID map %s(%s)", vertexA, a)
|
|
|
|
|
|
|
|
channelID, ok := channelIDMap[vertexB]
|
|
|
|
require.True(t, ok, "cannot find channelID using %s(%s)", vertexB, b)
|
|
|
|
|
|
|
|
return channelID
|
|
|
|
}
|
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
func (c *testCtx) RestartRouter(t *testing.T) {
|
2017-11-07 01:00:17 +01:00
|
|
|
// First, we'll reset the chainView's state as it doesn't persist the
|
|
|
|
// filter between restarts.
|
|
|
|
c.chainView.Reset()
|
|
|
|
|
|
|
|
// With the chainView reset, we'll now re-create the router itself, and
|
|
|
|
// start it.
|
|
|
|
router, err := New(Config{
|
2019-05-16 15:27:28 +02:00
|
|
|
Graph: c.graph,
|
|
|
|
Chain: c.chain,
|
|
|
|
ChainView: c.chainView,
|
2021-05-24 12:29:27 +02:00
|
|
|
Payer: &mockPaymentAttemptDispatcherOld{},
|
2019-05-23 20:05:29 +02:00
|
|
|
Control: makeMockControlTower(),
|
2017-11-07 01:00:17 +01:00
|
|
|
ChannelPruneExpiry: time.Hour * 24,
|
|
|
|
GraphPruneInterval: time.Hour * 2,
|
|
|
|
})
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "unable to create router")
|
|
|
|
require.NoError(t, router.Start(), "unable to start router")
|
2017-11-07 01:00:17 +01:00
|
|
|
|
|
|
|
// Finally, we'll swap out the pointer in the testCtx with this fresh
|
|
|
|
// instance of the router.
|
|
|
|
c.router = router
|
|
|
|
}
|
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
func createTestCtxFromGraphInstance(t *testing.T,
|
|
|
|
startingHeight uint32, graphInstance *testGraphInstance,
|
|
|
|
strictPruning bool) (*testCtx, func()) {
|
2017-03-21 02:25:54 +01:00
|
|
|
|
2021-02-18 03:55:56 +01:00
|
|
|
return createTestCtxFromGraphInstanceAssumeValid(
|
2021-04-14 06:58:12 +02:00
|
|
|
t, startingHeight, graphInstance, false, strictPruning,
|
2021-02-18 03:55:56 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
func createTestCtxFromGraphInstanceAssumeValid(t *testing.T,
|
|
|
|
startingHeight uint32, graphInstance *testGraphInstance,
|
|
|
|
assumeValid bool, strictPruning bool) (*testCtx, func()) {
|
2021-02-18 03:55:56 +01:00
|
|
|
|
2018-08-16 21:35:38 +02:00
|
|
|
// We'll initialize an instance of the channel router with mock
|
2017-03-21 02:25:54 +01:00
|
|
|
// versions of the chain and channel notifier. As we don't need to test
|
|
|
|
// any p2p functionality, the peer send and switch send messages won't
|
|
|
|
// be populated.
|
|
|
|
chain := newMockChain(startingHeight)
|
2017-11-07 01:00:51 +01:00
|
|
|
chainView := newMockChainView(chain)
|
2019-05-23 20:05:30 +02:00
|
|
|
|
2019-06-26 13:00:35 +02:00
|
|
|
pathFindingConfig := PathFindingConfig{
|
2020-09-08 13:07:35 +02:00
|
|
|
MinProbability: 0.01,
|
|
|
|
AttemptCost: 100,
|
2019-06-20 12:03:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mcConfig := &MissionControlConfig{
|
2021-01-19 09:57:13 +01:00
|
|
|
ProbabilityEstimatorCfg: ProbabilityEstimatorCfg{
|
|
|
|
PenaltyHalfLife: time.Hour,
|
|
|
|
AprioriHopProbability: 0.9,
|
|
|
|
AprioriWeight: 0.5,
|
|
|
|
},
|
2019-06-20 12:03:45 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 13:00:35 +02:00
|
|
|
mc, err := NewMissionControl(
|
2021-09-21 19:18:18 +02:00
|
|
|
graphInstance.graphBackend, route.Vertex{}, mcConfig,
|
2019-05-23 20:05:30 +02:00
|
|
|
)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "failed to create missioncontrol")
|
2019-06-18 18:30:56 +02:00
|
|
|
|
2021-10-21 13:55:22 +02:00
|
|
|
sourceNode, err := graphInstance.graph.SourceNode()
|
2021-09-21 19:18:24 +02:00
|
|
|
require.NoError(t, err)
|
2019-06-18 18:30:56 +02:00
|
|
|
sessionSource := &SessionSource{
|
2021-10-21 13:55:22 +02:00
|
|
|
Graph: graphInstance.graph,
|
|
|
|
SourceNode: sourceNode,
|
2021-10-19 09:37:44 +02:00
|
|
|
GetLink: graphInstance.getLink,
|
2019-06-26 13:00:35 +02:00
|
|
|
PathFindingConfig: pathFindingConfig,
|
|
|
|
MissionControl: mc,
|
2019-06-18 18:30:56 +02:00
|
|
|
}
|
|
|
|
|
2021-03-18 03:32:26 +01:00
|
|
|
notifier := &lnmock.ChainNotifier{
|
|
|
|
EpochChan: make(chan *chainntnfs.BlockEpoch),
|
|
|
|
SpendChan: make(chan *chainntnfs.SpendDetail),
|
|
|
|
ConfChan: make(chan *chainntnfs.TxConfirmation),
|
|
|
|
}
|
|
|
|
|
2017-03-21 02:25:54 +01:00
|
|
|
router, err := New(Config{
|
2019-05-16 15:27:28 +02:00
|
|
|
Graph: graphInstance.graph,
|
|
|
|
Chain: chain,
|
|
|
|
ChainView: chainView,
|
2021-05-24 12:29:27 +02:00
|
|
|
Payer: &mockPaymentAttemptDispatcherOld{},
|
2021-03-18 03:32:26 +01:00
|
|
|
Notifier: notifier,
|
2019-05-23 20:05:29 +02:00
|
|
|
Control: makeMockControlTower(),
|
2019-05-23 20:05:30 +02:00
|
|
|
MissionControl: mc,
|
2019-06-18 18:30:56 +02:00
|
|
|
SessionSource: sessionSource,
|
2017-10-05 04:39:38 +02:00
|
|
|
ChannelPruneExpiry: time.Hour * 24,
|
|
|
|
GraphPruneInterval: time.Hour * 2,
|
2021-10-19 09:37:44 +02:00
|
|
|
GetLink: graphInstance.getLink,
|
2019-05-16 15:27:28 +02:00
|
|
|
NextPaymentID: func() (uint64, error) {
|
|
|
|
next := atomic.AddUint64(&uniquePaymentID, 1)
|
|
|
|
return next, nil
|
|
|
|
},
|
2021-04-02 23:57:50 +02:00
|
|
|
PathFindingConfig: pathFindingConfig,
|
|
|
|
Clock: clock.NewTestClock(time.Unix(1, 0)),
|
|
|
|
AssumeChannelValid: assumeValid,
|
|
|
|
StrictZombiePruning: strictPruning,
|
2017-03-21 02:25:54 +01:00
|
|
|
})
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "unable to create router")
|
|
|
|
require.NoError(t, router.Start(), "unable to start router")
|
2017-03-21 02:25:54 +01:00
|
|
|
|
2017-11-07 01:00:51 +01:00
|
|
|
ctx := &testCtx{
|
2021-04-10 12:39:25 +02:00
|
|
|
router: router,
|
|
|
|
graph: graphInstance.graph,
|
|
|
|
aliases: graphInstance.aliasMap,
|
|
|
|
privKeys: graphInstance.privKeyMap,
|
|
|
|
channelIDs: graphInstance.channelIDs,
|
|
|
|
chain: chain,
|
|
|
|
chainView: chainView,
|
2021-03-18 03:32:26 +01:00
|
|
|
notifier: notifier,
|
2017-11-07 01:00:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanUp := func() {
|
|
|
|
ctx.router.Stop()
|
|
|
|
}
|
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
return ctx, cleanUp
|
2017-03-21 02:25:54 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
func createTestCtxSingleNode(t *testing.T,
|
|
|
|
startingHeight uint32) (*testCtx, func()) {
|
|
|
|
|
2021-09-21 19:18:18 +02:00
|
|
|
graph, graphBackend, cleanup, err := makeTestGraph()
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "failed to make test graph")
|
2018-08-16 21:35:38 +02:00
|
|
|
|
2021-09-21 19:18:18 +02:00
|
|
|
sourceNode, err := createTestNode()
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "failed to create test node")
|
|
|
|
|
|
|
|
require.NoError(t,
|
|
|
|
graph.SetSourceNode(sourceNode), "failed to set source node",
|
|
|
|
)
|
2018-08-16 21:35:38 +02:00
|
|
|
|
|
|
|
graphInstance := &testGraphInstance{
|
2021-09-21 19:18:18 +02:00
|
|
|
graph: graph,
|
|
|
|
graphBackend: graphBackend,
|
|
|
|
cleanUp: cleanup,
|
2018-08-16 21:35:38 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
return createTestCtxFromGraphInstance(
|
|
|
|
t, startingHeight, graphInstance, false,
|
|
|
|
)
|
2018-08-16 21:35:38 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
func createTestCtxFromFile(t *testing.T,
|
|
|
|
startingHeight uint32, testGraph string) (*testCtx, func()) {
|
|
|
|
|
2018-08-16 21:35:38 +02:00
|
|
|
// We'll attempt to locate and parse out the file
|
|
|
|
// that encodes the graph that our tests should be run against.
|
|
|
|
graphInstance, err := parseTestGraph(testGraph)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "unable to create test graph")
|
2018-08-16 21:35:38 +02:00
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
return createTestCtxFromGraphInstance(
|
|
|
|
t, startingHeight, graphInstance, false,
|
|
|
|
)
|
2018-08-16 21:35:38 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 13:42:57 +01:00
|
|
|
// Add valid signature to channel update simulated as error received from the
|
|
|
|
// network.
|
2021-04-14 06:58:12 +02:00
|
|
|
func signErrChanUpdate(t *testing.T, key *btcec.PrivateKey,
|
|
|
|
errChanUpdate *lnwire.ChannelUpdate) {
|
2018-12-10 13:42:57 +01:00
|
|
|
|
|
|
|
chanUpdateMsg, err := errChanUpdate.DataToSign()
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "failed to retrieve data to sign")
|
2018-12-10 13:42:57 +01:00
|
|
|
|
|
|
|
digest := chainhash.DoubleHashB(chanUpdateMsg)
|
|
|
|
sig, err := key.Sign(digest)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "failed to sign msg")
|
2018-12-10 13:42:57 +01:00
|
|
|
|
|
|
|
errChanUpdate.Signature, err = lnwire.NewSigFromSignature(sig)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "failed to create new signature")
|
2018-12-10 13:42:57 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 16:32:24 +02:00
|
|
|
// TestFindRoutesWithFeeLimit asserts that routes found by the FindRoutes method
|
|
|
|
// within the channel router contain a total fee less than or equal to the fee
|
|
|
|
// limit.
|
|
|
|
func TestFindRoutesWithFeeLimit(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromFile(
|
|
|
|
t, startingBlockHeight, basicGraphFilePath,
|
2018-04-19 16:32:24 +02:00
|
|
|
)
|
2018-12-04 11:20:37 +01:00
|
|
|
defer cleanUp()
|
2018-04-19 16:32:24 +02:00
|
|
|
|
|
|
|
// This test will attempt to find routes from roasbeef to sophon for 100
|
|
|
|
// satoshis with a fee limit of 10 satoshis. There are two routes from
|
|
|
|
// roasbeef to sophon:
|
|
|
|
// 1. roasbeef -> songoku -> sophon
|
|
|
|
// 2. roasbeef -> phamnuwen -> sophon
|
|
|
|
// The second route violates our fee limit, so we should only expect to
|
|
|
|
// see the first route.
|
|
|
|
target := ctx.aliases["sophon"]
|
|
|
|
paymentAmt := lnwire.NewMSatFromSatoshis(100)
|
2019-03-05 12:42:29 +01:00
|
|
|
restrictions := &RestrictParams{
|
2019-03-19 11:45:10 +01:00
|
|
|
FeeLimit: lnwire.NewMSatFromSatoshis(10),
|
|
|
|
ProbabilitySource: noProbabilitySource,
|
2019-10-11 21:46:10 +02:00
|
|
|
CltvLimit: math.MaxUint32,
|
2019-03-05 12:42:29 +01:00
|
|
|
}
|
2018-04-19 16:32:24 +02:00
|
|
|
|
2019-05-07 17:01:01 +02:00
|
|
|
route, err := ctx.router.FindRoute(
|
2019-03-05 16:49:26 +01:00
|
|
|
ctx.router.selfNode.PubKeyBytes,
|
2020-01-14 11:21:24 +01:00
|
|
|
target, paymentAmt, restrictions, nil, nil,
|
2020-07-24 22:13:56 +02:00
|
|
|
MinCLTVDelta,
|
2018-04-19 16:32:24 +02:00
|
|
|
)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "unable to find any routes")
|
2018-04-19 16:32:24 +02:00
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Falsef(t,
|
|
|
|
route.TotalFees() > restrictions.FeeLimit,
|
|
|
|
"route exceeded fee limit: %v", spew.Sdump(route),
|
|
|
|
)
|
2018-04-19 16:32:24 +02:00
|
|
|
|
2019-05-07 17:01:01 +02:00
|
|
|
hops := route.Hops
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equal(t, 2, len(hops), "expected 2 hops")
|
2018-04-19 16:32:24 +02:00
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equalf(t,
|
|
|
|
ctx.aliases["songoku"], hops[0].PubKeyBytes,
|
|
|
|
"expected first hop through songoku, got %s",
|
|
|
|
getAliasFromPubKey(hops[0].PubKeyBytes, ctx.aliases),
|
|
|
|
)
|
2018-04-19 16:32:24 +02:00
|
|
|
}
|
|
|
|
|
2017-03-21 02:58:21 +01:00
|
|
|
// TestSendPaymentRouteFailureFallback tests that when sending a payment, if
|
|
|
|
// one of the target routes is seen as unavailable, then the next route in the
|
|
|
|
// queue is used instead. This process should continue until either a payment
|
|
|
|
// succeeds, or all routes have been exhausted.
|
|
|
|
func TestSendPaymentRouteFailureFallback(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-03-21 02:58:21 +01:00
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromFile(
|
|
|
|
t, startingBlockHeight, basicGraphFilePath,
|
|
|
|
)
|
2018-12-04 11:20:37 +01:00
|
|
|
defer cleanUp()
|
2017-03-21 02:58:21 +01:00
|
|
|
|
|
|
|
// Craft a LightningPayment struct that'll send a payment from roasbeef
|
2018-01-31 23:36:10 +01:00
|
|
|
// to luo ji for 1000 satoshis, with a maximum of 1000 satoshis in fees.
|
2021-03-31 12:23:08 +02:00
|
|
|
var payHash lntypes.Hash
|
2018-01-31 23:36:10 +01:00
|
|
|
paymentAmt := lnwire.NewMSatFromSatoshis(1000)
|
2017-03-21 02:58:21 +01:00
|
|
|
payment := LightningPayment{
|
2019-06-18 19:42:51 +02:00
|
|
|
Target: ctx.aliases["sophon"],
|
2018-01-31 23:36:10 +01:00
|
|
|
Amount: paymentAmt,
|
2018-04-19 16:32:24 +02:00
|
|
|
FeeLimit: noFeeLimit,
|
2021-03-31 12:23:08 +02:00
|
|
|
paymentHash: &payHash,
|
2017-03-21 02:58:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var preImage [32]byte
|
|
|
|
copy(preImage[:], bytes.Repeat([]byte{9}, 32))
|
|
|
|
|
2021-04-10 13:27:58 +02:00
|
|
|
// Get the channel ID.
|
|
|
|
roasbeefSongoku := lnwire.NewShortChanIDFromInt(
|
|
|
|
ctx.getChannelIDFromAlias(t, "roasbeef", "songoku"),
|
|
|
|
)
|
|
|
|
|
2017-03-21 02:58:21 +01:00
|
|
|
// We'll modify the SendToSwitch method that's been set within the
|
2019-06-18 19:42:51 +02:00
|
|
|
// router's configuration to ignore the path that has son goku as the
|
2017-03-21 02:58:21 +01:00
|
|
|
// first hop. This should force the router to instead take the
|
2019-06-18 19:42:51 +02:00
|
|
|
// the more costly path (through pham nuwen).
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
2019-05-16 15:27:28 +02:00
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
|
|
|
|
2019-06-18 19:42:51 +02:00
|
|
|
if firstHop == roasbeefSongoku {
|
2020-01-14 14:07:29 +01:00
|
|
|
return [32]byte{}, htlcswitch.NewForwardingError(
|
|
|
|
// TODO(roasbeef): temp node failure
|
|
|
|
// should be?
|
|
|
|
&lnwire.FailTemporaryChannelFailure{},
|
2020-01-14 14:07:42 +01:00
|
|
|
1,
|
2020-01-14 14:07:29 +01:00
|
|
|
)
|
2018-01-31 05:26:26 +01:00
|
|
|
}
|
2017-03-21 02:58:21 +01:00
|
|
|
|
2019-05-16 15:27:28 +02:00
|
|
|
return preImage, nil
|
|
|
|
})
|
2017-03-21 02:58:21 +01:00
|
|
|
|
2019-06-18 19:42:51 +02:00
|
|
|
// Send off the payment request to the router, route through pham nuwen
|
2017-03-21 02:58:21 +01:00
|
|
|
// should've been selected as a fall back and succeeded correctly.
|
|
|
|
paymentPreImage, route, err := ctx.router.SendPayment(&payment)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "unable to send payment")
|
2017-03-21 02:58:21 +01:00
|
|
|
|
|
|
|
// The route selected should have two hops
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equal(t, 2, len(route.Hops), "incorrect route length")
|
2017-03-21 02:58:21 +01:00
|
|
|
|
|
|
|
// The preimage should match up with the once created above.
|
|
|
|
if !bytes.Equal(paymentPreImage[:], preImage[:]) {
|
|
|
|
t.Fatalf("incorrect preimage used: expected %x got %x",
|
|
|
|
preImage[:], paymentPreImage[:])
|
|
|
|
}
|
|
|
|
|
2019-06-18 19:42:51 +02:00
|
|
|
// The route should have pham nuwen as the first hop.
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equalf(t,
|
|
|
|
ctx.aliases["phamnuwen"], route.Hops[0].PubKeyBytes,
|
|
|
|
"route should go through phamnuwen as first hop, instead "+
|
|
|
|
"passes through: %v",
|
|
|
|
getAliasFromPubKey(route.Hops[0].PubKeyBytes, ctx.aliases),
|
|
|
|
)
|
2017-10-11 04:49:09 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 20:05:58 +02:00
|
|
|
// TestChannelUpdateValidation tests that a failed payment with an associated
|
|
|
|
// channel update will only be applied to the graph when the update contains a
|
|
|
|
// valid signature.
|
|
|
|
func TestChannelUpdateValidation(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Setup a three node network.
|
2018-12-09 03:42:46 +01:00
|
|
|
chanCapSat := btcutil.Amount(100000)
|
2021-04-14 06:58:12 +02:00
|
|
|
feeRate := lnwire.MilliSatoshi(400)
|
2018-08-18 20:05:58 +02:00
|
|
|
testChannels := []*testChannel{
|
2018-12-09 03:42:46 +01:00
|
|
|
symmetricTestChannel("a", "b", chanCapSat, &testChannelPolicy{
|
2018-08-18 20:05:58 +02:00
|
|
|
Expiry: 144,
|
2021-04-14 06:58:12 +02:00
|
|
|
FeeRate: feeRate,
|
2018-08-18 20:05:58 +02:00
|
|
|
MinHTLC: 1,
|
2018-12-09 03:42:46 +01:00
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(chanCapSat),
|
2018-08-18 20:05:58 +02:00
|
|
|
}, 1),
|
2018-12-09 03:42:46 +01:00
|
|
|
symmetricTestChannel("b", "c", chanCapSat, &testChannelPolicy{
|
2018-08-18 20:05:58 +02:00
|
|
|
Expiry: 144,
|
2021-04-14 06:58:12 +02:00
|
|
|
FeeRate: feeRate,
|
2018-08-18 20:05:58 +02:00
|
|
|
MinHTLC: 1,
|
2018-12-09 03:42:46 +01:00
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(chanCapSat),
|
2018-08-18 20:05:58 +02:00
|
|
|
}, 2),
|
|
|
|
}
|
|
|
|
|
2019-01-31 16:07:30 +01:00
|
|
|
testGraph, err := createTestGraphFromChannels(testChannels, "a")
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "unable to create graph")
|
2018-08-18 20:05:58 +02:00
|
|
|
defer testGraph.cleanUp()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromGraphInstance(
|
|
|
|
t, startingBlockHeight, testGraph, true,
|
2021-04-02 23:57:50 +02:00
|
|
|
)
|
2018-12-22 20:38:03 +01:00
|
|
|
defer cleanUp()
|
2018-08-18 20:05:58 +02:00
|
|
|
|
|
|
|
// Assert that the initially configured fee is retrieved correctly.
|
|
|
|
_, policy, _, err := ctx.router.GetChannelByID(
|
|
|
|
lnwire.NewShortChanIDFromInt(1))
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "cannot retrieve channel")
|
2018-08-18 20:05:58 +02:00
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equal(t,
|
|
|
|
feeRate, policy.FeeProportionalMillionths, "invalid fee",
|
|
|
|
)
|
2018-08-18 20:05:58 +02:00
|
|
|
|
|
|
|
// Setup a route from source a to destination c. The route will be used
|
|
|
|
// in a call to SendToRoute. SendToRoute also applies channel updates,
|
|
|
|
// but it saves us from including RequestRoute in the test scope too.
|
2019-03-05 16:55:19 +01:00
|
|
|
hop1 := ctx.aliases["b"]
|
|
|
|
hop2 := ctx.aliases["c"]
|
2019-04-05 17:36:11 +02:00
|
|
|
hops := []*route.Hop{
|
2018-08-18 20:05:58 +02:00
|
|
|
{
|
2019-07-31 06:41:58 +02:00
|
|
|
ChannelID: 1,
|
|
|
|
PubKeyBytes: hop1,
|
|
|
|
LegacyPayload: true,
|
2018-08-18 20:05:58 +02:00
|
|
|
},
|
|
|
|
{
|
2019-07-31 06:41:58 +02:00
|
|
|
ChannelID: 2,
|
|
|
|
PubKeyBytes: hop2,
|
|
|
|
LegacyPayload: true,
|
2018-08-18 20:05:58 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-04-05 17:36:11 +02:00
|
|
|
rt, err := route.NewRouteFromHops(
|
2018-10-24 21:16:38 +02:00
|
|
|
lnwire.MilliSatoshi(10000), 100,
|
2019-03-05 16:55:19 +01:00
|
|
|
ctx.aliases["a"], hops,
|
2018-10-24 21:16:38 +02:00
|
|
|
)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "unable to create route")
|
2018-08-18 20:05:58 +02:00
|
|
|
|
|
|
|
// Set up a channel update message with an invalid signature to be
|
|
|
|
// returned to the sender.
|
|
|
|
var invalidSignature [64]byte
|
|
|
|
errChanUpdate := lnwire.ChannelUpdate{
|
|
|
|
Signature: invalidSignature,
|
|
|
|
FeeRate: 500,
|
|
|
|
ShortChannelID: lnwire.NewShortChanIDFromInt(1),
|
|
|
|
Timestamp: uint32(testTime.Add(time.Minute).Unix()),
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll modify the SendToSwitch method so that it simulates a failed
|
|
|
|
// payment with an error originating from the first hop of the route.
|
|
|
|
// The unsigned channel update is attached to the failure message.
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
2019-05-16 15:27:28 +02:00
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
2020-01-14 14:07:29 +01:00
|
|
|
return [32]byte{}, htlcswitch.NewForwardingError(
|
|
|
|
&lnwire.FailFeeInsufficient{
|
2019-05-16 15:27:28 +02:00
|
|
|
Update: errChanUpdate,
|
|
|
|
},
|
2020-01-14 14:07:42 +01:00
|
|
|
1,
|
2020-01-14 14:07:29 +01:00
|
|
|
)
|
2019-05-16 15:27:28 +02:00
|
|
|
})
|
2018-08-18 20:05:58 +02:00
|
|
|
|
|
|
|
// The payment parameter is mostly redundant in SendToRoute. Can be left
|
|
|
|
// empty for this test.
|
2018-08-08 11:09:30 +02:00
|
|
|
var payment lntypes.Hash
|
2018-08-18 20:05:58 +02:00
|
|
|
|
|
|
|
// Send off the payment request to the router. The specified route
|
|
|
|
// should be attempted and the channel update should be received by
|
|
|
|
// router and ignored because it is missing a valid signature.
|
2018-08-08 11:09:30 +02:00
|
|
|
_, err = ctx.router.SendToRoute(payment, rt)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Error(t, err, "expected route to fail with channel update")
|
2018-08-18 20:05:58 +02:00
|
|
|
|
|
|
|
_, policy, _, err = ctx.router.GetChannelByID(
|
|
|
|
lnwire.NewShortChanIDFromInt(1))
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "cannot retrieve channel")
|
2018-08-18 20:05:58 +02:00
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equal(t,
|
|
|
|
feeRate, policy.FeeProportionalMillionths,
|
|
|
|
"fee updated without valid signature",
|
|
|
|
)
|
2018-08-18 20:05:58 +02:00
|
|
|
|
|
|
|
// Next, add a signature to the channel update.
|
2021-04-14 06:58:12 +02:00
|
|
|
signErrChanUpdate(t, testGraph.privKeyMap["b"], &errChanUpdate)
|
2018-08-18 20:05:58 +02:00
|
|
|
|
|
|
|
// Retry the payment using the same route as before.
|
2018-08-08 11:09:30 +02:00
|
|
|
_, err = ctx.router.SendToRoute(payment, rt)
|
2018-08-18 20:05:58 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected route to fail with channel update")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This time a valid signature was supplied and the policy change should
|
|
|
|
// have been applied to the graph.
|
|
|
|
_, policy, _, err = ctx.router.GetChannelByID(
|
|
|
|
lnwire.NewShortChanIDFromInt(1))
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "cannot retrieve channel")
|
2018-08-18 20:05:58 +02:00
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equal(t,
|
|
|
|
lnwire.MilliSatoshi(500), policy.FeeProportionalMillionths,
|
|
|
|
"fee not updated even though signature is valid",
|
|
|
|
)
|
2018-08-18 20:05:58 +02:00
|
|
|
}
|
|
|
|
|
2018-03-20 03:08:55 +01:00
|
|
|
// TestSendPaymentErrorRepeatedFeeInsufficient tests that if we receive
|
|
|
|
// multiple fee related errors from a channel that we're attempting to route
|
|
|
|
// through, then we'll prune the channel after the second attempt.
|
|
|
|
func TestSendPaymentErrorRepeatedFeeInsufficient(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromFile(
|
|
|
|
t, startingBlockHeight, basicGraphFilePath,
|
2018-12-10 13:42:57 +01:00
|
|
|
)
|
2018-12-04 11:20:37 +01:00
|
|
|
defer cleanUp()
|
2018-03-20 03:08:55 +01:00
|
|
|
|
2021-04-10 13:27:58 +02:00
|
|
|
// Get the channel ID.
|
|
|
|
roasbeefSongokuChanID := ctx.getChannelIDFromAlias(
|
|
|
|
t, "roasbeef", "songoku",
|
|
|
|
)
|
|
|
|
songokuSophonChanID := ctx.getChannelIDFromAlias(
|
|
|
|
t, "songoku", "sophon",
|
|
|
|
)
|
|
|
|
|
2018-03-20 03:08:55 +01:00
|
|
|
// Craft a LightningPayment struct that'll send a payment from roasbeef
|
2018-12-10 13:42:57 +01:00
|
|
|
// to sophon for 1000 satoshis.
|
2021-03-31 12:23:08 +02:00
|
|
|
var payHash lntypes.Hash
|
2018-04-19 16:32:24 +02:00
|
|
|
amt := lnwire.NewMSatFromSatoshis(1000)
|
2018-03-20 03:08:55 +01:00
|
|
|
payment := LightningPayment{
|
2018-04-06 03:01:40 +02:00
|
|
|
Target: ctx.aliases["sophon"],
|
2018-04-19 16:32:24 +02:00
|
|
|
Amount: amt,
|
|
|
|
FeeLimit: noFeeLimit,
|
2021-03-31 12:23:08 +02:00
|
|
|
paymentHash: &payHash,
|
2018-03-20 03:08:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var preImage [32]byte
|
|
|
|
copy(preImage[:], bytes.Repeat([]byte{9}, 32))
|
|
|
|
|
2018-12-10 13:42:57 +01:00
|
|
|
// We'll also fetch the first outgoing channel edge from son goku
|
|
|
|
// to sophon. We'll obtain this as we'll need to to generate the
|
2018-03-20 03:08:55 +01:00
|
|
|
// FeeInsufficient error that we'll send back.
|
2018-12-10 13:42:57 +01:00
|
|
|
_, _, edgeUpdateToFail, err := ctx.graph.FetchChannelEdgesByID(
|
|
|
|
songokuSophonChanID,
|
|
|
|
)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "unable to fetch chan id")
|
2018-03-20 03:08:55 +01:00
|
|
|
|
|
|
|
errChanUpdate := lnwire.ChannelUpdate{
|
2018-12-10 13:42:57 +01:00
|
|
|
ShortChannelID: lnwire.NewShortChanIDFromInt(
|
|
|
|
songokuSophonChanID,
|
|
|
|
),
|
2018-12-09 03:42:46 +01:00
|
|
|
Timestamp: uint32(edgeUpdateToFail.LastUpdate.Unix()),
|
|
|
|
MessageFlags: edgeUpdateToFail.MessageFlags,
|
|
|
|
ChannelFlags: edgeUpdateToFail.ChannelFlags,
|
|
|
|
TimeLockDelta: edgeUpdateToFail.TimeLockDelta,
|
|
|
|
HtlcMinimumMsat: edgeUpdateToFail.MinHTLC,
|
|
|
|
HtlcMaximumMsat: edgeUpdateToFail.MaxHTLC,
|
|
|
|
BaseFee: uint32(edgeUpdateToFail.FeeBaseMSat),
|
|
|
|
FeeRate: uint32(edgeUpdateToFail.FeeProportionalMillionths),
|
2018-03-20 03:08:55 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
signErrChanUpdate(t, ctx.privKeys["songoku"], &errChanUpdate)
|
2018-12-10 13:42:57 +01:00
|
|
|
|
2018-03-20 03:08:55 +01:00
|
|
|
// We'll now modify the SendToSwitch method to return an error for the
|
2018-04-06 03:01:40 +02:00
|
|
|
// outgoing channel to Son goku. This will be a fee related error, so
|
|
|
|
// it should only cause the edge to be pruned after the second attempt.
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
2019-05-16 15:27:28 +02:00
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
|
|
|
|
2018-12-10 13:42:57 +01:00
|
|
|
roasbeefSongoku := lnwire.NewShortChanIDFromInt(
|
|
|
|
roasbeefSongokuChanID,
|
|
|
|
)
|
2019-05-16 15:27:28 +02:00
|
|
|
if firstHop == roasbeefSongoku {
|
2020-01-14 14:07:29 +01:00
|
|
|
return [32]byte{}, htlcswitch.NewForwardingError(
|
|
|
|
// Within our error, we'll add a
|
|
|
|
// channel update which is meant to
|
|
|
|
// reflect the new fee schedule for the
|
|
|
|
// node/channel.
|
|
|
|
&lnwire.FailFeeInsufficient{
|
2019-05-16 15:27:28 +02:00
|
|
|
Update: errChanUpdate,
|
2020-01-14 14:07:42 +01:00
|
|
|
}, 1,
|
2020-01-14 14:07:29 +01:00
|
|
|
)
|
2019-03-05 16:55:19 +01:00
|
|
|
}
|
|
|
|
|
2019-05-16 15:27:28 +02:00
|
|
|
return preImage, nil
|
|
|
|
})
|
2018-03-20 03:08:55 +01:00
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
// Send off the payment request to the router, route through phamnuwen
|
2018-03-20 03:08:55 +01:00
|
|
|
// should've been selected as a fall back and succeeded correctly.
|
|
|
|
paymentPreImage, route, err := ctx.router.SendPayment(&payment)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "unable to send payment")
|
2018-03-20 03:08:55 +01:00
|
|
|
|
|
|
|
// The route selected should have two hops
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equal(t, 2, len(route.Hops), "incorrect route length")
|
2018-03-20 03:08:55 +01:00
|
|
|
|
|
|
|
// The preimage should match up with the once created above.
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equal(t, preImage[:], paymentPreImage[:], "incorrect preimage")
|
2018-03-20 03:08:55 +01:00
|
|
|
|
2018-04-06 03:01:40 +02:00
|
|
|
// The route should have pham nuwen as the first hop.
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equalf(t,
|
|
|
|
ctx.aliases["phamnuwen"], route.Hops[0].PubKeyBytes,
|
|
|
|
"route should go through pham nuwen as first hop, "+
|
2018-03-20 03:08:55 +01:00
|
|
|
"instead passes through: %v",
|
2021-04-14 06:58:12 +02:00
|
|
|
getAliasFromPubKey(route.Hops[0].PubKeyBytes, ctx.aliases),
|
|
|
|
)
|
2018-03-20 03:08:55 +01:00
|
|
|
}
|
|
|
|
|
2018-12-22 20:38:03 +01:00
|
|
|
// TestSendPaymentErrorFeeInsufficientPrivateEdge tests that if we receive
|
|
|
|
// a fee related error from a private channel that we're attempting to route
|
|
|
|
// through, then we'll update the fees in the route hints and successfully
|
|
|
|
// route through the private channel in the second attempt.
|
2021-04-14 14:01:27 +02:00
|
|
|
//
|
|
|
|
// The test will send a payment from roasbeef to elst, available paths are,
|
|
|
|
// path1: roasbeef -> songoku -> sophon -> elst, total fee: 210k
|
|
|
|
// path2: roasbeef -> phamnuwen -> sophon -> elst, total fee: 220k
|
|
|
|
// path3: roasbeef -> songoku ->(private channel) elst
|
|
|
|
// We will setup the path3 to have the lowest fee so it's always the preferred
|
|
|
|
// path.
|
2018-12-22 20:38:03 +01:00
|
|
|
func TestSendPaymentErrorFeeInsufficientPrivateEdge(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromFile(
|
|
|
|
t, startingBlockHeight, basicGraphFilePath,
|
2018-12-22 20:38:03 +01:00
|
|
|
)
|
|
|
|
defer cleanUp()
|
|
|
|
|
2021-04-10 13:27:58 +02:00
|
|
|
// Get the channel ID.
|
|
|
|
roasbeefSongoku := lnwire.NewShortChanIDFromInt(
|
|
|
|
ctx.getChannelIDFromAlias(t, "roasbeef", "songoku"),
|
|
|
|
)
|
|
|
|
|
2021-04-14 14:01:27 +02:00
|
|
|
var (
|
|
|
|
payHash lntypes.Hash
|
|
|
|
preImage [32]byte
|
|
|
|
amt = lnwire.NewMSatFromSatoshis(1000)
|
|
|
|
privateChannelID = uint64(55555)
|
|
|
|
feeBaseMSat = uint32(15)
|
|
|
|
expiryDelta = uint16(32)
|
|
|
|
sgNode = ctx.aliases["songoku"]
|
|
|
|
)
|
|
|
|
|
2018-12-22 20:38:03 +01:00
|
|
|
sgNodeID, err := btcec.ParsePubKey(sgNode[:], btcec.S256())
|
|
|
|
require.NoError(t, err)
|
2021-04-14 14:01:27 +02:00
|
|
|
|
|
|
|
// Craft a LightningPayment struct that'll send a payment from roasbeef
|
|
|
|
// to elst, through a private channel between songoku and elst for
|
|
|
|
// 1000 satoshis. This route has lowest fees compared with the rest.
|
|
|
|
// This also holds when the private channel fee is updated to a higher
|
|
|
|
// value.
|
2018-12-22 20:38:03 +01:00
|
|
|
payment := LightningPayment{
|
|
|
|
Target: ctx.aliases["elst"],
|
|
|
|
Amount: amt,
|
|
|
|
FeeLimit: noFeeLimit,
|
|
|
|
paymentHash: &payHash,
|
2021-04-14 14:01:27 +02:00
|
|
|
RouteHints: [][]zpay32.HopHint{{
|
|
|
|
// Add a private channel between songoku and elst.
|
|
|
|
zpay32.HopHint{
|
|
|
|
NodeID: sgNodeID,
|
|
|
|
ChannelID: privateChannelID,
|
|
|
|
FeeBaseMSat: feeBaseMSat,
|
|
|
|
CLTVExpiryDelta: expiryDelta,
|
|
|
|
},
|
|
|
|
}},
|
2018-12-22 20:38:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare an error update for the private channel, with twice the
|
|
|
|
// original fee.
|
|
|
|
updatedFeeBaseMSat := feeBaseMSat * 2
|
|
|
|
errChanUpdate := lnwire.ChannelUpdate{
|
|
|
|
ShortChannelID: lnwire.NewShortChanIDFromInt(privateChannelID),
|
|
|
|
Timestamp: uint32(testTime.Add(time.Minute).Unix()),
|
|
|
|
BaseFee: updatedFeeBaseMSat,
|
|
|
|
TimeLockDelta: expiryDelta,
|
|
|
|
}
|
2021-04-14 06:58:12 +02:00
|
|
|
signErrChanUpdate(t, ctx.privKeys["songoku"], &errChanUpdate)
|
2018-12-22 20:38:03 +01:00
|
|
|
|
2021-04-14 14:01:27 +02:00
|
|
|
// We'll now modify the SendHTLC method to return an error for the
|
|
|
|
// outgoing channel to songoku.
|
2018-12-22 20:38:03 +01:00
|
|
|
errorReturned := false
|
2021-04-14 14:01:27 +02:00
|
|
|
copy(preImage[:], bytes.Repeat([]byte{9}, 32))
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
2018-12-22 20:38:03 +01:00
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
|
|
|
|
2021-04-10 13:27:58 +02:00
|
|
|
if firstHop != roasbeefSongoku || errorReturned {
|
2018-12-22 20:38:03 +01:00
|
|
|
return preImage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
errorReturned = true
|
|
|
|
return [32]byte{}, htlcswitch.NewForwardingError(
|
|
|
|
// Within our error, we'll add a
|
|
|
|
// channel update which is meant to
|
|
|
|
// reflect the new fee schedule for the
|
|
|
|
// node/channel.
|
|
|
|
&lnwire.FailFeeInsufficient{
|
|
|
|
Update: errChanUpdate,
|
|
|
|
}, 1,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Send off the payment request to the router, route through son
|
|
|
|
// goku and then across the private channel to elst.
|
|
|
|
paymentPreImage, route, err := ctx.router.SendPayment(&payment)
|
|
|
|
require.NoError(t, err, "unable to send payment")
|
|
|
|
|
|
|
|
require.True(t, errorReturned,
|
|
|
|
"failed to simulate error in the first payment attempt",
|
|
|
|
)
|
|
|
|
|
|
|
|
// The route selected should have two hops. Make sure that,
|
2021-04-14 14:01:27 +02:00
|
|
|
// path: roasbeef -> son goku -> sophon -> elst
|
|
|
|
// path: roasbeef -> pham nuwen -> sophon -> elst
|
2018-12-22 20:38:03 +01:00
|
|
|
// are not selected instead.
|
|
|
|
require.Equal(t, 2, len(route.Hops), "incorrect route length")
|
|
|
|
|
|
|
|
// The preimage should match up with the one created above.
|
|
|
|
require.Equal(t,
|
|
|
|
paymentPreImage[:], preImage[:], "incorrect preimage used",
|
|
|
|
)
|
|
|
|
|
|
|
|
// The route should have son goku as the first hop.
|
|
|
|
require.Equal(t, route.Hops[0].PubKeyBytes, ctx.aliases["songoku"],
|
|
|
|
"route should go through son goku as first hop",
|
|
|
|
)
|
|
|
|
|
|
|
|
// The route should pass via the private channel.
|
|
|
|
require.Equal(t,
|
|
|
|
privateChannelID, route.FinalHop().ChannelID,
|
|
|
|
"route did not pass through private channel "+
|
|
|
|
"between pham nuwen and elst",
|
|
|
|
)
|
|
|
|
|
|
|
|
// The route should have the updated fee.
|
2021-04-14 14:01:27 +02:00
|
|
|
require.Equal(t,
|
|
|
|
lnwire.MilliSatoshi(updatedFeeBaseMSat).String(),
|
|
|
|
route.HopFee(0).String(),
|
2018-12-22 20:38:03 +01:00
|
|
|
"fee to forward to the private channel not matched",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:40:22 +02:00
|
|
|
// TestSendPaymentPrivateEdgeUpdateFeeExceedsLimit tests that upon receiving a
|
|
|
|
// ChannelUpdate in a fee related error from the private channel, we won't
|
|
|
|
// choose the route in our second attempt if the updated fee exceeds our fee
|
|
|
|
// limit specified in the payment.
|
|
|
|
//
|
|
|
|
// The test will send a payment from roasbeef to elst, available paths are,
|
|
|
|
// path1: roasbeef -> songoku -> sophon -> elst, total fee: 210k
|
|
|
|
// path2: roasbeef -> phamnuwen -> sophon -> elst, total fee: 220k
|
|
|
|
// path3: roasbeef -> songoku ->(private channel) elst
|
|
|
|
// We will setup the path3 to have the lowest fee and then update it with a fee
|
|
|
|
// exceeds our fee limit, thus this route won't be chosen.
|
|
|
|
func TestSendPaymentPrivateEdgeUpdateFeeExceedsLimit(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
|
|
|
ctx, cleanUp := createTestCtxFromFile(
|
|
|
|
t, startingBlockHeight, basicGraphFilePath,
|
|
|
|
)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
// Get the channel ID.
|
|
|
|
roasbeefSongoku := lnwire.NewShortChanIDFromInt(
|
|
|
|
ctx.getChannelIDFromAlias(t, "roasbeef", "songoku"),
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
payHash lntypes.Hash
|
|
|
|
preImage [32]byte
|
|
|
|
amt = lnwire.NewMSatFromSatoshis(1000)
|
|
|
|
privateChannelID = uint64(55555)
|
|
|
|
feeBaseMSat = uint32(15)
|
|
|
|
expiryDelta = uint16(32)
|
|
|
|
sgNode = ctx.aliases["songoku"]
|
|
|
|
feeLimit = lnwire.MilliSatoshi(500000)
|
|
|
|
)
|
|
|
|
|
|
|
|
sgNodeID, err := btcec.ParsePubKey(sgNode[:], btcec.S256())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Craft a LightningPayment struct that'll send a payment from roasbeef
|
|
|
|
// to elst, through a private channel between songoku and elst for
|
|
|
|
// 1000 satoshis. This route has lowest fees compared with the rest.
|
|
|
|
payment := LightningPayment{
|
|
|
|
Target: ctx.aliases["elst"],
|
|
|
|
Amount: amt,
|
|
|
|
FeeLimit: feeLimit,
|
|
|
|
paymentHash: &payHash,
|
|
|
|
RouteHints: [][]zpay32.HopHint{{
|
|
|
|
// Add a private channel between songoku and elst.
|
|
|
|
zpay32.HopHint{
|
|
|
|
NodeID: sgNodeID,
|
|
|
|
ChannelID: privateChannelID,
|
|
|
|
FeeBaseMSat: feeBaseMSat,
|
|
|
|
CLTVExpiryDelta: expiryDelta,
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare an error update for the private channel. The updated fee
|
|
|
|
// will exceeds the feeLimit.
|
|
|
|
updatedFeeBaseMSat := feeBaseMSat + uint32(feeLimit)
|
|
|
|
errChanUpdate := lnwire.ChannelUpdate{
|
|
|
|
ShortChannelID: lnwire.NewShortChanIDFromInt(privateChannelID),
|
|
|
|
Timestamp: uint32(testTime.Add(time.Minute).Unix()),
|
|
|
|
BaseFee: updatedFeeBaseMSat,
|
|
|
|
TimeLockDelta: expiryDelta,
|
|
|
|
}
|
|
|
|
signErrChanUpdate(t, ctx.privKeys["songoku"], &errChanUpdate)
|
|
|
|
|
|
|
|
// We'll now modify the SendHTLC method to return an error for the
|
|
|
|
// outgoing channel to songoku.
|
|
|
|
errorReturned := false
|
|
|
|
copy(preImage[:], bytes.Repeat([]byte{9}, 32))
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
2021-04-15 18:40:22 +02:00
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
|
|
|
|
|
|
|
if firstHop != roasbeefSongoku || errorReturned {
|
|
|
|
return preImage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
errorReturned = true
|
|
|
|
return [32]byte{}, htlcswitch.NewForwardingError(
|
|
|
|
// Within our error, we'll add a
|
|
|
|
// channel update which is meant to
|
|
|
|
// reflect the new fee schedule for the
|
|
|
|
// node/channel.
|
|
|
|
&lnwire.FailFeeInsufficient{
|
|
|
|
Update: errChanUpdate,
|
|
|
|
}, 1,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Send off the payment request to the router, route through son
|
|
|
|
// goku and then across the private channel to elst.
|
|
|
|
paymentPreImage, route, err := ctx.router.SendPayment(&payment)
|
|
|
|
require.NoError(t, err, "unable to send payment")
|
|
|
|
|
|
|
|
require.True(t, errorReturned,
|
|
|
|
"failed to simulate error in the first payment attempt",
|
|
|
|
)
|
|
|
|
|
|
|
|
// The route selected should have three hops. Make sure that,
|
|
|
|
// path1: roasbeef -> son goku -> sophon -> elst
|
|
|
|
// path2: roasbeef -> pham nuwen -> sophon -> elst
|
|
|
|
// path3: roasbeef -> sophon -> (private channel) else
|
|
|
|
// path1 is selected.
|
|
|
|
require.Equal(t, 3, len(route.Hops), "incorrect route length")
|
|
|
|
|
|
|
|
// The preimage should match up with the one created above.
|
|
|
|
require.Equal(t,
|
|
|
|
paymentPreImage[:], preImage[:], "incorrect preimage used",
|
|
|
|
)
|
|
|
|
|
|
|
|
// The route should have son goku as the first hop.
|
|
|
|
require.Equal(t, route.Hops[0].PubKeyBytes, ctx.aliases["songoku"],
|
|
|
|
"route should go through son goku as the first hop",
|
|
|
|
)
|
|
|
|
|
|
|
|
// The route should have sophon as the first hop.
|
|
|
|
require.Equal(t, route.Hops[1].PubKeyBytes, ctx.aliases["sophon"],
|
|
|
|
"route should go through sophon as the second hop",
|
|
|
|
)
|
|
|
|
// The route should pass via the public channel.
|
|
|
|
require.Equal(t, route.FinalHop().PubKeyBytes, ctx.aliases["elst"],
|
|
|
|
"route should go through elst as the final hop",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-03-23 01:40:07 +01:00
|
|
|
// TestSendPaymentErrorNonFinalTimeLockErrors tests that if we receive either
|
|
|
|
// an ExpiryTooSoon or a IncorrectCltvExpiry error from a node, then we prune
|
|
|
|
// that node from the available graph witin a mission control session. This
|
|
|
|
// test ensures that we'll route around errors due to nodes not knowing the
|
|
|
|
// current block height.
|
|
|
|
func TestSendPaymentErrorNonFinalTimeLockErrors(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromFile(
|
|
|
|
t, startingBlockHeight, basicGraphFilePath,
|
|
|
|
)
|
2018-12-04 11:20:37 +01:00
|
|
|
defer cleanUp()
|
2018-03-23 01:40:07 +01:00
|
|
|
|
|
|
|
// Craft a LightningPayment struct that'll send a payment from roasbeef
|
|
|
|
// to sophon for 1k satoshis.
|
2021-03-31 12:23:08 +02:00
|
|
|
var payHash lntypes.Hash
|
2018-04-19 16:32:24 +02:00
|
|
|
amt := lnwire.NewMSatFromSatoshis(1000)
|
2018-03-23 01:40:07 +01:00
|
|
|
payment := LightningPayment{
|
|
|
|
Target: ctx.aliases["sophon"],
|
2018-04-19 16:32:24 +02:00
|
|
|
Amount: amt,
|
|
|
|
FeeLimit: noFeeLimit,
|
2021-03-31 12:23:08 +02:00
|
|
|
paymentHash: &payHash,
|
2018-03-23 01:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var preImage [32]byte
|
|
|
|
copy(preImage[:], bytes.Repeat([]byte{9}, 32))
|
|
|
|
|
|
|
|
// We'll also fetch the first outgoing channel edge from roasbeef to
|
|
|
|
// son goku. This edge will be included in the time lock related expiry
|
|
|
|
// errors that we'll get back due to disagrements in what the current
|
|
|
|
// block height is.
|
2021-04-10 13:27:58 +02:00
|
|
|
chanID := ctx.getChannelIDFromAlias(t, "roasbeef", "songoku")
|
2018-07-10 03:12:34 +02:00
|
|
|
roasbeefSongoku := lnwire.NewShortChanIDFromInt(chanID)
|
2021-04-10 13:27:58 +02:00
|
|
|
|
2018-12-09 03:42:46 +01:00
|
|
|
_, _, edgeUpdateToFail, err := ctx.graph.FetchChannelEdgesByID(chanID)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "unable to fetch chan id")
|
2018-03-23 01:40:07 +01:00
|
|
|
|
|
|
|
errChanUpdate := lnwire.ChannelUpdate{
|
|
|
|
ShortChannelID: lnwire.NewShortChanIDFromInt(chanID),
|
2018-12-09 03:42:46 +01:00
|
|
|
Timestamp: uint32(edgeUpdateToFail.LastUpdate.Unix()),
|
|
|
|
MessageFlags: edgeUpdateToFail.MessageFlags,
|
|
|
|
ChannelFlags: edgeUpdateToFail.ChannelFlags,
|
|
|
|
TimeLockDelta: edgeUpdateToFail.TimeLockDelta,
|
|
|
|
HtlcMinimumMsat: edgeUpdateToFail.MinHTLC,
|
|
|
|
HtlcMaximumMsat: edgeUpdateToFail.MaxHTLC,
|
|
|
|
BaseFee: uint32(edgeUpdateToFail.FeeBaseMSat),
|
|
|
|
FeeRate: uint32(edgeUpdateToFail.FeeProportionalMillionths),
|
2018-03-23 01:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now modify the SendToSwitch method to return an error for the
|
|
|
|
// outgoing channel to son goku. Since this is a time lock related
|
|
|
|
// error, we should fail the payment flow all together, as Goku is the
|
|
|
|
// only channel to Sophon.
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
2019-05-16 15:27:28 +02:00
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
|
|
|
|
|
|
|
if firstHop == roasbeefSongoku {
|
2020-01-14 14:07:29 +01:00
|
|
|
return [32]byte{}, htlcswitch.NewForwardingError(
|
|
|
|
&lnwire.FailExpiryTooSoon{
|
2019-05-16 15:27:28 +02:00
|
|
|
Update: errChanUpdate,
|
2020-01-14 14:07:42 +01:00
|
|
|
}, 1,
|
2020-01-14 14:07:29 +01:00
|
|
|
)
|
2019-03-05 16:55:19 +01:00
|
|
|
}
|
|
|
|
|
2019-05-16 15:27:28 +02:00
|
|
|
return preImage, nil
|
|
|
|
})
|
2018-03-23 01:40:07 +01:00
|
|
|
|
|
|
|
// assertExpectedPath is a helper function that asserts the returned
|
|
|
|
// route properly routes around the failure we've introduced in the
|
|
|
|
// graph.
|
2019-04-05 17:36:11 +02:00
|
|
|
assertExpectedPath := func(retPreImage [32]byte, route *route.Route) {
|
2018-03-23 01:40:07 +01:00
|
|
|
// The route selected should have two hops
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equal(t, 2, len(route.Hops), "incorrect route length")
|
2018-03-23 01:40:07 +01:00
|
|
|
|
|
|
|
// The preimage should match up with the once created above.
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equal(t,
|
|
|
|
preImage[:], retPreImage[:], "incorrect preimage used",
|
|
|
|
)
|
2018-03-23 01:40:07 +01:00
|
|
|
|
|
|
|
// The route should have satoshi as the first hop.
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equalf(t,
|
|
|
|
ctx.aliases["phamnuwen"], route.Hops[0].PubKeyBytes,
|
|
|
|
"route should go through phamnuwen as first hop, "+
|
2018-03-23 01:40:07 +01:00
|
|
|
"instead passes through: %v",
|
2021-04-14 06:58:12 +02:00
|
|
|
getAliasFromPubKey(
|
|
|
|
route.Hops[0].PubKeyBytes, ctx.aliases,
|
|
|
|
),
|
|
|
|
)
|
2018-03-23 01:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send off the payment request to the router, this payment should
|
2018-09-06 10:48:46 +02:00
|
|
|
// succeed as we should actually go through Pham Nuwen in order to get
|
2018-03-23 01:40:07 +01:00
|
|
|
// to Sophon, even though he has higher fees.
|
2019-04-05 17:36:11 +02:00
|
|
|
paymentPreImage, rt, err := ctx.router.SendPayment(&payment)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "unable to send payment")
|
2018-03-23 01:40:07 +01:00
|
|
|
|
2019-04-05 17:36:11 +02:00
|
|
|
assertExpectedPath(paymentPreImage, rt)
|
2018-03-23 01:40:07 +01:00
|
|
|
|
|
|
|
// We'll now modify the error return an IncorrectCltvExpiry error
|
|
|
|
// instead, this should result in the same behavior of roasbeef routing
|
|
|
|
// around the faulty Son Goku node.
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
2019-05-16 15:27:28 +02:00
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
|
|
|
|
|
|
|
if firstHop == roasbeefSongoku {
|
2020-01-14 14:07:29 +01:00
|
|
|
return [32]byte{}, htlcswitch.NewForwardingError(
|
|
|
|
&lnwire.FailIncorrectCltvExpiry{
|
2019-05-16 15:27:28 +02:00
|
|
|
Update: errChanUpdate,
|
2020-01-14 14:07:42 +01:00
|
|
|
}, 1,
|
2020-01-14 14:07:29 +01:00
|
|
|
)
|
2019-03-05 16:55:19 +01:00
|
|
|
}
|
|
|
|
|
2019-05-16 15:27:28 +02:00
|
|
|
return preImage, nil
|
|
|
|
})
|
2018-03-23 01:40:07 +01:00
|
|
|
|
|
|
|
// Once again, Roasbeef should route around Goku since they disagree
|
2019-05-23 20:05:30 +02:00
|
|
|
// w.r.t to the block height, and instead go through Pham Nuwen. We
|
|
|
|
// flip a bit in the payment hash to allow resending this payment.
|
2021-03-31 12:23:08 +02:00
|
|
|
payment.paymentHash[1] ^= 1
|
2019-04-05 17:36:11 +02:00
|
|
|
paymentPreImage, rt, err = ctx.router.SendPayment(&payment)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "unable to send payment")
|
2018-03-23 01:40:07 +01:00
|
|
|
|
2019-04-05 17:36:11 +02:00
|
|
|
assertExpectedPath(paymentPreImage, rt)
|
2018-03-23 01:40:07 +01:00
|
|
|
}
|
|
|
|
|
2017-10-11 04:49:09 +02:00
|
|
|
// TestSendPaymentErrorPathPruning tests that the send of candidate routes
|
|
|
|
// properly gets pruned in response to ForwardingError response from the
|
|
|
|
// underlying SendToSwitch function.
|
|
|
|
func TestSendPaymentErrorPathPruning(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromFile(
|
|
|
|
t, startingBlockHeight, basicGraphFilePath,
|
|
|
|
)
|
2018-12-04 11:20:37 +01:00
|
|
|
defer cleanUp()
|
2017-10-11 04:49:09 +02:00
|
|
|
|
|
|
|
// Craft a LightningPayment struct that'll send a payment from roasbeef
|
2018-01-31 23:36:10 +01:00
|
|
|
// to luo ji for 1000 satoshis, with a maximum of 1000 satoshis in fees.
|
2021-03-31 12:23:08 +02:00
|
|
|
var payHash lntypes.Hash
|
2018-01-31 23:36:10 +01:00
|
|
|
paymentAmt := lnwire.NewMSatFromSatoshis(1000)
|
2017-10-11 04:49:09 +02:00
|
|
|
payment := LightningPayment{
|
2019-06-18 19:42:51 +02:00
|
|
|
Target: ctx.aliases["sophon"],
|
2018-01-31 23:36:10 +01:00
|
|
|
Amount: paymentAmt,
|
2018-04-19 16:32:24 +02:00
|
|
|
FeeLimit: noFeeLimit,
|
2021-03-31 12:23:08 +02:00
|
|
|
paymentHash: &payHash,
|
2017-10-11 04:49:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var preImage [32]byte
|
|
|
|
copy(preImage[:], bytes.Repeat([]byte{9}, 32))
|
|
|
|
|
2021-04-10 13:27:58 +02:00
|
|
|
roasbeefSongoku := lnwire.NewShortChanIDFromInt(
|
|
|
|
ctx.getChannelIDFromAlias(t, "roasbeef", "songoku"),
|
|
|
|
)
|
|
|
|
roasbeefPhanNuwen := lnwire.NewShortChanIDFromInt(
|
|
|
|
ctx.getChannelIDFromAlias(t, "roasbeef", "phamnuwen"),
|
|
|
|
)
|
2018-07-10 03:12:34 +02:00
|
|
|
|
2017-10-11 04:49:09 +02:00
|
|
|
// First, we'll modify the SendToSwitch method to return an error
|
2019-06-18 19:42:51 +02:00
|
|
|
// indicating that the channel from roasbeef to son goku is not operable
|
2017-10-11 04:49:09 +02:00
|
|
|
// with an UnknownNextPeer.
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
2019-05-16 15:27:28 +02:00
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
|
|
|
|
2019-06-18 19:42:51 +02:00
|
|
|
if firstHop == roasbeefSongoku {
|
2019-05-16 15:27:28 +02:00
|
|
|
// We'll first simulate an error from the first
|
2019-06-18 19:42:51 +02:00
|
|
|
// hop to simulate the channel from songoku to
|
|
|
|
// sophon not having enough capacity.
|
2020-01-14 14:07:29 +01:00
|
|
|
return [32]byte{}, htlcswitch.NewForwardingError(
|
|
|
|
&lnwire.FailTemporaryChannelFailure{},
|
2020-01-14 14:07:42 +01:00
|
|
|
1,
|
2020-01-14 14:07:29 +01:00
|
|
|
)
|
2019-03-05 16:55:19 +01:00
|
|
|
}
|
|
|
|
|
2019-06-18 19:42:51 +02:00
|
|
|
// Next, we'll create an error from phan nuwen to
|
|
|
|
// indicate that the sophon node is not longer online,
|
|
|
|
// which should prune out the rest of the routes.
|
|
|
|
if firstHop == roasbeefPhanNuwen {
|
2020-01-14 14:07:29 +01:00
|
|
|
return [32]byte{}, htlcswitch.NewForwardingError(
|
2020-01-14 14:07:42 +01:00
|
|
|
&lnwire.FailUnknownNextPeer{}, 1,
|
2020-01-14 14:07:29 +01:00
|
|
|
)
|
2017-10-11 04:49:09 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 15:27:28 +02:00
|
|
|
return preImage, nil
|
|
|
|
})
|
2017-10-11 04:49:09 +02:00
|
|
|
|
2019-05-23 20:05:30 +02:00
|
|
|
ctx.router.cfg.MissionControl.(*MissionControl).ResetHistory()
|
2017-10-17 04:07:31 +02:00
|
|
|
|
2017-10-11 04:49:09 +02:00
|
|
|
// When we try to dispatch that payment, we should receive an error as
|
|
|
|
// both attempts should fail and cause both routes to be pruned.
|
2021-04-14 06:58:12 +02:00
|
|
|
_, _, err := ctx.router.SendPayment(&payment)
|
|
|
|
require.Error(t, err, "payment didn't return error")
|
2017-10-11 04:49:09 +02:00
|
|
|
|
|
|
|
// The final error returned should also indicate that the peer wasn't
|
|
|
|
// online (the last error we returned).
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equal(t, channeldb.FailureReasonNoRoute, err)
|
2017-10-11 04:49:09 +02:00
|
|
|
|
2020-04-01 00:13:24 +02:00
|
|
|
// Inspect the two attempts that were made before the payment failed.
|
|
|
|
p, err := ctx.router.cfg.Control.FetchPayment(payHash)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err)
|
2020-04-01 00:13:24 +02:00
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equal(t, 2, len(p.HTLCs), "expected two attempts")
|
2020-04-01 00:13:24 +02:00
|
|
|
|
|
|
|
// We expect the first attempt to have failed with a
|
|
|
|
// TemporaryChannelFailure, the second with UnknownNextPeer.
|
|
|
|
msg := p.HTLCs[0].Failure.Message
|
2021-04-14 06:58:12 +02:00
|
|
|
_, ok := msg.(*lnwire.FailTemporaryChannelFailure)
|
|
|
|
require.True(t, ok, "unexpected fail message")
|
2020-04-01 00:13:24 +02:00
|
|
|
|
|
|
|
msg = p.HTLCs[1].Failure.Message
|
2021-04-14 06:58:12 +02:00
|
|
|
_, ok = msg.(*lnwire.FailUnknownNextPeer)
|
|
|
|
require.True(t, ok, "unexpected fail message")
|
2020-04-01 00:13:24 +02:00
|
|
|
|
2021-05-21 13:02:49 +02:00
|
|
|
err = ctx.router.cfg.MissionControl.(*MissionControl).ResetHistory()
|
|
|
|
require.NoError(t, err, "reset history failed")
|
2017-10-17 04:07:31 +02:00
|
|
|
|
2019-06-18 19:42:51 +02:00
|
|
|
// Next, we'll modify the SendToSwitch method to indicate that the
|
|
|
|
// connection between songoku and isn't up.
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
2019-05-16 15:27:28 +02:00
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
|
|
|
|
2019-06-18 19:42:51 +02:00
|
|
|
if firstHop == roasbeefSongoku {
|
2020-01-14 14:07:29 +01:00
|
|
|
failure := htlcswitch.NewForwardingError(
|
2020-01-14 14:07:42 +01:00
|
|
|
&lnwire.FailUnknownNextPeer{}, 1,
|
2020-01-14 14:07:29 +01:00
|
|
|
)
|
|
|
|
return [32]byte{}, failure
|
2017-10-11 04:49:09 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 15:27:28 +02:00
|
|
|
return preImage, nil
|
|
|
|
})
|
2017-10-11 04:49:09 +02:00
|
|
|
|
2018-04-24 02:53:41 +02:00
|
|
|
// This shouldn't return an error, as we'll make a payment attempt via
|
2019-06-18 19:42:51 +02:00
|
|
|
// the pham nuwen channel based on the assumption that there might be an
|
|
|
|
// intermittent issue with the songoku <-> sophon channel.
|
2019-04-05 17:36:11 +02:00
|
|
|
paymentPreImage, rt, err := ctx.router.SendPayment(&payment)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "unable send payment")
|
2018-04-24 02:53:41 +02:00
|
|
|
|
2019-06-18 19:42:51 +02:00
|
|
|
// This path should go: roasbeef -> pham nuwen -> sophon
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equal(t, 2, len(rt.Hops), "incorrect route length")
|
|
|
|
require.Equal(t, preImage[:], paymentPreImage[:], "incorrect preimage")
|
|
|
|
require.Equalf(t,
|
|
|
|
ctx.aliases["phamnuwen"], rt.Hops[0].PubKeyBytes,
|
|
|
|
"route should go through phamnuwen as first hop, "+
|
2018-04-24 02:53:41 +02:00
|
|
|
"instead passes through: %v",
|
2021-04-14 06:58:12 +02:00
|
|
|
getAliasFromPubKey(rt.Hops[0].PubKeyBytes, ctx.aliases),
|
|
|
|
)
|
2017-10-11 04:49:09 +02:00
|
|
|
|
2019-05-23 20:05:30 +02:00
|
|
|
ctx.router.cfg.MissionControl.(*MissionControl).ResetHistory()
|
2017-10-17 04:07:31 +02:00
|
|
|
|
2017-10-11 04:49:09 +02:00
|
|
|
// Finally, we'll modify the SendToSwitch function to indicate that the
|
2018-04-24 02:53:41 +02:00
|
|
|
// roasbeef -> luoji channel has insufficient capacity. This should
|
|
|
|
// again cause us to instead go via the satoshi route.
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
2019-05-16 15:27:28 +02:00
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
|
|
|
|
2019-06-18 19:42:51 +02:00
|
|
|
if firstHop == roasbeefSongoku {
|
2019-05-16 15:27:28 +02:00
|
|
|
// We'll first simulate an error from the first
|
|
|
|
// outgoing link to simulate the channel from luo ji to
|
|
|
|
// roasbeef not having enough capacity.
|
2020-01-14 14:07:29 +01:00
|
|
|
return [32]byte{}, htlcswitch.NewForwardingError(
|
|
|
|
&lnwire.FailTemporaryChannelFailure{},
|
2020-01-14 14:07:42 +01:00
|
|
|
1,
|
2020-01-14 14:07:29 +01:00
|
|
|
)
|
2017-10-11 04:49:09 +02:00
|
|
|
}
|
2019-05-16 15:27:28 +02:00
|
|
|
return preImage, nil
|
|
|
|
})
|
2017-10-11 04:49:09 +02:00
|
|
|
|
2019-05-23 20:05:30 +02:00
|
|
|
// We flip a bit in the payment hash to allow resending this payment.
|
2021-03-31 12:23:08 +02:00
|
|
|
payment.paymentHash[1] ^= 1
|
2019-04-05 17:36:11 +02:00
|
|
|
paymentPreImage, rt, err = ctx.router.SendPayment(&payment)
|
2021-04-14 06:58:12 +02:00
|
|
|
require.NoError(t, err, "unable send payment")
|
2017-10-11 04:49:09 +02:00
|
|
|
|
2017-10-17 04:07:31 +02:00
|
|
|
// This should succeed finally. The route selected should have two
|
|
|
|
// hops.
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equal(t, 2, len(rt.Hops), "incorrect route length")
|
2017-10-11 04:49:09 +02:00
|
|
|
|
|
|
|
// The preimage should match up with the once created above.
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equal(t, preImage[:], paymentPreImage[:], "incorrect preimage")
|
2017-10-11 04:49:09 +02:00
|
|
|
|
|
|
|
// The route should have satoshi as the first hop.
|
2021-04-14 06:58:12 +02:00
|
|
|
require.Equalf(t,
|
|
|
|
ctx.aliases["phamnuwen"], rt.Hops[0].PubKeyBytes,
|
|
|
|
"route should go through phamnuwen as first hop, "+
|
2017-03-21 02:58:21 +01:00
|
|
|
"instead passes through: %v",
|
2021-04-14 06:58:12 +02:00
|
|
|
getAliasFromPubKey(rt.Hops[0].PubKeyBytes, ctx.aliases),
|
|
|
|
)
|
2017-03-21 02:58:21 +01:00
|
|
|
}
|
2017-03-27 19:00:38 +02:00
|
|
|
|
|
|
|
// TestAddProof checks that we can update the channel proof after channel
|
|
|
|
// info was added to the database.
|
|
|
|
func TestAddProof(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanup := createTestCtxSingleNode(t, 0)
|
2017-03-27 19:00:38 +02:00
|
|
|
defer cleanup()
|
|
|
|
|
2017-05-15 04:27:14 +02:00
|
|
|
// Before creating out edge, we'll create two new nodes within the
|
|
|
|
// network that the channel will connect.
|
|
|
|
node1, err := createTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
node2, err := createTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to be able to add the edge we should have a valid funding
|
|
|
|
// UTXO within the blockchain.
|
2017-03-27 19:00:38 +02:00
|
|
|
fundingTx, _, chanID, err := createChannelEdge(ctx,
|
|
|
|
bitcoinKey1.SerializeCompressed(), bitcoinKey2.SerializeCompressed(),
|
|
|
|
100, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
fundingBlock := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{fundingTx},
|
|
|
|
}
|
2017-10-02 17:54:29 +02:00
|
|
|
ctx.chain.addBlock(fundingBlock, chanID.BlockHeight, chanID.BlockHeight)
|
2017-03-27 19:00:38 +02:00
|
|
|
|
|
|
|
// After utxo was recreated adding the edge without the proof.
|
|
|
|
edge := &channeldb.ChannelEdgeInfo{
|
2018-01-31 05:26:26 +01:00
|
|
|
ChannelID: chanID.ToUint64(),
|
|
|
|
NodeKey1Bytes: node1.PubKeyBytes,
|
|
|
|
NodeKey2Bytes: node2.PubKeyBytes,
|
|
|
|
AuthProof: nil,
|
2017-03-27 19:00:38 +02:00
|
|
|
}
|
2018-01-31 05:26:26 +01:00
|
|
|
copy(edge.BitcoinKey1Bytes[:], bitcoinKey1.SerializeCompressed())
|
|
|
|
copy(edge.BitcoinKey2Bytes[:], bitcoinKey2.SerializeCompressed())
|
2017-03-27 19:00:38 +02:00
|
|
|
|
|
|
|
if err := ctx.router.AddEdge(edge); err != nil {
|
|
|
|
t.Fatalf("unable to add edge: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-15 04:27:14 +02:00
|
|
|
// Now we'll attempt to update the proof and check that it has been
|
|
|
|
// properly updated.
|
2017-03-27 19:00:38 +02:00
|
|
|
if err := ctx.router.AddProof(*chanID, &testAuthProof); err != nil {
|
|
|
|
t.Fatalf("unable to add proof: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info, _, _, err := ctx.router.GetChannelByID(*chanID)
|
2019-09-13 04:59:07 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get channel: %v", err)
|
|
|
|
}
|
2017-03-27 19:00:38 +02:00
|
|
|
if info.AuthProof == nil {
|
|
|
|
t.Fatal("proof have been updated")
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 04:27:14 +02:00
|
|
|
|
2017-07-14 21:32:00 +02:00
|
|
|
// TestIgnoreNodeAnnouncement tests that adding a node to the router that is
|
2017-10-19 07:13:11 +02:00
|
|
|
// not known from any channel announcement, leads to the announcement being
|
2017-07-14 21:32:00 +02:00
|
|
|
// ignored.
|
|
|
|
func TestIgnoreNodeAnnouncement(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromFile(
|
|
|
|
t, startingBlockHeight, basicGraphFilePath,
|
|
|
|
)
|
2018-12-04 11:20:37 +01:00
|
|
|
defer cleanUp()
|
2017-07-14 21:32:00 +02:00
|
|
|
|
2018-01-31 05:26:26 +01:00
|
|
|
pub := priv1.PubKey()
|
2017-07-14 21:32:00 +02:00
|
|
|
node := &channeldb.LightningNode{
|
|
|
|
HaveNodeAnnouncement: true,
|
|
|
|
LastUpdate: time.Unix(123, 0),
|
|
|
|
Addresses: testAddrs,
|
|
|
|
Color: color.RGBA{1, 2, 3, 0},
|
|
|
|
Alias: "node11",
|
2018-01-31 05:26:26 +01:00
|
|
|
AuthSigBytes: testSig.Serialize(),
|
2017-07-14 21:32:00 +02:00
|
|
|
Features: testFeatures,
|
|
|
|
}
|
2018-01-31 05:26:26 +01:00
|
|
|
copy(node.PubKeyBytes[:], pub.SerializeCompressed())
|
2017-07-14 21:32:00 +02:00
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
err := ctx.router.AddNode(node)
|
2017-07-14 21:32:00 +02:00
|
|
|
if !IsError(err, ErrIgnored) {
|
|
|
|
t.Fatalf("expected to get ErrIgnore, instead got: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 15:33:01 +01:00
|
|
|
// TestIgnoreChannelEdgePolicyForUnknownChannel checks that a router will
|
|
|
|
// ignore a channel policy for a channel not in the graph.
|
|
|
|
func TestIgnoreChannelEdgePolicyForUnknownChannel(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
|
|
|
|
|
|
|
// Setup an initially empty network.
|
|
|
|
testChannels := []*testChannel{}
|
2019-01-31 16:07:30 +01:00
|
|
|
testGraph, err := createTestGraphFromChannels(
|
|
|
|
testChannels, "roasbeef",
|
|
|
|
)
|
2018-12-04 15:33:01 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create graph: %v", err)
|
|
|
|
}
|
|
|
|
defer testGraph.cleanUp()
|
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromGraphInstance(
|
|
|
|
t, startingBlockHeight, testGraph, false,
|
2018-12-04 15:33:01 +01:00
|
|
|
)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
var pub1 [33]byte
|
|
|
|
copy(pub1[:], priv1.PubKey().SerializeCompressed())
|
|
|
|
|
|
|
|
var pub2 [33]byte
|
|
|
|
copy(pub2[:], priv2.PubKey().SerializeCompressed())
|
|
|
|
|
|
|
|
// Add the edge between the two unknown nodes to the graph, and check
|
|
|
|
// that the nodes are found after the fact.
|
|
|
|
fundingTx, _, chanID, err := createChannelEdge(
|
|
|
|
ctx, bitcoinKey1.SerializeCompressed(),
|
|
|
|
bitcoinKey2.SerializeCompressed(), 10000, 500,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
fundingBlock := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{fundingTx},
|
|
|
|
}
|
|
|
|
ctx.chain.addBlock(fundingBlock, chanID.BlockHeight, chanID.BlockHeight)
|
|
|
|
|
|
|
|
edge := &channeldb.ChannelEdgeInfo{
|
|
|
|
ChannelID: chanID.ToUint64(),
|
|
|
|
NodeKey1Bytes: pub1,
|
|
|
|
NodeKey2Bytes: pub2,
|
|
|
|
BitcoinKey1Bytes: pub1,
|
|
|
|
BitcoinKey2Bytes: pub2,
|
|
|
|
AuthProof: nil,
|
|
|
|
}
|
|
|
|
edgePolicy := &channeldb.ChannelEdgePolicy{
|
|
|
|
SigBytes: testSig.Serialize(),
|
|
|
|
ChannelID: edge.ChannelID,
|
|
|
|
LastUpdate: testTime,
|
|
|
|
TimeLockDelta: 10,
|
|
|
|
MinHTLC: 1,
|
|
|
|
FeeBaseMSat: 10,
|
|
|
|
FeeProportionalMillionths: 10000,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to update the edge. This should be ignored, since the edge
|
|
|
|
// is not yet added to the router.
|
|
|
|
err = ctx.router.UpdateEdge(edgePolicy)
|
|
|
|
if !IsError(err, ErrIgnored) {
|
|
|
|
t.Fatalf("expected to get ErrIgnore, instead got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the edge.
|
|
|
|
if err := ctx.router.AddEdge(edge); err != nil {
|
|
|
|
t.Fatalf("expected to be able to add edge to the channel graph,"+
|
|
|
|
" even though the vertexes were unknown: %v.", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now updating the edge policy should succeed.
|
|
|
|
if err := ctx.router.UpdateEdge(edgePolicy); err != nil {
|
|
|
|
t.Fatalf("unable to update edge policy: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 04:27:14 +02:00
|
|
|
// TestAddEdgeUnknownVertexes tests that if an edge is added that contains two
|
2017-07-14 21:32:00 +02:00
|
|
|
// vertexes which we don't know of, the edge should be available for use
|
|
|
|
// regardless. This is due to the fact that we don't actually need node
|
|
|
|
// announcements for the channel vertexes to be able to use the channel.
|
2017-05-15 04:27:14 +02:00
|
|
|
func TestAddEdgeUnknownVertexes(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-14 21:32:00 +02:00
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromFile(
|
|
|
|
t, startingBlockHeight, basicGraphFilePath,
|
2019-07-31 06:41:58 +02:00
|
|
|
)
|
2018-12-04 11:20:37 +01:00
|
|
|
defer cleanUp()
|
2017-05-15 04:27:14 +02:00
|
|
|
|
2018-01-31 05:26:26 +01:00
|
|
|
var pub1 [33]byte
|
|
|
|
copy(pub1[:], priv1.PubKey().SerializeCompressed())
|
|
|
|
|
|
|
|
var pub2 [33]byte
|
|
|
|
copy(pub2[:], priv2.PubKey().SerializeCompressed())
|
|
|
|
|
2017-07-14 21:32:00 +02:00
|
|
|
// The two nodes we are about to add should not exist yet.
|
2018-01-31 05:26:26 +01:00
|
|
|
_, exists1, err := ctx.graph.HasLightningNode(pub1)
|
2017-07-14 21:32:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query graph: %v", err)
|
|
|
|
}
|
|
|
|
if exists1 {
|
|
|
|
t.Fatalf("node already existed")
|
|
|
|
}
|
2018-01-31 05:26:26 +01:00
|
|
|
_, exists2, err := ctx.graph.HasLightningNode(pub2)
|
2017-07-14 21:32:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query graph: %v", err)
|
|
|
|
}
|
|
|
|
if exists2 {
|
|
|
|
t.Fatalf("node already existed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the edge between the two unknown nodes to the graph, and check
|
|
|
|
// that the nodes are found after the fact.
|
|
|
|
fundingTx, _, chanID, err := createChannelEdge(ctx,
|
|
|
|
bitcoinKey1.SerializeCompressed(),
|
2017-08-22 08:43:20 +02:00
|
|
|
bitcoinKey2.SerializeCompressed(),
|
2019-07-31 06:41:58 +02:00
|
|
|
10000, 500,
|
|
|
|
)
|
2017-05-15 04:27:14 +02:00
|
|
|
if err != nil {
|
2017-05-16 03:16:28 +02:00
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
2017-05-15 04:27:14 +02:00
|
|
|
}
|
2017-07-14 21:32:00 +02:00
|
|
|
fundingBlock := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{fundingTx},
|
|
|
|
}
|
2017-10-02 17:54:29 +02:00
|
|
|
ctx.chain.addBlock(fundingBlock, chanID.BlockHeight, chanID.BlockHeight)
|
2017-05-15 04:27:14 +02:00
|
|
|
|
|
|
|
edge := &channeldb.ChannelEdgeInfo{
|
2018-01-31 05:26:26 +01:00
|
|
|
ChannelID: chanID.ToUint64(),
|
|
|
|
NodeKey1Bytes: pub1,
|
|
|
|
NodeKey2Bytes: pub2,
|
|
|
|
BitcoinKey1Bytes: pub1,
|
|
|
|
BitcoinKey2Bytes: pub2,
|
|
|
|
AuthProof: nil,
|
2017-05-15 04:27:14 +02:00
|
|
|
}
|
2017-07-14 21:32:00 +02:00
|
|
|
if err := ctx.router.AddEdge(edge); err != nil {
|
|
|
|
t.Fatalf("expected to be able to add edge to the channel graph,"+
|
|
|
|
" even though the vertexes were unknown: %v.", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must add the edge policy to be able to use the edge for route
|
|
|
|
// finding.
|
|
|
|
edgePolicy := &channeldb.ChannelEdgePolicy{
|
2018-01-31 05:26:26 +01:00
|
|
|
SigBytes: testSig.Serialize(),
|
2017-07-14 21:32:00 +02:00
|
|
|
ChannelID: edge.ChannelID,
|
2018-08-16 20:47:29 +02:00
|
|
|
LastUpdate: testTime,
|
2017-07-14 21:32:00 +02:00
|
|
|
TimeLockDelta: 10,
|
2017-08-22 08:43:20 +02:00
|
|
|
MinHTLC: 1,
|
|
|
|
FeeBaseMSat: 10,
|
|
|
|
FeeProportionalMillionths: 10000,
|
2021-09-21 19:18:20 +02:00
|
|
|
Node: &channeldb.LightningNode{
|
|
|
|
PubKeyBytes: edge.NodeKey2Bytes,
|
|
|
|
},
|
2017-07-14 21:32:00 +02:00
|
|
|
}
|
2019-01-12 18:59:43 +01:00
|
|
|
edgePolicy.ChannelFlags = 0
|
2017-07-14 21:32:00 +02:00
|
|
|
|
|
|
|
if err := ctx.router.UpdateEdge(edgePolicy); err != nil {
|
|
|
|
t.Fatalf("unable to update edge policy: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create edge in the other direction as well.
|
|
|
|
edgePolicy = &channeldb.ChannelEdgePolicy{
|
2018-01-31 05:26:26 +01:00
|
|
|
SigBytes: testSig.Serialize(),
|
2017-07-14 21:32:00 +02:00
|
|
|
ChannelID: edge.ChannelID,
|
2018-08-16 20:47:29 +02:00
|
|
|
LastUpdate: testTime,
|
2017-07-14 21:32:00 +02:00
|
|
|
TimeLockDelta: 10,
|
2017-08-22 08:43:20 +02:00
|
|
|
MinHTLC: 1,
|
|
|
|
FeeBaseMSat: 10,
|
|
|
|
FeeProportionalMillionths: 10000,
|
2021-09-21 19:18:20 +02:00
|
|
|
Node: &channeldb.LightningNode{
|
|
|
|
PubKeyBytes: edge.NodeKey1Bytes,
|
|
|
|
},
|
2017-07-14 21:32:00 +02:00
|
|
|
}
|
2019-01-12 18:59:43 +01:00
|
|
|
edgePolicy.ChannelFlags = 1
|
2017-07-14 21:32:00 +02:00
|
|
|
|
|
|
|
if err := ctx.router.UpdateEdge(edgePolicy); err != nil {
|
|
|
|
t.Fatalf("unable to update edge policy: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// After adding the edge between the two previously unknown nodes, they
|
|
|
|
// should have been added to the graph.
|
2018-01-31 05:26:26 +01:00
|
|
|
_, exists1, err = ctx.graph.HasLightningNode(pub1)
|
2017-07-14 21:32:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query graph: %v", err)
|
|
|
|
}
|
|
|
|
if !exists1 {
|
|
|
|
t.Fatalf("node1 was not added to the graph")
|
|
|
|
}
|
2018-01-31 05:26:26 +01:00
|
|
|
_, exists2, err = ctx.graph.HasLightningNode(pub2)
|
2017-07-14 21:32:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query graph: %v", err)
|
|
|
|
}
|
|
|
|
if !exists2 {
|
|
|
|
t.Fatalf("node2 was not added to the graph")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We will connect node1 to the rest of the test graph, and make sure
|
|
|
|
// we can find a route to node2, which will use the just added channel
|
|
|
|
// edge.
|
|
|
|
|
|
|
|
// We will connect node 1 to "sophon"
|
|
|
|
connectNode := ctx.aliases["sophon"]
|
2019-03-05 16:55:19 +01:00
|
|
|
connectNodeKey, err := btcec.ParsePubKey(connectNode[:], btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2017-07-14 21:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
pubKey1 *btcec.PublicKey
|
|
|
|
pubKey2 *btcec.PublicKey
|
|
|
|
)
|
|
|
|
node1Bytes := priv1.PubKey().SerializeCompressed()
|
2019-03-05 16:55:19 +01:00
|
|
|
node2Bytes := connectNode
|
|
|
|
if bytes.Compare(node1Bytes[:], node2Bytes[:]) == -1 {
|
2017-07-14 21:32:00 +02:00
|
|
|
pubKey1 = priv1.PubKey()
|
2019-03-05 16:55:19 +01:00
|
|
|
pubKey2 = connectNodeKey
|
2017-07-14 21:32:00 +02:00
|
|
|
} else {
|
2019-03-05 16:55:19 +01:00
|
|
|
pubKey1 = connectNodeKey
|
2017-07-14 21:32:00 +02:00
|
|
|
pubKey2 = priv1.PubKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
fundingTx, _, chanID, err = createChannelEdge(ctx,
|
|
|
|
pubKey1.SerializeCompressed(), pubKey2.SerializeCompressed(),
|
|
|
|
10000, 510)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
fundingBlock = &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{fundingTx},
|
|
|
|
}
|
2017-10-02 17:54:29 +02:00
|
|
|
ctx.chain.addBlock(fundingBlock, chanID.BlockHeight, chanID.BlockHeight)
|
2017-07-14 21:32:00 +02:00
|
|
|
|
|
|
|
edge = &channeldb.ChannelEdgeInfo{
|
2018-01-31 05:26:26 +01:00
|
|
|
ChannelID: chanID.ToUint64(),
|
|
|
|
AuthProof: nil,
|
2017-07-14 21:32:00 +02:00
|
|
|
}
|
2018-01-31 05:26:26 +01:00
|
|
|
copy(edge.NodeKey1Bytes[:], node1Bytes)
|
2019-03-05 16:55:19 +01:00
|
|
|
edge.NodeKey2Bytes = node2Bytes
|
2018-01-31 05:26:26 +01:00
|
|
|
copy(edge.BitcoinKey1Bytes[:], node1Bytes)
|
2019-03-05 16:55:19 +01:00
|
|
|
edge.BitcoinKey2Bytes = node2Bytes
|
2017-07-14 21:32:00 +02:00
|
|
|
|
|
|
|
if err := ctx.router.AddEdge(edge); err != nil {
|
|
|
|
t.Fatalf("unable to add edge to the channel graph: %v.", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edgePolicy = &channeldb.ChannelEdgePolicy{
|
2018-01-31 05:26:26 +01:00
|
|
|
SigBytes: testSig.Serialize(),
|
2017-07-14 21:32:00 +02:00
|
|
|
ChannelID: edge.ChannelID,
|
2018-08-16 20:47:29 +02:00
|
|
|
LastUpdate: testTime,
|
2017-07-14 21:32:00 +02:00
|
|
|
TimeLockDelta: 10,
|
2017-08-22 08:43:20 +02:00
|
|
|
MinHTLC: 1,
|
|
|
|
FeeBaseMSat: 10,
|
|
|
|
FeeProportionalMillionths: 10000,
|
2021-09-21 19:18:20 +02:00
|
|
|
Node: &channeldb.LightningNode{
|
|
|
|
PubKeyBytes: edge.NodeKey2Bytes,
|
|
|
|
},
|
2017-07-14 21:32:00 +02:00
|
|
|
}
|
2019-01-12 18:59:43 +01:00
|
|
|
edgePolicy.ChannelFlags = 0
|
2017-07-14 21:32:00 +02:00
|
|
|
|
|
|
|
if err := ctx.router.UpdateEdge(edgePolicy); err != nil {
|
|
|
|
t.Fatalf("unable to update edge policy: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edgePolicy = &channeldb.ChannelEdgePolicy{
|
2018-01-31 05:26:26 +01:00
|
|
|
SigBytes: testSig.Serialize(),
|
2017-07-14 21:32:00 +02:00
|
|
|
ChannelID: edge.ChannelID,
|
2018-08-16 20:47:29 +02:00
|
|
|
LastUpdate: testTime,
|
2017-07-14 21:32:00 +02:00
|
|
|
TimeLockDelta: 10,
|
2017-08-22 08:43:20 +02:00
|
|
|
MinHTLC: 1,
|
|
|
|
FeeBaseMSat: 10,
|
|
|
|
FeeProportionalMillionths: 10000,
|
2021-09-21 19:18:20 +02:00
|
|
|
Node: &channeldb.LightningNode{
|
|
|
|
PubKeyBytes: edge.NodeKey1Bytes,
|
|
|
|
},
|
2017-07-14 21:32:00 +02:00
|
|
|
}
|
2019-01-12 18:59:43 +01:00
|
|
|
edgePolicy.ChannelFlags = 1
|
2017-07-14 21:32:00 +02:00
|
|
|
|
|
|
|
if err := ctx.router.UpdateEdge(edgePolicy); err != nil {
|
|
|
|
t.Fatalf("unable to update edge policy: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-07 17:01:01 +02:00
|
|
|
// We should now be able to find a route to node 2.
|
2017-08-22 08:43:20 +02:00
|
|
|
paymentAmt := lnwire.NewMSatFromSatoshis(100)
|
2017-07-14 21:32:00 +02:00
|
|
|
targetNode := priv2.PubKey()
|
2019-04-05 17:36:11 +02:00
|
|
|
var targetPubKeyBytes route.Vertex
|
2019-03-05 16:55:19 +01:00
|
|
|
copy(targetPubKeyBytes[:], targetNode.SerializeCompressed())
|
2019-05-07 17:01:01 +02:00
|
|
|
_, err = ctx.router.FindRoute(
|
2019-03-05 16:49:26 +01:00
|
|
|
ctx.router.selfNode.PubKeyBytes,
|
2020-01-14 11:21:24 +01:00
|
|
|
targetPubKeyBytes, paymentAmt, noRestrictions, nil, nil,
|
2020-07-24 22:13:56 +02:00
|
|
|
MinCLTVDelta,
|
2018-01-31 23:36:10 +01:00
|
|
|
)
|
2017-07-14 21:32:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find any routes: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check that we can update the node info for the partial node
|
|
|
|
// without messing up the channel graph.
|
|
|
|
n1 := &channeldb.LightningNode{
|
|
|
|
HaveNodeAnnouncement: true,
|
|
|
|
LastUpdate: time.Unix(123, 0),
|
|
|
|
Addresses: testAddrs,
|
|
|
|
Color: color.RGBA{1, 2, 3, 0},
|
|
|
|
Alias: "node11",
|
2018-01-31 05:26:26 +01:00
|
|
|
AuthSigBytes: testSig.Serialize(),
|
2017-07-14 21:32:00 +02:00
|
|
|
Features: testFeatures,
|
|
|
|
}
|
2018-01-31 05:26:26 +01:00
|
|
|
copy(n1.PubKeyBytes[:], priv1.PubKey().SerializeCompressed())
|
2017-07-14 21:32:00 +02:00
|
|
|
|
|
|
|
if err := ctx.router.AddNode(n1); err != nil {
|
|
|
|
t.Fatalf("could not add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n2 := &channeldb.LightningNode{
|
|
|
|
HaveNodeAnnouncement: true,
|
|
|
|
LastUpdate: time.Unix(123, 0),
|
|
|
|
Addresses: testAddrs,
|
|
|
|
Color: color.RGBA{1, 2, 3, 0},
|
|
|
|
Alias: "node22",
|
2018-01-31 05:26:26 +01:00
|
|
|
AuthSigBytes: testSig.Serialize(),
|
2017-07-14 21:32:00 +02:00
|
|
|
Features: testFeatures,
|
|
|
|
}
|
2018-01-31 05:26:26 +01:00
|
|
|
copy(n2.PubKeyBytes[:], priv2.PubKey().SerializeCompressed())
|
2017-07-14 21:32:00 +02:00
|
|
|
|
|
|
|
if err := ctx.router.AddNode(n2); err != nil {
|
|
|
|
t.Fatalf("could not add node: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-07 17:01:01 +02:00
|
|
|
// Should still be able to find the route, and the info should be
|
2017-07-14 21:32:00 +02:00
|
|
|
// updated.
|
2019-05-07 17:01:01 +02:00
|
|
|
_, err = ctx.router.FindRoute(
|
2019-03-05 16:49:26 +01:00
|
|
|
ctx.router.selfNode.PubKeyBytes,
|
2020-01-14 11:21:24 +01:00
|
|
|
targetPubKeyBytes, paymentAmt, noRestrictions, nil, nil,
|
2020-07-24 22:13:56 +02:00
|
|
|
MinCLTVDelta,
|
2018-01-31 23:36:10 +01:00
|
|
|
)
|
2017-07-14 21:32:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find any routes: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-09-21 19:18:18 +02:00
|
|
|
copy1, err := ctx.graph.FetchLightningNode(pub1)
|
2017-07-14 21:32:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if copy1.Alias != n1.Alias {
|
|
|
|
t.Fatalf("fetched node not equal to original")
|
|
|
|
}
|
|
|
|
|
2021-09-21 19:18:18 +02:00
|
|
|
copy2, err := ctx.graph.FetchLightningNode(pub2)
|
2017-07-14 21:32:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if copy2.Alias != n2.Alias {
|
|
|
|
t.Fatalf("fetched node not equal to original")
|
2017-05-15 04:27:14 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 17:54:29 +02:00
|
|
|
|
|
|
|
// TestWakeUpOnStaleBranch tests that upon startup of the ChannelRouter, if the
|
|
|
|
// the chain previously reflected in the channel graph is stale (overtaken by a
|
|
|
|
// longer chain), the channel router will prune the graph for any channels
|
|
|
|
// confirmed on the stale chain, and resync to the main chain.
|
|
|
|
func TestWakeUpOnStaleBranch(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxSingleNode(t, startingBlockHeight)
|
2018-12-04 11:20:37 +01:00
|
|
|
defer cleanUp()
|
2017-10-02 17:54:29 +02:00
|
|
|
|
|
|
|
const chanValue = 10000
|
|
|
|
|
|
|
|
// chanID1 will not be reorged out.
|
|
|
|
var chanID1 uint64
|
|
|
|
|
|
|
|
// chanID2 will be reorged out.
|
|
|
|
var chanID2 uint64
|
|
|
|
|
|
|
|
// Create 10 common blocks, confirming chanID1.
|
|
|
|
for i := uint32(1); i <= 10; i++ {
|
|
|
|
block := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{},
|
|
|
|
}
|
|
|
|
height := startingBlockHeight + i
|
|
|
|
if i == 5 {
|
|
|
|
fundingTx, _, chanID, err := createChannelEdge(ctx,
|
|
|
|
bitcoinKey1.SerializeCompressed(),
|
|
|
|
bitcoinKey2.SerializeCompressed(),
|
|
|
|
chanValue, height)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
block.Transactions = append(block.Transactions,
|
|
|
|
fundingTx)
|
|
|
|
chanID1 = chanID.ToUint64()
|
|
|
|
|
|
|
|
}
|
|
|
|
ctx.chain.addBlock(block, height, rand.Uint32())
|
|
|
|
ctx.chain.setBestBlock(int32(height))
|
|
|
|
ctx.chainView.notifyBlock(block.BlockHash(), height,
|
2021-03-18 03:32:26 +01:00
|
|
|
[]*wire.MsgTx{}, t)
|
2017-10-02 17:54:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Give time to process new blocks
|
|
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
|
|
|
|
_, forkHeight, err := ctx.chain.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to ge best block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create 10 blocks on the minority chain, confirming chanID2.
|
|
|
|
for i := uint32(1); i <= 10; i++ {
|
|
|
|
block := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{},
|
|
|
|
}
|
|
|
|
height := uint32(forkHeight) + i
|
|
|
|
if i == 5 {
|
|
|
|
fundingTx, _, chanID, err := createChannelEdge(ctx,
|
|
|
|
bitcoinKey1.SerializeCompressed(),
|
|
|
|
bitcoinKey2.SerializeCompressed(),
|
|
|
|
chanValue, height)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
block.Transactions = append(block.Transactions,
|
|
|
|
fundingTx)
|
|
|
|
chanID2 = chanID.ToUint64()
|
|
|
|
}
|
|
|
|
ctx.chain.addBlock(block, height, rand.Uint32())
|
|
|
|
ctx.chain.setBestBlock(int32(height))
|
|
|
|
ctx.chainView.notifyBlock(block.BlockHash(), height,
|
2021-03-18 03:32:26 +01:00
|
|
|
[]*wire.MsgTx{}, t)
|
2017-10-02 17:54:29 +02:00
|
|
|
}
|
|
|
|
// Give time to process new blocks
|
|
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
|
|
|
|
// Now add the two edges to the channel graph, and check that they
|
|
|
|
// correctly show up in the database.
|
|
|
|
node1, err := createTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edge1 := &channeldb.ChannelEdgeInfo{
|
2018-01-31 05:26:26 +01:00
|
|
|
ChannelID: chanID1,
|
|
|
|
NodeKey1Bytes: node1.PubKeyBytes,
|
|
|
|
NodeKey2Bytes: node2.PubKeyBytes,
|
2017-10-02 17:54:29 +02:00
|
|
|
AuthProof: &channeldb.ChannelAuthProof{
|
2018-01-31 05:26:26 +01:00
|
|
|
NodeSig1Bytes: testSig.Serialize(),
|
|
|
|
NodeSig2Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig1Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig2Bytes: testSig.Serialize(),
|
2017-10-02 17:54:29 +02:00
|
|
|
},
|
|
|
|
}
|
2018-01-31 05:26:26 +01:00
|
|
|
copy(edge1.BitcoinKey1Bytes[:], bitcoinKey1.SerializeCompressed())
|
|
|
|
copy(edge1.BitcoinKey2Bytes[:], bitcoinKey2.SerializeCompressed())
|
2017-10-02 17:54:29 +02:00
|
|
|
|
|
|
|
if err := ctx.router.AddEdge(edge1); err != nil {
|
|
|
|
t.Fatalf("unable to add edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edge2 := &channeldb.ChannelEdgeInfo{
|
2018-01-31 05:26:26 +01:00
|
|
|
ChannelID: chanID2,
|
|
|
|
NodeKey1Bytes: node1.PubKeyBytes,
|
|
|
|
NodeKey2Bytes: node2.PubKeyBytes,
|
2017-10-02 17:54:29 +02:00
|
|
|
AuthProof: &channeldb.ChannelAuthProof{
|
2018-01-31 05:26:26 +01:00
|
|
|
NodeSig1Bytes: testSig.Serialize(),
|
|
|
|
NodeSig2Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig1Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig2Bytes: testSig.Serialize(),
|
2017-10-02 17:54:29 +02:00
|
|
|
},
|
|
|
|
}
|
2018-01-31 05:26:26 +01:00
|
|
|
copy(edge2.BitcoinKey1Bytes[:], bitcoinKey1.SerializeCompressed())
|
|
|
|
copy(edge2.BitcoinKey2Bytes[:], bitcoinKey2.SerializeCompressed())
|
2017-10-02 17:54:29 +02:00
|
|
|
|
|
|
|
if err := ctx.router.AddEdge(edge2); err != nil {
|
|
|
|
t.Fatalf("unable to add edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the fundingTxs are in the graph db.
|
2019-03-27 21:06:57 +01:00
|
|
|
_, _, has, isZombie, err := ctx.graph.HasChannelEdge(chanID1)
|
2017-10-02 17:54:29 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error looking for edge: %v", chanID1)
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
t.Fatalf("could not find edge in graph")
|
|
|
|
}
|
2019-03-27 21:06:57 +01:00
|
|
|
if isZombie {
|
|
|
|
t.Fatal("edge was marked as zombie")
|
|
|
|
}
|
2017-10-02 17:54:29 +02:00
|
|
|
|
2019-03-27 21:06:57 +01:00
|
|
|
_, _, has, isZombie, err = ctx.graph.HasChannelEdge(chanID2)
|
2017-10-02 17:54:29 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error looking for edge: %v", chanID2)
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
t.Fatalf("could not find edge in graph")
|
|
|
|
}
|
2019-03-27 21:06:57 +01:00
|
|
|
if isZombie {
|
|
|
|
t.Fatal("edge was marked as zombie")
|
|
|
|
}
|
2017-10-02 17:54:29 +02:00
|
|
|
|
|
|
|
// Stop the router, so we can reorg the chain while its offline.
|
|
|
|
if err := ctx.router.Stop(); err != nil {
|
|
|
|
t.Fatalf("unable to stop router: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a 15 block fork.
|
|
|
|
for i := uint32(1); i <= 15; i++ {
|
|
|
|
block := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{},
|
|
|
|
}
|
|
|
|
height := uint32(forkHeight) + i
|
|
|
|
ctx.chain.addBlock(block, height, rand.Uint32())
|
|
|
|
ctx.chain.setBestBlock(int32(height))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give time to process new blocks.
|
|
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
|
|
|
|
// Create new router with same graph database.
|
|
|
|
router, err := New(Config{
|
2019-05-16 15:27:28 +02:00
|
|
|
Graph: ctx.graph,
|
|
|
|
Chain: ctx.chain,
|
|
|
|
ChainView: ctx.chainView,
|
2021-05-24 12:29:27 +02:00
|
|
|
Payer: &mockPaymentAttemptDispatcherOld{},
|
2019-05-23 20:05:29 +02:00
|
|
|
Control: makeMockControlTower(),
|
2017-10-02 17:54:29 +02:00
|
|
|
ChannelPruneExpiry: time.Hour * 24,
|
|
|
|
GraphPruneInterval: time.Hour * 2,
|
2021-03-11 13:36:54 +01:00
|
|
|
|
|
|
|
// We'll set the delay to zero to prune immediately.
|
|
|
|
FirstTimePruneDelay: 0,
|
2017-10-02 17:54:29 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create router %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It should resync to the longer chain on startup.
|
|
|
|
if err := router.Start(); err != nil {
|
|
|
|
t.Fatalf("unable to start router: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The channel with chanID2 should not be in the database anymore,
|
|
|
|
// since it is not confirmed on the longest chain. chanID1 should
|
|
|
|
// still be.
|
2019-03-27 21:06:57 +01:00
|
|
|
_, _, has, isZombie, err = ctx.graph.HasChannelEdge(chanID1)
|
2017-10-02 17:54:29 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error looking for edge: %v", chanID1)
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
t.Fatalf("did not find edge in graph")
|
|
|
|
}
|
2019-03-27 21:06:57 +01:00
|
|
|
if isZombie {
|
|
|
|
t.Fatal("edge was marked as zombie")
|
|
|
|
}
|
2017-10-02 17:54:29 +02:00
|
|
|
|
2019-03-27 21:06:57 +01:00
|
|
|
_, _, has, isZombie, err = ctx.graph.HasChannelEdge(chanID2)
|
2017-10-02 17:54:29 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error looking for edge: %v", chanID2)
|
|
|
|
}
|
|
|
|
if has {
|
|
|
|
t.Fatalf("found edge in graph")
|
|
|
|
}
|
2019-03-27 21:06:57 +01:00
|
|
|
if isZombie {
|
|
|
|
t.Fatal("reorged edge should not be marked as zombie")
|
|
|
|
}
|
2017-10-02 17:54:29 +02:00
|
|
|
}
|
|
|
|
|
2017-11-07 01:00:51 +01:00
|
|
|
// TestDisconnectedBlocks checks that the router handles a reorg happening when
|
|
|
|
// it is active.
|
2017-10-02 17:54:29 +02:00
|
|
|
func TestDisconnectedBlocks(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxSingleNode(t, startingBlockHeight)
|
2018-12-04 11:20:37 +01:00
|
|
|
defer cleanUp()
|
2017-10-02 17:54:29 +02:00
|
|
|
|
|
|
|
const chanValue = 10000
|
|
|
|
|
2017-11-07 01:00:51 +01:00
|
|
|
// chanID1 will not be reorged out, while chanID2 will be reorged out.
|
|
|
|
var chanID1, chanID2 uint64
|
2017-10-02 17:54:29 +02:00
|
|
|
|
|
|
|
// Create 10 common blocks, confirming chanID1.
|
|
|
|
for i := uint32(1); i <= 10; i++ {
|
|
|
|
block := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{},
|
|
|
|
}
|
|
|
|
height := startingBlockHeight + i
|
|
|
|
if i == 5 {
|
|
|
|
fundingTx, _, chanID, err := createChannelEdge(ctx,
|
|
|
|
bitcoinKey1.SerializeCompressed(),
|
|
|
|
bitcoinKey2.SerializeCompressed(),
|
|
|
|
chanValue, height)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
block.Transactions = append(block.Transactions,
|
|
|
|
fundingTx)
|
|
|
|
chanID1 = chanID.ToUint64()
|
|
|
|
|
|
|
|
}
|
|
|
|
ctx.chain.addBlock(block, height, rand.Uint32())
|
|
|
|
ctx.chain.setBestBlock(int32(height))
|
|
|
|
ctx.chainView.notifyBlock(block.BlockHash(), height,
|
2021-03-18 03:32:26 +01:00
|
|
|
[]*wire.MsgTx{}, t)
|
2017-10-02 17:54:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Give time to process new blocks
|
|
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
|
|
|
|
_, forkHeight, err := ctx.chain.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get best block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create 10 blocks on the minority chain, confirming chanID2.
|
|
|
|
var minorityChain []*wire.MsgBlock
|
|
|
|
for i := uint32(1); i <= 10; i++ {
|
|
|
|
block := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{},
|
|
|
|
}
|
|
|
|
height := uint32(forkHeight) + i
|
|
|
|
if i == 5 {
|
|
|
|
fundingTx, _, chanID, err := createChannelEdge(ctx,
|
|
|
|
bitcoinKey1.SerializeCompressed(),
|
|
|
|
bitcoinKey2.SerializeCompressed(),
|
|
|
|
chanValue, height)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
block.Transactions = append(block.Transactions,
|
|
|
|
fundingTx)
|
|
|
|
chanID2 = chanID.ToUint64()
|
|
|
|
}
|
|
|
|
minorityChain = append(minorityChain, block)
|
|
|
|
ctx.chain.addBlock(block, height, rand.Uint32())
|
|
|
|
ctx.chain.setBestBlock(int32(height))
|
|
|
|
ctx.chainView.notifyBlock(block.BlockHash(), height,
|
2021-03-18 03:32:26 +01:00
|
|
|
[]*wire.MsgTx{}, t)
|
2017-10-02 17:54:29 +02:00
|
|
|
}
|
|
|
|
// Give time to process new blocks
|
|
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
|
|
|
|
// Now add the two edges to the channel graph, and check that they
|
|
|
|
// correctly show up in the database.
|
|
|
|
node1, err := createTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edge1 := &channeldb.ChannelEdgeInfo{
|
2018-01-31 05:26:26 +01:00
|
|
|
ChannelID: chanID1,
|
|
|
|
NodeKey1Bytes: node1.PubKeyBytes,
|
|
|
|
NodeKey2Bytes: node2.PubKeyBytes,
|
|
|
|
BitcoinKey1Bytes: node1.PubKeyBytes,
|
|
|
|
BitcoinKey2Bytes: node2.PubKeyBytes,
|
2017-10-02 17:54:29 +02:00
|
|
|
AuthProof: &channeldb.ChannelAuthProof{
|
2018-01-31 05:26:26 +01:00
|
|
|
NodeSig1Bytes: testSig.Serialize(),
|
|
|
|
NodeSig2Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig1Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig2Bytes: testSig.Serialize(),
|
2017-10-02 17:54:29 +02:00
|
|
|
},
|
|
|
|
}
|
2018-01-31 05:26:26 +01:00
|
|
|
copy(edge1.BitcoinKey1Bytes[:], bitcoinKey1.SerializeCompressed())
|
|
|
|
copy(edge1.BitcoinKey2Bytes[:], bitcoinKey2.SerializeCompressed())
|
2017-10-02 17:54:29 +02:00
|
|
|
|
|
|
|
if err := ctx.router.AddEdge(edge1); err != nil {
|
|
|
|
t.Fatalf("unable to add edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edge2 := &channeldb.ChannelEdgeInfo{
|
2018-01-31 05:26:26 +01:00
|
|
|
ChannelID: chanID2,
|
|
|
|
NodeKey1Bytes: node1.PubKeyBytes,
|
|
|
|
NodeKey2Bytes: node2.PubKeyBytes,
|
|
|
|
BitcoinKey1Bytes: node1.PubKeyBytes,
|
|
|
|
BitcoinKey2Bytes: node2.PubKeyBytes,
|
2017-10-02 17:54:29 +02:00
|
|
|
AuthProof: &channeldb.ChannelAuthProof{
|
2018-01-31 05:26:26 +01:00
|
|
|
NodeSig1Bytes: testSig.Serialize(),
|
|
|
|
NodeSig2Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig1Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig2Bytes: testSig.Serialize(),
|
2017-10-02 17:54:29 +02:00
|
|
|
},
|
|
|
|
}
|
2018-01-31 05:26:26 +01:00
|
|
|
copy(edge2.BitcoinKey1Bytes[:], bitcoinKey1.SerializeCompressed())
|
|
|
|
copy(edge2.BitcoinKey2Bytes[:], bitcoinKey2.SerializeCompressed())
|
2017-10-02 17:54:29 +02:00
|
|
|
|
|
|
|
if err := ctx.router.AddEdge(edge2); err != nil {
|
|
|
|
t.Fatalf("unable to add edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the fundingTxs are in the graph db.
|
2019-03-27 21:06:57 +01:00
|
|
|
_, _, has, isZombie, err := ctx.graph.HasChannelEdge(chanID1)
|
2017-10-02 17:54:29 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error looking for edge: %v", chanID1)
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
t.Fatalf("could not find edge in graph")
|
|
|
|
}
|
2019-03-27 21:06:57 +01:00
|
|
|
if isZombie {
|
|
|
|
t.Fatal("edge was marked as zombie")
|
|
|
|
}
|
2017-10-02 17:54:29 +02:00
|
|
|
|
2019-03-27 21:06:57 +01:00
|
|
|
_, _, has, isZombie, err = ctx.graph.HasChannelEdge(chanID2)
|
2017-10-02 17:54:29 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error looking for edge: %v", chanID2)
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
t.Fatalf("could not find edge in graph")
|
|
|
|
}
|
2019-03-27 21:06:57 +01:00
|
|
|
if isZombie {
|
|
|
|
t.Fatal("edge was marked as zombie")
|
|
|
|
}
|
2017-10-02 17:54:29 +02:00
|
|
|
|
2017-11-07 01:00:51 +01:00
|
|
|
// Create a 15 block fork. We first let the chainView notify the router
|
|
|
|
// about stale blocks, before sending the now connected blocks. We do
|
|
|
|
// this because we expect this order from the chainview.
|
2021-03-18 03:32:26 +01:00
|
|
|
ctx.chainView.notifyStaleBlockAck = make(chan struct{}, 1)
|
2017-10-02 17:54:29 +02:00
|
|
|
for i := len(minorityChain) - 1; i >= 0; i-- {
|
|
|
|
block := minorityChain[i]
|
|
|
|
height := uint32(forkHeight) + uint32(i) + 1
|
|
|
|
ctx.chainView.notifyStaleBlock(block.BlockHash(), height,
|
2021-03-18 03:32:26 +01:00
|
|
|
block.Transactions, t)
|
|
|
|
<-ctx.chainView.notifyStaleBlockAck
|
2017-10-02 17:54:29 +02:00
|
|
|
}
|
2021-03-18 03:32:26 +01:00
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
ctx.chainView.notifyBlockAck = make(chan struct{}, 1)
|
2017-10-02 17:54:29 +02:00
|
|
|
for i := uint32(1); i <= 15; i++ {
|
|
|
|
block := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{},
|
|
|
|
}
|
|
|
|
height := uint32(forkHeight) + i
|
|
|
|
ctx.chain.addBlock(block, height, rand.Uint32())
|
|
|
|
ctx.chain.setBestBlock(int32(height))
|
|
|
|
ctx.chainView.notifyBlock(block.BlockHash(), height,
|
2021-03-18 03:32:26 +01:00
|
|
|
block.Transactions, t)
|
|
|
|
<-ctx.chainView.notifyBlockAck
|
2017-10-02 17:54:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
|
2017-11-07 01:00:51 +01:00
|
|
|
// chanID2 should not be in the database anymore, since it is not
|
|
|
|
// confirmed on the longest chain. chanID1 should still be.
|
2019-03-27 21:06:57 +01:00
|
|
|
_, _, has, isZombie, err = ctx.graph.HasChannelEdge(chanID1)
|
2017-10-02 17:54:29 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error looking for edge: %v", chanID1)
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
t.Fatalf("did not find edge in graph")
|
|
|
|
}
|
2019-03-27 21:06:57 +01:00
|
|
|
if isZombie {
|
|
|
|
t.Fatal("edge was marked as zombie")
|
|
|
|
}
|
2017-10-02 17:54:29 +02:00
|
|
|
|
2019-03-27 21:06:57 +01:00
|
|
|
_, _, has, isZombie, err = ctx.graph.HasChannelEdge(chanID2)
|
2017-10-02 17:54:29 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error looking for edge: %v", chanID2)
|
|
|
|
}
|
|
|
|
if has {
|
|
|
|
t.Fatalf("found edge in graph")
|
|
|
|
}
|
2019-03-27 21:06:57 +01:00
|
|
|
if isZombie {
|
|
|
|
t.Fatal("reorged edge should not be marked as zombie")
|
|
|
|
}
|
2017-10-02 17:54:29 +02:00
|
|
|
}
|
2017-11-07 01:06:11 +01:00
|
|
|
|
|
|
|
// TestChansClosedOfflinePruneGraph tests that if channels we know of are
|
|
|
|
// closed while we're offline, then once we resume operation of the
|
|
|
|
// ChannelRouter, then the channels are properly pruned.
|
|
|
|
func TestRouterChansClosedOfflinePruneGraph(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxSingleNode(t, startingBlockHeight)
|
2018-12-04 11:20:37 +01:00
|
|
|
defer cleanUp()
|
2017-11-07 01:06:11 +01:00
|
|
|
|
|
|
|
const chanValue = 10000
|
|
|
|
|
|
|
|
// First, we'll create a channel, to be mined shortly at height 102.
|
|
|
|
block102 := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{},
|
|
|
|
}
|
|
|
|
nextHeight := startingBlockHeight + 1
|
|
|
|
fundingTx1, chanUTXO, chanID1, err := createChannelEdge(ctx,
|
|
|
|
bitcoinKey1.SerializeCompressed(),
|
|
|
|
bitcoinKey2.SerializeCompressed(),
|
|
|
|
chanValue, uint32(nextHeight))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
block102.Transactions = append(block102.Transactions, fundingTx1)
|
|
|
|
ctx.chain.addBlock(block102, uint32(nextHeight), rand.Uint32())
|
|
|
|
ctx.chain.setBestBlock(int32(nextHeight))
|
|
|
|
ctx.chainView.notifyBlock(block102.BlockHash(), uint32(nextHeight),
|
2021-03-18 03:32:26 +01:00
|
|
|
[]*wire.MsgTx{}, t)
|
2017-11-07 01:06:11 +01:00
|
|
|
|
|
|
|
// We'll now create the edges and nodes within the database required
|
|
|
|
// for the ChannelRouter to properly recognize the channel we added
|
|
|
|
// above.
|
|
|
|
node1, err := createTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
edge1 := &channeldb.ChannelEdgeInfo{
|
2018-01-31 05:26:26 +01:00
|
|
|
ChannelID: chanID1.ToUint64(),
|
|
|
|
NodeKey1Bytes: node1.PubKeyBytes,
|
|
|
|
NodeKey2Bytes: node2.PubKeyBytes,
|
2017-11-07 01:06:11 +01:00
|
|
|
AuthProof: &channeldb.ChannelAuthProof{
|
2018-01-31 05:26:26 +01:00
|
|
|
NodeSig1Bytes: testSig.Serialize(),
|
|
|
|
NodeSig2Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig1Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig2Bytes: testSig.Serialize(),
|
2017-11-07 01:06:11 +01:00
|
|
|
},
|
|
|
|
}
|
2018-01-31 05:26:26 +01:00
|
|
|
copy(edge1.BitcoinKey1Bytes[:], bitcoinKey1.SerializeCompressed())
|
|
|
|
copy(edge1.BitcoinKey2Bytes[:], bitcoinKey2.SerializeCompressed())
|
2017-11-07 01:06:11 +01:00
|
|
|
if err := ctx.router.AddEdge(edge1); err != nil {
|
|
|
|
t.Fatalf("unable to add edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The router should now be aware of the channel we created above.
|
2019-03-27 21:06:57 +01:00
|
|
|
_, _, hasChan, isZombie, err := ctx.graph.HasChannelEdge(chanID1.ToUint64())
|
2017-11-07 01:06:11 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error looking for edge: %v", chanID1)
|
|
|
|
}
|
|
|
|
if !hasChan {
|
|
|
|
t.Fatalf("could not find edge in graph")
|
|
|
|
}
|
2019-03-27 21:06:57 +01:00
|
|
|
if isZombie {
|
|
|
|
t.Fatal("edge was marked as zombie")
|
|
|
|
}
|
2017-11-07 01:06:11 +01:00
|
|
|
|
|
|
|
// With the transaction included, and the router's database state
|
|
|
|
// updated, we'll now mine 5 additional blocks on top of it.
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
nextHeight++
|
|
|
|
|
|
|
|
block := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{},
|
|
|
|
}
|
|
|
|
ctx.chain.addBlock(block, uint32(nextHeight), rand.Uint32())
|
|
|
|
ctx.chain.setBestBlock(int32(nextHeight))
|
|
|
|
ctx.chainView.notifyBlock(block.BlockHash(), uint32(nextHeight),
|
2021-03-18 03:32:26 +01:00
|
|
|
[]*wire.MsgTx{}, t)
|
2017-11-07 01:06:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, our starting height should be 107.
|
|
|
|
_, chainHeight, err := ctx.chain.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get best block: %v", err)
|
|
|
|
}
|
|
|
|
if chainHeight != 107 {
|
|
|
|
t.Fatalf("incorrect chain height: expected %v, got %v",
|
2017-11-07 01:49:27 +01:00
|
|
|
107, chainHeight)
|
2017-11-07 01:06:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll "shut down" the router in order to simulate downtime.
|
|
|
|
if err := ctx.router.Stop(); err != nil {
|
|
|
|
t.Fatalf("unable to shutdown router: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// While the router is "offline" we'll mine 5 additional blocks, with
|
|
|
|
// the second block closing the channel we created above.
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
nextHeight++
|
|
|
|
|
|
|
|
block := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{},
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == 2 {
|
|
|
|
// For the second block, we'll add a transaction that
|
|
|
|
// closes the channel we created above by spending the
|
|
|
|
// output.
|
|
|
|
closingTx := wire.NewMsgTx(2)
|
|
|
|
closingTx.AddTxIn(&wire.TxIn{
|
|
|
|
PreviousOutPoint: *chanUTXO,
|
|
|
|
})
|
|
|
|
block.Transactions = append(block.Transactions,
|
|
|
|
closingTx)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.chain.addBlock(block, uint32(nextHeight), rand.Uint32())
|
|
|
|
ctx.chain.setBestBlock(int32(nextHeight))
|
|
|
|
ctx.chainView.notifyBlock(block.BlockHash(), uint32(nextHeight),
|
2021-03-18 03:32:26 +01:00
|
|
|
[]*wire.MsgTx{}, t)
|
2017-11-07 01:06:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, our starting height should be 112.
|
|
|
|
_, chainHeight, err = ctx.chain.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get best block: %v", err)
|
|
|
|
}
|
|
|
|
if chainHeight != 112 {
|
|
|
|
t.Fatalf("incorrect chain height: expected %v, got %v",
|
2017-11-07 01:49:27 +01:00
|
|
|
112, chainHeight)
|
2017-11-07 01:06:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now we'll re-start the ChannelRouter. It should recognize that it's
|
|
|
|
// behind the main chain and prune all the blocks that it missed while
|
|
|
|
// it was down.
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx.RestartRouter(t)
|
2017-11-07 01:06:11 +01:00
|
|
|
|
|
|
|
// At this point, the channel that was pruned should no longer be known
|
|
|
|
// by the router.
|
2019-03-27 21:06:57 +01:00
|
|
|
_, _, hasChan, isZombie, err = ctx.graph.HasChannelEdge(chanID1.ToUint64())
|
2017-11-07 01:06:11 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error looking for edge: %v", chanID1)
|
|
|
|
}
|
|
|
|
if hasChan {
|
|
|
|
t.Fatalf("channel was found in graph but shouldn't have been")
|
|
|
|
}
|
2019-03-27 21:06:57 +01:00
|
|
|
if isZombie {
|
|
|
|
t.Fatal("closed channel should not be marked as zombie")
|
|
|
|
}
|
2017-11-07 01:06:11 +01:00
|
|
|
}
|
2018-02-13 01:27:30 +01:00
|
|
|
|
2019-04-17 22:22:33 +02:00
|
|
|
// TestPruneChannelGraphStaleEdges ensures that we properly prune stale edges
|
|
|
|
// from the channel graph.
|
|
|
|
func TestPruneChannelGraphStaleEdges(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
freshTimestamp := time.Now()
|
2019-07-10 14:20:42 +02:00
|
|
|
staleTimestamp := time.Unix(0, 0)
|
2019-04-17 22:22:33 +02:00
|
|
|
|
2021-03-24 00:58:55 +01:00
|
|
|
// We'll create the following test graph so that two of the channels
|
|
|
|
// are pruned.
|
2019-04-17 22:22:33 +02:00
|
|
|
testChannels := []*testChannel{
|
|
|
|
// No edges.
|
|
|
|
{
|
|
|
|
Node1: &testChannelEnd{Alias: "a"},
|
|
|
|
Node2: &testChannelEnd{Alias: "b"},
|
|
|
|
Capacity: 100000,
|
|
|
|
ChannelID: 1,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Only one edge with a stale timestamp.
|
|
|
|
{
|
|
|
|
Node1: &testChannelEnd{
|
2021-04-02 23:57:50 +02:00
|
|
|
Alias: "d",
|
2019-04-17 22:22:33 +02:00
|
|
|
testChannelPolicy: &testChannelPolicy{
|
|
|
|
LastUpdate: staleTimestamp,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Node2: &testChannelEnd{Alias: "b"},
|
|
|
|
Capacity: 100000,
|
|
|
|
ChannelID: 2,
|
|
|
|
},
|
|
|
|
|
2021-04-02 23:57:50 +02:00
|
|
|
// Only one edge with a stale timestamp, but it's the source
|
|
|
|
// node so it won't get pruned.
|
|
|
|
{
|
|
|
|
Node1: &testChannelEnd{
|
|
|
|
Alias: "a",
|
|
|
|
testChannelPolicy: &testChannelPolicy{
|
|
|
|
LastUpdate: staleTimestamp,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Node2: &testChannelEnd{Alias: "b"},
|
|
|
|
Capacity: 100000,
|
|
|
|
ChannelID: 3,
|
|
|
|
},
|
|
|
|
|
2019-04-17 22:22:33 +02:00
|
|
|
// Only one edge with a fresh timestamp.
|
|
|
|
{
|
|
|
|
Node1: &testChannelEnd{
|
|
|
|
Alias: "a",
|
|
|
|
testChannelPolicy: &testChannelPolicy{
|
|
|
|
LastUpdate: freshTimestamp,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Node2: &testChannelEnd{Alias: "b"},
|
|
|
|
Capacity: 100000,
|
2021-04-02 23:57:50 +02:00
|
|
|
ChannelID: 4,
|
2019-04-17 22:22:33 +02:00
|
|
|
},
|
|
|
|
|
2021-04-02 23:57:50 +02:00
|
|
|
// One edge fresh, one edge stale. This will be pruned with
|
|
|
|
// strict pruning activated.
|
2019-04-17 22:22:33 +02:00
|
|
|
{
|
|
|
|
Node1: &testChannelEnd{
|
|
|
|
Alias: "c",
|
|
|
|
testChannelPolicy: &testChannelPolicy{
|
|
|
|
LastUpdate: freshTimestamp,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Node2: &testChannelEnd{
|
|
|
|
Alias: "d",
|
|
|
|
testChannelPolicy: &testChannelPolicy{
|
|
|
|
LastUpdate: staleTimestamp,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Capacity: 100000,
|
2021-04-02 23:57:50 +02:00
|
|
|
ChannelID: 5,
|
2019-04-17 22:22:33 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Both edges fresh.
|
|
|
|
symmetricTestChannel("g", "h", 100000, &testChannelPolicy{
|
|
|
|
LastUpdate: freshTimestamp,
|
2021-04-02 23:57:50 +02:00
|
|
|
}, 6),
|
2019-04-17 22:22:33 +02:00
|
|
|
|
2021-04-02 23:57:50 +02:00
|
|
|
// Both edges stale, only one pruned. This should be pruned for
|
|
|
|
// both normal and strict pruning.
|
2019-04-17 22:22:33 +02:00
|
|
|
symmetricTestChannel("e", "f", 100000, &testChannelPolicy{
|
|
|
|
LastUpdate: staleTimestamp,
|
2021-04-02 23:57:50 +02:00
|
|
|
}, 7),
|
2019-04-17 22:22:33 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 23:57:50 +02:00
|
|
|
for _, strictPruning := range []bool{true, false} {
|
|
|
|
// We'll create our test graph and router backed with these test
|
|
|
|
// channels we've created.
|
|
|
|
testGraph, err := createTestGraphFromChannels(testChannels, "a")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test graph: %v", err)
|
|
|
|
}
|
|
|
|
defer testGraph.cleanUp()
|
2019-04-17 22:22:33 +02:00
|
|
|
|
2021-04-02 23:57:50 +02:00
|
|
|
const startingHeight = 100
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromGraphInstance(
|
|
|
|
t, startingHeight, testGraph, strictPruning,
|
2021-04-02 23:57:50 +02:00
|
|
|
)
|
|
|
|
defer cleanUp()
|
2019-04-17 22:22:33 +02:00
|
|
|
|
2021-04-02 23:57:50 +02:00
|
|
|
// All of the channels should exist before pruning them.
|
|
|
|
assertChannelsPruned(t, ctx.graph, testChannels)
|
2019-04-17 22:22:33 +02:00
|
|
|
|
2021-04-02 23:57:50 +02:00
|
|
|
// Proceed to prune the channels - only the last one should be pruned.
|
|
|
|
if err := ctx.router.pruneZombieChans(); err != nil {
|
|
|
|
t.Fatalf("unable to prune zombie channels: %v", err)
|
|
|
|
}
|
2019-04-17 22:22:33 +02:00
|
|
|
|
2021-04-02 23:57:50 +02:00
|
|
|
// We expect channels that have either both edges stale, or one edge
|
|
|
|
// stale with both known.
|
|
|
|
var prunedChannels []uint64
|
|
|
|
if strictPruning {
|
|
|
|
prunedChannels = []uint64{2, 5, 7}
|
|
|
|
} else {
|
|
|
|
prunedChannels = []uint64{2, 7}
|
|
|
|
}
|
|
|
|
assertChannelsPruned(t, ctx.graph, testChannels, prunedChannels...)
|
|
|
|
}
|
2019-04-17 22:22:33 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 22:24:14 +02:00
|
|
|
// TestPruneChannelGraphDoubleDisabled test that we can properly prune channels
|
|
|
|
// with both edges disabled from our channel graph.
|
|
|
|
func TestPruneChannelGraphDoubleDisabled(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2021-02-18 03:55:56 +01:00
|
|
|
t.Run("no_assumechannelvalid", func(t *testing.T) {
|
|
|
|
testPruneChannelGraphDoubleDisabled(t, false)
|
|
|
|
})
|
|
|
|
t.Run("assumechannelvalid", func(t *testing.T) {
|
|
|
|
testPruneChannelGraphDoubleDisabled(t, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
|
2019-04-17 22:24:14 +02:00
|
|
|
// We'll create the following test graph so that only the last channel
|
|
|
|
// is pruned. We'll use a fresh timestamp to ensure they're not pruned
|
|
|
|
// according to that heuristic.
|
|
|
|
timestamp := time.Now()
|
|
|
|
testChannels := []*testChannel{
|
2019-01-31 16:07:30 +01:00
|
|
|
// Channel from self shouldn't be pruned.
|
|
|
|
symmetricTestChannel(
|
|
|
|
"self", "a", 100000, &testChannelPolicy{
|
|
|
|
LastUpdate: timestamp,
|
|
|
|
Disabled: true,
|
|
|
|
}, 99,
|
|
|
|
),
|
|
|
|
|
2019-04-17 22:24:14 +02:00
|
|
|
// No edges.
|
|
|
|
{
|
|
|
|
Node1: &testChannelEnd{Alias: "a"},
|
|
|
|
Node2: &testChannelEnd{Alias: "b"},
|
|
|
|
Capacity: 100000,
|
|
|
|
ChannelID: 1,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Only one edge disabled.
|
|
|
|
{
|
|
|
|
Node1: &testChannelEnd{
|
|
|
|
Alias: "a",
|
|
|
|
testChannelPolicy: &testChannelPolicy{
|
|
|
|
LastUpdate: timestamp,
|
|
|
|
Disabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Node2: &testChannelEnd{Alias: "b"},
|
|
|
|
Capacity: 100000,
|
|
|
|
ChannelID: 2,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Only one edge enabled.
|
|
|
|
{
|
|
|
|
Node1: &testChannelEnd{
|
|
|
|
Alias: "a",
|
|
|
|
testChannelPolicy: &testChannelPolicy{
|
|
|
|
LastUpdate: timestamp,
|
|
|
|
Disabled: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Node2: &testChannelEnd{Alias: "b"},
|
|
|
|
Capacity: 100000,
|
|
|
|
ChannelID: 3,
|
|
|
|
},
|
|
|
|
|
|
|
|
// One edge disabled, one edge enabled.
|
|
|
|
{
|
|
|
|
Node1: &testChannelEnd{
|
|
|
|
Alias: "a",
|
|
|
|
testChannelPolicy: &testChannelPolicy{
|
|
|
|
LastUpdate: timestamp,
|
|
|
|
Disabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Node2: &testChannelEnd{
|
|
|
|
Alias: "b",
|
|
|
|
testChannelPolicy: &testChannelPolicy{
|
|
|
|
LastUpdate: timestamp,
|
|
|
|
Disabled: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Capacity: 100000,
|
|
|
|
ChannelID: 1,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Both edges enabled.
|
|
|
|
symmetricTestChannel("c", "d", 100000, &testChannelPolicy{
|
|
|
|
LastUpdate: timestamp,
|
|
|
|
Disabled: false,
|
|
|
|
}, 2),
|
|
|
|
|
|
|
|
// Both edges disabled, only one pruned.
|
|
|
|
symmetricTestChannel("e", "f", 100000, &testChannelPolicy{
|
|
|
|
LastUpdate: timestamp,
|
|
|
|
Disabled: true,
|
|
|
|
}, 3),
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll create our test graph and router backed with these test
|
|
|
|
// channels we've created.
|
2019-01-31 16:07:30 +01:00
|
|
|
testGraph, err := createTestGraphFromChannels(testChannels, "self")
|
2019-04-17 22:24:14 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test graph: %v", err)
|
|
|
|
}
|
|
|
|
defer testGraph.cleanUp()
|
|
|
|
|
|
|
|
const startingHeight = 100
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromGraphInstanceAssumeValid(
|
|
|
|
t, startingHeight, testGraph, assumeValid, false,
|
2019-04-17 22:24:14 +02:00
|
|
|
)
|
|
|
|
defer cleanUp()
|
|
|
|
|
2021-02-18 03:55:56 +01:00
|
|
|
// All the channels should exist within the graph before pruning them
|
|
|
|
// when not using AssumeChannelValid, otherwise we should have pruned
|
|
|
|
// the last channel on startup.
|
|
|
|
if !assumeValid {
|
|
|
|
assertChannelsPruned(t, ctx.graph, testChannels)
|
|
|
|
} else {
|
2021-03-11 13:36:54 +01:00
|
|
|
// Sleep to allow the pruning to finish.
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
2021-02-18 03:55:56 +01:00
|
|
|
prunedChannel := testChannels[len(testChannels)-1].ChannelID
|
|
|
|
assertChannelsPruned(t, ctx.graph, testChannels, prunedChannel)
|
2019-04-17 22:24:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := ctx.router.pruneZombieChans(); err != nil {
|
|
|
|
t.Fatalf("unable to prune zombie channels: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-18 03:55:56 +01:00
|
|
|
// If we attempted to prune them without AssumeChannelValid being set,
|
|
|
|
// none should be pruned. Otherwise the last channel should still be
|
|
|
|
// pruned.
|
|
|
|
if !assumeValid {
|
|
|
|
assertChannelsPruned(t, ctx.graph, testChannels)
|
|
|
|
} else {
|
|
|
|
prunedChannel := testChannels[len(testChannels)-1].ChannelID
|
|
|
|
assertChannelsPruned(t, ctx.graph, testChannels, prunedChannel)
|
|
|
|
}
|
2019-04-17 22:24:14 +02:00
|
|
|
}
|
|
|
|
|
2018-02-13 01:27:30 +01:00
|
|
|
// TestFindPathFeeWeighting tests that the findPath method will properly prefer
|
|
|
|
// routes with lower fees over routes with lower time lock values. This is
|
|
|
|
// meant to exercise the fact that the internal findPath method ranks edges
|
|
|
|
// with the square of the total fee in order bias towards lower fees.
|
|
|
|
func TestFindPathFeeWeighting(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromFile(
|
|
|
|
t, startingBlockHeight, basicGraphFilePath,
|
|
|
|
)
|
2018-12-04 11:20:37 +01:00
|
|
|
defer cleanUp()
|
2018-02-13 01:27:30 +01:00
|
|
|
|
|
|
|
var preImage [32]byte
|
|
|
|
copy(preImage[:], bytes.Repeat([]byte{9}, 32))
|
|
|
|
|
|
|
|
sourceNode, err := ctx.graph.SourceNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch source node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
amt := lnwire.MilliSatoshi(100)
|
|
|
|
|
|
|
|
target := ctx.aliases["luoji"]
|
|
|
|
|
|
|
|
// We'll now attempt a path finding attempt using this set up. Due to
|
|
|
|
// the edge weighting, we should select the direct path over the 2 hop
|
|
|
|
// path even though the direct path has a higher potential time lock.
|
2020-03-17 11:32:07 +01:00
|
|
|
path, err := dbFindPath(
|
2021-10-19 09:37:44 +02:00
|
|
|
ctx.graph, nil, &mockBandwidthHints{},
|
2019-03-19 11:45:10 +01:00
|
|
|
noRestrictions,
|
2019-06-20 12:03:45 +02:00
|
|
|
testPathFindingConfig,
|
2019-12-17 11:55:03 +01:00
|
|
|
sourceNode.PubKeyBytes, target, amt, 0,
|
2018-02-13 01:27:30 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find path: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The route that was chosen should be exactly one hop, and should be
|
|
|
|
// directly to luoji.
|
|
|
|
if len(path) != 1 {
|
|
|
|
t.Fatalf("expected path length of 1, instead was: %v", len(path))
|
|
|
|
}
|
2021-09-21 19:18:22 +02:00
|
|
|
if path[0].ToNodePubKey() != ctx.aliases["luoji"] {
|
|
|
|
t.Fatalf("wrong node: %v", path[0].ToNodePubKey())
|
2018-02-13 01:27:30 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-25 04:34:03 +01:00
|
|
|
|
|
|
|
// TestIsStaleNode tests that the IsStaleNode method properly detects stale
|
|
|
|
// node announcements.
|
|
|
|
func TestIsStaleNode(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxSingleNode(t, startingBlockHeight)
|
2018-12-04 11:20:37 +01:00
|
|
|
defer cleanUp()
|
2018-02-25 04:34:03 +01:00
|
|
|
|
|
|
|
// Before we can insert a node in to the database, we need to create a
|
|
|
|
// channel that it's linked to.
|
|
|
|
var (
|
|
|
|
pub1 [33]byte
|
|
|
|
pub2 [33]byte
|
|
|
|
)
|
|
|
|
copy(pub1[:], priv1.PubKey().SerializeCompressed())
|
|
|
|
copy(pub2[:], priv2.PubKey().SerializeCompressed())
|
|
|
|
|
|
|
|
fundingTx, _, chanID, err := createChannelEdge(ctx,
|
|
|
|
bitcoinKey1.SerializeCompressed(),
|
|
|
|
bitcoinKey2.SerializeCompressed(),
|
|
|
|
10000, 500)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
fundingBlock := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{fundingTx},
|
|
|
|
}
|
|
|
|
ctx.chain.addBlock(fundingBlock, chanID.BlockHeight, chanID.BlockHeight)
|
|
|
|
|
|
|
|
edge := &channeldb.ChannelEdgeInfo{
|
|
|
|
ChannelID: chanID.ToUint64(),
|
|
|
|
NodeKey1Bytes: pub1,
|
|
|
|
NodeKey2Bytes: pub2,
|
|
|
|
BitcoinKey1Bytes: pub1,
|
|
|
|
BitcoinKey2Bytes: pub2,
|
|
|
|
AuthProof: nil,
|
|
|
|
}
|
|
|
|
if err := ctx.router.AddEdge(edge); err != nil {
|
|
|
|
t.Fatalf("unable to add edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we add the node, if we query for staleness, we should get
|
|
|
|
// false, as we haven't added the full node.
|
|
|
|
updateTimeStamp := time.Unix(123, 0)
|
|
|
|
if ctx.router.IsStaleNode(pub1, updateTimeStamp) {
|
|
|
|
t.Fatalf("incorrectly detected node as stale")
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the node stub in the database, we'll add the fully node
|
|
|
|
// announcement to the database.
|
|
|
|
n1 := &channeldb.LightningNode{
|
|
|
|
HaveNodeAnnouncement: true,
|
|
|
|
LastUpdate: updateTimeStamp,
|
|
|
|
Addresses: testAddrs,
|
|
|
|
Color: color.RGBA{1, 2, 3, 0},
|
|
|
|
Alias: "node11",
|
|
|
|
AuthSigBytes: testSig.Serialize(),
|
|
|
|
Features: testFeatures,
|
|
|
|
}
|
|
|
|
copy(n1.PubKeyBytes[:], priv1.PubKey().SerializeCompressed())
|
|
|
|
if err := ctx.router.AddNode(n1); err != nil {
|
|
|
|
t.Fatalf("could not add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we use the same timestamp and query for staleness, we should get
|
|
|
|
// true.
|
|
|
|
if !ctx.router.IsStaleNode(pub1, updateTimeStamp) {
|
|
|
|
t.Fatalf("failure to detect stale node update")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we update the timestamp and once again query for staleness, it
|
|
|
|
// should report false.
|
|
|
|
newTimeStamp := time.Unix(1234, 0)
|
|
|
|
if ctx.router.IsStaleNode(pub1, newTimeStamp) {
|
|
|
|
t.Fatalf("incorrectly detected node as stale")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestIsKnownEdge tests that the IsKnownEdge method properly detects stale
|
|
|
|
// channel announcements.
|
|
|
|
func TestIsKnownEdge(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxSingleNode(t, startingBlockHeight)
|
2018-12-04 11:20:37 +01:00
|
|
|
defer cleanUp()
|
2018-02-25 04:34:03 +01:00
|
|
|
|
|
|
|
// First, we'll create a new channel edge (just the info) and insert it
|
|
|
|
// into the database.
|
|
|
|
var (
|
|
|
|
pub1 [33]byte
|
|
|
|
pub2 [33]byte
|
|
|
|
)
|
|
|
|
copy(pub1[:], priv1.PubKey().SerializeCompressed())
|
|
|
|
copy(pub2[:], priv2.PubKey().SerializeCompressed())
|
|
|
|
|
|
|
|
fundingTx, _, chanID, err := createChannelEdge(ctx,
|
|
|
|
bitcoinKey1.SerializeCompressed(),
|
|
|
|
bitcoinKey2.SerializeCompressed(),
|
|
|
|
10000, 500)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
fundingBlock := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{fundingTx},
|
|
|
|
}
|
|
|
|
ctx.chain.addBlock(fundingBlock, chanID.BlockHeight, chanID.BlockHeight)
|
|
|
|
|
|
|
|
edge := &channeldb.ChannelEdgeInfo{
|
|
|
|
ChannelID: chanID.ToUint64(),
|
|
|
|
NodeKey1Bytes: pub1,
|
|
|
|
NodeKey2Bytes: pub2,
|
|
|
|
BitcoinKey1Bytes: pub1,
|
|
|
|
BitcoinKey2Bytes: pub2,
|
|
|
|
AuthProof: nil,
|
|
|
|
}
|
|
|
|
if err := ctx.router.AddEdge(edge); err != nil {
|
|
|
|
t.Fatalf("unable to add edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the edge has been inserted, query is the router already
|
|
|
|
// knows of the edge should return true.
|
|
|
|
if !ctx.router.IsKnownEdge(*chanID) {
|
|
|
|
t.Fatalf("router should detect edge as known")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestIsStaleEdgePolicy tests that the IsStaleEdgePolicy properly detects
|
|
|
|
// stale channel edge update announcements.
|
|
|
|
func TestIsStaleEdgePolicy(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromFile(
|
|
|
|
t, startingBlockHeight, basicGraphFilePath,
|
|
|
|
)
|
2018-12-04 11:20:37 +01:00
|
|
|
defer cleanUp()
|
2018-02-25 04:34:03 +01:00
|
|
|
|
|
|
|
// First, we'll create a new channel edge (just the info) and insert it
|
|
|
|
// into the database.
|
|
|
|
var (
|
|
|
|
pub1 [33]byte
|
|
|
|
pub2 [33]byte
|
|
|
|
)
|
|
|
|
copy(pub1[:], priv1.PubKey().SerializeCompressed())
|
|
|
|
copy(pub2[:], priv2.PubKey().SerializeCompressed())
|
|
|
|
|
|
|
|
fundingTx, _, chanID, err := createChannelEdge(ctx,
|
|
|
|
bitcoinKey1.SerializeCompressed(),
|
|
|
|
bitcoinKey2.SerializeCompressed(),
|
|
|
|
10000, 500)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
fundingBlock := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{fundingTx},
|
|
|
|
}
|
|
|
|
ctx.chain.addBlock(fundingBlock, chanID.BlockHeight, chanID.BlockHeight)
|
|
|
|
|
|
|
|
// If we query for staleness before adding the edge, we should get
|
|
|
|
// false.
|
|
|
|
updateTimeStamp := time.Unix(123, 0)
|
|
|
|
if ctx.router.IsStaleEdgePolicy(*chanID, updateTimeStamp, 0) {
|
|
|
|
t.Fatalf("router failed to detect fresh edge policy")
|
|
|
|
}
|
|
|
|
if ctx.router.IsStaleEdgePolicy(*chanID, updateTimeStamp, 1) {
|
|
|
|
t.Fatalf("router failed to detect fresh edge policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
edge := &channeldb.ChannelEdgeInfo{
|
|
|
|
ChannelID: chanID.ToUint64(),
|
|
|
|
NodeKey1Bytes: pub1,
|
|
|
|
NodeKey2Bytes: pub2,
|
|
|
|
BitcoinKey1Bytes: pub1,
|
|
|
|
BitcoinKey2Bytes: pub2,
|
|
|
|
AuthProof: nil,
|
|
|
|
}
|
|
|
|
if err := ctx.router.AddEdge(edge); err != nil {
|
|
|
|
t.Fatalf("unable to add edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll also add two edge policies, one for each direction.
|
|
|
|
edgePolicy := &channeldb.ChannelEdgePolicy{
|
|
|
|
SigBytes: testSig.Serialize(),
|
|
|
|
ChannelID: edge.ChannelID,
|
|
|
|
LastUpdate: updateTimeStamp,
|
|
|
|
TimeLockDelta: 10,
|
|
|
|
MinHTLC: 1,
|
|
|
|
FeeBaseMSat: 10,
|
|
|
|
FeeProportionalMillionths: 10000,
|
|
|
|
}
|
2019-01-12 18:59:43 +01:00
|
|
|
edgePolicy.ChannelFlags = 0
|
2018-02-25 04:34:03 +01:00
|
|
|
if err := ctx.router.UpdateEdge(edgePolicy); err != nil {
|
|
|
|
t.Fatalf("unable to update edge policy: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edgePolicy = &channeldb.ChannelEdgePolicy{
|
|
|
|
SigBytes: testSig.Serialize(),
|
|
|
|
ChannelID: edge.ChannelID,
|
|
|
|
LastUpdate: updateTimeStamp,
|
|
|
|
TimeLockDelta: 10,
|
|
|
|
MinHTLC: 1,
|
|
|
|
FeeBaseMSat: 10,
|
|
|
|
FeeProportionalMillionths: 10000,
|
|
|
|
}
|
2019-01-12 18:59:43 +01:00
|
|
|
edgePolicy.ChannelFlags = 1
|
2018-02-25 04:34:03 +01:00
|
|
|
if err := ctx.router.UpdateEdge(edgePolicy); err != nil {
|
|
|
|
t.Fatalf("unable to update edge policy: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the edges have been added, an identical (chanID, flag,
|
|
|
|
// timestamp) tuple for each edge should be detected as a stale edge.
|
|
|
|
if !ctx.router.IsStaleEdgePolicy(*chanID, updateTimeStamp, 0) {
|
|
|
|
t.Fatalf("router failed to detect stale edge policy")
|
|
|
|
}
|
|
|
|
if !ctx.router.IsStaleEdgePolicy(*chanID, updateTimeStamp, 1) {
|
|
|
|
t.Fatalf("router failed to detect stale edge policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we now update the timestamp for both edges, the router should
|
|
|
|
// detect that this tuple represents a fresh edge.
|
|
|
|
updateTimeStamp = time.Unix(9999, 0)
|
|
|
|
if ctx.router.IsStaleEdgePolicy(*chanID, updateTimeStamp, 0) {
|
|
|
|
t.Fatalf("router failed to detect fresh edge policy")
|
|
|
|
}
|
|
|
|
if ctx.router.IsStaleEdgePolicy(*chanID, updateTimeStamp, 1) {
|
|
|
|
t.Fatalf("router failed to detect fresh edge policy")
|
|
|
|
}
|
|
|
|
}
|
2018-07-30 22:40:20 +02:00
|
|
|
|
|
|
|
// TestEmptyRoutesGenerateSphinxPacket tests that the generateSphinxPacket
|
|
|
|
// function is able to gracefully handle being passed a nil set of hops for the
|
|
|
|
// route by the caller.
|
|
|
|
func TestEmptyRoutesGenerateSphinxPacket(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-05-16 15:27:29 +02:00
|
|
|
sessionKey, _ := btcec.NewPrivateKey(btcec.S256())
|
2019-04-05 17:36:11 +02:00
|
|
|
emptyRoute := &route.Route{}
|
2019-05-16 15:27:29 +02:00
|
|
|
_, _, err := generateSphinxPacket(emptyRoute, testHash[:], sessionKey)
|
2019-04-05 17:36:11 +02:00
|
|
|
if err != route.ErrNoRouteHopsProvided {
|
2018-07-30 22:40:20 +02:00
|
|
|
t.Fatalf("expected empty hops error: instead got: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-04-17 22:22:33 +02:00
|
|
|
|
2019-06-19 11:12:10 +02:00
|
|
|
// TestUnknownErrorSource tests that if the source of an error is unknown, all
|
|
|
|
// edges along the route will be pruned.
|
|
|
|
func TestUnknownErrorSource(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Setup a network. It contains two paths to c: a->b->c and an
|
|
|
|
// alternative a->d->c.
|
|
|
|
chanCapSat := btcutil.Amount(100000)
|
|
|
|
testChannels := []*testChannel{
|
|
|
|
symmetricTestChannel("a", "b", chanCapSat, &testChannelPolicy{
|
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 400,
|
|
|
|
MinHTLC: 1,
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(chanCapSat),
|
|
|
|
}, 1),
|
|
|
|
symmetricTestChannel("b", "c", chanCapSat, &testChannelPolicy{
|
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 400,
|
|
|
|
MinHTLC: 1,
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(chanCapSat),
|
|
|
|
}, 3),
|
|
|
|
symmetricTestChannel("a", "d", chanCapSat, &testChannelPolicy{
|
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 400,
|
|
|
|
FeeBaseMsat: 100000,
|
|
|
|
MinHTLC: 1,
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(chanCapSat),
|
|
|
|
}, 2),
|
|
|
|
symmetricTestChannel("d", "c", chanCapSat, &testChannelPolicy{
|
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 400,
|
|
|
|
FeeBaseMsat: 100000,
|
|
|
|
MinHTLC: 1,
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(chanCapSat),
|
|
|
|
}, 4),
|
|
|
|
}
|
|
|
|
|
|
|
|
testGraph, err := createTestGraphFromChannels(testChannels, "a")
|
|
|
|
defer testGraph.cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create graph: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromGraphInstance(
|
|
|
|
t, startingBlockHeight, testGraph, false,
|
2021-04-02 23:57:50 +02:00
|
|
|
)
|
2019-06-19 11:12:10 +02:00
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
// Create a payment to node c.
|
2021-03-31 12:23:08 +02:00
|
|
|
var payHash lntypes.Hash
|
2019-06-19 11:12:10 +02:00
|
|
|
payment := LightningPayment{
|
|
|
|
Target: ctx.aliases["c"],
|
|
|
|
Amount: lnwire.NewMSatFromSatoshis(1000),
|
|
|
|
FeeLimit: noFeeLimit,
|
2021-03-31 12:23:08 +02:00
|
|
|
paymentHash: &payHash,
|
2019-06-19 11:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll modify the SendToSwitch method so that it simulates hop b as a
|
|
|
|
// node that returns an unparsable failure if approached via the a->b
|
|
|
|
// channel.
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
2019-06-19 11:12:10 +02:00
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
|
|
|
|
|
|
|
// If channel a->b is used, return an error without
|
|
|
|
// source and message. The sender won't know the origin
|
|
|
|
// of the error.
|
|
|
|
if firstHop.ToUint64() == 1 {
|
|
|
|
return [32]byte{},
|
|
|
|
htlcswitch.ErrUnreadableFailureMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise the payment succeeds.
|
|
|
|
return lntypes.Preimage{}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Send off the payment request to the router. The expectation is that
|
|
|
|
// the route a->b->c is tried first. An unreadable faiure is returned
|
|
|
|
// which should pruning the channel a->b. We expect the payment to
|
|
|
|
// succeed via a->d.
|
|
|
|
_, _, err = ctx.router.SendPayment(&payment)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected payment to succeed, but got: %v", err)
|
|
|
|
}
|
2019-06-19 15:09:23 +02:00
|
|
|
|
|
|
|
// Next we modify payment result to return an unknown failure.
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
2019-06-19 15:09:23 +02:00
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
|
|
|
|
|
|
|
// If channel a->b is used, simulate that the failure
|
|
|
|
// couldn't be decoded (FailureMessage is nil).
|
|
|
|
if firstHop.ToUint64() == 2 {
|
|
|
|
return [32]byte{},
|
2020-01-14 14:07:29 +01:00
|
|
|
htlcswitch.NewUnknownForwardingError(1)
|
2019-06-19 15:09:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise the payment succeeds.
|
|
|
|
return lntypes.Preimage{}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Send off the payment request to the router. We expect the payment to
|
|
|
|
// fail because both routes have been pruned.
|
2021-03-31 12:23:08 +02:00
|
|
|
payHash = lntypes.Hash{1}
|
|
|
|
payment.paymentHash = &payHash
|
2019-06-19 15:09:23 +02:00
|
|
|
_, _, err = ctx.router.SendPayment(&payment)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected payment to fail")
|
|
|
|
}
|
2019-06-19 11:12:10 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 22:22:33 +02:00
|
|
|
// assertChannelsPruned ensures that only the given channels are pruned from the
|
|
|
|
// graph out of the set of all channels.
|
|
|
|
func assertChannelsPruned(t *testing.T, graph *channeldb.ChannelGraph,
|
|
|
|
channels []*testChannel, prunedChanIDs ...uint64) {
|
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
pruned := make(map[uint64]struct{}, len(channels))
|
|
|
|
for _, chanID := range prunedChanIDs {
|
|
|
|
pruned[chanID] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, channel := range channels {
|
|
|
|
_, shouldPrune := pruned[channel.ChannelID]
|
|
|
|
_, _, exists, isZombie, err := graph.HasChannelEdge(
|
|
|
|
channel.ChannelID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to determine existence of "+
|
|
|
|
"channel=%v in the graph: %v",
|
|
|
|
channel.ChannelID, err)
|
|
|
|
}
|
|
|
|
if !shouldPrune && !exists {
|
|
|
|
t.Fatalf("expected channel=%v to exist within "+
|
|
|
|
"the graph", channel.ChannelID)
|
|
|
|
}
|
|
|
|
if shouldPrune && exists {
|
|
|
|
t.Fatalf("expected channel=%v to not exist "+
|
|
|
|
"within the graph", channel.ChannelID)
|
|
|
|
}
|
|
|
|
if !shouldPrune && isZombie {
|
|
|
|
t.Fatalf("expected channel=%v to not be marked "+
|
|
|
|
"as zombie", channel.ChannelID)
|
|
|
|
}
|
|
|
|
if shouldPrune && !isZombie {
|
|
|
|
t.Fatalf("expected channel=%v to be marked as "+
|
|
|
|
"zombie", channel.ChannelID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-23 20:05:29 +02:00
|
|
|
|
2019-05-23 21:17:16 +02:00
|
|
|
// TestSendToRouteStructuredError asserts that SendToRoute returns a structured
|
|
|
|
// error.
|
|
|
|
func TestSendToRouteStructuredError(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Setup a three node network.
|
|
|
|
chanCapSat := btcutil.Amount(100000)
|
|
|
|
testChannels := []*testChannel{
|
|
|
|
symmetricTestChannel("a", "b", chanCapSat, &testChannelPolicy{
|
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 400,
|
|
|
|
MinHTLC: 1,
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(chanCapSat),
|
|
|
|
}, 1),
|
|
|
|
symmetricTestChannel("b", "c", chanCapSat, &testChannelPolicy{
|
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 400,
|
|
|
|
MinHTLC: 1,
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(chanCapSat),
|
|
|
|
}, 2),
|
|
|
|
}
|
|
|
|
|
2019-01-31 16:07:30 +01:00
|
|
|
testGraph, err := createTestGraphFromChannels(testChannels, "a")
|
2019-05-23 21:17:16 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create graph: %v", err)
|
|
|
|
}
|
|
|
|
defer testGraph.cleanUp()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromGraphInstance(
|
|
|
|
t, startingBlockHeight, testGraph, false,
|
2019-05-23 21:17:16 +02:00
|
|
|
)
|
|
|
|
defer cleanUp()
|
|
|
|
|
2019-06-04 10:34:59 +02:00
|
|
|
// Set up an init channel for the control tower, such that we can make
|
|
|
|
// sure the payment is initiated correctly.
|
|
|
|
init := make(chan initArgs, 1)
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Control.(*mockControlTowerOld).init = init
|
2019-06-04 10:34:59 +02:00
|
|
|
|
2019-05-23 21:17:16 +02:00
|
|
|
// Setup a route from source a to destination c. The route will be used
|
|
|
|
// in a call to SendToRoute. SendToRoute also applies channel updates,
|
|
|
|
// but it saves us from including RequestRoute in the test scope too.
|
2019-06-04 10:34:59 +02:00
|
|
|
const payAmt = lnwire.MilliSatoshi(10000)
|
2019-05-23 21:17:16 +02:00
|
|
|
hop1 := ctx.aliases["b"]
|
|
|
|
hop2 := ctx.aliases["c"]
|
|
|
|
hops := []*route.Hop{
|
|
|
|
{
|
2019-07-31 06:41:58 +02:00
|
|
|
ChannelID: 1,
|
|
|
|
PubKeyBytes: hop1,
|
|
|
|
AmtToForward: payAmt,
|
|
|
|
LegacyPayload: true,
|
2019-05-23 21:17:16 +02:00
|
|
|
},
|
|
|
|
{
|
2019-07-31 06:41:58 +02:00
|
|
|
ChannelID: 2,
|
|
|
|
PubKeyBytes: hop2,
|
|
|
|
AmtToForward: payAmt,
|
|
|
|
LegacyPayload: true,
|
2019-05-23 21:17:16 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-04 10:34:59 +02:00
|
|
|
rt, err := route.NewRouteFromHops(payAmt, 100, ctx.aliases["a"], hops)
|
2019-05-23 21:17:16 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create route: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-07-07 03:35:20 +02:00
|
|
|
finalHopIndex := len(hops)
|
|
|
|
testCases := map[int]lnwire.FailureMessage{
|
|
|
|
finalHopIndex: lnwire.NewFailIncorrectDetails(payAmt, 100),
|
|
|
|
1: &lnwire.FailFeeInsufficient{
|
|
|
|
Update: lnwire.ChannelUpdate{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for failIndex, errorType := range testCases {
|
|
|
|
failIndex := failIndex
|
|
|
|
errorType := errorType
|
|
|
|
|
|
|
|
t.Run(fmt.Sprintf("%T", errorType), func(t *testing.T) {
|
|
|
|
// We'll modify the SendToSwitch method so that it
|
|
|
|
// simulates a failed payment with an error originating
|
|
|
|
// from the final hop in the route.
|
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
|
|
|
return [32]byte{}, htlcswitch.NewForwardingError(
|
|
|
|
errorType, failIndex,
|
|
|
|
)
|
|
|
|
},
|
2020-01-14 14:07:29 +01:00
|
|
|
)
|
2019-05-23 21:17:16 +02:00
|
|
|
|
2021-07-07 03:35:20 +02:00
|
|
|
// The payment parameter is mostly redundant in
|
|
|
|
// SendToRoute. Can be left empty for this test.
|
|
|
|
var payment lntypes.Hash
|
2019-05-23 21:17:16 +02:00
|
|
|
|
2021-07-07 03:35:20 +02:00
|
|
|
// Send off the payment request to the router. The
|
|
|
|
// specified route should be attempted and the channel
|
|
|
|
// update should be received by router and ignored
|
|
|
|
// because it is missing a valid
|
|
|
|
// signature.
|
|
|
|
_, err = ctx.router.SendToRoute(payment, rt)
|
2019-05-23 21:17:16 +02:00
|
|
|
|
2021-07-07 03:35:20 +02:00
|
|
|
fErr, ok := err.(*htlcswitch.ForwardingError)
|
|
|
|
require.True(
|
|
|
|
t, ok, "expected forwarding error, got: %T", err,
|
|
|
|
)
|
2019-05-23 21:17:16 +02:00
|
|
|
|
2021-07-07 03:35:20 +02:00
|
|
|
require.IsType(
|
|
|
|
t, errorType, fErr.WireMessage(),
|
|
|
|
"expected type %T got %T", errorType,
|
|
|
|
fErr.WireMessage(),
|
|
|
|
)
|
2019-06-04 10:34:59 +02:00
|
|
|
|
2021-07-07 03:35:20 +02:00
|
|
|
// Check that the correct values were used when
|
|
|
|
// initiating the payment.
|
|
|
|
select {
|
|
|
|
case initVal := <-init:
|
|
|
|
if initVal.c.Value != payAmt {
|
|
|
|
t.Fatalf("expected %v, got %v", payAmt,
|
|
|
|
initVal.c.Value)
|
|
|
|
}
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Fatalf("initPayment not called")
|
|
|
|
}
|
|
|
|
})
|
2019-06-04 10:34:59 +02:00
|
|
|
}
|
2019-05-23 21:17:16 +02:00
|
|
|
}
|
2019-08-29 13:03:37 +02:00
|
|
|
|
2020-04-01 00:13:27 +02:00
|
|
|
// TestSendToRouteMultiShardSend checks that a 3-shard payment can be executed
|
|
|
|
// using SendToRoute.
|
|
|
|
func TestSendToRouteMultiShardSend(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanup := createTestCtxSingleNode(t, 0)
|
2020-04-01 00:13:27 +02:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
const numShards = 3
|
|
|
|
const payAmt = lnwire.MilliSatoshi(numShards * 10000)
|
|
|
|
node, err := createTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a simple 1-hop route that we will use for all three shards.
|
|
|
|
hops := []*route.Hop{
|
|
|
|
{
|
|
|
|
ChannelID: 1,
|
|
|
|
PubKeyBytes: node.PubKeyBytes,
|
|
|
|
AmtToForward: payAmt / numShards,
|
|
|
|
MPP: record.NewMPP(payAmt, [32]byte{}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceNode, err := ctx.graph.SourceNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rt, err := route.NewRouteFromHops(
|
|
|
|
payAmt, 100, sourceNode.PubKeyBytes, hops,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create route: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first shard we send we'll fail immediately, to check that we are
|
|
|
|
// still allowed to retry with other shards after a failed one.
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
2020-04-01 00:13:27 +02:00
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
|
|
|
return [32]byte{}, htlcswitch.NewForwardingError(
|
|
|
|
&lnwire.FailFeeInsufficient{
|
|
|
|
Update: lnwire.ChannelUpdate{},
|
|
|
|
}, 1,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// The payment parameter is mostly redundant in SendToRoute. Can be left
|
|
|
|
// empty for this test.
|
|
|
|
var payment lntypes.Hash
|
|
|
|
|
|
|
|
// Send the shard using the created route, and expect an error to be
|
|
|
|
// returned.
|
|
|
|
_, err = ctx.router.SendToRoute(payment, rt)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected forwarding error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we'll modify the SendToSwitch method again to wait until all
|
|
|
|
// three shards are initiated before returning a result. We do this by
|
|
|
|
// signalling when the method has been called, and then stop to wait
|
|
|
|
// for the test to deliver the final result on the channel below.
|
|
|
|
waitForResultSignal := make(chan struct{}, numShards)
|
|
|
|
results := make(chan lntypes.Preimage, numShards)
|
|
|
|
|
2021-05-24 12:29:27 +02:00
|
|
|
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
|
2020-04-01 00:13:27 +02:00
|
|
|
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
|
|
|
|
|
|
|
|
// Signal that the shard has been initiated and is
|
|
|
|
// waiting for a result.
|
|
|
|
waitForResultSignal <- struct{}{}
|
|
|
|
|
|
|
|
// Wait for a result before returning it.
|
|
|
|
res, ok := <-results
|
|
|
|
if !ok {
|
|
|
|
return [32]byte{}, fmt.Errorf("failure")
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Launch three shards by calling SendToRoute in three goroutines,
|
|
|
|
// returning their final error on the channel.
|
|
|
|
errChan := make(chan error)
|
|
|
|
successes := make(chan lntypes.Preimage)
|
|
|
|
|
|
|
|
for i := 0; i < numShards; i++ {
|
|
|
|
go func() {
|
2020-05-06 15:36:51 +02:00
|
|
|
attempt, err := ctx.router.SendToRoute(payment, rt)
|
2020-04-01 00:13:27 +02:00
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-06 15:36:51 +02:00
|
|
|
successes <- attempt.Settle.Preimage
|
2020-04-01 00:13:27 +02:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all shards to signal they have been initiated.
|
|
|
|
for i := 0; i < numShards; i++ {
|
|
|
|
select {
|
|
|
|
case <-waitForResultSignal:
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("not waiting for results")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deliver a dummy preimage to all the shard handlers.
|
|
|
|
preimage := lntypes.Preimage{}
|
|
|
|
preimage[4] = 42
|
|
|
|
for i := 0; i < numShards; i++ {
|
|
|
|
results <- preimage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally expect all shards to return with the above preimage.
|
|
|
|
for i := 0; i < numShards; i++ {
|
|
|
|
select {
|
|
|
|
case p := <-successes:
|
|
|
|
if p != preimage {
|
|
|
|
t.Fatalf("preimage mismatch")
|
|
|
|
}
|
|
|
|
case err := <-errChan:
|
|
|
|
t.Fatalf("unexpected error from SendToRoute: %v", err)
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("result not received")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 08:56:41 +01:00
|
|
|
// TestSendToRouteMaxHops asserts that SendToRoute fails when using a route that
|
|
|
|
// exceeds the maximum number of hops.
|
|
|
|
func TestSendToRouteMaxHops(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Setup a two node network.
|
|
|
|
chanCapSat := btcutil.Amount(100000)
|
|
|
|
testChannels := []*testChannel{
|
|
|
|
symmetricTestChannel("a", "b", chanCapSat, &testChannelPolicy{
|
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 400,
|
|
|
|
MinHTLC: 1,
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(chanCapSat),
|
|
|
|
}, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
testGraph, err := createTestGraphFromChannels(testChannels, "a")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create graph: %v", err)
|
|
|
|
}
|
|
|
|
defer testGraph.cleanUp()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromGraphInstance(
|
|
|
|
t, startingBlockHeight, testGraph, false,
|
2019-12-11 08:56:41 +01:00
|
|
|
)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
// Create a 30 hop route that exceeds the maximum hop limit.
|
|
|
|
const payAmt = lnwire.MilliSatoshi(10000)
|
|
|
|
hopA := ctx.aliases["a"]
|
|
|
|
hopB := ctx.aliases["b"]
|
|
|
|
|
|
|
|
var hops []*route.Hop
|
|
|
|
for i := 0; i < 15; i++ {
|
|
|
|
hops = append(hops, &route.Hop{
|
|
|
|
ChannelID: 1,
|
|
|
|
PubKeyBytes: hopB,
|
|
|
|
AmtToForward: payAmt,
|
|
|
|
LegacyPayload: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
hops = append(hops, &route.Hop{
|
|
|
|
ChannelID: 1,
|
|
|
|
PubKeyBytes: hopA,
|
|
|
|
AmtToForward: payAmt,
|
|
|
|
LegacyPayload: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
rt, err := route.NewRouteFromHops(payAmt, 100, ctx.aliases["a"], hops)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create route: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send off the payment request to the router. We expect an error back
|
|
|
|
// indicating that the route is too long.
|
|
|
|
var payment lntypes.Hash
|
|
|
|
_, err = ctx.router.SendToRoute(payment, rt)
|
|
|
|
if err != route.ErrMaxRouteHopsExceeded {
|
|
|
|
t.Fatalf("expected ErrMaxRouteHopsExceeded, but got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 13:03:37 +02:00
|
|
|
// TestBuildRoute tests whether correct routes are built.
|
|
|
|
func TestBuildRoute(t *testing.T) {
|
|
|
|
// Setup a three node network.
|
|
|
|
chanCapSat := btcutil.Amount(100000)
|
2020-11-24 05:17:16 +01:00
|
|
|
paymentAddrFeatures := lnwire.NewFeatureVector(
|
|
|
|
lnwire.NewRawFeatureVector(lnwire.PaymentAddrOptional),
|
|
|
|
lnwire.Features,
|
|
|
|
)
|
2019-08-29 13:03:37 +02:00
|
|
|
testChannels := []*testChannel{
|
|
|
|
// Create two local channels from a. The bandwidth is estimated
|
|
|
|
// in this test as the channel capacity. For building routes, we
|
|
|
|
// expected the channel with the largest estimated bandwidth to
|
|
|
|
// be selected.
|
|
|
|
symmetricTestChannel("a", "b", chanCapSat, &testChannelPolicy{
|
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 20000,
|
|
|
|
MinHTLC: lnwire.NewMSatFromSatoshis(5),
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(chanCapSat),
|
|
|
|
}, 1),
|
|
|
|
symmetricTestChannel("a", "b", chanCapSat/2, &testChannelPolicy{
|
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 20000,
|
|
|
|
MinHTLC: lnwire.NewMSatFromSatoshis(5),
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(chanCapSat / 2),
|
|
|
|
}, 6),
|
|
|
|
|
|
|
|
// Create two channels from b to c. For building routes, we
|
|
|
|
// expect the lowest cost channel to be selected. Note that this
|
|
|
|
// isn't a situation that we are expecting in reality. Routing
|
|
|
|
// nodes are recommended to keep their channel policies towards
|
|
|
|
// the same peer identical.
|
|
|
|
symmetricTestChannel("b", "c", chanCapSat, &testChannelPolicy{
|
2020-11-24 05:17:16 +01:00
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 50000,
|
|
|
|
MinHTLC: lnwire.NewMSatFromSatoshis(20),
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(120),
|
|
|
|
Features: paymentAddrFeatures,
|
2019-08-29 13:03:37 +02:00
|
|
|
}, 2),
|
|
|
|
symmetricTestChannel("b", "c", chanCapSat, &testChannelPolicy{
|
2020-11-24 05:17:16 +01:00
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 60000,
|
|
|
|
MinHTLC: lnwire.NewMSatFromSatoshis(20),
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(120),
|
|
|
|
Features: paymentAddrFeatures,
|
2019-08-29 13:03:37 +02:00
|
|
|
}, 7),
|
|
|
|
|
|
|
|
symmetricTestChannel("a", "e", chanCapSat, &testChannelPolicy{
|
2020-11-24 05:17:16 +01:00
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 80000,
|
|
|
|
MinHTLC: lnwire.NewMSatFromSatoshis(5),
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(10),
|
|
|
|
Features: paymentAddrFeatures,
|
2019-08-29 13:03:37 +02:00
|
|
|
}, 5),
|
|
|
|
symmetricTestChannel("e", "c", chanCapSat, &testChannelPolicy{
|
2020-11-24 05:17:16 +01:00
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 100000,
|
|
|
|
MinHTLC: lnwire.NewMSatFromSatoshis(20),
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(chanCapSat),
|
|
|
|
Features: paymentAddrFeatures,
|
2019-08-29 13:03:37 +02:00
|
|
|
}, 4),
|
|
|
|
}
|
|
|
|
|
|
|
|
testGraph, err := createTestGraphFromChannels(testChannels, "a")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create graph: %v", err)
|
|
|
|
}
|
|
|
|
defer testGraph.cleanUp()
|
|
|
|
|
|
|
|
const startingBlockHeight = 101
|
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanUp := createTestCtxFromGraphInstance(
|
|
|
|
t, startingBlockHeight, testGraph, false,
|
2019-08-29 13:03:37 +02:00
|
|
|
)
|
|
|
|
defer cleanUp()
|
|
|
|
|
2020-11-24 05:17:16 +01:00
|
|
|
checkHops := func(rt *route.Route, expected []uint64,
|
|
|
|
payAddr [32]byte) {
|
|
|
|
|
2019-10-03 12:52:38 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2019-08-29 13:03:37 +02:00
|
|
|
if len(rt.Hops) != len(expected) {
|
|
|
|
t.Fatal("hop count mismatch")
|
|
|
|
}
|
|
|
|
for i, hop := range rt.Hops {
|
|
|
|
if hop.ChannelID != expected[i] {
|
|
|
|
t.Fatalf("expected channel %v at pos %v, but "+
|
|
|
|
"got channel %v",
|
|
|
|
expected[i], i, hop.ChannelID)
|
|
|
|
}
|
|
|
|
}
|
2020-11-24 05:17:16 +01:00
|
|
|
|
|
|
|
lastHop := rt.Hops[len(rt.Hops)-1]
|
|
|
|
require.NotNil(t, lastHop.MPP)
|
|
|
|
require.Equal(t, lastHop.MPP.PaymentAddr(), payAddr)
|
2019-08-29 13:03:37 +02:00
|
|
|
}
|
|
|
|
|
2020-11-24 05:17:16 +01:00
|
|
|
var payAddr [32]byte
|
|
|
|
_, err = rand.Read(payAddr[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-08-29 13:03:37 +02:00
|
|
|
// Create hop list from the route node pubkeys.
|
|
|
|
hops := []route.Vertex{
|
|
|
|
ctx.aliases["b"], ctx.aliases["c"],
|
|
|
|
}
|
|
|
|
amt := lnwire.NewMSatFromSatoshis(100)
|
|
|
|
|
|
|
|
// Build the route for the given amount.
|
|
|
|
rt, err := ctx.router.BuildRoute(
|
2020-11-24 05:17:16 +01:00
|
|
|
&amt, hops, nil, 40, &payAddr,
|
2019-08-29 13:03:37 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we get the expected route back. The total amount should be
|
2019-09-30 15:45:16 +02:00
|
|
|
// the amount to deliver to hop c (100 sats) plus the max fee for the
|
|
|
|
// connection b->c (6 sats).
|
2020-11-24 05:17:16 +01:00
|
|
|
checkHops(rt, []uint64{1, 7}, payAddr)
|
2019-09-30 15:45:16 +02:00
|
|
|
if rt.TotalAmount != 106000 {
|
2019-08-29 13:03:37 +02:00
|
|
|
t.Fatalf("unexpected total amount %v", rt.TotalAmount)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the route for the minimum amount.
|
|
|
|
rt, err = ctx.router.BuildRoute(
|
2020-11-24 05:17:16 +01:00
|
|
|
nil, hops, nil, 40, &payAddr,
|
2019-08-29 13:03:37 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we get the expected route back. The minimum that we can
|
2019-09-30 15:45:16 +02:00
|
|
|
// send from b to c is 20 sats. Hop b charges 1200 msat for the
|
|
|
|
// forwarding. The channel between hop a and b can carry amounts in the
|
|
|
|
// range [5, 100], so 21200 msats is the minimum amount for this route.
|
2020-11-24 05:17:16 +01:00
|
|
|
checkHops(rt, []uint64{1, 7}, payAddr)
|
2019-09-30 15:45:16 +02:00
|
|
|
if rt.TotalAmount != 21200 {
|
2019-08-29 13:03:37 +02:00
|
|
|
t.Fatalf("unexpected total amount %v", rt.TotalAmount)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test a route that contains incompatible channel htlc constraints.
|
|
|
|
// There is no amount that can pass through both channel 5 and 4.
|
|
|
|
hops = []route.Vertex{
|
|
|
|
ctx.aliases["e"], ctx.aliases["c"],
|
|
|
|
}
|
|
|
|
_, err = ctx.router.BuildRoute(
|
2020-11-24 05:17:16 +01:00
|
|
|
nil, hops, nil, 40, nil,
|
2019-08-29 13:03:37 +02:00
|
|
|
)
|
|
|
|
errNoChannel, ok := err.(ErrNoChannel)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected incompatible policies error, but got %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
if errNoChannel.position != 0 {
|
|
|
|
t.Fatalf("unexpected no channel error position")
|
|
|
|
}
|
|
|
|
if errNoChannel.fromNode != ctx.aliases["a"] {
|
|
|
|
t.Fatalf("unexpected no channel error node")
|
|
|
|
}
|
|
|
|
}
|
2021-04-21 01:24:34 +02:00
|
|
|
|
|
|
|
// edgeCreationModifier is an enum-like type used to modify steps that are
|
|
|
|
// skipped when creating a channel in the test context.
|
|
|
|
type edgeCreationModifier uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// edgeCreationNoFundingTx is used to skip adding the funding
|
|
|
|
// transaction of an edge to the chain.
|
|
|
|
edgeCreationNoFundingTx edgeCreationModifier = iota
|
|
|
|
|
|
|
|
// edgeCreationNoUTXO is used to skip adding the UTXO of a channel to
|
|
|
|
// the UTXO set.
|
|
|
|
edgeCreationNoUTXO
|
2021-04-23 01:41:15 +02:00
|
|
|
|
|
|
|
// edgeCreationBadScript is used to create the edge, but use the wrong
|
|
|
|
// scrip which should cause it to fail output validation.
|
|
|
|
edgeCreationBadScript
|
2021-04-21 01:24:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// newChannelEdgeInfo is a helper function used to create a new channel edge,
|
|
|
|
// possibly skipping adding it to parts of the chain/state as well.
|
|
|
|
func newChannelEdgeInfo(ctx *testCtx, fundingHeight uint32,
|
|
|
|
ecm edgeCreationModifier) (*channeldb.ChannelEdgeInfo, error) {
|
|
|
|
|
|
|
|
node1, err := createTestNode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node2, err := createTestNode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fundingTx, _, chanID, err := createChannelEdge(
|
|
|
|
ctx, bitcoinKey1.SerializeCompressed(),
|
|
|
|
bitcoinKey2.SerializeCompressed(), 100, fundingHeight,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to create edge: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edge := &channeldb.ChannelEdgeInfo{
|
|
|
|
ChannelID: chanID.ToUint64(),
|
|
|
|
NodeKey1Bytes: node1.PubKeyBytes,
|
|
|
|
NodeKey2Bytes: node2.PubKeyBytes,
|
|
|
|
}
|
|
|
|
copy(edge.BitcoinKey1Bytes[:], bitcoinKey1.SerializeCompressed())
|
|
|
|
copy(edge.BitcoinKey2Bytes[:], bitcoinKey2.SerializeCompressed())
|
|
|
|
|
|
|
|
if ecm == edgeCreationNoFundingTx {
|
|
|
|
return edge, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fundingBlock := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{fundingTx},
|
|
|
|
}
|
|
|
|
ctx.chain.addBlock(fundingBlock, chanID.BlockHeight, chanID.BlockHeight)
|
|
|
|
|
|
|
|
if ecm == edgeCreationNoUTXO {
|
|
|
|
ctx.chain.delUtxo(wire.OutPoint{
|
|
|
|
Hash: fundingTx.TxHash(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-23 01:41:15 +02:00
|
|
|
if ecm == edgeCreationBadScript {
|
|
|
|
fundingTx.TxOut[0].PkScript[0] ^= 1
|
|
|
|
}
|
|
|
|
|
2021-04-21 01:24:34 +02:00
|
|
|
return edge, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertChanChainRejection(t *testing.T, ctx *testCtx,
|
|
|
|
edge *channeldb.ChannelEdgeInfo, failCode errorCode) {
|
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
err := ctx.router.AddEdge(edge)
|
|
|
|
if !IsError(err, failCode) {
|
|
|
|
t.Fatalf("validation should have failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This channel should now be present in the zombie channel index.
|
|
|
|
_, _, _, isZombie, err := ctx.graph.HasChannelEdge(
|
|
|
|
edge.ChannelID,
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.True(t, isZombie, "edge should be marked as zombie")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestChannelOnChainRejectionZombie tests that if we fail validating a channel
|
|
|
|
// due to some sort of on-chain rejection (no funding transaction, or invalid
|
|
|
|
// UTXO), then we'll mark the channel as a zombie.
|
|
|
|
func TestChannelOnChainRejectionZombie(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2021-04-14 06:58:12 +02:00
|
|
|
ctx, cleanup := createTestCtxSingleNode(t, 0)
|
2021-04-21 01:24:34 +02:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// To start, we'll make an edge for the channel, but we won't add the
|
|
|
|
// funding transaction to the mock blockchain, which should cause the
|
|
|
|
// validation to fail below.
|
|
|
|
edge, err := newChannelEdgeInfo(ctx, 1, edgeCreationNoFundingTx)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// We expect this to fail as the transaction isn't present in the
|
|
|
|
// chain (nor the block).
|
|
|
|
assertChanChainRejection(t, ctx, edge, ErrNoFundingTransaction)
|
|
|
|
|
|
|
|
// Next, we'll make another channel edge, but actually add it to the
|
|
|
|
// graph this time.
|
|
|
|
edge, err = newChannelEdgeInfo(ctx, 2, edgeCreationNoUTXO)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// Instead now, we'll remove it from the set of UTXOs which should
|
|
|
|
// cause the spentness validation to fail.
|
|
|
|
assertChanChainRejection(t, ctx, edge, ErrChannelSpent)
|
2021-04-23 01:41:15 +02:00
|
|
|
|
|
|
|
// If we cause the funding transaction the chain to fail validation, we
|
|
|
|
// should see similar behavior.
|
|
|
|
edge, err = newChannelEdgeInfo(ctx, 3, edgeCreationBadScript)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assertChanChainRejection(t, ctx, edge, ErrInvalidFundingOutput)
|
2021-04-21 01:24:34 +02:00
|
|
|
}
|
2021-05-21 13:02:49 +02:00
|
|
|
|
|
|
|
func createDummyTestGraph(t *testing.T) *testGraphInstance {
|
|
|
|
// Setup two simple channels such that we can mock sending along this
|
|
|
|
// route.
|
|
|
|
chanCapSat := btcutil.Amount(100000)
|
|
|
|
testChannels := []*testChannel{
|
|
|
|
symmetricTestChannel("a", "b", chanCapSat, &testChannelPolicy{
|
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 400,
|
|
|
|
MinHTLC: 1,
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(chanCapSat),
|
|
|
|
}, 1),
|
|
|
|
symmetricTestChannel("b", "c", chanCapSat, &testChannelPolicy{
|
|
|
|
Expiry: 144,
|
|
|
|
FeeRate: 400,
|
|
|
|
MinHTLC: 1,
|
|
|
|
MaxHTLC: lnwire.NewMSatFromSatoshis(chanCapSat),
|
|
|
|
}, 2),
|
|
|
|
}
|
|
|
|
|
|
|
|
testGraph, err := createTestGraphFromChannels(testChannels, "a")
|
|
|
|
require.NoError(t, err, "failed to create graph")
|
|
|
|
return testGraph
|
|
|
|
}
|
|
|
|
|
|
|
|
func createDummyLightningPayment(t *testing.T,
|
|
|
|
target route.Vertex, amt lnwire.MilliSatoshi) *LightningPayment {
|
|
|
|
|
|
|
|
var preImage lntypes.Preimage
|
|
|
|
_, err := rand.Read(preImage[:])
|
|
|
|
require.NoError(t, err, "unable to generate preimage")
|
|
|
|
|
|
|
|
payHash := preImage.Hash()
|
|
|
|
|
|
|
|
return &LightningPayment{
|
|
|
|
Target: target,
|
|
|
|
Amount: amt,
|
|
|
|
FeeLimit: noFeeLimit,
|
|
|
|
paymentHash: &payHash,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSendMPPaymentSucceed tests that we can successfully send a MPPayment via
|
|
|
|
// router.SendPayment. This test mainly focuses on testing the logic of the
|
|
|
|
// method resumePayment is implemented as expected.
|
|
|
|
func TestSendMPPaymentSucceed(t *testing.T) {
|
|
|
|
const startingBlockHeight = 101
|
|
|
|
|
|
|
|
// Create mockers to initialize the router.
|
|
|
|
controlTower := &mockControlTower{}
|
|
|
|
sessionSource := &mockPaymentSessionSource{}
|
|
|
|
missionControl := &mockMissionControl{}
|
|
|
|
payer := &mockPaymentAttemptDispatcher{}
|
|
|
|
chain := newMockChain(startingBlockHeight)
|
|
|
|
chainView := newMockChainView(chain)
|
|
|
|
testGraph := createDummyTestGraph(t)
|
|
|
|
|
|
|
|
// Define the behavior of the mockers to the point where we can
|
|
|
|
// successfully start the router.
|
|
|
|
controlTower.On("FetchInFlightPayments").Return(
|
|
|
|
[]*channeldb.MPPayment{}, nil,
|
|
|
|
)
|
|
|
|
payer.On("CleanStore", mock.Anything).Return(nil)
|
|
|
|
|
|
|
|
// Create and start the router.
|
|
|
|
router, err := New(Config{
|
|
|
|
Control: controlTower,
|
|
|
|
SessionSource: sessionSource,
|
|
|
|
MissionControl: missionControl,
|
|
|
|
Payer: payer,
|
|
|
|
|
|
|
|
// TODO(yy): create new mocks for the chain and chainview.
|
|
|
|
Chain: chain,
|
|
|
|
ChainView: chainView,
|
|
|
|
|
|
|
|
// TODO(yy): mock the graph once it's changed into interface.
|
|
|
|
Graph: testGraph.graph,
|
|
|
|
|
|
|
|
Clock: clock.NewTestClock(time.Unix(1, 0)),
|
|
|
|
GraphPruneInterval: time.Hour * 2,
|
|
|
|
NextPaymentID: func() (uint64, error) {
|
|
|
|
next := atomic.AddUint64(&uniquePaymentID, 1)
|
|
|
|
return next, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err, "failed to create router")
|
|
|
|
|
|
|
|
// Make sure the router can start and stop without error.
|
|
|
|
require.NoError(t, router.Start(), "router failed to start")
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, router.Stop(), "router failed to stop")
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Once the router is started, check that the mocked methods are called
|
|
|
|
// as expected.
|
|
|
|
controlTower.AssertExpectations(t)
|
|
|
|
payer.AssertExpectations(t)
|
|
|
|
|
|
|
|
// Mock the methods to the point where we are inside the function
|
|
|
|
// resumePayment.
|
|
|
|
paymentAmt := lnwire.MilliSatoshi(10000)
|
|
|
|
req := createDummyLightningPayment(
|
|
|
|
t, testGraph.aliasMap["c"], paymentAmt,
|
|
|
|
)
|
|
|
|
identifier := lntypes.Hash(req.Identifier())
|
|
|
|
session := &mockPaymentSession{}
|
|
|
|
sessionSource.On("NewPaymentSession", req).Return(session, nil)
|
|
|
|
controlTower.On("InitPayment", identifier, mock.Anything).Return(nil)
|
|
|
|
|
|
|
|
// The following mocked methods are called inside resumePayment. Note
|
|
|
|
// that the payment object below will determine the state of the
|
|
|
|
// paymentLifecycle.
|
|
|
|
payment := &channeldb.MPPayment{}
|
|
|
|
controlTower.On("FetchPayment", identifier).Return(payment, nil)
|
|
|
|
|
|
|
|
// Create a route that can send 1/4 of the total amount. This value
|
|
|
|
// will be returned by calling RequestRoute.
|
|
|
|
shard, err := createTestRoute(paymentAmt/4, testGraph.aliasMap)
|
|
|
|
require.NoError(t, err, "failed to create route")
|
|
|
|
session.On("RequestRoute",
|
|
|
|
mock.Anything, mock.Anything, mock.Anything, mock.Anything,
|
|
|
|
).Return(shard, nil)
|
|
|
|
|
|
|
|
// Make a new htlc attempt with zero fee and append it to the payment's
|
|
|
|
// HTLCs when calling RegisterAttempt.
|
|
|
|
activeAttempt := makeActiveAttempt(int(paymentAmt/4), 0)
|
|
|
|
controlTower.On("RegisterAttempt",
|
|
|
|
identifier, mock.Anything,
|
|
|
|
).Return(nil).Run(func(args mock.Arguments) {
|
|
|
|
payment.HTLCs = append(payment.HTLCs, activeAttempt)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Create a buffered chan and it will be returned by GetPaymentResult.
|
|
|
|
payer.resultChan = make(chan *htlcswitch.PaymentResult, 10)
|
|
|
|
payer.On("GetPaymentResult",
|
|
|
|
mock.Anything, identifier, mock.Anything,
|
|
|
|
).Run(func(args mock.Arguments) {
|
|
|
|
// Before the mock method is returned, we send the result to
|
|
|
|
// the read-only chan.
|
|
|
|
payer.resultChan <- &htlcswitch.PaymentResult{}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Simple mocking the rest.
|
|
|
|
payer.On("SendHTLC",
|
|
|
|
mock.Anything, mock.Anything, mock.Anything,
|
|
|
|
).Return(nil)
|
|
|
|
missionControl.On("ReportPaymentSuccess",
|
|
|
|
mock.Anything, mock.Anything,
|
|
|
|
).Return(nil)
|
|
|
|
|
|
|
|
// Mock SettleAttempt by changing one of the HTLCs to be settled.
|
|
|
|
preimage := lntypes.Preimage{1, 2, 3}
|
|
|
|
settledAttempt := makeSettledAttempt(
|
|
|
|
int(paymentAmt/4), 0, preimage,
|
|
|
|
)
|
|
|
|
controlTower.On("SettleAttempt",
|
|
|
|
identifier, mock.Anything, mock.Anything,
|
|
|
|
).Return(&settledAttempt, nil).Run(func(args mock.Arguments) {
|
|
|
|
// Whenever this method is invoked, we will mark the first
|
|
|
|
// active attempt settled and exit.
|
|
|
|
for i, attempt := range payment.HTLCs {
|
|
|
|
if attempt.Settle == nil {
|
|
|
|
attempt.Settle = &channeldb.HTLCSettleInfo{
|
|
|
|
Preimage: preimage,
|
|
|
|
}
|
|
|
|
payment.HTLCs[i] = attempt
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Call the actual method SendPayment on router. This is place inside a
|
|
|
|
// goroutine so we can set a timeout for the whole test, in case
|
|
|
|
// anything goes wrong and the test never finishes.
|
|
|
|
done := make(chan struct{})
|
|
|
|
var p lntypes.Hash
|
|
|
|
go func() {
|
|
|
|
p, _, err = router.SendPayment(req)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatalf("SendPayment didn't exit")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, validate the returned values and check that the mock
|
|
|
|
// methods are called as expected.
|
|
|
|
require.NoError(t, err, "send payment failed")
|
|
|
|
require.EqualValues(t, preimage, p, "preimage not match")
|
|
|
|
|
|
|
|
// Note that we also implicitly check the methods such as FailAttempt,
|
|
|
|
// ReportPaymentFail, etc, are not called because we never mocked them
|
|
|
|
// in this test. If any of the unexpected methods was called, the test
|
|
|
|
// would fail.
|
|
|
|
controlTower.AssertExpectations(t)
|
|
|
|
payer.AssertExpectations(t)
|
|
|
|
sessionSource.AssertExpectations(t)
|
|
|
|
session.AssertExpectations(t)
|
|
|
|
missionControl.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSendMPPaymentSucceedOnExtraShards tests that we need extra attempts if
|
|
|
|
// there are failed ones,so that a payment is successfully sent. This test
|
|
|
|
// mainly focuses on testing the logic of the method resumePayment is
|
|
|
|
// implemented as expected.
|
|
|
|
func TestSendMPPaymentSucceedOnExtraShards(t *testing.T) {
|
|
|
|
const startingBlockHeight = 101
|
|
|
|
|
|
|
|
// Create mockers to initialize the router.
|
|
|
|
controlTower := &mockControlTower{}
|
|
|
|
sessionSource := &mockPaymentSessionSource{}
|
|
|
|
missionControl := &mockMissionControl{}
|
|
|
|
payer := &mockPaymentAttemptDispatcher{}
|
|
|
|
chain := newMockChain(startingBlockHeight)
|
|
|
|
chainView := newMockChainView(chain)
|
|
|
|
testGraph := createDummyTestGraph(t)
|
|
|
|
|
|
|
|
// Define the behavior of the mockers to the point where we can
|
|
|
|
// successfully start the router.
|
|
|
|
controlTower.On("FetchInFlightPayments").Return(
|
|
|
|
[]*channeldb.MPPayment{}, nil,
|
|
|
|
)
|
|
|
|
payer.On("CleanStore", mock.Anything).Return(nil)
|
|
|
|
|
|
|
|
// Create and start the router.
|
|
|
|
router, err := New(Config{
|
|
|
|
Control: controlTower,
|
|
|
|
SessionSource: sessionSource,
|
|
|
|
MissionControl: missionControl,
|
|
|
|
Payer: payer,
|
|
|
|
|
|
|
|
// TODO(yy): create new mocks for the chain and chainview.
|
|
|
|
Chain: chain,
|
|
|
|
ChainView: chainView,
|
|
|
|
|
|
|
|
// TODO(yy): mock the graph once it's changed into interface.
|
|
|
|
Graph: testGraph.graph,
|
|
|
|
|
|
|
|
Clock: clock.NewTestClock(time.Unix(1, 0)),
|
|
|
|
GraphPruneInterval: time.Hour * 2,
|
|
|
|
NextPaymentID: func() (uint64, error) {
|
|
|
|
next := atomic.AddUint64(&uniquePaymentID, 1)
|
|
|
|
return next, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err, "failed to create router")
|
|
|
|
|
|
|
|
// Make sure the router can start and stop without error.
|
|
|
|
require.NoError(t, router.Start(), "router failed to start")
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, router.Stop(), "router failed to stop")
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Once the router is started, check that the mocked methods are called
|
|
|
|
// as expected.
|
|
|
|
controlTower.AssertExpectations(t)
|
|
|
|
payer.AssertExpectations(t)
|
|
|
|
|
|
|
|
// Mock the methods to the point where we are inside the function
|
|
|
|
// resumePayment.
|
|
|
|
paymentAmt := lnwire.MilliSatoshi(20000)
|
|
|
|
req := createDummyLightningPayment(
|
|
|
|
t, testGraph.aliasMap["c"], paymentAmt,
|
|
|
|
)
|
|
|
|
identifier := lntypes.Hash(req.Identifier())
|
|
|
|
session := &mockPaymentSession{}
|
|
|
|
sessionSource.On("NewPaymentSession", req).Return(session, nil)
|
|
|
|
controlTower.On("InitPayment", identifier, mock.Anything).Return(nil)
|
|
|
|
|
|
|
|
// The following mocked methods are called inside resumePayment. Note
|
|
|
|
// that the payment object below will determine the state of the
|
|
|
|
// paymentLifecycle.
|
|
|
|
payment := &channeldb.MPPayment{}
|
|
|
|
controlTower.On("FetchPayment", identifier).Return(payment, nil)
|
|
|
|
|
|
|
|
// Create a route that can send 1/4 of the total amount. This value
|
|
|
|
// will be returned by calling RequestRoute.
|
|
|
|
shard, err := createTestRoute(paymentAmt/4, testGraph.aliasMap)
|
|
|
|
require.NoError(t, err, "failed to create route")
|
|
|
|
session.On("RequestRoute",
|
|
|
|
mock.Anything, mock.Anything, mock.Anything, mock.Anything,
|
|
|
|
).Return(shard, nil)
|
|
|
|
|
|
|
|
// Make a new htlc attempt with zero fee and append it to the payment's
|
|
|
|
// HTLCs when calling RegisterAttempt.
|
|
|
|
activeAttempt := makeActiveAttempt(int(paymentAmt/4), 0)
|
|
|
|
controlTower.On("RegisterAttempt",
|
|
|
|
identifier, mock.Anything,
|
|
|
|
).Return(nil).Run(func(args mock.Arguments) {
|
|
|
|
payment.HTLCs = append(payment.HTLCs, activeAttempt)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Create a buffered chan and it will be returned by GetPaymentResult.
|
|
|
|
payer.resultChan = make(chan *htlcswitch.PaymentResult, 10)
|
|
|
|
|
|
|
|
// We use the failAttemptCount to track how many attempts we want to
|
|
|
|
// fail. Each time the following mock method is called, the count gets
|
|
|
|
// updated.
|
|
|
|
failAttemptCount := 0
|
|
|
|
payer.On("GetPaymentResult",
|
|
|
|
mock.Anything, identifier, mock.Anything,
|
|
|
|
).Run(func(args mock.Arguments) {
|
|
|
|
// Before the mock method is returned, we send the result to
|
|
|
|
// the read-only chan.
|
|
|
|
|
|
|
|
// Update the counter.
|
|
|
|
failAttemptCount++
|
|
|
|
|
|
|
|
// We will make the first two attempts failed with temporary
|
|
|
|
// error.
|
|
|
|
if failAttemptCount <= 2 {
|
|
|
|
payer.resultChan <- &htlcswitch.PaymentResult{
|
|
|
|
Error: htlcswitch.NewForwardingError(
|
|
|
|
&lnwire.FailTemporaryChannelFailure{},
|
|
|
|
1,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we will mark the attempt succeeded.
|
|
|
|
payer.resultChan <- &htlcswitch.PaymentResult{}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Mock the FailAttempt method to fail one of the attempts.
|
|
|
|
var failedAttempt channeldb.HTLCAttempt
|
|
|
|
controlTower.On("FailAttempt",
|
|
|
|
identifier, mock.Anything, mock.Anything,
|
|
|
|
).Return(&failedAttempt, nil).Run(func(args mock.Arguments) {
|
|
|
|
// Whenever this method is invoked, we will mark the first
|
|
|
|
// active attempt as failed and exit.
|
|
|
|
for i, attempt := range payment.HTLCs {
|
|
|
|
if attempt.Settle != nil || attempt.Failure != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
attempt.Failure = &channeldb.HTLCFailInfo{}
|
|
|
|
failedAttempt = attempt
|
|
|
|
payment.HTLCs[i] = attempt
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
// Setup ReportPaymentFail to return nil reason and error so the
|
|
|
|
// payment won't fail.
|
|
|
|
missionControl.On("ReportPaymentFail",
|
|
|
|
mock.Anything, mock.Anything, mock.Anything, mock.Anything,
|
|
|
|
).Return(nil, nil)
|
|
|
|
|
|
|
|
// Simple mocking the rest.
|
|
|
|
payer.On("SendHTLC",
|
|
|
|
mock.Anything, mock.Anything, mock.Anything,
|
|
|
|
).Return(nil)
|
|
|
|
missionControl.On("ReportPaymentSuccess",
|
|
|
|
mock.Anything, mock.Anything,
|
|
|
|
).Return(nil)
|
|
|
|
|
|
|
|
// Mock SettleAttempt by changing one of the HTLCs to be settled.
|
|
|
|
preimage := lntypes.Preimage{1, 2, 3}
|
|
|
|
settledAttempt := makeSettledAttempt(
|
|
|
|
int(paymentAmt/4), 0, preimage,
|
|
|
|
)
|
|
|
|
controlTower.On("SettleAttempt",
|
|
|
|
identifier, mock.Anything, mock.Anything,
|
|
|
|
).Return(&settledAttempt, nil).Run(func(args mock.Arguments) {
|
|
|
|
// Whenever this method is invoked, we will mark the first
|
|
|
|
// active attempt settled and exit.
|
|
|
|
for i, attempt := range payment.HTLCs {
|
|
|
|
if attempt.Settle != nil || attempt.Failure != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
attempt.Settle = &channeldb.HTLCSettleInfo{
|
|
|
|
Preimage: preimage,
|
|
|
|
}
|
|
|
|
payment.HTLCs[i] = attempt
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Call the actual method SendPayment on router. This is place inside a
|
|
|
|
// goroutine so we can set a timeout for the whole test, in case
|
|
|
|
// anything goes wrong and the test never finishes.
|
|
|
|
done := make(chan struct{})
|
|
|
|
var p lntypes.Hash
|
|
|
|
go func() {
|
|
|
|
p, _, err = router.SendPayment(req)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatalf("SendPayment didn't exit")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, validate the returned values and check that the mock
|
|
|
|
// methods are called as expected.
|
|
|
|
require.NoError(t, err, "send payment failed")
|
|
|
|
require.EqualValues(t, preimage, p, "preimage not match")
|
|
|
|
|
|
|
|
controlTower.AssertExpectations(t)
|
|
|
|
payer.AssertExpectations(t)
|
|
|
|
sessionSource.AssertExpectations(t)
|
|
|
|
session.AssertExpectations(t)
|
|
|
|
missionControl.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSendMPPaymentFailed tests that when one of the shard fails with a
|
|
|
|
// terminal error, the router will stop attempting and the payment will fail.
|
|
|
|
// This test mainly focuses on testing the logic of the method resumePayment
|
|
|
|
// is implemented as expected.
|
|
|
|
func TestSendMPPaymentFailed(t *testing.T) {
|
|
|
|
const startingBlockHeight = 101
|
|
|
|
|
|
|
|
// Create mockers to initialize the router.
|
|
|
|
controlTower := &mockControlTower{}
|
|
|
|
sessionSource := &mockPaymentSessionSource{}
|
|
|
|
missionControl := &mockMissionControl{}
|
|
|
|
payer := &mockPaymentAttemptDispatcher{}
|
|
|
|
chain := newMockChain(startingBlockHeight)
|
|
|
|
chainView := newMockChainView(chain)
|
|
|
|
testGraph := createDummyTestGraph(t)
|
|
|
|
|
|
|
|
// Define the behavior of the mockers to the point where we can
|
|
|
|
// successfully start the router.
|
|
|
|
controlTower.On("FetchInFlightPayments").Return(
|
|
|
|
[]*channeldb.MPPayment{}, nil,
|
|
|
|
)
|
|
|
|
payer.On("CleanStore", mock.Anything).Return(nil)
|
|
|
|
|
|
|
|
// Create and start the router.
|
|
|
|
router, err := New(Config{
|
|
|
|
Control: controlTower,
|
|
|
|
SessionSource: sessionSource,
|
|
|
|
MissionControl: missionControl,
|
|
|
|
Payer: payer,
|
|
|
|
|
|
|
|
// TODO(yy): create new mocks for the chain and chainview.
|
|
|
|
Chain: chain,
|
|
|
|
ChainView: chainView,
|
|
|
|
|
|
|
|
// TODO(yy): mock the graph once it's changed into interface.
|
|
|
|
Graph: testGraph.graph,
|
|
|
|
|
|
|
|
Clock: clock.NewTestClock(time.Unix(1, 0)),
|
|
|
|
GraphPruneInterval: time.Hour * 2,
|
|
|
|
NextPaymentID: func() (uint64, error) {
|
|
|
|
next := atomic.AddUint64(&uniquePaymentID, 1)
|
|
|
|
return next, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err, "failed to create router")
|
|
|
|
|
|
|
|
// Make sure the router can start and stop without error.
|
|
|
|
require.NoError(t, router.Start(), "router failed to start")
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, router.Stop(), "router failed to stop")
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Once the router is started, check that the mocked methods are called
|
|
|
|
// as expected.
|
|
|
|
controlTower.AssertExpectations(t)
|
|
|
|
payer.AssertExpectations(t)
|
|
|
|
|
|
|
|
// Mock the methods to the point where we are inside the function
|
|
|
|
// resumePayment.
|
|
|
|
paymentAmt := lnwire.MilliSatoshi(10000)
|
|
|
|
req := createDummyLightningPayment(
|
|
|
|
t, testGraph.aliasMap["c"], paymentAmt,
|
|
|
|
)
|
|
|
|
identifier := lntypes.Hash(req.Identifier())
|
|
|
|
session := &mockPaymentSession{}
|
|
|
|
sessionSource.On("NewPaymentSession", req).Return(session, nil)
|
|
|
|
controlTower.On("InitPayment", identifier, mock.Anything).Return(nil)
|
|
|
|
|
|
|
|
// The following mocked methods are called inside resumePayment. Note
|
|
|
|
// that the payment object below will determine the state of the
|
|
|
|
// paymentLifecycle.
|
|
|
|
payment := &channeldb.MPPayment{}
|
|
|
|
controlTower.On("FetchPayment", identifier).Return(payment, nil)
|
|
|
|
|
|
|
|
// Create a route that can send 1/4 of the total amount. This value
|
|
|
|
// will be returned by calling RequestRoute.
|
|
|
|
shard, err := createTestRoute(paymentAmt/4, testGraph.aliasMap)
|
|
|
|
require.NoError(t, err, "failed to create route")
|
|
|
|
session.On("RequestRoute",
|
|
|
|
mock.Anything, mock.Anything, mock.Anything, mock.Anything,
|
|
|
|
).Return(shard, nil)
|
|
|
|
|
|
|
|
// Make a new htlc attempt with zero fee and append it to the payment's
|
|
|
|
// HTLCs when calling RegisterAttempt.
|
|
|
|
activeAttempt := makeActiveAttempt(int(paymentAmt/4), 0)
|
|
|
|
controlTower.On("RegisterAttempt",
|
|
|
|
identifier, mock.Anything,
|
|
|
|
).Return(nil).Run(func(args mock.Arguments) {
|
|
|
|
payment.HTLCs = append(payment.HTLCs, activeAttempt)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Create a buffered chan and it will be returned by GetPaymentResult.
|
|
|
|
payer.resultChan = make(chan *htlcswitch.PaymentResult, 10)
|
|
|
|
|
|
|
|
// We use the failAttemptCount to track how many attempts we want to
|
|
|
|
// fail. Each time the following mock method is called, the count gets
|
|
|
|
// updated.
|
|
|
|
failAttemptCount := 0
|
|
|
|
payer.On("GetPaymentResult",
|
|
|
|
mock.Anything, identifier, mock.Anything,
|
|
|
|
).Run(func(args mock.Arguments) {
|
|
|
|
// Before the mock method is returned, we send the result to
|
|
|
|
// the read-only chan.
|
|
|
|
|
|
|
|
// Update the counter.
|
|
|
|
failAttemptCount++
|
|
|
|
|
|
|
|
// We fail the first attempt with terminal error.
|
|
|
|
if failAttemptCount == 1 {
|
|
|
|
payer.resultChan <- &htlcswitch.PaymentResult{
|
|
|
|
Error: htlcswitch.NewForwardingError(
|
|
|
|
&lnwire.FailIncorrectDetails{},
|
|
|
|
1,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// We will make the rest attempts failed with temporary error.
|
|
|
|
payer.resultChan <- &htlcswitch.PaymentResult{
|
|
|
|
Error: htlcswitch.NewForwardingError(
|
|
|
|
&lnwire.FailTemporaryChannelFailure{},
|
|
|
|
1,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Mock the FailAttempt method to fail one of the attempts.
|
|
|
|
var failedAttempt channeldb.HTLCAttempt
|
|
|
|
controlTower.On("FailAttempt",
|
|
|
|
identifier, mock.Anything, mock.Anything,
|
|
|
|
).Return(&failedAttempt, nil).Run(func(args mock.Arguments) {
|
|
|
|
// Whenever this method is invoked, we will mark the first
|
|
|
|
// active attempt as failed and exit.
|
|
|
|
for i, attempt := range payment.HTLCs {
|
|
|
|
if attempt.Settle != nil || attempt.Failure != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
attempt.Failure = &channeldb.HTLCFailInfo{}
|
|
|
|
failedAttempt = attempt
|
|
|
|
payment.HTLCs[i] = attempt
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
// Setup ReportPaymentFail to return nil reason and error so the
|
|
|
|
// payment won't fail.
|
|
|
|
var called bool
|
|
|
|
failureReason := channeldb.FailureReasonPaymentDetails
|
|
|
|
missionControl.On("ReportPaymentFail",
|
|
|
|
mock.Anything, mock.Anything, mock.Anything, mock.Anything,
|
2021-07-27 07:12:03 +02:00
|
|
|
).Return(&failureReason, nil).Run(func(args mock.Arguments) {
|
2021-05-21 13:02:49 +02:00
|
|
|
// We only return the terminal error once, thus when the method
|
|
|
|
// is called, we will return it with a nil error.
|
|
|
|
if called {
|
2021-07-27 07:12:03 +02:00
|
|
|
args[0] = nil
|
2021-05-21 13:02:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's the first time calling this method, we will return a
|
|
|
|
// terminal error.
|
|
|
|
payment.FailureReason = &failureReason
|
|
|
|
called = true
|
|
|
|
})
|
|
|
|
|
|
|
|
// Simple mocking the rest.
|
|
|
|
controlTower.On("Fail", identifier, failureReason).Return(nil)
|
|
|
|
payer.On("SendHTLC",
|
|
|
|
mock.Anything, mock.Anything, mock.Anything,
|
|
|
|
).Return(nil)
|
|
|
|
|
|
|
|
// Call the actual method SendPayment on router. This is place inside a
|
|
|
|
// goroutine so we can set a timeout for the whole test, in case
|
|
|
|
// anything goes wrong and the test never finishes.
|
|
|
|
done := make(chan struct{})
|
|
|
|
var p lntypes.Hash
|
|
|
|
go func() {
|
|
|
|
p, _, err = router.SendPayment(req)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatalf("SendPayment didn't exit")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, validate the returned values and check that the mock
|
|
|
|
// methods are called as expected.
|
|
|
|
require.Error(t, err, "expected send payment error")
|
|
|
|
require.EqualValues(t, [32]byte{}, p, "preimage not match")
|
|
|
|
|
|
|
|
controlTower.AssertExpectations(t)
|
|
|
|
payer.AssertExpectations(t)
|
|
|
|
sessionSource.AssertExpectations(t)
|
|
|
|
session.AssertExpectations(t)
|
|
|
|
missionControl.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSendMPPaymentFailedWithShardsInFlight tests that when the payment is in
|
|
|
|
// terminal state, even if we have shards in flight, we still fail the payment
|
|
|
|
// and exit. This test mainly focuses on testing the logic of the method
|
|
|
|
// resumePayment is implemented as expected.
|
|
|
|
func TestSendMPPaymentFailedWithShardsInFlight(t *testing.T) {
|
|
|
|
const startingBlockHeight = 101
|
|
|
|
|
|
|
|
// Create mockers to initialize the router.
|
|
|
|
controlTower := &mockControlTower{}
|
|
|
|
sessionSource := &mockPaymentSessionSource{}
|
|
|
|
missionControl := &mockMissionControl{}
|
|
|
|
payer := &mockPaymentAttemptDispatcher{}
|
|
|
|
chain := newMockChain(startingBlockHeight)
|
|
|
|
chainView := newMockChainView(chain)
|
|
|
|
testGraph := createDummyTestGraph(t)
|
|
|
|
|
|
|
|
// Define the behavior of the mockers to the point where we can
|
|
|
|
// successfully start the router.
|
|
|
|
controlTower.On("FetchInFlightPayments").Return(
|
|
|
|
[]*channeldb.MPPayment{}, nil,
|
|
|
|
)
|
|
|
|
payer.On("CleanStore", mock.Anything).Return(nil)
|
|
|
|
|
|
|
|
// Create and start the router.
|
|
|
|
router, err := New(Config{
|
|
|
|
Control: controlTower,
|
|
|
|
SessionSource: sessionSource,
|
|
|
|
MissionControl: missionControl,
|
|
|
|
Payer: payer,
|
|
|
|
|
|
|
|
// TODO(yy): create new mocks for the chain and chainview.
|
|
|
|
Chain: chain,
|
|
|
|
ChainView: chainView,
|
|
|
|
|
|
|
|
// TODO(yy): mock the graph once it's changed into interface.
|
|
|
|
Graph: testGraph.graph,
|
|
|
|
|
|
|
|
Clock: clock.NewTestClock(time.Unix(1, 0)),
|
|
|
|
GraphPruneInterval: time.Hour * 2,
|
|
|
|
NextPaymentID: func() (uint64, error) {
|
|
|
|
next := atomic.AddUint64(&uniquePaymentID, 1)
|
|
|
|
return next, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err, "failed to create router")
|
|
|
|
|
|
|
|
// Make sure the router can start and stop without error.
|
|
|
|
require.NoError(t, router.Start(), "router failed to start")
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, router.Stop(), "router failed to stop")
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Once the router is started, check that the mocked methods are called
|
|
|
|
// as expected.
|
|
|
|
controlTower.AssertExpectations(t)
|
|
|
|
payer.AssertExpectations(t)
|
|
|
|
|
|
|
|
// Mock the methods to the point where we are inside the function
|
|
|
|
// resumePayment.
|
|
|
|
paymentAmt := lnwire.MilliSatoshi(10000)
|
|
|
|
req := createDummyLightningPayment(
|
|
|
|
t, testGraph.aliasMap["c"], paymentAmt,
|
|
|
|
)
|
|
|
|
identifier := lntypes.Hash(req.Identifier())
|
|
|
|
session := &mockPaymentSession{}
|
|
|
|
sessionSource.On("NewPaymentSession", req).Return(session, nil)
|
|
|
|
controlTower.On("InitPayment", identifier, mock.Anything).Return(nil)
|
|
|
|
|
|
|
|
// The following mocked methods are called inside resumePayment. Note
|
|
|
|
// that the payment object below will determine the state of the
|
|
|
|
// paymentLifecycle.
|
|
|
|
payment := &channeldb.MPPayment{}
|
|
|
|
controlTower.On("FetchPayment", identifier).Return(payment, nil)
|
|
|
|
|
|
|
|
// Create a route that can send 1/4 of the total amount. This value
|
|
|
|
// will be returned by calling RequestRoute.
|
|
|
|
shard, err := createTestRoute(paymentAmt/4, testGraph.aliasMap)
|
|
|
|
require.NoError(t, err, "failed to create route")
|
|
|
|
session.On("RequestRoute",
|
|
|
|
mock.Anything, mock.Anything, mock.Anything, mock.Anything,
|
|
|
|
).Return(shard, nil)
|
|
|
|
|
|
|
|
// Make a new htlc attempt with zero fee and append it to the payment's
|
|
|
|
// HTLCs when calling RegisterAttempt.
|
|
|
|
activeAttempt := makeActiveAttempt(int(paymentAmt/4), 0)
|
|
|
|
controlTower.On("RegisterAttempt",
|
|
|
|
identifier, mock.Anything,
|
|
|
|
).Return(nil).Run(func(args mock.Arguments) {
|
|
|
|
payment.HTLCs = append(payment.HTLCs, activeAttempt)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Create a buffered chan and it will be returned by GetPaymentResult.
|
|
|
|
payer.resultChan = make(chan *htlcswitch.PaymentResult, 10)
|
|
|
|
|
|
|
|
// We use the failAttemptCount to track how many attempts we want to
|
|
|
|
// fail. Each time the following mock method is called, the count gets
|
|
|
|
// updated.
|
|
|
|
failAttemptCount := 0
|
|
|
|
payer.On("GetPaymentResult",
|
|
|
|
mock.Anything, identifier, mock.Anything,
|
|
|
|
).Run(func(args mock.Arguments) {
|
|
|
|
// Before the mock method is returned, we send the result to
|
|
|
|
// the read-only chan.
|
|
|
|
|
|
|
|
// Update the counter.
|
|
|
|
failAttemptCount++
|
|
|
|
|
|
|
|
// We fail the first attempt with terminal error.
|
|
|
|
if failAttemptCount == 1 {
|
|
|
|
payer.resultChan <- &htlcswitch.PaymentResult{
|
|
|
|
Error: htlcswitch.NewForwardingError(
|
|
|
|
&lnwire.FailIncorrectDetails{},
|
|
|
|
1,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the rest attempts we will NOT send anything to the
|
|
|
|
// resultChan, thus making all the shards in active state,
|
|
|
|
// neither settled or failed.
|
|
|
|
})
|
|
|
|
|
|
|
|
// Mock the FailAttempt method to fail EXACTLY once.
|
|
|
|
var failedAttempt channeldb.HTLCAttempt
|
|
|
|
controlTower.On("FailAttempt",
|
|
|
|
identifier, mock.Anything, mock.Anything,
|
|
|
|
).Return(&failedAttempt, nil).Run(func(args mock.Arguments) {
|
|
|
|
// Whenever this method is invoked, we will mark the first
|
|
|
|
// active attempt as failed and exit.
|
|
|
|
failedAttempt = payment.HTLCs[0]
|
|
|
|
failedAttempt.Failure = &channeldb.HTLCFailInfo{}
|
|
|
|
payment.HTLCs[0] = failedAttempt
|
|
|
|
}).Once()
|
|
|
|
|
|
|
|
// Setup ReportPaymentFail to return nil reason and error so the
|
|
|
|
// payment won't fail.
|
|
|
|
failureReason := channeldb.FailureReasonPaymentDetails
|
|
|
|
missionControl.On("ReportPaymentFail",
|
|
|
|
mock.Anything, mock.Anything, mock.Anything, mock.Anything,
|
2021-07-27 07:12:03 +02:00
|
|
|
).Return(&failureReason, nil).Run(func(args mock.Arguments) {
|
2021-05-21 13:02:49 +02:00
|
|
|
payment.FailureReason = &failureReason
|
|
|
|
}).Once()
|
|
|
|
|
|
|
|
// Simple mocking the rest.
|
|
|
|
controlTower.On("Fail", identifier, failureReason).Return(nil).Once()
|
|
|
|
payer.On("SendHTLC",
|
|
|
|
mock.Anything, mock.Anything, mock.Anything,
|
|
|
|
).Return(nil)
|
|
|
|
|
|
|
|
// Call the actual method SendPayment on router. This is place inside a
|
|
|
|
// goroutine so we can set a timeout for the whole test, in case
|
|
|
|
// anything goes wrong and the test never finishes.
|
|
|
|
done := make(chan struct{})
|
|
|
|
var p lntypes.Hash
|
|
|
|
go func() {
|
|
|
|
p, _, err = router.SendPayment(req)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatalf("SendPayment didn't exit")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, validate the returned values and check that the mock
|
|
|
|
// methods are called as expected.
|
|
|
|
require.Error(t, err, "expected send payment error")
|
|
|
|
require.EqualValues(t, [32]byte{}, p, "preimage not match")
|
|
|
|
|
|
|
|
controlTower.AssertExpectations(t)
|
|
|
|
payer.AssertExpectations(t)
|
|
|
|
sessionSource.AssertExpectations(t)
|
|
|
|
session.AssertExpectations(t)
|
|
|
|
missionControl.AssertExpectations(t)
|
|
|
|
}
|
2021-03-18 03:32:26 +01:00
|
|
|
|
|
|
|
// TestBlockDifferenceFix tests if when the router is behind on blocks, the
|
|
|
|
// router catches up to the best block head.
|
|
|
|
func TestBlockDifferenceFix(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
initialBlockHeight := uint32(0)
|
2021-09-17 00:21:00 +02:00
|
|
|
|
2021-03-18 03:32:26 +01:00
|
|
|
// Starting height here is set to 0, which is behind where we want to be.
|
|
|
|
ctx, cleanup := createTestCtxSingleNode(t, initialBlockHeight)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Add initial block to our mini blockchain.
|
|
|
|
block := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{},
|
|
|
|
}
|
|
|
|
ctx.chain.addBlock(block, initialBlockHeight, rand.Uint32())
|
|
|
|
|
|
|
|
// Let's generate a new block of height 5, 5 above where our node is at.
|
|
|
|
newBlock := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{},
|
|
|
|
}
|
|
|
|
newBlockHeight := uint32(5)
|
|
|
|
|
|
|
|
blockDifference := newBlockHeight - initialBlockHeight
|
|
|
|
|
|
|
|
ctx.chainView.notifyBlockAck = make(chan struct{}, 1)
|
|
|
|
|
|
|
|
ctx.chain.addBlock(newBlock, newBlockHeight, rand.Uint32())
|
|
|
|
ctx.chain.setBestBlock(int32(newBlockHeight))
|
|
|
|
ctx.chainView.notifyBlock(block.BlockHash(), newBlockHeight,
|
|
|
|
[]*wire.MsgTx{}, t)
|
|
|
|
|
|
|
|
<-ctx.chainView.notifyBlockAck
|
|
|
|
|
|
|
|
// At this point, the chain notifier should have noticed that we're
|
|
|
|
// behind on blocks, and will send the n missing blocks that we
|
|
|
|
// need to the client's epochs channel. Let's replicate this
|
|
|
|
// functionality.
|
|
|
|
for i := 0; i < int(blockDifference); i++ {
|
|
|
|
currBlockHeight := int32(i + 1)
|
|
|
|
|
|
|
|
nonce := rand.Uint32()
|
|
|
|
|
|
|
|
newBlock := &wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{},
|
|
|
|
Header: wire.BlockHeader{Nonce: nonce},
|
|
|
|
}
|
|
|
|
ctx.chain.addBlock(newBlock, uint32(currBlockHeight), nonce)
|
|
|
|
currHash := newBlock.Header.BlockHash()
|
|
|
|
|
|
|
|
newEpoch := &chainntnfs.BlockEpoch{
|
|
|
|
Height: currBlockHeight,
|
|
|
|
Hash: &currHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.notifier.EpochChan <- newEpoch
|
|
|
|
|
|
|
|
ctx.chainView.notifyBlock(currHash,
|
|
|
|
uint32(currBlockHeight), block.Transactions, t)
|
|
|
|
|
|
|
|
<-ctx.chainView.notifyBlockAck
|
|
|
|
}
|
|
|
|
|
2021-09-17 00:21:00 +02:00
|
|
|
err := wait.NoError(func() error {
|
|
|
|
// Then router height should be updated to the latest block.
|
|
|
|
if atomic.LoadUint32(&ctx.router.bestHeight) != newBlockHeight {
|
|
|
|
return fmt.Errorf("height should have been updated "+
|
|
|
|
"to %v, instead got %v", newBlockHeight,
|
|
|
|
ctx.router.bestHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, testTimeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("block height wasn't updated: %v", err)
|
2021-03-18 03:32:26 +01:00
|
|
|
}
|
|
|
|
}
|