2019-08-05 13:29:16 +03:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2019-10-31 07:18:52 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2019-08-05 13:29:16 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
hops = []route.Vertex{
|
|
|
|
{1, 0}, {1, 1}, {1, 2}, {1, 3}, {1, 4},
|
|
|
|
}
|
|
|
|
|
2019-06-25 11:51:55 +03:00
|
|
|
routeOneHop = route.Route{
|
|
|
|
SourcePubKey: hops[0],
|
|
|
|
TotalAmount: 100,
|
|
|
|
Hops: []*route.Hop{
|
|
|
|
{PubKeyBytes: hops[1], AmtToForward: 99},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-05 13:29:16 +03:00
|
|
|
routeTwoHop = route.Route{
|
|
|
|
SourcePubKey: hops[0],
|
|
|
|
TotalAmount: 100,
|
|
|
|
Hops: []*route.Hop{
|
|
|
|
{PubKeyBytes: hops[1], AmtToForward: 99},
|
|
|
|
{PubKeyBytes: hops[2], AmtToForward: 97},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
routeFourHop = route.Route{
|
|
|
|
SourcePubKey: hops[0],
|
|
|
|
TotalAmount: 100,
|
|
|
|
Hops: []*route.Hop{
|
|
|
|
{PubKeyBytes: hops[1], AmtToForward: 99},
|
|
|
|
{PubKeyBytes: hops[2], AmtToForward: 97},
|
|
|
|
{PubKeyBytes: hops[3], AmtToForward: 94},
|
|
|
|
{PubKeyBytes: hops[4], AmtToForward: 90},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-06-25 11:51:55 +03:00
|
|
|
func getTestPair(from, to int) DirectedNodePair {
|
|
|
|
return NewDirectedNodePair(hops[from], hops[to])
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:18:52 +03:00
|
|
|
func getPolicyFailure(from, to int) *DirectedNodePair {
|
|
|
|
pair := getTestPair(from, to)
|
|
|
|
return &pair
|
|
|
|
}
|
|
|
|
|
2019-08-05 13:29:16 +03:00
|
|
|
type resultTestCase struct {
|
|
|
|
name string
|
|
|
|
route *route.Route
|
2019-07-29 15:20:06 +03:00
|
|
|
success bool
|
2019-08-05 13:29:16 +03:00
|
|
|
failureSrcIdx int
|
|
|
|
failure lnwire.FailureMessage
|
|
|
|
|
|
|
|
expectedResult *interpretedResult
|
|
|
|
}
|
|
|
|
|
|
|
|
var resultTestCases = []resultTestCase{
|
|
|
|
// Tests that a temporary channel failure result is properly
|
|
|
|
// interpreted.
|
|
|
|
{
|
|
|
|
name: "fail",
|
|
|
|
route: &routeTwoHop,
|
|
|
|
failureSrcIdx: 1,
|
|
|
|
failure: lnwire.NewTemporaryChannelFailure(nil),
|
|
|
|
|
|
|
|
expectedResult: &interpretedResult{
|
2019-07-29 15:20:06 +03:00
|
|
|
pairResults: map[DirectedNodePair]pairResult{
|
2019-09-30 20:45:49 +03:00
|
|
|
getTestPair(0, 1): successPairResult(),
|
|
|
|
getTestPair(1, 2): failPairResult(99),
|
2019-08-05 13:29:16 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Tests that a expiry too soon failure result is properly interpreted.
|
|
|
|
{
|
|
|
|
name: "fail expiry too soon",
|
|
|
|
route: &routeFourHop,
|
|
|
|
failureSrcIdx: 3,
|
|
|
|
failure: lnwire.NewExpiryTooSoon(lnwire.ChannelUpdate{}),
|
|
|
|
|
|
|
|
expectedResult: &interpretedResult{
|
2019-07-29 15:20:06 +03:00
|
|
|
pairResults: map[DirectedNodePair]pairResult{
|
2019-09-30 20:45:49 +03:00
|
|
|
getTestPair(0, 1): failPairResult(0),
|
|
|
|
getTestPair(1, 0): failPairResult(0),
|
|
|
|
getTestPair(1, 2): failPairResult(0),
|
|
|
|
getTestPair(2, 1): failPairResult(0),
|
|
|
|
getTestPair(2, 3): failPairResult(0),
|
|
|
|
getTestPair(3, 2): failPairResult(0),
|
2019-07-29 15:20:06 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Tests an incorrect payment details result. This should be a final
|
|
|
|
// failure, but mark all pairs along the route as successful.
|
|
|
|
{
|
|
|
|
name: "fail incorrect details",
|
|
|
|
route: &routeTwoHop,
|
|
|
|
failureSrcIdx: 2,
|
2019-06-12 13:18:58 +03:00
|
|
|
failure: lnwire.NewFailIncorrectDetails(97, 0),
|
2019-07-29 15:20:06 +03:00
|
|
|
|
|
|
|
expectedResult: &interpretedResult{
|
|
|
|
pairResults: map[DirectedNodePair]pairResult{
|
2019-09-30 20:45:49 +03:00
|
|
|
getTestPair(0, 1): successPairResult(),
|
|
|
|
getTestPair(1, 2): successPairResult(),
|
2019-07-29 15:20:06 +03:00
|
|
|
},
|
|
|
|
finalFailureReason: &reasonIncorrectDetails,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Tests a successful direct payment.
|
|
|
|
{
|
|
|
|
name: "success direct",
|
|
|
|
route: &routeOneHop,
|
|
|
|
success: true,
|
|
|
|
|
|
|
|
expectedResult: &interpretedResult{
|
|
|
|
pairResults: map[DirectedNodePair]pairResult{
|
2019-09-30 20:45:49 +03:00
|
|
|
getTestPair(0, 1): successPairResult(),
|
2019-07-29 15:20:06 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Tests a successful two hop payment.
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
route: &routeTwoHop,
|
|
|
|
success: true,
|
|
|
|
|
|
|
|
expectedResult: &interpretedResult{
|
|
|
|
pairResults: map[DirectedNodePair]pairResult{
|
2019-09-30 20:45:49 +03:00
|
|
|
getTestPair(0, 1): successPairResult(),
|
|
|
|
getTestPair(1, 2): successPairResult(),
|
2019-06-25 11:51:55 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Tests a malformed htlc from a direct peer.
|
|
|
|
{
|
|
|
|
name: "fail malformed htlc from direct peer",
|
|
|
|
route: &routeTwoHop,
|
|
|
|
failureSrcIdx: 0,
|
|
|
|
failure: lnwire.NewInvalidOnionKey(nil),
|
|
|
|
|
|
|
|
expectedResult: &interpretedResult{
|
|
|
|
nodeFailure: &hops[1],
|
2019-09-04 17:26:18 +03:00
|
|
|
pairResults: map[DirectedNodePair]pairResult{
|
|
|
|
getTestPair(1, 0): failPairResult(0),
|
|
|
|
getTestPair(1, 2): failPairResult(0),
|
|
|
|
},
|
2019-06-25 11:51:55 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Tests a malformed htlc from a direct peer that is also the final
|
|
|
|
// destination.
|
|
|
|
{
|
|
|
|
name: "fail malformed htlc from direct final peer",
|
|
|
|
route: &routeOneHop,
|
|
|
|
failureSrcIdx: 0,
|
|
|
|
failure: lnwire.NewInvalidOnionKey(nil),
|
|
|
|
|
|
|
|
expectedResult: &interpretedResult{
|
|
|
|
finalFailureReason: &reasonError,
|
|
|
|
nodeFailure: &hops[1],
|
2019-09-04 17:26:18 +03:00
|
|
|
pairResults: map[DirectedNodePair]pairResult{
|
|
|
|
getTestPair(1, 0): failPairResult(0),
|
|
|
|
},
|
2019-08-05 13:29:16 +03:00
|
|
|
},
|
|
|
|
},
|
2019-10-31 07:18:52 +03:00
|
|
|
|
|
|
|
// Tests that a fee insufficient failure to an intermediate hop with
|
|
|
|
// index 2 results in the first hop marked as success, and then a
|
|
|
|
// bidirectional failure for the incoming channel. It should also result
|
|
|
|
// in a policy failure for the outgoing hop.
|
|
|
|
{
|
|
|
|
name: "fail fee insufficient intermediate",
|
|
|
|
route: &routeFourHop,
|
|
|
|
failureSrcIdx: 2,
|
|
|
|
failure: lnwire.NewFeeInsufficient(0, lnwire.ChannelUpdate{}),
|
|
|
|
|
|
|
|
expectedResult: &interpretedResult{
|
|
|
|
pairResults: map[DirectedNodePair]pairResult{
|
|
|
|
getTestPair(0, 1): {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
getTestPair(1, 2): {},
|
|
|
|
getTestPair(2, 1): {},
|
|
|
|
},
|
|
|
|
policyFailure: getPolicyFailure(2, 3),
|
|
|
|
},
|
|
|
|
},
|
2019-10-31 07:20:08 +03:00
|
|
|
|
|
|
|
// Tests an invalid onion payload from a final hop. The final hop should
|
|
|
|
// be failed while the proceeding hops are reproed as successes. The
|
|
|
|
// failure is terminal since the receiver can't process our onion.
|
|
|
|
{
|
|
|
|
name: "fail invalid onion payload final hop",
|
|
|
|
route: &routeFourHop,
|
|
|
|
failureSrcIdx: 4,
|
|
|
|
failure: lnwire.NewInvalidOnionPayload(0, 0),
|
|
|
|
|
|
|
|
expectedResult: &interpretedResult{
|
|
|
|
pairResults: map[DirectedNodePair]pairResult{
|
|
|
|
getTestPair(0, 1): {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
getTestPair(1, 2): {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
getTestPair(2, 3): {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
getTestPair(4, 3): {},
|
|
|
|
},
|
|
|
|
finalFailureReason: &reasonError,
|
|
|
|
nodeFailure: &hops[4],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Tests an invalid onion payload from an intermediate hop. Only the
|
|
|
|
// reporting node should be failed. The failure is non-terminal since we
|
|
|
|
// can still try other paths.
|
|
|
|
{
|
|
|
|
name: "fail invalid onion payload intermediate",
|
|
|
|
route: &routeFourHop,
|
|
|
|
failureSrcIdx: 3,
|
|
|
|
failure: lnwire.NewInvalidOnionPayload(0, 0),
|
|
|
|
|
|
|
|
expectedResult: &interpretedResult{
|
|
|
|
pairResults: map[DirectedNodePair]pairResult{
|
|
|
|
getTestPair(0, 1): {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
getTestPair(1, 2): {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
getTestPair(3, 2): {},
|
|
|
|
getTestPair(3, 4): {},
|
|
|
|
},
|
|
|
|
nodeFailure: &hops[3],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Tests an invalid onion payload in a direct peer that is also the
|
|
|
|
// final hop. The final node should be failed and the error is terminal
|
|
|
|
// since the remote node can't process our onion.
|
|
|
|
{
|
|
|
|
name: "fail invalid onion payload direct",
|
|
|
|
route: &routeOneHop,
|
|
|
|
failureSrcIdx: 1,
|
|
|
|
failure: lnwire.NewInvalidOnionPayload(0, 0),
|
|
|
|
|
|
|
|
expectedResult: &interpretedResult{
|
|
|
|
pairResults: map[DirectedNodePair]pairResult{
|
|
|
|
getTestPair(1, 0): {},
|
|
|
|
},
|
|
|
|
finalFailureReason: &reasonError,
|
|
|
|
nodeFailure: &hops[1],
|
|
|
|
},
|
|
|
|
},
|
2019-08-05 13:29:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestResultInterpretation executes a list of test cases that test the result
|
|
|
|
// interpretation logic.
|
|
|
|
func TestResultInterpretation(t *testing.T) {
|
2019-07-29 15:20:06 +03:00
|
|
|
emptyResults := make(map[DirectedNodePair]pairResult)
|
2019-08-05 13:29:16 +03:00
|
|
|
|
|
|
|
for _, testCase := range resultTestCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
i := interpretResult(
|
2019-07-29 15:20:06 +03:00
|
|
|
testCase.route, testCase.success,
|
|
|
|
&testCase.failureSrcIdx, testCase.failure,
|
2019-08-05 13:29:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
expected := testCase.expectedResult
|
|
|
|
|
|
|
|
// Replace nil pairResults with empty map to satisfy
|
|
|
|
// DeepEqual.
|
|
|
|
if expected.pairResults == nil {
|
|
|
|
expected.pairResults = emptyResults
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(i, expected) {
|
2019-10-31 07:18:52 +03:00
|
|
|
t.Fatalf("unexpected result\nwant: %v\ngot: %v",
|
|
|
|
spew.Sdump(expected), spew.Sdump(i))
|
2019-08-05 13:29:16 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|