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 22 commits
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
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