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

     1  package simulation
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"math/rand"
     7  
     8  	sdk "github.com/cosmos/cosmos-sdk/types"
     9  	"github.com/cosmos/cosmos-sdk/types/module"
    10  	"github.com/cosmos/cosmos-sdk/types/simulation"
    11  	"github.com/cosmos/cosmos-sdk/x/auth/types"
    12  	vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
    13  )
    14  
    15  // Simulation parameter constants
    16  const (
    17  	MaxMemoChars           = "max_memo_characters"
    18  	TxSigLimit             = "tx_sig_limit"
    19  	TxSizeCostPerByte      = "tx_size_cost_per_byte"
    20  	SigVerifyCostED25519   = "sig_verify_cost_ed25519"
    21  	SigVerifyCostSECP256K1 = "sig_verify_cost_secp256k1"
    22  )
    23  
    24  // RandomGenesisAccounts defines the default RandomGenesisAccountsFn used on the SDK.
    25  // It creates a slice of BaseAccount, ContinuousVestingAccount and DelayedVestingAccount.
    26  func RandomGenesisAccounts(simState *module.SimulationState) types.GenesisAccounts {
    27  	genesisAccs := make(types.GenesisAccounts, len(simState.Accounts))
    28  	for i, acc := range simState.Accounts {
    29  		bacc := types.NewBaseAccountWithAddress(acc.Address)
    30  
    31  		// Only consider making a vesting account once the initial bonded validator
    32  		// set is exhausted due to needing to track DelegatedVesting.
    33  		if !(int64(i) > simState.NumBonded && simState.Rand.Intn(100) < 50) {
    34  			genesisAccs[i] = bacc
    35  			continue
    36  		}
    37  
    38  		initialVesting := sdk.NewCoins(sdk.NewInt64Coin(simState.BondDenom, simState.Rand.Int63n(simState.InitialStake.Int64())))
    39  		var endTime int64
    40  
    41  		startTime := simState.GenTimestamp.Unix()
    42  
    43  		// Allow for some vesting accounts to vest very quickly while others very slowly.
    44  		if simState.Rand.Intn(100) < 50 {
    45  			endTime = int64(simulation.RandIntBetween(simState.Rand, int(startTime)+1, int(startTime+(60*60*24*30))))
    46  		} else {
    47  			endTime = int64(simulation.RandIntBetween(simState.Rand, int(startTime)+1, int(startTime+(60*60*12))))
    48  		}
    49  
    50  		bva, err := vestingtypes.NewBaseVestingAccount(bacc, initialVesting, endTime)
    51  		if err != nil {
    52  			panic(err)
    53  		}
    54  
    55  		if simState.Rand.Intn(100) < 50 {
    56  			genesisAccs[i] = vestingtypes.NewContinuousVestingAccountRaw(bva, startTime)
    57  		} else {
    58  			genesisAccs[i] = vestingtypes.NewDelayedVestingAccountRaw(bva)
    59  		}
    60  	}
    61  
    62  	return genesisAccs
    63  }
    64  
    65  // GenMaxMemoChars randomized MaxMemoChars
    66  func GenMaxMemoChars(r *rand.Rand) uint64 {
    67  	return uint64(simulation.RandIntBetween(r, 100, 200))
    68  }
    69  
    70  // GenTxSigLimit randomized TxSigLimit
    71  // make sure that sigLimit is always high
    72  // so that arbitrarily simulated messages from other
    73  // modules can still create valid transactions
    74  func GenTxSigLimit(r *rand.Rand) uint64 {
    75  	return uint64(r.Intn(7) + 5)
    76  }
    77  
    78  // GenTxSizeCostPerByte randomized TxSizeCostPerByte
    79  func GenTxSizeCostPerByte(r *rand.Rand) uint64 {
    80  	return uint64(simulation.RandIntBetween(r, 5, 15))
    81  }
    82  
    83  // GenSigVerifyCostED25519 randomized SigVerifyCostED25519
    84  func GenSigVerifyCostED25519(r *rand.Rand) uint64 {
    85  	return uint64(simulation.RandIntBetween(r, 500, 1000))
    86  }
    87  
    88  // GenSigVerifyCostSECP256K1 randomized SigVerifyCostSECP256K1
    89  func GenSigVerifyCostSECP256K1(r *rand.Rand) uint64 {
    90  	return uint64(simulation.RandIntBetween(r, 500, 1000))
    91  }
    92  
    93  // RandomizedGenState generates a random GenesisState for auth
    94  func RandomizedGenState(simState *module.SimulationState, randGenAccountsFn types.RandomGenesisAccountsFn) {
    95  	var maxMemoChars uint64
    96  	simState.AppParams.GetOrGenerate(MaxMemoChars, &maxMemoChars, simState.Rand, func(r *rand.Rand) { maxMemoChars = GenMaxMemoChars(r) })
    97  
    98  	var txSigLimit uint64
    99  	simState.AppParams.GetOrGenerate(TxSigLimit, &txSigLimit, simState.Rand, func(r *rand.Rand) { txSigLimit = GenTxSigLimit(r) })
   100  
   101  	var txSizeCostPerByte uint64
   102  	simState.AppParams.GetOrGenerate(TxSizeCostPerByte, &txSizeCostPerByte, simState.Rand, func(r *rand.Rand) { txSizeCostPerByte = GenTxSizeCostPerByte(r) })
   103  
   104  	var sigVerifyCostED25519 uint64
   105  	simState.AppParams.GetOrGenerate(SigVerifyCostED25519, &sigVerifyCostED25519, simState.Rand, func(r *rand.Rand) { sigVerifyCostED25519 = GenSigVerifyCostED25519(r) })
   106  
   107  	var sigVerifyCostSECP256K1 uint64
   108  	simState.AppParams.GetOrGenerate(SigVerifyCostSECP256K1, &sigVerifyCostSECP256K1, simState.Rand, func(r *rand.Rand) { sigVerifyCostSECP256K1 = GenSigVerifyCostSECP256K1(r) })
   109  
   110  	params := types.NewParams(maxMemoChars, txSigLimit, txSizeCostPerByte,
   111  		sigVerifyCostED25519, sigVerifyCostSECP256K1)
   112  	genesisAccs := randGenAccountsFn(simState)
   113  
   114  	authGenesis := types.NewGenesisState(params, genesisAccs)
   115  
   116  	bz, err := json.MarshalIndent(&authGenesis.Params, "", " ")
   117  	if err != nil {
   118  		panic(err)
   119  	}
   120  	fmt.Printf("Selected randomly generated auth parameters:\n%s\n", bz)
   121  	simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(authGenesis)
   122  }