-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Changes from 1 commit
267ef39
0d50d31
586ea7a
075a4ef
e1521d5
ef05dde
e8488ae
ab59c83
a735812
7770196
0f8cf26
f084fe1
a97ca37
846f63d
eb0a7ab
5d608ac
06cb049
b4d4289
b63535d
9ca4e39
6fcc163
2bc7a87
e7c8a5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package v040 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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...) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package v040 | ||
|
||
// Default parameter values | ||
const ( | ||
ModuleName = "staking" | ||
) |
There was a problem hiding this comment.
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