graph/db: version channelCache by gossip version

Add a channelCacheKey struct keyed by {GossipVersion, chanID}, matching
the pattern already used by rejectCache. This prevents v1 and v2
channel data from colliding in the shared cache.

All callers in KVStore (always GossipVersion1) and SQLStore (version
from context) are updated to pass the version parameter.

This will be needed for later on when we update methods that use this
cache to be versioned (like ChannelUpdatesInHorizon).
This commit is contained in:
Elle Mouton committed 2026-02-20 08:46:57 +02:00
1 parent 14a01eb84f
commit 2aaa52adf9
4 files changed
+89 -45

No files matched your search

+34 -10
View File
@@ -1,11 +1,20 @@
package graphdb
import "github.com/lightningnetwork/lnd/lnwire"
// channelCacheKey uniquely identifies a channel entry in the channel cache by
// gossip version and channel ID.
type channelCacheKey struct {
version lnwire.GossipVersion
chanID uint64
}
// channelCache is an in-memory cache used to improve the performance of
// ChanUpdatesInHorizon. It caches the chan info and edge policies for a
// particular channel.
type channelCache struct {
n int
channels map[uint64]ChannelEdge
channels map[channelCacheKey]ChannelEdge
}
// newChannelCache creates a new channelCache with maximum capacity of n
@@ -13,13 +22,18 @@ type channelCache struct {
func newChannelCache(n int) *channelCache {
return &channelCache{
n: n,
channels: make(map[uint64]ChannelEdge),
channels: make(map[channelCacheKey]ChannelEdge),
}
}
// get returns the channel from the cache, if it exists.
func (c *channelCache) get(chanid uint64) (ChannelEdge, bool) {
channel, ok := c.channels[chanid]
func (c *channelCache) get(version lnwire.GossipVersion,
chanid uint64) (ChannelEdge, bool) {
channel, ok := c.channels[channelCacheKey{
version: version,
chanID: chanid,
}]
return channel, ok
}
@@ -27,10 +41,17 @@ func (c *channelCache) get(chanid uint64) (ChannelEdge, bool) {
// exists, it will be replaced with the new entry. If the entry doesn't exist,
// it will be inserted to the cache, performing a random eviction if the cache
// is at capacity.
func (c *channelCache) insert(chanid uint64, channel ChannelEdge) {
func (c *channelCache) insert(version lnwire.GossipVersion, chanid uint64,
channel ChannelEdge) {
key := channelCacheKey{
version: version,
chanID: chanid,
}
// If entry exists, replace it.
if _, ok := c.channels[chanid]; ok {
c.channels[chanid] = channel
if _, ok := c.channels[key]; ok {
c.channels[key] = channel
return
}
@@ -41,10 +62,13 @@ func (c *channelCache) insert(chanid uint64, channel ChannelEdge) {
break
}
}
c.channels[chanid] = channel
c.channels[key] = channel
}
// remove deletes an edge for chanid from the cache, if it exists.
func (c *channelCache) remove(chanid uint64) {
delete(c.channels, chanid)
func (c *channelCache) remove(version lnwire.GossipVersion, chanid uint64) {
delete(c.channels, channelCacheKey{
version: version,
chanID: chanid,
})
}