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

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