fix: add missing nil check in store.GetStore (#9354)

* fix: add missing nil check in store.GetStore

* check nil in rootmulti as well
This commit is contained in:
Robert Zaremba 2021-05-19 10:17:46 +02:00 committed by GitHub
parent 377360a391
commit b065e20f2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 3 deletions

View File

@ -173,13 +173,17 @@ func (cms Store) CacheMultiStoreWithVersion(_ int64) (types.CacheMultiStore, err
// GetStore returns an underlying Store by key.
func (cms Store) GetStore(key types.StoreKey) types.Store {
return cms.stores[key].(types.Store)
s := cms.stores[key]
if key == nil || s == nil {
panic(fmt.Sprintf("kv store with key %v has not been registered in stores", key))
}
return s.(types.Store)
}
// GetKVStore returns an underlying KVStore by key.
func (cms Store) GetKVStore(key types.StoreKey) types.KVStore {
store := cms.stores[key]
if key == nil {
if key == nil || store == nil {
panic(fmt.Sprintf("kv store with key %v has not been registered in stores", key))
}
return store.(types.KVStore)

View File

@ -0,0 +1,23 @@
package cachemulti
import (
"fmt"
"testing"
"github.com/cosmos/cosmos-sdk/store/types"
"github.com/stretchr/testify/require"
)
func TestStoreGetKVStore(t *testing.T) {
require := require.New(t)
s := Store{stores: map[types.StoreKey]types.CacheWrap{}}
key := types.NewKVStoreKey("abc")
errMsg := fmt.Sprintf("kv store with key %v has not been registered in stores", key)
require.PanicsWithValue(errMsg,
func() { s.GetStore(key) })
require.PanicsWithValue(errMsg,
func() { s.GetKVStore(key) })
}

View File

@ -492,7 +492,11 @@ func (rs *Store) GetStore(key types.StoreKey) types.Store {
// NOTE: The returned KVStore may be wrapped in an inter-block cache if it is
// set on the root store.
func (rs *Store) GetKVStore(key types.StoreKey) types.KVStore {
store := rs.stores[key].(types.KVStore)
s := rs.stores[key]
if s == nil {
panic(fmt.Sprintf("store does not exist for key: %s", key.Name()))
}
store := s.(types.KVStore)
if rs.TracingEnabled() {
store = tracekv.NewStore(store, rs.traceWriter, rs.traceContext)