Skip to content

Commit

Permalink
feat: More flexibility for CacheMultiStoreWithVersion (backport cos…
Browse files Browse the repository at this point in the history
…mos#15683) (cosmos#15775)

Co-authored-by: khanh-notional <[email protected]>
Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
3 people authored and JeancarloBarrios committed Sep 28, 2024
1 parent 48fd5f9 commit 05fc156
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Improvements

* (store) [#15683](https://github.com/cosmos/cosmos-sdk/pull/15683) `rootmulti.Store.CacheMultiStoreWithVersion` now can handle loading archival states that don't persist any of the module stores the current state has.

### Bug Fixes

* (store/iavl) [#15717](https://github.com/cosmos/cosmos-sdk/pull/15717) Upstream error on empty version (this change was present on all version but v0.46).
Expand Down
23 changes: 22 additions & 1 deletion store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,30 @@ func (rs *Store) CacheMultiStoreWithVersion(version int64) (types.CacheMultiStor
// version does not exist or is pruned, an error should be returned.
var err error
cacheStore, err = store.(*iavl.Store).GetImmutable(version)
// if we got error from loading a module store
// we fetch commit info of this version
// we use commit info to check if the store existed at this version or not
if err != nil {
return nil, err
if commitInfo == nil {
var errCommitInfo error
commitInfo, errCommitInfo = getCommitInfo(rs.db, version)

if errCommitInfo != nil {
return nil, errCommitInfo
}

for _, storeInfo := range commitInfo.StoreInfos {
storeInfos[storeInfo.Name] = true
}
}

// If the store existed at this version, it means there's actually an error
// getting the root store at this version.
if storeInfos[key.Name()] {
return nil, err
}
}

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

// add new module stores (store4 and store5) to multi stores and commit
key4, key5 := types.NewKVStoreKey("store4"), types.NewKVStoreKey("store5")
ms.MountStoreWithDB(key4, types.StoreTypeIAVL, nil)
ms.MountStoreWithDB(key5, types.StoreTypeIAVL, nil)
ms.MountStoreWithDB(types.NewKVStoreKey("store4"), types.StoreTypeIAVL, nil)
ms.MountStoreWithDB(types.NewKVStoreKey("store5"), 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
cms2, err := ms.CacheMultiStoreWithVersion(1)
_, 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 05fc156

Please sign in to comment.