Skip to content

Commit

Permalink
Merge PR cosmos#4686: v0.36 supply changes to genesis migration
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze authored and alexanderbez committed Jul 18, 2019
1 parent 257d3bf commit a922dad
Show file tree
Hide file tree
Showing 35 changed files with 1,531 additions and 1,250 deletions.
42 changes: 27 additions & 15 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ var (
slashing.AppModuleBasic{},
supply.AppModuleBasic{},
)

// module account permissions
maccPerms = map[string][]string{
auth.FeeCollectorName: nil,
distr.ModuleName: nil,
mint.ModuleName: {supply.Minter},
staking.BondedPoolName: {supply.Burner, supply.Staking},
staking.NotBondedPoolName: {supply.Burner, supply.Staking},
gov.ModuleName: {supply.Burner},
}
)

// custom tx codec
Expand Down Expand Up @@ -105,16 +115,18 @@ type SimApp struct {
}

// NewSimApp returns a reference to an initialized SimApp.
func NewSimApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool,
invCheckPeriod uint, baseAppOptions ...func(*bam.BaseApp)) *SimApp {
func NewSimApp(
logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool,
invCheckPeriod uint, baseAppOptions ...func(*bam.BaseApp),
) *SimApp {

cdc := MakeCodec()

bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetAppVersion(version.Version)

var app = &SimApp{
app := &SimApp{
BaseApp: bApp,
cdc: cdc,
invCheckPeriod: invCheckPeriod,
Expand Down Expand Up @@ -143,16 +155,6 @@ func NewSimApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bo
govSubspace := app.paramsKeeper.Subspace(gov.DefaultParamspace)
crisisSubspace := app.paramsKeeper.Subspace(crisis.DefaultParamspace)

// account permissions
maccPerms := map[string][]string{
auth.FeeCollectorName: nil,
distr.ModuleName: nil,
mint.ModuleName: []string{supply.Minter},
staking.BondedPoolName: []string{supply.Burner, supply.Staking},
staking.NotBondedPoolName: []string{supply.Burner, supply.Staking},
gov.ModuleName: []string{supply.Burner},
}

// add keepers
app.accountKeeper = auth.NewAccountKeeper(app.cdc, app.keyAccount, authSubspace, auth.ProtoBaseAccount)
app.bankKeeper = bank.NewBaseKeeper(app.accountKeeper, bankSubspace, bank.DefaultCodespace)
Expand Down Expand Up @@ -202,9 +204,9 @@ func NewSimApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bo

// genutils must occur after staking so that pools are properly
// initialized with tokens from genesis accounts.
app.mm.SetOrderInitGenesis(genaccounts.ModuleName, supply.ModuleName, distr.ModuleName,
app.mm.SetOrderInitGenesis(genaccounts.ModuleName, distr.ModuleName,
staking.ModuleName, auth.ModuleName, bank.ModuleName, slashing.ModuleName,
gov.ModuleName, mint.ModuleName, crisis.ModuleName, genutil.ModuleName)
gov.ModuleName, mint.ModuleName, supply.ModuleName, crisis.ModuleName, genutil.ModuleName)

app.mm.RegisterInvariants(&app.crisisKeeper)
app.mm.RegisterRoutes(app.Router(), app.QueryRouter())
Expand Down Expand Up @@ -250,3 +252,13 @@ func (app *SimApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.
func (app *SimApp) LoadHeight(height int64) error {
return app.LoadVersion(height, app.keyMain)
}

// ModuleAccountAddrs returns all the app's module account addresses.
func (app *SimApp) ModuleAccountAddrs() map[string]bool {
modAccAddrs := make(map[string]bool)
for acc := range maccPerms {
modAccAddrs[app.supplyKeeper.GetModuleAddress(acc).String()] = true
}

return modAccAddrs
}
7 changes: 5 additions & 2 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ func init() {
// helper function for populating input for SimulateFromSeed
func getSimulateFromSeedInput(tb testing.TB, w io.Writer, app *SimApp) (
testing.TB, io.Writer, *baseapp.BaseApp, simulation.AppStateFn, int64,
simulation.WeightedOperations, sdk.Invariants, int, int, bool, bool, bool, bool) {
simulation.WeightedOperations, sdk.Invariants, int, int, bool, bool, bool, bool, map[string]bool) {

return tb, w, app.BaseApp, appStateFn, seed,
testAndRunTxs(app), invariants(app), numBlocks, blockSize, commit, lean, onOperation, allInvariants
testAndRunTxs(app), invariants(app), numBlocks, blockSize, commit,
lean, onOperation, allInvariants, app.ModuleAccountAddrs()
}

func appStateFn(
Expand Down Expand Up @@ -604,6 +605,7 @@ func TestAppStateDeterminism(t *testing.T) {
false,
false,
false,
app.ModuleAccountAddrs(),
)
appHash := app.LastCommitID().Hash
appHashList[j] = appHash
Expand All @@ -630,6 +632,7 @@ func BenchmarkInvariants(b *testing.B) {
_, err := simulation.SimulateFromSeed(
b, ioutil.Discard, app.BaseApp, appStateFn, seed, testAndRunTxs(app),
[]sdk.Invariant{}, numBlocks, blockSize, commit, lean, onOperation, false,
app.ModuleAccountAddrs(),
)
if err != nil {
fmt.Println(err)
Expand Down
26 changes: 26 additions & 0 deletions x/auth/legacy/v0_34/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// DONTCOVER
// nolint
package v0_34

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
ModuleName = "auth"
)

type (
Params struct {
MaxMemoCharacters uint64 `json:"max_memo_characters"`
TxSigLimit uint64 `json:"tx_sig_limit"`
TxSizeCostPerByte uint64 `json:"tx_size_cost_per_byte"`
SigVerifyCostED25519 uint64 `json:"sig_verify_cost_ed25519"`
SigVerifyCostSecp256k1 uint64 `json:"sig_verify_cost_secp256k1"`
}

GenesisState struct {
CollectedFees sdk.Coins `json:"collected_fees"`
Params Params `json:"params"`
}
)
14 changes: 14 additions & 0 deletions x/auth/legacy/v0_36/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// DONTCOVER
// nolint
package v0_36

import (
v034auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_34"
)

// Migrate accepts exported genesis state from v0.34 and migrates it to v0.36
// genesis state. This migration removes the CollectedFees coins from the old
// FeeCollectorKeeper.
func Migrate(oldGenState v034auth.GenesisState) GenesisState {
return NewGenesisState(oldGenState.Params)
}
25 changes: 25 additions & 0 deletions x/auth/legacy/v0_36/migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package v0_36

import (
"github.com/cosmos/cosmos-sdk/types"
v034auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_34"
"testing"

"github.com/stretchr/testify/require"
)

func TestMigrate(t *testing.T) {
var genesisState GenesisState
require.NotPanics(t, func() {
genesisState = Migrate(v034auth.GenesisState{
CollectedFees: types.Coins{
{
Amount: types.NewInt(10),
Denom: "stake",
},
},
Params: v034auth.Params{}, // forwarded structure: filling and checking will be testing a no-op
})
})
require.Equal(t, genesisState, GenesisState{Params: v034auth.Params{}})
}
19 changes: 19 additions & 0 deletions x/auth/legacy/v0_36/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// DONTCOVER
// nolint
package v0_36

import v034auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_34"

const (
ModuleName = "auth"
)

type (
GenesisState struct {
Params v034auth.Params `json:"params"`
}
)

func NewGenesisState(params v034auth.Params) GenesisState {
return GenesisState{params}
}
2 changes: 1 addition & 1 deletion x/auth/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
StoreKey = "acc"

// FeeCollectorName the root string for the fee collector account address
FeeCollectorName = "FeeCollector"
FeeCollectorName = "fee_collector"

// QuerierRoute is the querier route for acc
QuerierRoute = StoreKey
Expand Down
22 changes: 12 additions & 10 deletions x/distribution/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,9 @@ func (k Keeper) withdrawDelegationRewards(ctx sdk.Context, val exported.Validato
val.GetOperator(), del.GetDelegatorAddr(), rewardsRaw, rewards))
}

// decrement reference count of starting period
startingInfo := k.GetDelegatorStartingInfo(ctx, del.GetValidatorAddr(), del.GetDelegatorAddr())
startingPeriod := startingInfo.PreviousPeriod
k.decrementReferenceCount(ctx, del.GetValidatorAddr(), startingPeriod)

// truncate coins, return remainder to community pool
coins, remainder := rewards.TruncateDecimal()

k.SetValidatorOutstandingRewards(ctx, del.GetValidatorAddr(), outstanding.Sub(rewards))
feePool := k.GetFeePool(ctx)
feePool.CommunityPool = feePool.CommunityPool.Add(remainder)
k.SetFeePool(ctx, feePool)

// add coins to user account
if !coins.IsZero() {
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, del.GetDelegatorAddr())
Expand All @@ -179,6 +169,18 @@ func (k Keeper) withdrawDelegationRewards(ctx sdk.Context, val exported.Validato
}
}

// update the outstanding rewards and the community pool only if the
// transaction was successful
k.SetValidatorOutstandingRewards(ctx, del.GetValidatorAddr(), outstanding.Sub(rewards))
feePool := k.GetFeePool(ctx)
feePool.CommunityPool = feePool.CommunityPool.Add(remainder)
k.SetFeePool(ctx, feePool)

// decrement reference count of starting period
startingInfo := k.GetDelegatorStartingInfo(ctx, del.GetValidatorAddr(), del.GetDelegatorAddr())
startingPeriod := startingInfo.PreviousPeriod
k.decrementReferenceCount(ctx, del.GetValidatorAddr(), startingPeriod)

// remove delegator starting info
k.DeleteDelegatorStartingInfo(ctx, del.GetValidatorAddr(), del.GetDelegatorAddr())

Expand Down
75 changes: 6 additions & 69 deletions x/distribution/legacy/v0_36/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,86 +8,23 @@ import (
// 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,
},
Event: se.Event,
}
}

return NewGenesisState(
feePool, oldGenState.CommunityTax, oldGenState.BaseProposerReward,
oldGenState.FeePool, oldGenState.CommunityTax, oldGenState.BaseProposerReward,
oldGenState.BonusProposerReward, oldGenState.WithdrawAddrEnabled,
dwifos, oldGenState.PreviousProposer, outRewards, accumComm,
histRewards, currRewards, delStartingInfos, slashEvents,
oldGenState.DelegatorWithdrawInfos, oldGenState.PreviousProposer,
oldGenState.OutstandingRewards, oldGenState.ValidatorAccumulatedCommissions,
oldGenState.ValidatorHistoricalRewards, oldGenState.ValidatorCurrentRewards,
oldGenState.DelegatorStartingInfos, slashEvents,
)
}
Loading

0 comments on commit a922dad

Please sign in to comment.