Merge PR #4669: v0.36 distribution genesis migration

This commit is contained in:
Alexander Bezobchuk 2019-07-05 08:34:02 -04:00 committed by GitHub
parent e0b55a89a7
commit 2fdbf631e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 346 additions and 19 deletions

View File

@ -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"`
}
)

View File

@ -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,
)
}

View File

@ -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,
}
}

View File

@ -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()

View File

@ -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
}

View File

@ -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) })
}

View File

@ -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
)

View File

@ -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,

View File

@ -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)