Skip to content

Commit 7b7f3a2

Browse files
mmsqeyihuangAlex | Interchain Labs
authored
fix(store): avoid panic when store not exists in historical version (port: cosmos#20425) (cosmos#24549)
Co-authored-by: yihuang <[email protected]> Co-authored-by: Alex | Interchain Labs <[email protected]>
1 parent 4025458 commit 7b7f3a2

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

store/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ Ref: https://keepachangelog.com/en/1.0.0/
2323

2424
# Changelog
2525

26+
## [Unreleased]
27+
28+
### Bug Fixes
29+
30+
* [#20425](https://github.com/cosmos/cosmos-sdk/pull/20425) Fix nil pointer panic when querying historical state where a new store does not exist.
31+
2632
## v1.1.2 (March 31, 2025)
2733

2834
### Bug Fixes

store/rootmulti/store.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,10 @@ func (rs *Store) CacheMultiStoreWithVersion(version int64) (types.CacheMultiStor
627627
if storeInfos[key.Name()] {
628628
return nil, err
629629
}
630+
631+
// If the store donesn't exist at this version, create a dummy one to prevent
632+
// nil pointer panic in newer query APIs.
633+
cacheStore = dbadapter.Store{DB: dbm.NewMemDB()}
630634
}
631635

632636
default:

store/rootmulti/store_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ func TestCacheMultiStoreWithVersion(t *testing.T) {
120120
})
121121
}
122122

123+
func TestCacheMultiStoreWithVersionStoreNotExist(t *testing.T) {
124+
db := dbm.NewMemDB()
125+
ms := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
126+
err := ms.LoadLatestVersion()
127+
require.Nil(t, err)
128+
cID := ms.Commit()
129+
require.Equal(t, int64(1), cID.Version)
130+
// add new module stores (store4 and store5) to multi stores and commit
131+
key4, key5 := types.NewKVStoreKey("store4"), types.NewKVStoreKey("store5")
132+
ms.MountStoreWithDB(key4, types.StoreTypeIAVL, nil)
133+
ms.MountStoreWithDB(key5, types.StoreTypeIAVL, nil)
134+
err = ms.LoadLatestVersionAndUpgrade(&types.StoreUpgrades{Added: []string{"store4", "store5"}})
135+
require.NoError(t, err)
136+
ms.Commit()
137+
// cache multistore of version before adding store4 should works
138+
cms2, err := ms.CacheMultiStoreWithVersion(1)
139+
require.NoError(t, err)
140+
require.Empty(t, cms2.GetKVStore(key4).Get([]byte("key")))
141+
}
142+
123143
func TestHashStableWithEmptyCommit(t *testing.T) {
124144
var db dbm.DB = dbm.NewMemDB()
125145
ms := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))

0 commit comments

Comments
 (0)