github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/staking/simulation/genesis.go (about)

     1  package simulation
     2  
     3  // DONTCOVER
     4  
     5  import (
     6  	"fmt"
     7  	"math/rand"
     8  	"time"
     9  
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    11  
    12  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    13  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module"
    14  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/simulation"
    15  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/staking/types"
    16  )
    17  
    18  // Simulation parameter constants
    19  const (
    20  	UnbondingTime = "unbonding_time"
    21  	MaxValidators = "max_validators"
    22  )
    23  
    24  // GenUnbondingTime 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 randomized MaxValidators
    30  func GenMaxValidators(r *rand.Rand) (maxValidators uint16) {
    31  	return uint16(r.Intn(250) + 1)
    32  }
    33  
    34  // RandomizedGenState generates a random GenesisState for staking
    35  func RandomizedGenState(simState *module.SimulationState) {
    36  	// params
    37  	var unbondTime time.Duration
    38  	simState.AppParams.GetOrGenerate(
    39  		simState.Cdc, UnbondingTime, &unbondTime, simState.Rand,
    40  		func(r *rand.Rand) { unbondTime = GenUnbondingTime(r) },
    41  	)
    42  
    43  	var maxValidators uint16
    44  	simState.AppParams.GetOrGenerate(
    45  		simState.Cdc, MaxValidators, &maxValidators, simState.Rand,
    46  		func(r *rand.Rand) { maxValidators = GenMaxValidators(r) },
    47  	)
    48  
    49  	// NOTE: the slashing module need to be defined after the staking module on the
    50  	// NewSimulationManager constructor for this to work
    51  	simState.UnbondTime = unbondTime
    52  
    53  	params := types.NewParams(simState.UnbondTime, maxValidators, 7, 3, sdk.DefaultBondDenom)
    54  
    55  	// validators & delegations
    56  	var (
    57  		validators  []types.Validator
    58  		delegations []types.Delegation
    59  	)
    60  
    61  	valAddrs := make([]sdk.ValAddress, simState.NumBonded)
    62  	for i := 0; i < int(simState.NumBonded); i++ {
    63  		valAddr := sdk.ValAddress(simState.Accounts[i].Address)
    64  		valAddrs[i] = valAddr
    65  
    66  		maxCommission := sdk.NewDecWithPrec(int64(simulation.RandIntBetween(simState.Rand, 1, 100)), 2)
    67  		commission := types.NewCommission(
    68  			simulation.RandomDecAmount(simState.Rand, maxCommission),
    69  			maxCommission,
    70  			simulation.RandomDecAmount(simState.Rand, maxCommission),
    71  		)
    72  
    73  		validator := types.NewValidator(valAddr, simState.Accounts[i].PubKey, types.Description{})
    74  		validator.Tokens = sdk.NewInt(simState.InitialStake)
    75  		validator.DelegatorShares = sdk.NewDec(simState.InitialStake)
    76  		validator.Commission = commission
    77  
    78  		delegation := types.NewDelegation(simState.Accounts[i].Address, valAddr, sdk.NewDec(simState.InitialStake))
    79  		validators = append(validators, validator)
    80  		delegations = append(delegations, delegation)
    81  	}
    82  
    83  	stakingGenesis := types.NewGenesisState(params, validators, delegations)
    84  
    85  	fmt.Printf("Selected randomly generated staking parameters:\n%s\n", codec.MustMarshalJSONIndent(simState.Cdc, stakingGenesis.Params))
    86  	simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(stakingGenesis)
    87  }