fix: x/gov migrations (#13045)

* fix migration tests

* add change log

* Update CHANGELOG.md

* review changes

* review changes
This commit is contained in:
atheeshp 2022-08-29 18:45:36 +05:30 committed by GitHub
parent bc274d8d95
commit 08ed4371af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 124 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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: &params,
}, nil
}

View File

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