github.com/cosmos/cosmos-sdk@v0.50.10/x/staking/simulation/genesis.go (about)

     1  package simulation
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"math/rand"
     7  	"time"
     8  
     9  	sdkmath "cosmossdk.io/math"
    10  
    11  	sdk "github.com/cosmos/cosmos-sdk/types"
    12  	"github.com/cosmos/cosmos-sdk/types/module"
    13  	"github.com/cosmos/cosmos-sdk/types/simulation"
    14  	"github.com/cosmos/cosmos-sdk/x/staking/types"
    15  )
    16  
    17  // Simulation parameter constants
    18  const (
    19  	unbondingTime     = "unbonding_time"
    20  	maxValidators     = "max_validators"
    21  	historicalEntries = "historical_entries"
    22  )
    23  
    24  // genUnbondingTime returns randomized UnbondingTime
    25  func genUnbondingTime(r *rand.Rand) (ubdTime time.Duration) {
    26  	return time.Duration(simulation.RandIntBetween(r, 60, 60*60*24*3*2)) * time.Second
    27  }
    28  
    29  // genMaxValidators returns randomized MaxValidators
    30  func genMaxValidators(r *rand.Rand) (maxValidators uint32) {
    31  	return uint32(r.Intn(250) + 1)
    32  }
    33  
    34  // getHistEntries returns randomized HistoricalEntries between 0-100.
    35  func getHistEntries(r *rand.Rand) uint32 {
    36  	return uint32(r.Intn(int(types.DefaultHistoricalEntries + 1)))
    37  }
    38  
    39  // RandomizedGenState generates a random GenesisState for staking
    40  func RandomizedGenState(simState *module.SimulationState) {
    41  	// params
    42  	var (
    43  		unbondTime        time.Duration
    44  		maxVals           uint32
    45  		histEntries       uint32
    46  		minCommissionRate sdkmath.LegacyDec
    47  	)
    48  
    49  	simState.AppParams.GetOrGenerate(unbondingTime, &unbondTime, simState.Rand, func(r *rand.Rand) { unbondTime = genUnbondingTime(r) })
    50  
    51  	simState.AppParams.GetOrGenerate(maxValidators, &maxVals, simState.Rand, func(r *rand.Rand) { maxVals = genMaxValidators(r) })
    52  
    53  	simState.AppParams.GetOrGenerate(historicalEntries, &histEntries, simState.Rand, func(r *rand.Rand) { histEntries = getHistEntries(r) })
    54  
    55  	// NOTE: the slashing module need to be defined after the staking module on the
    56  	// NewSimulationManager constructor for this to work
    57  	simState.UnbondTime = unbondTime
    58  	params := types.NewParams(simState.UnbondTime, maxVals, 7, histEntries, simState.BondDenom, minCommissionRate)
    59  
    60  	// validators & delegations
    61  	var (
    62  		validators  []types.Validator
    63  		delegations []types.Delegation
    64  	)
    65  
    66  	valAddrs := make([]sdk.ValAddress, simState.NumBonded)
    67  
    68  	for i := 0; i < int(simState.NumBonded); i++ {
    69  		valAddr := sdk.ValAddress(simState.Accounts[i].Address)
    70  		valAddrs[i] = valAddr
    71  
    72  		maxCommission := sdkmath.LegacyNewDecWithPrec(int64(simulation.RandIntBetween(simState.Rand, 1, 100)), 2)
    73  		commission := types.NewCommission(
    74  			simulation.RandomDecAmount(simState.Rand, maxCommission),
    75  			maxCommission,
    76  			simulation.RandomDecAmount(simState.Rand, maxCommission),
    77  		)
    78  
    79  		validator, err := types.NewValidator(valAddr.String(), simState.Accounts[i].ConsKey.PubKey(), types.Description{})
    80  		if err != nil {
    81  			panic(err)
    82  		}
    83  		validator.Tokens = simState.InitialStake
    84  		validator.DelegatorShares = sdkmath.LegacyNewDecFromInt(simState.InitialStake)
    85  		validator.Commission = commission
    86  
    87  		delegation := types.NewDelegation(simState.Accounts[i].Address.String(), valAddr.String(), sdkmath.LegacyNewDecFromInt(simState.InitialStake))
    88  
    89  		validators = append(validators, validator)
    90  		delegations = append(delegations, delegation)
    91  	}
    92  
    93  	stakingGenesis := types.NewGenesisState(params, validators, delegations)
    94  
    95  	bz, err := json.MarshalIndent(&stakingGenesis.Params, "", " ")
    96  	if err != nil {
    97  		panic(err)
    98  	}
    99  	fmt.Printf("Selected randomly generated staking parameters:\n%s\n", bz)
   100  	simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(stakingGenesis)
   101  }