From 267ef390f1dd38c7584c5bad37dce6725d58732e Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Tue, 6 Oct 2020 19:00:43 +0200 Subject: [PATCH 01/15] Migrate staking module --- x/genutil/legacy/v040/migrate.go | 18 ++++- x/staking/legacy/v038/types.go | 18 ++++- x/staking/legacy/v040/migrate.go | 116 +++++++++++++++++++++++++++++++ x/staking/legacy/v040/types.go | 6 ++ 4 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 x/staking/legacy/v040/migrate.go create mode 100644 x/staking/legacy/v040/types.go diff --git a/x/genutil/legacy/v040/migrate.go b/x/genutil/legacy/v040/migrate.go index 121c24b0c167..8c3b8ed5da52 100644 --- a/x/genutil/legacy/v040/migrate.go +++ b/x/genutil/legacy/v040/migrate.go @@ -14,6 +14,8 @@ import ( "github.com/cosmos/cosmos-sdk/x/genutil/types" v039slashing "github.com/cosmos/cosmos-sdk/x/slashing/legacy/v039" v040slashing "github.com/cosmos/cosmos-sdk/x/slashing/legacy/v040" + v038staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v038" + v040staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v040" ) // Migrate migrates exported state from v0.39 to a v0.40 genesis state. @@ -82,7 +84,7 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { var slashingGenState v039slashing.GenesisState v039Codec.MustUnmarshalJSON(appState[v039slashing.ModuleName], &slashingGenState) - // delete deprecated x/evidence genesis state + // delete deprecated x/slashing genesis state delete(appState, v039slashing.ModuleName) // Migrate relative source genesis application state and marshal it into @@ -90,5 +92,19 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { appState[v040slashing.ModuleName] = v040Codec.MustMarshalJSON(v040slashing.Migrate(slashingGenState)) } + // Migrate x/staking. + if appState[v038staking.ModuleName] != nil { + // unmarshal relative source genesis application state + var stakingGenState v038staking.GenesisState + v039Codec.MustUnmarshalJSON(appState[v038staking.ModuleName], &stakingGenState) + + // delete deprecated x/staking genesis state + delete(appState, v038staking.ModuleName) + + // Migrate relative source genesis application state and marshal it into + // the respective key. + appState[v040staking.ModuleName] = v040Codec.MustMarshalJSON(v040staking.Migrate(stakingGenState)) + } + return appState } diff --git a/x/staking/legacy/v038/types.go b/x/staking/legacy/v038/types.go index def28b31cbf8..3e6cb1d0a889 100644 --- a/x/staking/legacy/v038/types.go +++ b/x/staking/legacy/v038/types.go @@ -56,8 +56,16 @@ type ( Validators []Validator + Params struct { + UnbondingTime time.Duration `json:"unbonding_time" yaml:"unbonding_time"` // time duration of unbonding + MaxValidators uint16 `json:"max_validators" yaml:"max_validators"` // maximum number of validators (max uint16 = 65535) + MaxEntries uint16 `json:"max_entries" yaml:"max_entries"` // max entries for either unbonding delegation or redelegation (per pair/trio) + HistoricalEntries uint16 `json:"historical_entries" yaml:"historical_entries"` // number of historical entries to persist + BondDenom string `json:"bond_denom" yaml:"bond_denom"` // bondable coin denomination + } + GenesisState struct { - Params v034staking.Params `json:"params"` + Params Params `json:"params"` LastTotalPower sdk.Int `json:"last_total_power"` LastValidatorPowers []v034staking.LastValidatorPower `json:"last_validator_powers"` Validators Validators `json:"validators"` @@ -87,7 +95,13 @@ func NewGenesisState( ) GenesisState { return GenesisState{ - Params: params, + Params: Params{ + UnbondingTime: params.UnbondingTime, + MaxValidators: params.MaxValidators, + MaxEntries: params.MaxEntries, + BondDenom: params.BondDenom, + HistoricalEntries: 0, + }, LastTotalPower: lastTotalPower, LastValidatorPowers: lastValPowers, Validators: validators, diff --git a/x/staking/legacy/v040/migrate.go b/x/staking/legacy/v040/migrate.go new file mode 100644 index 000000000000..c8de8d32dbbd --- /dev/null +++ b/x/staking/legacy/v040/migrate.go @@ -0,0 +1,116 @@ +package v040 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + v038staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v038" + v040staking "github.com/cosmos/cosmos-sdk/x/staking/types" +) + +// Migrate accepts exported v0.38 x/staking genesis state and migrates it to +// v0.40 x/staking genesis state. The migration includes: +// +// - Re-encode in v0.40 GenesisState +func Migrate(stakingState v038staking.GenesisState) *v040staking.GenesisState { + newLastValidatorPowers := make([]v040staking.LastValidatorPower, len(stakingState.LastValidatorPowers)) + for i, oldLastValidatorPower := range stakingState.LastValidatorPowers { + newLastValidatorPowers[i] = v040staking.LastValidatorPower{ + Address: oldLastValidatorPower.Address.String(), + Power: oldLastValidatorPower.Power, + } + } + + newValidators := make([]v040staking.Validator, len(stakingState.Validators)) + for i, oldValidator := range stakingState.Validators { + newValidators[i] = v040staking.Validator{ + OperatorAddress: oldValidator.OperatorAddress.String(), + ConsensusPubkey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, oldValidator.ConsPubKey), + Jailed: oldValidator.Jailed, + Status: oldValidator.Status, + Tokens: oldValidator.Tokens, + DelegatorShares: oldValidator.DelegatorShares, + Description: v040staking.Description{ + Moniker: oldValidator.Description.Moniker, + Identity: oldValidator.Description.Identity, + Website: oldValidator.Description.Website, + SecurityContact: oldValidator.Description.SecurityContact, + Details: oldValidator.Description.Details, + }, + UnbondingHeight: oldValidator.UnbondingHeight, + UnbondingTime: oldValidator.UnbondingCompletionTime, + Commission: v040staking.Commission{ + CommissionRates: v040staking.CommissionRates{ + Rate: oldValidator.Commission.Rate, + MaxRate: oldValidator.Commission.MaxRate, + MaxChangeRate: oldValidator.Commission.MaxChangeRate, + }, + UpdateTime: oldValidator.Commission.UpdateTime, + }, + MinSelfDelegation: oldValidator.MinSelfDelegation, + } + } + + newDelegations := make([]v040staking.Delegation, len(stakingState.Delegations)) + for i, oldDelegation := range stakingState.Delegations { + newDelegations[i] = v040staking.Delegation{ + DelegatorAddress: oldDelegation.DelegatorAddress.String(), + ValidatorAddress: oldDelegation.ValidatorAddress.String(), + Shares: oldDelegation.Shares, + } + } + + newUnbondingDelegations := make([]v040staking.UnbondingDelegation, len(stakingState.UnbondingDelegations)) + for i, oldUnbondingDelegation := range stakingState.UnbondingDelegations { + newEntries := make([]v040staking.UnbondingDelegationEntry, len(oldUnbondingDelegation.Entries)) + for j, oldEntry := range oldUnbondingDelegation.Entries { + newEntries[j] = v040staking.UnbondingDelegationEntry{ + CreationHeight: oldEntry.CreationHeight, + CompletionTime: oldEntry.CompletionTime, + InitialBalance: oldEntry.InitialBalance, + Balance: oldEntry.Balance, + } + } + + newUnbondingDelegations[i] = v040staking.UnbondingDelegation{ + DelegatorAddress: oldUnbondingDelegation.DelegatorAddress.String(), + ValidatorAddress: oldUnbondingDelegation.ValidatorAddress.String(), + Entries: newEntries, + } + } + + newRedelegations := make([]v040staking.Redelegation, len(stakingState.Redelegations)) + for i, oldRedelegation := range stakingState.Redelegations { + newEntries := make([]v040staking.RedelegationEntry, len(oldRedelegation.Entries)) + for j, oldEntry := range oldRedelegation.Entries { + newEntries[j] = v040staking.RedelegationEntry{ + CreationHeight: oldEntry.CreationHeight, + CompletionTime: oldEntry.CompletionTime, + InitialBalance: oldEntry.InitialBalance, + SharesDst: oldEntry.SharesDst, + } + } + + newRedelegations[i] = v040staking.Redelegation{ + DelegatorAddress: oldRedelegation.DelegatorAddress.String(), + ValidatorSrcAddress: oldRedelegation.ValidatorSrcAddress.String(), + ValidatorDstAddress: oldRedelegation.ValidatorDstAddress.String(), + Entries: newEntries, + } + } + + return &v040staking.GenesisState{ + Params: v040staking.Params{ + UnbondingTime: stakingState.Params.UnbondingTime, + MaxValidators: uint32(stakingState.Params.MaxValidators), + MaxEntries: uint32(stakingState.Params.MaxEntries), + HistoricalEntries: uint32(stakingState.Params.HistoricalEntries), + BondDenom: stakingState.Params.BondDenom, + }, + LastTotalPower: stakingState.LastTotalPower, + LastValidatorPowers: newLastValidatorPowers, + Validators: newValidators, + Delegations: newDelegations, + UnbondingDelegations: newUnbondingDelegations, + Redelegations: newRedelegations, + Exported: stakingState.Exported, + } +} diff --git a/x/staking/legacy/v040/types.go b/x/staking/legacy/v040/types.go new file mode 100644 index 000000000000..cff719fb8f66 --- /dev/null +++ b/x/staking/legacy/v040/types.go @@ -0,0 +1,6 @@ +package v040 + +// Default parameter values +const ( + ModuleName = "staking" +) From 075a4ef3f6ae7dfa42c5e227aeefa9b7375d7d55 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Wed, 7 Oct 2020 13:43:53 +0200 Subject: [PATCH 02/15] Add gov legacy --- x/gov/legacy/v040/migrate.go | 116 +++++++++++++++++++++++++++++++++++ x/gov/legacy/v040/types.go | 6 ++ 2 files changed, 122 insertions(+) create mode 100644 x/gov/legacy/v040/migrate.go create mode 100644 x/gov/legacy/v040/types.go diff --git a/x/gov/legacy/v040/migrate.go b/x/gov/legacy/v040/migrate.go new file mode 100644 index 000000000000..e7a8f9c10154 --- /dev/null +++ b/x/gov/legacy/v040/migrate.go @@ -0,0 +1,116 @@ +package v040 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + v036gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v036" + v040gov "github.com/cosmos/cosmos-sdk/x/gov/types" +) + +// Migrate accepts exported v0.36 x/gov genesis state and migrates it to +// v0.40 x/gov genesis state. The migration includes: +// +// - Re-encode in v0.40 GenesisState +func Migrate(govState v036gov.GenesisState) *v040gov.GenesisState { + newLastValidatorPowers := make([]v040gov.LastValidatorPower, len(govState.LastValidatorPowers)) + for i, oldLastValidatorPower := range govState.LastValidatorPowers { + newLastValidatorPowers[i] = v040gov.LastValidatorPower{ + Address: oldLastValidatorPower.Address.String(), + Power: oldLastValidatorPower.Power, + } + } + + newValidators := make([]v040gov.Validator, len(govState.Validators)) + for i, oldValidator := range govState.Validators { + newValidators[i] = v040gov.Validator{ + OperatorAddress: oldValidator.OperatorAddress.String(), + ConsensusPubkey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, oldValidator.ConsPubKey), + Jailed: oldValidator.Jailed, + Status: oldValidator.Status, + Tokens: oldValidator.Tokens, + DelegatorShares: oldValidator.DelegatorShares, + Description: v040gov.Description{ + Moniker: oldValidator.Description.Moniker, + Identity: oldValidator.Description.Identity, + Website: oldValidator.Description.Website, + SecurityContact: oldValidator.Description.SecurityContact, + Details: oldValidator.Description.Details, + }, + UnbondingHeight: oldValidator.UnbondingHeight, + UnbondingTime: oldValidator.UnbondingCompletionTime, + Commission: v040gov.Commission{ + CommissionRates: v040gov.CommissionRates{ + Rate: oldValidator.Commission.Rate, + MaxRate: oldValidator.Commission.MaxRate, + MaxChangeRate: oldValidator.Commission.MaxChangeRate, + }, + UpdateTime: oldValidator.Commission.UpdateTime, + }, + MinSelfDelegation: oldValidator.MinSelfDelegation, + } + } + + newDelegations := make([]v040gov.Delegation, len(govState.Delegations)) + for i, oldDelegation := range govState.Delegations { + newDelegations[i] = v040gov.Delegation{ + DelegatorAddress: oldDelegation.DelegatorAddress.String(), + ValidatorAddress: oldDelegation.ValidatorAddress.String(), + Shares: oldDelegation.Shares, + } + } + + newUnbondingDelegations := make([]v040gov.UnbondingDelegation, len(govState.UnbondingDelegations)) + for i, oldUnbondingDelegation := range govState.UnbondingDelegations { + newEntries := make([]v040gov.UnbondingDelegationEntry, len(oldUnbondingDelegation.Entries)) + for j, oldEntry := range oldUnbondingDelegation.Entries { + newEntries[j] = v040gov.UnbondingDelegationEntry{ + CreationHeight: oldEntry.CreationHeight, + CompletionTime: oldEntry.CompletionTime, + InitialBalance: oldEntry.InitialBalance, + Balance: oldEntry.Balance, + } + } + + newUnbondingDelegations[i] = v040gov.UnbondingDelegation{ + DelegatorAddress: oldUnbondingDelegation.DelegatorAddress.String(), + ValidatorAddress: oldUnbondingDelegation.ValidatorAddress.String(), + Entries: newEntries, + } + } + + newRedelegations := make([]v040gov.Redelegation, len(govState.Redelegations)) + for i, oldRedelegation := range govState.Redelegations { + newEntries := make([]v040gov.RedelegationEntry, len(oldRedelegation.Entries)) + for j, oldEntry := range oldRedelegation.Entries { + newEntries[j] = v040gov.RedelegationEntry{ + CreationHeight: oldEntry.CreationHeight, + CompletionTime: oldEntry.CompletionTime, + InitialBalance: oldEntry.InitialBalance, + SharesDst: oldEntry.SharesDst, + } + } + + newRedelegations[i] = v040gov.Redelegation{ + DelegatorAddress: oldRedelegation.DelegatorAddress.String(), + ValidatorSrcAddress: oldRedelegation.ValidatorSrcAddress.String(), + ValidatorDstAddress: oldRedelegation.ValidatorDstAddress.String(), + Entries: newEntries, + } + } + + return &v040gov.GenesisState{ + Params: v040gov.Params{ + UnbondingTime: govState.Params.UnbondingTime, + MaxValidators: uint32(govState.Params.MaxValidators), + MaxEntries: uint32(govState.Params.MaxEntries), + HistoricalEntries: uint32(govState.Params.HistoricalEntries), + BondDenom: govState.Params.BondDenom, + }, + LastTotalPower: govState.LastTotalPower, + LastValidatorPowers: newLastValidatorPowers, + Validators: newValidators, + Delegations: newDelegations, + UnbondingDelegations: newUnbondingDelegations, + Redelegations: newRedelegations, + Exported: govState.Exported, + } +} diff --git a/x/gov/legacy/v040/types.go b/x/gov/legacy/v040/types.go new file mode 100644 index 000000000000..27f668b2e1aa --- /dev/null +++ b/x/gov/legacy/v040/types.go @@ -0,0 +1,6 @@ +package v040 + +// Default parameter values +const ( + ModuleName = "gov" +) From e1521d53e4783a52f7ca2f9281385717cc2ea8f5 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Wed, 7 Oct 2020 14:24:18 +0200 Subject: [PATCH 03/15] Add comments --- x/auth/legacy/v040/migrate.go | 1 + x/bank/legacy/v040/migrate.go | 1 + x/evidence/legacy/v040/migrate.go | 49 ++++---- x/gov/legacy/v040/migrate.go | 202 +++++++++++++++++------------- x/slashing/legacy/v040/migrate.go | 1 + x/staking/legacy/v040/migrate.go | 2 +- 6 files changed, 140 insertions(+), 116 deletions(-) diff --git a/x/auth/legacy/v040/migrate.go b/x/auth/legacy/v040/migrate.go index 4fe663a48d48..3d335804a50d 100644 --- a/x/auth/legacy/v040/migrate.go +++ b/x/auth/legacy/v040/migrate.go @@ -47,6 +47,7 @@ func convertBaseVestingAccount(old *v039auth.BaseVestingAccount) *v040vesting.Ba // it to v0.40 x/auth genesis state. The migration includes: // // - Removing coins from account encoding. +// - Re-encode in v0.40 GenesisState. func Migrate(authGenState v039auth.GenesisState) *v040auth.GenesisState { // Convert v0.39 accounts to v0.40 ones. var v040Accounts = make([]v040auth.GenesisAccount, len(authGenState.Accounts)) diff --git a/x/bank/legacy/v040/migrate.go b/x/bank/legacy/v040/migrate.go index ba617427648e..ab2e596f57c8 100644 --- a/x/bank/legacy/v040/migrate.go +++ b/x/bank/legacy/v040/migrate.go @@ -12,6 +12,7 @@ import ( // // - Moving balances from x/auth to x/bank genesis state. // - Moving supply from x/supply to x/bank genesis state. +// - Re-encode in v0.40 GenesisState. func Migrate( bankGenState v038bank.GenesisState, authGenState v039auth.GenesisState, diff --git a/x/evidence/legacy/v040/migrate.go b/x/evidence/legacy/v040/migrate.go index 7785cd12e791..cbcaa2ac7359 100644 --- a/x/evidence/legacy/v040/migrate.go +++ b/x/evidence/legacy/v040/migrate.go @@ -1,46 +1,43 @@ package v040 import ( + "fmt" + "github.com/cosmos/cosmos-sdk/client" codectypes "github.com/cosmos/cosmos-sdk/codec/types" v038evidence "github.com/cosmos/cosmos-sdk/x/evidence/legacy/v038" v040evidence "github.com/cosmos/cosmos-sdk/x/evidence/types" ) +func migrateEvidence(oldEvidence v038evidence.Evidence) *codectypes.Any { + switch oldEvidence := oldEvidence.(type) { + case *v040evidence.Equivocation: + { + any, err := codectypes.NewAnyWithValue(oldEvidence) + if err != nil { + panic(err) + } + + return any + } + default: + panic(fmt.Errorf("'%T' is not a valid evidence type", oldEvidence)) + } +} + // Migrate accepts exported v0.38 x/evidence genesis state and migrates it to // v0.40 x/evidence genesis state. The migration includes: // // - Removing the `Params` field. // - Converting Equivocations into Anys. +// - Re-encode in v0.40 GenesisState. func Migrate(evidenceState v038evidence.GenesisState, _ client.Context) *v040evidence.GenesisState { - var newEquivocations = make([]v040evidence.Equivocation, len(evidenceState.Evidence)) - for i, evidence := range evidenceState.Evidence { - equivocation, ok := evidence.(v038evidence.Equivocation) - if !ok { - // There's only equivocation in 0.38. - continue - } - - newEquivocations[i] = v040evidence.Equivocation{ - Height: equivocation.Height, - Time: equivocation.Time, - Power: equivocation.Power, - ConsensusAddress: equivocation.ConsensusAddress.String(), - } - } - - // Then convert the equivocations into Any. - newEvidence := make([]*codectypes.Any, len(newEquivocations)) - for i := range newEquivocations { - any, err := codectypes.NewAnyWithValue(&newEquivocations[i]) - if err != nil { - panic(err) - } - - newEvidence[i] = any + var newEvidences = make([]*codectypes.Any, len(evidenceState.Evidence)) + for i, oldEvidence := range evidenceState.Evidence { + newEvidences[i] = migrateEvidence(oldEvidence) } return &v040evidence.GenesisState{ - Evidence: newEvidence, + Evidence: newEvidences, } } diff --git a/x/gov/legacy/v040/migrate.go b/x/gov/legacy/v040/migrate.go index e7a8f9c10154..a46aa3f56950 100644 --- a/x/gov/legacy/v040/migrate.go +++ b/x/gov/legacy/v040/migrate.go @@ -1,116 +1,140 @@ package v040 import ( - sdk "github.com/cosmos/cosmos-sdk/types" + "fmt" + + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + v034gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v034" v036gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v036" v040gov "github.com/cosmos/cosmos-sdk/x/gov/types" ) -// Migrate accepts exported v0.36 x/gov genesis state and migrates it to -// v0.40 x/gov genesis state. The migration includes: -// -// - Re-encode in v0.40 GenesisState -func Migrate(govState v036gov.GenesisState) *v040gov.GenesisState { - newLastValidatorPowers := make([]v040gov.LastValidatorPower, len(govState.LastValidatorPowers)) - for i, oldLastValidatorPower := range govState.LastValidatorPowers { - newLastValidatorPowers[i] = v040gov.LastValidatorPower{ - Address: oldLastValidatorPower.Address.String(), - Power: oldLastValidatorPower.Power, - } - } +func migrateVoteOption(oldVoteOption v034gov.VoteOption) v040gov.VoteOption { + switch oldVoteOption { + case v034gov.OptionEmpty: + return v040gov.OptionEmpty - newValidators := make([]v040gov.Validator, len(govState.Validators)) - for i, oldValidator := range govState.Validators { - newValidators[i] = v040gov.Validator{ - OperatorAddress: oldValidator.OperatorAddress.String(), - ConsensusPubkey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, oldValidator.ConsPubKey), - Jailed: oldValidator.Jailed, - Status: oldValidator.Status, - Tokens: oldValidator.Tokens, - DelegatorShares: oldValidator.DelegatorShares, - Description: v040gov.Description{ - Moniker: oldValidator.Description.Moniker, - Identity: oldValidator.Description.Identity, - Website: oldValidator.Description.Website, - SecurityContact: oldValidator.Description.SecurityContact, - Details: oldValidator.Description.Details, - }, - UnbondingHeight: oldValidator.UnbondingHeight, - UnbondingTime: oldValidator.UnbondingCompletionTime, - Commission: v040gov.Commission{ - CommissionRates: v040gov.CommissionRates{ - Rate: oldValidator.Commission.Rate, - MaxRate: oldValidator.Commission.MaxRate, - MaxChangeRate: oldValidator.Commission.MaxChangeRate, - }, - UpdateTime: oldValidator.Commission.UpdateTime, - }, - MinSelfDelegation: oldValidator.MinSelfDelegation, - } + case v034gov.OptionYes: + return v040gov.OptionYes + + case v034gov.OptionAbstain: + return v040gov.OptionAbstain + + case v034gov.OptionNo: + return v040gov.OptionNo + + case v034gov.OptionNoWithVeto: + return v040gov.OptionNoWithVeto + + default: + panic(fmt.Errorf("'%s' is not a valid vote option", oldVoteOption)) } +} - newDelegations := make([]v040gov.Delegation, len(govState.Delegations)) - for i, oldDelegation := range govState.Delegations { - newDelegations[i] = v040gov.Delegation{ - DelegatorAddress: oldDelegation.DelegatorAddress.String(), - ValidatorAddress: oldDelegation.ValidatorAddress.String(), - Shares: oldDelegation.Shares, - } +func migrateProposalStatus(oldProposalStatus v034gov.ProposalStatus) v040gov.ProposalStatus { + switch oldProposalStatus { + + case v034gov.StatusNil: + return v040gov.StatusNil + + case v034gov.StatusDepositPeriod: + return v040gov.StatusDepositPeriod + + case v034gov.StatusVotingPeriod: + return v040gov.StatusVotingPeriod + + case v034gov.StatusPassed: + return v040gov.StatusPassed + + case v034gov.StatusRejected: + return v040gov.StatusRejected + + case v034gov.StatusFailed: + return v040gov.StatusFailed + + default: + panic(fmt.Errorf("'%s' is not a valid proposal status", oldProposalStatus)) } +} - newUnbondingDelegations := make([]v040gov.UnbondingDelegation, len(govState.UnbondingDelegations)) - for i, oldUnbondingDelegation := range govState.UnbondingDelegations { - newEntries := make([]v040gov.UnbondingDelegationEntry, len(oldUnbondingDelegation.Entries)) - for j, oldEntry := range oldUnbondingDelegation.Entries { - newEntries[j] = v040gov.UnbondingDelegationEntry{ - CreationHeight: oldEntry.CreationHeight, - CompletionTime: oldEntry.CompletionTime, - InitialBalance: oldEntry.InitialBalance, - Balance: oldEntry.Balance, +func migrateContent(oldContent v036gov.Content) *codectypes.Any { + switch oldContent := oldContent.(type) { + case *v040gov.TextProposal: + { + // Convert the content into Any. + contentAny, err := codectypes.NewAnyWithValue(oldContent) + if err != nil { + panic(err) } + + return contentAny } + default: + panic(fmt.Errorf("'%T' is not a valid proposal content type", oldContent)) + } +} - newUnbondingDelegations[i] = v040gov.UnbondingDelegation{ - DelegatorAddress: oldUnbondingDelegation.DelegatorAddress.String(), - ValidatorAddress: oldUnbondingDelegation.ValidatorAddress.String(), - Entries: newEntries, +// Migrate accepts exported v0.36 x/gov genesis state and migrates it to +// v0.40 x/gov genesis state. The migration includes: +// +// - Convert vote option & proposal status from byte to enum. +// - Migrate proposal content to Any. +// - Re-encode in v0.40 GenesisState. +func Migrate(oldGovState v036gov.GenesisState) *v040gov.GenesisState { + newDeposits := make([]v040gov.Deposit, len(oldGovState.Deposits)) + for i, oldDeposit := range oldGovState.Deposits { + newDeposits[i] = v040gov.Deposit{ + ProposalId: oldDeposit.ProposalID, + Depositor: oldDeposit.Depositor.String(), + Amount: oldDeposit.Amount, } } - newRedelegations := make([]v040gov.Redelegation, len(govState.Redelegations)) - for i, oldRedelegation := range govState.Redelegations { - newEntries := make([]v040gov.RedelegationEntry, len(oldRedelegation.Entries)) - for j, oldEntry := range oldRedelegation.Entries { - newEntries[j] = v040gov.RedelegationEntry{ - CreationHeight: oldEntry.CreationHeight, - CompletionTime: oldEntry.CompletionTime, - InitialBalance: oldEntry.InitialBalance, - SharesDst: oldEntry.SharesDst, - } + newVotes := make([]v040gov.Vote, len(oldGovState.Votes)) + for i, oldVote := range oldGovState.Votes { + newVotes[i] = v040gov.Vote{ + ProposalId: oldVote.ProposalID, + Voter: oldVote.Voter.String(), + Option: migrateVoteOption(oldVote.Option), } + } - newRedelegations[i] = v040gov.Redelegation{ - DelegatorAddress: oldRedelegation.DelegatorAddress.String(), - ValidatorSrcAddress: oldRedelegation.ValidatorSrcAddress.String(), - ValidatorDstAddress: oldRedelegation.ValidatorDstAddress.String(), - Entries: newEntries, + newProposals := make([]v040gov.Proposal, len(oldGovState.Proposals)) + for i, oldProposal := range oldGovState.Proposals { + newProposals[i] = v040gov.Proposal{ + ProposalId: oldProposal.ProposalID, + Content: migrateContent(oldProposal.Content), + Status: migrateProposalStatus(oldProposal.Status), + FinalTallyResult: v040gov.TallyResult{ + Yes: oldProposal.FinalTallyResult.Yes, + Abstain: oldProposal.FinalTallyResult.Abstain, + No: oldProposal.FinalTallyResult.No, + NoWithVeto: oldProposal.FinalTallyResult.NoWithVeto, + }, + SubmitTime: oldProposal.SubmitTime, + DepositEndTime: oldProposal.DepositEndTime, + TotalDeposit: oldProposal.TotalDeposit, + VotingStartTime: oldProposal.VotingStartTime, + VotingEndTime: oldProposal.VotingEndTime, } } return &v040gov.GenesisState{ - Params: v040gov.Params{ - UnbondingTime: govState.Params.UnbondingTime, - MaxValidators: uint32(govState.Params.MaxValidators), - MaxEntries: uint32(govState.Params.MaxEntries), - HistoricalEntries: uint32(govState.Params.HistoricalEntries), - BondDenom: govState.Params.BondDenom, + StartingProposalId: oldGovState.StartingProposalID, + Deposits: newDeposits, + Votes: newVotes, + Proposals: newProposals, + DepositParams: v040gov.DepositParams{ + MinDeposit: oldGovState.DepositParams.MinDeposit, + MaxDepositPeriod: oldGovState.DepositParams.MaxDepositPeriod, + }, + VotingParams: v040gov.VotingParams{ + VotingPeriod: oldGovState.VotingParams.VotingPeriod, + }, + TallyParams: v040gov.TallyParams{ + Quorum: oldGovState.TallyParams.Quorum, + Threshold: oldGovState.TallyParams.Threshold, + VetoThreshold: oldGovState.TallyParams.Veto, }, - LastTotalPower: govState.LastTotalPower, - LastValidatorPowers: newLastValidatorPowers, - Validators: newValidators, - Delegations: newDelegations, - UnbondingDelegations: newUnbondingDelegations, - Redelegations: newRedelegations, - Exported: govState.Exported, } } diff --git a/x/slashing/legacy/v040/migrate.go b/x/slashing/legacy/v040/migrate.go index 4f99af1446b0..c01fd501baaf 100644 --- a/x/slashing/legacy/v040/migrate.go +++ b/x/slashing/legacy/v040/migrate.go @@ -11,6 +11,7 @@ import ( // to v0.40 x/slashing genesis state. The migration includes: // // - Chaning SigningInfos and MissedBlocks from map to array. +// - Re-encode in v0.40 GenesisState. func Migrate(oldGenState v039slashing.GenesisState) *v040slashing.GenesisState { // Note that the two following `for` loop over a map's keys, so are not // deterministic. diff --git a/x/staking/legacy/v040/migrate.go b/x/staking/legacy/v040/migrate.go index c8de8d32dbbd..ad0bd2c9c1df 100644 --- a/x/staking/legacy/v040/migrate.go +++ b/x/staking/legacy/v040/migrate.go @@ -9,7 +9,7 @@ import ( // Migrate accepts exported v0.38 x/staking genesis state and migrates it to // v0.40 x/staking genesis state. The migration includes: // -// - Re-encode in v0.40 GenesisState +// - Re-encode in v0.40 GenesisState. func Migrate(stakingState v038staking.GenesisState) *v040staking.GenesisState { newLastValidatorPowers := make([]v040staking.LastValidatorPower, len(stakingState.LastValidatorPowers)) for i, oldLastValidatorPower := range stakingState.LastValidatorPowers { From ef05dde86056010b81e000d3795db218cd55cc01 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Wed, 7 Oct 2020 15:10:44 +0200 Subject: [PATCH 04/15] Add x/distrib --- x/distribution/legacy/v040/migrate.go | 108 +++++++++++++++++++++++++ x/distribution/legacy/v040/types.go | 6 ++ x/evidence/legacy/v040/migrate.go | 3 +- x/evidence/legacy/v040/migrate_test.go | 2 +- x/genutil/legacy/v040/migrate.go | 34 +++++++- x/gov/legacy/v040/migrate.go | 1 + x/slashing/legacy/v040/migrate.go | 1 + x/staking/legacy/v040/migrate.go | 1 + 8 files changed, 152 insertions(+), 4 deletions(-) create mode 100644 x/distribution/legacy/v040/migrate.go create mode 100644 x/distribution/legacy/v040/types.go diff --git a/x/distribution/legacy/v040/migrate.go b/x/distribution/legacy/v040/migrate.go new file mode 100644 index 000000000000..b0ba30bba861 --- /dev/null +++ b/x/distribution/legacy/v040/migrate.go @@ -0,0 +1,108 @@ +package v040 + +import ( + v038distribution "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v038" + v040distribution "github.com/cosmos/cosmos-sdk/x/distribution/types" +) + +// Migrate accepts exported x/distribution genesis state from v0.38 and migrates it +// to v0.40 x/distribution genesis state. The migration includes: +// +// - Convert addresses from bytes to bech32 strings. +// - Re-encode in v0.40 GenesisState. +func Migrate(oldDistributionState v038distribution.GenesisState) *v040distribution.GenesisState { + newDelegatorWithdrawInfos := make([]v040distribution.DelegatorWithdrawInfo, len(oldDistributionState.DelegatorWithdrawInfos)) + for i, oldDelegatorWithdrawInfo := range oldDistributionState.DelegatorWithdrawInfos { + newDelegatorWithdrawInfos[i] = v040distribution.DelegatorWithdrawInfo{ + DelegatorAddress: oldDelegatorWithdrawInfo.DelegatorAddress.String(), + WithdrawAddress: oldDelegatorWithdrawInfo.WithdrawAddress.String(), + } + } + + newValidatorOutstandingRewards := make([]v040distribution.ValidatorOutstandingRewardsRecord, len(oldDistributionState.OutstandingRewards)) + for i, oldValidatorOutstandingReward := range oldDistributionState.OutstandingRewards { + newValidatorOutstandingRewards[i] = v040distribution.ValidatorOutstandingRewardsRecord{ + ValidatorAddress: oldValidatorOutstandingReward.ValidatorAddress.String(), + OutstandingRewards: oldValidatorOutstandingReward.OutstandingRewards, + } + } + + newValidatorAccumulatedCommissions := make([]v040distribution.ValidatorAccumulatedCommissionRecord, len(oldDistributionState.ValidatorAccumulatedCommissions)) + for i, oldValidatorAccumulatedCommission := range oldDistributionState.ValidatorAccumulatedCommissions { + newValidatorAccumulatedCommissions[i] = v040distribution.ValidatorAccumulatedCommissionRecord{ + ValidatorAddress: oldValidatorAccumulatedCommission.ValidatorAddress.String(), + Accumulated: v040distribution.ValidatorAccumulatedCommission{ + Commission: oldValidatorAccumulatedCommission.Accumulated, + }, + } + } + + newValidatorHistoricalRewards := make([]v040distribution.ValidatorHistoricalRewardsRecord, len(oldDistributionState.ValidatorHistoricalRewards)) + for i, oldValidatorHistoricalReward := range oldDistributionState.ValidatorHistoricalRewards { + newValidatorHistoricalRewards[i] = v040distribution.ValidatorHistoricalRewardsRecord{ + ValidatorAddress: oldValidatorHistoricalReward.ValidatorAddress.String(), + Period: oldValidatorHistoricalReward.Period, + Rewards: v040distribution.ValidatorHistoricalRewards{ + CumulativeRewardRatio: oldValidatorHistoricalReward.Rewards.CumulativeRewardRatio, + ReferenceCount: uint32(oldValidatorHistoricalReward.Rewards.ReferenceCount), + }, + } + } + + newValidatorCurrentRewards := make([]v040distribution.ValidatorCurrentRewardsRecord, len(oldDistributionState.ValidatorCurrentRewards)) + for i, oldValidatorCurrentReward := range oldDistributionState.ValidatorCurrentRewards { + newValidatorCurrentRewards[i] = v040distribution.ValidatorCurrentRewardsRecord{ + ValidatorAddress: oldValidatorCurrentReward.ValidatorAddress.String(), + Rewards: v040distribution.ValidatorCurrentRewards{ + Rewards: oldValidatorCurrentReward.Rewards.Rewards, + Period: oldValidatorCurrentReward.Rewards.Period, + }, + } + } + + newDelegatorStartingInfos := make([]v040distribution.DelegatorStartingInfoRecord, len(oldDistributionState.DelegatorStartingInfos)) + for i, oldDelegatorStartingInfo := range oldDistributionState.DelegatorStartingInfos { + newDelegatorStartingInfos[i] = v040distribution.DelegatorStartingInfoRecord{ + DelegatorAddress: oldDelegatorStartingInfo.DelegatorAddress.String(), + ValidatorAddress: oldDelegatorStartingInfo.ValidatorAddress.String(), + StartingInfo: v040distribution.DelegatorStartingInfo{ + PreviousPeriod: oldDelegatorStartingInfo.StartingInfo.PreviousPeriod, + Stake: oldDelegatorStartingInfo.StartingInfo.Stake, + Height: oldDelegatorStartingInfo.StartingInfo.Height, + }, + } + } + + newValidatorSlashEvents := make([]v040distribution.ValidatorSlashEventRecord, len(oldDistributionState.ValidatorSlashEvents)) + for i, oldValidatorSlashEvent := range oldDistributionState.ValidatorSlashEvents { + newValidatorSlashEvents[i] = v040distribution.ValidatorSlashEventRecord{ + ValidatorAddress: oldValidatorSlashEvent.ValidatorAddress.String(), + Height: oldValidatorSlashEvent.Height, + Period: oldValidatorSlashEvent.Period, + ValidatorSlashEvent: v040distribution.ValidatorSlashEvent{ + ValidatorPeriod: oldValidatorSlashEvent.Event.ValidatorPeriod, + Fraction: oldValidatorSlashEvent.Event.Fraction, + }, + } + } + + return &v040distribution.GenesisState{ + Params: v040distribution.Params{ + CommunityTax: oldDistributionState.Params.CommunityTax, + BaseProposerReward: oldDistributionState.Params.BaseProposerReward, + BonusProposerReward: oldDistributionState.Params.BonusProposerReward, + WithdrawAddrEnabled: oldDistributionState.Params.WithdrawAddrEnabled, + }, + FeePool: v040distribution.FeePool{ + CommunityPool: oldDistributionState.FeePool.CommunityPool, + }, + DelegatorWithdrawInfos: newDelegatorWithdrawInfos, + PreviousProposer: oldDistributionState.PreviousProposer.String(), + OutstandingRewards: newValidatorOutstandingRewards, + ValidatorAccumulatedCommissions: newValidatorAccumulatedCommissions, + ValidatorHistoricalRewards: newValidatorHistoricalRewards, + ValidatorCurrentRewards: newValidatorCurrentRewards, + DelegatorStartingInfos: newDelegatorStartingInfos, + ValidatorSlashEvents: newValidatorSlashEvents, + } +} diff --git a/x/distribution/legacy/v040/types.go b/x/distribution/legacy/v040/types.go new file mode 100644 index 000000000000..27f668b2e1aa --- /dev/null +++ b/x/distribution/legacy/v040/types.go @@ -0,0 +1,6 @@ +package v040 + +// Default parameter values +const ( + ModuleName = "gov" +) diff --git a/x/evidence/legacy/v040/migrate.go b/x/evidence/legacy/v040/migrate.go index cbcaa2ac7359..ec7178cb845c 100644 --- a/x/evidence/legacy/v040/migrate.go +++ b/x/evidence/legacy/v040/migrate.go @@ -3,7 +3,6 @@ package v040 import ( "fmt" - "github.com/cosmos/cosmos-sdk/client" codectypes "github.com/cosmos/cosmos-sdk/codec/types" v038evidence "github.com/cosmos/cosmos-sdk/x/evidence/legacy/v038" v040evidence "github.com/cosmos/cosmos-sdk/x/evidence/types" @@ -31,7 +30,7 @@ func migrateEvidence(oldEvidence v038evidence.Evidence) *codectypes.Any { // - Removing the `Params` field. // - Converting Equivocations into Anys. // - Re-encode in v0.40 GenesisState. -func Migrate(evidenceState v038evidence.GenesisState, _ client.Context) *v040evidence.GenesisState { +func Migrate(evidenceState v038evidence.GenesisState) *v040evidence.GenesisState { var newEvidences = make([]*codectypes.Any, len(evidenceState.Evidence)) for i, oldEvidence := range evidenceState.Evidence { newEvidences[i] = migrateEvidence(oldEvidence) diff --git a/x/evidence/legacy/v040/migrate_test.go b/x/evidence/legacy/v040/migrate_test.go index dcbe77e487ca..a9c23093f47f 100644 --- a/x/evidence/legacy/v040/migrate_test.go +++ b/x/evidence/legacy/v040/migrate_test.go @@ -31,7 +31,7 @@ func TestMigrate(t *testing.T) { }}, } - migrated := v040evidence.Migrate(evidenceGenState, clientCtx) + migrated := v040evidence.Migrate(evidenceGenState) expected := `{"evidence":[{"@type":"/cosmos.evidence.v1beta1.Equivocation","height":"20","time":"0001-01-01T00:00:00Z","power":"100","consensus_address":"cosmosvalcons1xxkueklal9vejv9unqu80w9vptyepfa99x2a3w"}]}` bz, err := clientCtx.JSONMarshaler.MarshalJSON(migrated) diff --git a/x/genutil/legacy/v040/migrate.go b/x/genutil/legacy/v040/migrate.go index 8c3b8ed5da52..675e1072b28e 100644 --- a/x/genutil/legacy/v040/migrate.go +++ b/x/genutil/legacy/v040/migrate.go @@ -9,9 +9,13 @@ import ( v036supply "github.com/cosmos/cosmos-sdk/x/bank/legacy/v036" v038bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v038" v040bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v040" + v038distribution "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v038" + v040distribution "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v040" v038evidence "github.com/cosmos/cosmos-sdk/x/evidence/legacy/v038" v040evidence "github.com/cosmos/cosmos-sdk/x/evidence/legacy/v040" "github.com/cosmos/cosmos-sdk/x/genutil/types" + v036gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v036" + v040gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v040" v039slashing "github.com/cosmos/cosmos-sdk/x/slashing/legacy/v039" v040slashing "github.com/cosmos/cosmos-sdk/x/slashing/legacy/v040" v038staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v038" @@ -64,6 +68,20 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { appState[v040auth.ModuleName] = v040Codec.MustMarshalJSON(v040auth.Migrate(authGenState)) } + // Migrate x/distribution. + if appState[v038distribution.ModuleName] != nil { + // unmarshal relative source genesis application state + var distributionGenState v038distribution.GenesisState + v039Codec.MustUnmarshalJSON(appState[v038distribution.ModuleName], &distributionGenState) + + // delete deprecated x/distribution genesis state + delete(appState, v038distribution.ModuleName) + + // Migrate relative source genesis application state and marshal it into + // the respective key. + appState[v040distribution.ModuleName] = v040Codec.MustMarshalJSON(v040distribution.Migrate(distributionGenState)) + } + // Migrate x/evidence. if appState[v038evidence.ModuleName] != nil { // unmarshal relative source genesis application state @@ -75,7 +93,21 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { // Migrate relative source genesis application state and marshal it into // the respective key. - appState[v040evidence.ModuleName] = v040Codec.MustMarshalJSON(v040evidence.Migrate(evidenceGenState, clientCtx)) + appState[v040evidence.ModuleName] = v040Codec.MustMarshalJSON(v040evidence.Migrate(evidenceGenState)) + } + + // Migrate x/gov. + if appState[v036gov.ModuleName] != nil { + // unmarshal relative source genesis application state + var govGenState v036gov.GenesisState + v039Codec.MustUnmarshalJSON(appState[v036gov.ModuleName], &govGenState) + + // delete deprecated x/gov genesis state + delete(appState, v036gov.ModuleName) + + // Migrate relative source genesis application state and marshal it into + // the respective key. + appState[v040gov.ModuleName] = v040Codec.MustMarshalJSON(v040gov.Migrate(govGenState)) } // Migrate x/slashing. diff --git a/x/gov/legacy/v040/migrate.go b/x/gov/legacy/v040/migrate.go index a46aa3f56950..73ee066dd60b 100644 --- a/x/gov/legacy/v040/migrate.go +++ b/x/gov/legacy/v040/migrate.go @@ -79,6 +79,7 @@ func migrateContent(oldContent v036gov.Content) *codectypes.Any { // // - Convert vote option & proposal status from byte to enum. // - Migrate proposal content to Any. +// - Convert addresses from bytes to bech32 strings. // - Re-encode in v0.40 GenesisState. func Migrate(oldGovState v036gov.GenesisState) *v040gov.GenesisState { newDeposits := make([]v040gov.Deposit, len(oldGovState.Deposits)) diff --git a/x/slashing/legacy/v040/migrate.go b/x/slashing/legacy/v040/migrate.go index c01fd501baaf..ba494dcad14a 100644 --- a/x/slashing/legacy/v040/migrate.go +++ b/x/slashing/legacy/v040/migrate.go @@ -11,6 +11,7 @@ import ( // to v0.40 x/slashing genesis state. The migration includes: // // - Chaning SigningInfos and MissedBlocks from map to array. +// - Convert addresses from bytes to bech32 strings. // - Re-encode in v0.40 GenesisState. func Migrate(oldGenState v039slashing.GenesisState) *v040slashing.GenesisState { // Note that the two following `for` loop over a map's keys, so are not diff --git a/x/staking/legacy/v040/migrate.go b/x/staking/legacy/v040/migrate.go index ad0bd2c9c1df..07a4eba87a5f 100644 --- a/x/staking/legacy/v040/migrate.go +++ b/x/staking/legacy/v040/migrate.go @@ -9,6 +9,7 @@ import ( // Migrate accepts exported v0.38 x/staking genesis state and migrates it to // v0.40 x/staking genesis state. The migration includes: // +// - Convert addresses from bytes to bech32 strings. // - Re-encode in v0.40 GenesisState. func Migrate(stakingState v038staking.GenesisState) *v040staking.GenesisState { newLastValidatorPowers := make([]v040staking.LastValidatorPower, len(stakingState.LastValidatorPowers)) From e8488ae05de8a9829b87f434d45871f47504bc70 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Wed, 7 Oct 2020 15:15:39 +0200 Subject: [PATCH 05/15] x/crisis --- x/crisis/legacy/v039/types.go | 13 +++++++++++++ x/crisis/legacy/v040/migrate.go | 16 ++++++++++++++++ x/crisis/legacy/v040/types.go | 5 +++++ x/distribution/legacy/v040/types.go | 2 +- x/genutil/legacy/v040/migrate.go | 16 ++++++++++++++++ x/staking/legacy/v040/types.go | 1 - 6 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 x/crisis/legacy/v039/types.go create mode 100644 x/crisis/legacy/v040/migrate.go create mode 100644 x/crisis/legacy/v040/types.go diff --git a/x/crisis/legacy/v039/types.go b/x/crisis/legacy/v039/types.go new file mode 100644 index 000000000000..44903e349118 --- /dev/null +++ b/x/crisis/legacy/v039/types.go @@ -0,0 +1,13 @@ +package v039 + +import sdk "github.com/cosmos/cosmos-sdk/types" + +const ( + ModuleName = "crisis" +) + +type ( + GenesisState struct { + ConstantFee sdk.Coin `json:"constant_fee" yaml:"constant_fee"` + } +) diff --git a/x/crisis/legacy/v040/migrate.go b/x/crisis/legacy/v040/migrate.go new file mode 100644 index 000000000000..f3af6c6cd827 --- /dev/null +++ b/x/crisis/legacy/v040/migrate.go @@ -0,0 +1,16 @@ +package v040 + +import ( + v039crisis "github.com/cosmos/cosmos-sdk/x/crisis/legacy/v039" + v040crisis "github.com/cosmos/cosmos-sdk/x/crisis/types" +) + +// Migrate accepts exported v0.39 x/crisis enesis state and +// migrates it to v0.40 x/crisis genesis state. The migration includes: +// +// - Re-encode in v0.40 GenesisState. +func Migrate(crisisGenState v039crisis.GenesisState) *v040crisis.GenesisState { + return &v040crisis.GenesisState{ + ConstantFee: crisisGenState.ConstantFee, + } +} diff --git a/x/crisis/legacy/v040/types.go b/x/crisis/legacy/v040/types.go new file mode 100644 index 000000000000..04ac05f9fcd3 --- /dev/null +++ b/x/crisis/legacy/v040/types.go @@ -0,0 +1,5 @@ +package v040 + +const ( + ModuleName = "bank" +) diff --git a/x/distribution/legacy/v040/types.go b/x/distribution/legacy/v040/types.go index 27f668b2e1aa..aa502303803e 100644 --- a/x/distribution/legacy/v040/types.go +++ b/x/distribution/legacy/v040/types.go @@ -2,5 +2,5 @@ package v040 // Default parameter values const ( - ModuleName = "gov" + ModuleName = "distribution" ) diff --git a/x/genutil/legacy/v040/migrate.go b/x/genutil/legacy/v040/migrate.go index 675e1072b28e..2d3a4d04d65a 100644 --- a/x/genutil/legacy/v040/migrate.go +++ b/x/genutil/legacy/v040/migrate.go @@ -9,6 +9,8 @@ import ( v036supply "github.com/cosmos/cosmos-sdk/x/bank/legacy/v036" v038bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v038" v040bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v040" + v039crisis "github.com/cosmos/cosmos-sdk/x/crisis/legacy/v039" + v040crisis "github.com/cosmos/cosmos-sdk/x/crisis/legacy/v040" v038distribution "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v038" v040distribution "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v040" v038evidence "github.com/cosmos/cosmos-sdk/x/evidence/legacy/v038" @@ -68,6 +70,20 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { appState[v040auth.ModuleName] = v040Codec.MustMarshalJSON(v040auth.Migrate(authGenState)) } + // Migrate x/crisis. + if appState[v039crisis.ModuleName] != nil { + // unmarshal relative source genesis application state + var crisisGenState v039crisis.GenesisState + v039Codec.MustUnmarshalJSON(appState[v039crisis.ModuleName], &crisisGenState) + + // delete deprecated x/crisis genesis state + delete(appState, v039crisis.ModuleName) + + // Migrate relative source genesis application state and marshal it into + // the respective key. + appState[v040crisis.ModuleName] = v040Codec.MustMarshalJSON(v040crisis.Migrate(crisisGenState)) + } + // Migrate x/distribution. if appState[v038distribution.ModuleName] != nil { // unmarshal relative source genesis application state diff --git a/x/staking/legacy/v040/types.go b/x/staking/legacy/v040/types.go index cff719fb8f66..6ad280e277e5 100644 --- a/x/staking/legacy/v040/types.go +++ b/x/staking/legacy/v040/types.go @@ -1,6 +1,5 @@ package v040 -// Default parameter values const ( ModuleName = "staking" ) From ab59c836f4c51fed7c3259f75cc19e7d5bb0b218 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Wed, 7 Oct 2020 15:21:18 +0200 Subject: [PATCH 06/15] x/mint --- x/crisis/legacy/v040/migrate.go | 2 +- x/crisis/legacy/v040/types.go | 2 +- x/genutil/legacy/v040/migrate.go | 16 ++++++++++++++++ x/mint/legacy/v039/types.go | 31 +++++++++++++++++++++++++++++++ x/mint/legacy/v040/migrate.go | 27 +++++++++++++++++++++++++++ x/mint/legacy/v040/types.go | 5 +++++ 6 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 x/mint/legacy/v039/types.go create mode 100644 x/mint/legacy/v040/migrate.go create mode 100644 x/mint/legacy/v040/types.go diff --git a/x/crisis/legacy/v040/migrate.go b/x/crisis/legacy/v040/migrate.go index f3af6c6cd827..eb97ba9ee645 100644 --- a/x/crisis/legacy/v040/migrate.go +++ b/x/crisis/legacy/v040/migrate.go @@ -5,7 +5,7 @@ import ( v040crisis "github.com/cosmos/cosmos-sdk/x/crisis/types" ) -// Migrate accepts exported v0.39 x/crisis enesis state and +// Migrate accepts exported v0.39 x/crisis genesis state and // migrates it to v0.40 x/crisis genesis state. The migration includes: // // - Re-encode in v0.40 GenesisState. diff --git a/x/crisis/legacy/v040/types.go b/x/crisis/legacy/v040/types.go index 04ac05f9fcd3..68c25e93eec1 100644 --- a/x/crisis/legacy/v040/types.go +++ b/x/crisis/legacy/v040/types.go @@ -1,5 +1,5 @@ package v040 const ( - ModuleName = "bank" + ModuleName = "crisis" ) diff --git a/x/genutil/legacy/v040/migrate.go b/x/genutil/legacy/v040/migrate.go index 2d3a4d04d65a..9b88d1474696 100644 --- a/x/genutil/legacy/v040/migrate.go +++ b/x/genutil/legacy/v040/migrate.go @@ -18,6 +18,8 @@ import ( "github.com/cosmos/cosmos-sdk/x/genutil/types" v036gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v036" v040gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v040" + v039mint "github.com/cosmos/cosmos-sdk/x/mint/legacy/v039" + v040mint "github.com/cosmos/cosmos-sdk/x/mint/legacy/v040" v039slashing "github.com/cosmos/cosmos-sdk/x/slashing/legacy/v039" v040slashing "github.com/cosmos/cosmos-sdk/x/slashing/legacy/v040" v038staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v038" @@ -126,6 +128,20 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { appState[v040gov.ModuleName] = v040Codec.MustMarshalJSON(v040gov.Migrate(govGenState)) } + // Migrate x/mint. + if appState[v039mint.ModuleName] != nil { + // unmarshal relative source genesis application state + var mintGenState v039mint.GenesisState + v039Codec.MustUnmarshalJSON(appState[v039mint.ModuleName], &mintGenState) + + // delete deprecated x/mint genesis state + delete(appState, v039mint.ModuleName) + + // Migrate relative source genesis application state and marshal it into + // the respective key. + appState[v040mint.ModuleName] = v040Codec.MustMarshalJSON(v040mint.Migrate(mintGenState)) + } + // Migrate x/slashing. if appState[v039slashing.ModuleName] != nil { // unmarshal relative source genesis application state diff --git a/x/mint/legacy/v039/types.go b/x/mint/legacy/v039/types.go new file mode 100644 index 000000000000..10b351b7fb8f --- /dev/null +++ b/x/mint/legacy/v039/types.go @@ -0,0 +1,31 @@ +package v039 + +import sdk "github.com/cosmos/cosmos-sdk/types" + +const ( + ModuleName = "mint" +) + +type ( + // Minter represents the minting state. + Minter struct { + Inflation sdk.Dec `json:"inflation" yaml:"inflation"` // current annual inflation rate + AnnualProvisions sdk.Dec `json:"annual_provisions" yaml:"annual_provisions"` // current annual expected provisions + } + + // mint parameters + Params struct { + MintDenom string `json:"mint_denom" yaml:"mint_denom"` // type of coin to mint + InflationRateChange sdk.Dec `json:"inflation_rate_change" yaml:"inflation_rate_change"` // maximum annual change in inflation rate + InflationMax sdk.Dec `json:"inflation_max" yaml:"inflation_max"` // maximum inflation rate + InflationMin sdk.Dec `json:"inflation_min" yaml:"inflation_min"` // minimum inflation rate + GoalBonded sdk.Dec `json:"goal_bonded" yaml:"goal_bonded"` // goal of percent bonded atoms + BlocksPerYear uint64 `json:"blocks_per_year" yaml:"blocks_per_year"` // expected blocks per year + } + + // GenesisState - minter state + GenesisState struct { + Minter Minter `json:"minter" yaml:"minter"` // minter object + Params Params `json:"params" yaml:"params"` // inflation params + } +) diff --git a/x/mint/legacy/v040/migrate.go b/x/mint/legacy/v040/migrate.go new file mode 100644 index 000000000000..b417c6828951 --- /dev/null +++ b/x/mint/legacy/v040/migrate.go @@ -0,0 +1,27 @@ +package v040 + +import ( + v039mint "github.com/cosmos/cosmos-sdk/x/mint/legacy/v039" + v040mint "github.com/cosmos/cosmos-sdk/x/mint/types" +) + +// Migrate accepts exported v0.39 x/mint genesis state and +// migrates it to v0.40 x/mint genesis state. The migration includes: +// +// - Re-encode in v0.40 GenesisState. +func Migrate(mintGenState v039mint.GenesisState) *v040mint.GenesisState { + return &v040mint.GenesisState{ + Minter: v040mint.Minter{ + Inflation: mintGenState.Minter.Inflation, + AnnualProvisions: mintGenState.Minter.AnnualProvisions, + }, + Params: v040mint.Params{ + MintDenom: mintGenState.Params.MintDenom, + InflationRateChange: mintGenState.Params.InflationRateChange, + InflationMax: mintGenState.Params.InflationMax, + InflationMin: mintGenState.Params.InflationMin, + GoalBonded: mintGenState.Params.GoalBonded, + BlocksPerYear: mintGenState.Params.BlocksPerYear, + }, + } +} diff --git a/x/mint/legacy/v040/types.go b/x/mint/legacy/v040/types.go new file mode 100644 index 000000000000..d725b48c34fb --- /dev/null +++ b/x/mint/legacy/v040/types.go @@ -0,0 +1,5 @@ +package v040 + +const ( + ModuleName = "mint" +) From 777019604de5807498e6b672a0ba2e17a7b48d56 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Wed, 7 Oct 2020 15:41:02 +0200 Subject: [PATCH 07/15] Fix test --- x/evidence/legacy/v040/migrate.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/x/evidence/legacy/v040/migrate.go b/x/evidence/legacy/v040/migrate.go index ec7178cb845c..f848130f345c 100644 --- a/x/evidence/legacy/v040/migrate.go +++ b/x/evidence/legacy/v040/migrate.go @@ -10,9 +10,15 @@ import ( func migrateEvidence(oldEvidence v038evidence.Evidence) *codectypes.Any { switch oldEvidence := oldEvidence.(type) { - case *v040evidence.Equivocation: + case v038evidence.Equivocation: { - any, err := codectypes.NewAnyWithValue(oldEvidence) + newEquivocation := &v040evidence.Equivocation{ + Height: oldEvidence.Height, + Time: oldEvidence.Time, + Power: oldEvidence.Power, + ConsensusAddress: oldEvidence.ConsensusAddress.String(), + } + any, err := codectypes.NewAnyWithValue(newEquivocation) if err != nil { panic(err) } From f084fe1a08c23935aaa00871b079d27ba7b3fd68 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Fri, 9 Oct 2020 11:17:58 +0200 Subject: [PATCH 08/15] migrate x/genutil --- x/genutil/legacy/v039/types.go | 12 ++++++++++++ x/genutil/legacy/v040/migrate.go | 22 ++++++++++++++++++++++ x/genutil/legacy/v040/types.go | 5 +++++ 3 files changed, 39 insertions(+) create mode 100644 x/genutil/legacy/v039/types.go create mode 100644 x/genutil/legacy/v040/types.go diff --git a/x/genutil/legacy/v039/types.go b/x/genutil/legacy/v039/types.go new file mode 100644 index 000000000000..12d082d1bc8a --- /dev/null +++ b/x/genutil/legacy/v039/types.go @@ -0,0 +1,12 @@ +package v039 + +import "encoding/json" + +const ( + ModuleName = "genutil" +) + +// GenesisState defines the raw genesis transaction in JSON +type GenesisState struct { + GenTxs []json.RawMessage `json:"gentxs" yaml:"gentxs"` +} diff --git a/x/genutil/legacy/v040/migrate.go b/x/genutil/legacy/v040/migrate.go index 9b88d1474696..d91d9ae3b1bd 100644 --- a/x/genutil/legacy/v040/migrate.go +++ b/x/genutil/legacy/v040/migrate.go @@ -15,7 +15,9 @@ import ( v040distribution "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v040" v038evidence "github.com/cosmos/cosmos-sdk/x/evidence/legacy/v038" v040evidence "github.com/cosmos/cosmos-sdk/x/evidence/legacy/v040" + v039genutil "github.com/cosmos/cosmos-sdk/x/genutil/legacy/v039" "github.com/cosmos/cosmos-sdk/x/genutil/types" + v040genutil "github.com/cosmos/cosmos-sdk/x/genutil/types" v036gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v036" v040gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v040" v039mint "github.com/cosmos/cosmos-sdk/x/mint/legacy/v039" @@ -26,6 +28,12 @@ import ( v040staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v040" ) +func migrateGenutil(oldGenState v039genutil.GenesisState) *v040genutil.GenesisState { + return &v040genutil.GenesisState{ + GenTxs: oldGenState.GenTxs, + } +} + // Migrate migrates exported state from v0.39 to a v0.40 genesis state. func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { v039Codec := codec.NewLegacyAmino() @@ -170,5 +178,19 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { appState[v040staking.ModuleName] = v040Codec.MustMarshalJSON(v040staking.Migrate(stakingGenState)) } + // Remove x/genutil + if appState[v039genutil.ModuleName] != nil { + // unmarshal relative source genesis application state + var genutilGenState v039genutil.GenesisState + v039Codec.MustUnmarshalJSON(appState[v039genutil.ModuleName], &genutilGenState) + + // delete deprecated x/staking genesis state + delete(appState, v039genutil.ModuleName) + + // Migrate relative source genesis application state and marshal it into + // the respective key. + appState[v040genutil.ModuleName] = v040Codec.MustMarshalJSON(migrateGenutil(genutilGenState)) + } + return appState } diff --git a/x/genutil/legacy/v040/types.go b/x/genutil/legacy/v040/types.go new file mode 100644 index 000000000000..f641dbff51e1 --- /dev/null +++ b/x/genutil/legacy/v040/types.go @@ -0,0 +1,5 @@ +package v040 + +const ( + ModuleName = "genutil" +) From eb0a7ab91d8a1136efcb77f15c044822202aed2e Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Fri, 9 Oct 2020 11:37:56 +0200 Subject: [PATCH 09/15] Fix lint --- x/genutil/legacy/v040/migrate.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/x/genutil/legacy/v040/migrate.go b/x/genutil/legacy/v040/migrate.go index d91d9ae3b1bd..0a8da3b306b7 100644 --- a/x/genutil/legacy/v040/migrate.go +++ b/x/genutil/legacy/v040/migrate.go @@ -17,7 +17,6 @@ import ( v040evidence "github.com/cosmos/cosmos-sdk/x/evidence/legacy/v040" v039genutil "github.com/cosmos/cosmos-sdk/x/genutil/legacy/v039" "github.com/cosmos/cosmos-sdk/x/genutil/types" - v040genutil "github.com/cosmos/cosmos-sdk/x/genutil/types" v036gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v036" v040gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v040" v039mint "github.com/cosmos/cosmos-sdk/x/mint/legacy/v039" @@ -28,8 +27,8 @@ import ( v040staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v040" ) -func migrateGenutil(oldGenState v039genutil.GenesisState) *v040genutil.GenesisState { - return &v040genutil.GenesisState{ +func migrateGenutil(oldGenState v039genutil.GenesisState) *types.GenesisState { + return &types.GenesisState{ GenTxs: oldGenState.GenTxs, } } @@ -189,7 +188,7 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { // Migrate relative source genesis application state and marshal it into // the respective key. - appState[v040genutil.ModuleName] = v040Codec.MustMarshalJSON(migrateGenutil(genutilGenState)) + appState[ModuleName] = v040Codec.MustMarshalJSON(migrateGenutil(genutilGenState)) } return appState From 5d608ac1dfe8c18e370f7d27935a1de302b4c823 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Fri, 9 Oct 2020 12:03:45 +0200 Subject: [PATCH 10/15] Fix staking constants --- types/staking.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/staking.go b/types/staking.go index 91c7601386d6..017af03c5738 100644 --- a/types/staking.go +++ b/types/staking.go @@ -40,9 +40,9 @@ type BondStatus int32 // staking constants const ( - Unbonded BondStatus = 1 - Unbonding BondStatus = 2 - Bonded BondStatus = 3 + Unbonded BondStatus = 0 + Unbonding BondStatus = 1 + Bonded BondStatus = 2 BondStatusUnbonded = "Unbonded" BondStatusUnbonding = "Unbonding" From 06cb04957f59f9578eee3fcbcd607f1611013048 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Fri, 9 Oct 2020 12:12:50 +0200 Subject: [PATCH 11/15] Fix test --- types/staking_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/staking_test.go b/types/staking_test.go index 4ad06cc18b78..42dd3c3ed20a 100644 --- a/types/staking_test.go +++ b/types/staking_test.go @@ -24,7 +24,7 @@ func (s *stakingTestSuite) TestBondStatus() { s.Require().False(sdk.Unbonded.Equal(sdk.Bonded)) s.Require().False(sdk.Unbonded.Equal(sdk.Unbonding)) s.Require().False(sdk.Bonded.Equal(sdk.Unbonding)) - s.Require().Panicsf(func() { sdk.BondStatus(0).String() }, "invalid bond status") // nolint:govet + s.Require().Panicsf(func() { sdk.BondStatus(3).String() }, "invalid bond status") // nolint:govet s.Require().Equal(sdk.BondStatusUnbonded, sdk.Unbonded.String()) s.Require().Equal(sdk.BondStatusBonded, sdk.Bonded.String()) s.Require().Equal(sdk.BondStatusUnbonding, sdk.Unbonding.String()) From b4d4289bb31bf34f1879f9ae02239637b6c2e6e2 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Fri, 9 Oct 2020 13:02:58 +0200 Subject: [PATCH 12/15] Update x/genutil/legacy/v040/migrate.go Co-authored-by: Marie Gauthier --- x/genutil/legacy/v040/migrate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/genutil/legacy/v040/migrate.go b/x/genutil/legacy/v040/migrate.go index 0a8da3b306b7..c07bcdb74243 100644 --- a/x/genutil/legacy/v040/migrate.go +++ b/x/genutil/legacy/v040/migrate.go @@ -177,7 +177,7 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap { appState[v040staking.ModuleName] = v040Codec.MustMarshalJSON(v040staking.Migrate(stakingGenState)) } - // Remove x/genutil + // Migrate x/genutil if appState[v039genutil.ModuleName] != nil { // unmarshal relative source genesis application state var genutilGenState v039genutil.GenesisState From b63535d8e9457cb93988fce29aa3bd6e96084ff4 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Fri, 9 Oct 2020 13:44:54 +0200 Subject: [PATCH 13/15] Add migrate script instead of change BondStatus constants --- types/staking.go | 6 +- x/genaccounts/legacy/v036/migrate.go | 4 +- x/staking/legacy/v034/types.go | 18 ++++- x/staking/legacy/v036/types.go | 4 +- x/staking/legacy/v038/types.go | 4 +- x/staking/legacy/v040/migrate.go | 22 ++++++- x/staking/legacy/v040/migrate_test.go | 94 +++++++++++++++++++++++++++ 7 files changed, 140 insertions(+), 12 deletions(-) create mode 100644 x/staking/legacy/v040/migrate_test.go diff --git a/types/staking.go b/types/staking.go index 017af03c5738..91c7601386d6 100644 --- a/types/staking.go +++ b/types/staking.go @@ -40,9 +40,9 @@ type BondStatus int32 // staking constants const ( - Unbonded BondStatus = 0 - Unbonding BondStatus = 1 - Bonded BondStatus = 2 + Unbonded BondStatus = 1 + Unbonding BondStatus = 2 + Bonded BondStatus = 3 BondStatusUnbonded = "Unbonded" BondStatusUnbonding = "Unbonding" diff --git a/x/genaccounts/legacy/v036/migrate.go b/x/genaccounts/legacy/v036/migrate.go index 8dff3e454781..4da938aeaf79 100644 --- a/x/genaccounts/legacy/v036/migrate.go +++ b/x/genaccounts/legacy/v036/migrate.go @@ -90,10 +90,10 @@ func Migrate( // get staking module accounts coins for _, validator := range vals { switch validator.Status { - case sdk.Bonded: + case v034staking.Bonded: bondedAmt = bondedAmt.Add(validator.Tokens) - case sdk.Unbonding, sdk.Unbonded: + case v034staking.Unbonding, v034staking.Unbonded: notBondedAmt = notBondedAmt.Add(validator.Tokens) default: diff --git a/x/staking/legacy/v034/types.go b/x/staking/legacy/v034/types.go index f062ef2fa121..6e93b7986a52 100644 --- a/x/staking/legacy/v034/types.go +++ b/x/staking/legacy/v034/types.go @@ -15,7 +15,21 @@ const ( ModuleName = "staking" ) +// staking constants +const ( + Unbonded BondStatus = 0x00 + Unbonding BondStatus = 0x01 + Bonded BondStatus = 0x02 + + BondStatusUnbonded = "Unbonded" + BondStatusUnbonding = "Unbonding" + BondStatusBonded = "Bonded" +) + type ( + // BondStatus is the status of a validator + BondStatus byte + Pool struct { NotBondedTokens sdk.Int `json:"not_bonded_tokens"` BondedTokens sdk.Int `json:"bonded_tokens"` @@ -51,7 +65,7 @@ type ( OperatorAddress sdk.ValAddress `json:"operator_address"` // the bech32 address of the validator's operator ConsPubKey string `json:"consensus_pubkey"` // the bech32 consensus public key of the validator Jailed bool `json:"jailed"` // has the validator been jailed from bonded status? - Status sdk.BondStatus `json:"status"` // validator status (bonded/unbonding/unbonded) + Status BondStatus `json:"status"` // validator status (bonded/unbonding/unbonded) Tokens sdk.Int `json:"tokens"` // delegated tokens (incl. self-delegation) DelegatorShares sdk.Dec `json:"delegator_shares"` // total shares issued to a validator's delegators Description Description `json:"description"` // description terms for the validator @@ -65,7 +79,7 @@ type ( OperatorAddress sdk.ValAddress `json:"operator_address"` ConsPubKey crypto.PubKey `json:"consensus_pubkey"` Jailed bool `json:"jailed"` - Status sdk.BondStatus `json:"status"` + Status BondStatus `json:"status"` Tokens sdk.Int `json:"tokens"` DelegatorShares sdk.Dec `json:"delegator_shares"` Description Description `json:"description"` diff --git a/x/staking/legacy/v036/types.go b/x/staking/legacy/v036/types.go index 7adc446eb009..6c110bbe57dc 100644 --- a/x/staking/legacy/v036/types.go +++ b/x/staking/legacy/v036/types.go @@ -32,7 +32,7 @@ type ( OperatorAddress sdk.ValAddress `json:"operator_address" yaml:"operator_address"` ConsPubKey crypto.PubKey `json:"consensus_pubkey" yaml:"consensus_pubkey"` Jailed bool `json:"jailed" yaml:"jailed"` - Status sdk.BondStatus `json:"status" yaml:"status"` + Status v034staking.BondStatus `json:"status" yaml:"status"` Tokens sdk.Int `json:"tokens" yaml:"tokens"` DelegatorShares sdk.Dec `json:"delegator_shares" yaml:"delegator_shares"` Description v034staking.Description `json:"description" yaml:"description"` @@ -46,7 +46,7 @@ type ( OperatorAddress sdk.ValAddress `json:"operator_address" yaml:"operator_address"` ConsPubKey string `json:"consensus_pubkey" yaml:"consensus_pubkey"` Jailed bool `json:"jailed" yaml:"jailed"` - Status sdk.BondStatus `json:"status" yaml:"status"` + Status v034staking.BondStatus `json:"status" yaml:"status"` Tokens sdk.Int `json:"tokens" yaml:"tokens"` DelegatorShares sdk.Dec `json:"delegator_shares" yaml:"delegator_shares"` Description v034staking.Description `json:"description" yaml:"description"` diff --git a/x/staking/legacy/v038/types.go b/x/staking/legacy/v038/types.go index 3e6cb1d0a889..aff9a559d457 100644 --- a/x/staking/legacy/v038/types.go +++ b/x/staking/legacy/v038/types.go @@ -30,7 +30,7 @@ type ( OperatorAddress sdk.ValAddress `json:"operator_address" yaml:"operator_address"` ConsPubKey crypto.PubKey `json:"consensus_pubkey" yaml:"consensus_pubkey"` Jailed bool `json:"jailed" yaml:"jailed"` - Status sdk.BondStatus `json:"status" yaml:"status"` + Status v034staking.BondStatus `json:"status" yaml:"status"` Tokens sdk.Int `json:"tokens" yaml:"tokens"` DelegatorShares sdk.Dec `json:"delegator_shares" yaml:"delegator_shares"` Description Description `json:"description" yaml:"description"` @@ -44,7 +44,7 @@ type ( OperatorAddress sdk.ValAddress `json:"operator_address" yaml:"operator_address"` ConsPubKey string `json:"consensus_pubkey" yaml:"consensus_pubkey"` Jailed bool `json:"jailed" yaml:"jailed"` - Status sdk.BondStatus `json:"status" yaml:"status"` + Status v034staking.BondStatus `json:"status" yaml:"status"` Tokens sdk.Int `json:"tokens" yaml:"tokens"` DelegatorShares sdk.Dec `json:"delegator_shares" yaml:"delegator_shares"` Description Description `json:"description" yaml:"description"` diff --git a/x/staking/legacy/v040/migrate.go b/x/staking/legacy/v040/migrate.go index 07a4eba87a5f..4cd55afa504a 100644 --- a/x/staking/legacy/v040/migrate.go +++ b/x/staking/legacy/v040/migrate.go @@ -1,15 +1,35 @@ package v040 import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" + v034staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v034" v038staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v038" v040staking "github.com/cosmos/cosmos-sdk/x/staking/types" ) +func migrateBondStatus(oldStatus v034staking.BondStatus) sdk.BondStatus { + switch oldStatus { + case v034staking.Unbonded: + return sdk.Unbonded + + case v034staking.Unbonding: + return sdk.Unbonding + + case v034staking.Bonded: + return sdk.Bonded + + default: + panic(fmt.Errorf("invalid bond status %d", oldStatus)) + } +} + // Migrate accepts exported v0.38 x/staking genesis state and migrates it to // v0.40 x/staking genesis state. The migration includes: // // - Convert addresses from bytes to bech32 strings. +// - Update BondStatus staking constants. // - Re-encode in v0.40 GenesisState. func Migrate(stakingState v038staking.GenesisState) *v040staking.GenesisState { newLastValidatorPowers := make([]v040staking.LastValidatorPower, len(stakingState.LastValidatorPowers)) @@ -26,7 +46,7 @@ func Migrate(stakingState v038staking.GenesisState) *v040staking.GenesisState { OperatorAddress: oldValidator.OperatorAddress.String(), ConsensusPubkey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, oldValidator.ConsPubKey), Jailed: oldValidator.Jailed, - Status: oldValidator.Status, + Status: migrateBondStatus(oldValidator.Status), Tokens: oldValidator.Tokens, DelegatorShares: oldValidator.DelegatorShares, Description: v040staking.Description{ diff --git a/x/staking/legacy/v040/migrate_test.go b/x/staking/legacy/v040/migrate_test.go new file mode 100644 index 000000000000..727428c8ce80 --- /dev/null +++ b/x/staking/legacy/v040/migrate_test.go @@ -0,0 +1,94 @@ +package v040_test + +import ( + "encoding/json" + "fmt" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/simapp" + v034staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v034" + v038staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v038" + v040staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v040" +) + +func TestMigrate(t *testing.T) { + encodingConfig := simapp.MakeEncodingConfig() + clientCtx := client.Context{}. + WithInterfaceRegistry(encodingConfig.InterfaceRegistry). + WithTxConfig(encodingConfig.TxConfig). + WithLegacyAmino(encodingConfig.Amino). + WithJSONMarshaler(encodingConfig.Marshaler) + + consPubKey := ed25519.GenPrivKey().PubKey() + stakingGenState := v038staking.GenesisState{ + Validators: v038staking.Validators{v038staking.Validator{ + ConsPubKey: consPubKey, + Status: v034staking.Unbonded, + }},x + } + + migrated := v040staking.Migrate(stakingGenState) + + bz, err := clientCtx.JSONMarshaler.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, "", " ") + require.NoError(t, err) + + // Make sure about: + // - consensus_pubkey: should be bech32 pubkey + // - validator's status should be 1 (new unbonded) + expected := `{ + "delegations": [], + "exported": false, + "last_total_power": "0", + "last_validator_powers": [], + "params": { + "bond_denom": "", + "historical_entries": 0, + "max_entries": 0, + "max_validators": 0, + "unbonding_time": "0s" + }, + "redelegations": [], + "unbonding_delegations": [], + "validators": [ + { + "commission": { + "commission_rates": { + "max_change_rate": "0", + "max_rate": "0", + "rate": "0" + }, + "update_time": "0001-01-01T00:00:00Z" + }, + "consensus_pubkey": "cosmosvalconspub1zcjduepqtrz32re64nu80d3lagdry56ywym6yjrayrv2ycrvvm07f9tkve8s397nyf", + "delegator_shares": "0", + "description": { + "details": "", + "identity": "", + "moniker": "", + "security_contact": "", + "website": "" + }, + "jailed": false, + "min_self_delegation": "0", + "operator_address": "", + "status": 1, + "tokens": "0", + "unbonding_height": "0", + "unbonding_time": "0001-01-01T00:00:00Z" + } + ] +}` + + require.Equal(t, expected, string(indentedBz)) +} From 6fcc16338cee7edec6cda56261f3361aa1edf40c Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Fri, 9 Oct 2020 14:16:22 +0200 Subject: [PATCH 14/15] Fix test --- types/staking_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/staking_test.go b/types/staking_test.go index 42dd3c3ed20a..4ad06cc18b78 100644 --- a/types/staking_test.go +++ b/types/staking_test.go @@ -24,7 +24,7 @@ func (s *stakingTestSuite) TestBondStatus() { s.Require().False(sdk.Unbonded.Equal(sdk.Bonded)) s.Require().False(sdk.Unbonded.Equal(sdk.Unbonding)) s.Require().False(sdk.Bonded.Equal(sdk.Unbonding)) - s.Require().Panicsf(func() { sdk.BondStatus(3).String() }, "invalid bond status") // nolint:govet + s.Require().Panicsf(func() { sdk.BondStatus(0).String() }, "invalid bond status") // nolint:govet s.Require().Equal(sdk.BondStatusUnbonded, sdk.Unbonded.String()) s.Require().Equal(sdk.BondStatusBonded, sdk.Bonded.String()) s.Require().Equal(sdk.BondStatusUnbonding, sdk.Unbonding.String()) From 2bc7a87da17f7edbc4cbbf1087d53064f8531bdf Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Fri, 9 Oct 2020 14:27:59 +0200 Subject: [PATCH 15/15] Fix another test --- x/staking/legacy/v040/migrate_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/x/staking/legacy/v040/migrate_test.go b/x/staking/legacy/v040/migrate_test.go index 727428c8ce80..1d593a6fc98a 100644 --- a/x/staking/legacy/v040/migrate_test.go +++ b/x/staking/legacy/v040/migrate_test.go @@ -2,7 +2,6 @@ package v040_test import ( "encoding/json" - "fmt" "testing" "github.com/stretchr/testify/require" @@ -23,12 +22,12 @@ func TestMigrate(t *testing.T) { WithLegacyAmino(encodingConfig.Amino). WithJSONMarshaler(encodingConfig.Marshaler) - consPubKey := ed25519.GenPrivKey().PubKey() + consPubKey := ed25519.GenPrivKeyFromSecret([]byte("val0")).PubKey() stakingGenState := v038staking.GenesisState{ Validators: v038staking.Validators{v038staking.Validator{ ConsPubKey: consPubKey, Status: v034staking.Unbonded, - }},x + }}, } migrated := v040staking.Migrate(stakingGenState) @@ -70,7 +69,7 @@ func TestMigrate(t *testing.T) { }, "update_time": "0001-01-01T00:00:00Z" }, - "consensus_pubkey": "cosmosvalconspub1zcjduepqtrz32re64nu80d3lagdry56ywym6yjrayrv2ycrvvm07f9tkve8s397nyf", + "consensus_pubkey": "cosmosvalconspub1zcjduepq9ymett3nlv6fytn7lqxzd3q3ckvd79eqlcf3wkhgamcl4rzghesq83ecpx", "delegator_shares": "0", "description": { "details": "",