github.com/Finschia/finschia-sdk@v0.48.1/x/slashing/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/slashing/types" 15 ) 16 17 // Simulation parameter constants 18 const ( 19 SignedBlocksWindow = "signed_blocks_window" 20 MinSignedPerWindow = "min_signed_per_window" 21 DowntimeJailDuration = "downtime_jail_duration" 22 SlashFractionDoubleSign = "slash_fraction_double_sign" 23 SlashFractionDowntime = "slash_fraction_downtime" 24 ) 25 26 // GenSignedBlocksWindow randomized SignedBlocksWindow 27 func GenSignedBlocksWindow(r *rand.Rand) int64 { 28 return int64(simulation.RandIntBetween(r, 10, 1000)) 29 } 30 31 // GenMinSignedPerWindow randomized MinSignedPerWindow 32 func GenMinSignedPerWindow(r *rand.Rand) sdk.Dec { 33 return sdk.NewDecWithPrec(int64(r.Intn(10)), 1) 34 } 35 36 // GenDowntimeJailDuration randomized DowntimeJailDuration 37 func GenDowntimeJailDuration(r *rand.Rand) time.Duration { 38 return time.Duration(simulation.RandIntBetween(r, 60, 60*60*24)) * time.Second 39 } 40 41 // GenSlashFractionDoubleSign randomized SlashFractionDoubleSign 42 func GenSlashFractionDoubleSign(r *rand.Rand) sdk.Dec { 43 return sdk.NewDec(1).Quo(sdk.NewDec(int64(r.Intn(50) + 1))) 44 } 45 46 // GenSlashFractionDowntime randomized SlashFractionDowntime 47 func GenSlashFractionDowntime(r *rand.Rand) sdk.Dec { 48 return sdk.NewDec(1).Quo(sdk.NewDec(int64(r.Intn(200) + 1))) 49 } 50 51 // RandomizedGenState generates a random GenesisState for slashing 52 func RandomizedGenState(simState *module.SimulationState) { 53 var signedBlocksWindow int64 54 simState.AppParams.GetOrGenerate( 55 simState.Cdc, SignedBlocksWindow, &signedBlocksWindow, simState.Rand, 56 func(r *rand.Rand) { signedBlocksWindow = GenSignedBlocksWindow(r) }, 57 ) 58 59 var minSignedPerWindow sdk.Dec 60 simState.AppParams.GetOrGenerate( 61 simState.Cdc, MinSignedPerWindow, &minSignedPerWindow, simState.Rand, 62 func(r *rand.Rand) { minSignedPerWindow = GenMinSignedPerWindow(r) }, 63 ) 64 65 var downtimeJailDuration time.Duration 66 simState.AppParams.GetOrGenerate( 67 simState.Cdc, DowntimeJailDuration, &downtimeJailDuration, simState.Rand, 68 func(r *rand.Rand) { downtimeJailDuration = GenDowntimeJailDuration(r) }, 69 ) 70 71 var slashFractionDoubleSign sdk.Dec 72 simState.AppParams.GetOrGenerate( 73 simState.Cdc, SlashFractionDoubleSign, &slashFractionDoubleSign, simState.Rand, 74 func(r *rand.Rand) { slashFractionDoubleSign = GenSlashFractionDoubleSign(r) }, 75 ) 76 77 var slashFractionDowntime sdk.Dec 78 simState.AppParams.GetOrGenerate( 79 simState.Cdc, SlashFractionDowntime, &slashFractionDowntime, simState.Rand, 80 func(r *rand.Rand) { slashFractionDowntime = GenSlashFractionDowntime(r) }, 81 ) 82 83 params := types.NewParams( 84 signedBlocksWindow, minSignedPerWindow, downtimeJailDuration, 85 slashFractionDoubleSign, slashFractionDowntime, 86 ) 87 88 slashingGenesis := types.NewGenesisState(params, []types.SigningInfo{}, []types.ValidatorMissedBlocks{}) 89 90 bz, err := json.MarshalIndent(&slashingGenesis, "", " ") 91 if err != nil { 92 panic(err) 93 } 94 fmt.Printf("Selected randomly generated slashing parameters:\n%s\n", bz) 95 simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(slashingGenesis) 96 }