github.com/Finschia/finschia-sdk@v0.49.1/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/Finschia/finschia-sdk/types"
     9  	"github.com/Finschia/finschia-sdk/types/module"
    10  	"github.com/Finschia/finschia-sdk/types/simulation"
    11  	"github.com/Finschia/finschia-sdk/x/auth/types"
    12  	vestingtypes "github.com/Finschia/finschia-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(sdk.DefaultBondDenom, simState.Rand.Int63n(simState.InitialStake)))
    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 := vestingtypes.NewBaseVestingAccount(bacc, initialVesting, endTime)
    51  
    52  		if simState.Rand.Intn(100) < 50 {
    53  			genesisAccs[i] = vestingtypes.NewContinuousVestingAccountRaw(bva, startTime)
    54  		} else {
    55  			genesisAccs[i] = vestingtypes.NewDelayedVestingAccountRaw(bva)
    56  		}
    57  	}
    58  
    59  	return genesisAccs
    60  }
    61  
    62  // GenMaxMemoChars randomized MaxMemoChars
    63  func GenMaxMemoChars(r *rand.Rand) uint64 {
    64  	return uint64(simulation.RandIntBetween(r, 100, 200))
    65  }
    66  
    67  // GenTxSigLimit randomized TxSigLimit
    68  // make sure that sigLimit is always high
    69  // so that arbitrarily simulated messages from other
    70  // modules can still create valid transactions
    71  func GenTxSigLimit(r *rand.Rand) uint64 {
    72  	return uint64(r.Intn(7) + 5)
    73  }
    74  
    75  // GenTxSizeCostPerByte randomized TxSizeCostPerByte
    76  func GenTxSizeCostPerByte(r *rand.Rand) uint64 {
    77  	return uint64(simulation.RandIntBetween(r, 5, 15))
    78  }
    79  
    80  // GenSigVerifyCostED25519 randomized SigVerifyCostED25519
    81  func GenSigVerifyCostED25519(r *rand.Rand) uint64 {
    82  	return uint64(simulation.RandIntBetween(r, 500, 1000))
    83  }
    84  
    85  // GenSigVerifyCostSECP256K1 randomized SigVerifyCostSECP256K1
    86  func GenSigVerifyCostSECP256K1(r *rand.Rand) uint64 {
    87  	return uint64(simulation.RandIntBetween(r, 500, 1000))
    88  }
    89  
    90  // RandomizedGenState generates a random GenesisState for auth
    91  func RandomizedGenState(simState *module.SimulationState, randGenAccountsFn types.RandomGenesisAccountsFn) {
    92  	var maxMemoChars uint64
    93  	simState.AppParams.GetOrGenerate(
    94  		simState.Cdc, MaxMemoChars, &maxMemoChars, simState.Rand,
    95  		func(r *rand.Rand) { maxMemoChars = GenMaxMemoChars(r) },
    96  	)
    97  
    98  	var txSigLimit uint64
    99  	simState.AppParams.GetOrGenerate(
   100  		simState.Cdc, TxSigLimit, &txSigLimit, simState.Rand,
   101  		func(r *rand.Rand) { txSigLimit = GenTxSigLimit(r) },
   102  	)
   103  
   104  	var txSizeCostPerByte uint64
   105  	simState.AppParams.GetOrGenerate(
   106  		simState.Cdc, TxSizeCostPerByte, &txSizeCostPerByte, simState.Rand,
   107  		func(r *rand.Rand) { txSizeCostPerByte = GenTxSizeCostPerByte(r) },
   108  	)
   109  
   110  	var sigVerifyCostED25519 uint64
   111  	simState.AppParams.GetOrGenerate(
   112  		simState.Cdc, SigVerifyCostED25519, &sigVerifyCostED25519, simState.Rand,
   113  		func(r *rand.Rand) { sigVerifyCostED25519 = GenSigVerifyCostED25519(r) },
   114  	)
   115  
   116  	var sigVerifyCostSECP256K1 uint64
   117  	simState.AppParams.GetOrGenerate(
   118  		simState.Cdc, SigVerifyCostSECP256K1, &sigVerifyCostSECP256K1, simState.Rand,
   119  		func(r *rand.Rand) { sigVerifyCostSECP256K1 = GenSigVerifyCostSECP256K1(r) },
   120  	)
   121  
   122  	params := types.NewParams(maxMemoChars, txSigLimit, txSizeCostPerByte,
   123  		sigVerifyCostED25519, sigVerifyCostSECP256K1)
   124  	genesisAccs := randGenAccountsFn(simState)
   125  
   126  	authGenesis := types.NewGenesisState(params, genesisAccs)
   127  
   128  	bz, err := json.MarshalIndent(&authGenesis.Params, "", " ")
   129  	if err != nil {
   130  		panic(err)
   131  	}
   132  	fmt.Printf("Selected randomly generated auth parameters:\n%s\n", bz)
   133  	simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(authGenesis)
   134  }