github.com/Finschia/finschia-sdk@v0.49.1/x/gov/simulation/genesis.go (about)

     1  package simulation
     2  
     3  // DONTCOVER
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"math/rand"
     9  	"time"
    10  
    11  	sdk "github.com/Finschia/finschia-sdk/types"
    12  	"github.com/Finschia/finschia-sdk/types/module"
    13  	"github.com/Finschia/finschia-sdk/types/simulation"
    14  	"github.com/Finschia/finschia-sdk/x/gov/types"
    15  )
    16  
    17  // Simulation parameter constants
    18  const (
    19  	DepositParamsMinDeposit    = "deposit_params_min_deposit"
    20  	DepositParamsDepositPeriod = "deposit_params_deposit_period"
    21  	VotingParamsVotingPeriod   = "voting_params_voting_period"
    22  	TallyParamsQuorum          = "tally_params_quorum"
    23  	TallyParamsThreshold       = "tally_params_threshold"
    24  	TallyParamsVeto            = "tally_params_veto"
    25  )
    26  
    27  // GenDepositParamsDepositPeriod randomized DepositParamsDepositPeriod
    28  func GenDepositParamsDepositPeriod(r *rand.Rand) time.Duration {
    29  	return time.Duration(simulation.RandIntBetween(r, 1, 2*60*60*24*2)) * time.Second
    30  }
    31  
    32  // GenDepositParamsMinDeposit randomized DepositParamsMinDeposit
    33  func GenDepositParamsMinDeposit(r *rand.Rand) sdk.Coins {
    34  	return sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, int64(simulation.RandIntBetween(r, 1, 1e3))))
    35  }
    36  
    37  // GenVotingParamsVotingPeriod randomized VotingParamsVotingPeriod
    38  func GenVotingParamsVotingPeriod(r *rand.Rand) time.Duration {
    39  	return time.Duration(simulation.RandIntBetween(r, 1, 2*60*60*24*2)) * time.Second
    40  }
    41  
    42  // GenTallyParamsQuorum randomized TallyParamsQuorum
    43  func GenTallyParamsQuorum(r *rand.Rand) sdk.Dec {
    44  	return sdk.NewDecWithPrec(int64(simulation.RandIntBetween(r, 334, 500)), 3)
    45  }
    46  
    47  // GenTallyParamsThreshold randomized TallyParamsThreshold
    48  func GenTallyParamsThreshold(r *rand.Rand) sdk.Dec {
    49  	return sdk.NewDecWithPrec(int64(simulation.RandIntBetween(r, 450, 550)), 3)
    50  }
    51  
    52  // GenTallyParamsVeto randomized TallyParamsVeto
    53  func GenTallyParamsVeto(r *rand.Rand) sdk.Dec {
    54  	return sdk.NewDecWithPrec(int64(simulation.RandIntBetween(r, 250, 334)), 3)
    55  }
    56  
    57  // RandomizedGenState generates a random GenesisState for gov
    58  func RandomizedGenState(simState *module.SimulationState) {
    59  	startingProposalID := uint64(simState.Rand.Intn(100))
    60  
    61  	var minDeposit sdk.Coins
    62  	simState.AppParams.GetOrGenerate(
    63  		simState.Cdc, DepositParamsMinDeposit, &minDeposit, simState.Rand,
    64  		func(r *rand.Rand) { minDeposit = GenDepositParamsMinDeposit(r) },
    65  	)
    66  
    67  	var depositPeriod time.Duration
    68  	simState.AppParams.GetOrGenerate(
    69  		simState.Cdc, DepositParamsDepositPeriod, &depositPeriod, simState.Rand,
    70  		func(r *rand.Rand) { depositPeriod = GenDepositParamsDepositPeriod(r) },
    71  	)
    72  
    73  	var votingPeriod time.Duration
    74  	simState.AppParams.GetOrGenerate(
    75  		simState.Cdc, VotingParamsVotingPeriod, &votingPeriod, simState.Rand,
    76  		func(r *rand.Rand) { votingPeriod = GenVotingParamsVotingPeriod(r) },
    77  	)
    78  
    79  	var quorum sdk.Dec
    80  	simState.AppParams.GetOrGenerate(
    81  		simState.Cdc, TallyParamsQuorum, &quorum, simState.Rand,
    82  		func(r *rand.Rand) { quorum = GenTallyParamsQuorum(r) },
    83  	)
    84  
    85  	var threshold sdk.Dec
    86  	simState.AppParams.GetOrGenerate(
    87  		simState.Cdc, TallyParamsThreshold, &threshold, simState.Rand,
    88  		func(r *rand.Rand) { threshold = GenTallyParamsThreshold(r) },
    89  	)
    90  
    91  	var veto sdk.Dec
    92  	simState.AppParams.GetOrGenerate(
    93  		simState.Cdc, TallyParamsVeto, &veto, simState.Rand,
    94  		func(r *rand.Rand) { veto = GenTallyParamsVeto(r) },
    95  	)
    96  
    97  	govGenesis := types.NewGenesisState(
    98  		startingProposalID,
    99  		types.NewDepositParams(minDeposit, depositPeriod),
   100  		types.NewVotingParams(votingPeriod),
   101  		types.NewTallyParams(quorum, threshold, veto),
   102  	)
   103  
   104  	bz, err := json.MarshalIndent(&govGenesis, "", " ")
   105  	if err != nil {
   106  		panic(err)
   107  	}
   108  	fmt.Printf("Selected randomly generated governance parameters:\n%s\n", bz)
   109  	simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(govGenesis)
   110  }