Merge pull request #3221 from joostjager/queryroutes-mc

routing+routerrpc+lnrpc: add option to use mc in queryroutes
This commit is contained in:
Olaoluwa Osuntokun 2019-07-18 20:30:29 -07:00 committed by GitHub
commit 02a5408b15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 672 additions and 561 deletions

View File

@ -2997,6 +2997,10 @@ var queryRoutesCommand = cli.Command{
Usage: "(optional) number of blocks the last hop has to reveal " +
"the preimage",
},
cli.BoolFlag{
Name: "use_mc",
Usage: "use mission control probabilities",
},
},
Action: actionDecorator(queryRoutes),
}
@ -3042,10 +3046,11 @@ func queryRoutes(ctx *cli.Context) error {
}
req := &lnrpc.QueryRoutesRequest{
PubKey: dest,
Amt: amt,
FeeLimit: feeLimit,
FinalCltvDelta: int32(ctx.Int("final_cltv_delta")),
PubKey: dest,
Amt: amt,
FeeLimit: feeLimit,
FinalCltvDelta: int32(ctx.Int("final_cltv_delta")),
UseMissionControl: ctx.Bool("use_mc"),
}
route, err := client.QueryRoutes(ctxb, req)

View File

@ -42,7 +42,7 @@ type RouterBackend struct {
amt lnwire.MilliSatoshi, restrictions *routing.RestrictParams,
finalExpiry ...uint16) (*route.Route, error)
MissionControl *routing.MissionControl
MissionControl MissionControl
// ActiveNetParams are the network parameters of the primary network
// that the route is operating on. This is necessary so we can ensure
@ -55,6 +55,22 @@ type RouterBackend struct {
Tower routing.ControlTower
}
// MissionControl defines the mission control dependencies of routerrpc.
type MissionControl interface {
// GetEdgeProbability is expected to return the success probability of a payment
// from fromNode along edge.
GetEdgeProbability(fromNode route.Vertex,
edge routing.EdgeLocator, amt lnwire.MilliSatoshi) float64
// ResetHistory resets the history of MissionControl returning it to a state as
// if no payment attempts have been made.
ResetHistory()
// GetHistorySnapshot takes a snapshot from the current mission control state
// and actual probability estimates.
GetHistorySnapshot() *routing.MissionControlSnapshot
}
// QueryRoutes attempts to query the daemons' Channel Router for a possible
// route to a target destination capable of carrying a specific amount of
// satoshis within the route's flow. The retuned route contains the full
@ -151,9 +167,14 @@ func (r *RouterBackend) QueryRoutes(ctx context.Context,
return 0
}
return 1
if !in.UseMissionControl {
return 1
}
return r.MissionControl.GetEdgeProbability(
node, edge, amt,
)
},
PaymentAttemptPenalty: routing.DefaultPaymentAttemptPenalty,
}
// Query the channel router for a possible path to the destination that

View File

