From 016fd9bb4d41adc85b611020c11d9264bdcedf26 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Thu, 26 Jun 2025 10:51:13 +0200 Subject: [PATCH] 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. --- graph/db/kv_store.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/graph/db/kv_store.go b/graph/db/kv_store.go index e679b5777..d0892b48d 100644 --- a/graph/db/kv_store.go +++ b/graph/db/kv_store.go @@ -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