multi: let ForEachChannel be a method on ChannelGraph

Having a `ForEachChannel` method on the `LightningNode` struct itself
results in a `kvdb.Backend` object needing to be stored within the
LightningNode struct. In this commit, this method is replaced with a
`ForEachNodeChannel` method on the `ChannelGraph` struct will perform
the same function without needing the db pointer to be stored within the
LightningNode. This change, the LightningNode struct more closely
represents the schema on disk.

The existing `ForEachNodeChannel` method on `ChannelGraph` is renamed to
`ForEachNodeDirectedChannel`. It performs a slightly different function
since it's call-back operates on Cached policies.
This commit is contained in:
Elle Mouton
2023-10-23 13:44:40 +02:00
parent dacb86fdbc
commit fa7c1e250b
7 changed files with 207 additions and 171 deletions

View File

@@ -53,6 +53,8 @@ func ChannelGraphFromDatabase(db *channeldb.ChannelGraph) ChannelGraph {
// channeldb.LightningNode. The wrapper method implement the autopilot.Node
// interface.
type dbNode struct {
db *channeldb.ChannelGraph
tx kvdb.RTx
node *channeldb.LightningNode
@@ -86,31 +88,36 @@ func (d dbNode) Addrs() []net.Addr {
//
// NOTE: Part of the autopilot.Node interface.
func (d dbNode) ForEachChannel(cb func(ChannelEdge) error) error {
return d.node.ForEachChannel(d.tx, func(tx kvdb.RTx,
ei *channeldb.ChannelEdgeInfo, ep, _ *channeldb.ChannelEdgePolicy) error {
return d.db.ForEachNodeChannel(d.tx, d.node.PubKeyBytes,
func(tx kvdb.RTx, ei *channeldb.ChannelEdgeInfo, ep,
_ *channeldb.ChannelEdgePolicy) error {
// Skip channels for which no outgoing edge policy is available.
//
// TODO(joostjager): Ideally the case where channels have a nil
// policy should be supported, as autopilot is not looking at
// the policies. For now, it is not easily possible to get a
// reference to the other end LightningNode object without
// retrieving the policy.
if ep == nil {
return nil
}
// Skip channels for which no outgoing edge policy is
// available.
//
// TODO(joostjager): Ideally the case where channels
// have a nil policy should be supported, as autopilot
// is not looking at the policies. For now, it is not
// easily possible to get a reference to the other end
// LightningNode object without retrieving the policy.
if ep == nil {
return nil
}
edge := ChannelEdge{
ChanID: lnwire.NewShortChanIDFromInt(ep.ChannelID),
Capacity: ei.Capacity,
Peer: dbNode{
tx: tx,
node: ep.Node,
},
}
edge := ChannelEdge{
ChanID: lnwire.NewShortChanIDFromInt(
ep.ChannelID,
),
Capacity: ei.Capacity,
Peer: dbNode{
tx: tx,
db: d.db,
node: ep.Node,
},
}
return cb(edge)
})
return cb(edge)
})
}
// ForEachNode is a higher-order function that should be called once for each
@@ -128,6 +135,7 @@ func (d *databaseChannelGraph) ForEachNode(cb func(Node) error) error {
}
node := dbNode{
db: d.db,
tx: tx,
node: n,
}
@@ -266,6 +274,7 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey,
ChanID: chanID,
Capacity: capacity,
Peer: dbNode{
db: d.db,
node: vertex1,
},
},
@@ -273,6 +282,7 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey,
ChanID: chanID,
Capacity: capacity,
Peer: dbNode{
db: d.db,
node: vertex2,
},
},