cosmos-sdk/x/staking/simulation/genesis.go

107 lines
3.2 KiB
Go
Raw Normal View History

package simulation
// DONTCOVER
import (
"encoding/json"
"fmt"
"math/rand"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
// Simulation parameter constants
const (
unbondingTime = "unbonding_time"
maxValidators = "max_validators"
historicalEntries = "historical_entries"
)
// GenUnbondingTime randomized UnbondingTime
func GenUnbondingTime(r *rand.Rand) (ubdTime time.Duration) {
return time.Duration(simulation.RandIntBetween(r, 60, 60*60*24*3*2)) * time.Second
}
// GenMaxValidators randomized MaxValidators
func GenMaxValidators(r *rand.Rand) (maxValidators uint32) {
return uint32(r.Intn(250) + 1)
}
// GetHistEntries randomized HistoricalEntries between 0-100.
func GetHistEntries(r *rand.Rand) uint32 {
return uint32(r.Intn(int(types.DefaultHistoricalEntries + 1)))
}
// RandomizedGenState generates a random GenesisState for staking
func RandomizedGenState(simState *module.SimulationState) {
// params
var (
unbondTime time.Duration
maxVals uint32
histEntries uint32
)
simState.AppParams.GetOrGenerate(
simState.Cdc, unbondingTime, &unbondTime, simState.Rand,
func(r *rand.Rand) { unbondTime = GenUnbondingTime(r) },
)
simState.AppParams.GetOrGenerate(
simState.Cdc, maxValidators, &maxVals, simState.Rand,
func(r *rand.Rand) { maxVals = GenMaxValidators(r) },
)
simState.AppParams.GetOrGenerate(
simState.Cdc, historicalEntries, &histEntries, simState.Rand,
func(r *rand.Rand) { histEntries = GetHistEntries(r) },
)
// NOTE: the slashing module need to be defined after the staking module on the
// NewSimulationManager constructor for this to work
simState.UnbondTime = unbondTime
params := types.NewParams(simState.UnbondTime, maxVals, 7, histEntries, sdk.DefaultBondDenom)
// validators & delegations
var (
validators []types.Validator
delegations []types.Delegation
)
valAddrs := make([]sdk.ValAddress, simState.NumBonded)
for i := 0; i < int(simState.NumBonded); i++ {
valAddr := sdk.ValAddress(simState.Accounts[i].Address)
valAddrs[i] = valAddr
update simulation operations to use BaseApp (#4946) * update operations to use baseapp * updates and cleanup operations * update operations * restructure sim ops params * rename sim /operations/msg.go to /operations.go * move GenTx to a helper pkg to avoid circle deps * rm msg.ValidateBasic * changelog * random fees; delete auth's DeductFees sim operation * add chain-id for sig verification * Update x/simulation/account.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * fix bank, gov and distr errors * fix staking and slashing errors; increase prob for send enabled * increase gas x10 * make format * fix some distr and staking edge cases * fix all edge cases * golang ci * rename acc vars; default no fees to 0stake * cleanup; check for exchange rate and skip invalid ops * fixes * check for max entries * add pubkey to genaccounts * fix gov bug * update staking sim ops * fix small redelegation error * fix small self delegation on unjail * rm inf loop on random val/accs * copy array * add ok boolean to RandomValidator return values * format * Update x/bank/simulation/operations.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * Update simapp/helpers/test_helpers.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * address @colin-axner comments * add genaccount pubkey validation * fix test * update operations and move RandomFees to x/simulation * update gov ops * address @alexanderbez comments * avoid modifications to config * reorder params * changelog * Update x/distribution/simulation/genesis.go Co-Authored-By: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * remove named return values * ensure all operations are simulated * golangci * add nolint * disable whitespace and funlen linter * disable godox * add TODO on unjail * update ops weights * remove dup * update godoc * x/slashing/simulation/operations.go linting * x/staking/simulation/operations.go linting * update operations format * x/bank/simulation/operations.go linting * x/distribution/simulation/operations.go linting * x/staking/simulation/operations.go linting * start changes: make bank simulate send multiple coins, code cleanup * fix nondeterminism bug * fix txsiglimit err * fix multisend bug * simplify simulation, cleanup opt privkey args * make slashing test invalid unjail msgs * Update simapp/state.go * golangCI changes
2019-10-23 02:14:45 -07:00
maxCommission := sdk.NewDecWithPrec(int64(simulation.RandIntBetween(simState.Rand, 1, 100)), 2)
commission := types.NewCommission(
simulation.RandomDecAmount(simState.Rand, maxCommission),
maxCommission,
simulation.RandomDecAmount(simState.Rand, maxCommission),
)
validator := types.NewValidator(valAddr, simState.Accounts[i].ConsKey.PubKey(), types.Description{})
validator.Tokens = sdk.NewInt(simState.InitialStake)
validator.DelegatorShares = sdk.NewDec(simState.InitialStake)
update simulation operations to use BaseApp (#4946) * update operations to use baseapp * updates and cleanup operations * update operations * restructure sim ops params * rename sim /operations/msg.go to /operations.go * move GenTx to a helper pkg to avoid circle deps * rm msg.ValidateBasic * changelog * random fees; delete auth's DeductFees sim operation * add chain-id for sig verification * Update x/simulation/account.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * fix bank, gov and distr errors * fix staking and slashing errors; increase prob for send enabled * increase gas x10 * make format * fix some distr and staking edge cases * fix all edge cases * golang ci * rename acc vars; default no fees to 0stake * cleanup; check for exchange rate and skip invalid ops * fixes * check for max entries * add pubkey to genaccounts * fix gov bug * update staking sim ops * fix small redelegation error * fix small self delegation on unjail * rm inf loop on random val/accs * copy array * add ok boolean to RandomValidator return values * format * Update x/bank/simulation/operations.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * Update simapp/helpers/test_helpers.go Co-Authored-By: colin axner <colinaxner@berkeley.edu> * address @colin-axner comments * add genaccount pubkey validation * fix test * update operations and move RandomFees to x/simulation * update gov ops * address @alexanderbez comments * avoid modifications to config * reorder params * changelog * Update x/distribution/simulation/genesis.go Co-Authored-By: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * remove named return values * ensure all operations are simulated * golangci * add nolint * disable whitespace and funlen linter * disable godox * add TODO on unjail * update ops weights * remove dup * update godoc * x/slashing/simulation/operations.go linting * x/staking/simulation/operations.go linting * update operations format * x/bank/simulation/operations.go linting * x/distribution/simulation/operations.go linting * x/staking/simulation/operations.go linting * start changes: make bank simulate send multiple coins, code cleanup * fix nondeterminism bug * fix txsiglimit err * fix multisend bug * simplify simulation, cleanup opt privkey args * make slashing test invalid unjail msgs * Update simapp/state.go * golangCI changes
2019-10-23 02:14:45 -07:00
validator.Commission = commission
delegation := types.NewDelegation(simState.Accounts[i].Address, valAddr, sdk.NewDec(simState.InitialStake))
validators = append(validators, validator)
delegations = append(delegations, delegation)
}
stakingGenesis := types.NewGenesisState(params, validators, delegations)
bz, err := json.MarshalIndent(&stakingGenesis, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Selected randomly generated staking parameters:\n%s\n", bz)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(stakingGenesis)
}