Skip to content

Commit

Permalink
CLI migrate command follow-up: decode & re-encode (#7464)
Browse files Browse the repository at this point in the history
* Migrate staking module

* Add gov legacy

* Add comments

* Add x/distrib

* x/crisis

* x/mint

* Fix test

* migrate x/genutil

* Fix lint

* Fix staking constants

* Fix test

* Update x/genutil/legacy/v040/migrate.go

Co-authored-by: Marie Gauthier <[email protected]>

* Add migrate script instead of change BondStatus constants

* Fix test

* Fix another test

Co-authored-by: Marie Gauthier <[email protected]>
Co-authored-by: Cory <[email protected]>
  • Loading branch information
3 people authored Oct 9, 2020
1 parent f8e3fcb commit c14a3a7
Show file tree
Hide file tree
Showing 25 changed files with 786 additions and 41 deletions.
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
13 changes: 13 additions & 0 deletions x/crisis/legacy/v039/types.go
Original file line number Diff line number Diff line change
@@ -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"`
}
)
16 changes: 16 additions & 0 deletions x/crisis/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
@@ -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 genesis 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,
}
}
5 changes: 5 additions & 0 deletions x/crisis/legacy/v040/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package v040

const (
ModuleName = "crisis"
)
108 changes: 108 additions & 0 deletions x/distribution/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
6 changes: 6 additions & 0 deletions x/distribution/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 = "distribution"
)
58 changes: 30 additions & 28 deletions x/evidence/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
package v040

import (
"github.com/cosmos/cosmos-sdk/client"
"fmt"

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 v038evidence.Equivocation:
{
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)
}

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.
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
// - Re-encode in v0.40 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)
}

return &v040evidence.GenesisState{
Evidence: newEvidence,
Evidence: newEvidences,
}
}
2 changes: 1 addition & 1 deletion x/evidence/legacy/v040/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions x/genaccounts/legacy/v036/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions x/genutil/legacy/v039/types.go
Original file line number Diff line number Diff line change
@@ -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"`
}
Loading

0 comments on commit c14a3a7

Please sign in to comment.