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.
This commit is contained in:
Elle Mouton
2025-02-05 11:42:13 +02:00
parent 3e5d807773
commit 14cedef58e
2 changed files with 84 additions and 0 deletions

View File

@@ -4717,6 +4717,65 @@ func deserializeChanEdgePolicyRaw(r io.Reader) (*models.ChannelEdgePolicy,
return edge, nil
}
// chanGraphNodeTx is an implementation of the NodeRTx interface backed by the
// ChannelGraph and a kvdb.RTx.
type chanGraphNodeTx struct {
tx kvdb.RTx
db *ChannelGraph
node *models.LightningNode
}
// A compile-time constraint to ensure chanGraphNodeTx implements the NodeRTx
// interface.
var _ NodeRTx = (*chanGraphNodeTx)(nil)
func newChanGraphNodeTx(tx kvdb.RTx, db *ChannelGraph,
node *models.LightningNode) *chanGraphNodeTx {
return &chanGraphNodeTx{
tx: tx,
db: db,
node: node,
}
}
// Node returns the raw information of the node.
//
// NOTE: This is a part of the NodeRTx interface.
func (c *chanGraphNodeTx) Node() *models.LightningNode {
return c.node
}
// FetchNode fetches the node with the given pub key under the same transaction
// used to fetch the current node. The returned node is also a NodeRTx and any
// operations on that NodeRTx will also be done under the same transaction.
//
// NOTE: This is a part of the NodeRTx interface.
func (c *chanGraphNodeTx) FetchNode(nodePub route.Vertex) (NodeRTx, error) {
node, err := c.db.FetchLightningNodeTx(c.tx, nodePub)
if err != nil {
return nil, err
}
return newChanGraphNodeTx(c.tx, c.db, node), nil
}
// ForEachChannel can be used to iterate over the node's channels under
// the same transaction used to fetch the node.
//
// NOTE: This is a part of the NodeRTx interface.
func (c *chanGraphNodeTx) ForEachChannel(f func(*models.ChannelEdgeInfo,
*models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
return c.db.ForEachNodeChannelTx(c.tx, c.node.PubKeyBytes,
func(_ kvdb.RTx, info *models.ChannelEdgeInfo, policy1,
policy2 *models.ChannelEdgePolicy) error {
return f(info, policy1, policy2)
},
)
}
// MakeTestGraph creates a new instance of the ChannelGraph for testing
// purposes.
func MakeTestGraph(t testing.TB, modifiers ...OptionModifier) (*ChannelGraph,

25
graph/db/interfaces.go Normal file
View File

@@ -0,0 +1,25 @@
package graphdb
import (
"github.com/lightningnetwork/lnd/graph/db/models"
"github.com/lightningnetwork/lnd/routing/route"
)
// NodeRTx represents transaction object with an underlying node associated that
// can be used to make further queries to the graph under the same transaction.
// This is useful for consistency during graph traversal and queries.
type NodeRTx interface {
// Node returns the raw information of the node.
Node() *models.LightningNode
// ForEachChannel can be used to iterate over the node's channels under
// the same transaction used to fetch the node.
ForEachChannel(func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy) error) error
// FetchNode fetches the node with the given pub key under the same
// transaction used to fetch the current node. The returned node is also
// a NodeRTx and any operations on that NodeRTx will also be done under
// the same transaction.
FetchNode(node route.Vertex) (NodeRTx, error)
}