mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-05-31 18:21:42 +02:00
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:
parent
7719bc432f
commit
c62c9d64da
1155
lnrpc/rpc.pb.go
1155
lnrpc/rpc.pb.go
File diff suppressed because it is too large
Load Diff
@ -1490,6 +1490,12 @@ message QueryRoutesRequest {
|
|||||||
A list of edges to ignore during path finding.
|
A list of edges to ignore during path finding.
|
||||||
*/
|
*/
|
||||||
repeated EdgeLocator ignored_edges = 7;
|
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 {
|
message EdgeLocator {
|
||||||
|
@ -604,6 +604,13 @@
|
|||||||
"type": "string",
|
"type": "string",
|
||||||
"format": "byte"
|
"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": [
|
"tags": [
|
||||||
|
@ -1712,7 +1712,7 @@ func TestPathFindSpecExample(t *testing.T) {
|
|||||||
carol := ctx.aliases["C"]
|
carol := ctx.aliases["C"]
|
||||||
const amt lnwire.MilliSatoshi = 4999999
|
const amt lnwire.MilliSatoshi = 4999999
|
||||||
routes, err := ctx.router.FindRoutes(
|
routes, err := ctx.router.FindRoutes(
|
||||||
carol, amt, noRestrictions, 100,
|
bobNode.PubKeyBytes, carol, amt, noRestrictions, 100,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to find route: %v", err)
|
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.
|
// We'll now request a route from A -> B -> C.
|
||||||
ctx.router.routeCache = make(map[routeTuple][]*Route)
|
ctx.router.routeCache = make(map[routeTuple][]*Route)
|
||||||
routes, err = ctx.router.FindRoutes(
|
routes, err = ctx.router.FindRoutes(
|
||||||
carol, amt, noRestrictions, 100,
|
source.PubKeyBytes, carol, amt, noRestrictions, 100,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to find routes: %v", err)
|
t.Fatalf("unable to find routes: %v", err)
|
||||||
|
@ -1325,9 +1325,9 @@ func pathsToFeeSortedRoutes(source Vertex, paths [][]*channeldb.ChannelEdgePolic
|
|||||||
// the required fee and time lock values running backwards along the route. The
|
// 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
|
// route that will be ranked the highest is the one with the lowest cumulative
|
||||||
// fee along the route.
|
// fee along the route.
|
||||||
func (r *ChannelRouter) FindRoutes(target Vertex, amt lnwire.MilliSatoshi,
|
func (r *ChannelRouter) FindRoutes(source, target Vertex,
|
||||||
restrictions *RestrictParams, numPaths uint32, finalExpiry ...uint16) (
|
amt lnwire.MilliSatoshi, restrictions *RestrictParams, numPaths uint32,
|
||||||
[]*Route, error) {
|
finalExpiry ...uint16) ([]*Route, error) {
|
||||||
|
|
||||||
var finalCLTVDelta uint16
|
var finalCLTVDelta uint16
|
||||||
if len(finalExpiry) == 0 {
|
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
|
// we'll execute our KSP algorithm to find the k-shortest paths from
|
||||||
// our source to the destination.
|
// our source to the destination.
|
||||||
shortestPaths, err := findPaths(
|
shortestPaths, err := findPaths(
|
||||||
tx, r.cfg.Graph, r.selfNode.PubKeyBytes, target, amt,
|
tx, r.cfg.Graph, source, target, amt, restrictions,
|
||||||
restrictions, numPaths, bandwidthHints,
|
numPaths, bandwidthHints,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
|
@ -184,6 +184,7 @@ func TestFindRoutesFeeSorting(t *testing.T) {
|
|||||||
paymentAmt := lnwire.NewMSatFromSatoshis(100)
|
paymentAmt := lnwire.NewMSatFromSatoshis(100)
|
||||||
target := ctx.aliases["luoji"]
|
target := ctx.aliases["luoji"]
|
||||||
routes, err := ctx.router.FindRoutes(
|
routes, err := ctx.router.FindRoutes(
|
||||||
|
ctx.router.selfNode.PubKeyBytes,
|
||||||
target, paymentAmt, noRestrictions, defaultNumRoutes,
|
target, paymentAmt, noRestrictions, defaultNumRoutes,
|
||||||
DefaultFinalCLTVDelta,
|
DefaultFinalCLTVDelta,
|
||||||
)
|
)
|
||||||
@ -245,6 +246,7 @@ func TestFindRoutesWithFeeLimit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
routes, err := ctx.router.FindRoutes(
|
routes, err := ctx.router.FindRoutes(
|
||||||
|
ctx.router.selfNode.PubKeyBytes,
|
||||||
target, paymentAmt, restrictions, defaultNumRoutes,
|
target, paymentAmt, restrictions, defaultNumRoutes,
|
||||||
DefaultFinalCLTVDelta,
|
DefaultFinalCLTVDelta,
|
||||||
)
|
)
|
||||||
@ -1341,6 +1343,7 @@ func TestAddEdgeUnknownVertexes(t *testing.T) {
|
|||||||
var targetPubKeyBytes Vertex
|
var targetPubKeyBytes Vertex
|
||||||
copy(targetPubKeyBytes[:], targetNode.SerializeCompressed())
|
copy(targetPubKeyBytes[:], targetNode.SerializeCompressed())
|
||||||
routes, err := ctx.router.FindRoutes(
|
routes, err := ctx.router.FindRoutes(
|
||||||
|
ctx.router.selfNode.PubKeyBytes,
|
||||||
targetPubKeyBytes, paymentAmt, noRestrictions, defaultNumRoutes,
|
targetPubKeyBytes, paymentAmt, noRestrictions, defaultNumRoutes,
|
||||||
DefaultFinalCLTVDelta,
|
DefaultFinalCLTVDelta,
|
||||||
)
|
)
|
||||||
@ -1386,6 +1389,7 @@ func TestAddEdgeUnknownVertexes(t *testing.T) {
|
|||||||
// Should still be able to find the routes, and the info should be
|
// Should still be able to find the routes, and the info should be
|
||||||
// updated.
|
// updated.
|
||||||
routes, err = ctx.router.FindRoutes(
|
routes, err = ctx.router.FindRoutes(
|
||||||
|
ctx.router.selfNode.PubKeyBytes,
|
||||||
targetPubKeyBytes, paymentAmt, noRestrictions, defaultNumRoutes,
|
targetPubKeyBytes, paymentAmt, noRestrictions, defaultNumRoutes,
|
||||||
DefaultFinalCLTVDelta,
|
DefaultFinalCLTVDelta,
|
||||||
)
|
)
|
||||||
|
46
rpcserver.go
46
rpcserver.go
@ -3997,18 +3997,48 @@ func (r *rpcServer) GetNodeInfo(ctx context.Context,
|
|||||||
func (r *rpcServer) QueryRoutes(ctx context.Context,
|
func (r *rpcServer) QueryRoutes(ctx context.Context,
|
||||||
in *lnrpc.QueryRoutesRequest) (*lnrpc.QueryRoutesResponse, error) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(pubKeyBytes) != 33 {
|
var sourcePubKey routing.Vertex
|
||||||
return nil, errors.New("invalid key length")
|
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
|
channelGraph := r.server.chanDB.ChannelGraph()
|
||||||
copy(pubKey[:], pubKeyBytes)
|
selfNode, err := channelGraph.SourceNode()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sourcePubKey = selfNode.PubKeyBytes
|
||||||
|
}
|
||||||
|
|
||||||
// Currently, within the bootstrap phase of the network, we limit the
|
// Currently, within the bootstrap phase of the network, we limit the
|
||||||
// largest payment size allotted to (2^32) - 1 mSAT or 4.29 million
|
// 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 {
|
if in.FinalCltvDelta == 0 {
|
||||||
routes, findErr = r.server.chanRouter.FindRoutes(
|
routes, findErr = r.server.chanRouter.FindRoutes(
|
||||||
pubKey, amtMSat, restrictions, numRoutesIn,
|
sourcePubKey, targetPubKey, amtMSat, restrictions, numRoutesIn,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
routes, findErr = r.server.chanRouter.FindRoutes(
|
routes, findErr = r.server.chanRouter.FindRoutes(
|
||||||
pubKey, amtMSat, restrictions, numRoutesIn,
|
sourcePubKey, targetPubKey, amtMSat, restrictions, numRoutesIn,
|
||||||
uint16(in.FinalCltvDelta),
|
uint16(in.FinalCltvDelta),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user