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

CLI migrate command follow-up: decode & re-encode #7464

Merged
merged 23 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
267ef39
Migrate staking module
amaury1093 Oct 6, 2020
0d50d31
Merge branch 'master' into am-migrate-followup
amaury1093 Oct 6, 2020
586ea7a
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into am-m…
amaury1093 Oct 7, 2020
075a4ef
Add gov legacy
amaury1093 Oct 7, 2020
e1521d5
Add comments
amaury1093 Oct 7, 2020
ef05dde
Add x/distrib
amaury1093 Oct 7, 2020
e8488ae
x/crisis
amaury1093 Oct 7, 2020
ab59c83
x/mint
amaury1093 Oct 7, 2020
a735812
Merge branch 'master' into am-migrate-followup
amaury1093 Oct 7, 2020
7770196
Fix test
amaury1093 Oct 7, 2020
0f8cf26
Merge branch 'master' into am-migrate-followup
amaury1093 Oct 8, 2020
f084fe1
migrate x/genutil
amaury1093 Oct 9, 2020
a97ca37
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into am-m…
amaury1093 Oct 9, 2020
846f63d
Merge branch 'am-migrate-followup' of ssh://github.com/cosmos/cosmos-…
amaury1093 Oct 9, 2020
eb0a7ab
Fix lint
amaury1093 Oct 9, 2020
5d608ac
Fix staking constants
amaury1093 Oct 9, 2020
06cb049
Fix test
amaury1093 Oct 9, 2020
b4d4289
Update x/genutil/legacy/v040/migrate.go
amaury1093 Oct 9, 2020
b63535d
Add migrate script instead of change BondStatus constants
amaury1093 Oct 9, 2020
9ca4e39
Merge branch 'am-migrate-followup' of ssh://github.com/cosmos/cosmos-…
amaury1093 Oct 9, 2020
6fcc163
Fix test
amaury1093 Oct 9, 2020
2bc7a87
Fix another test
amaury1093 Oct 9, 2020
e7c8a5a
Merge branch 'master' into am-migrate-followup
clevinson Oct 9, 2020
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
18 changes: 17 additions & 1 deletion x/genutil/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -82,13 +84,27 @@ 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
// the respective key.
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
}
18 changes: 16 additions & 2 deletions x/staking/legacy/v038/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,16 @@ type (

Validators []Validator

Params struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing a 0.36->0.38 migration here

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"`
Expand Down Expand Up @@ -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,
Expand Down
116 changes: 116 additions & 0 deletions x/staking/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package v040
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll pay a beer to anyone who tells me if there's a way to make this boilerplate code shorter (esp the 5 for loops and some inner loops...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may be able to just amino JSON encode and decode from one version to the next. If there were no other changes, the amino JSON should be the same.


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,
}
}
6 changes: 6 additions & 0 deletions x/staking/legacy/v040/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package v040

// Default parameter values
const (
ModuleName = "staking"
)