From 6a105fef231a2eec21adbbe00a99805d23fb79b6 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 22 Feb 2023 00:37:12 +0100 Subject: [PATCH] chore: Add check for uneven stores' height (backport #14410) (#15115) Co-authored-by: khanh-notional <50263489+catShaark@users.noreply.github.com> Co-authored-by: marbar3778 --- CHANGELOG.md | 1 + go.mod | 3 +-- store/rootmulti/store.go | 9 +++++++++ store/rootmulti/store_test.go | 28 +++++++++++++++++++++++++++- x/upgrade/types/storeloader_test.go | 7 ------- 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a251f8c4ebb..86873149ebaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements * (cli) [#14953](https://github.com/cosmos/cosmos-sdk/pull/14953) Enable profiling block replay during abci handshake with `--cpu-profile`. +* (store) [#14410](https://github.com/cosmos/cosmos-sdk/pull/14410) `rootmulti.Store.loadVersion` has validation to check if all the module stores' height is correct, it will error if any module store has incorrect height. ## [v0.46.9](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.9) - 2022-02-07 diff --git a/go.mod b/go.mod index a2057ba57764..3272ccba7e6b 100644 --- a/go.mod +++ b/go.mod @@ -47,7 +47,7 @@ require ( github.com/spf13/viper v1.13.0 github.com/stretchr/testify v1.8.1 github.com/tendermint/go-amino v0.16.0 - github.com/tendermint/tendermint v0.34.26 + github.com/tendermint/tendermint v0.34.24 github.com/tendermint/tm-db v0.6.7 github.com/tidwall/btree v1.5.0 golang.org/x/crypto v0.5.0 @@ -162,7 +162,6 @@ replace ( // TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409 github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.7.0 github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1 - github.com/jhump/protoreflect => github.com/jhump/protoreflect v1.9.0 // use informal system fork of tendermint github.com/tendermint/tendermint => github.com/informalsystems/tendermint v0.34.26 diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index f5ff7a09ec31..4ef8aab57c30 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -1100,6 +1100,15 @@ type storeParams struct { initialVersion uint64 } +func newStoreParams(key types.StoreKey, db dbm.DB, typ types.StoreType, initialVersion uint64) storeParams { // nolint + return storeParams{ + key: key, + db: db, + typ: typ, + initialVersion: initialVersion, + } +} + func GetLatestVersion(db dbm.DB) int64 { bz, err := db.Get([]byte(latestVersionKey)) if err != nil { diff --git a/store/rootmulti/store_test.go b/store/rootmulti/store_test.go index 1999e8e099a9..929812325660 100644 --- a/store/rootmulti/store_test.go +++ b/store/rootmulti/store_test.go @@ -301,7 +301,7 @@ func TestMultistoreLoadWithUpgrade(t *testing.T) { migratedID := restore.Commit() require.Equal(t, migratedID.Version, int64(2)) - reload, _ := newMultiStoreWithModifiedMounts(db, types.PruneNothing) + reload, _ := newMultiStoreWithModifiedMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing)) // unmount store3 since store3 was deleted unmountStore(reload, "store3") @@ -669,6 +669,32 @@ func TestUnevenStoresHeightCheck(t *testing.T) { require.Nil(t, err) } +// TestUnevenStoresHeightCheck tests if loading root store correctly errors when +// there's any module store with the wrong height +func TestUnevenStoresHeightCheck(t *testing.T) { + var db dbm.DB = dbm.NewMemDB() + store := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing)) + err := store.LoadLatestVersion() + require.Nil(t, err) + + // commit to increment store's height + store.Commit() + + // mount store4 to root store + store.MountStoreWithDB(types.NewKVStoreKey("store4"), types.StoreTypeIAVL, nil) + + // load the stores without upgrades + err = store.LoadLatestVersion() + require.Error(t, err) + + // now, let's load with upgrades... + upgrades := &types.StoreUpgrades{ + Added: []string{"store4"}, + } + err = store.LoadLatestVersionAndUpgrade(upgrades) + require.Nil(t, err) +} + func TestSetInitialVersion(t *testing.T) { db := coretesting.NewMemDB() multi := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing)) diff --git a/x/upgrade/types/storeloader_test.go b/x/upgrade/types/storeloader_test.go index 56820a56c63f..dcc7617864d0 100644 --- a/x/upgrade/types/storeloader_test.go +++ b/x/upgrade/types/storeloader_test.go @@ -141,13 +141,6 @@ func TestSetLoader(t *testing.T) { require.Equal(t, upgradeHeight-1, newApp.LastBlockHeight()) - // "execute" one block - _, err = newApp.FinalizeBlock(&abci.FinalizeBlockRequest{Height: upgradeHeight}) - require.NoError(t, err) - _, err = newApp.Commit() - require.NoError(t, err) - require.Equal(t, upgradeHeight, newApp.LastBlockHeight()) - // check db is properly updated checkStore(t, db, upgradeHeight, tc.loadStoreKey, k, v) })