Skip to content

Commit

Permalink
fix: x/gov migrations (cosmos#13045)
Browse files Browse the repository at this point in the history
* fix migration tests

* add change log

* Update CHANGELOG.md

* review changes

* review changes
  • Loading branch information
atheeshp authored and Wryhder committed Oct 26, 2022
1 parent 9d4b580 commit 87fc042
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 19 deletions.
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))
}

0 comments on commit 87fc042

Please sign in to comment.