2019-08-28 07:58:25 -07:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
// DONTCOVER
|
|
|
|
|
|
|
|
import (
|
2020-08-26 02:39:38 -07:00
|
|
|
"encoding/json"
|
2019-08-28 07:58:25 -07:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
2020-03-23 04:55:44 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/simulation"
|
2019-08-28 07:58:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Simulation parameter constants
|
|
|
|
const (
|
2020-04-23 08:06:30 -07:00
|
|
|
unbondingTime = "unbonding_time"
|
|
|
|
maxValidators = "max_validators"
|
|
|
|
historicalEntries = "historical_entries"
|
2019-08-28 07:58:25 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2020-02-06 11:21:02 -08:00
|
|
|
func GenMaxValidators(r *rand.Rand) (maxValidators uint32) {
|
|
|
|
return uint32(r.Intn(250) + 1)
|
2019-08-28 07:58:25 -07:00
|
|
|
}
|
|
|
|
|
2020-04-23 08:06:30 -07:00
|
|
|
// GetHistEntries randomized HistoricalEntries between 0-100.
|
|
|
|
func GetHistEntries(r *rand.Rand) uint32 {
|
|
|
|
return uint32(r.Intn(int(types.DefaultHistoricalEntries + 1)))
|
|
|
|
}
|
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
// RandomizedGenState generates a random GenesisState for staking
|
|
|
|
func RandomizedGenState(simState *module.SimulationState) {
|
|
|
|
// params
|
2020-05-02 12:26:59 -07:00
|
|
|
var (
|
|
|
|
unbondTime time.Duration
|
|
|
|
maxVals uint32
|
|
|
|
histEntries uint32
|
|
|
|
)
|
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
simState.AppParams.GetOrGenerate(
|
2020-04-23 08:06:30 -07:00
|
|
|
simState.Cdc, unbondingTime, &unbondTime, simState.Rand,
|
2019-08-28 07:58:25 -07:00
|
|
|
func(r *rand.Rand) { unbondTime = GenUnbondingTime(r) },
|
|
|
|
)
|
|
|
|
|
2020-04-23 08:06:30 -07:00
|
|
|
simState.AppParams.GetOrGenerate(
|
|
|
|
simState.Cdc, maxValidators, &maxVals, simState.Rand,
|
|
|
|
func(r *rand.Rand) { maxVals = GenMaxValidators(r) },
|
|
|
|
)
|
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
simState.AppParams.GetOrGenerate(
|
2020-04-23 08:06:30 -07:00
|
|
|
simState.Cdc, historicalEntries, &histEntries, simState.Rand,
|
|
|
|
func(r *rand.Rand) { histEntries = GetHistEntries(r) },
|
2019-08-28 07:58:25 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// NOTE: the slashing module need to be defined after the staking module on the
|
|
|
|
// NewSimulationManager constructor for this to work
|
|
|
|
simState.UnbondTime = unbondTime
|
2020-04-23 08:06:30 -07:00
|
|
|
params := types.NewParams(simState.UnbondTime, maxVals, 7, histEntries, sdk.DefaultBondDenom)
|
2019-08-28 07:58:25 -07:00
|
|
|
|
|
|
|
// validators & delegations
|
|
|
|
var (
|
|
|
|
validators []types.Validator
|
|
|
|
delegations []types.Delegation
|
|
|
|
)
|
|
|
|
|
|
|
|
valAddrs := make([]sdk.ValAddress, simState.NumBonded)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
for i := 0; i < int(simState.NumBonded); i++ {
|
|
|
|
valAddr := sdk.ValAddress(simState.Accounts[i].Address)
|
|
|
|
valAddrs[i] = valAddr
|
|
|
|
|
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),
|
|
|
|
)
|
|
|
|
|
2020-10-23 05:07:52 -07:00
|
|
|
validator, err := types.NewValidator(valAddr, simState.Accounts[i].ConsKey.PubKey(), types.Description{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-08-28 07:58:25 -07:00
|
|
|
validator.Tokens = sdk.NewInt(simState.InitialStake)
|
|
|
|
validator.DelegatorShares = sdk.NewDec(simState.InitialStake)
|
2019-10-23 02:14:45 -07:00
|
|
|
validator.Commission = commission
|
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
delegation := types.NewDelegation(simState.Accounts[i].Address, valAddr, sdk.NewDec(simState.InitialStake))
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
validators = append(validators, validator)
|
|
|
|
delegations = append(delegations, delegation)
|
|
|
|
}
|
|
|
|
|
|
|
|
stakingGenesis := types.NewGenesisState(params, validators, delegations)
|
|
|
|
|
2020-10-23 05:07:52 -07:00
|
|
|
bz, err := json.MarshalIndent(&stakingGenesis.Params, "", " ")
|
2020-08-26 02:39:38 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Printf("Selected randomly generated staking parameters:\n%s\n", bz)
|
2019-08-28 07:58:25 -07:00
|
|
|
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(stakingGenesis)
|
|
|
|
}
|