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

     1  package simulation
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"math/rand"
     7  
     8  	"cosmossdk.io/math"
     9  
    10  	"github.com/cosmos/cosmos-sdk/types/module"
    11  	"github.com/cosmos/cosmos-sdk/x/distribution/types"
    12  )
    13  
    14  // Simulation parameter constants
    15  const (
    16  	CommunityTax    = "community_tax"
    17  	WithdrawEnabled = "withdraw_enabled"
    18  )
    19  
    20  // GenCommunityTax randomized CommunityTax
    21  func GenCommunityTax(r *rand.Rand) math.LegacyDec {
    22  	return math.LegacyNewDecWithPrec(1, 2).Add(math.LegacyNewDecWithPrec(int64(r.Intn(30)), 2))
    23  }
    24  
    25  // GenWithdrawEnabled returns a randomized WithdrawEnabled parameter.
    26  func GenWithdrawEnabled(r *rand.Rand) bool {
    27  	return r.Int63n(101) <= 95 // 95% chance of withdraws being enabled
    28  }
    29  
    30  // RandomizedGenState generates a random GenesisState for distribution
    31  func RandomizedGenState(simState *module.SimulationState) {
    32  	var communityTax math.LegacyDec
    33  	simState.AppParams.GetOrGenerate(CommunityTax, &communityTax, simState.Rand, func(r *rand.Rand) { communityTax = GenCommunityTax(r) })
    34  
    35  	var withdrawEnabled bool
    36  	simState.AppParams.GetOrGenerate(WithdrawEnabled, &withdrawEnabled, simState.Rand, func(r *rand.Rand) { withdrawEnabled = GenWithdrawEnabled(r) })
    37  
    38  	distrGenesis := types.GenesisState{
    39  		FeePool: types.InitialFeePool(),
    40  		Params: types.Params{
    41  			CommunityTax:        communityTax,
    42  			WithdrawAddrEnabled: withdrawEnabled,
    43  		},
    44  	}
    45  
    46  	bz, err := json.MarshalIndent(&distrGenesis, "", " ")
    47  	if err != nil {
    48  		panic(err)
    49  	}
    50  	fmt.Printf("Selected randomly generated distribution parameters:\n%s\n", bz)
    51  	simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&distrGenesis)
    52  }