From 3069a67b62317c5f08faab32685de05365f0a97a Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Mon, 9 Jun 2025 12:08:18 +0200 Subject: [PATCH] graph/db: let GraghCache.AddChannel take CachedEdgeInfo Define a new CachedEdgeInfo type and let the graph cache's AddChannel use this. This will let us later on (for the SQL impl of the graph db) only load from the DB what we actually need for the graph cache. --- graph/db/graph.go | 6 +++--- graph/db/graph_cache.go | 2 +- graph/db/graph_cache_test.go | 2 +- graph/db/models/cached_edge_info.go | 33 +++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 graph/db/models/cached_edge_info.go diff --git a/graph/db/graph.go b/graph/db/graph.go index b414ff696..7378dcef8 100644 --- a/graph/db/graph.go +++ b/graph/db/graph.go @@ -178,7 +178,7 @@ func (c *ChannelGraph) populateCache() error { policy1, policy2 *models.ChannelEdgePolicy) error { c.graphCache.AddChannel( - info, + models.NewCachedEdge(info), models.NewCachedPolicy(policy1), models.NewCachedPolicy(policy2), ) @@ -316,7 +316,7 @@ func (c *ChannelGraph) AddChannelEdge(edge *models.ChannelEdgeInfo, } if c.graphCache != nil { - c.graphCache.AddChannel(edge, nil, nil) + c.graphCache.AddChannel(models.NewCachedEdge(edge), nil, nil) } select { @@ -352,7 +352,7 @@ func (c *ChannelGraph) MarkEdgeLive(chanID uint64) error { info := infos[0] c.graphCache.AddChannel( - info.Info, + models.NewCachedEdge(info.Info), models.NewCachedPolicy(info.Policy1), models.NewCachedPolicy(info.Policy2), ) diff --git a/graph/db/graph_cache.go b/graph/db/graph_cache.go index f07c0d38b..a691361a2 100644 --- a/graph/db/graph_cache.go +++ b/graph/db/graph_cache.go @@ -114,7 +114,7 @@ func (c *GraphCache) AddNodeFeatures(node route.Vertex, // and policy 2 does not matter, the directionality is extracted from the info // and policy flags automatically. The policy will be set as the outgoing policy // on one node and the incoming policy on the peer's side. -func (c *GraphCache) AddChannel(info *models.ChannelEdgeInfo, +func (c *GraphCache) AddChannel(info *models.CachedEdgeInfo, policy1, policy2 *models.CachedEdgePolicy) { if info == nil { diff --git a/graph/db/graph_cache_test.go b/graph/db/graph_cache_test.go index d9ad1dad8..43c35862e 100644 --- a/graph/db/graph_cache_test.go +++ b/graph/db/graph_cache_test.go @@ -61,7 +61,7 @@ func TestGraphCacheAddNode(t *testing.T) { } cache := NewGraphCache(10) cache.AddNodeFeatures(nodeA, lnwire.EmptyFeatureVector()) - cache.AddChannel(&models.ChannelEdgeInfo{ + cache.AddChannel(&models.CachedEdgeInfo{ ChannelID: 1000, // Those are direction independent! NodeKey1Bytes: pubKey1, diff --git a/graph/db/models/cached_edge_info.go b/graph/db/models/cached_edge_info.go new file mode 100644 index 000000000..eab12fbdd --- /dev/null +++ b/graph/db/models/cached_edge_info.go @@ -0,0 +1,33 @@ +package models + +import "github.com/btcsuite/btcd/btcutil" + +// CachedEdgeInfo is a struct that only caches the information of a +// ChannelEdgeInfo that we actually use for pathfinding and therefore need to +// store in the cache. +type CachedEdgeInfo struct { + // ChannelID is the unique channel ID for the channel. The first 3 + // bytes are the block height, the next 3 the index within the block, + // and the last 2 bytes are the output index for the channel. + ChannelID uint64 + + // NodeKey1Bytes is the raw public key of the first node. + NodeKey1Bytes [33]byte + + // NodeKey2Bytes is the raw public key of the second node. + NodeKey2Bytes [33]byte + + // Capacity is the total capacity of the channel, this is determined by + // the value output in the outpoint that created this channel. + Capacity btcutil.Amount +} + +// NewCachedEdge creates a new CachedEdgeInfo from the provided ChannelEdgeInfo. +func NewCachedEdge(edgeInfo *ChannelEdgeInfo) *CachedEdgeInfo { + return &CachedEdgeInfo{ + ChannelID: edgeInfo.ChannelID, + NodeKey1Bytes: edgeInfo.NodeKey1Bytes, + NodeKey2Bytes: edgeInfo.NodeKey2Bytes, + Capacity: edgeInfo.Capacity, + } +}