Skip to content

Commit 14cedef

Browse files
committed
graph/db: add NodeRTx interface and implement it
In this commit, a new NodeRTx interface is added which represents consistent access to a persisted models.LightningNode. The ForEachChannel method of the interface gives the caller access to the node's channels under the same read transaction (if any) that was used to fetch the node in the first place. The FetchNode method returns another NodeRTx which again will have the same underlying read transaction. The main point of this interface is to provide this consistent access without needing to expose the `kvdb.RTx` type as a method parameter. This will then make it much easier in future to add new implementations of this interface that are backed by other databases (or RPC connections) where the `kvdb.RTx` type does not apply. We will make use of the new interface in the `autopilot` package in upcoming commits in order to remove the `autopilot`'s dependence on the pointer to the `*graphdb.ChannelGraph` which it has today.
1 parent 3e5d807 commit 14cedef

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

graph/db/graph.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4717,6 +4717,65 @@ func deserializeChanEdgePolicyRaw(r io.Reader) (*models.ChannelEdgePolicy,
47174717
return edge, nil
47184718
}
47194719

4720+
// chanGraphNodeTx is an implementation of the NodeRTx interface backed by the
4721+
// ChannelGraph and a kvdb.RTx.
4722+
type chanGraphNodeTx struct {
4723+
tx kvdb.RTx
4724+
db *ChannelGraph
4725+
node *models.LightningNode
4726+
}
4727+
4728+
// A compile-time constraint to ensure chanGraphNodeTx implements the NodeRTx
4729+
// interface.
4730+
var _ NodeRTx = (*chanGraphNodeTx)(nil)
4731+
4732+
func newChanGraphNodeTx(tx kvdb.RTx, db *ChannelGraph,
4733+
node *models.LightningNode) *chanGraphNodeTx {
4734+
4735+
return &chanGraphNodeTx{
4736+
tx: tx,
4737+
db: db,
4738+
node: node,
4739+
}
4740+
}
4741+
4742+
// Node returns the raw information of the node.
4743+
//
4744+
// NOTE: This is a part of the NodeRTx interface.
4745+
func (c *chanGraphNodeTx) Node() *models.LightningNode {
4746+
return c.node
4747+
}
4748+
4749+
// FetchNode fetches the node with the given pub key under the same transaction
4750+
// used to fetch the current node. The returned node is also a NodeRTx and any
4751+
// operations on that NodeRTx will also be done under the same transaction.
4752+
//
4753+
// NOTE: This is a part of the NodeRTx interface.
4754+
func (c *chanGraphNodeTx) FetchNode(nodePub route.Vertex) (NodeRTx, error) {
4755+
node, err := c.db.FetchLightningNodeTx(c.tx, nodePub)
4756+
if err != nil {
4757+
return nil, err
4758+
}
4759+
4760+
return newChanGraphNodeTx(c.tx, c.db, node), nil
4761+
}
4762+
4763+
// ForEachChannel can be used to iterate over the node's channels under
4764+
// the same transaction used to fetch the node.
4765+
//
4766+
// NOTE: This is a part of the NodeRTx interface.
4767+
func (c *chanGraphNodeTx) ForEachChannel(f func(*models.ChannelEdgeInfo,
4768+
*models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
4769+
4770+
return c.db.ForEachNodeChannelTx(c.tx, c.node.PubKeyBytes,
4771+
func(_ kvdb.RTx, info *models.ChannelEdgeInfo, policy1,
4772+
policy2 *models.ChannelEdgePolicy) error {
4773+
4774+
return f(info, policy1, policy2)
4775+
},
4776+
)
4777+
}
4778+
47204779
// MakeTestGraph creates a new instance of the ChannelGraph for testing
47214780
// purposes.
47224781
func MakeTestGraph(t testing.TB, modifiers ...OptionModifier) (*ChannelGraph,

graph/db/interfaces.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package graphdb
2+
3+
import (
4+
"github.com/lightningnetwork/lnd/graph/db/models"
5+
"github.com/lightningnetwork/lnd/routing/route"
6+
)
7+
8+
// NodeRTx represents transaction object with an underlying node associated that
9+
// can be used to make further queries to the graph under the same transaction.
10+
// This is useful for consistency during graph traversal and queries.
11+
type NodeRTx interface {
12+
// Node returns the raw information of the node.
13+
Node() *models.LightningNode
14+
15+
// ForEachChannel can be used to iterate over the node's channels under
16+
// the same transaction used to fetch the node.
17+
ForEachChannel(func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
18+
*models.ChannelEdgePolicy) error) error
19+
20+
// FetchNode fetches the node with the given pub key under the same
21+
// transaction used to fetch the current node. The returned node is also
22+
// a NodeRTx and any operations on that NodeRTx will also be done under
23+
// the same transaction.
24+
FetchNode(node route.Vertex) (NodeRTx, error)
25+
}

0 commit comments

Comments
 (0)