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

     1  package simulation
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"math/rand"
     7  	"time"
     8  
     9  	"cosmossdk.io/math"
    10  
    11  	"github.com/cosmos/cosmos-sdk/types/module"
    12  	"github.com/cosmos/cosmos-sdk/types/simulation"
    13  	"github.com/cosmos/cosmos-sdk/x/slashing/types"
    14  )
    15  
    16  // Simulation parameter constants
    17  const (
    18  	SignedBlocksWindow      = "signed_blocks_window"
    19  	MinSignedPerWindow      = "min_signed_per_window"
    20  	DowntimeJailDuration    = "downtime_jail_duration"
    21  	SlashFractionDoubleSign = "slash_fraction_double_sign"
    22  	SlashFractionDowntime   = "slash_fraction_downtime"
    23  )
    24  
    25  // GenSignedBlocksWindow randomized SignedBlocksWindow
    26  func GenSignedBlocksWindow(r *rand.Rand) int64 {
    27  	return int64(simulation.RandIntBetween(r, 10, 1000))
    28  }
    29  
    30  // GenMinSignedPerWindow randomized MinSignedPerWindow
    31  func GenMinSignedPerWindow(r *rand.Rand) math.LegacyDec {
    32  	return math.LegacyNewDecWithPrec(int64(r.Intn(10)), 1)
    33  }
    34  
    35  // GenDowntimeJailDuration randomized DowntimeJailDuration
    36  func GenDowntimeJailDuration(r *rand.Rand) time.Duration {
    37  	return time.Duration(simulation.RandIntBetween(r, 60, 60*60*24)) * time.Second
    38  }
    39  
    40  // GenSlashFractionDoubleSign randomized SlashFractionDoubleSign
    41  func GenSlashFractionDoubleSign(r *rand.Rand) math.LegacyDec {
    42  	return math.LegacyNewDec(1).Quo(math.LegacyNewDec(int64(r.Intn(50) + 1)))
    43  }
    44  
    45  // GenSlashFractionDowntime randomized SlashFractionDowntime
    46  func GenSlashFractionDowntime(r *rand.Rand) math.LegacyDec {
    47  	return math.LegacyNewDec(1).Quo(math.LegacyNewDec(int64(r.Intn(200) + 1)))
    48  }
    49  
    50  // RandomizedGenState generates a random GenesisState for slashing
    51  func RandomizedGenState(simState *module.SimulationState) {
    52  	var signedBlocksWindow int64
    53  	simState.AppParams.GetOrGenerate(SignedBlocksWindow, &signedBlocksWindow, simState.Rand, func(r *rand.Rand) { signedBlocksWindow = GenSignedBlocksWindow(r) })
    54  
    55  	var minSignedPerWindow math.LegacyDec
    56  	simState.AppParams.GetOrGenerate(MinSignedPerWindow, &minSignedPerWindow, simState.Rand, func(r *rand.Rand) { minSignedPerWindow = GenMinSignedPerWindow(r) })
    57  
    58  	var downtimeJailDuration time.Duration
    59  	simState.AppParams.GetOrGenerate(DowntimeJailDuration, &downtimeJailDuration, simState.Rand, func(r *rand.Rand) { downtimeJailDuration = GenDowntimeJailDuration(r) })
    60  
    61  	var slashFractionDoubleSign math.LegacyDec
    62  	simState.AppParams.GetOrGenerate(SlashFractionDoubleSign, &slashFractionDoubleSign, simState.Rand, func(r *rand.Rand) { slashFractionDoubleSign = GenSlashFractionDoubleSign(r) })
    63  
    64  	var slashFractionDowntime math.LegacyDec
    65  	simState.AppParams.GetOrGenerate(SlashFractionDowntime, &slashFractionDowntime, simState.Rand, func(r *rand.Rand) { slashFractionDowntime = GenSlashFractionDowntime(r) })
    66  
    67  	params := types.NewParams(
    68  		signedBlocksWindow, minSignedPerWindow, downtimeJailDuration,
    69  		slashFractionDoubleSign, slashFractionDowntime,
    70  	)
    71  
    72  	slashingGenesis := types.NewGenesisState(params, []types.SigningInfo{}, []types.ValidatorMissedBlocks{})
    73  
    74  	bz, err := json.MarshalIndent(&slashingGenesis, "", " ")
    75  	if err != nil {
    76  		panic(err)
    77  	}
    78  	fmt.Printf("Selected randomly generated slashing parameters:\n%s\n", bz)
    79  	simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(slashingGenesis)
    80  }