Skip to content

Commit

Permalink
Problem: panic when store not exists in historical version (#435) (#1006
Browse files Browse the repository at this point in the history
)

* Problem: panic when store not exists in historical version

Solution:
- make a dummy store, so client get empty data instead of panic on nil pointer

* unit test

* changelog

Co-authored-by: yihuang <[email protected]>
  • Loading branch information
mmsqe and yihuang authored Dec 5, 2024
1 parent 6031e79 commit 142c0af
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 4 additions & 0 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,10 @@ func (rs *Store) CacheMultiStoreWithVersion(version int64) (types.CacheMultiStor
if storeInfos[key.Name()] {
return nil, err
}

// If the store donesn't exist at this version, create a dummy one to prevent
// nil pointer panic in newer query APIs.
cacheStore = dbadapter.Store{DB: dbm.NewMemDB()}
}

default:
Expand Down
9 changes: 6 additions & 3 deletions store/rootmulti/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,19 @@ func TestCacheMultiStoreWithVersion(t *testing.T) {
require.Equal(t, kvStore.Get(k), v)

// add new module stores (store4 and store5) to multi stores and commit
ms.MountStoreWithDB(types.NewKVStoreKey("store4"), types.StoreTypeIAVL, nil)
ms.MountStoreWithDB(types.NewKVStoreKey("store5"), types.StoreTypeIAVL, nil)
key4, key5 := types.NewKVStoreKey("store4"), types.NewKVStoreKey("store5")
ms.MountStoreWithDB(key4, types.StoreTypeIAVL, nil)
ms.MountStoreWithDB(key5, types.StoreTypeIAVL, nil)
err = ms.LoadLatestVersionAndUpgrade(&types.StoreUpgrades{Added: []string{"store4", "store5"}})
require.NoError(t, err)
ms.Commit()

// cache multistore of version before adding store4 should works
_, err = ms.CacheMultiStoreWithVersion(1)
cms2, err := ms.CacheMultiStoreWithVersion(1)
require.NoError(t, err)

require.Empty(t, cms2.GetKVStore(key4).Get([]byte("key")))

// require we cannot commit (write) to a cache-versioned multi-store
require.Panics(t, func() {
kvStore.Set(k, []byte("newValue"))
Expand Down

0 comments on commit 142c0af

Please sign in to comment.