mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-12-08 11:53:16 +01:00
multi: remove the need for the graphsession package
In this commit, we add a `GraphSession` method to the `ChannelGraph`. This method provides a caller with access to a `NodeTraverser`. This is used by pathfinding to create a graph "session" overwhich to perform a set of queries for a pathfinding attempt. With this refactor, we hide details such as DB transaction creation and transaction commits from the caller. So with this, pathfinding does not need to remember to "close the graph session". With this commit, the `graphsession` package may be completely removed.
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/lightningnetwork/lnd/fn/v2"
|
||||
graphdb "github.com/lightningnetwork/lnd/graph/db"
|
||||
"github.com/lightningnetwork/lnd/kvdb"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
"github.com/lightningnetwork/lnd/routing/route"
|
||||
@@ -211,7 +210,7 @@ func (c *integratedRoutingContext) testPayment(maxParts uint32,
|
||||
|
||||
session, err := newPaymentSession(
|
||||
&payment, c.graph.source.pubkey, getBandwidthHints,
|
||||
newMockGraphSessionFactory(c.graph), mc, c.pathFindingCfg,
|
||||
c.graph, mc, c.pathFindingCfg,
|
||||
)
|
||||
if err != nil {
|
||||
c.t.Fatal(err)
|
||||
@@ -317,88 +316,3 @@ func getNodeIndex(route *route.Route, failureSource route.Vertex) *int {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type mockGraphSessionFactory struct {
|
||||
Graph
|
||||
}
|
||||
|
||||
func newMockGraphSessionFactory(graph Graph) GraphSessionFactory {
|
||||
return &mockGraphSessionFactory{Graph: graph}
|
||||
}
|
||||
|
||||
func (m *mockGraphSessionFactory) NewGraphSession() (Graph, func() error,
|
||||
error) {
|
||||
|
||||
return m, func() error {
|
||||
return nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
var _ GraphSessionFactory = (*mockGraphSessionFactory)(nil)
|
||||
var _ Graph = (*mockGraphSessionFactory)(nil)
|
||||
|
||||
type mockGraphSessionFactoryChanDB struct {
|
||||
graph *graphdb.ChannelGraph
|
||||
}
|
||||
|
||||
func newMockGraphSessionFactoryFromChanDB(
|
||||
graph *graphdb.ChannelGraph) *mockGraphSessionFactoryChanDB {
|
||||
|
||||
return &mockGraphSessionFactoryChanDB{
|
||||
graph: graph,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *mockGraphSessionFactoryChanDB) NewGraphSession() (Graph, func() error,
|
||||
error) {
|
||||
|
||||
tx, err := g.graph.NewPathFindTx()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
session := &mockGraphSessionChanDB{
|
||||
graph: g.graph,
|
||||
tx: tx,
|
||||
}
|
||||
|
||||
return session, session.close, nil
|
||||
}
|
||||
|
||||
var _ GraphSessionFactory = (*mockGraphSessionFactoryChanDB)(nil)
|
||||
|
||||
type mockGraphSessionChanDB struct {
|
||||
graph *graphdb.ChannelGraph
|
||||
tx kvdb.RTx
|
||||
}
|
||||
|
||||
func newMockGraphSessionChanDB(graph *graphdb.ChannelGraph) Graph {
|
||||
return &mockGraphSessionChanDB{
|
||||
graph: graph,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *mockGraphSessionChanDB) close() error {
|
||||
if g.tx == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := g.tx.Rollback()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error closing db tx: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *mockGraphSessionChanDB) ForEachNodeDirectedChannel(nodePub route.Vertex,
|
||||
cb func(channel *graphdb.DirectedChannel) error) error {
|
||||
|
||||
return g.graph.ForEachNodeDirectedChannelTx(g.tx, nodePub, cb)
|
||||
}
|
||||
|
||||
func (g *mockGraphSessionChanDB) FetchNodeFeatures(nodePub route.Vertex) (
|
||||
*lnwire.FeatureVector, error) {
|
||||
|
||||
return g.graph.FetchNodeFeaturesTx(g.tx, nodePub)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user