routing+routerrpc: add capacity in rpcs
FetchPairCapacity is used by the following endpoints to introduce the capacity for probability calculations: * QueryProbability * QueryRoutes
This commit is contained in:
9 files changed
+117
-34
No files matched your search
@@ -45,6 +45,11 @@ type RouterBackend struct {
|
||||
// capacity of a channel to populate in responses.
|
||||
FetchChannelCapacity func(chanID uint64) (btcutil.Amount, error)
|
||||
|
||||
// FetchAmountPairCapacity determines the maximal channel capacity
|
||||
// between two nodes given a certain amount.
|
||||
FetchAmountPairCapacity func(nodeFrom, nodeTo route.Vertex,
|
||||
amount lnwire.MilliSatoshi) (btcutil.Amount, error)
|
||||
|
||||
// FetchChannelEndpoints returns the pubkeys of both endpoints of the
|
||||
// given channel id.
|
||||
FetchChannelEndpoints func(chanID uint64) (route.Vertex,
|
||||
@@ -325,7 +330,7 @@ func (r *RouterBackend) QueryRoutes(ctx context.Context,
|
||||
// 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.
|
||||
route, _, err := r.FindRoute(
|
||||
route, successProb, err := r.FindRoute(
|
||||
sourcePubKey, targetPubKey, amt, in.TimePref, restrictions,
|
||||
customRecords, routeHintEdges, finalCLTVDelta,
|
||||
)
|
||||
@@ -340,11 +345,6 @@ func (r *RouterBackend) QueryRoutes(ctx context.Context,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Calculate route success probability. Do not rely on a probability
|
||||
// that could have been returned from path finding, because mission
|
||||
// control may have been disabled in the provided ProbabilitySource.
|
||||
successProb := r.getSuccessProbability(route)
|
||||
|
||||
routeResp := &lnrpc.QueryRoutesResponse{
|
||||
Routes: []*lnrpc.Route{rpcRoute},
|
||||
SuccessProb: successProb,
|
||||
@@ -353,28 +353,6 @@ func (r *RouterBackend) QueryRoutes(ctx context.Context,
|
||||
return routeResp, nil
|
||||
}
|
||||
|
||||
// getSuccessProbability returns the success probability for the given route
|
||||
// based on the current state of mission control.
|
||||
func (r *RouterBackend) getSuccessProbability(rt *route.Route) float64 {
|
||||
fromNode := rt.SourcePubKey
|
||||
amtToFwd := rt.TotalAmount
|
||||
successProb := 1.0
|
||||
for _, hop := range rt.Hops {
|
||||
toNode := hop.PubKeyBytes
|
||||
|
||||
probability := r.MissionControl.GetProbability(
|
||||
fromNode, toNode, amtToFwd, 0,
|
||||
)
|
||||
|
||||
successProb *= probability
|
||||
|
||||
amtToFwd = hop.AmtToForward
|
||||
fromNode = toNode
|
||||
}
|
||||
|
||||
return successProb
|
||||
}
|
||||
|
||||
// rpcEdgeToPair looks up the provided channel and returns the channel endpoints
|
||||
// as a directed pair.
|
||||
func (r *RouterBackend) rpcEdgeToPair(e *lnrpc.EdgeLocator) (
|
||||
|
||||
@@ -200,6 +200,11 @@ func testQueryRoutes(t *testing.T, useMissionControl bool, useMsat bool,
|
||||
|
||||
return 1, nil
|
||||
},
|
||||
FetchAmountPairCapacity: func(nodeFrom, nodeTo route.Vertex,
|
||||
amount lnwire.MilliSatoshi) (btcutil.Amount, error) {
|
||||
|
||||
return 1, nil
|
||||
},
|
||||
MissionControl: &mockMissionControl{},
|
||||
FetchChannelEndpoints: func(chanID uint64) (route.Vertex,
|
||||
route.Vertex, error) {
|
||||
|
||||
@@ -709,8 +709,22 @@ func (s *Server) QueryProbability(ctx context.Context,
|
||||
|
||||
amt := lnwire.MilliSatoshi(req.AmtMsat)
|
||||
|
||||
// Compute the probability.
|
||||
var prob float64
|
||||
mc := s.cfg.RouterBackend.MissionControl
|
||||
prob := mc.GetProbability(fromNode, toNode, amt, 0)
|
||||
capacity, err := s.cfg.RouterBackend.FetchAmountPairCapacity(
|
||||
fromNode, toNode, amt,
|
||||
)
|
||||
|
||||
// If we cannot query the capacity this means that either we don't have
|
||||
// information available or that the channel fails min/maxHtlc
|
||||
// constraints, so we return a zero probability.
|
||||
if err != nil {
|
||||
log.Errorf("Cannot fetch capacity: %v", err)
|
||||
} else {
|
||||
prob = mc.GetProbability(fromNode, toNode, amt, capacity)
|
||||
}
|
||||
|
||||
history := mc.GetPairHistorySnapshot(fromNode, toNode)
|
||||
|
||||
return &QueryProbabilityResponse{
|
||||
|
||||
Reference in new issue
Block a user