graph: let FetchNodeFeatures take an optional read tx

For consistency in the graphsessoin.graph interface, we let the
FetchNodeFeatures method take a read transaction just like the
ForEachNodeDirectedChannel. This is nice because then all calls in the
same pathfinding transaction use the same read transaction.
This commit is contained in:
Elle Mouton 2024-11-08 15:49:45 +02:00
parent b08bc99945
commit 9068ffcd8b
No known key found for this signature in database
GPG key ID: D7D916376026F177
3 changed files with 19 additions and 9 deletions

View file

@ -503,9 +503,12 @@ func (c *ChannelGraph) ForEachChannel(cb func(*models.ChannelEdgeInfo,
// ForEachNodeDirectedChannel iterates through all channels of a given node,
// executing the passed callback on the directed edge representing the channel
// and its incoming policy. If the callback returns an error, then the iteration
// is halted with the error propagated back up to the caller.
// is halted with the error propagated back up to the caller. An optional read
// transaction may be provided. If none is provided, a new one will be created.
//
// Unknown policies are passed into the callback as nil values.
//
// NOTE: this is part of the graphsession.graph interface.
func (c *ChannelGraph) ForEachNodeDirectedChannel(tx kvdb.RTx,
node route.Vertex, cb func(channel *DirectedChannel) error) error {
@ -517,7 +520,7 @@ func (c *ChannelGraph) ForEachNodeDirectedChannel(tx kvdb.RTx,
toNodeCallback := func() route.Vertex {
return node
}
toNodeFeatures, err := c.FetchNodeFeatures(node)
toNodeFeatures, err := c.FetchNodeFeatures(tx, node)
if err != nil {
return err
}
@ -562,8 +565,11 @@ func (c *ChannelGraph) ForEachNodeDirectedChannel(tx kvdb.RTx,
}
// FetchNodeFeatures returns the features of a given node. If no features are
// known for the node, an empty feature vector is returned.
func (c *ChannelGraph) FetchNodeFeatures(
// known for the node, an empty feature vector is returned. An optional read
// transaction may be provided. If none is provided, a new one will be created.
//
// NOTE: this is part of the graphsession.graph interface.
func (c *ChannelGraph) FetchNodeFeatures(tx kvdb.RTx,
node route.Vertex) (*lnwire.FeatureVector, error) {
if c.graphCache != nil {
@ -571,7 +577,7 @@ func (c *ChannelGraph) FetchNodeFeatures(
}
// Fallback that uses the database.
targetNode, err := c.FetchLightningNode(node)
targetNode, err := c.FetchLightningNodeTx(tx, node)
switch err {
// If the node exists and has features, return them directly.
case nil:
@ -618,7 +624,7 @@ func (c *ChannelGraph) ForEachNodeCached(cb func(node route.Vertex,
return node.PubKeyBytes
}
toNodeFeatures, err := c.FetchNodeFeatures(
node.PubKeyBytes,
tx, node.PubKeyBytes,
)
if err != nil {
return err

View file

@ -96,7 +96,7 @@ func (g *session) ForEachNodeChannel(nodePub route.Vertex,
func (g *session) FetchNodeFeatures(nodePub route.Vertex) (
*lnwire.FeatureVector, error) {
return g.graph.FetchNodeFeatures(nodePub)
return g.graph.FetchNodeFeatures(g.tx, nodePub)
}
// A compile-time check to ensure that *session implements the
@ -133,7 +133,11 @@ type graph interface {
// FetchNodeFeatures returns the features of a given node. If no
// features are known for the node, an empty feature vector is returned.
FetchNodeFeatures(node route.Vertex) (*lnwire.FeatureVector, error)
//
// NOTE: if a nil tx is provided, then it is expected that the
// implementation create a read only tx.
FetchNodeFeatures(tx kvdb.RTx, node route.Vertex) (
*lnwire.FeatureVector, error)
}
// A compile-time check to ensure that *channeldb.ChannelGraph implements the

View file

@ -400,5 +400,5 @@ func (g *mockGraphSessionChanDB) ForEachNodeChannel(nodePub route.Vertex,
func (g *mockGraphSessionChanDB) FetchNodeFeatures(nodePub route.Vertex) (
*lnwire.FeatureVector, error) {
return g.graph.FetchNodeFeatures(nodePub)
return g.graph.FetchNodeFeatures(g.tx, nodePub)
}