cosmos-sdk/x/distribution/simulation/genesis.go
Marie 7f59723d88
Make JSONMarshaler methods require proto.Message (#7054)
* Make JSONMarshaler require proto.Message

* Use &msg with MarshalJSON

* Use *LegacyAmino in queriers instead of JSONMarshaler

* Revert ABCIMessageLogs String() and coins tests

* Use LegacyAmino in client/debug and fix subspace tests

* Use LegacyAmino in all legacy queriers and adapt simulation

* Make AminoCodec implement Marshaler and some godoc fixes

* Test fixes

* Remove unrelevant comment

* Use TxConfig.TxJSONEncoder

* Use encoding/json in genutil cli migrate/validate genesis cmds

* Address simulation related comments

* Use JSONMarshaler in cli tests

* Use proto.Message as respType in cli tests

* Use tmjson for tm GenesisDoc

* Update types/module/simulation.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Update types/module/module_test.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Add godoc comments

* Remove unused InsertKeyJSON

* Fix tests

Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
2020-08-26 09:39:38 +00:00

86 lines
2.6 KiB
Go

package simulation
// DONTCOVER
import (
"encoding/json"
"fmt"
"math/rand"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)
// Simulation parameter constants
const (
CommunityTax = "community_tax"
BaseProposerReward = "base_proposer_reward"
BonusProposerReward = "bonus_proposer_reward"
WithdrawEnabled = "withdraw_enabled"
)
// GenCommunityTax randomized CommunityTax
func GenCommunityTax(r *rand.Rand) sdk.Dec {
return sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(int64(r.Intn(30)), 2))
}
// GenBaseProposerReward randomized BaseProposerReward
func GenBaseProposerReward(r *rand.Rand) sdk.Dec {
return sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(int64(r.Intn(30)), 2))
}
// GenBonusProposerReward randomized BonusProposerReward
func GenBonusProposerReward(r *rand.Rand) sdk.Dec {
return sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(int64(r.Intn(30)), 2))
}
// GenWithdrawEnabled returns a randomized WithdrawEnabled parameter.
func GenWithdrawEnabled(r *rand.Rand) bool {
return r.Int63n(101) <= 95 // 95% chance of withdraws being enabled
}
// RandomizedGenState generates a random GenesisState for distribution
func RandomizedGenState(simState *module.SimulationState) {
var communityTax sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, CommunityTax, &communityTax, simState.Rand,
func(r *rand.Rand) { communityTax = GenCommunityTax(r) },
)
var baseProposerReward sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, BaseProposerReward, &baseProposerReward, simState.Rand,
func(r *rand.Rand) { baseProposerReward = GenBaseProposerReward(r) },
)
var bonusProposerReward sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, BonusProposerReward, &bonusProposerReward, simState.Rand,
func(r *rand.Rand) { bonusProposerReward = GenBonusProposerReward(r) },
)
var withdrawEnabled bool
simState.AppParams.GetOrGenerate(
simState.Cdc, WithdrawEnabled, &withdrawEnabled, simState.Rand,
func(r *rand.Rand) { withdrawEnabled = GenWithdrawEnabled(r) },
)
distrGenesis := types.GenesisState{
FeePool: types.InitialFeePool(),
Params: types.Params{
CommunityTax: communityTax,
BaseProposerReward: baseProposerReward,
BonusProposerReward: bonusProposerReward,
WithdrawAddrEnabled: withdrawEnabled,
},
}
bz, err := json.MarshalIndent(&distrGenesis, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Selected randomly generated distribution parameters:\n%s\n", bz)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&distrGenesis)
}