Skip to content

Commit fc55b61

Browse files
committed
move max check to get and getAll instead of polluting checkReload
1 parent 4071f13 commit fc55b61

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

x/staking/cache/entry.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,23 @@ func (e *Entry[V, E]) getAll(ctx context.Context, cdc codec.BinaryCodec, logger
5555
e.mu.Lock()
5656
defer e.mu.Unlock()
5757

58-
if err := e.checkReload(ctx, cdc, logger); err != nil {
58+
store := e.storeService.OpenMemoryStore(ctx)
59+
60+
metadata, err := e.getMetadata(store, cdc)
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
if metadata.IsFull {
66+
return nil, types.ErrCacheMaxSizeReached
67+
}
68+
69+
if err := e.checkReload(ctx, store, cdc, logger); err != nil {
5970
return nil, err
6071
}
6172

6273
result := make(map[string]V)
6374

64-
store := e.storeService.OpenMemoryStore(ctx)
6575
prefix := e.getPrefix()
6676
iter, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix))
6777
if err != nil {
@@ -88,11 +98,12 @@ func (e *Entry[V, E]) get(ctx context.Context, cdc codec.BinaryCodec, key string
8898
e.mu.Lock()
8999
defer e.mu.Unlock()
90100

91-
if err := e.checkReload(ctx, cdc, logger); err != nil {
101+
store := e.storeService.OpenMemoryStore(ctx)
102+
103+
if err := e.checkReload(ctx, store, cdc, logger); err != nil {
92104
return make(V, 0), err
93105
}
94106

95-
store := e.storeService.OpenMemoryStore(ctx)
96107
storeKey := e.getStoreKey(key)
97108

98109
bz, err := store.Get(storeKey)
@@ -290,18 +301,12 @@ func (e *Entry[V, E]) clear(ctx context.Context, cdc codec.BinaryCodec) error {
290301
}
291302

292303
// checkReload: caller MUST hold lock
293-
func (e *Entry[V, E]) checkReload(ctx context.Context, cdc codec.BinaryCodec, logger func(ctx context.Context) log.Logger) error {
294-
store := e.storeService.OpenMemoryStore(ctx)
295-
304+
func (e *Entry[V, E]) checkReload(ctx context.Context, store corestoretypes.KVStore, cdc codec.BinaryCodec, logger func(ctx context.Context) log.Logger) error {
296305
metadata, err := e.getMetadata(store, cdc)
297306
if err != nil {
298307
return err
299308
}
300309

301-
if metadata.IsFull {
302-
return types.ErrCacheMaxSizeReached
303-
}
304-
305310
if !metadata.IsDirty {
306311
return nil
307312
}

0 commit comments

Comments
 (0)