Skip to content

Commit 9068ffc

Browse files
committed
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.
1 parent b08bc99 commit 9068ffc

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

graph/db/graph.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,12 @@ func (c *ChannelGraph) ForEachChannel(cb func(*models.ChannelEdgeInfo,
503503
// ForEachNodeDirectedChannel iterates through all channels of a given node,
504504
// executing the passed callback on the directed edge representing the channel
505505
// and its incoming policy. If the callback returns an error, then the iteration
506-
// is halted with the error propagated back up to the caller.
506+
// is halted with the error propagated back up to the caller. An optional read
507+
// transaction may be provided. If none is provided, a new one will be created.
507508
//
508509
// Unknown policies are passed into the callback as nil values.
510+
//
511+
// NOTE: this is part of the graphsession.graph interface.
509512
func (c *ChannelGraph) ForEachNodeDirectedChannel(tx kvdb.RTx,
510513
node route.Vertex, cb func(channel *DirectedChannel) error) error {
511514

@@ -517,7 +520,7 @@ func (c *ChannelGraph) ForEachNodeDirectedChannel(tx kvdb.RTx,
517520
toNodeCallback := func() route.Vertex {
518521
return node
519522
}
520-
toNodeFeatures, err := c.FetchNodeFeatures(node)
523+
toNodeFeatures, err := c.FetchNodeFeatures(tx, node)
521524
if err != nil {
522525
return err
523526
}
@@ -562,16 +565,19 @@ func (c *ChannelGraph) ForEachNodeDirectedChannel(tx kvdb.RTx,
562565
}
563566

564567
// FetchNodeFeatures returns the features of a given node. If no features are
565-
// known for the node, an empty feature vector is returned.
566-
func (c *ChannelGraph) FetchNodeFeatures(
568+
// known for the node, an empty feature vector is returned. An optional read
569+
// transaction may be provided. If none is provided, a new one will be created.
570+
//
571+
// NOTE: this is part of the graphsession.graph interface.
572+
func (c *ChannelGraph) FetchNodeFeatures(tx kvdb.RTx,
567573
node route.Vertex) (*lnwire.FeatureVector, error) {
568574

569575
if c.graphCache != nil {
570576
return c.graphCache.GetFeatures(node), nil
571577
}
572578

573579
// Fallback that uses the database.
574-
targetNode, err := c.FetchLightningNode(node)
580+
targetNode, err := c.FetchLightningNodeTx(tx, node)
575581
switch err {
576582
// If the node exists and has features, return them directly.
577583
case nil:
@@ -618,7 +624,7 @@ func (c *ChannelGraph) ForEachNodeCached(cb func(node route.Vertex,
618624
return node.PubKeyBytes
619625
}
620626
toNodeFeatures, err := c.FetchNodeFeatures(
621-
node.PubKeyBytes,
627+
tx, node.PubKeyBytes,
622628
)
623629
if err != nil {
624630
return err

graph/graphsession/graph_session.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (g *session) ForEachNodeChannel(nodePub route.Vertex,
9696
func (g *session) FetchNodeFeatures(nodePub route.Vertex) (
9797
*lnwire.FeatureVector, error) {
9898

99-
return g.graph.FetchNodeFeatures(nodePub)
99+
return g.graph.FetchNodeFeatures(g.tx, nodePub)
100100
}
101101

102102
// A compile-time check to ensure that *session implements the
@@ -133,7 +133,11 @@ type graph interface {
133133

134134
// FetchNodeFeatures returns the features of a given node. If no
135135
// features are known for the node, an empty feature vector is returned.
136-
FetchNodeFeatures(node route.Vertex) (*lnwire.FeatureVector, error)
136+
//
137+
// NOTE: if a nil tx is provided, then it is expected that the
138+
// implementation create a read only tx.
139+
FetchNodeFeatures(tx kvdb.RTx, node route.Vertex) (
140+
*lnwire.FeatureVector, error)
137141
}
138142

139143
// A compile-time check to ensure that *channeldb.ChannelGraph implements the

routing/integrated_routing_context_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,5 +400,5 @@ func (g *mockGraphSessionChanDB) ForEachNodeChannel(nodePub route.Vertex,
400400
func (g *mockGraphSessionChanDB) FetchNodeFeatures(nodePub route.Vertex) (
401401
*lnwire.FeatureVector, error) {
402402

403-
return g.graph.FetchNodeFeatures(nodePub)
403+
return g.graph.FetchNodeFeatures(g.tx, nodePub)
404404
}

0 commit comments

Comments
 (0)