From c4e3578087ae89f899ff25f94df4b075a552eb00 Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Fri, 2 Aug 2019 17:28:50 +0200 Subject: [PATCH] Merge PR #4791: resume simulations at a given height --- .../improvements/simulation/_4490-add-init-heigh | 1 + simapp/sim_test.go | 12 +++++++----- simapp/utils.go | 2 ++ x/simulation/simulate.go | 8 +++++--- 4 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 .pending/improvements/simulation/_4490-add-init-heigh diff --git a/.pending/improvements/simulation/_4490-add-init-heigh b/.pending/improvements/simulation/_4490-add-init-heigh new file mode 100644 index 000000000..df6b069c5 --- /dev/null +++ b/.pending/improvements/simulation/_4490-add-init-heigh @@ -0,0 +1 @@ +# 4490 add `InitialBlockHeight` flag to resume a simulation from a given block \ No newline at end of file diff --git a/simapp/sim_test.go b/simapp/sim_test.go index a8b7b4965..fd4c98334 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -45,7 +45,8 @@ func init() { flag.StringVar(&exportStatePath, "ExportStatePath", "", "custom file path to save the exported app state JSON") flag.StringVar(&exportStatsPath, "ExportStatsPath", "", "custom file path to save the exported simulation statistics JSON") flag.Int64Var(&seed, "Seed", 42, "simulation random seed") - flag.IntVar(&numBlocks, "NumBlocks", 500, "number of blocks") + flag.IntVar(&initialBlockHeight, "InitialBlockHeight", 1, "initial block to start the simulation") + flag.IntVar(&numBlocks, "NumBlocks", 500, "number of new blocks to simulate from the initial block height") flag.IntVar(&blockSize, "BlockSize", 200, "operations per block") flag.BoolVar(&enabled, "Enabled", false, "enable the simulation") flag.BoolVar(&verbose, "Verbose", false, "verbose log output") @@ -58,16 +59,17 @@ func init() { } // helper function for populating input for SimulateFromSeed +// TODO: clean up this function along with the simulation refactor func getSimulateFromSeedInput(tb testing.TB, w io.Writer, app *SimApp) ( testing.TB, io.Writer, *baseapp.BaseApp, simulation.AppStateFn, int64, - simulation.WeightedOperations, sdk.Invariants, int, int, int, string, + simulation.WeightedOperations, sdk.Invariants, int, int, int, int, string, bool, bool, bool, bool, bool, map[string]bool) { exportParams := exportParamsPath != "" return tb, w, app.BaseApp, appStateFn, seed, testAndRunTxs(app), invariants(app), - numBlocks, exportParamsHeight, blockSize, + initialBlockHeight, numBlocks, exportParamsHeight, blockSize, exportStatsPath, exportParams, commit, lean, onOperation, allInvariants, app.ModuleAccountAddrs() } @@ -715,7 +717,7 @@ func TestAppStateDeterminism(t *testing.T) { simulation.SimulateFromSeed( t, os.Stdout, app.BaseApp, appStateFn, seed, testAndRunTxs(app), []sdk.Invariant{}, - 50, 100, 0, "", + 1, 50, 100, 0, "", false, true, false, false, false, app.ModuleAccountAddrs(), ) appHash := app.LastCommitID().Hash @@ -743,7 +745,7 @@ func BenchmarkInvariants(b *testing.B) { // 2. Run parameterized simulation (w/o invariants) _, params, simErr := simulation.SimulateFromSeed( b, ioutil.Discard, app.BaseApp, appStateFn, seed, testAndRunTxs(app), - []sdk.Invariant{}, numBlocks, exportParamsHeight, blockSize, + []sdk.Invariant{}, initialBlockHeight, numBlocks, exportParamsHeight, blockSize, exportStatsPath, exportParams, commit, lean, onOperation, false, app.ModuleAccountAddrs(), ) diff --git a/simapp/utils.go b/simapp/utils.go index a867615d3..b4f14db26 100644 --- a/simapp/utils.go +++ b/simapp/utils.go @@ -33,6 +33,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/supply" ) +// List of available flags for the simulator var ( genesisFile string paramsFile string @@ -41,6 +42,7 @@ var ( exportStatePath string exportStatsPath string seed int64 + initialBlockHeight int numBlocks int blockSize int enabled bool diff --git a/x/simulation/simulate.go b/x/simulation/simulate.go index 59c24a395..ca2b507c1 100644 --- a/x/simulation/simulate.go +++ b/x/simulation/simulate.go @@ -47,7 +47,7 @@ func SimulateFromSeed( tb testing.TB, w io.Writer, app *baseapp.BaseApp, appStateFn AppStateFn, seed int64, ops WeightedOperations, invariants sdk.Invariants, - numBlocks, exportParamsHeight, blockSize int, + initialHeight, numBlocks, exportParamsHeight, blockSize int, exportStatsPath string, exportParams, commit, lean, onOperation, allInvariants bool, blackListedAccs map[string]bool, @@ -139,10 +139,12 @@ func SimulateFromSeed( } // set exported params to the initial state - exportedParams = params + if exportParams && exportParamsHeight == 0 { + exportedParams = params + } // TODO: split up the contents of this for loop into new functions - for height := 1; height <= numBlocks && !stopEarly; height++ { + for height := initialHeight; height < numBlocks+initialHeight && !stopEarly; height++ { // Log the header time for future lookup pastTimes = append(pastTimes, header.Time)