Merge PR #4750: Allow the use of a custom genesis time in simulations

This commit is contained in:
Alexander Bezobchuk
2019-07-19 14:46:06 -07:00
committed by Jack Zampolin
parent 8af2230ee4
commit 3c822547cf
3 changed files with 24 additions and 16 deletions
+10 -3
View File
@@ -46,6 +46,7 @@ func init() {
flag.IntVar(&period, "Period", 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
@@ -63,11 +64,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")
@@ -90,7 +97,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
+1
View File
@@ -52,6 +52,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.
+13 -13
View File
@@ -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
@@ -59,23 +60,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, params, 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 {