Skip to content

Commit

Permalink
Problem: store key type assertion is incorrect (cosmos#244)
Browse files Browse the repository at this point in the history
fix and add test
  • Loading branch information
yihuang authored and mmsqe committed Dec 14, 2024
1 parent 0a2b3ef commit c6bc908
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
9 changes: 9 additions & 0 deletions store/cachemulti/store_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cachemulti

import (
"errors"
"fmt"
"testing"

Expand Down Expand Up @@ -51,4 +52,12 @@ func TestRunAtomic(t *testing.T) {
})
require.Equal(t, []byte("value"), s.GetKVStore(keys["abc"]).Get([]byte("key")))
require.Equal(t, "value", s.GetObjKVStore(keys["obj"]).Get([]byte("key")).(string))

require.Error(t, s.RunAtomic(func(ms types.CacheMultiStore) error {
ms.GetKVStore(keys["abc"]).Set([]byte("key"), []byte("value2"))
ms.GetObjKVStore(keys["obj"]).Set([]byte("key"), "value2")
return errors.New("failure")
}))
require.Equal(t, []byte("value"), s.GetKVStore(keys["abc"]).Get([]byte("key")))
require.Equal(t, "value", s.GetObjKVStore(keys["obj"]).Get([]byte("key")).(string))
}
16 changes: 7 additions & 9 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1061,26 +1061,24 @@ func (rs *Store) loadCommitStoreFromParams(key types.StoreKey, id types.CommitID
return commitDBStoreAdapter{Store: dbadapter.Store{DB: db}}, nil

case types.StoreTypeTransient:
_, ok := key.(*types.TransientStoreKey)
if !ok {
return nil, fmt.Errorf("invalid StoreKey for StoreTypeTransient: %s", key.String())
if _, ok := key.(*types.TransientStoreKey); !ok {
return nil, fmt.Errorf("unexpected key type for a TransientStoreKey; got: %s, %T", key.String(), key)
}

return transient.NewStore(), nil

case types.StoreTypeMemory:
_, ok := key.(*types.ObjectStoreKey)
if !ok {
return nil, fmt.Errorf("invalid StoreKey for StoreTypeTransient: %s", key.String())
}

if _, ok := key.(*types.MemoryStoreKey); !ok {
return nil, fmt.Errorf("unexpected key type for a MemoryStoreKey; got: %s", key.String())
return nil, fmt.Errorf("unexpected key type for a MemoryStoreKey; got: %s, %T", key.String(), key)
}

return mem.NewStore(), nil

case types.StoreTypeObject:
if _, ok := key.(*types.ObjectStoreKey); !ok {
return nil, fmt.Errorf("unexpected key type for a ObjectStoreKey; got: %s, %T", key.String(), key)
}

return transient.NewObjStore(), nil

default:
Expand Down
1 change: 1 addition & 0 deletions store/rootmulti/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,7 @@ func prepareStoreMap() (map[types.StoreKey]types.CommitStore, error) {
store.MountStoreWithDB(types.NewKVStoreKey("iavl1"), types.StoreTypeIAVL, nil)
store.MountStoreWithDB(types.NewKVStoreKey("iavl2"), types.StoreTypeIAVL, nil)
store.MountStoreWithDB(types.NewTransientStoreKey("trans1"), types.StoreTypeTransient, nil)
store.MountStoreWithDB(types.NewMemoryStoreKey("mem1"), types.StoreTypeMemory, nil)
store.MountStoreWithDB(types.NewObjectStoreKey("obj1"), types.StoreTypeObject, nil)
if err := store.LoadLatestVersion(); err != nil {
return nil, err
Expand Down

0 comments on commit c6bc908

Please sign in to comment.