From 67a04a5cf185462dfbf3fe3ea344114267d341fd Mon Sep 17 00:00:00 2001 From: Amaury <1293565+amaurym@users.noreply.github.com> Date: Fri, 3 Jun 2022 16:06:23 +0200 Subject: [PATCH] fix(staking): Fix JSON genesis migration (#12140) --- CHANGELOG.md | 1 + x/genutil/migrations/v046/migrate.go | 31 +++++++++++--- x/staking/migrations/v043/keys.go | 6 +++ x/staking/migrations/v046/json.go | 13 ++++++ x/staking/migrations/v046/json_test.go | 57 ++++++++++++++++++++++++++ x/staking/migrations/v046/keys.go | 6 +++ 6 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 x/staking/migrations/v043/keys.go create mode 100644 x/staking/migrations/v046/json.go create mode 100644 x/staking/migrations/v046/json_test.go create mode 100644 x/staking/migrations/v046/keys.go diff --git a/CHANGELOG.md b/CHANGELOG.md index afd9133aa7c1..f8226fb79b41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (baseapp) [#12089](https://github.com/cosmos/cosmos-sdk/pull/12089) Include antehandler and runMsgs events in SimulateTx. * (cli) [#12095](https://github.com/cosmos/cosmos-sdk/pull/12095) Fix running a tx with --dry-run returns an error * (x/auth) [#12108](https://github.com/cosmos/cosmos-sdk/pull/12108) Fix GetBlockWithTxs error when querying block with 0 tx +* (genutil) [#12140](https://github.com/cosmos/cosmos-sdk/pull/12140) Fix staking's genesis JSON migrate in the `simd migrate v0.46` CLI command. ## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23 diff --git a/x/genutil/migrations/v046/migrate.go b/x/genutil/migrations/v046/migrate.go index 10a04fd5bd55..b3fead6961dd 100644 --- a/x/genutil/migrations/v046/migrate.go +++ b/x/genutil/migrations/v046/migrate.go @@ -5,7 +5,10 @@ import ( "github.com/cosmos/cosmos-sdk/x/genutil/types" v043gov "github.com/cosmos/cosmos-sdk/x/gov/migrations/v043" v046gov "github.com/cosmos/cosmos-sdk/x/gov/migrations/v046" - gov "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" + govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" + v043staking "github.com/cosmos/cosmos-sdk/x/staking/migrations/v043" + v046staking "github.com/cosmos/cosmos-sdk/x/staking/migrations/v046" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) // Migrate migrates exported state from v0.43 to a v0.46 genesis state. @@ -13,19 +16,37 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { // Migrate x/gov. if appState[v043gov.ModuleName] != nil { // unmarshal relative source genesis application state - var oldGovState gov.GenesisState - clientCtx.Codec.MustUnmarshalJSON(appState[v043gov.ModuleName], &oldGovState) + var old govv1beta1.GenesisState + clientCtx.Codec.MustUnmarshalJSON(appState[v043gov.ModuleName], &old) // delete deprecated x/gov genesis state delete(appState, v043gov.ModuleName) // Migrate relative source genesis application state and marshal it into // the respective key. - newGovState, err := v046gov.MigrateJSON(&oldGovState) + new, err := v046gov.MigrateJSON(&old) if err != nil { panic(err) } - appState[v046gov.ModuleName] = clientCtx.Codec.MustMarshalJSON(newGovState) + appState[v046gov.ModuleName] = clientCtx.Codec.MustMarshalJSON(new) + } + + // Migrate x/staking. + if appState[v043staking.ModuleName] != nil { + // unmarshal relative source genesis application state + var old stakingtypes.GenesisState + clientCtx.Codec.MustUnmarshalJSON(appState[v043staking.ModuleName], &old) + + // delete deprecated x/staking genesis state + delete(appState, v043staking.ModuleName) + + // Migrate relative source genesis application state and marshal it into + // the respective key. + new, err := v046staking.MigrateJSON(old) + if err != nil { + panic(err) + } + appState[v046staking.ModuleName] = clientCtx.Codec.MustMarshalJSON(&new) } return appState diff --git a/x/staking/migrations/v043/keys.go b/x/staking/migrations/v043/keys.go new file mode 100644 index 000000000000..bca943d8aec8 --- /dev/null +++ b/x/staking/migrations/v043/keys.go @@ -0,0 +1,6 @@ +package v043 + +const ( + // ModuleName is the name of the module + ModuleName = "staking" +) diff --git a/x/staking/migrations/v046/json.go b/x/staking/migrations/v046/json.go new file mode 100644 index 000000000000..e3be292a2c74 --- /dev/null +++ b/x/staking/migrations/v046/json.go @@ -0,0 +1,13 @@ +package v046 + +import "github.com/cosmos/cosmos-sdk/x/staking/types" + +// MigrateJSON accepts exported v0.43 x/stakinng genesis state and migrates it to +// v0.46 x/staking genesis state. The migration includes: +// +// - Add MinCommissionRate param. +func MigrateJSON(oldState types.GenesisState) (types.GenesisState, error) { + oldState.Params.MinCommissionRate = types.DefaultMinCommissionRate + + return oldState, nil +} diff --git a/x/staking/migrations/v046/json_test.go b/x/staking/migrations/v046/json_test.go new file mode 100644 index 000000000000..df1d8db94329 --- /dev/null +++ b/x/staking/migrations/v046/json_test.go @@ -0,0 +1,57 @@ +package v046_test + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/simapp" + v046 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v046" + "github.com/cosmos/cosmos-sdk/x/staking/types" +) + +func TestMigrateJSON(t *testing.T) { + encodingConfig := simapp.MakeTestEncodingConfig() + clientCtx := client.Context{}. + WithInterfaceRegistry(encodingConfig.InterfaceRegistry). + WithTxConfig(encodingConfig.TxConfig). + WithCodec(encodingConfig.Codec) + + oldState := types.DefaultGenesisState() + + newState, err := v046.MigrateJSON(*oldState) + require.NoError(t, err) + + bz, err := clientCtx.Codec.MarshalJSON(&newState) + 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 new param MinCommissionRate. + expected := `{ + "delegations": [], + "exported": false, + "last_total_power": "0", + "last_validator_powers": [], + "params": { + "bond_denom": "stake", + "historical_entries": 10000, + "max_entries": 7, + "max_validators": 100, + "min_commission_rate": "0.000000000000000000", + "unbonding_time": "1814400s" + }, + "redelegations": [], + "unbonding_delegations": [], + "validators": [] +}` + + require.Equal(t, expected, string(indentedBz)) +} diff --git a/x/staking/migrations/v046/keys.go b/x/staking/migrations/v046/keys.go new file mode 100644 index 000000000000..f937f1593058 --- /dev/null +++ b/x/staking/migrations/v046/keys.go @@ -0,0 +1,6 @@ +package v046 + +const ( + // ModuleName is the name of the module + ModuleName = "staking" +)