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

Use enum instead of int32 for BondStatus #7499

Merged
merged 34 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 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
049ca8d
Change staking bondStatus to enum
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
f0cd710
Remove staking exported
amaury1093 Oct 9, 2020
bcc9db2
Merge branch 'am-migrate-followup' into am-migrate-followup-staking
amaury1093 Oct 9, 2020
2379a24
fix references
amaury1093 Oct 9, 2020
7c276aa
Better constants
amaury1093 Oct 9, 2020
6d0560b
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into am-m…
amaury1093 Oct 12, 2020
7c4329a
Fix build
amaury1093 Oct 12, 2020
510c3b6
Fix lint
amaury1093 Oct 12, 2020
bd03ff3
Remove buf version
amaury1093 Oct 12, 2020
8ed1fdc
Fix tests
amaury1093 Oct 12, 2020
8fa8bf0
Fix test
amaury1093 Oct 12, 2020
fe7e6ad
Merge branch 'master' into am-migrate-followup-staking
alexanderbez Oct 12, 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
Prev Previous commit
Next Next commit
Add comments
  • Loading branch information
amaury1093 committed Oct 7, 2020
commit e1521d53e4783a52f7ca2f9281385717cc2ea8f5
1 change: 1 addition & 0 deletions x/auth/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions x/bank/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
49 changes: 23 additions & 26 deletions x/evidence/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
202 changes: 113 additions & 89 deletions x/gov/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
1 change: 1 addition & 0 deletions x/slashing/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion x/staking/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down