routing: add source parameter to query routes

This commit allows execution of QueryRoutes from any source node.
Previously this was restricted to only the self node.
This commit is contained in:
Joost Jager 2019-03-05 16:49:26 +01:00
parent 7719bc432f
commit c62c9d64da
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7
7 changed files with 646 additions and 586 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1490,6 +1490,12 @@ message QueryRoutesRequest {
A list of edges to ignore during path finding.
*/
repeated EdgeLocator ignored_edges = 7;
/**
The source node where the request route should originated from. If empty,
self is assumed.
*/
string source_pub_key = 8;
}
message EdgeLocator {

View File

@ -604,6 +604,13 @@
"type": "string",
"format": "byte"
}
},
{
"name": "source_pub_key",
"description": "*\nThe source node where the request route should originated from. If empty,\nself is assumed.",
"in": "query",
"required": false,
"type": "string"
}
],
"tags": [

View File

@ -1712,7 +1712,7 @@ func TestPathFindSpecExample(t *testing.T) {
carol := ctx.aliases["C"]
const amt lnwire.MilliSatoshi = 4999999
routes, err := ctx.router.FindRoutes(
carol, amt, noRestrictions, 100,
bobNode.PubKeyBytes, carol, amt, noRestrictions, 100,
)
if err != nil {
t.Fatalf("unable to find route: %v", err)
@ -1779,7 +1779,7 @@ func TestPathFindSpecExample(t *testing.T) {
// We'll now request a route from A -> B -> C.
ctx.router.routeCache = make(map[routeTuple][]*Route)
routes, err = ctx.router.FindRoutes(
carol, amt, noRestrictions, 100,
source.PubKeyBytes, carol, amt, noRestrictions, 100,
)
if err != nil {
t.Fatalf("unable to find routes: %v", err)

View File

@ -1325,9 +1325,9 @@ func pathsToFeeSortedRoutes(source Vertex, paths [][]*channeldb.ChannelEdgePolic
// the required fee and time lock values running backwards along the route. The
// route that will be ranked the highest is the one with the lowest cumulative
// fee along the route.
func (r *ChannelRouter) FindRoutes(target Vertex, amt lnwire.MilliSatoshi,
restrictions *RestrictParams, numPaths uint32, finalExpiry ...uint16) (
[]*Route, error) {
func (r *ChannelRouter) FindRoutes(source, target Vertex,
amt lnwire.MilliSatoshi, restrictions *RestrictParams, numPaths uint32,
finalExpiry ...uint16) ([]*Route, error) {
var finalCLTVDelta uint16
if len(finalExpiry) == 0 {
@ -1394,8 +1394,8 @@ func (r *ChannelRouter) FindRoutes(target Vertex, amt lnwire.MilliSatoshi,
// we'll execute our KSP algorithm to find the k-shortest paths from
// our source to the destination.
shortestPaths, err := findPaths(
tx, r.cfg.Graph, r.selfNode.PubKeyBytes, target, amt,
restrictions, numPaths, bandwidthHints,
tx, r.cfg.Graph, source, target, amt, restrictions,
numPaths, bandwidthHints,
)
if err != nil {
tx.Rollback()

View File

@ -184,6 +184,7 @@ func TestFindRoutesFeeSorting(t *testing.T) {
paymentAmt := lnwire.NewMSatFromSatoshis(100)
target := ctx.aliases["luoji"]
routes, err := ctx.router.FindRoutes(
ctx.router.selfNode.PubKeyBytes,
target, paymentAmt, noRestrictions, defaultNumRoutes,
DefaultFinalCLTVDelta,
)
@ -245,6 +246,7 @@ func TestFindRoutesWithFeeLimit(t *testing.T) {
}
routes, err := ctx.router.FindRoutes(
ctx.router.selfNode.PubKeyBytes,
target, paymentAmt, restrictions, defaultNumRoutes,
DefaultFinalCLTVDelta,
)
@ -1341,6 +1343,7 @@ func TestAddEdgeUnknownVertexes(t *testing.T) {
var targetPubKeyBytes Vertex
copy(targetPubKeyBytes[:], targetNode.SerializeCompressed())
routes, err := ctx.router.FindRoutes(
ctx.router.selfNode.PubKeyBytes,
targetPubKeyBytes, paymentAmt, noRestrictions, defaultNumRoutes,
DefaultFinalCLTVDelta,
)
@ -1386,6 +1389,7 @@ func TestAddEdgeUnknownVertexes(t *testing.T) {
// Should still be able to find the routes, and the info should be
// updated.
routes, err = ctx.router.FindRoutes(
ctx.router.selfNode.PubKeyBytes,
targetPubKeyBytes, paymentAmt, noRestrictions, defaultNumRoutes,
DefaultFinalCLTVDelta,
)

View File

@ -3997,18 +3997,48 @@ func (r *rpcServer) GetNodeInfo(ctx context.Context,
func (r *rpcServer) QueryRoutes(ctx context.Context,
in *lnrpc.QueryRoutesRequest) (*lnrpc.QueryRoutesResponse, error) {
parsePubKey := func(key string) (routing.Vertex, error) {
pubKeyBytes, err := hex.DecodeString(key)
if err != nil {
return routing.Vertex{}, err
}
pubKeyBytes, err := hex.DecodeString(in.PubKey)
if len(pubKeyBytes) != 33 {
return routing.Vertex{},
errors.New("invalid key length")
}
var v routing.Vertex
copy(v[:], pubKeyBytes)
return v, nil
}
// Parse the hex-encoded source and target public keys into full public
// key objects we can properly manipulate.
targetPubKey, err := parsePubKey(in.PubKey)
if err != nil {
return nil, err
}
if len(pubKeyBytes) != 33 {
return nil, errors.New("invalid key length")
}
var sourcePubKey routing.Vertex
if in.SourcePubKey != "" {
var err error
sourcePubKey, err = parsePubKey(in.SourcePubKey)
if err != nil {
return nil, err
}
} else {
// If no source is specified, use self.
var pubKey routing.Vertex
copy(pubKey[:], pubKeyBytes)
channelGraph := r.server.chanDB.ChannelGraph()
selfNode, err := channelGraph.SourceNode()
if err != nil {
return nil, err
}
sourcePubKey = selfNode.PubKeyBytes
}
// Currently, within the bootstrap phase of the network, we limit the
// largest payment size allotted to (2^32) - 1 mSAT or 4.29 million
@ -4066,11 +4096,11 @@ func (r *rpcServer) QueryRoutes(ctx context.Context,
if in.FinalCltvDelta == 0 {
routes, findErr = r.server.chanRouter.FindRoutes(
pubKey, amtMSat, restrictions, numRoutesIn,
sourcePubKey, targetPubKey, amtMSat, restrictions, numRoutesIn,
)
} else {
routes, findErr = r.server.chanRouter.FindRoutes(
pubKey, amtMSat, restrictions, numRoutesIn,
sourcePubKey, targetPubKey, amtMSat, restrictions, numRoutesIn,
uint16(in.FinalCltvDelta),
)
}