-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI
migrate
command follow-up: decode & re-encode (#7464)
* 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
1 parent
f8e3fcb
commit c14a3a7
Showing
25 changed files
with
786 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package v040 | ||
|
||
const ( | ||
ModuleName = "crisis" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package v040 | ||
|
||
// Default parameter values | ||
const ( | ||
ModuleName = "distribution" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
Oops, something went wrong.