routerrpc: connect UnmarshallRoute to existing config fields

This commit is contained in:
Joost Jager 2019-03-14 15:13:45 +01:00
parent ba3fa94268
commit b2eb2619bf
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7
2 changed files with 40 additions and 33 deletions

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing" "github.com/lightningnetwork/lnd/routing"
@ -27,6 +26,11 @@ type RouterBackend struct {
// capacity of a channel to populate in responses. // capacity of a channel to populate in responses.
FetchChannelCapacity func(chanID uint64) (btcutil.Amount, error) FetchChannelCapacity func(chanID uint64) (btcutil.Amount, error)
// FetchChannelEndpoints returns the pubkeys of both endpoints of the
// given channel id.
FetchChannelEndpoints func(chanID uint64) (route.Vertex,
route.Vertex, error)
// FindRoutes is a closure that abstracts away how we locate/query for // FindRoutes is a closure that abstracts away how we locate/query for
// routes. // routes.
FindRoute func(source, target route.Vertex, FindRoute func(source, target route.Vertex,
@ -224,22 +228,21 @@ func (r *RouterBackend) MarshallRoute(route *route.Route) *lnrpc.Route {
// not known. This function will query the channel graph with channel id to // not known. This function will query the channel graph with channel id to
// retrieve both endpoints and determine the hop pubkey using the previous hop // retrieve both endpoints and determine the hop pubkey using the previous hop
// pubkey. If the channel is unknown, an error is returned. // pubkey. If the channel is unknown, an error is returned.
func UnmarshallHopByChannelLookup(graph *channeldb.ChannelGraph, hop *lnrpc.Hop, func (r *RouterBackend) UnmarshallHopByChannelLookup(hop *lnrpc.Hop,
prevPubKeyBytes [33]byte) (*route.Hop, error) { prevPubKeyBytes [33]byte) (*route.Hop, error) {
// Discard edge policies, because they may be nil. // Discard edge policies, because they may be nil.
edgeInfo, _, _, err := graph.FetchChannelEdgesByID(hop.ChanId) node1, node2, err := r.FetchChannelEndpoints(hop.ChanId)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to fetch channel edges by "+ return nil, err
"channel ID %d: %v", hop.ChanId, err)
} }
var pubKeyBytes [33]byte var pubKeyBytes [33]byte
switch { switch {
case prevPubKeyBytes == edgeInfo.NodeKey1Bytes: case prevPubKeyBytes == node1:
pubKeyBytes = edgeInfo.NodeKey2Bytes pubKeyBytes = node2
case prevPubKeyBytes == edgeInfo.NodeKey2Bytes: case prevPubKeyBytes == node2:
pubKeyBytes = edgeInfo.NodeKey1Bytes pubKeyBytes = node1
default: default:
return nil, fmt.Errorf("channel edge does not match expected node") return nil, fmt.Errorf("channel edge does not match expected node")
} }
@ -248,7 +251,7 @@ func UnmarshallHopByChannelLookup(graph *channeldb.ChannelGraph, hop *lnrpc.Hop,
OutgoingTimeLock: hop.Expiry, OutgoingTimeLock: hop.Expiry,
AmtToForward: lnwire.MilliSatoshi(hop.AmtToForwardMsat), AmtToForward: lnwire.MilliSatoshi(hop.AmtToForwardMsat),
PubKeyBytes: pubKeyBytes, PubKeyBytes: pubKeyBytes,
ChannelID: edgeInfo.ChannelID, ChannelID: hop.ChanId,
}, nil }, nil
} }
@ -274,14 +277,14 @@ func UnmarshallKnownPubkeyHop(hop *lnrpc.Hop) (*route.Hop, error) {
// UnmarshallHop unmarshalls an rpc hop that may or may not contain a node // UnmarshallHop unmarshalls an rpc hop that may or may not contain a node
// pubkey. // pubkey.
func UnmarshallHop(graph *channeldb.ChannelGraph, hop *lnrpc.Hop, func (r *RouterBackend) UnmarshallHop(hop *lnrpc.Hop,
prevNodePubKey [33]byte) (*route.Hop, error) { prevNodePubKey [33]byte) (*route.Hop, error) {
if hop.PubKey == "" { if hop.PubKey == "" {
// If no pub key is given of the hop, the local channel // If no pub key is given of the hop, the local channel
// graph needs to be queried to complete the information // graph needs to be queried to complete the information
// necessary for routing. // necessary for routing.
return UnmarshallHopByChannelLookup(graph, hop, prevNodePubKey) return r.UnmarshallHopByChannelLookup(hop, prevNodePubKey)
} }
return UnmarshallKnownPubkeyHop(hop) return UnmarshallKnownPubkeyHop(hop)
@ -289,21 +292,14 @@ func UnmarshallHop(graph *channeldb.ChannelGraph, hop *lnrpc.Hop,
// UnmarshallRoute unmarshalls an rpc route. For hops that don't specify a // UnmarshallRoute unmarshalls an rpc route. For hops that don't specify a
// pubkey, the channel graph is queried. // pubkey, the channel graph is queried.
func UnmarshallRoute(rpcroute *lnrpc.Route, func (r *RouterBackend) UnmarshallRoute(rpcroute *lnrpc.Route) (
graph *channeldb.ChannelGraph) (*route.Route, error) { *route.Route, error) {
sourceNode, err := graph.SourceNode() prevNodePubKey := r.SelfNode
if err != nil {
return nil, fmt.Errorf("unable to fetch source node from graph "+
"while unmarshaling route. %v", err)
}
prevNodePubKey := sourceNode.PubKeyBytes
hops := make([]*route.Hop, len(rpcroute.Hops)) hops := make([]*route.Hop, len(rpcroute.Hops))
for i, hop := range rpcroute.Hops { for i, hop := range rpcroute.Hops {
routeHop, err := UnmarshallHop(graph, routeHop, err := r.UnmarshallHop(hop, prevNodePubKey)
hop, prevNodePubKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -316,7 +312,7 @@ func UnmarshallRoute(rpcroute *lnrpc.Route,
route, err := route.NewRouteFromHops( route, err := route.NewRouteFromHops(
lnwire.MilliSatoshi(rpcroute.TotalAmtMsat), lnwire.MilliSatoshi(rpcroute.TotalAmtMsat),
rpcroute.TotalTimeLock, rpcroute.TotalTimeLock,
sourceNode.PubKeyBytes, r.SelfNode,
hops, hops,
) )
if err != nil { if err != nil {

@ -462,6 +462,21 @@ func newRPCServer(s *server, macService *macaroons.Service,
} }
return info.Capacity, nil return info.Capacity, nil
}, },
FetchChannelEndpoints: func(chanID uint64) (route.Vertex,
route.Vertex, error) {
info, _, _, err := graph.FetchChannelEdgesByID(
chanID,
)
if err != nil {
return route.Vertex{}, route.Vertex{},
fmt.Errorf("unable to fetch channel "+
"edges by channel ID %d: %v",
chanID, err)
}
return info.NodeKey1Bytes, info.NodeKey2Bytes, nil
},
FindRoute: s.chanRouter.FindRoute, FindRoute: s.chanRouter.FindRoute,
} }
@ -2837,9 +2852,7 @@ func (r *rpcServer) SendToRoute(stream lnrpc.Lightning_SendToRouteServer) error
return nil, err return nil, err
} }
graph := r.server.chanDB.ChannelGraph() return r.unmarshallSendToRouteRequest(req)
return unmarshallSendToRouteRequest(req, graph)
}, },
send: func(r *lnrpc.SendResponse) error { send: func(r *lnrpc.SendResponse) error {
// Calling stream.Send concurrently is not safe. // Calling stream.Send concurrently is not safe.
@ -2851,14 +2864,14 @@ func (r *rpcServer) SendToRoute(stream lnrpc.Lightning_SendToRouteServer) error
} }
// unmarshallSendToRouteRequest unmarshalls an rpc sendtoroute request // unmarshallSendToRouteRequest unmarshalls an rpc sendtoroute request
func unmarshallSendToRouteRequest(req *lnrpc.SendToRouteRequest, func (r *rpcServer) unmarshallSendToRouteRequest(
graph *channeldb.ChannelGraph) (*rpcPaymentRequest, error) { req *lnrpc.SendToRouteRequest) (*rpcPaymentRequest, error) {
if req.Route == nil { if req.Route == nil {
return nil, fmt.Errorf("unable to send, no route provided") return nil, fmt.Errorf("unable to send, no route provided")
} }
route, err := routerrpc.UnmarshallRoute(req.Route, graph) route, err := r.routerBackend.UnmarshallRoute(req.Route)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3308,9 +3321,7 @@ func (r *rpcServer) SendToRouteSync(ctx context.Context,
return nil, fmt.Errorf("unable to send, no routes provided") return nil, fmt.Errorf("unable to send, no routes provided")
} }
graph := r.server.chanDB.ChannelGraph() paymentRequest, err := r.unmarshallSendToRouteRequest(req)
paymentRequest, err := unmarshallSendToRouteRequest(req, graph)
if err != nil { if err != nil {
return nil, err return nil, err
} }