routing: check for invalid routes

This commit is contained in:
Joost Jager
2019-12-11 08:56:41 +01:00
parent b8abae6a7e
commit 31b2c22cf5
5 changed files with 101 additions and 4 deletions

View File

@@ -3339,6 +3339,74 @@ func TestSendToRouteStructuredError(t *testing.T) {
}
}
// 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
ctx, cleanUp, err := createTestCtxFromGraphInstance(
startingBlockHeight, testGraph,
)
if err != nil {
t.Fatalf("unable to create router: %v", err)
}
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)
}
}
// TestBuildRoute tests whether correct routes are built.
func TestBuildRoute(t *testing.T) {
// Setup a three node network.