routing+refactor: remove the need to give CachedGraph source node access

In preparation for the next commit.
This commit is contained in:
Elle Mouton committed 2024-07-15 13:10:24 +02:00
1 parent 3f121cbe81
commit 90d6b863a8
6 files changed
+15 -46

No files matched your search

+8 -12
View File
@@ -25,9 +25,8 @@ type Graph interface {
// CachedGraph is a Graph implementation that retrieves from the // CachedGraph is a Graph implementation that retrieves from the
// database. // database.
type CachedGraph struct { type CachedGraph struct {
graph *channeldb.ChannelGraph graph *channeldb.ChannelGraph
tx kvdb.RTx tx kvdb.RTx
source route.Vertex
} }
// A compile time assertion to make sure CachedGraph implements the Graph // A compile time assertion to make sure CachedGraph implements the Graph
@@ -36,18 +35,15 @@ var _ Graph = (*CachedGraph)(nil)
// NewCachedGraph instantiates a new db-connected routing graph. It implicitly // NewCachedGraph instantiates a new db-connected routing graph. It implicitly
// instantiates a new read transaction. // instantiates a new read transaction.
func NewCachedGraph(sourceNode *channeldb.LightningNode, func NewCachedGraph(graph *channeldb.ChannelGraph) (*CachedGraph, error) {
graph *channeldb.ChannelGraph) (*CachedGraph, error) {
tx, err := graph.NewPathFindTx() tx, err := graph.NewPathFindTx()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &CachedGraph{ return &CachedGraph{
graph: graph, graph: graph,
tx: tx, tx: tx,
source: sourceNode.PubKeyBytes,
}, nil }, nil
} }
@@ -82,16 +78,16 @@ func (g *CachedGraph) FetchNodeFeatures(nodePub route.Vertex) (
// FetchAmountPairCapacity determines the maximal public capacity between two // FetchAmountPairCapacity determines the maximal public capacity between two
// nodes depending on the amount we try to send. // nodes depending on the amount we try to send.
func (g *CachedGraph) FetchAmountPairCapacity(nodeFrom, nodeTo route.Vertex, func FetchAmountPairCapacity(graph Graph, source, nodeFrom, nodeTo route.Vertex,
amount lnwire.MilliSatoshi) (btcutil.Amount, error) { amount lnwire.MilliSatoshi) (btcutil.Amount, error) {
// Create unified edges for all incoming connections. // Create unified edges for all incoming connections.
// //
// Note: Inbound fees are not used here because this method is only used // Note: Inbound fees are not used here because this method is only used
// by a deprecated router rpc. // by a deprecated router rpc.
u := newNodeEdgeUnifier(g.source, nodeTo, false, nil) u := newNodeEdgeUnifier(source, nodeTo, false, nil)
err := u.addGraphPolicies(g) err := u.addGraphPolicies(graph)
if err != nil { if err != nil {
return 0, err return 0, err
} }
-25
View File
@@ -227,31 +227,6 @@ func (m *mockGraph) FetchNodeFeatures(nodePub route.Vertex) (
return lnwire.EmptyFeatureVector(), nil return lnwire.EmptyFeatureVector(), nil
} }
// FetchAmountPairCapacity returns the maximal capacity between nodes in the
// graph.
//
// NOTE: Part of the Graph 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 // htlcResult describes the resolution of an htlc. If failure is nil, the htlc
// was settled. // was settled.
type htlcResult struct { type htlcResult struct {
+1 -1
View File
@@ -3201,7 +3201,7 @@ func dbFindPath(graph *channeldb.ChannelGraph,
return nil, err return nil, err
} }
routingGraph, err := NewCachedGraph(sourceNode, graph) routingGraph, err := NewCachedGraph(graph)
if err != nil { if err != nil {
return nil, err return nil, err
} }
+1 -1
View File
@@ -47,7 +47,7 @@ type SessionSource struct {
// getRoutingGraph returns a routing graph and a clean-up function for // getRoutingGraph returns a routing graph and a clean-up function for
// pathfinding. // pathfinding.
func (m *SessionSource) getRoutingGraph() (Graph, func(), error) { func (m *SessionSource) getRoutingGraph() (Graph, func(), error) {
routingTx, err := NewCachedGraph(m.SourceNode, m.Graph) routingTx, err := NewCachedGraph(m.Graph)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
+1 -2
View File
@@ -517,8 +517,7 @@ func New(cfg Config) (*ChannelRouter, error) {
r := &ChannelRouter{ r := &ChannelRouter{
cfg: &cfg, cfg: &cfg,
cachedGraph: &CachedGraph{ cachedGraph: &CachedGraph{
graph: cfg.Graph, graph: cfg.Graph,
source: selfNode.PubKeyBytes,
}, },
networkUpdates: make(chan *routingMsg), networkUpdates: make(chan *routingMsg),
topologyClients: &lnutils.SyncMap[uint64, *topologyClient]{}, topologyClients: &lnutils.SyncMap[uint64, *topologyClient]{},
+4 -5
View File
@@ -691,9 +691,7 @@ func (r *rpcServer) addDeps(s *server, macService *macaroons.Service,
FetchAmountPairCapacity: func(nodeFrom, nodeTo route.Vertex, FetchAmountPairCapacity: func(nodeFrom, nodeTo route.Vertex,
amount lnwire.MilliSatoshi) (btcutil.Amount, error) { amount lnwire.MilliSatoshi) (btcutil.Amount, error) {
routingGraph, err := routing.NewCachedGraph( routingGraph, err := routing.NewCachedGraph(graph)
selfNode, graph,
)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@@ -706,8 +704,9 @@ func (r *rpcServer) addDeps(s *server, macService *macaroons.Service,
} }
}() }()
return routingGraph.FetchAmountPairCapacity( return routing.FetchAmountPairCapacity(
nodeFrom, nodeTo, amount, routingGraph, selfNode.PubKeyBytes, nodeFrom,
nodeTo, amount,
) )
}, },
FetchChannelEndpoints: func(chanID uint64) (route.Vertex, FetchChannelEndpoints: func(chanID uint64) (route.Vertex,