From e2df5a11877f4c36144ae41dd8a7e7599fb09984 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 19 Jul 2019 13:48:46 -0400 Subject: [PATCH] Allow the use of a custom genesis time --- simapp/sim_test.go | 13 ++++++++++--- simapp/utils.go | 1 + x/simulation/simulate.go | 26 +++++++++++++------------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/simapp/sim_test.go b/simapp/sim_test.go index 59a9b6d4ad41..d4a6ce3bcbd6 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -43,6 +43,7 @@ func init() { flag.IntVar(&period, "SimulationPeriod", 1, "run slow invariants only once every period assertions") flag.BoolVar(&onOperation, "SimulateEveryOperation", false, "run slow invariants every operation") flag.BoolVar(&allInvariants, "PrintAllInvariants", false, "print all invariants if a broken invariant is found") + flag.Int64Var(&genesisTime, "GenesisTime", 0, "override genesis UNIX time instead of using a random UNIX time") } // helper function for populating input for SimulateFromSeed @@ -56,11 +57,17 @@ func getSimulateFromSeedInput(tb testing.TB, w io.Writer, app *SimApp) ( } func appStateFn( - r *rand.Rand, accs []simulation.Account, genesisTimestamp time.Time, -) (appState json.RawMessage, simAccs []simulation.Account, chainID string) { + r *rand.Rand, accs []simulation.Account, +) (appState json.RawMessage, simAccs []simulation.Account, chainID string, genesisTimestamp time.Time) { cdc := MakeCodec() + if genesisTime == 0 { + genesisTimestamp = simulation.RandTimestamp(r) + } else { + genesisTimestamp = time.Unix(genesisTime, 0) + } + switch { case paramsFile != "" && genesisFile != "": panic("cannot provide both a genesis file and a params file") @@ -83,7 +90,7 @@ func appStateFn( appState, simAccs, chainID = appStateRandomizedFn(r, accs, genesisTimestamp, appParams) } - return appState, simAccs, chainID + return appState, simAccs, chainID, genesisTimestamp } // TODO refactor out random initialization code to the modules diff --git a/simapp/utils.go b/simapp/utils.go index 0624b4e9fd13..f2e2a0a204a4 100644 --- a/simapp/utils.go +++ b/simapp/utils.go @@ -49,6 +49,7 @@ var ( period int onOperation bool // TODO Remove in favor of binary search for invariant violation allInvariants bool + genesisTime int64 ) // NewSimAppUNSAFE is used for debugging purposes only. diff --git a/x/simulation/simulate.go b/x/simulation/simulate.go index 08920e295348..16257b864381 100644 --- a/x/simulation/simulate.go +++ b/x/simulation/simulate.go @@ -19,15 +19,16 @@ import ( ) // AppStateFn returns the app state json bytes, the genesis accounts, and the chain identifier -type AppStateFn func(r *rand.Rand, accs []Account, genesisTimestamp time.Time) (appState json.RawMessage, accounts []Account, chainId string) +type AppStateFn func( + r *rand.Rand, accs []Account, +) (appState json.RawMessage, accounts []Account, chainId string, genesisTimestamp time.Time) // initialize the chain for the simulation func initChain( - r *rand.Rand, params Params, accounts []Account, - app *baseapp.BaseApp, appStateFn AppStateFn, genesisTimestamp time.Time, -) (mockValidators, []Account) { + r *rand.Rand, params Params, accounts []Account, app *baseapp.BaseApp, appStateFn AppStateFn, +) (mockValidators, time.Time, []Account) { - appState, accounts, chainID := appStateFn(r, accounts, genesisTimestamp) + appState, accounts, chainID, genesisTimestamp := appStateFn(r, accounts) req := abci.RequestInitChain{ AppStateBytes: appState, @@ -36,7 +37,7 @@ func initChain( res := app.InitChain(req) validators := newMockValidators(r, res.Validators, params) - return validators, accounts + return validators, genesisTimestamp, accounts } // SimulateFromSeed tests an application by running the provided @@ -58,23 +59,22 @@ func SimulateFromSeed( params := RandomParams(r) fmt.Fprintf(w, "Randomized simulation params: \n%s\n", mustMarshalJSONIndent(params)) - genesisTimestamp := RandTimestamp(r) - fmt.Printf( - "Starting the simulation from time %v, unixtime %v\n", - genesisTimestamp.UTC().Format(time.UnixDate), genesisTimestamp.Unix(), - ) - timeDiff := maxTimePerBlock - minTimePerBlock accs := RandomAccounts(r, params.NumKeys) eventStats := newEventStats() // Second variable to keep pending validator set (delayed one block since // TM 0.24) Initially this is the same as the initial validator set - validators, accs := initChain(r, params, accs, app, appStateFn, genesisTimestamp) + validators, genesisTimestamp, accs := initChain(r, params, accs, app, appStateFn) if len(accs) == 0 { return true, fmt.Errorf("must have greater than zero genesis accounts") } + fmt.Printf( + "Starting the simulation from time %v (unixtime %v)\n", + genesisTimestamp.UTC().Format(time.UnixDate), genesisTimestamp.Unix(), + ) + // remove module account address if they exist in accs var tmpAccs []Account for _, acc := range accs {