From df6f2d6e25491ec1f94dbb17600d5a3f316e8f15 Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Mon, 22 Apr 2019 10:57:58 -0400 Subject: [PATCH] Merge PR #4140: Fix Failed Simulation Seeds --- cmd/gaia/app/sim_test.go | 32 ++++++++++++++++---------------- x/simulation/params.go | 2 +- x/simulation/rand_util.go | 5 +++++ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/cmd/gaia/app/sim_test.go b/cmd/gaia/app/sim_test.go index 5872b1aa621c..f8ebb62c0a71 100644 --- a/cmd/gaia/app/sim_test.go +++ b/cmd/gaia/app/sim_test.go @@ -120,7 +120,7 @@ func appStateRandomizedFn(r *rand.Rand, accs []simulation.Account, genesisTimest if int64(i) > numInitiallyBonded && r.Intn(100) < 50 { var ( vacc auth.VestingAccount - endTime int + endTime int64 ) startTime := genesisTimestamp.Unix() @@ -128,15 +128,19 @@ func appStateRandomizedFn(r *rand.Rand, accs []simulation.Account, genesisTimest // Allow for some vesting accounts to vest very quickly while others very // slowly. if r.Intn(100) < 50 { - endTime = randIntBetween(r, int(startTime), int(startTime+(60*60*24*30))) + endTime = int64(simulation.RandIntBetween(r, int(startTime), int(startTime+(60*60*24*30)))) } else { - endTime = randIntBetween(r, int(startTime), int(startTime+(60*60*12))) + endTime = int64(simulation.RandIntBetween(r, int(startTime), int(startTime+(60*60*12)))) + } + + if startTime == endTime { + endTime += 1 } if r.Intn(100) < 50 { - vacc = auth.NewContinuousVestingAccount(&bacc, startTime, int64(endTime)) + vacc = auth.NewContinuousVestingAccount(&bacc, startTime, endTime) } else { - vacc = auth.NewDelayedVestingAccount(&bacc, int64(endTime)) + vacc = auth.NewDelayedVestingAccount(&bacc, endTime) } gacc = NewGenesisAccountI(vacc) @@ -149,11 +153,11 @@ func appStateRandomizedFn(r *rand.Rand, accs []simulation.Account, genesisTimest authGenesis := auth.GenesisState{ Params: auth.Params{ - MaxMemoCharacters: uint64(randIntBetween(r, 100, 200)), + MaxMemoCharacters: uint64(simulation.RandIntBetween(r, 100, 200)), TxSigLimit: uint64(r.Intn(7) + 1), - TxSizeCostPerByte: uint64(randIntBetween(r, 5, 15)), - SigVerifyCostED25519: uint64(randIntBetween(r, 500, 1000)), - SigVerifyCostSecp256k1: uint64(randIntBetween(r, 500, 1000)), + TxSizeCostPerByte: uint64(simulation.RandIntBetween(r, 5, 15)), + SigVerifyCostED25519: uint64(simulation.RandIntBetween(r, 500, 1000)), + SigVerifyCostSecp256k1: uint64(simulation.RandIntBetween(r, 500, 1000)), }, } fmt.Printf("Selected randomly generated auth parameters:\n\t%+v\n", authGenesis) @@ -183,7 +187,7 @@ func appStateRandomizedFn(r *rand.Rand, accs []simulation.Account, genesisTimest stakingGenesis := staking.GenesisState{ Pool: staking.InitialPool(), Params: staking.Params{ - UnbondingTime: time.Duration(randIntBetween(r, 60, 60*60*24*3*2)) * time.Second, + UnbondingTime: time.Duration(simulation.RandIntBetween(r, 60, 60*60*24*3*2)) * time.Second, MaxValidators: uint16(r.Intn(250) + 1), BondDenom: sdk.DefaultBondDenom, }, @@ -193,9 +197,9 @@ func appStateRandomizedFn(r *rand.Rand, accs []simulation.Account, genesisTimest slashingGenesis := slashing.GenesisState{ Params: slashing.Params{ MaxEvidenceAge: stakingGenesis.Params.UnbondingTime, - SignedBlocksWindow: int64(randIntBetween(r, 10, 1000)), + SignedBlocksWindow: int64(simulation.RandIntBetween(r, 10, 1000)), MinSignedPerWindow: sdk.NewDecWithPrec(int64(r.Intn(10)), 1), - DowntimeJailDuration: time.Duration(randIntBetween(r, 60, 60*60*24)) * time.Second, + DowntimeJailDuration: time.Duration(simulation.RandIntBetween(r, 60, 60*60*24)) * time.Second, SlashFractionDoubleSign: sdk.NewDec(1).Quo(sdk.NewDec(int64(r.Intn(50) + 1))), SlashFractionDowntime: sdk.NewDec(1).Quo(sdk.NewDec(int64(r.Intn(200) + 1))), }, @@ -270,10 +274,6 @@ func appStateFn(r *rand.Rand, accs []simulation.Account, genesisTimestamp time.T return appStateRandomizedFn(r, accs, genesisTimestamp) } -func randIntBetween(r *rand.Rand, min, max int) int { - return r.Intn(max-min) + min -} - func testAndRunTxs(app *GaiaApp) []simulation.WeightedOperation { return []simulation.WeightedOperation{ {5, authsim.SimulateDeductFee(app.accountKeeper, app.feeCollectionKeeper)}, diff --git a/x/simulation/params.go b/x/simulation/params.go index 8499e6c1189b..6202ec6fef37 100644 --- a/x/simulation/params.go +++ b/x/simulation/params.go @@ -60,7 +60,7 @@ func DefaultParams() Params { func RandomParams(r *rand.Rand) Params { return Params{ PastEvidenceFraction: r.Float64(), - NumKeys: r.Intn(250), + NumKeys: RandIntBetween(r, 2, 250), EvidenceFraction: r.Float64(), InitialLivenessWeightings: []int{r.Intn(80), r.Intn(10), r.Intn(10)}, LivenessTransitionMatrix: defaultLivenessTransitionMatrix, diff --git a/x/simulation/rand_util.go b/x/simulation/rand_util.go index a9b32ca48dd4..9677f2a612e6 100644 --- a/x/simulation/rand_util.go +++ b/x/simulation/rand_util.go @@ -72,6 +72,11 @@ func RandTimestamp(r *rand.Rand) time.Time { return time.Unix(unixTime, 0) } +// RandIntBetween returns a random int between two numbers inclusively. +func RandIntBetween(r *rand.Rand, min, max int) int { + return r.Intn(max-min) + min +} + // Derive a new rand deterministically from a rand. // Unlike rand.New(rand.NewSource(seed)), the result is "more random" // depending on the source and state of r.