Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulation: Allow the use of a custom genesis time #4750

Merged
merged 2 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions simapp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 13 additions & 13 deletions x/simulation/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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 {
Expand Down