Skip to content

Commit

Permalink
chore: disable #Set() on historical cache
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Dec 18, 2024
1 parent 6774d3a commit 086d219
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
9 changes: 5 additions & 4 deletions pkg/client/query/cache/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
6 changes: 4 additions & 2 deletions pkg/client/query/cache/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/query/cache/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 086d219

Please sign in to comment.