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

     1  package simulation
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"math/rand"
     7  	"time"
     8  
     9  	sdkmath "cosmossdk.io/math"
    10  
    11  	sdk "github.com/cosmos/cosmos-sdk/types"
    12  	"github.com/cosmos/cosmos-sdk/types/module"
    13  	"github.com/cosmos/cosmos-sdk/types/simulation"
    14  	"github.com/cosmos/cosmos-sdk/x/gov/types"
    15  	v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
    16  )
    17  
    18  // Simulation parameter constants
    19  const (
    20  	MinDeposit            = "min_deposit"
    21  	ExpeditedMinDeposit   = "expedited_min_deposit"
    22  	DepositPeriod         = "deposit_period"
    23  	MinInitialRatio       = "min_initial_ratio"
    24  	VotingPeriod          = "voting_period"
    25  	ExpeditedVotingPeriod = "expedited_voting_period"
    26  	Quorum                = "quorum"
    27  	Threshold             = "threshold"
    28  	ExpeditedThreshold    = "expedited_threshold"
    29  	Veto                  = "veto"
    30  	ProposalCancelRate    = "proposal_cancel_rate"
    31  	MinDepositRatio       = "min_deposit_ratio"
    32  
    33  	// ExpeditedThreshold must be at least as large as the regular Threshold
    34  	// Therefore, we use this break out point in randomization.
    35  	tallyNonExpeditedMax = 500
    36  
    37  	// Similarly, expedited voting period must be strictly less than the regular
    38  	// voting period to be valid. Therefore, we use this break out point in randomization.
    39  	expeditedMaxVotingPeriod = 60 * 60 * 24 * 2
    40  )
    41  
    42  // GenDepositPeriod returns randomized DepositPeriod
    43  func GenDepositPeriod(r *rand.Rand) time.Duration {
    44  	return time.Duration(simulation.RandIntBetween(r, 1, 2*60*60*24*2)) * time.Second
    45  }
    46  
    47  // GenMinDeposit returns randomized MinDeposit
    48  func GenMinDeposit(r *rand.Rand, bondDenom string) sdk.Coins {
    49  	return sdk.NewCoins(sdk.NewInt64Coin(bondDenom, int64(simulation.RandIntBetween(r, 1, 1e3/2))))
    50  }
    51  
    52  // GenExpeditedMinDeposit returns randomized ExpeditedMinDeposit
    53  // It is always greater than GenMinDeposit
    54  func GenExpeditedMinDeposit(r *rand.Rand, bondDenom string) sdk.Coins {
    55  	return sdk.NewCoins(sdk.NewInt64Coin(bondDenom, int64(simulation.RandIntBetween(r, 1e3/2, 1e3))))
    56  }
    57  
    58  // GenDepositMinInitialRatio returns randomized DepositMinInitialRatio
    59  func GenDepositMinInitialDepositRatio(r *rand.Rand) sdkmath.LegacyDec {
    60  	return sdkmath.LegacyNewDec(int64(simulation.RandIntBetween(r, 0, 99))).Quo(sdkmath.LegacyNewDec(100))
    61  }
    62  
    63  // GenProposalCancelRate returns randomized ProposalCancelRate
    64  func GenProposalCancelRate(r *rand.Rand) sdkmath.LegacyDec {
    65  	return sdkmath.LegacyNewDec(int64(simulation.RandIntBetween(r, 0, 99))).Quo(sdkmath.LegacyNewDec(100))
    66  }
    67  
    68  // GenVotingPeriod returns randomized VotingPeriod
    69  func GenVotingPeriod(r *rand.Rand) time.Duration {
    70  	return time.Duration(simulation.RandIntBetween(r, expeditedMaxVotingPeriod, 2*expeditedMaxVotingPeriod)) * time.Second
    71  }
    72  
    73  // GenExpeditedVotingPeriod randomized ExpeditedVotingPeriod
    74  func GenExpeditedVotingPeriod(r *rand.Rand) time.Duration {
    75  	return time.Duration(simulation.RandIntBetween(r, 1, expeditedMaxVotingPeriod)) * time.Second
    76  }
    77  
    78  // GenQuorum returns randomized Quorum
    79  func GenQuorum(r *rand.Rand) sdkmath.LegacyDec {
    80  	return sdkmath.LegacyNewDecWithPrec(int64(simulation.RandIntBetween(r, 334, 500)), 3)
    81  }
    82  
    83  // GenThreshold returns randomized Threshold
    84  func GenThreshold(r *rand.Rand) sdkmath.LegacyDec {
    85  	return sdkmath.LegacyNewDecWithPrec(int64(simulation.RandIntBetween(r, 450, tallyNonExpeditedMax+1)), 3)
    86  }
    87  
    88  // GenExpeditedThreshold randomized ExpeditedThreshold
    89  func GenExpeditedThreshold(r *rand.Rand) sdkmath.LegacyDec {
    90  	return sdkmath.LegacyNewDecWithPrec(int64(simulation.RandIntBetween(r, tallyNonExpeditedMax, 550)), 3)
    91  }
    92  
    93  // GenVeto returns randomized Veto
    94  func GenVeto(r *rand.Rand) sdkmath.LegacyDec {
    95  	return sdkmath.LegacyNewDecWithPrec(int64(simulation.RandIntBetween(r, 250, 334)), 3)
    96  }
    97  
    98  // GenMinDepositRatio returns randomized DepositMinRatio
    99  func GenMinDepositRatio(r *rand.Rand) sdkmath.LegacyDec {
   100  	return sdkmath.LegacyMustNewDecFromStr("0.01")
   101  }
   102  
   103  // RandomizedGenState generates a random GenesisState for gov
   104  func RandomizedGenState(simState *module.SimulationState) {
   105  	startingProposalID := uint64(simState.Rand.Intn(100))
   106  
   107  	var minDeposit sdk.Coins
   108  	simState.AppParams.GetOrGenerate(MinDeposit, &minDeposit, simState.Rand, func(r *rand.Rand) { minDeposit = GenMinDeposit(r, simState.BondDenom) })
   109  
   110  	var expeditedMinDeposit sdk.Coins
   111  	simState.AppParams.GetOrGenerate(ExpeditedMinDeposit, &expeditedMinDeposit, simState.Rand, func(r *rand.Rand) { expeditedMinDeposit = GenExpeditedMinDeposit(r, simState.BondDenom) })
   112  
   113  	var depositPeriod time.Duration
   114  	simState.AppParams.GetOrGenerate(DepositPeriod, &depositPeriod, simState.Rand, func(r *rand.Rand) { depositPeriod = GenDepositPeriod(r) })
   115  
   116  	var minInitialDepositRatio sdkmath.LegacyDec
   117  	simState.AppParams.GetOrGenerate(MinInitialRatio, &minInitialDepositRatio, simState.Rand, func(r *rand.Rand) { minInitialDepositRatio = GenDepositMinInitialDepositRatio(r) })
   118  
   119  	var proposalCancelRate sdkmath.LegacyDec
   120  	simState.AppParams.GetOrGenerate(ProposalCancelRate, &proposalCancelRate, simState.Rand, func(r *rand.Rand) { proposalCancelRate = GenProposalCancelRate(r) })
   121  
   122  	var votingPeriod time.Duration
   123  	simState.AppParams.GetOrGenerate(VotingPeriod, &votingPeriod, simState.Rand, func(r *rand.Rand) { votingPeriod = GenVotingPeriod(r) })
   124  
   125  	var expeditedVotingPeriod time.Duration
   126  	simState.AppParams.GetOrGenerate(ExpeditedVotingPeriod, &expeditedVotingPeriod, simState.Rand, func(r *rand.Rand) { expeditedVotingPeriod = GenExpeditedVotingPeriod(r) })
   127  
   128  	var quorum sdkmath.LegacyDec
   129  	simState.AppParams.GetOrGenerate(Quorum, &quorum, simState.Rand, func(r *rand.Rand) { quorum = GenQuorum(r) })
   130  
   131  	var threshold sdkmath.LegacyDec
   132  	simState.AppParams.GetOrGenerate(Threshold, &threshold, simState.Rand, func(r *rand.Rand) { threshold = GenThreshold(r) })
   133  
   134  	var expitedVotingThreshold sdkmath.LegacyDec
   135  	simState.AppParams.GetOrGenerate(ExpeditedThreshold, &expitedVotingThreshold, simState.Rand, func(r *rand.Rand) { expitedVotingThreshold = GenExpeditedThreshold(r) })
   136  
   137  	var veto sdkmath.LegacyDec
   138  	simState.AppParams.GetOrGenerate(Veto, &veto, simState.Rand, func(r *rand.Rand) { veto = GenVeto(r) })
   139  
   140  	var minDepositRatio sdkmath.LegacyDec
   141  	simState.AppParams.GetOrGenerate(MinDepositRatio, &minDepositRatio, simState.Rand, func(r *rand.Rand) { minDepositRatio = GenMinDepositRatio(r) })
   142  
   143  	govGenesis := v1.NewGenesisState(
   144  		startingProposalID,
   145  		v1.NewParams(minDeposit, expeditedMinDeposit, depositPeriod, votingPeriod, expeditedVotingPeriod, quorum.String(), threshold.String(), expitedVotingThreshold.String(), veto.String(), minInitialDepositRatio.String(), proposalCancelRate.String(), "", simState.Rand.Intn(2) == 0, simState.Rand.Intn(2) == 0, simState.Rand.Intn(2) == 0, minDepositRatio.String()),
   146  	)
   147  
   148  	bz, err := json.MarshalIndent(&govGenesis, "", " ")
   149  	if err != nil {
   150  		panic(err)
   151  	}
   152  	fmt.Printf("Selected randomly generated governance parameters:\n%s\n", bz)
   153  	simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(govGenesis)
   154  }