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
+40
-2
@@ -1,6 +1,9 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/btcsuite/btcd/btcutil"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/kvdb"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
@@ -20,6 +23,11 @@ type routingGraph interface {
|
||||
|
||||
// fetchNodeFeatures returns the features of the given node.
|
||||
fetchNodeFeatures(nodePub route.Vertex) (*lnwire.FeatureVector, error)
|
||||
|
||||
// FetchAmountPairCapacity determines the maximal capacity between two
|
||||
// pairs of nodes.
|
||||
FetchAmountPairCapacity(nodeFrom, nodeTo route.Vertex,
|
||||
amount lnwire.MilliSatoshi) (btcutil.Amount, error)
|
||||
}
|
||||
|
||||
// CachedGraph is a routingGraph implementation that retrieves from the
|
||||
@@ -51,9 +59,9 @@ func NewCachedGraph(sourceNode *channeldb.LightningNode,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// close attempts to close the underlying db transaction. This is a no-op in
|
||||
// Close attempts to close the underlying db transaction. This is a no-op in
|
||||
// case the underlying graph uses an in-memory cache.
|
||||
func (g *CachedGraph) close() error {
|
||||
func (g *CachedGraph) Close() error {
|
||||
if g.tx == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -86,3 +94,33 @@ func (g *CachedGraph) fetchNodeFeatures(nodePub route.Vertex) (
|
||||
|
||||
return g.graph.FetchNodeFeatures(nodePub)
|
||||
}
|
||||
|
||||
// FetchAmountPairCapacity determines the maximal public capacity between two
|
||||
// nodes depending on the amount we try to send.
|
||||
//
|
||||
// NOTE: Part of the routingGraph interface.
|
||||
func (g *CachedGraph) FetchAmountPairCapacity(nodeFrom, nodeTo route.Vertex,
|
||||
amount lnwire.MilliSatoshi) (btcutil.Amount, error) {
|
||||
|
||||
// Create unified edges for all incoming connections.
|
||||
u := newNodeEdgeUnifier(g.sourceNode(), nodeTo, nil)
|
||||
|
||||
err := u.addGraphPolicies(g)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
edgeUnifier, ok := u.edgeUnifiers[nodeFrom]
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("no edge info for node pair %v -> %v",
|
||||
nodeFrom, nodeTo)
|
||||
}
|
||||
|
||||
edge := edgeUnifier.getEdgeNetwork(amount)
|
||||
if edge == nil {
|
||||
return 0, fmt.Errorf("no edge for node pair %v -> %v "+
|
||||
"(amount %v)", nodeFrom, nodeTo, amount)
|
||||
}
|
||||
|
||||
return edge.capacity, nil
|
||||
}
|
||||
@@ -226,6 +226,31 @@ func (m *mockGraph) fetchNodeFeatures(nodePub route.Vertex) (
|
||||
return lnwire.EmptyFeatureVector(), nil
|
||||
}
|
||||
|
||||
// FetchAmountPairCapacity returns the maximal capacity between nodes in the
|
||||
// graph.
|
||||
//
|
||||
// NOTE: Part of the routingGraph interface.
|
||||
func (m *mockGraph) FetchAmountPairCapacity(nodeFrom, nodeTo route.Vertex,
|
||||
amount lnwire.MilliSatoshi) (btcutil.Amount, error) {
|
||||
|
||||
var capacity btcutil.Amount
|
||||
|
||||
cb := func(channel *channeldb.DirectedChannel) error {
|
||||
if channel.OtherNode == nodeTo {
|
||||
capacity = channel.Capacity
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
err := m.forEachNodeChannel(nodeFrom, cb)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return capacity, nil
|
||||
}
|
||||
|
||||
// htlcResult describes the resolution of an htlc. If failure is nil, the htlc
|
||||
// was settled.
|
||||
type htlcResult struct {
|
||||
|
||||
@@ -3112,7 +3112,7 @@ func dbFindPath(graph *channeldb.ChannelGraph,
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := routingGraph.close(); err != nil {
|
||||
if err := routingGraph.Close(); err != nil {
|
||||
log.Errorf("Error closing db tx: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -51,7 +51,7 @@ func (m *SessionSource) getRoutingGraph() (routingGraph, func(), error) {
|
||||
return nil, nil, err
|
||||
}
|
||||
return routingTx, func() {
|
||||
err := routingTx.close()
|
||||
err := routingTx.Close()
|
||||
if err != nil {
|
||||
log.Errorf("Error closing db tx: %v", err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user