Skip to content

Commit

Permalink
Merge 7a5bd29 into c4415f0
Browse files Browse the repository at this point in the history
  • Loading branch information
joostjager committed May 7, 2019
2 parents c4415f0 + 7a5bd29 commit 6836a8a
Show file tree
Hide file tree
Showing 13 changed files with 715 additions and 1,229 deletions.
6 changes: 0 additions & 6 deletions cmd/lncli/commands.go
Expand Up @@ -2977,11 +2977,6 @@ var queryRoutesCommand = cli.Command{
Usage: "percentage of the payment's amount used as the " +
"maximum fee allowed when sending the payment",
},
cli.Int64Flag{
Name: "num_max_routes",
Usage: "the max number of routes to be returned",
Value: 10,
},
cli.Int64Flag{
Name: "final_cltv_delta",
Usage: "(optional) number of blocks the last hop has to reveal " +
Expand Down Expand Up @@ -3035,7 +3030,6 @@ func queryRoutes(ctx *cli.Context) error {
PubKey: dest,
Amt: amt,
FeeLimit: feeLimit,
NumRoutes: int32(ctx.Int("num_max_routes")),
FinalCltvDelta: int32(ctx.Int("final_cltv_delta")),
}

Expand Down
25 changes: 7 additions & 18 deletions lnd_test.go
Expand Up @@ -1460,7 +1460,6 @@ func testUpdateChannelPolicy(net *lntest.NetworkHarness, t *harnessTest) {
routesReq := &lnrpc.QueryRoutesRequest{
PubKey: carol.PubKeyStr,
Amt: int64(payAmt),
NumRoutes: 1,
FinalCltvDelta: defaultTimeLockDelta,
}

Expand Down Expand Up @@ -4270,7 +4269,6 @@ func testSingleHopSendToRoute(net *lntest.NetworkHarness, t *harnessTest) {
routesReq := &lnrpc.QueryRoutesRequest{
PubKey: net.Bob.PubKeyStr,
Amt: paymentAmt,
NumRoutes: 1,
FinalCltvDelta: defaultBitcoinTimeLockDelta,
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
Expand Down Expand Up @@ -4442,7 +4440,6 @@ func testMultiHopSendToRoute(net *lntest.NetworkHarness, t *harnessTest) {
routesReq := &lnrpc.QueryRoutesRequest{
PubKey: carol.PubKeyStr,
Amt: paymentAmt,
NumRoutes: 1,
FinalCltvDelta: defaultBitcoinTimeLockDelta,
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
Expand Down Expand Up @@ -4603,9 +4600,8 @@ func testSendToRouteErrorPropagation(net *lntest.NetworkHarness, t *harnessTest)
// Query routes from Carol to Charlie which will be an invalid route
// for Alice -> Bob.
fakeReq := &lnrpc.QueryRoutesRequest{
PubKey: charlie.PubKeyStr,
Amt: int64(1),
NumRoutes: 1,
PubKey: charlie.PubKeyStr,
Amt: int64(1),
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
fakeRoute, err := carol.QueryRoutes(ctxt, fakeReq)
Expand Down Expand Up @@ -12342,9 +12338,8 @@ func testQueryRoutes(net *lntest.NetworkHarness, t *harnessTest) {
// Query for routes to pay from Alice to Dave.
const paymentAmt = 1000
routesReq := &lnrpc.QueryRoutesRequest{
PubKey: dave.PubKeyStr,
Amt: paymentAmt,
NumRoutes: 1,
PubKey: dave.PubKeyStr,
Amt: paymentAmt,
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
routesRes, err := net.Alice.QueryRoutes(ctxt, routesReq)
Expand Down Expand Up @@ -12646,22 +12641,16 @@ func testRouteFeeCutoff(net *lntest.NetworkHarness, t *harnessTest) {
// payments.
testFeeCutoff := func(feeLimit *lnrpc.FeeLimit) {
queryRoutesReq := &lnrpc.QueryRoutesRequest{
PubKey: dave.PubKeyStr,
Amt: paymentAmt,
FeeLimit: feeLimit,
NumRoutes: 2,
PubKey: dave.PubKeyStr,
Amt: paymentAmt,
FeeLimit: feeLimit,
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
routesResp, err := net.Alice.QueryRoutes(ctxt, queryRoutesReq)
if err != nil {
t.Fatalf("unable to get routes: %v", err)
}

if len(routesResp.Routes) != 1 {
t.Fatalf("expected one route, got %d",
len(routesResp.Routes))
}

checkRoute(routesResp.Routes[0])

invoice := &lnrpc.Invoice{Value: paymentAmt}
Expand Down
39 changes: 10 additions & 29 deletions lnrpc/routerrpc/router_backend.go
Expand Up @@ -28,10 +28,9 @@ type RouterBackend struct {

// FindRoutes is a closure that abstracts away how we locate/query for
// routes.
FindRoutes func(source, target route.Vertex,
FindRoute func(source, target route.Vertex,
amt lnwire.MilliSatoshi, restrictions *routing.RestrictParams,
numPaths uint32, finalExpiry ...uint16) (
[]*route.Route, error)
finalExpiry ...uint16) (*route.Route, error)
}

// QueryRoutes attempts to query the daemons' Channel Router for a possible
Expand Down Expand Up @@ -122,53 +121,35 @@ func (r *RouterBackend) QueryRoutes(ctx context.Context,
IgnoredEdges: ignoredEdges,
}

// numRoutes will default to 10 if not specified explicitly.
numRoutesIn := uint32(in.NumRoutes)
if numRoutesIn == 0 {
numRoutesIn = 10
}

// Query the channel router for a possible path to the destination that
// can carry `in.Amt` satoshis _including_ the total fee required on
// the route.
var (
routes []*route.Route
route *route.Route
findErr error
)

if in.FinalCltvDelta == 0 {
routes, findErr = r.FindRoutes(
route, findErr = r.FindRoute(
sourcePubKey, targetPubKey, amtMSat, restrictions,
numRoutesIn,
)
} else {
routes, findErr = r.FindRoutes(
route, findErr = r.FindRoute(
sourcePubKey, targetPubKey, amtMSat, restrictions,
numRoutesIn, uint16(in.FinalCltvDelta),
uint16(in.FinalCltvDelta),
)
}
if findErr != nil {
return nil, findErr
}

// As the number of returned routes can be less than the number of
// requested routes, we'll clamp down the length of the response to the
// minimum of the two.
numRoutes := uint32(len(routes))
if numRoutesIn < numRoutes {
numRoutes = numRoutesIn
}

// For each valid route, we'll convert the result into the format
// required by the RPC system.

rpcRoute := r.MarshallRoute(route)

routeResp := &lnrpc.QueryRoutesResponse{
Routes: make([]*lnrpc.Route, 0, in.NumRoutes),
}
for i := uint32(0); i < numRoutes; i++ {
routeResp.Routes = append(
routeResp.Routes,
r.MarshallRoute(routes[i]),
)
Routes: []*lnrpc.Route{rpcRoute},
}

return routeResp, nil
Expand Down
16 changes: 4 additions & 12 deletions lnrpc/routerrpc/router_backend_test.go
Expand Up @@ -42,7 +42,6 @@ func TestQueryRoutes(t *testing.T) {
request := &lnrpc.QueryRoutesRequest{
PubKey: destKey,
Amt: 100000,
NumRoutes: 1,
FinalCltvDelta: 100,
FeeLimit: &lnrpc.FeeLimit{
Limit: &lnrpc.FeeLimit_Fixed{
Expand All @@ -58,19 +57,14 @@ func TestQueryRoutes(t *testing.T) {

rt := &route.Route{}

findRoutes := func(source, target route.Vertex,
findRoute := func(source, target route.Vertex,
amt lnwire.MilliSatoshi, restrictions *routing.RestrictParams,
numPaths uint32, finalExpiry ...uint16) (
[]*route.Route, error) {
finalExpiry ...uint16) (*route.Route, error) {

if int64(amt) != request.Amt*1000 {
t.Fatal("unexpected amount")
}

if numPaths != 1 {
t.Fatal("unexpected number of routes")
}

if source != sourceKey {
t.Fatal("unexpected source key")
}
Expand Down Expand Up @@ -101,14 +95,12 @@ func TestQueryRoutes(t *testing.T) {
t.Fatal("unexpected ignored node")
}

return []*route.Route{
rt,
}, nil
return rt, nil
}

backend := &RouterBackend{
MaxPaymentMSat: lnwire.NewMSatFromSatoshis(1000000),
FindRoutes: findRoutes,
FindRoute: findRoute,
SelfNode: route.Vertex{1, 2, 3},
FetchChannelCapacity: func(chanID uint64) (
btcutil.Amount, error) {
Expand Down
12 changes: 4 additions & 8 deletions lnrpc/routerrpc/router_server.go
Expand Up @@ -247,22 +247,18 @@ func (s *Server) EstimateRouteFee(ctx context.Context,

// Finally, we'll query for a route to the destination that can carry
// that target amount, we'll only request a single route.
routes, err := s.cfg.Router.FindRoutes(
route, err := s.cfg.Router.FindRoute(
s.cfg.RouterBackend.SelfNode, destNode, amtMsat,
&routing.RestrictParams{
FeeLimit: feeLimit,
}, 1,
},
)
if err != nil {
return nil, err
}

if len(routes) == 0 {
return nil, fmt.Errorf("unable to find route to dest: %v", err)
}

return &RouteFeeResponse{
RoutingFeeMsat: int64(routes[0].TotalFees),
TimeLockDelay: int64(routes[0].TotalTimeLock),
RoutingFeeMsat: int64(route.TotalFees),
TimeLockDelay: int64(route.TotalTimeLock),
}, nil
}

0 comments on commit 6836a8a

Please sign in to comment.