From ed9b7552d1ab9fde1aa8d7f18a529bc4eaec9c4e Mon Sep 17 00:00:00 2001 From: atheesh Date: Thu, 25 Aug 2022 19:09:09 +0530 Subject: [PATCH 1/5] fix migration tests --- x/genutil/migrations/v047/migrate.go | 19 +++++++++ x/gov/migrations/v3/json.go | 11 ++--- x/gov/migrations/v3/json_test.go | 23 +++++----- x/gov/migrations/v4/json.go | 23 ++++++++++ x/gov/migrations/v4/json_test.go | 64 ++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 19 deletions(-) create mode 100644 x/gov/migrations/v4/json.go create mode 100644 x/gov/migrations/v4/json_test.go diff --git a/x/genutil/migrations/v047/migrate.go b/x/genutil/migrations/v047/migrate.go index 2d322fb6174b..5af007ff0aa0 100644 --- a/x/genutil/migrations/v047/migrate.go +++ b/x/genutil/migrations/v047/migrate.go @@ -5,6 +5,8 @@ import ( bankv4 "github.com/cosmos/cosmos-sdk/x/bank/migrations/v4" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/genutil/types" + v4gov "github.com/cosmos/cosmos-sdk/x/gov/migrations/v4" + v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" ) // Migrate migrates exported state from v0.46 to a v0.47 genesis state. @@ -17,5 +19,22 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { newBankState := bankv4.MigrateGenState(oldBankState) appState[banktypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(newBankState) } + + if govOldState, ok := appState[v4gov.ModuleName]; ok { + // unmarshal relative source genesis application state + var old v1.GenesisState + clientCtx.Codec.MustUnmarshalJSON(govOldState, &old) + + // delete deprecated x/gov genesis state + delete(appState, v4gov.ModuleName) + + // the respective key. + new, err := v4gov.MigrateJSON(&old) + if err != nil { + panic(err) + } + appState[v4gov.ModuleName] = clientCtx.Codec.MustMarshalJSON(new) + } + return appState } diff --git a/x/gov/migrations/v3/json.go b/x/gov/migrations/v3/json.go index 94116b776564..327e83a58b19 100644 --- a/x/gov/migrations/v3/json.go +++ b/x/gov/migrations/v3/json.go @@ -27,13 +27,8 @@ func MigrateJSON(oldState *v1beta1.GenesisState) (*v1.GenesisState, error) { Deposits: convertToNewDeposits(oldState.Deposits), Votes: newVotes, Proposals: newProps, - Params: &v1.Params{ - MinDeposit: depParams.MinDeposit, - MaxDepositPeriod: depParams.MaxDepositPeriod, - VotingPeriod: votingParms.VotingPeriod, - Quorum: tallyParams.Quorum, - Threshold: tallyParams.Threshold, - VetoThreshold: tallyParams.VetoThreshold, - }, + DepositParams: &depParams, + VotingParams: &votingParms, + TallyParams: &tallyParams, }, nil } diff --git a/x/gov/migrations/v3/json_test.go b/x/gov/migrations/v3/json_test.go index 6eee6b86bad2..7cc1ccd93dcb 100644 --- a/x/gov/migrations/v3/json_test.go +++ b/x/gov/migrations/v3/json_test.go @@ -74,22 +74,17 @@ func TestMigrateJSON(t *testing.T) { // Make sure about: // - Proposals use MsgExecLegacyContent expected := `{ - "deposit_params": null, - "deposits": [], - "params": { + "deposit_params": { "max_deposit_period": "172800s", "min_deposit": [ { "amount": "10000000", "denom": "stake" } - ], - "min_initial_deposit_ratio": "", - "quorum": "0.334000000000000000", - "threshold": "0.500000000000000000", - "veto_threshold": "0.334000000000000000", - "voting_period": "172800s" + ] }, + "deposits": [], + "params": null, "proposals": [ { "deposit_end_time": "2001-09-09T01:46:40Z", @@ -125,7 +120,11 @@ func TestMigrateJSON(t *testing.T) { } ], "starting_proposal_id": "1", - "tally_params": null, + "tally_params": { + "quorum": "0.334000000000000000", + "threshold": "0.500000000000000000", + "veto_threshold": "0.334000000000000000" + }, "votes": [ { "metadata": "", @@ -150,7 +149,9 @@ func TestMigrateJSON(t *testing.T) { "voter": "cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh" } ], - "voting_params": null + "voting_params": { + "voting_period": "172800s" + } }` require.Equal(t, expected, string(indentedBz)) diff --git a/x/gov/migrations/v4/json.go b/x/gov/migrations/v4/json.go new file mode 100644 index 000000000000..033281adff04 --- /dev/null +++ b/x/gov/migrations/v4/json.go @@ -0,0 +1,23 @@ +package v4 + +import ( + v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" +) + +func MigrateJSON(oldState *v1.GenesisState) (*v1.GenesisState, error) { + + params := v1.NewParams( + oldState.DepositParams.MinDeposit, + *oldState.DepositParams.MaxDepositPeriod, + *oldState.VotingParams.VotingPeriod, + oldState.TallyParams.Quorum, + oldState.TallyParams.Threshold, + oldState.TallyParams.VetoThreshold, + v1.DefaultParams().MinInitialDepositRatio, + ) + + return &v1.GenesisState{ + StartingProposalId: oldState.StartingProposalId, + Params: ¶ms, + }, nil +} diff --git a/x/gov/migrations/v4/json_test.go b/x/gov/migrations/v4/json_test.go new file mode 100644 index 000000000000..0b58e63d7607 --- /dev/null +++ b/x/gov/migrations/v4/json_test.go @@ -0,0 +1,64 @@ +package v4_test + +import ( + "encoding/json" + "testing" + + "github.com/cosmos/cosmos-sdk/client" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + "github.com/cosmos/cosmos-sdk/x/gov" + v4 "github.com/cosmos/cosmos-sdk/x/gov/migrations/v4" + v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" + "github.com/stretchr/testify/require" +) + +func TestMigrateJSON(t *testing.T) { + encodingConfig := moduletestutil.MakeTestEncodingConfig(gov.AppModuleBasic{}) + clientCtx := client.Context{}. + WithInterfaceRegistry(encodingConfig.InterfaceRegistry). + WithTxConfig(encodingConfig.TxConfig). + WithCodec(encodingConfig.Codec) + + govGenState := v1.DefaultGenesisState() + + migrated, err := v4.MigrateJSON(govGenState) + require.NoError(t, err) + + bz, err := clientCtx.Codec.MarshalJSON(migrated) + require.NoError(t, err) + + // Indent the JSON bz correctly. + var jsonObj map[string]interface{} + err = json.Unmarshal(bz, &jsonObj) + require.NoError(t, err) + indentedBz, err := json.MarshalIndent(jsonObj, "", "\t") + require.NoError(t, err) + + // Make sure about: + // - Proposals use MsgExecLegacyContent + expected := `{ + "deposit_params": null, + "deposits": [], + "params": { + "max_deposit_period": "172800s", + "min_deposit": [ + { + "amount": "10000000", + "denom": "stake" + } + ], + "min_initial_deposit_ratio": "0.000000000000000000", + "quorum": "0.334000000000000000", + "threshold": "0.500000000000000000", + "veto_threshold": "0.334000000000000000", + "voting_period": "172800s" + }, + "proposals": [], + "starting_proposal_id": "1", + "tally_params": null, + "votes": [], + "voting_params": null +}` + + require.Equal(t, expected, string(indentedBz)) +} From 0523a2ee12e19dcdf6d7288672c265467222ee3f Mon Sep 17 00:00:00 2001 From: atheesh Date: Thu, 25 Aug 2022 19:20:31 +0530 Subject: [PATCH 2/5] add change log --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index de0f0433ed0f..04b9027a8b15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -136,6 +136,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/upgrade) [#12906](https://github.com/cosmos/cosmos-sdk/pull/12906) Fix upgrade failure by moving downgrade verification logic after store migration. * (store) [#12945](https://github.com/cosmos/cosmos-sdk/pull/12945) Fix nil end semantics in store/cachekv/iterator when iterating a dirty cache. * (export) [#13029](https://github.com/cosmos/cosmos-sdk/pull/13029) Fix exporting the blockParams regression. +* (x/gov) [#13045](https://github.com/cosmos/cosmos-sdk/pull/13045) Fix gov migrations for v3(0.46). + ### Deprecated From 8ecc7d79ded91c8dd6f2d01c8d0d296c8c81c58b Mon Sep 17 00:00:00 2001 From: atheeshp <59333759+atheeshp@users.noreply.github.com> Date: Mon, 29 Aug 2022 09:49:41 +0530 Subject: [PATCH 3/5] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d616a0a9a594..caa81209e359 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -138,7 +138,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (sdk/dec_coins) [#12903](https://github.com/cosmos/cosmos-sdk/pull/12903) Fix nil `DecCoin` creation when converting `Coins` to `DecCoins` * (x/upgrade) [#12906](https://github.com/cosmos/cosmos-sdk/pull/12906) Fix upgrade failure by moving downgrade verification logic after store migration. * (store) [#12945](https://github.com/cosmos/cosmos-sdk/pull/12945) Fix nil end semantics in store/cachekv/iterator when iterating a dirty cache. -* (export) [#13029](https://github.com/cosmos/cosmos-sdk/pull/13029) Fix exporting the blockParams regression +* (export) [#13029](https://github.com/cosmos/cosmos-sdk/pull/13029) Fix exporting the blockParams regression. * (x/gov) [#13051](https://github.com/cosmos/cosmos-sdk/pull/13051) In SubmitPropsal, when a legacy msg fails it's handler call, wrap the error as ErrInvalidProposalContent (instead of ErrNoProposalHandlerExists). * (x/gov) [#13045](https://github.com/cosmos/cosmos-sdk/pull/13045) Fix gov migrations for v3(0.46). From a95366a1f00e4a9df468c42299bd3005b67b6b0e Mon Sep 17 00:00:00 2001 From: atheesh Date: Mon, 29 Aug 2022 17:50:12 +0530 Subject: [PATCH 4/5] review changes --- x/genutil/migrations/v047/migrate.go | 2 +- x/gov/migrations/v4/json.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/x/genutil/migrations/v047/migrate.go b/x/genutil/migrations/v047/migrate.go index 5af007ff0aa0..699b24a0a590 100644 --- a/x/genutil/migrations/v047/migrate.go +++ b/x/genutil/migrations/v047/migrate.go @@ -28,7 +28,7 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { // delete deprecated x/gov genesis state delete(appState, v4gov.ModuleName) - // the respective key. + // set the x/gov genesis state with new state. new, err := v4gov.MigrateJSON(&old) if err != nil { panic(err) diff --git a/x/gov/migrations/v4/json.go b/x/gov/migrations/v4/json.go index 033281adff04..fa9e7decd442 100644 --- a/x/gov/migrations/v4/json.go +++ b/x/gov/migrations/v4/json.go @@ -5,7 +5,6 @@ import ( ) func MigrateJSON(oldState *v1.GenesisState) (*v1.GenesisState, error) { - params := v1.NewParams( oldState.DepositParams.MinDeposit, *oldState.DepositParams.MaxDepositPeriod, From f911f60e221804a647c1da5b8ac8fa83c5d7dd43 Mon Sep 17 00:00:00 2001 From: atheesh Date: Mon, 29 Aug 2022 17:58:46 +0530 Subject: [PATCH 5/5] review changes --- x/gov/migrations/v4/json.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/x/gov/migrations/v4/json.go b/x/gov/migrations/v4/json.go index fa9e7decd442..f633167aec34 100644 --- a/x/gov/migrations/v4/json.go +++ b/x/gov/migrations/v4/json.go @@ -17,6 +17,9 @@ func MigrateJSON(oldState *v1.GenesisState) (*v1.GenesisState, error) { return &v1.GenesisState{ StartingProposalId: oldState.StartingProposalId, + Deposits: oldState.Deposits, + Votes: oldState.Votes, + Proposals: oldState.Proposals, Params: ¶ms, }, nil }