From c61b1aa591da31a1d2943444edd70637c5ac5c74 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 18 Jul 2018 01:27:51 +0200 Subject: [PATCH] Event stats --- Makefile | 4 +++- x/mock/simulation/random_simulate_blocks.go | 10 +++++++++- x/mock/simulation/types.go | 2 +- x/mock/simulation/util.go | 6 ++++++ x/stake/simulation/msgs.go | 21 ++++++++++++++------- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 645973472..830aed42e 100644 --- a/Makefile +++ b/Makefile @@ -114,8 +114,10 @@ test_race: @go test -race $(PACKAGES_NOCLITEST) test_sim: + @echo "Running individual module simulations..." + @go test $(PACKAGES_SIMTEST) -v + @echo "Running full Gaia simulation..." @ENABLE_GAIA_SIMULATION=1 go test ./cmd/gaia/app -run TestFullGaiaSimulation -v - @go test $(PACKAGES_SIMTEST) test_cover: @bash tests/test_cover.sh diff --git a/x/mock/simulation/random_simulate_blocks.go b/x/mock/simulation/random_simulate_blocks.go index eca9757c5..e952ce07f 100644 --- a/x/mock/simulation/random_simulate_blocks.go +++ b/x/mock/simulation/random_simulate_blocks.go @@ -33,6 +33,12 @@ func SimulateFromSeed( keys, addrs := mock.GeneratePrivKeyAddressPairs(numKeys) r := rand.New(rand.NewSource(seed)) + // Setup event stats + events := make(map[string]uint) + event := func(what string) { + events[what] += 1 + } + app.InitChain(abci.RequestInitChain{AppStateBytes: appStateFn(r, addrs)}) for i := 0; i < len(setups); i++ { setups[i](r, keys) @@ -53,7 +59,7 @@ func SimulateFromSeed( // TODO: Add modes to simulate "no load", "medium load", and // "high load" blocks. for j := 0; j < blockSize; j++ { - logUpdate, err := ops[r.Intn(len(ops))](t, r, app, ctx, keys, log) + logUpdate, err := ops[r.Intn(len(ops))](t, r, app, ctx, keys, log, event) log += "\n" + logUpdate require.Nil(t, err, log) @@ -63,6 +69,8 @@ func SimulateFromSeed( app.EndBlock(abci.RequestEndBlock{}) header.Height++ } + + DisplayEvents(events) } // AssertAllInvariants asserts a list of provided invariants against application state diff --git a/x/mock/simulation/types.go b/x/mock/simulation/types.go index 41736ce2c..6e1d9f198 100644 --- a/x/mock/simulation/types.go +++ b/x/mock/simulation/types.go @@ -15,7 +15,7 @@ type ( // about what this fuzzed tx actually did, for ease of debugging. TestAndRunTx func( t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - privKeys []crypto.PrivKey, log string, + privKeys []crypto.PrivKey, log string, event func(string), ) (action string, err sdk.Error) // RandSetup performs the random setup the mock module needs. diff --git a/x/mock/simulation/util.go b/x/mock/simulation/util.go index 97307dc3e..c6f8ed54b 100644 --- a/x/mock/simulation/util.go +++ b/x/mock/simulation/util.go @@ -1,6 +1,7 @@ package simulation import ( + "fmt" "math/rand" ) @@ -29,3 +30,8 @@ func RandStringOfLength(r *rand.Rand, n int) string { } return string(b) } + +func DisplayEvents(events map[string]uint) { + // TODO + fmt.Printf("Events: %v\n", events) +} diff --git a/x/stake/simulation/msgs.go b/x/stake/simulation/msgs.go index 2c7bf80f4..c3060190f 100644 --- a/x/stake/simulation/msgs.go +++ b/x/stake/simulation/msgs.go @@ -19,7 +19,7 @@ import ( // SimulateMsgCreateValidator func SimulateMsgCreateValidator(m auth.AccountMapper, k stake.Keeper) simulation.TestAndRunTx { - return func(t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, keys []crypto.PrivKey, log string) (action string, err sdk.Error) { + return func(t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, keys []crypto.PrivKey, log string, event func(string)) (action string, err sdk.Error) { denom := k.GetParams(ctx).BondDenom description := stake.Description{ Moniker: simulation.RandStringOfLength(r, 10), @@ -47,6 +47,7 @@ func SimulateMsgCreateValidator(m auth.AccountMapper, k stake.Keeper) simulation if result.IsOK() { write() } + event(fmt.Sprintf("stake/MsgCreateValidator/%v", result.IsOK())) // require.True(t, result.IsOK(), "expected OK result but instead got %v", result) action = fmt.Sprintf("TestMsgCreateValidator: %s", msg.GetSignBytes()) return action, nil @@ -55,7 +56,7 @@ func SimulateMsgCreateValidator(m auth.AccountMapper, k stake.Keeper) simulation // SimulateMsgEditValidator func SimulateMsgEditValidator(k stake.Keeper) simulation.TestAndRunTx { - return func(t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, keys []crypto.PrivKey, log string) (action string, err sdk.Error) { + return func(t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, keys []crypto.PrivKey, log string, event func(string)) (action string, err sdk.Error) { description := stake.Description{ Moniker: simulation.RandStringOfLength(r, 10), Identity: simulation.RandStringOfLength(r, 10), @@ -75,6 +76,7 @@ func SimulateMsgEditValidator(k stake.Keeper) simulation.TestAndRunTx { if result.IsOK() { write() } + event(fmt.Sprintf("stake/MsgEditValidator/%v", result.IsOK())) action = fmt.Sprintf("TestMsgEditValidator: %s", msg.GetSignBytes()) return action, nil } @@ -82,7 +84,7 @@ func SimulateMsgEditValidator(k stake.Keeper) simulation.TestAndRunTx { // SimulateMsgDelegate func SimulateMsgDelegate(m auth.AccountMapper, k stake.Keeper) simulation.TestAndRunTx { - return func(t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, keys []crypto.PrivKey, log string) (action string, err sdk.Error) { + return func(t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, keys []crypto.PrivKey, log string, event func(string)) (action string, err sdk.Error) { denom := k.GetParams(ctx).BondDenom validatorKey := keys[r.Intn(len(keys))] validatorAddress := sdk.AccAddress(validatorKey.PubKey().Address()) @@ -106,6 +108,7 @@ func SimulateMsgDelegate(m auth.AccountMapper, k stake.Keeper) simulation.TestAn if result.IsOK() { write() } + event(fmt.Sprintf("stake/MsgDelegate/%v", result.IsOK())) action = fmt.Sprintf("TestMsgDelegate: %s", msg.GetSignBytes()) return action, nil } @@ -113,7 +116,7 @@ func SimulateMsgDelegate(m auth.AccountMapper, k stake.Keeper) simulation.TestAn // SimulateMsgBeginUnbonding func SimulateMsgBeginUnbonding(m auth.AccountMapper, k stake.Keeper) simulation.TestAndRunTx { - return func(t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, keys []crypto.PrivKey, log string) (action string, err sdk.Error) { + return func(t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, keys []crypto.PrivKey, log string, event func(string)) (action string, err sdk.Error) { denom := k.GetParams(ctx).BondDenom validatorKey := keys[r.Intn(len(keys))] validatorAddress := sdk.AccAddress(validatorKey.PubKey().Address()) @@ -137,6 +140,7 @@ func SimulateMsgBeginUnbonding(m auth.AccountMapper, k stake.Keeper) simulation. if result.IsOK() { write() } + event(fmt.Sprintf("stake/MsgBeginUnbonding/%v", result.IsOK())) action = fmt.Sprintf("TestMsgBeginUnbonding: %s", msg.GetSignBytes()) return action, nil } @@ -144,7 +148,7 @@ func SimulateMsgBeginUnbonding(m auth.AccountMapper, k stake.Keeper) simulation. // SimulateMsgCompleteUnbonding func SimulateMsgCompleteUnbonding(k stake.Keeper) simulation.TestAndRunTx { - return func(t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, keys []crypto.PrivKey, log string) (action string, err sdk.Error) { + return func(t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, keys []crypto.PrivKey, log string, event func(string)) (action string, err sdk.Error) { validatorKey := keys[r.Intn(len(keys))] validatorAddress := sdk.AccAddress(validatorKey.PubKey().Address()) delegatorKey := keys[r.Intn(len(keys))] @@ -159,6 +163,7 @@ func SimulateMsgCompleteUnbonding(k stake.Keeper) simulation.TestAndRunTx { if result.IsOK() { write() } + event(fmt.Sprintf("stake/MsgCompleteUnbonding/%v", result.IsOK())) action = fmt.Sprintf("TestMsgCompleteUnbonding with %s", msg.GetSignBytes()) return action, nil } @@ -166,7 +171,7 @@ func SimulateMsgCompleteUnbonding(k stake.Keeper) simulation.TestAndRunTx { // SimulateMsgBeginRedelegate func SimulateMsgBeginRedelegate(m auth.AccountMapper, k stake.Keeper) simulation.TestAndRunTx { - return func(t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, keys []crypto.PrivKey, log string) (action string, err sdk.Error) { + return func(t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, keys []crypto.PrivKey, log string, event func(string)) (action string, err sdk.Error) { denom := k.GetParams(ctx).BondDenom sourceValidatorKey := keys[r.Intn(len(keys))] sourceValidatorAddress := sdk.AccAddress(sourceValidatorKey.PubKey().Address()) @@ -194,6 +199,7 @@ func SimulateMsgBeginRedelegate(m auth.AccountMapper, k stake.Keeper) simulation if result.IsOK() { write() } + event(fmt.Sprintf("stake/MsgBeginRedelegate/%v", result.IsOK())) action = fmt.Sprintf("TestMsgBeginRedelegate: %s", msg.GetSignBytes()) return action, nil } @@ -201,7 +207,7 @@ func SimulateMsgBeginRedelegate(m auth.AccountMapper, k stake.Keeper) simulation // SimulateMsgCompleteRedelegate func SimulateMsgCompleteRedelegate(k stake.Keeper) simulation.TestAndRunTx { - return func(t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, keys []crypto.PrivKey, log string) (action string, err sdk.Error) { + return func(t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, keys []crypto.PrivKey, log string, event func(string)) (action string, err sdk.Error) { validatorSrcKey := keys[r.Intn(len(keys))] validatorSrcAddress := sdk.AccAddress(validatorSrcKey.PubKey().Address()) validatorDstKey := keys[r.Intn(len(keys))] @@ -219,6 +225,7 @@ func SimulateMsgCompleteRedelegate(k stake.Keeper) simulation.TestAndRunTx { if result.IsOK() { write() } + event(fmt.Sprintf("stake/MsgCompleteRedelegate/%v", result.IsOK())) action = fmt.Sprintf("TestMsgCompleteRedelegate with %s", msg.GetSignBytes()) return action, nil }