2019-03-11 11:56:05 +03:00
|
|
|
package routerrpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil"
|
2020-01-14 13:21:24 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-03-11 11:56:05 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-12-11 12:52:27 +03:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2019-03-11 11:56:05 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing"
|
2019-04-05 18:36:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2021-03-25 05:53:13 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-11 11:56:05 +03:00
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
destKey = "0286098b97bc843372b4426d4b276cea9aa2f48f0428d6f5b66ae101befc14f8b4"
|
|
|
|
ignoreNodeKey = "02f274f48f3c0d590449a6776e3ce8825076ac376e470e992246eebc565ef8bb2a"
|
2020-01-14 13:21:24 +03:00
|
|
|
hintNodeKey = "0274e7fb33eafd74fe1acb6db7680bb4aa78e9c839a6e954e38abfad680f645ef7"
|
2019-06-19 09:29:44 +03:00
|
|
|
|
|
|
|
testMissionControlProb = 0.5
|
2019-03-11 11:56:05 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-04-05 18:36:11 +03:00
|
|
|
sourceKey = route.Vertex{1, 2, 3}
|
2019-07-29 16:10:58 +03:00
|
|
|
|
|
|
|
node1 = route.Vertex{10}
|
|
|
|
|
|
|
|
node2 = route.Vertex{11}
|
2019-03-11 11:56:05 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestQueryRoutes asserts that query routes rpc parameters are properly parsed
|
|
|
|
// and passed onto path finding.
|
|
|
|
func TestQueryRoutes(t *testing.T) {
|
2019-06-19 09:29:44 +03:00
|
|
|
t.Run("no mission control", func(t *testing.T) {
|
2019-11-11 18:44:08 +03:00
|
|
|
testQueryRoutes(t, false, false)
|
|
|
|
})
|
|
|
|
t.Run("no mission control and msat", func(t *testing.T) {
|
|
|
|
testQueryRoutes(t, false, true)
|
2019-06-19 09:29:44 +03:00
|
|
|
})
|
|
|
|
t.Run("with mission control", func(t *testing.T) {
|
2019-11-11 18:44:08 +03:00
|
|
|
testQueryRoutes(t, true, false)
|
2019-06-19 09:29:44 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-11 18:44:08 +03:00
|
|
|
func testQueryRoutes(t *testing.T, useMissionControl bool, useMsat bool) {
|
2019-03-11 11:56:05 +03:00
|
|
|
ignoreNodeBytes, err := hex.DecodeString(ignoreNodeKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-04-05 18:36:11 +03:00
|
|
|
var ignoreNodeVertex route.Vertex
|
2019-03-11 11:56:05 +03:00
|
|
|
copy(ignoreNodeVertex[:], ignoreNodeBytes)
|
|
|
|
|
|
|
|
destNodeBytes, err := hex.DecodeString(destKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-01-14 13:21:24 +03:00
|
|
|
var (
|
|
|
|
lastHop = route.Vertex{64}
|
|
|
|
outgoingChan = uint64(383322)
|
|
|
|
)
|
|
|
|
|
|
|
|
hintNode, err := route.NewVertexFromStr(hintNodeKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcRouteHints := []*lnrpc.RouteHint{
|
|
|
|
{
|
|
|
|
HopHints: []*lnrpc.HopHint{
|
|
|
|
{
|
|
|
|
ChanId: 38484,
|
|
|
|
NodeId: hintNodeKey,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-03-11 11:56:05 +03:00
|
|
|
request := &lnrpc.QueryRoutesRequest{
|
|
|
|
PubKey: destKey,
|
|
|
|
FinalCltvDelta: 100,
|
2019-11-11 18:44:08 +03:00
|
|
|
IgnoredNodes: [][]byte{ignoreNodeBytes},
|
2019-04-05 18:25:49 +03:00
|
|
|
IgnoredEdges: []*lnrpc.EdgeLocator{{
|
2019-03-11 11:56:05 +03:00
|
|
|
ChannelId: 555,
|
|
|
|
DirectionReverse: true,
|
|
|
|
}},
|
2019-07-29 16:10:58 +03:00
|
|
|
IgnoredPairs: []*lnrpc.NodePair{{
|
|
|
|
From: node1[:],
|
|
|
|
To: node2[:],
|
|
|
|
}},
|
2019-06-19 09:29:44 +03:00
|
|
|
UseMissionControl: useMissionControl,
|
2020-01-14 13:21:24 +03:00
|
|
|
LastHopPubkey: lastHop[:],
|
|
|
|
OutgoingChanId: outgoingChan,
|
|
|
|
DestFeatures: []lnrpc.FeatureBit{lnrpc.FeatureBit_MPP_OPT},
|
|
|
|
RouteHints: rpcRouteHints,
|
2019-03-11 11:56:05 +03:00
|
|
|
}
|
|
|
|
|
2019-11-11 18:44:08 +03:00
|
|
|
amtSat := int64(100000)
|
|
|
|
if useMsat {
|
|
|
|
request.AmtMsat = amtSat * 1000
|
|
|
|
request.FeeLimit = &lnrpc.FeeLimit{
|
|
|
|
Limit: &lnrpc.FeeLimit_FixedMsat{
|
|
|
|
FixedMsat: 250000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
request.Amt = amtSat
|
|
|
|
request.FeeLimit = &lnrpc.FeeLimit{
|
|
|
|
Limit: &lnrpc.FeeLimit_Fixed{
|
|
|
|
Fixed: 250,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 18:01:01 +03:00
|
|
|
findRoute := func(source, target route.Vertex,
|
2019-03-11 11:56:05 +03:00
|
|
|
amt lnwire.MilliSatoshi, restrictions *routing.RestrictParams,
|
2019-12-11 12:52:27 +03:00
|
|
|
_ record.CustomSet,
|
2020-01-14 13:21:24 +03:00
|
|
|
routeHints map[route.Vertex][]*channeldb.ChannelEdgePolicy,
|
2020-01-14 14:00:26 +03:00
|
|
|
finalExpiry uint16) (*route.Route, error) {
|
2019-03-11 11:56:05 +03:00
|
|
|
|
2019-11-11 18:44:08 +03:00
|
|
|
if int64(amt) != amtSat*1000 {
|
2019-03-11 11:56:05 +03:00
|
|
|
t.Fatal("unexpected amount")
|
|
|
|
}
|
|
|
|
|
|
|
|
if source != sourceKey {
|
|
|
|
t.Fatal("unexpected source key")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(target[:], destNodeBytes) {
|
|
|
|
t.Fatal("unexpected target key")
|
|
|
|
}
|
|
|
|
|
|
|
|
if restrictions.FeeLimit != 250*1000 {
|
|
|
|
t.Fatal("unexpected fee limit")
|
|
|
|
}
|
|
|
|
|
2019-07-29 16:10:58 +03:00
|
|
|
if restrictions.ProbabilitySource(route.Vertex{2},
|
|
|
|
route.Vertex{1}, 0,
|
2019-03-19 13:45:10 +03:00
|
|
|
) != 0 {
|
|
|
|
t.Fatal("expecting 0% probability for ignored edge")
|
2019-03-11 11:56:05 +03:00
|
|
|
}
|
|
|
|
|
2019-03-19 13:45:10 +03:00
|
|
|
if restrictions.ProbabilitySource(ignoreNodeVertex,
|
2019-07-29 16:10:58 +03:00
|
|
|
route.Vertex{6}, 0,
|
2019-03-19 13:45:10 +03:00
|
|
|
) != 0 {
|
|
|
|
t.Fatal("expecting 0% probability for ignored node")
|
2019-03-11 11:56:05 +03:00
|
|
|
}
|
|
|
|
|
2019-07-29 16:10:58 +03:00
|
|
|
if restrictions.ProbabilitySource(node1, node2, 0) != 0 {
|
|
|
|
t.Fatal("expecting 0% probability for ignored pair")
|
|
|
|
}
|
|
|
|
|
2020-01-14 13:21:24 +03:00
|
|
|
if *restrictions.LastHop != lastHop {
|
|
|
|
t.Fatal("unexpected last hop")
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:48:39 +03:00
|
|
|
if restrictions.OutgoingChannelIDs[0] != outgoingChan {
|
2020-01-14 13:21:24 +03:00
|
|
|
t.Fatal("unexpected outgoing channel id")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !restrictions.DestFeatures.HasFeature(lnwire.MPPOptional) {
|
|
|
|
t.Fatal("unexpected dest features")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := routeHints[hintNode]; !ok {
|
|
|
|
t.Fatal("expected route hint")
|
|
|
|
}
|
|
|
|
|
2019-06-19 09:29:44 +03:00
|
|
|
expectedProb := 1.0
|
|
|
|
if useMissionControl {
|
|
|
|
expectedProb = testMissionControlProb
|
|
|
|
}
|
2019-07-29 16:10:58 +03:00
|
|
|
if restrictions.ProbabilitySource(route.Vertex{4},
|
|
|
|
route.Vertex{5}, 0,
|
2019-06-19 09:29:44 +03:00
|
|
|
) != expectedProb {
|
2019-03-19 13:45:10 +03:00
|
|
|
t.Fatal("expecting 100% probability")
|
2019-03-11 11:56:05 +03:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:33:43 +03:00
|
|
|
hops := []*route.Hop{{}}
|
2019-05-15 15:37:58 +03:00
|
|
|
return route.NewRouteFromHops(amt, 144, source, hops)
|
2019-03-11 11:56:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
backend := &RouterBackend{
|
2020-05-28 20:28:34 +03:00
|
|
|
FindRoute: findRoute,
|
|
|
|
SelfNode: route.Vertex{1, 2, 3},
|
2019-03-11 11:56:05 +03:00
|
|
|
FetchChannelCapacity: func(chanID uint64) (
|
|
|
|
btcutil.Amount, error) {
|
|
|
|
|
|
|
|
return 1, nil
|
|
|
|
},
|
2019-06-19 09:29:44 +03:00
|
|
|
MissionControl: &mockMissionControl{},
|
2019-07-29 16:10:58 +03:00
|
|
|
FetchChannelEndpoints: func(chanID uint64) (route.Vertex,
|
|
|
|
route.Vertex, error) {
|
|
|
|
|
|
|
|
if chanID != 555 {
|
|
|
|
t.Fatal("expected endpoints to be fetched for "+
|
|
|
|
"channel 555, but got %v instead",
|
|
|
|
chanID)
|
|
|
|
}
|
|
|
|
return route.Vertex{1}, route.Vertex{2}, nil
|
|
|
|
},
|
2019-03-11 11:56:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := backend.QueryRoutes(context.Background(), request)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(resp.Routes) != 1 {
|
|
|
|
t.Fatal("expected a single route response")
|
|
|
|
}
|
|
|
|
}
|
2019-06-19 09:29:44 +03:00
|
|
|
|
|
|
|
type mockMissionControl struct {
|
2021-01-19 11:57:15 +03:00
|
|
|
MissionControl
|
2019-06-19 09:29:44 +03:00
|
|
|
}
|
|
|
|
|
2019-07-29 16:10:58 +03:00
|
|
|
func (m *mockMissionControl) GetProbability(fromNode, toNode route.Vertex,
|
|
|
|
amt lnwire.MilliSatoshi) float64 {
|
|
|
|
|
2019-06-19 09:29:44 +03:00
|
|
|
return testMissionControlProb
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:00:35 +03:00
|
|
|
func (m *mockMissionControl) ResetHistory() error {
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-19 09:29:44 +03:00
|
|
|
|
|
|
|
func (m *mockMissionControl) GetHistorySnapshot() *routing.MissionControlSnapshot {
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-27 12:43:12 +03:00
|
|
|
|
|
|
|
func (m *mockMissionControl) GetPairHistorySnapshot(fromNode,
|
|
|
|
toNode route.Vertex) routing.TimedPairResult {
|
|
|
|
|
|
|
|
return routing.TimedPairResult{}
|
|
|
|
}
|
2019-11-05 02:11:23 +03:00
|
|
|
|
2021-03-25 05:53:13 +03:00
|
|
|
type recordParseOutcome byte
|
2019-11-05 02:11:23 +03:00
|
|
|
|
|
|
|
const (
|
2021-03-25 05:53:13 +03:00
|
|
|
valid recordParseOutcome = iota
|
2019-11-05 02:11:23 +03:00
|
|
|
invalid
|
2021-03-25 05:53:13 +03:00
|
|
|
norecord
|
2019-11-05 02:11:23 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type unmarshalMPPTest struct {
|
|
|
|
name string
|
|
|
|
mpp *lnrpc.MPPRecord
|
2021-03-25 05:53:13 +03:00
|
|
|
outcome recordParseOutcome
|
2019-11-05 02:11:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestUnmarshalMPP checks both positive and negative cases of UnmarshalMPP to
|
|
|
|
// assert that an MPP record is only returned when both fields are properly
|
|
|
|
// specified. It also asserts that zero-values for both inputs is also valid,
|
|
|
|
// but returns a nil record.
|
|
|
|
func TestUnmarshalMPP(t *testing.T) {
|
|
|
|
tests := []unmarshalMPPTest{
|
|
|
|
{
|
|
|
|
name: "nil record",
|
|
|
|
mpp: nil,
|
2021-03-25 05:53:13 +03:00
|
|
|
outcome: norecord,
|
2019-11-05 02:11:23 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid total or addr",
|
|
|
|
mpp: &lnrpc.MPPRecord{
|
|
|
|
PaymentAddr: nil,
|
|
|
|
TotalAmtMsat: 0,
|
|
|
|
},
|
|
|
|
outcome: invalid,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid total only",
|
|
|
|
mpp: &lnrpc.MPPRecord{
|
|
|
|
PaymentAddr: nil,
|
|
|
|
TotalAmtMsat: 8,
|
|
|
|
},
|
|
|
|
outcome: invalid,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid addr only",
|
|
|
|
mpp: &lnrpc.MPPRecord{
|
|
|
|
PaymentAddr: bytes.Repeat([]byte{0x02}, 32),
|
|
|
|
TotalAmtMsat: 0,
|
|
|
|
},
|
|
|
|
outcome: invalid,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid total and invalid addr",
|
|
|
|
mpp: &lnrpc.MPPRecord{
|
|
|
|
PaymentAddr: []byte{0x02},
|
|
|
|
TotalAmtMsat: 8,
|
|
|
|
},
|
|
|
|
outcome: invalid,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid total and valid addr",
|
|
|
|
mpp: &lnrpc.MPPRecord{
|
|
|
|
PaymentAddr: bytes.Repeat([]byte{0x02}, 32),
|
|
|
|
TotalAmtMsat: 8,
|
|
|
|
},
|
|
|
|
outcome: valid,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
testUnmarshalMPP(t, test)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUnmarshalMPP(t *testing.T, test unmarshalMPPTest) {
|
|
|
|
mpp, err := UnmarshalMPP(test.mpp)
|
|
|
|
switch test.outcome {
|
|
|
|
|
|
|
|
// Valid arguments should result in no error, a non-nil MPP record, and
|
|
|
|
// the fields should be set correctly.
|
|
|
|
case valid:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to parse mpp record: %v", err)
|
|
|
|
}
|
|
|
|
if mpp == nil {
|
|
|
|
t.Fatalf("mpp payload should be non-nil")
|
|
|
|
}
|
|
|
|
if int64(mpp.TotalMsat()) != test.mpp.TotalAmtMsat {
|
|
|
|
t.Fatalf("incorrect total msat")
|
|
|
|
}
|
|
|
|
addr := mpp.PaymentAddr()
|
|
|
|
if !bytes.Equal(addr[:], test.mpp.PaymentAddr) {
|
|
|
|
t.Fatalf("incorrect payment addr")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid arguments should produce a failure and nil MPP record.
|
|
|
|
case invalid:
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected failure for invalid mpp")
|
|
|
|
}
|
|
|
|
if mpp != nil {
|
|
|
|
t.Fatalf("mpp payload should be nil for failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arguments that produce no MPP field should return no error and no MPP
|
|
|
|
// record.
|
2021-03-25 05:53:13 +03:00
|
|
|
case norecord:
|
2019-11-05 02:11:23 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failure for args resulting for no-mpp")
|
|
|
|
}
|
|
|
|
if mpp != nil {
|
|
|
|
t.Fatalf("mpp payload should be nil for no-mpp")
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
t.Fatalf("test case has non-standard outcome")
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 05:53:13 +03:00
|
|
|
|
|
|
|
type unmarshalAMPTest struct {
|
|
|
|
name string
|
|
|
|
amp *lnrpc.AMPRecord
|
|
|
|
outcome recordParseOutcome
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestUnmarshalAMP asserts the behavior of decoding an RPC AMPRecord.
|
|
|
|
func TestUnmarshalAMP(t *testing.T) {
|
|
|
|
rootShare := bytes.Repeat([]byte{0x01}, 32)
|
|
|
|
setID := bytes.Repeat([]byte{0x02}, 32)
|
|
|
|
|
|
|
|
// All child indexes are valid.
|
|
|
|
childIndex := uint32(3)
|
|
|
|
|
|
|
|
tests := []unmarshalAMPTest{
|
|
|
|
{
|
|
|
|
name: "nil record",
|
|
|
|
amp: nil,
|
|
|
|
outcome: norecord,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid root share invalid set id",
|
|
|
|
amp: &lnrpc.AMPRecord{
|
|
|
|
RootShare: []byte{0x01},
|
|
|
|
SetId: []byte{0x02},
|
|
|
|
ChildIndex: childIndex,
|
|
|
|
},
|
|
|
|
outcome: invalid,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid root share invalid set id",
|
|
|
|
amp: &lnrpc.AMPRecord{
|
|
|
|
RootShare: rootShare,
|
|
|
|
SetId: []byte{0x02},
|
|
|
|
ChildIndex: childIndex,
|
|
|
|
},
|
|
|
|
outcome: invalid,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid root share valid set id",
|
|
|
|
amp: &lnrpc.AMPRecord{
|
|
|
|
RootShare: []byte{0x01},
|
|
|
|
SetId: setID,
|
|
|
|
ChildIndex: childIndex,
|
|
|
|
},
|
|
|
|
outcome: invalid,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid root share valid set id",
|
|
|
|
amp: &lnrpc.AMPRecord{
|
|
|
|
RootShare: rootShare,
|
|
|
|
SetId: setID,
|
|
|
|
ChildIndex: childIndex,
|
|
|
|
},
|
|
|
|
outcome: valid,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
testUnmarshalAMP(t, test)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUnmarshalAMP(t *testing.T, test unmarshalAMPTest) {
|
|
|
|
amp, err := UnmarshalAMP(test.amp)
|
|
|
|
switch test.outcome {
|
|
|
|
case valid:
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, amp)
|
|
|
|
|
|
|
|
rootShare := amp.RootShare()
|
|
|
|
setID := amp.SetID()
|
|
|
|
require.Equal(t, test.amp.RootShare, rootShare[:])
|
|
|
|
require.Equal(t, test.amp.SetId, setID[:])
|
|
|
|
require.Equal(t, test.amp.ChildIndex, amp.ChildIndex())
|
|
|
|
|
|
|
|
case invalid:
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, amp)
|
|
|
|
|
|
|
|
case norecord:
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, amp)
|
|
|
|
|
|
|
|
default:
|
|
|
|
t.Fatalf("test case has non-standard outcome")
|
|
|
|
}
|
|
|
|
}
|