From 2fdbf631e763132b7641214701d4d7a763018757 Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Fri, 5 Jul 2019 08:34:02 -0400 Subject: [PATCH] Merge PR #4669: v0.36 distribution genesis migration --- x/distribution/legacy/v0_34/types.go | 98 +++++++++++++++++++ x/distribution/legacy/v0_36/migrate.go | 93 +++++++++++++++++++ x/distribution/legacy/v0_36/types.go | 124 +++++++++++++++++++++++++ x/genutil/client/cli/migrate.go | 2 +- x/genutil/legacy/v036/migrate.go | 24 ++++- x/genutil/legacy/v036/migrate_test.go | 7 +- x/genutil/types.go | 4 +- x/slashing/types/genesis.go | 5 +- x/slashing/types/params.go | 8 +- 9 files changed, 346 insertions(+), 19 deletions(-) create mode 100644 x/distribution/legacy/v0_34/types.go create mode 100644 x/distribution/legacy/v0_36/migrate.go create mode 100644 x/distribution/legacy/v0_36/types.go diff --git a/x/distribution/legacy/v0_34/types.go b/x/distribution/legacy/v0_34/types.go new file mode 100644 index 0000000000..84e1b4f0ad --- /dev/null +++ b/x/distribution/legacy/v0_34/types.go @@ -0,0 +1,98 @@ +// DONTCOVER +// nolint +package v0_34 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// ---------------------------------------------------------------------------- +// Types and Constants +// ---------------------------------------------------------------------------- + +const ( + ModuleName = "distr" +) + +type ( + ValidatorAccumulatedCommission = sdk.DecCoins + + DelegatorStartingInfo struct { + PreviousPeriod uint64 `json:"previous_period"` + Stake sdk.Dec `json:"stake"` + Height uint64 `json:"height"` + } + + DelegatorWithdrawInfo struct { + DelegatorAddress sdk.AccAddress `json:"delegator_address"` + WithdrawAddress sdk.AccAddress `json:"withdraw_address"` + } + + ValidatorOutstandingRewardsRecord struct { + ValidatorAddress sdk.ValAddress `json:"validator_address"` + OutstandingRewards sdk.DecCoins `json:"outstanding_rewards"` + } + + ValidatorAccumulatedCommissionRecord struct { + ValidatorAddress sdk.ValAddress `json:"validator_address"` + Accumulated ValidatorAccumulatedCommission `json:"accumulated"` + } + + ValidatorHistoricalRewardsRecord struct { + ValidatorAddress sdk.ValAddress `json:"validator_address"` + Period uint64 `json:"period"` + Rewards ValidatorHistoricalRewards `json:"rewards"` + } + + ValidatorHistoricalRewards struct { + CumulativeRewardRatio sdk.DecCoins `json:"cumulative_reward_ratio"` + ReferenceCount uint16 `json:"reference_count"` + } + + ValidatorCurrentRewards struct { + Rewards sdk.DecCoins `json:"rewards"` + Period uint64 `json:"period"` + } + + ValidatorCurrentRewardsRecord struct { + ValidatorAddress sdk.ValAddress `json:"validator_address"` + Rewards ValidatorCurrentRewards `json:"rewards"` + } + + DelegatorStartingInfoRecord struct { + DelegatorAddress sdk.AccAddress `json:"delegator_address"` + ValidatorAddress sdk.ValAddress `json:"validator_address"` + StartingInfo DelegatorStartingInfo `json:"starting_info"` + } + + ValidatorSlashEventRecord struct { + ValidatorAddress sdk.ValAddress `json:"validator_address"` + Height uint64 `json:"height"` + Event ValidatorSlashEvent `json:"validator_slash_event"` + } + + FeePool struct { + CommunityPool sdk.DecCoins `json:"community_pool"` + } + + ValidatorSlashEvent struct { + ValidatorPeriod uint64 `json:"validator_period"` + Fraction sdk.Dec `json:"fraction"` + } + + GenesisState struct { + FeePool FeePool `json:"fee_pool"` + CommunityTax sdk.Dec `json:"community_tax"` + BaseProposerReward sdk.Dec `json:"base_proposer_reward"` + BonusProposerReward sdk.Dec `json:"bonus_proposer_reward"` + WithdrawAddrEnabled bool `json:"withdraw_addr_enabled"` + DelegatorWithdrawInfos []DelegatorWithdrawInfo `json:"delegator_withdraw_infos"` + PreviousProposer sdk.ConsAddress `json:"previous_proposer"` + OutstandingRewards []ValidatorOutstandingRewardsRecord `json:"outstanding_rewards"` + ValidatorAccumulatedCommissions []ValidatorAccumulatedCommissionRecord `json:"validator_accumulated_commissions"` + ValidatorHistoricalRewards []ValidatorHistoricalRewardsRecord `json:"validator_historical_rewards"` + ValidatorCurrentRewards []ValidatorCurrentRewardsRecord `json:"validator_current_rewards"` + DelegatorStartingInfos []DelegatorStartingInfoRecord `json:"delegator_starting_infos"` + ValidatorSlashEvents []ValidatorSlashEventRecord `json:"validator_slash_events"` + } +) diff --git a/x/distribution/legacy/v0_36/migrate.go b/x/distribution/legacy/v0_36/migrate.go new file mode 100644 index 0000000000..4898d5be6e --- /dev/null +++ b/x/distribution/legacy/v0_36/migrate.go @@ -0,0 +1,93 @@ +package v0_36 + +import ( + v034distr "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v0_34" +) + +// Migrate accepts exported genesis state from v0.34 and migrates it to v0.36 +// genesis state. All entries are identical except for validator slashing events +// which now include the period. +func Migrate(oldGenState v034distr.GenesisState) GenesisState { + feePool := FeePool{CommunityPool: oldGenState.FeePool.CommunityPool} + + dwifos := make([]DelegatorWithdrawInfo, len(oldGenState.DelegatorWithdrawInfos)) + for i, info := range oldGenState.DelegatorWithdrawInfos { + dwifos[i] = DelegatorWithdrawInfo{ + DelegatorAddress: info.DelegatorAddress, + WithdrawAddress: info.WithdrawAddress, + } + } + + outRewards := make([]ValidatorOutstandingRewardsRecord, len(oldGenState.OutstandingRewards)) + for i, rew := range oldGenState.OutstandingRewards { + outRewards[i] = ValidatorOutstandingRewardsRecord{ + ValidatorAddress: rew.ValidatorAddress, + OutstandingRewards: rew.OutstandingRewards, + } + } + + accumComm := make([]ValidatorAccumulatedCommissionRecord, len(oldGenState.ValidatorAccumulatedCommissions)) + for i, comm := range oldGenState.ValidatorAccumulatedCommissions { + accumComm[i] = ValidatorAccumulatedCommissionRecord{ + ValidatorAddress: comm.ValidatorAddress, + Accumulated: comm.Accumulated, + } + } + + histRewards := make([]ValidatorHistoricalRewardsRecord, len(oldGenState.ValidatorHistoricalRewards)) + for i, rew := range oldGenState.ValidatorHistoricalRewards { + histRewards[i] = ValidatorHistoricalRewardsRecord{ + ValidatorAddress: rew.ValidatorAddress, + Period: rew.Period, + Rewards: ValidatorHistoricalRewards{ + CumulativeRewardRatio: rew.Rewards.CumulativeRewardRatio, + ReferenceCount: rew.Rewards.ReferenceCount, + }, + } + } + + currRewards := make([]ValidatorCurrentRewardsRecord, len(oldGenState.ValidatorCurrentRewards)) + for i, rew := range oldGenState.ValidatorCurrentRewards { + currRewards[i] = ValidatorCurrentRewardsRecord{ + ValidatorAddress: rew.ValidatorAddress, + Rewards: ValidatorCurrentRewards{ + Rewards: rew.Rewards.Rewards, + Period: rew.Rewards.Period, + }, + } + } + + delStartingInfos := make([]DelegatorStartingInfoRecord, len(oldGenState.DelegatorStartingInfos)) + for i, delInfo := range oldGenState.DelegatorStartingInfos { + delStartingInfos[i] = DelegatorStartingInfoRecord{ + DelegatorAddress: delInfo.DelegatorAddress, + ValidatorAddress: delInfo.ValidatorAddress, + StartingInfo: DelegatorStartingInfo{ + PreviousPeriod: delInfo.StartingInfo.PreviousPeriod, + Stake: delInfo.StartingInfo.Stake, + Height: delInfo.StartingInfo.Height, + }, + } + } + + // migrate slash events which now have the period included + slashEvents := make([]ValidatorSlashEventRecord, len(oldGenState.ValidatorSlashEvents)) + for i, se := range oldGenState.ValidatorSlashEvents { + slashEvents[i] = ValidatorSlashEventRecord{ + ValidatorAddress: se.ValidatorAddress, + Height: se.Height, + Period: se.Event.ValidatorPeriod, + Event: ValidatorSlashEvent{ + ValidatorPeriod: se.Event.ValidatorPeriod, + Fraction: se.Event.Fraction, + }, + } + } + + return NewGenesisState( + feePool, oldGenState.CommunityTax, oldGenState.BaseProposerReward, + oldGenState.BonusProposerReward, oldGenState.WithdrawAddrEnabled, + dwifos, oldGenState.PreviousProposer, outRewards, accumComm, + histRewards, currRewards, delStartingInfos, slashEvents, + ) +} diff --git a/x/distribution/legacy/v0_36/types.go b/x/distribution/legacy/v0_36/types.go new file mode 100644 index 0000000000..b8b29244de --- /dev/null +++ b/x/distribution/legacy/v0_36/types.go @@ -0,0 +1,124 @@ +// DONTCOVER +// nolint +package v0_36 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// ---------------------------------------------------------------------------- +// Types and Constants +// ---------------------------------------------------------------------------- + +const ( + ModuleName = "distribution" +) + +type ( + ValidatorAccumulatedCommission = sdk.DecCoins + + ValidatorCurrentRewards struct { + Rewards sdk.DecCoins `json:"rewards"` + Period uint64 `json:"period"` + } + + ValidatorHistoricalRewards struct { + CumulativeRewardRatio sdk.DecCoins `json:"cumulative_reward_ratio"` + ReferenceCount uint16 `json:"reference_count"` + } + + DelegatorWithdrawInfo struct { + DelegatorAddress sdk.AccAddress `json:"delegator_address"` + WithdrawAddress sdk.AccAddress `json:"withdraw_address"` + } + + ValidatorOutstandingRewardsRecord struct { + ValidatorAddress sdk.ValAddress `json:"validator_address"` + OutstandingRewards sdk.DecCoins `json:"outstanding_rewards"` + } + + ValidatorAccumulatedCommissionRecord struct { + ValidatorAddress sdk.ValAddress `json:"validator_address"` + Accumulated ValidatorAccumulatedCommission `json:"accumulated"` + } + + ValidatorHistoricalRewardsRecord struct { + ValidatorAddress sdk.ValAddress `json:"validator_address"` + Period uint64 `json:"period"` + Rewards ValidatorHistoricalRewards `json:"rewards"` + } + + ValidatorCurrentRewardsRecord struct { + ValidatorAddress sdk.ValAddress `json:"validator_address"` + Rewards ValidatorCurrentRewards `json:"rewards"` + } + + DelegatorStartingInfo struct { + PreviousPeriod uint64 `json:"previous_period"` + Stake sdk.Dec `json:"stake"` + Height uint64 `json:"creation_height"` + } + + DelegatorStartingInfoRecord struct { + DelegatorAddress sdk.AccAddress `json:"delegator_address"` + ValidatorAddress sdk.ValAddress `json:"validator_address"` + StartingInfo DelegatorStartingInfo `json:"starting_info"` + } + + ValidatorSlashEvent struct { + ValidatorPeriod uint64 `json:"validator_period"` + Fraction sdk.Dec `json:"fraction"` + } + + ValidatorSlashEventRecord struct { + ValidatorAddress sdk.ValAddress `json:"validator_address"` + Height uint64 `json:"height"` + Period uint64 `json:"period"` + Event ValidatorSlashEvent `json:"validator_slash_event"` + } + + FeePool struct { + CommunityPool sdk.DecCoins `json:"community_pool"` + } + + GenesisState struct { + FeePool FeePool `json:"fee_pool"` + CommunityTax sdk.Dec `json:"community_tax"` + BaseProposerReward sdk.Dec `json:"base_proposer_reward"` + BonusProposerReward sdk.Dec `json:"bonus_proposer_reward"` + WithdrawAddrEnabled bool `json:"withdraw_addr_enabled"` + DelegatorWithdrawInfos []DelegatorWithdrawInfo `json:"delegator_withdraw_infos"` + PreviousProposer sdk.ConsAddress `json:"previous_proposer"` + OutstandingRewards []ValidatorOutstandingRewardsRecord `json:"outstanding_rewards"` + ValidatorAccumulatedCommissions []ValidatorAccumulatedCommissionRecord `json:"validator_accumulated_commissions"` + ValidatorHistoricalRewards []ValidatorHistoricalRewardsRecord `json:"validator_historical_rewards"` + ValidatorCurrentRewards []ValidatorCurrentRewardsRecord `json:"validator_current_rewards"` + DelegatorStartingInfos []DelegatorStartingInfoRecord `json:"delegator_starting_infos"` + ValidatorSlashEvents []ValidatorSlashEventRecord `json:"validator_slash_events"` + } +) + +func NewGenesisState( + feePool FeePool, communityTax, baseProposerReward, bonusProposerReward sdk.Dec, + withdrawAddrEnabled bool, dwis []DelegatorWithdrawInfo, pp sdk.ConsAddress, + r []ValidatorOutstandingRewardsRecord, acc []ValidatorAccumulatedCommissionRecord, + historical []ValidatorHistoricalRewardsRecord, cur []ValidatorCurrentRewardsRecord, + dels []DelegatorStartingInfoRecord, slashes []ValidatorSlashEventRecord, +) GenesisState { + + return GenesisState{ + FeePool: feePool, + CommunityTax: communityTax, + BaseProposerReward: baseProposerReward, + BonusProposerReward: bonusProposerReward, + WithdrawAddrEnabled: withdrawAddrEnabled, + DelegatorWithdrawInfos: dwis, + PreviousProposer: pp, + OutstandingRewards: r, + ValidatorAccumulatedCommissions: acc, + ValidatorHistoricalRewards: historical, + ValidatorCurrentRewards: cur, + DelegatorStartingInfos: dels, + ValidatorSlashEvents: slashes, + } +} diff --git a/x/genutil/client/cli/migrate.go b/x/genutil/client/cli/migrate.go index 74d9f9fb3f..6b6504804e 100644 --- a/x/genutil/client/cli/migrate.go +++ b/x/genutil/client/cli/migrate.go @@ -49,7 +49,7 @@ $ %s migrate v0.36 /path/to/genesis.json --chain-id=cosmoshub-3 --genesis-time=2 return fmt.Errorf("unknown migration function version: %s", target) } - newGenState := migrationMap[target](initialState, cdc) + newGenState := migrationMap[target](initialState) genDoc.AppState = cdc.MustMarshalJSON(newGenState) genesisTime := cmd.Flag(flagGenesisTime).Value.String() diff --git a/x/genutil/legacy/v036/migrate.go b/x/genutil/legacy/v036/migrate.go index 66379ab380..c25866ae2d 100644 --- a/x/genutil/legacy/v036/migrate.go +++ b/x/genutil/legacy/v036/migrate.go @@ -2,25 +2,41 @@ package v036 import ( "github.com/cosmos/cosmos-sdk/codec" + v034distr "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v0_34" + v036distr "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v0_36" "github.com/cosmos/cosmos-sdk/x/genutil" v034gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v034" v036gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v036" ) // Migrate migrates exported state from v0.34 to a v0.36 genesis state. -func Migrate(appState genutil.AppMap, cdc *codec.Codec) genutil.AppMap { +func Migrate(appState genutil.AppMap) genutil.AppMap { v034Codec := codec.New() codec.RegisterCrypto(v034Codec) + v036Codec := codec.New() codec.RegisterCrypto(v036Codec) + // migrate genesis state if appState[v034gov.ModuleName] != nil { - var govState v034gov.GenesisState v034gov.RegisterCodec(v034Codec) - v034Codec.MustUnmarshalJSON(appState[v034gov.ModuleName], &govState) v036gov.RegisterCodec(v036Codec) - delete(appState, v034gov.ModuleName) // Drop old key, in case it changed name + + var govState v034gov.GenesisState + v034Codec.MustUnmarshalJSON(appState[v034gov.ModuleName], &govState) + + delete(appState, v034gov.ModuleName) // delete old key in case the name changed appState[v036gov.ModuleName] = v036Codec.MustMarshalJSON(v036gov.MigrateGovernance(govState)) } + + // migrate distribution state + if appState[v034distr.ModuleName] != nil { + var slashingGenState v034distr.GenesisState + v034Codec.MustUnmarshalJSON(appState[v034distr.ModuleName], &slashingGenState) + + delete(appState, v034distr.ModuleName) // delete old key in case the name changed + appState[v036distr.ModuleName] = v036Codec.MustMarshalJSON(v036distr.Migrate(slashingGenState)) + } + return appState } diff --git a/x/genutil/legacy/v036/migrate_test.go b/x/genutil/legacy/v036/migrate_test.go index 68836d513c..3909d3d4b6 100644 --- a/x/genutil/legacy/v036/migrate_test.go +++ b/x/genutil/legacy/v036/migrate_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/stretchr/testify/require" - "github.com/tendermint/go-amino" "github.com/cosmos/cosmos-sdk/x/genutil" ) @@ -91,8 +90,7 @@ func TestDummyGenesis(t *testing.T) { "foo": {}, "bar": []byte(`{"custom": "module"}`), } - cdc := amino.NewCodec() - migratedDummy := Migrate(genesisDummy, cdc) + migratedDummy := Migrate(genesisDummy) // We should not touch custom modules in the map require.Equal(t, genesisDummy["foo"], migratedDummy["foo"]) @@ -103,7 +101,6 @@ func TestGovGenesis(t *testing.T) { genesis := genutil.AppMap{ "gov": basic034Gov, } - cdc := amino.NewCodec() - require.NotPanics(t, func() { Migrate(genesis, cdc) }) + require.NotPanics(t, func() { Migrate(genesis) }) } diff --git a/x/genutil/types.go b/x/genutil/types.go index f093e1a966..88c304a38f 100644 --- a/x/genutil/types.go +++ b/x/genutil/types.go @@ -2,15 +2,13 @@ package genutil import ( "encoding/json" - - "github.com/cosmos/cosmos-sdk/codec" ) type ( // AppMap map modules names with their json raw representation AppMap map[string]json.RawMessage // MigrationCallback converts a genesis map from the previous version to the targeted one - MigrationCallback func(AppMap, *codec.Codec) AppMap + MigrationCallback func(AppMap) AppMap // MigrationMap defines a mapping from a version to a MigrationCallback MigrationMap map[string]MigrationCallback ) diff --git a/x/slashing/types/genesis.go b/x/slashing/types/genesis.go index a375235b79..804ef579ea 100644 --- a/x/slashing/types/genesis.go +++ b/x/slashing/types/genesis.go @@ -15,8 +15,9 @@ type GenesisState struct { } // NewGenesisState creates a new GenesisState object -func NewGenesisState(params Params, signingInfos map[string]ValidatorSigningInfo, - missedBlocks map[string][]MissedBlock) GenesisState { +func NewGenesisState( + params Params, signingInfos map[string]ValidatorSigningInfo, missedBlocks map[string][]MissedBlock, +) GenesisState { return GenesisState{ Params: params, diff --git a/x/slashing/types/params.go b/x/slashing/types/params.go index 6173effe01..321834c695 100644 --- a/x/slashing/types/params.go +++ b/x/slashing/types/params.go @@ -10,10 +10,10 @@ import ( // Default parameter namespace const ( - DefaultParamspace = ModuleName - DefaultMaxEvidenceAge time.Duration = 60 * 2 * time.Second - DefaultSignedBlocksWindow int64 = 100 - DefaultDowntimeJailDuration time.Duration = 60 * 10 * time.Second + DefaultParamspace = ModuleName + DefaultMaxEvidenceAge = 60 * 2 * time.Second + DefaultSignedBlocksWindow = int64(100) + DefaultDowntimeJailDuration = 60 * 10 * time.Second ) // The Double Sign Jail period ends at Max Time supported by Amino (Dec 31, 9999 - 23:59:59 GMT)