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 x/distrib
  • Loading branch information
amaury1093 committed Oct 7, 2020
commit ef05dde86056010b81e000d3795db218cd55cc01
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 = "gov"
)
3 changes: 1 addition & 2 deletions x/evidence/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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"
Expand Down Expand Up @@ -31,7 +30,7 @@ func migrateEvidence(oldEvidence v038evidence.Evidence) *codectypes.Any {
// - Removing the `Params` field.
// - Converting Equivocations into Anys.
// - Re-encode in v0.40 GenesisState.
func Migrate(evidenceState v038evidence.GenesisState, _ client.Context) *v040evidence.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)
Expand Down
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
34 changes: 33 additions & 1 deletion x/genutil/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import (
v036supply "github.com/cosmos/cosmos-sdk/x/bank/legacy/v036"
v038bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v038"
v040bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v040"
v038distribution "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v038"
v040distribution "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v040"
v038evidence "github.com/cosmos/cosmos-sdk/x/evidence/legacy/v038"
v040evidence "github.com/cosmos/cosmos-sdk/x/evidence/legacy/v040"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
v036gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v036"
v040gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v040"
v039slashing "github.com/cosmos/cosmos-sdk/x/slashing/legacy/v039"
v040slashing "github.com/cosmos/cosmos-sdk/x/slashing/legacy/v040"
v038staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v038"
Expand Down Expand Up @@ -64,6 +68,20 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap {
appState[v040auth.ModuleName] = v040Codec.MustMarshalJSON(v040auth.Migrate(authGenState))
}

// Migrate x/distribution.
if appState[v038distribution.ModuleName] != nil {
// unmarshal relative source genesis application state
var distributionGenState v038distribution.GenesisState
v039Codec.MustUnmarshalJSON(appState[v038distribution.ModuleName], &distributionGenState)

// delete deprecated x/distribution genesis state
delete(appState, v038distribution.ModuleName)

// Migrate relative source genesis application state and marshal it into
// the respective key.
appState[v040distribution.ModuleName] = v040Codec.MustMarshalJSON(v040distribution.Migrate(distributionGenState))
}

// Migrate x/evidence.
if appState[v038evidence.ModuleName] != nil {
// unmarshal relative source genesis application state
Expand All @@ -75,7 +93,21 @@ func Migrate(appState types.AppMap, clientCtx client.Context) types.AppMap {

// Migrate relative source genesis application state and marshal it into
// the respective key.
appState[v040evidence.ModuleName] = v040Codec.MustMarshalJSON(v040evidence.Migrate(evidenceGenState, clientCtx))
appState[v040evidence.ModuleName] = v040Codec.MustMarshalJSON(v040evidence.Migrate(evidenceGenState))
}

// Migrate x/gov.
if appState[v036gov.ModuleName] != nil {
// unmarshal relative source genesis application state
var govGenState v036gov.GenesisState
v039Codec.MustUnmarshalJSON(appState[v036gov.ModuleName], &govGenState)

// delete deprecated x/gov genesis state
delete(appState, v036gov.ModuleName)

// Migrate relative source genesis application state and marshal it into
// the respective key.
appState[v040gov.ModuleName] = v040Codec.MustMarshalJSON(v040gov.Migrate(govGenState))
}

// Migrate x/slashing.
Expand Down
1 change: 1 addition & 0 deletions x/gov/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func migrateContent(oldContent v036gov.Content) *codectypes.Any {
//
// - Convert vote option & proposal status from byte to enum.
// - Migrate proposal content to Any.
// - Convert addresses from bytes to bech32 strings.
// - Re-encode in v0.40 GenesisState.
func Migrate(oldGovState v036gov.GenesisState) *v040gov.GenesisState {
newDeposits := make([]v040gov.Deposit, len(oldGovState.Deposits))
Expand Down
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.
// - Convert addresses from bytes to bech32 strings.
// - 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
Expand Down
1 change: 1 addition & 0 deletions x/staking/legacy/v040/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +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:
//
// - Convert addresses from bytes to bech32 strings.
// - Re-encode in v0.40 GenesisState.
func Migrate(stakingState v038staking.GenesisState) *v040staking.GenesisState {
newLastValidatorPowers := make([]v040staking.LastValidatorPower, len(stakingState.LastValidatorPowers))
Expand Down