Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: x/gov migrations #13045

Merged
merged 7 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions x/genutil/migrations/v047/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
11 changes: 3 additions & 8 deletions x/gov/migrations/v3/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
23 changes: 12 additions & 11 deletions x/gov/migrations/v3/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": "",
Expand All @@ -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))
Expand Down
25 changes: 25 additions & 0 deletions x/gov/migrations/v4/json.go
Original file line number Diff line number Diff line change
@@ -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
}
64 changes: 64 additions & 0 deletions x/gov/migrations/v4/json_test.go
Original file line number Diff line number Diff line change
@@ -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))
}