diff --git a/CHANGELOG.md b/CHANGELOG.md index 069a91f883ce..caa81209e359 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -140,6 +140,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (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) [#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). ### Deprecated diff --git a/x/genutil/migrations/v047/migrate.go b/x/genutil/migrations/v047/migrate.go index 2d322fb6174b..699b24a0a590 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) + + // set the x/gov genesis state with new state. + 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..f633167aec34 --- /dev/null +++ b/x/gov/migrations/v4/json.go @@ -0,0 +1,25 @@ +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, + Deposits: oldState.Deposits, + Votes: oldState.Votes, + Proposals: oldState.Proposals, + 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)) +}