fix(staking): Fix JSON genesis migration (#12140)

This commit is contained in:
Amaury 2022-06-03 16:06:23 +02:00 committed by GitHub
parent 7120dc2d45
commit 67a04a5cf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 109 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,6 @@
package v043
const (
// ModuleName is the name of the module
ModuleName = "staking"
)

View File

@ -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
}

View File

@ -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))
}

View File

@ -0,0 +1,6 @@
package v046
const (
// ModuleName is the name of the module
ModuleName = "staking"
)