From 26447565826d99c76afe5b720d4f6a4c624ef46b Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Fri, 3 Jun 2022 13:08:23 +0200 Subject: [PATCH 1/4] fix(staking): Fix JSON genesis migration --- x/genutil/migrations/v046/migrate.go | 31 +++++++++++--- x/staking/migrations/v043/keys.go | 6 +++ x/staking/migrations/v046/json.go | 27 ++++++++++++ x/staking/migrations/v046/json_test.go | 57 ++++++++++++++++++++++++++ x/staking/migrations/v046/keys.go | 6 +++ 5 files changed, 122 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/x/genutil/migrations/v046/migrate.go b/x/genutil/migrations/v046/migrate.go index 10a04fd5bd55..15880a23eeda 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/gov 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..8aa631f4000e --- /dev/null +++ b/x/staking/migrations/v046/json.go @@ -0,0 +1,27 @@ +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) { + return &types.GenesisState{ + Params: types.Params{ + UnbondingTime: oldState.Params.UnbondingTime, + MaxValidators: oldState.Params.MaxValidators, + MaxEntries: oldState.Params.MaxEntries, + HistoricalEntries: oldState.Params.HistoricalEntries, + BondDenom: oldState.Params.BondDenom, + MinCommissionRate: types.DefaultMinCommissionRate, + }, + LastTotalPower: oldState.LastTotalPower, + LastValidatorPowers: oldState.LastValidatorPowers, + Validators: oldState.Validators, + Delegations: oldState.Delegations, + UnbondingDelegations: oldState.UnbondingDelegations, + Redelegations: oldState.Redelegations, + Exported: oldState.Exported, + }, 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..eee43ff59207 --- /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" +) From 37c8728876154820b5633c7b658ed80e454f037c Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Fri, 3 Jun 2022 13:10:27 +0200 Subject: [PATCH 2/4] add cl --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e678450c462..708e25eba0c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (genutil) [#12140](https://github.com/cosmos/cosmos-sdk/pull/12140) Fix staking's genesis JSON migrate in the `simd migrate v0.46` CLI command. * (cli) [#12127](https://github.com/cosmos/cosmos-sdk/pull/12127) Fix the CLI not always taking into account `--fee-payer` and `--fee-granter` flags. * (migrations) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Fix v0.45->v0.46 in-place store migrations. * (baseapp) [#12089](https://github.com/cosmos/cosmos-sdk/pull/12089) Include antehandler and runMsgs events in SimulateTx. From 9bc5006f83a512900acb25b3d3eb152786e2dfaa Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Fri, 3 Jun 2022 13:13:29 +0200 Subject: [PATCH 3/4] less code --- x/genutil/migrations/v046/migrate.go | 4 ++-- x/staking/migrations/v046/json.go | 22 ++++------------------ x/staking/migrations/v046/json_test.go | 4 ++-- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/x/genutil/migrations/v046/migrate.go b/x/genutil/migrations/v046/migrate.go index 15880a23eeda..7d9106cb3410 100644 --- a/x/genutil/migrations/v046/migrate.go +++ b/x/genutil/migrations/v046/migrate.go @@ -42,11 +42,11 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { // Migrate relative source genesis application state and marshal it into // the respective key. - new, err := v046staking.MigrateJSON(&old) + new, err := v046staking.MigrateJSON(old) if err != nil { panic(err) } - appState[v046staking.ModuleName] = clientCtx.Codec.MustMarshalJSON(new) + appState[v046staking.ModuleName] = clientCtx.Codec.MustMarshalJSON(&new) } return appState diff --git a/x/staking/migrations/v046/json.go b/x/staking/migrations/v046/json.go index 8aa631f4000e..e3be292a2c74 100644 --- a/x/staking/migrations/v046/json.go +++ b/x/staking/migrations/v046/json.go @@ -6,22 +6,8 @@ import "github.com/cosmos/cosmos-sdk/x/staking/types" // v0.46 x/staking genesis state. The migration includes: // // - Add MinCommissionRate param. -func MigrateJSON(oldState *types.GenesisState) (*types.GenesisState, error) { - return &types.GenesisState{ - Params: types.Params{ - UnbondingTime: oldState.Params.UnbondingTime, - MaxValidators: oldState.Params.MaxValidators, - MaxEntries: oldState.Params.MaxEntries, - HistoricalEntries: oldState.Params.HistoricalEntries, - BondDenom: oldState.Params.BondDenom, - MinCommissionRate: types.DefaultMinCommissionRate, - }, - LastTotalPower: oldState.LastTotalPower, - LastValidatorPowers: oldState.LastValidatorPowers, - Validators: oldState.Validators, - Delegations: oldState.Delegations, - UnbondingDelegations: oldState.UnbondingDelegations, - Redelegations: oldState.Redelegations, - Exported: oldState.Exported, - }, nil +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 index eee43ff59207..df1d8db94329 100644 --- a/x/staking/migrations/v046/json_test.go +++ b/x/staking/migrations/v046/json_test.go @@ -21,10 +21,10 @@ func TestMigrateJSON(t *testing.T) { oldState := types.DefaultGenesisState() - newState, err := v046.MigrateJSON(oldState) + newState, err := v046.MigrateJSON(*oldState) require.NoError(t, err) - bz, err := clientCtx.Codec.MarshalJSON(newState) + bz, err := clientCtx.Codec.MarshalJSON(&newState) require.NoError(t, err) // Indent the JSON bz correctly. From 30d5bf0e209c89c6af87e549aea87307968378c1 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Fri, 3 Jun 2022 13:14:18 +0200 Subject: [PATCH 4/4] Update comment --- x/genutil/migrations/v046/migrate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/genutil/migrations/v046/migrate.go b/x/genutil/migrations/v046/migrate.go index 7d9106cb3410..b3fead6961dd 100644 --- a/x/genutil/migrations/v046/migrate.go +++ b/x/genutil/migrations/v046/migrate.go @@ -37,7 +37,7 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { var old stakingtypes.GenesisState clientCtx.Codec.MustUnmarshalJSON(appState[v043staking.ModuleName], &old) - // delete deprecated x/gov genesis state + // delete deprecated x/staking genesis state delete(appState, v043staking.ModuleName) // Migrate relative source genesis application state and marshal it into