graph/db: create a re-usable forEachChannel

Create a forEachChannel function that does not rely on a KVStore being
instantiated. We will use this in our upcoming kvdb->SQL migration.
This commit is contained in:
Elle Mouton
2025-06-26 10:51:13 +02:00
parent 9452c0b950
commit 016fd9bb4d

View File

@@ -407,7 +407,22 @@ func (c *KVStore) AddrsForNode(ctx context.Context,
func (c *KVStore) ForEachChannel(cb func(*models.ChannelEdgeInfo,
*models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
return c.db.View(func(tx kvdb.RTx) error {
return forEachChannel(c.db, cb)
}
// forEachChannel iterates through all the channel edges stored within the
// graph and invokes the passed callback for each edge. The callback takes two
// edges as since this is a directed graph, both the in/out edges are visited.
// If the callback returns an error, then the transaction is aborted and the
// iteration stops early.
//
// NOTE: If an edge can't be found, or wasn't advertised, then a nil pointer
// for that particular channel edge routing policy will be passed into the
// callback.
func forEachChannel(db kvdb.Backend, cb func(*models.ChannelEdgeInfo,
*models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error {
return db.View(func(tx kvdb.RTx) error {
edges := tx.ReadBucket(edgeBucket)
if edges == nil {
return ErrGraphNoEdgesFound