From 49e770c25d03a1af4334fe56dd01256855e91ee0 Mon Sep 17 00:00:00 2001 From: romelukaku Date: Fri, 23 Dec 2022 20:50:41 +0700 Subject: [PATCH 01/17] add store height check in loadVersion(); add test for that --- store/rootmulti/store.go | 2 ++ store/rootmulti/store_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 38bcab8de605..7c59da1eb046 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -246,6 +246,8 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error { // If it has been added, set the initial version if upgrades.IsAdded(key.Name()) { storeParams.initialVersion = uint64(ver) + 1 + } else if commitID.Version != ver { + return fmt.Errorf("version of store %s mismatch root store's version", key.Name()) } store, err := rs.loadCommitStoreFromParams(key, commitID, storeParams) diff --git a/store/rootmulti/store_test.go b/store/rootmulti/store_test.go index ed28c117b71d..713afeb23eda 100644 --- a/store/rootmulti/store_test.go +++ b/store/rootmulti/store_test.go @@ -605,6 +605,33 @@ func TestMultiStore_PruningRestart(t *testing.T) { } } +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) + + err = store.LoadVersion(0) + require.Nil(t, err) +} + func TestSetInitialVersion(t *testing.T) { db := dbm.NewMemDB() multi := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing)) From a16b35ffd8c07aadf0ba42e762ac85429a8a86cd Mon Sep 17 00:00:00 2001 From: romelukaku Date: Fri, 23 Dec 2022 20:51:15 +0700 Subject: [PATCH 02/17] minor --- store/rootmulti/store_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/store/rootmulti/store_test.go b/store/rootmulti/store_test.go index 713afeb23eda..fd099b44d4dd 100644 --- a/store/rootmulti/store_test.go +++ b/store/rootmulti/store_test.go @@ -627,9 +627,6 @@ func TestUnevenStoresHeightCheck(t *testing.T) { } err = store.LoadLatestVersionAndUpgrade(upgrades) require.Nil(t, err) - - err = store.LoadVersion(0) - require.Nil(t, err) } func TestSetInitialVersion(t *testing.T) { From 8cba224c61b3221f27034aa3ac257e3cadfa1a51 Mon Sep 17 00:00:00 2001 From: catShaark Date: Fri, 30 Dec 2022 15:20:51 +0700 Subject: [PATCH 03/17] update --- store/rootmulti/store.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 7c59da1eb046..537be67b565f 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -244,7 +244,7 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error { commitID := rs.getCommitID(infos, key.Name()) // If it has been added, set the initial version - if upgrades.IsAdded(key.Name()) { + if upgrades.IsAdded(key.Name()) || upgrades.RenamedFrom(key.Name()) != "" { storeParams.initialVersion = uint64(ver) + 1 } else if commitID.Version != ver { return fmt.Errorf("version of store %s mismatch root store's version", key.Name()) @@ -267,8 +267,9 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error { // handle renames specially // make an unregistered key to satisfy loadCommitStore params oldKey := types.NewKVStoreKey(oldName) - oldParams := storeParams - oldParams.key = oldKey + oldParams := rs.storesParams[oldKey] + fmt.Println(oldParams) + fmt.Println(storeParams) // load from the old name oldStore, err := rs.loadCommitStoreFromParams(oldKey, rs.getCommitID(infos, oldName), oldParams) From 57033e9a2f0854e0d491de7e2c2bccf19c0816de Mon Sep 17 00:00:00 2001 From: catShaark Date: Fri, 30 Dec 2022 16:56:50 +0700 Subject: [PATCH 04/17] NewStoreParams --- store/rootmulti/store.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 537be67b565f..927da877e039 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -1032,6 +1032,15 @@ type storeParams struct { initialVersion uint64 } +func NewStoreParams(key types.StoreKey, db dbm.DB, typ types.StoreType, initialVersion uint64) storeParams { + 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 { From 59d2d354b586cb6b1a931dc117a659dc5f866054 Mon Sep 17 00:00:00 2001 From: catShaark Date: Fri, 30 Dec 2022 16:59:33 +0700 Subject: [PATCH 05/17] use NewStoreParams --- store/rootmulti/store.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 927da877e039..3a3feee5dbce 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -147,11 +147,7 @@ func (rs *Store) MountStoreWithDB(key types.StoreKey, typ types.StoreType, db db if _, ok := rs.keysByName[key.Name()]; ok { panic(fmt.Sprintf("store duplicate store key name %v", key)) } - rs.storesParams[key] = storeParams{ - key: key, - typ: typ, - db: db, - } + rs.storesParams[key] = NewStoreParams(key, db, typ, 0) rs.keysByName[key.Name()] = key } From a821cbda9e3b49470c172af6e7aa46368f3ed156 Mon Sep 17 00:00:00 2001 From: khanh-notional <50263489+catShaark@users.noreply.github.com> Date: Wed, 11 Jan 2023 22:49:31 +0700 Subject: [PATCH 06/17] Update store/rootmulti/store.go Co-authored-by: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com> --- store/rootmulti/store.go | 1 - 1 file changed, 1 deletion(-) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 404b9696128e..d3ec741a4aea 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -274,7 +274,6 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error { // make an unregistered key to satisfy loadCommitStore params oldKey := types.NewKVStoreKey(oldName) oldParams := rs.storesParams[oldKey] - fmt.Println(oldParams) fmt.Println(storeParams) // load from the old name From 7f23d14597faa49682d6807fb75801bdcde46153 Mon Sep 17 00:00:00 2001 From: khanh-notional <50263489+catShaark@users.noreply.github.com> Date: Wed, 11 Jan 2023 22:49:43 +0700 Subject: [PATCH 07/17] Update store/rootmulti/store.go Co-authored-by: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com> --- store/rootmulti/store.go | 1 - 1 file changed, 1 deletion(-) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index d3ec741a4aea..12b4d657faa2 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -274,7 +274,6 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error { // make an unregistered key to satisfy loadCommitStore params oldKey := types.NewKVStoreKey(oldName) oldParams := rs.storesParams[oldKey] - fmt.Println(storeParams) // load from the old name oldStore, err := rs.loadCommitStoreFromParams(oldKey, rs.getCommitID(infos, oldName), oldParams) From cf716970547e65d5d91c2556abe300bd6fafd649 Mon Sep 17 00:00:00 2001 From: romelukaku Date: Wed, 18 Jan 2023 10:20:36 +0800 Subject: [PATCH 08/17] update loadVersion --- store/rootmulti/store.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 12b4d657faa2..9aba4273554c 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -252,8 +252,8 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error { // If it has been added, set the initial version if upgrades.IsAdded(key.Name()) || upgrades.RenamedFrom(key.Name()) != "" { storeParams.initialVersion = uint64(ver) + 1 - } else if commitID.Version != ver { - return fmt.Errorf("version of store %s mismatch root store's version", key.Name()) + } else if commitID.Version != ver && storeParams.typ == types.StoreTypeIAVL { + return fmt.Errorf("version of store %s mismatch root store's version; expect %d got %d", key.Name(), ver, commitID.Version) } store, err := rs.loadCommitStoreFromParams(key, commitID, storeParams) @@ -273,7 +273,7 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error { // handle renames specially // make an unregistered key to satisfy loadCommitStore params oldKey := types.NewKVStoreKey(oldName) - oldParams := rs.storesParams[oldKey] + oldParams := NewStoreParams(oldKey, storeParams.db, storeParams.typ, 0) // load from the old name oldStore, err := rs.loadCommitStoreFromParams(oldKey, rs.getCommitID(infos, oldName), oldParams) From 0352c085ea52dbc5de86754f8da85aa0ebc9344a Mon Sep 17 00:00:00 2001 From: romelukaku Date: Wed, 18 Jan 2023 10:22:53 +0800 Subject: [PATCH 09/17] fix tests in store_test.go --- store/rootmulti/store_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/store/rootmulti/store_test.go b/store/rootmulti/store_test.go index fed99f221c18..f1741dae2a84 100644 --- a/store/rootmulti/store_test.go +++ b/store/rootmulti/store_test.go @@ -271,6 +271,12 @@ func TestMultistoreLoadWithUpgrade(t *testing.T) { require.Equal(t, migratedID.Version, int64(2)) reload, _ := newMultiStoreWithModifiedMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing)) + // unmount store3 since store3 was deleted + unmountStore(reload, "store3") + + rs3, _ := reload.GetStoreByName("store3").(types.KVStore) + require.Nil(t, rs3) + err = reload.LoadLatestVersion() require.Nil(t, err) require.Equal(t, migratedID, reload.LastCommitID()) @@ -890,6 +896,13 @@ func newMultiStoreWithModifiedMounts(db dbm.DB, pruningOpts pruningtypes.Pruning return store, upgrades } +func unmountStore(rootStore *Store, storeKeyName string) { + sk := rootStore.keysByName[storeKeyName] + delete(rootStore.stores, sk) + delete(rootStore.storesParams, sk) + delete(rootStore.keysByName, storeKeyName) +} + func checkStore(t *testing.T, store *Store, expect, got types.CommitID) { require.Equal(t, expect, got) require.Equal(t, expect, store.LastCommitID()) From a55ba071a0c4fef9616eefd1da7750bb7fa20a75 Mon Sep 17 00:00:00 2001 From: romelukaku Date: Wed, 18 Jan 2023 10:27:55 +0800 Subject: [PATCH 10/17] changes to commitStores() --- store/rootmulti/store.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 9aba4273554c..5d8616c2da0f 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -1082,12 +1082,10 @@ func commitStores(version int64, storeMap map[types.StoreKey]types.CommitKVStore continue } - if !removalMap[key] { - si := types.StoreInfo{} - si.Name = key.Name() - si.CommitId = commitID - storeInfos = append(storeInfos, si) - } + si := types.StoreInfo{} + si.Name = key.Name() + si.CommitId = commitID + storeInfos = append(storeInfos, si) } sort.SliceStable(storeInfos, func(i, j int) bool { From 54fddbe7f759574f2003d16b39c875140727fd68 Mon Sep 17 00:00:00 2001 From: romelukaku Date: Sat, 21 Jan 2023 16:04:30 +0700 Subject: [PATCH 11/17] revert commitStores changes --- store/rootmulti/store.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 5d8616c2da0f..9aba4273554c 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -1082,10 +1082,12 @@ func commitStores(version int64, storeMap map[types.StoreKey]types.CommitKVStore continue } - si := types.StoreInfo{} - si.Name = key.Name() - si.CommitId = commitID - storeInfos = append(storeInfos, si) + if !removalMap[key] { + si := types.StoreInfo{} + si.Name = key.Name() + si.CommitId = commitID + storeInfos = append(storeInfos, si) + } } sort.SliceStable(storeInfos, func(i, j int) bool { From c11e0dafcf5765ad99579fc655c3db2799528a25 Mon Sep 17 00:00:00 2001 From: romelukaku Date: Sat, 21 Jan 2023 16:05:40 +0700 Subject: [PATCH 12/17] update TestSetLoader --- x/upgrade/types/storeloader_test.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/x/upgrade/types/storeloader_test.go b/x/upgrade/types/storeloader_test.go index e2e961baad50..41909d4dab4c 100644 --- a/x/upgrade/types/storeloader_test.go +++ b/x/upgrade/types/storeloader_test.go @@ -146,11 +146,6 @@ func TestSetLoader(t *testing.T) { res := app.Commit() require.NotNil(t, res.Data) - // checking the case of the store being renamed - if tc.setLoader != nil { - checkStore(t, db, upgradeHeight, tc.origStoreKey, k, nil) - } - // check db is properly updated checkStore(t, db, upgradeHeight, tc.loadStoreKey, k, v) }) From db95ddd0681622aa054bd88643a2da678f1150c7 Mon Sep 17 00:00:00 2001 From: romelukaku Date: Sat, 21 Jan 2023 16:22:20 +0700 Subject: [PATCH 13/17] add nolint for NewStoreParams --- store/rootmulti/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 9aba4273554c..0789ef90ae8a 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -1036,7 +1036,7 @@ type storeParams struct { initialVersion uint64 } -func NewStoreParams(key types.StoreKey, db dbm.DB, typ types.StoreType, initialVersion uint64) storeParams { +func NewStoreParams(key types.StoreKey, db dbm.DB, typ types.StoreType, initialVersion uint64) storeParams { // nolint return storeParams{ key: key, db: db, From f40a22fb489518f6f736c84df0e0544ff6a66343 Mon Sep 17 00:00:00 2001 From: romelukaku Date: Wed, 25 Jan 2023 11:36:38 +0700 Subject: [PATCH 14/17] add go doc to TestUnevenStoresHeightCheck --- store/rootmulti/store_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/store/rootmulti/store_test.go b/store/rootmulti/store_test.go index f1741dae2a84..867674317d41 100644 --- a/store/rootmulti/store_test.go +++ b/store/rootmulti/store_test.go @@ -612,6 +612,8 @@ func TestMultiStore_PruningRestart(t *testing.T) { } } +// 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)) From 527583d159c4029026e2a8020b57e9f6c998a4f4 Mon Sep 17 00:00:00 2001 From: catShaark Date: Fri, 27 Jan 2023 10:49:12 +0700 Subject: [PATCH 15/17] NewStoreParams -> newStoreParams --- store/rootmulti/store.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 50e0a7ddb8df..dfde21bb033b 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -156,7 +156,7 @@ func (rs *Store) MountStoreWithDB(key types.StoreKey, typ types.StoreType, db db if _, ok := rs.keysByName[key.Name()]; ok { panic(fmt.Sprintf("store duplicate store key name %v", key)) } - rs.storesParams[key] = NewStoreParams(key, db, typ, 0) + rs.storesParams[key] = newStoreParams(key, db, typ, 0) rs.keysByName[key.Name()] = key } @@ -273,7 +273,7 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error { // handle renames specially // make an unregistered key to satisfy loadCommitStore params oldKey := types.NewKVStoreKey(oldName) - oldParams := NewStoreParams(oldKey, storeParams.db, storeParams.typ, 0) + oldParams := newStoreParams(oldKey, storeParams.db, storeParams.typ, 0) // load from the old name oldStore, err := rs.loadCommitStoreFromParams(oldKey, rs.getCommitID(infos, oldName), oldParams) @@ -1036,7 +1036,7 @@ type storeParams struct { initialVersion uint64 } -func NewStoreParams(key types.StoreKey, db dbm.DB, typ types.StoreType, initialVersion uint64) storeParams { // nolint +func newStoreParams(key types.StoreKey, db dbm.DB, typ types.StoreType, initialVersion uint64) storeParams { // nolint return storeParams{ key: key, db: db, From 0f9e7a8242e449f704c4ff1ecad4b4bc46e5ae91 Mon Sep 17 00:00:00 2001 From: catShaark Date: Fri, 27 Jan 2023 11:06:43 +0700 Subject: [PATCH 16/17] update CL --- CHANGELOG.md | 1 + store/CHANGELOG.md | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4daf2eb52e3..87b5b91f879a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -74,6 +74,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (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. * (x/evidence) [#14757](https://github.com/cosmos/cosmos-sdk/pull/14757) Evidence messages do not need to implement a `.Type()` anymore. * (x/auth/tx) [#14751](https://github.com/cosmos/cosmos-sdk/pull/14751) Remove `.Type()` and `Route()` methods from all msgs and `legacytx.LegacyMsg` interface. * [#14691](https://github.com/cosmos/cosmos-sdk/pull/14691) Change behavior of `sdk.StringifyEvents` to not flatten events attributes by events type. diff --git a/store/CHANGELOG.md b/store/CHANGELOG.md index 2b6a94c78aca..f94f225911b8 100644 --- a/store/CHANGELOG.md +++ b/store/CHANGELOG.md @@ -27,4 +27,5 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features -* (store) [14746](https://github.com/cosmos/cosmos-sdk/pull/14746) The `store` module is extracted to have a separate go.mod file which allows it be a standalone module. \ No newline at end of file +* (store) [14746](https://github.com/cosmos/cosmos-sdk/pull/14746) The `store` module is extracted to have a separate go.mod file which allows it be a standalone module. +* (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. \ No newline at end of file From 712d79591e2864a0d613032b2b665a5205f2b09b Mon Sep 17 00:00:00 2001 From: khanh-notional <50263489+catShaark@users.noreply.github.com> Date: Fri, 27 Jan 2023 18:45:18 +0700 Subject: [PATCH 17/17] Update store/rootmulti/store.go Co-authored-by: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com> --- store/rootmulti/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index dfde21bb033b..a969d0c507d5 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -253,7 +253,7 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error { if upgrades.IsAdded(key.Name()) || upgrades.RenamedFrom(key.Name()) != "" { storeParams.initialVersion = uint64(ver) + 1 } else if commitID.Version != ver && storeParams.typ == types.StoreTypeIAVL { - return fmt.Errorf("version of store %s mismatch root store's version; expect %d got %d", key.Name(), ver, commitID.Version) + return fmt.Errorf("version of store %s mismatch root store's version; expected %d got %d", key.Name(), ver, commitID.Version) } store, err := rs.loadCommitStoreFromParams(key, commitID, storeParams)