@ -17,6 +17,8 @@ import (
const (
destKey = "0286098b97bc843372b4426d4b276cea9aa2f48f0428d6f5b66ae101befc14f8b4"
ignoreNodeKey = "02f274f48f3c0d590449a6776e3ce8825076ac376e470e992246eebc565ef8bb2a"
testMissionControlProb = 0.5
)
var (
@ -26,6 +28,15 @@ var (
// TestQueryRoutes asserts that query routes rpc parameters are properly parsed
// and passed onto path finding.
func TestQueryRoutes(t *testing.T) {
t.Run("no mission control", func(t *testing.T) {
testQueryRoutes(t, false)
})
t.Run("with mission control", func(t *testing.T) {
testQueryRoutes(t, true)
})
}
func testQueryRoutes(t *testing.T, useMissionControl bool) {
ignoreNodeBytes, err := hex.DecodeString(ignoreNodeKey)
if err != nil {
t.Fatal(err)
@ -58,6 +69,7 @@ func TestQueryRoutes(t *testing.T) {
ChannelId: 555,
DirectionReverse: true,
}},
UseMissionControl: useMissionControl,
}
findRoute := func(source, target route.Vertex,
@ -92,9 +104,13 @@ func TestQueryRoutes(t *testing.T) {
t.Fatal("expecting 0% probability for ignored node")
}
expectedProb := 1.0
if useMissionControl {
expectedProb = testMissionControlProb
}
if restrictions.ProbabilitySource(route.Vertex{},
routing.EdgeLocator{}, 0,
) != 1 {
) != expectedProb {
t.Fatal("expecting 100% probability")
}
@ -111,6 +127,7 @@ func TestQueryRoutes(t *testing.T) {
return 1, nil
},
MissionControl: &mockMissionControl{},
}
resp, err := backend.QueryRoutes(context.Background(), request)
@ -121,3 +138,17 @@ func TestQueryRoutes(t *testing.T) {
t.Fatal("expected a single route response")
}
}
type mockMissionControl struct {
}
func (m *mockMissionControl) GetEdgeProbability(fromNode route.Vertex,
edge routing.EdgeLocator, amt lnwire.MilliSatoshi) float64 {
return testMissionControlProb
}
func (m *mockMissionControl) ResetHistory() {}
func (m *mockMissionControl) GetHistorySnapshot() *routing.MissionControlSnapshot {
return nil
}

File diff suppressed because it is too large Load Diff

View File

@ -1651,6 +1651,12 @@ message QueryRoutesRequest {
self is assumed.
*/
string source_pub_key = 8;
/**
If set to true, edge probabilities from mission control will be used to get
the optimal route.
*/
bool use_mission_control = 9;
}
message EdgeLocator {

View File

@ -722,6 +722,14 @@
"in": "query",
"required": false,
"type": "string"
},
{
"name": "use_mission_control",
"description": "*\nIf set to true, edge probabilities from mission control will be used to get\nthe optimal route.",
"in": "query",
"required": false,
"type": "boolean",
"format": "boolean"
}
],
"tags": [

View File

@ -37,8 +37,8 @@ const (
// pathFinder defines the interface of a path finding algorithm.
type pathFinder = func(g *graphParams, r *RestrictParams,
source, target route.Vertex, amt lnwire.MilliSatoshi) (
[]*channeldb.ChannelEdgePolicy, error)
cfg *PathFindingConfig, source, target route.Vertex,
amt lnwire.MilliSatoshi) ([]*channeldb.ChannelEdgePolicy, error)
var (
// DefaultPaymentAttemptPenalty is the virtual cost in path finding weight
@ -261,7 +261,11 @@ type RestrictParams struct {
// ctlv. After path finding is complete, the caller needs to increase
// all cltv expiry heights with the required final cltv delta.
CltvLimit *uint32
}
// PathFindingConfig defines global parameters that control the trade-off in
// path finding between fees and probabiity.
type PathFindingConfig struct {
// PaymentAttemptPenalty is the virtual cost in path finding weight
// units of executing a payment attempt that fails. It is used to trade
// off potentially better routes against their probability of
@ -284,8 +288,9 @@ type RestrictParams struct {
// destination node back to source. This is to properly accumulate fees
// that need to be paid along the path and accurately check the amount
// to forward at every node against the available bandwidth.
func findPath(g *graphParams, r *RestrictParams, source, target route.Vertex,
amt lnwire.MilliSatoshi) ([]*channeldb.ChannelEdgePolicy, error) {
func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig,
source, target route.Vertex, amt lnwire.MilliSatoshi) (
[]*channeldb.ChannelEdgePolicy, error) {
var err error
tx := g.tx
@ -476,7 +481,7 @@ func findPath(g *graphParams, r *RestrictParams, source, target route.Vertex,
// If the probability is below the specified lower bound, we can
// abandon this direction. Adding further nodes can only lower
// the probability more.
if probability < r.MinProbability {
if probability < cfg.MinProbability {
return
}
@ -494,7 +499,8 @@ func findPath(g *graphParams, r *RestrictParams, source, target route.Vertex,
// Add an extra factor to the weight to take into account the
// probability.
tempDist := getProbabilityBasedDist(
tempWeight, probability, int64(r.PaymentAttemptPenalty),
tempWeight, probability,
int64(cfg.PaymentAttemptPenalty),
)
// If the current best route is better than this candidate

View File

@ -55,6 +55,8 @@ var (
FeeLimit: noFeeLimit,
ProbabilitySource: noProbabilitySource,
}
testPathFindingConfig = &PathFindingConfig{}
)
var (
@ -650,6 +652,7 @@ func TestFindLowestFeePath(t *testing.T) {
graph: testGraphInstance.graph,
},
noRestrictions,
testPathFindingConfig,
sourceNode.PubKeyBytes, target, paymentAmt,
)
if err != nil {
@ -791,6 +794,7 @@ func testBasicGraphPathFindingCase(t *testing.T, graphInstance *testGraphInstanc
FeeLimit: test.feeLimit,
ProbabilitySource: noProbabilitySource,
},
testPathFindingConfig,
sourceNode.PubKeyBytes, target, paymentAmt,
)
if test.expectFailureNoPath {
@ -957,7 +961,7 @@ func TestPathFindingWithAdditionalEdges(t *testing.T) {
graph: graph.graph,
additionalEdges: additionalEdges,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, doge.PubKeyBytes, paymentAmt,
)
if err != nil {
@ -1211,7 +1215,7 @@ func TestNewRoutePathTooLong(t *testing.T) {
&graphParams{
graph: graph.graph,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, paymentAmt,
)
if err != nil {
@ -1225,7 +1229,7 @@ func TestNewRoutePathTooLong(t *testing.T) {
&graphParams{
graph: graph.graph,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, paymentAmt,
)
if err == nil {
@ -1265,7 +1269,7 @@ func TestPathNotAvailable(t *testing.T) {
&graphParams{
graph: graph.graph,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, unknownNode, 100,
)
if !IsError(err, ErrNoPathFound) {
@ -1302,7 +1306,7 @@ func TestPathInsufficientCapacity(t *testing.T) {
&graphParams{
graph: graph.graph,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if !IsError(err, ErrNoPathFound) {
@ -1335,7 +1339,7 @@ func TestRouteFailMinHTLC(t *testing.T) {
&graphParams{
graph: graph.graph,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if !IsError(err, ErrNoPathFound) {
@ -1393,7 +1397,7 @@ func TestRouteFailMaxHTLC(t *testing.T) {
&graphParams{
graph: graph.graph,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if err != nil {
@ -1415,7 +1419,7 @@ func TestRouteFailMaxHTLC(t *testing.T) {
&graphParams{
graph: graph.graph,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if !IsError(err, ErrNoPathFound) {
@ -1450,7 +1454,7 @@ func TestRouteFailDisabledEdge(t *testing.T) {
&graphParams{
graph: graph.graph,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if err != nil {
@ -1478,7 +1482,7 @@ func TestRouteFailDisabledEdge(t *testing.T) {
&graphParams{
graph: graph.graph,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if err != nil {
@ -1503,7 +1507,7 @@ func TestRouteFailDisabledEdge(t *testing.T) {
&graphParams{
graph: graph.graph,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if !IsError(err, ErrNoPathFound) {
@ -1537,7 +1541,7 @@ func TestPathSourceEdgesBandwidth(t *testing.T) {
&graphParams{
graph: graph.graph,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if err != nil {
@ -1561,7 +1565,7 @@ func TestPathSourceEdgesBandwidth(t *testing.T) {
graph: graph.graph,
bandwidthHints: bandwidths,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if !IsError(err, ErrNoPathFound) {
@ -1579,7 +1583,7 @@ func TestPathSourceEdgesBandwidth(t *testing.T) {
graph: graph.graph,
bandwidthHints: bandwidths,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if err != nil {
@ -1610,7 +1614,7 @@ func TestPathSourceEdgesBandwidth(t *testing.T) {
graph: graph.graph,
bandwidthHints: bandwidths,
},
noRestrictions,
noRestrictions, testPathFindingConfig,
sourceNode.PubKeyBytes, target, payAmt,
)
if err != nil {
@ -1903,6 +1907,7 @@ func TestRestrictOutgoingChannel(t *testing.T) {
OutgoingChannelID: &outgoingChannelID,
ProbabilitySource: noProbabilitySource,
},
testPathFindingConfig,
sourceVertex, target, paymentAmt,
)
if err != nil {
@ -1998,6 +2003,7 @@ func testCltvLimit(t *testing.T, limit uint32, expectedChannel uint64) {
CltvLimit: cltvLimit,
ProbabilitySource: noProbabilitySource,
},
testPathFindingConfig,
sourceVertex, target, paymentAmt,
)
if expectedChannel == 0 {
@ -2174,8 +2180,10 @@ func testProbabilityRouting(t *testing.T, p10, p11, p20, minProbability float64,
graph: testGraphInstance.graph,
},
&RestrictParams{
FeeLimit: noFeeLimit,
ProbabilitySource: probabilitySource,
FeeLimit: noFeeLimit,
ProbabilitySource: probabilitySource,
},
&PathFindingConfig{
PaymentAttemptPenalty: lnwire.NewMSatFromSatoshis(10),
MinProbability: minProbability,
},

View File

@ -83,12 +83,10 @@ func (p *paymentSession) RequestRoute(payment *LightningPayment,
ss := p.sessionSource
restrictions := &RestrictParams{
ProbabilitySource: ss.MissionControl.GetEdgeProbability,
FeeLimit: payment.FeeLimit,
OutgoingChannelID: payment.OutgoingChannelID,
CltvLimit: cltvLimit,
PaymentAttemptPenalty: ss.PaymentAttemptPenalty,
MinProbability: ss.MinRouteProbability,
ProbabilitySource: ss.MissionControl.GetEdgeProbability,
FeeLimit: payment.FeeLimit,
OutgoingChannelID: payment.OutgoingChannelID,
CltvLimit: cltvLimit,
}
path, err := p.pathFinder(
@ -97,7 +95,8 @@ func (p *paymentSession) RequestRoute(payment *LightningPayment,
additionalEdges: p.additionalEdges,
bandwidthHints: p.bandwidthHints,
},
restrictions, ss.SelfNode.PubKeyBytes, payment.Target,
restrictions, &ss.PathFindingConfig,
ss.SelfNode.PubKeyBytes, payment.Target,
payment.Amount,
)
if err != nil {

View File

@ -38,15 +38,9 @@ type SessionSource struct {
// execution.
MissionControl MissionController
// PaymentAttemptPenalty is the virtual cost in path finding weight
// units of executing a payment attempt that fails. It is used to trade
// off potentially better routes against their probability of
// succeeding.
PaymentAttemptPenalty lnwire.MilliSatoshi
// MinProbability defines the minimum success probability of the
// returned route.
MinRouteProbability float64
// PathFindingConfig defines global parameters that control the
// trade-off in path finding between fees and probabiity.
PathFindingConfig PathFindingConfig
}
// NewPaymentSession creates a new payment session backed by the latest prune

View File

@ -14,8 +14,9 @@ func TestRequestRoute(t *testing.T) {
)
findPath := func(g *graphParams, r *RestrictParams,
source, target route.Vertex, amt lnwire.MilliSatoshi) (
[]*channeldb.ChannelEdgePolicy, error) {
cfg *PathFindingConfig, source, target route.Vertex,
amt lnwire.MilliSatoshi) ([]*channeldb.ChannelEdgePolicy,
error) {
// We expect find path to receive a cltv limit excluding the
// final cltv delta.

View File

@ -287,6 +287,9 @@ type Config struct {
// spentness of channel outpoints. For neutrino, this saves long rescans
// from blocking initial usage of the daemon.
AssumeChannelValid bool
// PathFindingConfig defines global path finding parameters.
PathFindingConfig PathFindingConfig
}
// routeTuple is an entry within the ChannelRouter's route cache. We cache
@ -1455,7 +1458,8 @@ func (r *ChannelRouter) FindRoute(source, target route.Vertex,
graph: r.cfg.Graph,
bandwidthHints: bandwidthHints,
},
restrictions, source, target, amt,
restrictions, &r.cfg.PathFindingConfig,
source, target, amt,
)
if err != nil {
return nil, err

View File

@ -90,11 +90,18 @@ func createTestCtxFromGraphInstance(startingHeight uint32, graphInstance *testGr
return nil, nil, err
}
pathFindingConfig := &PathFindingConfig{
MinProbability: 0.01,
PaymentAttemptPenalty: 100,
}
mcConfig := &MissionControlConfig{
PenaltyHalfLife: time.Hour,
AprioriHopProbability: 0.9,
}
mc := NewMissionControl(
&MissionControlConfig{
PenaltyHalfLife: time.Hour,
AprioriHopProbability: 0.9,
},
mcConfig,
)
sessionSource := &SessionSource{
@ -103,9 +110,11 @@ func createTestCtxFromGraphInstance(startingHeight uint32, graphInstance *testGr
QueryBandwidth: func(e *channeldb.ChannelEdgeInfo) lnwire.MilliSatoshi {
return lnwire.NewMSatFromSatoshis(e.Capacity)
},
MinRouteProbability: 0.01,
PaymentAttemptPenalty: 100,
MissionControl: mc,
PathFindingConfig: PathFindingConfig{
MinProbability: 0.01,
PaymentAttemptPenalty: 100,
},
MissionControl: mc,
}
router, err := New(Config{
@ -125,6 +134,7 @@ func createTestCtxFromGraphInstance(startingHeight uint32, graphInstance *testGr
next := atomic.AddUint64(&uniquePaymentID, 1)
return next, nil
},
PathFindingConfig: *pathFindingConfig,
})
if err != nil {
return nil, nil, fmt.Errorf("unable to create router %v", err)
@ -2163,6 +2173,7 @@ func TestFindPathFeeWeighting(t *testing.T) {
graph: ctx.graph,
},
noRestrictions,
testPathFindingConfig,
sourceNode.PubKeyBytes, target, amt,
)
if err != nil {

View File

@ -666,13 +666,17 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB,
int64(routingConfig.PaymentAttemptPenalty.ToSatoshis()),
routingConfig.MinRouteProbability)
paymentSessionSource := &routing.SessionSource{
Graph: chanGraph,
MissionControl: s.missionControl,
QueryBandwidth: queryBandwidth,
SelfNode: selfNode,
pathFindingConfig := routing.PathFindingConfig{
PaymentAttemptPenalty: routingConfig.PaymentAttemptPenalty,
MinRouteProbability: routingConfig.MinRouteProbability,
MinProbability: routingConfig.MinRouteProbability,
}
paymentSessionSource := &routing.SessionSource{
Graph: chanGraph,
MissionControl: s.missionControl,
QueryBandwidth: queryBandwidth,
SelfNode: selfNode,
PathFindingConfig: pathFindingConfig,
}
paymentControl := channeldb.NewPaymentControl(chanDB)
@ -692,6 +696,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB,
QueryBandwidth: queryBandwidth,
AssumeChannelValid: cfg.Routing.UseAssumeChannelValid(),
NextPaymentID: sequencer.NextID,
PathFindingConfig: pathFindingConfig,
})
if err != nil {
return nil, fmt.Errorf("can't create router: %v", err)