diff --git a/pkg/client/query/cache/errors.go b/pkg/client/query/cache/errors.go index b80342999..b12bb5cac 100644 --- a/pkg/client/query/cache/errors.go +++ b/pkg/client/query/cache/errors.go @@ -5,8 +5,9 @@ import "cosmossdk.io/errors" const codesace = "client/query/cache" var ( - ErrCacheMiss = errors.Register(codesace, 1, "cache miss") - ErrHistoricalModeNotEnabled = errors.Register(codesace, 2, "historical mode not enabled") - ErrQueryCacheConfigValidation = errors.Register(codesace, 3, "invalid query cache config") - ErrCacheInternal = errors.Register(codesace, 4, "cache internal error") + ErrCacheMiss = errors.Register(codesace, 1, "cache miss") + ErrHistoricalModeNotEnabled = errors.Register(codesace, 2, "historical mode not enabled") + ErrQueryCacheConfigValidation = errors.Register(codesace, 3, "invalid query cache config") + ErrCacheInternal = errors.Register(codesace, 4, "cache internal error") + ErrUnsupportedHistoricalModeOp = errors.Register(codesace, 5, "operation not supported in historical mode") ) diff --git a/pkg/client/query/cache/memory.go b/pkg/client/query/cache/memory.go index f229cb1d4..088f3f5d1 100644 --- a/pkg/client/query/cache/memory.go +++ b/pkg/client/query/cache/memory.go @@ -171,7 +171,7 @@ func (c *inMemoryCache[T]) GetAsOfVersion(key string, version int64) (T, error) // guaranteed to be the current version w.r.t. the blockchain. func (c *inMemoryCache[T]) Set(key string, value T) error { if c.config.historical { - return c.SetAsOfVersion(key, value, c.latestVersion.Load()) + return ErrUnsupportedHistoricalModeOp.Wrap("inMemoryCache#Set() is not supported in historical mode") } c.valuesMu.Lock() @@ -205,7 +205,9 @@ func (c *inMemoryCache[T]) SetAsOfVersion(key string, value T, version int64) er latestVersion := c.latestVersion.Load() if version > latestVersion { // NB: Only update if c.latestVersion hasn't changed since we loaded it above. - if !c.latestVersion.CompareAndSwap(latestVersion, version) { + if c.latestVersion.CompareAndSwap(latestVersion, version) { + latestVersion = version + } else { // Reload the latestVersion if it did change. latestVersion = c.latestVersion.Load() } diff --git a/pkg/client/query/cache/memory_test.go b/pkg/client/query/cache/memory_test.go index 93c20cd9e..ac5a742ad 100644 --- a/pkg/client/query/cache/memory_test.go +++ b/pkg/client/query/cache/memory_test.go @@ -206,12 +206,12 @@ func TestInMemoryCache_Historical(t *testing.T) { // Regular Set should work with latest version err = cache.Set("key", "value3") - require.NoError(t, err) + require.ErrorIs(t, err, ErrUnsupportedHistoricalModeOp) // Regular Get should return the latest value val, err := cache.Get("key") require.NoError(t, err) - require.Equal(t, "value3", val) + require.Equal(t, "value2", val) // Delete should remove all historical values cache.Delete("key")