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

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