Skip to content

Commit

Permalink
fix(staking): Fix JSON genesis migration (cosmos#12140)
Browse files Browse the repository at this point in the history
  • Loading branch information
amaury1093 authored Jun 3, 2022
1 parent 7120dc2 commit 67a04a5
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 26 additions & 5 deletions x/genutil/migrations/v046/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,48 @@ 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.
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
Expand Down
6 changes: 6 additions & 0 deletions x/staking/migrations/v043/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package v043

const (
// ModuleName is the name of the module
ModuleName = "staking"
)
13 changes: 13 additions & 0 deletions x/staking/migrations/v046/json.go
Original file line number Diff line number Diff line change
@@ -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
}
57 changes: 57 additions & 0 deletions x/staking/migrations/v046/json_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
6 changes: 6 additions & 0 deletions x/staking/migrations/v046/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package v046

const (
// ModuleName is the name of the module
ModuleName = "staking"
)

0 comments on commit 67a04a5

Please sign in to comment.