From dae544be28445fda2d132fcdbd73e699af391ab6 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 26 Feb 2019 02:30:00 -0500 Subject: [PATCH 01/19] simulation saved to ~/.gaiad/simulations/ --- cmd/gaia/app/sim_test.go | 3 ++- x/mock/simulation/util.go | 14 +++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/gaia/app/sim_test.go b/cmd/gaia/app/sim_test.go index 2cba3545804c..3af1871a5fdb 100644 --- a/cmd/gaia/app/sim_test.go +++ b/cmd/gaia/app/sim_test.go @@ -446,7 +446,8 @@ func TestGaiaImportExport(t *testing.T) { storeKeysPrefixes := []StoreKeysPrefixes{ {app.keyMain, newApp.keyMain, [][]byte{}}, {app.keyAccount, newApp.keyAccount, [][]byte{}}, - {app.keyStaking, newApp.keyStaking, [][]byte{staking.UnbondingQueueKey, staking.RedelegationQueueKey, staking.ValidatorQueueKey}}, // ordering may change but it doesn't matter + {app.keyStaking, newApp.keyStaking, [][]byte{staking.UnbondingQueueKey, + staking.RedelegationQueueKey, staking.ValidatorQueueKey}}, // ordering may change but it doesn't matter {app.keySlashing, newApp.keySlashing, [][]byte{}}, {app.keyMint, newApp.keyMint, [][]byte{}}, {app.keyDistr, newApp.keyDistr, [][]byte{}}, diff --git a/x/mock/simulation/util.go b/x/mock/simulation/util.go index 962dbfe43a4f..96cade8f6670 100644 --- a/x/mock/simulation/util.go +++ b/x/mock/simulation/util.go @@ -4,6 +4,7 @@ import ( "fmt" "math/rand" "os" + "path" "strings" "testing" "time" @@ -72,11 +73,14 @@ func logPrinter(testingmode bool, logs []*strings.Builder) func() { var f *os.File if numLoggers > 10 { - fileName := fmt.Sprintf("simulation_log_%s.txt", - time.Now().Format("2006-01-02 15:04:05")) - fmt.Printf("Too many logs to display, instead writing to %s\n", - fileName) - f, _ = os.Create(fileName) + fileName := fmt.Sprintf("%s.txt", time.Now().Format("2006-01-02_15:04:05")) + + folderPath := os.ExpandEnv("$HOME/.gaiad/simulations") + filePath := path.Join(folderPath, fileName) + fmt.Printf("Too many logs to display, instead writing to %s\n", filePath) + + os.MkdirAll(folderPath, os.ModePerm) + f, _ = os.Create(filePath) } for i := 0; i < numLoggers; i++ { From ba448945f675fe6d2e96727db3bcf72c6b911114 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 26 Feb 2019 04:40:05 -0500 Subject: [PATCH 02/19] lean simulation output int --- cmd/gaia/app/sim_test.go | 75 +++++++++++----------------- x/auth/simulation/fake.go | 12 ++--- x/bank/simulation/msgs.go | 81 ++++++++++++++++++------------- x/distribution/simulation/msgs.go | 33 +++++++------ x/gov/simulation/msgs.go | 45 +++++++++-------- x/mock/simulation/operation.go | 2 +- x/mock/simulation/simulate.go | 36 ++++++++------ x/mock/simulation/util.go | 2 +- x/slashing/simulation/msgs.go | 6 +-- x/staking/simulation/msgs.go | 40 +++++++-------- 10 files changed, 171 insertions(+), 161 deletions(-) diff --git a/cmd/gaia/app/sim_test.go b/cmd/gaia/app/sim_test.go index 3af1871a5fdb..7a85973f52ac 100644 --- a/cmd/gaia/app/sim_test.go +++ b/cmd/gaia/app/sim_test.go @@ -43,19 +43,30 @@ var ( blockSize int enabled bool verbose bool + lean bool commit bool period int ) func init() { - flag.StringVar(&genesisFile, "SimulationGenesis", "", "Custom simulation genesis file") - flag.Int64Var(&seed, "SimulationSeed", 42, "Simulation random seed") - flag.IntVar(&numBlocks, "SimulationNumBlocks", 500, "Number of blocks") - flag.IntVar(&blockSize, "SimulationBlockSize", 200, "Operations per block") - flag.BoolVar(&enabled, "SimulationEnabled", false, "Enable the simulation") - flag.BoolVar(&verbose, "SimulationVerbose", false, "Verbose log output") - flag.BoolVar(&commit, "SimulationCommit", false, "Have the simulation commit") - flag.IntVar(&period, "SimulationPeriod", 1, "Run slow invariants only once every period assertions") + flag.StringVar(&genesisFile, "SimulationGenesis", "", "custom simulation genesis file") + flag.Int64Var(&seed, "SimulationSeed", 42, "simulation random seed") + flag.IntVar(&numBlocks, "SimulationNumBlocks", 500, "number of blocks") + flag.IntVar(&blockSize, "SimulationBlockSize", 200, "operations per block") + flag.BoolVar(&enabled, "SimulationEnabled", false, "enable the simulation") + flag.BoolVar(&verbose, "SimulationVerbose", false, "verbose log output") + flag.BoolVar(&lean, "SimulationLean", false, "lean simulation log output") + flag.BoolVar(&commit, "SimulationCommit", false, "have the simulation commit") + flag.IntVar(&period, "SimulationPeriod", 1, "run slow invariants only once every period assertions") +} + +// helper function for populating input for SimulateFromSeed +func getSimulateFromSeedInput(tb testing.TB, app *GaiaApp) ( + testing.TB, *baseapp.BaseApp, simulation.AppStateFn, int64, + simulation.WeightedOperations, sdk.Invariants, int, int, bool, bool) { + + return tb, app.BaseApp, appStateFn, seed, + testAndRunTxs(app), invariants(app), numBlocks, blockSize, commit, lean } func appStateFromGenesisFileFn(r *rand.Rand, accs []simulation.Account, genesisTimestamp time.Time) (json.RawMessage, []simulation.Account, string) { @@ -314,14 +325,7 @@ func BenchmarkFullGaiaSimulation(b *testing.B) { // Run randomized simulation // TODO parameterize numbers, save for a later PR - _, err := simulation.SimulateFromSeed( - b, app.BaseApp, appStateFn, seed, - testAndRunTxs(app), - invariants(app), // these shouldn't get ran - numBlocks, - blockSize, - commit, - ) + _, err := simulation.SimulateFromSeed(getSimulateFromSeedInput(b, app)) if err != nil { fmt.Println(err) b.Fail() @@ -356,14 +360,7 @@ func TestFullGaiaSimulation(t *testing.T) { require.Equal(t, "GaiaApp", app.Name()) // Run randomized simulation - _, err := simulation.SimulateFromSeed( - t, app.BaseApp, appStateFn, seed, - testAndRunTxs(app), - invariants(app), - numBlocks, - blockSize, - commit, - ) + _, err := simulation.SimulateFromSeed(getSimulateFromSeedInput(t, app)) if commit { // for memdb: // fmt.Println("Database Size", db.Stats()["database.size"]) @@ -397,14 +394,8 @@ func TestGaiaImportExport(t *testing.T) { require.Equal(t, "GaiaApp", app.Name()) // Run randomized simulation - _, err := simulation.SimulateFromSeed( - t, app.BaseApp, appStateFn, seed, - testAndRunTxs(app), - invariants(app), - numBlocks, - blockSize, - commit, - ) + _, err := simulation.SimulateFromSeed(getSimulateFromSeedInput(t, app)) + if commit { // for memdb: // fmt.Println("Database Size", db.Stats()["database.size"]) @@ -493,14 +484,8 @@ func TestGaiaSimulationAfterImport(t *testing.T) { require.Equal(t, "GaiaApp", app.Name()) // Run randomized simulation - stopEarly, err := simulation.SimulateFromSeed( - t, app.BaseApp, appStateFn, seed, - testAndRunTxs(app), - invariants(app), - numBlocks, - blockSize, - commit, - ) + stopEarly, err := simulation.SimulateFromSeed(getSimulateFromSeedInput(t, app)) + if commit { // for memdb: // fmt.Println("Database Size", db.Stats()["database.size"]) @@ -538,14 +523,7 @@ func TestGaiaSimulationAfterImport(t *testing.T) { }) // Run randomized simulation on imported app - _, err = simulation.SimulateFromSeed( - t, newApp.BaseApp, appStateFn, seed, - testAndRunTxs(newApp), - invariants(newApp), - numBlocks, - blockSize, - commit, - ) + _, err = simulation.SimulateFromSeed(getSimulateFromSeedInput(t, app)) require.Nil(t, err) } @@ -576,6 +554,7 @@ func TestAppStateDeterminism(t *testing.T) { 50, 100, true, + false, ) appHash := app.LastCommitID().Hash appHashList[j] = appHash diff --git a/x/auth/simulation/fake.go b/x/auth/simulation/fake.go index f28c27cd9285..af996a3627e3 100644 --- a/x/auth/simulation/fake.go +++ b/x/auth/simulation/fake.go @@ -16,7 +16,7 @@ import ( func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulation.Operation { return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, fOp []simulation.FutureOperation, err error) { + action string, ok bool, fOp []simulation.FutureOperation, err error) { account := simulation.RandomAcc(r, accs) stored := m.GetAccount(ctx, account.Address) @@ -24,7 +24,7 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat if len(initCoins) == 0 { event(fmt.Sprintf("auth/SimulateDeductFee/false")) - return action, nil, nil + return action, false, nil, nil } denomIndex := r.Intn(len(initCoins)) @@ -33,7 +33,7 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat amt, err := randPositiveInt(r, randCoin.Amount) if err != nil { event(fmt.Sprintf("auth/SimulateDeductFee/false")) - return action, nil, nil + return action, false, nil, nil } // Create a random fee and verify the fees are within the account's spendable @@ -42,14 +42,14 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat spendableCoins := stored.SpendableCoins(ctx.BlockHeader().Time) if _, hasNeg := spendableCoins.SafeSub(fees); hasNeg { event(fmt.Sprintf("auth/SimulateDeductFee/false")) - return action, nil, nil + return action, false, nil, nil } // get the new account balance newCoins, hasNeg := initCoins.SafeSub(fees) if hasNeg { event(fmt.Sprintf("auth/SimulateDeductFee/false")) - return action, nil, nil + return action, false, nil, nil } if err := stored.SetCoins(newCoins); err != nil { @@ -61,7 +61,7 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat event(fmt.Sprintf("auth/SimulateDeductFee/true")) action = "TestDeductFee" - return action, nil, nil + return action, false, nil, nil } } diff --git a/x/bank/simulation/msgs.go b/x/bank/simulation/msgs.go index 1e4c529a9e69..c5e8ab6f4098 100644 --- a/x/bank/simulation/msgs.go +++ b/x/bank/simulation/msgs.go @@ -20,40 +20,46 @@ import ( // accounts already exist. func SendMsg(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Operation { handler := bank.NewHandler(bk) - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (action string, fOps []simulation.FutureOperation, err error) { - fromAcc, action, msg, abort := createSendMsg(r, ctx, accs, mapper) - if abort { - return action, nil, nil + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( + action string, ok bool, fOps []simulation.FutureOperation, err error) { + + fromAcc, action, msg, ok := createSendMsg(r, ctx, accs, mapper) + if !ok { + return action, ok, nil, nil } err = sendAndVerifyMsgSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, handler) if err != nil { - return "", nil, err + return "", false, nil, err } event("bank/sendAndVerifyTxSend/ok") - return action, nil, nil + return action, ok, nil, nil } } // SendTx tests and runs a single tx send, with auth where both // accounts already exist. func SendTx(mapper auth.AccountKeeper) simulation.Operation { - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (action string, fOps []simulation.FutureOperation, err error) { - fromAcc, action, msg, abort := createSendMsg(r, ctx, accs, mapper) - if abort { - return action, nil, nil + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( + action string, ok bool, fOps []simulation.FutureOperation, err error) { + + fromAcc, action, msg, ok := createSendMsg(r, ctx, accs, mapper) + if !ok { + return action, ok, nil, nil } err = sendAndVerifyMsgSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, nil) if err != nil { - return "", nil, err + return "", false, nil, err } event("bank/sendAndVerifyTxSend/ok") - return action, nil, nil + return action, ok, nil, nil } } -func createSendMsg(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper auth.AccountKeeper) (fromAcc simulation.Account, action string, msg bank.MsgSend, abort bool) { +func createSendMsg(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper auth.AccountKeeper) ( + fromAcc simulation.Account, action string, msg bank.MsgSend, ok bool) { + fromAcc = simulation.RandomAcc(r, accs) toAcc := simulation.RandomAcc(r, accs) // Disallow sending money to yourself @@ -67,13 +73,13 @@ func createSendMsg(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, map initFromCoins := mapper.GetAccount(ctx, fromAcc.Address).SpendableCoins(ctx.BlockHeader().Time) if len(initFromCoins) == 0 { - return fromAcc, "skipping, no coins at all", msg, true + return fromAcc, "skipping, no coins at all", msg, false } denomIndex := r.Intn(len(initFromCoins)) amt, goErr := randPositiveInt(r, initFromCoins[denomIndex].Amount) if goErr != nil { - return fromAcc, "skipping bank send due to account having no coins of denomination " + initFromCoins[denomIndex].Denom, msg, true + return fromAcc, "skipping bank send due to account having no coins of denomination " + initFromCoins[denomIndex].Denom, msg, false } action = fmt.Sprintf("%s is sending %s %s to %s", @@ -85,7 +91,8 @@ func createSendMsg(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, map coins := sdk.Coins{sdk.NewCoin(initFromCoins[denomIndex].Denom, amt)} msg = bank.NewMsgSend(fromAcc.Address, toAcc.Address, coins) - return + + return fromAcc, action, msg, true } // Sends and verifies the transition of a msg send. @@ -136,18 +143,20 @@ func sendAndVerifyMsgSend(app *baseapp.BaseApp, mapper auth.AccountKeeper, msg b // SingleInputSendTx tests and runs a single msg multisend w/ auth, with one input and one output, where both // accounts already exist. func SingleInputMultiSendTx(mapper auth.AccountKeeper) simulation.Operation { - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (action string, fOps []simulation.FutureOperation, err error) { - fromAcc, action, msg, abort := createSingleInputMsgMultiSend(r, ctx, accs, mapper) - if abort { - return action, nil, nil + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( + action string, ok bool, fOps []simulation.FutureOperation, err error) { + + fromAcc, action, msg, ok := createSingleInputMsgMultiSend(r, ctx, accs, mapper) + if !ok { + return action, ok, nil, nil } err = sendAndVerifyMsgMultiSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, nil) if err != nil { - return "", nil, err + return "", false, nil, err } event("bank/sendAndVerifyTxMultiSend/ok") - return action, nil, nil + return action, ok, nil, nil } } @@ -155,22 +164,26 @@ func SingleInputMultiSendTx(mapper auth.AccountKeeper) simulation.Operation { // accounts already exist. func SingleInputMsgMultiSend(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Operation { handler := bank.NewHandler(bk) - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (action string, fOps []simulation.FutureOperation, err error) { - fromAcc, action, msg, abort := createSingleInputMsgMultiSend(r, ctx, accs, mapper) - if abort { - return action, nil, nil + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( + action string, ok bool, fOps []simulation.FutureOperation, err error) { + + fromAcc, action, msg, ok := createSingleInputMsgMultiSend(r, ctx, accs, mapper) + if !ok { + return action, ok, nil, nil } err = sendAndVerifyMsgMultiSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, handler) if err != nil { - return "", nil, err + return "", false, nil, err } event("bank/sendAndVerifyMsgMultiSend/ok") - return action, nil, nil + return action, ok, nil, nil } } -func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper auth.AccountKeeper) (fromAcc simulation.Account, action string, msg bank.MsgMultiSend, abort bool) { +func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper auth.AccountKeeper) ( + fromAcc simulation.Account, action string, msg bank.MsgMultiSend, ok bool) { + fromAcc = simulation.RandomAcc(r, accs) toAcc := simulation.RandomAcc(r, accs) // Disallow sending money to yourself @@ -184,13 +197,13 @@ func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulat initFromCoins := mapper.GetAccount(ctx, fromAcc.Address).SpendableCoins(ctx.BlockHeader().Time) if len(initFromCoins) == 0 { - return fromAcc, "skipping, no coins at all", msg, true + return fromAcc, "skipping, no coins at all", msg, false } denomIndex := r.Intn(len(initFromCoins)) amt, goErr := randPositiveInt(r, initFromCoins[denomIndex].Amount) if goErr != nil { - return fromAcc, "skipping bank send due to account having no coins of denomination " + initFromCoins[denomIndex].Denom, msg, true + return fromAcc, "skipping bank send due to account having no coins of denomination " + initFromCoins[denomIndex].Denom, msg, false } action = fmt.Sprintf("%s is sending %s %s to %s", @@ -205,12 +218,14 @@ func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulat Inputs: []bank.Input{bank.NewInput(fromAcc.Address, coins)}, Outputs: []bank.Output{bank.NewOutput(toAddr, coins)}, } - return + return fromAcc, action, msg, true } // Sends and verifies the transition of a msg multisend. This fails if there are repeated inputs or outputs // pass in handler as nil to handle txs, otherwise handle msgs -func sendAndVerifyMsgMultiSend(app *baseapp.BaseApp, mapper auth.AccountKeeper, msg bank.MsgMultiSend, ctx sdk.Context, privkeys []crypto.PrivKey, handler sdk.Handler) error { +func sendAndVerifyMsgMultiSend(app *baseapp.BaseApp, mapper auth.AccountKeeper, msg bank.MsgMultiSend, + ctx sdk.Context, privkeys []crypto.PrivKey, handler sdk.Handler) error { + initialInputAddrCoins := make([]sdk.Coins, len(msg.Inputs)) initialOutputAddrCoins := make([]sdk.Coins, len(msg.Outputs)) AccountNumbers := make([]uint64, len(msg.Inputs)) diff --git a/x/distribution/simulation/msgs.go b/x/distribution/simulation/msgs.go index 9f979d86d47c..a5f7768512ca 100644 --- a/x/distribution/simulation/msgs.go +++ b/x/distribution/simulation/msgs.go @@ -16,14 +16,14 @@ func SimulateMsgSetWithdrawAddress(m auth.AccountKeeper, k distribution.Keeper) handler := distribution.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, fOp []simulation.FutureOperation, err error) { + action string, ok bool, fOp []simulation.FutureOperation, err error) { accountOrigin := simulation.RandomAcc(r, accs) accountDestination := simulation.RandomAcc(r, accs) msg := distribution.NewMsgSetWithdrawAddress(accountOrigin.Address, accountDestination.Address) if msg.ValidateBasic() != nil { - return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() @@ -32,10 +32,11 @@ func SimulateMsgSetWithdrawAddress(m auth.AccountKeeper, k distribution.Keeper) write() } - event(fmt.Sprintf("distribution/MsgSetWithdrawAddress/%v", result.IsOK())) + ok = result.IsOK() + event(fmt.Sprintf("distribution/MsgSetWithdrawAddress/%v", ok)) - action = fmt.Sprintf("TestMsgSetWithdrawAddress: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, nil, nil + action = fmt.Sprintf("TestMsgSetWithdrawAddress: ok %v, msg %s", ok, msg.GetSignBytes()) + return action, ok, nil, nil } } @@ -44,14 +45,14 @@ func SimulateMsgWithdrawDelegatorReward(m auth.AccountKeeper, k distribution.Kee handler := distribution.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, fOp []simulation.FutureOperation, err error) { + action string, ok bool, fOp []simulation.FutureOperation, err error) { delegatorAccount := simulation.RandomAcc(r, accs) validatorAccount := simulation.RandomAcc(r, accs) msg := distribution.NewMsgWithdrawDelegatorReward(delegatorAccount.Address, sdk.ValAddress(validatorAccount.Address)) if msg.ValidateBasic() != nil { - return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() @@ -60,10 +61,11 @@ func SimulateMsgWithdrawDelegatorReward(m auth.AccountKeeper, k distribution.Kee write() } - event(fmt.Sprintf("distribution/MsgWithdrawDelegatorReward/%v", result.IsOK())) + ok = result.IsOK() + event(fmt.Sprintf("distribution/MsgWithdrawDelegatorReward/%v", ok)) - action = fmt.Sprintf("TestMsgWithdrawDelegatorReward: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, nil, nil + action = fmt.Sprintf("TestMsgWithdrawDelegatorReward: ok %v, msg %s", ok, msg.GetSignBytes()) + return action, ok, nil, nil } } @@ -72,13 +74,13 @@ func SimulateMsgWithdrawValidatorCommission(m auth.AccountKeeper, k distribution handler := distribution.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, fOp []simulation.FutureOperation, err error) { + action string, ok bool, fOp []simulation.FutureOperation, err error) { account := simulation.RandomAcc(r, accs) msg := distribution.NewMsgWithdrawValidatorCommission(sdk.ValAddress(account.Address)) if msg.ValidateBasic() != nil { - return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() @@ -87,9 +89,10 @@ func SimulateMsgWithdrawValidatorCommission(m auth.AccountKeeper, k distribution write() } - event(fmt.Sprintf("distribution/MsgWithdrawValidatorCommission/%v", result.IsOK())) + ok = result.IsOK() + event(fmt.Sprintf("distribution/MsgWithdrawValidatorCommission/%v", ok)) - action = fmt.Sprintf("TestMsgWithdrawValidatorCommission: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, nil, nil + action = fmt.Sprintf("TestMsgWithdrawValidatorCommission: ok %v, msg %s", ok, msg.GetSignBytes()) + return action, ok, nil, nil } } diff --git a/x/gov/simulation/msgs.go b/x/gov/simulation/msgs.go index 4c5eddd372bc..ca234f3236db 100644 --- a/x/gov/simulation/msgs.go +++ b/x/gov/simulation/msgs.go @@ -38,17 +38,19 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Ope }) statePercentageArray := []float64{1, .9, .75, .4, .15, 0} curNumVotesState := 1 - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (action string, fOps []simulation.FutureOperation, err error) { + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( + action string, ok bool, fOps []simulation.FutureOperation, err error) { + // 1) submit proposal now sender := simulation.RandomAcc(r, accs) msg, err := simulationCreateMsgSubmitProposal(r, sender) if err != nil { - return "", nil, err + return "", false, nil, err } - action, ok := simulateHandleMsgSubmitProposal(msg, handler, ctx, event) + action, ok = simulateHandleMsgSubmitProposal(msg, handler, ctx, event) // don't schedule votes if proposal failed if !ok { - return action, nil, nil + return action, ok, nil, nil } proposalID := k.GetLastProposalID(ctx) // 2) Schedule operations for votes @@ -69,7 +71,7 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Ope // TODO: Find a way to check if a validator was slashed other than just checking their balance a block // before and after. - return action, fops, nil + return action, ok, fops, nil } } @@ -77,14 +79,16 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Ope // Note: Currently doesn't ensure that the proposal txt is in JSON form func SimulateMsgSubmitProposal(k gov.Keeper) simulation.Operation { handler := gov.NewHandler(k) - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (action string, fOps []simulation.FutureOperation, err error) { + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( + action string, ok bool, fOps []simulation.FutureOperation, err error) { + sender := simulation.RandomAcc(r, accs) msg, err := simulationCreateMsgSubmitProposal(r, sender) if err != nil { - return "", nil, err + return "", false, nil, err } - action, _ = simulateHandleMsgSubmitProposal(msg, handler, ctx, event) - return action, nil, nil + action, ok = simulateHandleMsgSubmitProposal(msg, handler, ctx, event) + return action, ok, nil, nil } } @@ -117,16 +121,18 @@ func simulationCreateMsgSubmitProposal(r *rand.Rand, sender simulation.Account) // SimulateMsgDeposit func SimulateMsgDeposit(k gov.Keeper) simulation.Operation { - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (action string, fOp []simulation.FutureOperation, err error) { + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( + action string, ok bool, fOp []simulation.FutureOperation, err error) { + acc := simulation.RandomAcc(r, accs) proposalID, ok := randomProposalID(r, k, ctx) if !ok { - return "no-operation", nil, nil + return "no-operation", false, nil, nil } deposit := randomDeposit(r) msg := gov.NewMsgDeposit(acc.Address, proposalID, deposit) if msg.ValidateBasic() != nil { - return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() result := gov.NewHandler(k)(ctx, msg) @@ -135,7 +141,7 @@ func SimulateMsgDeposit(k gov.Keeper) simulation.Operation { } event(fmt.Sprintf("gov/MsgDeposit/%v", result.IsOK())) action = fmt.Sprintf("TestMsgDeposit: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, nil, nil + return action, result.IsOK(), nil, nil } } @@ -147,24 +153,25 @@ func SimulateMsgVote(k gov.Keeper) simulation.Operation { // nolint: unparam func operationSimulateMsgVote(k gov.Keeper, acc simulation.Account, proposalID uint64) simulation.Operation { - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (action string, fOp []simulation.FutureOperation, err error) { + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( + action string, ok bool, fOp []simulation.FutureOperation, err error) { + if acc.Equals(simulation.Account{}) { acc = simulation.RandomAcc(r, accs) } - var ok bool - if proposalID < 0 { + var ok bool proposalID, ok = randomProposalID(r, k, ctx) if !ok { - return "no-operation", nil, nil + return "no-operation", false, nil, nil } } option := randomVotingOption(r) msg := gov.NewMsgVote(acc.Address, proposalID, option) if msg.ValidateBasic() != nil { - return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() @@ -175,7 +182,7 @@ func operationSimulateMsgVote(k gov.Keeper, acc simulation.Account, proposalID u event(fmt.Sprintf("gov/MsgVote/%v", result.IsOK())) action = fmt.Sprintf("TestMsgVote: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, nil, nil + return action, result.IsOK(), nil, nil } } diff --git a/x/mock/simulation/operation.go b/x/mock/simulation/operation.go index 00237e03ee31..7380349b96f4 100644 --- a/x/mock/simulation/operation.go +++ b/x/mock/simulation/operation.go @@ -20,7 +20,7 @@ import ( // These will be ran at the beginning of the corresponding block. type Operation func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, event func(string)) ( - action string, futureOps []FutureOperation, err error) + action string, ok bool, futureOps []FutureOperation, err error) // queue of operations type OperationQueue map[int][]Operation diff --git a/x/mock/simulation/simulate.go b/x/mock/simulation/simulate.go index e67bddc3c44d..7afe23a53b14 100644 --- a/x/mock/simulation/simulate.go +++ b/x/mock/simulation/simulate.go @@ -24,11 +24,11 @@ type AppStateFn func(r *rand.Rand, accs []Account, genesisTimestamp time.Time) ( // Simulate tests application by sending random messages. func Simulate(t *testing.T, app *baseapp.BaseApp, appStateFn AppStateFn, ops WeightedOperations, - invariants sdk.Invariants, numBlocks int, blockSize int, commit bool) (bool, error) { + invariants sdk.Invariants, numBlocks int, blockSize int, commit, lean bool) (bool, error) { time := time.Now().UnixNano() return SimulateFromSeed(t, app, appStateFn, time, ops, - invariants, numBlocks, blockSize, commit) + invariants, numBlocks, blockSize, commit, lean) } // initialize the chain for the simulation @@ -55,7 +55,7 @@ func initChain( func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, appStateFn AppStateFn, seed int64, ops WeightedOperations, invariants sdk.Invariants, - numBlocks int, blockSize int, commit bool) (stopEarly bool, simError error) { + numBlocks, blockSize int, commit, lean bool) (stopEarly bool, simError error) { // in case we have to end early, don't os.Exit so that we can run cleanup code. testingMode, t, b := getTestingMode(tb) @@ -119,7 +119,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, blockSimulator := createBlockSimulator( testingMode, tb, t, params, eventStats.tally, invariants, ops, operationQueue, timeOperationQueue, - numBlocks, blockSize, displayLogs) + numBlocks, blockSize, displayLogs, lean) if !testingMode { b.ResetTimer() @@ -163,12 +163,12 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, numQueuedOpsRan := runQueuedOperations( operationQueue, int(header.Height), tb, r, app, ctx, accs, logWriter, - displayLogs, eventStats.tally) + displayLogs, eventStats.tally, lean) numQueuedTimeOpsRan := runQueuedTimeOperations( timeOperationQueue, header.Time, tb, r, app, ctx, accs, - logWriter, displayLogs, eventStats.tally) + logWriter, displayLogs, eventStats.tally, lean) if testingMode && onOperation { assertAllInvariants(t, app, invariants, "QueuedOperations", displayLogs) @@ -238,7 +238,7 @@ type blockSimFn func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params Params, event func(string), invariants sdk.Invariants, ops WeightedOperations, operationQueue OperationQueue, timeOperationQueue []FutureOperation, - totalNumBlocks int, avgBlockSize int, displayLogs func()) blockSimFn { + totalNumBlocks int, avgBlockSize int, displayLogs func(), lean bool) blockSimFn { var lastBlocksizeState = 0 // state for [4 * uniform distribution] var blocksize int @@ -269,8 +269,10 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params // NOTE: the Rand 'r' should not be used here. opAndR := opAndRz[i] op, r2 := opAndR.op, opAndR.rand - logUpdate, futureOps, err := op(r2, app, ctx, accounts, event) - logWriter(logUpdate) + logUpdate, ok, futureOps, err := op(r2, app, ctx, accounts, event) + if !lean || (ok && lean) { + logWriter(logUpdate) + } if err != nil { displayLogs() tb.Fatalf("error on operation %d within block %d, %v", @@ -298,7 +300,7 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params func runQueuedOperations(queueOps map[int][]Operation, height int, tb testing.TB, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, logWriter func(string), - displayLogs func(), tallyEvent func(string)) (numOpsRan int) { + displayLogs func(), tallyEvent func(string), lean bool) (numOpsRan int) { queuedOp, ok := queueOps[height] if !ok { @@ -311,8 +313,10 @@ func runQueuedOperations(queueOps map[int][]Operation, // For now, queued operations cannot queue more operations. // If a need arises for us to support queued messages to queue more messages, this can // be changed. - logUpdate, _, err := queuedOp[i](r, app, ctx, accounts, tallyEvent) - logWriter(logUpdate) + logUpdate, ok, _, err := queuedOp[i](r, app, ctx, accounts, tallyEvent) + if !lean || (ok && lean) { + logWriter(logUpdate) + } if err != nil { displayLogs() tb.FailNow() @@ -325,7 +329,7 @@ func runQueuedOperations(queueOps map[int][]Operation, func runQueuedTimeOperations(queueOps []FutureOperation, currentTime time.Time, tb testing.TB, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, - logWriter func(string), displayLogs func(), tallyEvent func(string)) (numOpsRan int) { + logWriter func(string), displayLogs func(), tallyEvent func(string), lean bool) (numOpsRan int) { numOpsRan = 0 for len(queueOps) > 0 && currentTime.After(queueOps[0].BlockTime) { @@ -333,8 +337,10 @@ func runQueuedTimeOperations(queueOps []FutureOperation, // For now, queued operations cannot queue more operations. // If a need arises for us to support queued messages to queue more messages, this can // be changed. - logUpdate, _, err := queueOps[0].Op(r, app, ctx, accounts, tallyEvent) - logWriter(logUpdate) + logUpdate, ok, _, err := queueOps[0].Op(r, app, ctx, accounts, tallyEvent) + if !lean || (ok && lean) { + logWriter(logUpdate) + } if err != nil { displayLogs() tb.FailNow() diff --git a/x/mock/simulation/util.go b/x/mock/simulation/util.go index 96cade8f6670..73ae731b54ca 100644 --- a/x/mock/simulation/util.go +++ b/x/mock/simulation/util.go @@ -37,7 +37,7 @@ func getTestingMode(tb testing.TB) (testingMode bool, t *testing.T, b *testing.B } else { b = tb.(*testing.B) } - return + return testingMode, t, b } // Builds a function to add logs for this particular block diff --git a/x/slashing/simulation/msgs.go b/x/slashing/simulation/msgs.go index 732b3c28c4e9..9cad43cdf5ff 100644 --- a/x/slashing/simulation/msgs.go +++ b/x/slashing/simulation/msgs.go @@ -14,13 +14,13 @@ import ( func SimulateMsgUnjail(k slashing.Keeper) simulation.Operation { return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, fOp []simulation.FutureOperation, err error) { + action string, ok bool, fOp []simulation.FutureOperation, err error) { acc := simulation.RandomAcc(r, accs) address := sdk.ValAddress(acc.Address) msg := slashing.NewMsgUnjail(address) if msg.ValidateBasic() != nil { - return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() result := slashing.NewHandler(k)(ctx, msg) @@ -29,6 +29,6 @@ func SimulateMsgUnjail(k slashing.Keeper) simulation.Operation { } event(fmt.Sprintf("slashing/MsgUnjail/%v", result.IsOK())) action = fmt.Sprintf("TestMsgUnjail: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, nil, nil + return action, result.IsOK(), nil, nil } } diff --git a/x/staking/simulation/msgs.go b/x/staking/simulation/msgs.go index 559b0ae96dac..0871b1d2ab78 100644 --- a/x/staking/simulation/msgs.go +++ b/x/staking/simulation/msgs.go @@ -21,7 +21,7 @@ func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulati handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, fOp []simulation.FutureOperation, err error) { + action string, ok bool, fOp []simulation.FutureOperation, err error) { denom := k.GetParams(ctx).BondDenom description := staking.Description{ @@ -43,7 +43,7 @@ func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulati } if amount.Equal(sdk.ZeroInt()) { - return noOperation, nil, nil + return noOperation, false, nil, nil } selfDelegation := sdk.NewCoin(denom, amount) @@ -51,7 +51,7 @@ func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulati selfDelegation, description, commission, sdk.OneInt()) if msg.ValidateBasic() != nil { - return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() @@ -64,7 +64,7 @@ func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulati // require.True(t, result.IsOK(), "expected OK result but instead got %v", result) action = fmt.Sprintf("TestMsgCreateValidator: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, nil, nil + return action, result.IsOK(), nil, nil } } @@ -73,7 +73,7 @@ func SimulateMsgEditValidator(k staking.Keeper) simulation.Operation { handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, fOp []simulation.FutureOperation, err error) { + action string, ok bool, fOp []simulation.FutureOperation, err error) { description := staking.Description{ Moniker: simulation.RandStringOfLength(r, 10), @@ -92,7 +92,7 @@ func SimulateMsgEditValidator(k staking.Keeper) simulation.Operation { msg := staking.NewMsgEditValidator(address, description, &newCommissionRate, nil) if msg.ValidateBasic() != nil { - return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() result := handler(ctx, msg) @@ -101,7 +101,7 @@ func SimulateMsgEditValidator(k staking.Keeper) simulation.Operation { } event(fmt.Sprintf("staking/MsgEditValidator/%v", result.IsOK())) action = fmt.Sprintf("TestMsgEditValidator: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, nil, nil + return action, result.IsOK(), nil, nil } } @@ -110,7 +110,7 @@ func SimulateMsgDelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Oper handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, fOp []simulation.FutureOperation, err error) { + action string, ok bool, fOp []simulation.FutureOperation, err error) { denom := k.GetParams(ctx).BondDenom if len(k.GetAllValidators(ctx)) == 0 { @@ -125,14 +125,14 @@ func SimulateMsgDelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Oper amount = simulation.RandomAmount(r, amount) } if amount.Equal(sdk.ZeroInt()) { - return noOperation, nil, nil + return noOperation, false nil, nil } msg := staking.NewMsgDelegate( delegatorAddress, validatorAddress, sdk.NewCoin(denom, amount)) if msg.ValidateBasic() != nil { - return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() result := handler(ctx, msg) @@ -141,7 +141,7 @@ func SimulateMsgDelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Oper } event(fmt.Sprintf("staking/MsgDelegate/%v", result.IsOK())) action = fmt.Sprintf("TestMsgDelegate: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, nil, nil + return action, result.IsOK(), nil, nil } } @@ -150,19 +150,19 @@ func SimulateMsgUndelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Op handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, fOp []simulation.FutureOperation, err error) { + action string, ok bool, fOp []simulation.FutureOperation, err error) { delegatorAcc := simulation.RandomAcc(r, accs) delegatorAddress := delegatorAcc.Address delegations := k.GetAllDelegatorDelegations(ctx, delegatorAddress) if len(delegations) == 0 { - return noOperation, nil, nil + return noOperation, false, nil, nil } delegation := delegations[r.Intn(len(delegations))] numShares := simulation.RandomDecAmount(r, delegation.Shares) if numShares.Equal(sdk.ZeroDec()) { - return noOperation, nil, nil + return noOperation, false, nil, nil } msg := staking.MsgUndelegate{ DelegatorAddress: delegatorAddress, @@ -170,7 +170,7 @@ func SimulateMsgUndelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Op SharesAmount: numShares, } if msg.ValidateBasic() != nil { - return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s, got error %v", + return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s, got error %v", msg.GetSignBytes(), msg.ValidateBasic()) } ctx, write := ctx.CacheContext() @@ -180,7 +180,7 @@ func SimulateMsgUndelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Op } event(fmt.Sprintf("staking/MsgUndelegate/%v", result.IsOK())) action = fmt.Sprintf("TestMsgUndelegate: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, nil, nil + return action, result.IsOK(), nil, nil } } @@ -189,7 +189,7 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulati handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, fOp []simulation.FutureOperation, err error) { + action string, ok bool, fOp []simulation.FutureOperation, err error) { denom := k.GetParams(ctx).BondDenom if len(k.GetAllValidators(ctx)) == 0 { @@ -207,7 +207,7 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulati amount = simulation.RandomAmount(r, amount) } if amount.Equal(sdk.ZeroInt()) { - return noOperation, nil, nil + return noOperation, false, nil, nil } msg := staking.MsgBeginRedelegate{ DelegatorAddress: delegatorAddress, @@ -216,7 +216,7 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulati SharesAmount: amount.ToDec(), } if msg.ValidateBasic() != nil { - return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() result := handler(ctx, msg) @@ -225,6 +225,6 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulati } event(fmt.Sprintf("staking/MsgBeginRedelegate/%v", result.IsOK())) action = fmt.Sprintf("TestMsgBeginRedelegate: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, nil, nil + return action, result.IsOK(), nil, nil } } From d79e4f8f938a86de0cdc18878690e5c6041501e4 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 26 Feb 2019 05:46:37 -0500 Subject: [PATCH 03/19] cleanup bank simulation messages --- cmd/gaia/app/sim_test.go | 4 +-- x/bank/simulation/msgs.go | 65 ++++----------------------------------- 2 files changed, 8 insertions(+), 61 deletions(-) diff --git a/cmd/gaia/app/sim_test.go b/cmd/gaia/app/sim_test.go index 7a85973f52ac..e2003c6810c7 100644 --- a/cmd/gaia/app/sim_test.go +++ b/cmd/gaia/app/sim_test.go @@ -276,8 +276,8 @@ func randIntBetween(r *rand.Rand, min, max int) int { func testAndRunTxs(app *GaiaApp) []simulation.WeightedOperation { return []simulation.WeightedOperation{ {5, authsim.SimulateDeductFee(app.accountKeeper, app.feeCollectionKeeper)}, - {100, banksim.SendMsg(app.accountKeeper, app.bankKeeper)}, - {10, banksim.SingleInputMsgMultiSend(app.accountKeeper, app.bankKeeper)}, + {100, banksim.SimulateMsgSend(app.accountKeeper, app.bankKeeper)}, + {10, banksim.SimulateSingleInputMsgMultiSend(app.accountKeeper, app.bankKeeper)}, {50, distrsim.SimulateMsgSetWithdrawAddress(app.accountKeeper, app.distrKeeper)}, {50, distrsim.SimulateMsgWithdrawDelegatorReward(app.accountKeeper, app.distrKeeper)}, {50, distrsim.SimulateMsgWithdrawValidatorCommission(app.accountKeeper, app.distrKeeper)}, diff --git a/x/bank/simulation/msgs.go b/x/bank/simulation/msgs.go index c5e8ab6f4098..5e58ab09b5f8 100644 --- a/x/bank/simulation/msgs.go +++ b/x/bank/simulation/msgs.go @@ -18,12 +18,12 @@ import ( // SendTx tests and runs a single msg send where both // accounts already exist. -func SendMsg(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Operation { +func SimulateMsgSend(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Operation { handler := bank.NewHandler(bk) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( action string, ok bool, fOps []simulation.FutureOperation, err error) { - fromAcc, action, msg, ok := createSendMsg(r, ctx, accs, mapper) + fromAcc, action, msg, ok := createMsgSend(r, ctx, accs, mapper) if !ok { return action, ok, nil, nil } @@ -37,27 +37,7 @@ func SendMsg(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Operation { } } -// SendTx tests and runs a single tx send, with auth where both -// accounts already exist. -func SendTx(mapper auth.AccountKeeper) simulation.Operation { - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOps []simulation.FutureOperation, err error) { - - fromAcc, action, msg, ok := createSendMsg(r, ctx, accs, mapper) - if !ok { - return action, ok, nil, nil - } - err = sendAndVerifyMsgSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, nil) - if err != nil { - return "", false, nil, err - } - event("bank/sendAndVerifyTxSend/ok") - - return action, ok, nil, nil - } -} - -func createSendMsg(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper auth.AccountKeeper) ( +func createMsgSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper auth.AccountKeeper) ( fromAcc simulation.Account, action string, msg bank.MsgSend, ok bool) { fromAcc = simulation.RandomAcc(r, accs) @@ -69,7 +49,6 @@ func createSendMsg(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, map } toAcc = simulation.RandomAcc(r, accs) } - toAddr := toAcc.Address initFromCoins := mapper.GetAccount(ctx, fromAcc.Address).SpendableCoins(ctx.BlockHeader().Time) if len(initFromCoins) == 0 { @@ -82,15 +61,9 @@ func createSendMsg(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, map return fromAcc, "skipping bank send due to account having no coins of denomination " + initFromCoins[denomIndex].Denom, msg, false } - action = fmt.Sprintf("%s is sending %s %s to %s", - fromAcc.Address.String(), - amt.String(), - initFromCoins[denomIndex].Denom, - toAddr.String(), - ) - coins := sdk.Coins{sdk.NewCoin(initFromCoins[denomIndex].Denom, amt)} msg = bank.NewMsgSend(fromAcc.Address, toAcc.Address, coins) + action = fmt.Sprintf("TestMsgSend: ok %v, msg %s", true, msg.GetSignBytes()) return fromAcc, action, msg, true } @@ -140,29 +113,9 @@ func sendAndVerifyMsgSend(app *baseapp.BaseApp, mapper auth.AccountKeeper, msg b return nil } -// SingleInputSendTx tests and runs a single msg multisend w/ auth, with one input and one output, where both -// accounts already exist. -func SingleInputMultiSendTx(mapper auth.AccountKeeper) simulation.Operation { - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOps []simulation.FutureOperation, err error) { - - fromAcc, action, msg, ok := createSingleInputMsgMultiSend(r, ctx, accs, mapper) - if !ok { - return action, ok, nil, nil - } - err = sendAndVerifyMsgMultiSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, nil) - if err != nil { - return "", false, nil, err - } - event("bank/sendAndVerifyTxMultiSend/ok") - - return action, ok, nil, nil - } -} - // SingleInputSendMsg tests and runs a single msg multisend, with one input and one output, where both // accounts already exist. -func SingleInputMsgMultiSend(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Operation { +func SimulateSingleInputMsgMultiSend(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Operation { handler := bank.NewHandler(bk) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( action string, ok bool, fOps []simulation.FutureOperation, err error) { @@ -206,18 +159,12 @@ func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulat return fromAcc, "skipping bank send due to account having no coins of denomination " + initFromCoins[denomIndex].Denom, msg, false } - action = fmt.Sprintf("%s is sending %s %s to %s", - fromAcc.Address.String(), - amt.String(), - initFromCoins[denomIndex].Denom, - toAddr.String(), - ) - coins := sdk.Coins{sdk.NewCoin(initFromCoins[denomIndex].Denom, amt)} msg = bank.MsgMultiSend{ Inputs: []bank.Input{bank.NewInput(fromAcc.Address, coins)}, Outputs: []bank.Output{bank.NewOutput(toAddr, coins)}, } + action = fmt.Sprintf("TestMsgMultiSend: ok %v, msg %s", true, msg.GetSignBytes()) return fromAcc, action, msg, true } From 0a6ea3b9066884219a6216bc2b04307cbe601097 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 26 Feb 2019 07:43:23 -0500 Subject: [PATCH 04/19] operation messges int --- x/auth/simulation/fake.go | 15 +++++---- x/bank/simulation/msgs.go | 35 ++++++++++--------- x/distribution/simulation/msgs.go | 42 ++++++++++------------- x/gov/simulation/msgs.go | 55 ++++++++++++++++-------------- x/mock/simulation/operation.go | 55 +++++++++++++++++++++++++++++- x/mock/simulation/simulate.go | 20 +++++------ x/slashing/simulation/msgs.go | 8 ++--- x/staking/simulation/msgs.go | 56 ++++++++++++++----------------- 8 files changed, 165 insertions(+), 121 deletions(-) diff --git a/x/auth/simulation/fake.go b/x/auth/simulation/fake.go index af996a3627e3..4b073e7b1739 100644 --- a/x/auth/simulation/fake.go +++ b/x/auth/simulation/fake.go @@ -16,15 +16,16 @@ import ( func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulation.Operation { return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOp []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { account := simulation.RandomAcc(r, accs) stored := m.GetAccount(ctx, account.Address) initCoins := stored.GetCoins() + opMsg = simulation.NewOperationMsgBasic("auth", "deduct_fee", "", false, nil) if len(initCoins) == 0 { event(fmt.Sprintf("auth/SimulateDeductFee/false")) - return action, false, nil, nil + return opMsg, nil, nil } denomIndex := r.Intn(len(initCoins)) @@ -33,7 +34,7 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat amt, err := randPositiveInt(r, randCoin.Amount) if err != nil { event(fmt.Sprintf("auth/SimulateDeductFee/false")) - return action, false, nil, nil + return opMsg, nil, nil } // Create a random fee and verify the fees are within the account's spendable @@ -42,14 +43,14 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat spendableCoins := stored.SpendableCoins(ctx.BlockHeader().Time) if _, hasNeg := spendableCoins.SafeSub(fees); hasNeg { event(fmt.Sprintf("auth/SimulateDeductFee/false")) - return action, false, nil, nil + return opMsg, nil, nil } // get the new account balance newCoins, hasNeg := initCoins.SafeSub(fees) if hasNeg { event(fmt.Sprintf("auth/SimulateDeductFee/false")) - return action, false, nil, nil + return opMsg, nil, nil } if err := stored.SetCoins(newCoins); err != nil { @@ -60,8 +61,8 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat f.AddCollectedFees(ctx, fees) event(fmt.Sprintf("auth/SimulateDeductFee/true")) - action = "TestDeductFee" - return action, false, nil, nil + opMsg.OK = true + return opMsg, nil, nil } } diff --git a/x/bank/simulation/msgs.go b/x/bank/simulation/msgs.go index 5e58ab09b5f8..489b2ec94cd1 100644 --- a/x/bank/simulation/msgs.go +++ b/x/bank/simulation/msgs.go @@ -21,24 +21,24 @@ import ( func SimulateMsgSend(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Operation { handler := bank.NewHandler(bk) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOps []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { - fromAcc, action, msg, ok := createMsgSend(r, ctx, accs, mapper) + fromAcc, comment, msg, ok := createMsgSend(r, ctx, accs, mapper) + opMsg = simulation.NewOperationMsg(msg, ok, comment) if !ok { - return action, ok, nil, nil + return opMsg, nil, nil } err = sendAndVerifyMsgSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, handler) if err != nil { - return "", false, nil, err + return opMsg, nil, err } event("bank/sendAndVerifyTxSend/ok") - - return action, ok, nil, nil + return opMsg, nil, nil } } func createMsgSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper auth.AccountKeeper) ( - fromAcc simulation.Account, action string, msg bank.MsgSend, ok bool) { + fromAcc simulation.Account, comment string, msg bank.MsgSend, ok bool) { fromAcc = simulation.RandomAcc(r, accs) toAcc := simulation.RandomAcc(r, accs) @@ -63,9 +63,7 @@ func createMsgSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, map coins := sdk.Coins{sdk.NewCoin(initFromCoins[denomIndex].Denom, amt)} msg = bank.NewMsgSend(fromAcc.Address, toAcc.Address, coins) - action = fmt.Sprintf("TestMsgSend: ok %v, msg %s", true, msg.GetSignBytes()) - - return fromAcc, action, msg, true + return fromAcc, "", msg, true } // Sends and verifies the transition of a msg send. @@ -118,24 +116,26 @@ func sendAndVerifyMsgSend(app *baseapp.BaseApp, mapper auth.AccountKeeper, msg b func SimulateSingleInputMsgMultiSend(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Operation { handler := bank.NewHandler(bk) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOps []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { - fromAcc, action, msg, ok := createSingleInputMsgMultiSend(r, ctx, accs, mapper) + fromAcc, comment, msg, ok := createSingleInputMsgMultiSend(r, ctx, accs, mapper) + opMsg = simulation.NewOperationMsg(msg, ok, comment) if !ok { - return action, ok, nil, nil + return opMsg, nil, nil } err = sendAndVerifyMsgMultiSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, handler) if err != nil { - return "", false, nil, err + return opMsg, nil, err } event("bank/sendAndVerifyMsgMultiSend/ok") - return action, ok, nil, nil + opMsg = simulation.NewOperationMsg(msg, ok, comment) + return opMsg, nil, nil } } func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper auth.AccountKeeper) ( - fromAcc simulation.Account, action string, msg bank.MsgMultiSend, ok bool) { + fromAcc simulation.Account, comment string, msg bank.MsgMultiSend, ok bool) { fromAcc = simulation.RandomAcc(r, accs) toAcc := simulation.RandomAcc(r, accs) @@ -164,8 +164,7 @@ func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulat Inputs: []bank.Input{bank.NewInput(fromAcc.Address, coins)}, Outputs: []bank.Output{bank.NewOutput(toAddr, coins)}, } - action = fmt.Sprintf("TestMsgMultiSend: ok %v, msg %s", true, msg.GetSignBytes()) - return fromAcc, action, msg, true + return fromAcc, "", msg, true } // Sends and verifies the transition of a msg multisend. This fails if there are repeated inputs or outputs diff --git a/x/distribution/simulation/msgs.go b/x/distribution/simulation/msgs.go index a5f7768512ca..ffb97971ac6e 100644 --- a/x/distribution/simulation/msgs.go +++ b/x/distribution/simulation/msgs.go @@ -16,27 +16,25 @@ func SimulateMsgSetWithdrawAddress(m auth.AccountKeeper, k distribution.Keeper) handler := distribution.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOp []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { accountOrigin := simulation.RandomAcc(r, accs) accountDestination := simulation.RandomAcc(r, accs) msg := distribution.NewMsgSetWithdrawAddress(accountOrigin.Address, accountDestination.Address) if msg.ValidateBasic() != nil { - return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() - result := handler(ctx, msg) - if result.IsOK() { + ok := handler(ctx, msg).IsOK() + if ok { write() } - ok = result.IsOK() event(fmt.Sprintf("distribution/MsgSetWithdrawAddress/%v", ok)) - - action = fmt.Sprintf("TestMsgSetWithdrawAddress: ok %v, msg %s", ok, msg.GetSignBytes()) - return action, ok, nil, nil + opMsg = simulation.NewOperationMsg(msg, ok, "") + return opMsg, nil, nil } } @@ -45,27 +43,25 @@ func SimulateMsgWithdrawDelegatorReward(m auth.AccountKeeper, k distribution.Kee handler := distribution.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOp []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { delegatorAccount := simulation.RandomAcc(r, accs) validatorAccount := simulation.RandomAcc(r, accs) msg := distribution.NewMsgWithdrawDelegatorReward(delegatorAccount.Address, sdk.ValAddress(validatorAccount.Address)) if msg.ValidateBasic() != nil { - return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() - result := handler(ctx, msg) - if result.IsOK() { + ok := handler(ctx, msg).IsOK() + if ok { write() } - ok = result.IsOK() event(fmt.Sprintf("distribution/MsgWithdrawDelegatorReward/%v", ok)) - - action = fmt.Sprintf("TestMsgWithdrawDelegatorReward: ok %v, msg %s", ok, msg.GetSignBytes()) - return action, ok, nil, nil + opMsg = simulation.NewOperationMsg(msg, ok, "") + return opMsg, nil, nil } } @@ -74,25 +70,23 @@ func SimulateMsgWithdrawValidatorCommission(m auth.AccountKeeper, k distribution handler := distribution.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOp []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { account := simulation.RandomAcc(r, accs) msg := distribution.NewMsgWithdrawValidatorCommission(sdk.ValAddress(account.Address)) if msg.ValidateBasic() != nil { - return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() - result := handler(ctx, msg) - if result.IsOK() { + ok := handler(ctx, msg).IsOK() + if ok { write() } - ok = result.IsOK() event(fmt.Sprintf("distribution/MsgWithdrawValidatorCommission/%v", ok)) - - action = fmt.Sprintf("TestMsgWithdrawValidatorCommission: ok %v, msg %s", ok, msg.GetSignBytes()) - return action, ok, nil, nil + opMsg = simulation.NewOperationMsg(msg, ok, "") + return opMsg, nil, nil } } diff --git a/x/gov/simulation/msgs.go b/x/gov/simulation/msgs.go index ca234f3236db..33263c830e72 100644 --- a/x/gov/simulation/msgs.go +++ b/x/gov/simulation/msgs.go @@ -39,18 +39,19 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Ope statePercentageArray := []float64{1, .9, .75, .4, .15, 0} curNumVotesState := 1 return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOps []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { // 1) submit proposal now sender := simulation.RandomAcc(r, accs) msg, err := simulationCreateMsgSubmitProposal(r, sender) if err != nil { - return "", false, nil, err + return simulation.NoOpMsg(), nil, err } - action, ok = simulateHandleMsgSubmitProposal(msg, handler, ctx, event) + ok := simulateHandleMsgSubmitProposal(msg, handler, ctx, event) + opMsg = simulation.NewOperationMsg(msg, ok, "") // don't schedule votes if proposal failed if !ok { - return action, ok, nil, nil + return opMsg, nil, nil } proposalID := k.GetLastProposalID(ctx) // 2) Schedule operations for votes @@ -71,7 +72,7 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Ope // TODO: Find a way to check if a validator was slashed other than just checking their balance a block // before and after. - return action, ok, fops, nil + return opMsg, fops, nil } } @@ -80,19 +81,20 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Ope func SimulateMsgSubmitProposal(k gov.Keeper) simulation.Operation { handler := gov.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOps []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { sender := simulation.RandomAcc(r, accs) msg, err := simulationCreateMsgSubmitProposal(r, sender) if err != nil { - return "", false, nil, err + return simulation.NoOpMsg(), nil, err } - action, ok = simulateHandleMsgSubmitProposal(msg, handler, ctx, event) - return action, ok, nil, nil + ok := simulateHandleMsgSubmitProposal(msg, handler, ctx, event) + opMsg = simulation.NewOperationMsg(msg, ok, "") + return opMsg, nil, nil } } -func simulateHandleMsgSubmitProposal(msg gov.MsgSubmitProposal, handler sdk.Handler, ctx sdk.Context, event func(string)) (action string, ok bool) { +func simulateHandleMsgSubmitProposal(msg gov.MsgSubmitProposal, handler sdk.Handler, ctx sdk.Context, event func(string)) (ok bool) { ctx, write := ctx.CacheContext() result := handler(ctx, msg) ok = result.IsOK() @@ -100,8 +102,7 @@ func simulateHandleMsgSubmitProposal(msg gov.MsgSubmitProposal, handler sdk.Hand write() } event(fmt.Sprintf("gov/MsgSubmitProposal/%v", ok)) - action = fmt.Sprintf("TestMsgSubmitProposal: ok %v, msg %s", ok, msg.GetSignBytes()) - return + return ok } func simulationCreateMsgSubmitProposal(r *rand.Rand, sender simulation.Account) (msg gov.MsgSubmitProposal, err error) { @@ -122,26 +123,28 @@ func simulationCreateMsgSubmitProposal(r *rand.Rand, sender simulation.Account) // SimulateMsgDeposit func SimulateMsgDeposit(k gov.Keeper) simulation.Operation { return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOp []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { acc := simulation.RandomAcc(r, accs) proposalID, ok := randomProposalID(r, k, ctx) if !ok { - return "no-operation", false, nil, nil + return simulation.NoOpMsg(), nil, nil } deposit := randomDeposit(r) msg := gov.NewMsgDeposit(acc.Address, proposalID, deposit) if msg.ValidateBasic() != nil { - return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() result := gov.NewHandler(k)(ctx, msg) if result.IsOK() { write() } - event(fmt.Sprintf("gov/MsgDeposit/%v", result.IsOK())) - action = fmt.Sprintf("TestMsgDeposit: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, result.IsOK(), nil, nil + ok = gov.NewHandler(k)(ctx, msg).IsOK() + + event(fmt.Sprintf("gov/MsgDeposit/%v", ok)) + opMsg = simulation.NewOperationMsg(msg, ok, "") + return opMsg, nil, nil } } @@ -154,7 +157,7 @@ func SimulateMsgVote(k gov.Keeper) simulation.Operation { // nolint: unparam func operationSimulateMsgVote(k gov.Keeper, acc simulation.Account, proposalID uint64) simulation.Operation { return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOp []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { if acc.Equals(simulation.Account{}) { acc = simulation.RandomAcc(r, accs) @@ -164,25 +167,25 @@ func operationSimulateMsgVote(k gov.Keeper, acc simulation.Account, proposalID u var ok bool proposalID, ok = randomProposalID(r, k, ctx) if !ok { - return "no-operation", false, nil, nil + return simulation.NoOpMsg(), nil, nil } } option := randomVotingOption(r) msg := gov.NewMsgVote(acc.Address, proposalID, option) if msg.ValidateBasic() != nil { - return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() - result := gov.NewHandler(k)(ctx, msg) - if result.IsOK() { + ok := gov.NewHandler(k)(ctx, msg).IsOK() + if ok { write() } - event(fmt.Sprintf("gov/MsgVote/%v", result.IsOK())) - action = fmt.Sprintf("TestMsgVote: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, result.IsOK(), nil, nil + event(fmt.Sprintf("gov/MsgVote/%v", ok)) + opMsg = simulation.NewOperationMsg(msg, ok, "") + return opMsg, nil, nil } } diff --git a/x/mock/simulation/operation.go b/x/mock/simulation/operation.go index 7380349b96f4..86121a6f12e0 100644 --- a/x/mock/simulation/operation.go +++ b/x/mock/simulation/operation.go @@ -1,6 +1,7 @@ package simulation import ( + "encoding/json" "math/rand" "sort" "time" @@ -20,7 +21,59 @@ import ( // These will be ran at the beginning of the corresponding block. type Operation func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, event func(string)) ( - action string, ok bool, futureOps []FutureOperation, err error) + OperationMsg OperationMsg, futureOps []FutureOperation, err error) + +// OperationMsg - structure for operation output +type OperationMsg struct { + Route string `json:"route"` + Name string `json:"name"` + Comment string `json:"comment"` + OK bool `json:"ok"` + Msg json.RawMessage `json:"msg"` +} + +// OperationMsg - create a new operation message from sdk.Msg +func NewOperationMsg(msg sdk.Msg, ok bool, comment string) OperationMsg { + + return OperationMsg{ + Route: msg.Route(), + Name: msg.Type(), + Comment: comment, + OK: ok, + Msg: msg.GetSignBytes(), + } +} + +// OperationMsg - create a new operation message from raw input +func NewOperationMsgBasic(route, name, comment string, ok bool, msg []byte) OperationMsg { + return OperationMsg{ + Route: route, + Name: name, + Comment: comment, + OK: ok, + Msg: msg, + } +} + +// OperationMsg - create a new operation message from sdk.Msg +func NoOpMsg() OperationMsg { + return OperationMsg{ + Route: "", + Name: "no-operation", + Comment: "", + OK: false, + Msg: nil, + } +} + +// LogEntry - log entry text for this operation msg +func (om OperationMsg) String() string { + out, err := json.Marshal(om) + if err != nil { + panic(err) + } + return string(out) +} // queue of operations type OperationQueue map[int][]Operation diff --git a/x/mock/simulation/simulate.go b/x/mock/simulation/simulate.go index 7afe23a53b14..e5e2c3affe7c 100644 --- a/x/mock/simulation/simulate.go +++ b/x/mock/simulation/simulate.go @@ -269,9 +269,9 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params // NOTE: the Rand 'r' should not be used here. opAndR := opAndRz[i] op, r2 := opAndR.op, opAndR.rand - logUpdate, ok, futureOps, err := op(r2, app, ctx, accounts, event) - if !lean || (ok && lean) { - logWriter(logUpdate) + opMsg, futureOps, err := op(r2, app, ctx, accounts, event) + if !lean || (opMsg.OK && lean) { + logWriter(opMsg.String()) } if err != nil { displayLogs() @@ -282,7 +282,7 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params queueOperations(operationQueue, timeOperationQueue, futureOps) if testingMode { if onOperation { - eventStr := fmt.Sprintf("operation: %v", logUpdate) + eventStr := fmt.Sprintf("operation: %v", opMsg.String()) assertAllInvariants(t, app, invariants, eventStr, displayLogs) } if opCount%50 == 0 { @@ -313,9 +313,9 @@ func runQueuedOperations(queueOps map[int][]Operation, // For now, queued operations cannot queue more operations. // If a need arises for us to support queued messages to queue more messages, this can // be changed. - logUpdate, ok, _, err := queuedOp[i](r, app, ctx, accounts, tallyEvent) - if !lean || (ok && lean) { - logWriter(logUpdate) + opMsg, _, err := queuedOp[i](r, app, ctx, accounts, tallyEvent) + if !lean || (opMsg.OK && lean) { + logWriter(opMsg.String()) } if err != nil { displayLogs() @@ -337,9 +337,9 @@ func runQueuedTimeOperations(queueOps []FutureOperation, // For now, queued operations cannot queue more operations. // If a need arises for us to support queued messages to queue more messages, this can // be changed. - logUpdate, ok, _, err := queueOps[0].Op(r, app, ctx, accounts, tallyEvent) - if !lean || (ok && lean) { - logWriter(logUpdate) + opMsg, _, err := queueOps[0].Op(r, app, ctx, accounts, tallyEvent) + if !lean || (opMsg.OK && lean) { + logWriter(opMsg.String()) } if err != nil { displayLogs() diff --git a/x/slashing/simulation/msgs.go b/x/slashing/simulation/msgs.go index 9cad43cdf5ff..a7cb45cf3ce4 100644 --- a/x/slashing/simulation/msgs.go +++ b/x/slashing/simulation/msgs.go @@ -14,13 +14,13 @@ import ( func SimulateMsgUnjail(k slashing.Keeper) simulation.Operation { return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOp []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { acc := simulation.RandomAcc(r, accs) address := sdk.ValAddress(acc.Address) msg := slashing.NewMsgUnjail(address) if msg.ValidateBasic() != nil { - return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() result := slashing.NewHandler(k)(ctx, msg) @@ -28,7 +28,7 @@ func SimulateMsgUnjail(k slashing.Keeper) simulation.Operation { write() } event(fmt.Sprintf("slashing/MsgUnjail/%v", result.IsOK())) - action = fmt.Sprintf("TestMsgUnjail: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, result.IsOK(), nil, nil + opMsg = simulation.NewOperationMsg(msg, result.IsOK(), "") + return opMsg, nil, nil } } diff --git a/x/staking/simulation/msgs.go b/x/staking/simulation/msgs.go index 0871b1d2ab78..2c3ad5fcbf81 100644 --- a/x/staking/simulation/msgs.go +++ b/x/staking/simulation/msgs.go @@ -12,16 +12,12 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/keeper" ) -const ( - noOperation = "no-operation" -) - // SimulateMsgCreateValidator func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulation.Operation { handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOp []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { denom := k.GetParams(ctx).BondDenom description := staking.Description{ @@ -43,7 +39,7 @@ func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulati } if amount.Equal(sdk.ZeroInt()) { - return noOperation, false, nil, nil + return simulation.NoOpMsg(), nil, nil } selfDelegation := sdk.NewCoin(denom, amount) @@ -51,7 +47,7 @@ func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulati selfDelegation, description, commission, sdk.OneInt()) if msg.ValidateBasic() != nil { - return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() @@ -61,10 +57,8 @@ func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulati } event(fmt.Sprintf("staking/MsgCreateValidator/%v", result.IsOK())) - - // require.True(t, result.IsOK(), "expected OK result but instead got %v", result) - action = fmt.Sprintf("TestMsgCreateValidator: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, result.IsOK(), nil, nil + opMsg = simulation.NewOperationMsg(msg, result.IsOK(), "") + return opMsg, nil, nil } } @@ -73,7 +67,7 @@ func SimulateMsgEditValidator(k staking.Keeper) simulation.Operation { handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOp []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { description := staking.Description{ Moniker: simulation.RandStringOfLength(r, 10), @@ -92,7 +86,7 @@ func SimulateMsgEditValidator(k staking.Keeper) simulation.Operation { msg := staking.NewMsgEditValidator(address, description, &newCommissionRate, nil) if msg.ValidateBasic() != nil { - return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() result := handler(ctx, msg) @@ -100,8 +94,8 @@ func SimulateMsgEditValidator(k staking.Keeper) simulation.Operation { write() } event(fmt.Sprintf("staking/MsgEditValidator/%v", result.IsOK())) - action = fmt.Sprintf("TestMsgEditValidator: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, result.IsOK(), nil, nil + opMsg = simulation.NewOperationMsg(msg, result.IsOK(), "") + return opMsg, nil, nil } } @@ -110,7 +104,7 @@ func SimulateMsgDelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Oper handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOp []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { denom := k.GetParams(ctx).BondDenom if len(k.GetAllValidators(ctx)) == 0 { @@ -125,14 +119,14 @@ func SimulateMsgDelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Oper amount = simulation.RandomAmount(r, amount) } if amount.Equal(sdk.ZeroInt()) { - return noOperation, false nil, nil + return simulation.NoOpMsg(), nil, nil } msg := staking.NewMsgDelegate( delegatorAddress, validatorAddress, sdk.NewCoin(denom, amount)) if msg.ValidateBasic() != nil { - return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() result := handler(ctx, msg) @@ -140,8 +134,8 @@ func SimulateMsgDelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Oper write() } event(fmt.Sprintf("staking/MsgDelegate/%v", result.IsOK())) - action = fmt.Sprintf("TestMsgDelegate: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, result.IsOK(), nil, nil + opMsg = simulation.NewOperationMsg(msg, result.IsOK(), "") + return opMsg, nil, nil } } @@ -150,19 +144,19 @@ func SimulateMsgUndelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Op handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOp []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { delegatorAcc := simulation.RandomAcc(r, accs) delegatorAddress := delegatorAcc.Address delegations := k.GetAllDelegatorDelegations(ctx, delegatorAddress) if len(delegations) == 0 { - return noOperation, false, nil, nil + return simulation.NoOpMsg(), nil, nil } delegation := delegations[r.Intn(len(delegations))] numShares := simulation.RandomDecAmount(r, delegation.Shares) if numShares.Equal(sdk.ZeroDec()) { - return noOperation, false, nil, nil + return simulation.NoOpMsg(), nil, nil } msg := staking.MsgUndelegate{ DelegatorAddress: delegatorAddress, @@ -170,7 +164,7 @@ func SimulateMsgUndelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Op SharesAmount: numShares, } if msg.ValidateBasic() != nil { - return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s, got error %v", + return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s, got error %v", msg.GetSignBytes(), msg.ValidateBasic()) } ctx, write := ctx.CacheContext() @@ -179,8 +173,8 @@ func SimulateMsgUndelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Op write() } event(fmt.Sprintf("staking/MsgUndelegate/%v", result.IsOK())) - action = fmt.Sprintf("TestMsgUndelegate: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, result.IsOK(), nil, nil + opMsg = simulation.NewOperationMsg(msg, result.IsOK(), "") + return opMsg, nil, nil } } @@ -189,7 +183,7 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulati handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( - action string, ok bool, fOp []simulation.FutureOperation, err error) { + opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { denom := k.GetParams(ctx).BondDenom if len(k.GetAllValidators(ctx)) == 0 { @@ -207,7 +201,7 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulati amount = simulation.RandomAmount(r, amount) } if amount.Equal(sdk.ZeroInt()) { - return noOperation, false, nil, nil + return simulation.NoOpMsg(), nil, nil } msg := staking.MsgBeginRedelegate{ DelegatorAddress: delegatorAddress, @@ -216,7 +210,7 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulati SharesAmount: amount.ToDec(), } if msg.ValidateBasic() != nil { - return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) + return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() result := handler(ctx, msg) @@ -224,7 +218,7 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulati write() } event(fmt.Sprintf("staking/MsgBeginRedelegate/%v", result.IsOK())) - action = fmt.Sprintf("TestMsgBeginRedelegate: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) - return action, result.IsOK(), nil, nil + opMsg = simulation.NewOperationMsg(msg, result.IsOK(), "") + return opMsg, nil, nil } } From de6f42f0a1dd8d1dc59562488b287d58cde5aa0f Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 26 Feb 2019 14:39:21 -0500 Subject: [PATCH 05/19] lint --- x/mock/simulation/util.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/x/mock/simulation/util.go b/x/mock/simulation/util.go index 73ae731b54ca..21e63470d709 100644 --- a/x/mock/simulation/util.go +++ b/x/mock/simulation/util.go @@ -79,7 +79,10 @@ func logPrinter(testingmode bool, logs []*strings.Builder) func() { filePath := path.Join(folderPath, fileName) fmt.Printf("Too many logs to display, instead writing to %s\n", filePath) - os.MkdirAll(folderPath, os.ModePerm) + err := os.MkdirAll(folderPath, os.ModePerm) + if err != nil { + panic(err) + } f, _ = os.Create(filePath) } From d65a9c94ec550391eb65487687b22cb8459269b6 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 26 Feb 2019 22:33:55 -0500 Subject: [PATCH 06/19] move simulation to its own module --- cmd/gaia/app/sim_test.go | 2 +- x/auth/simulation/fake.go | 2 +- x/bank/simulation/msgs.go | 2 +- x/distribution/simulation/msgs.go | 2 +- x/gov/simulation/msgs.go | 2 +- x/{mock => }/simulation/account.go | 0 x/{mock => }/simulation/doc.go | 0 x/{mock => }/simulation/event_stats.go | 0 x/{mock => }/simulation/mock_tendermint.go | 0 x/{mock => }/simulation/operation.go | 0 x/{mock => }/simulation/params.go | 0 x/{mock => }/simulation/rand_util.go | 10 ---------- x/{mock => }/simulation/simulate.go | 0 x/{mock => }/simulation/transition_matrix.go | 0 x/{mock => }/simulation/util.go | 0 x/slashing/simulation/msgs.go | 2 +- x/staking/simulation/msgs.go | 2 +- 17 files changed, 7 insertions(+), 17 deletions(-) rename x/{mock => }/simulation/account.go (100%) rename x/{mock => }/simulation/doc.go (100%) rename x/{mock => }/simulation/event_stats.go (100%) rename x/{mock => }/simulation/mock_tendermint.go (100%) rename x/{mock => }/simulation/operation.go (100%) rename x/{mock => }/simulation/params.go (100%) rename x/{mock => }/simulation/rand_util.go (89%) rename x/{mock => }/simulation/simulate.go (100%) rename x/{mock => }/simulation/transition_matrix.go (100%) rename x/{mock => }/simulation/util.go (100%) diff --git a/cmd/gaia/app/sim_test.go b/cmd/gaia/app/sim_test.go index e2003c6810c7..a183579dd7f1 100644 --- a/cmd/gaia/app/sim_test.go +++ b/cmd/gaia/app/sim_test.go @@ -29,7 +29,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/gov" govsim "github.com/cosmos/cosmos-sdk/x/gov/simulation" "github.com/cosmos/cosmos-sdk/x/mint" - "github.com/cosmos/cosmos-sdk/x/mock/simulation" + "github.com/cosmos/cosmos-sdk/x/simulation" "github.com/cosmos/cosmos-sdk/x/slashing" slashingsim "github.com/cosmos/cosmos-sdk/x/slashing/simulation" "github.com/cosmos/cosmos-sdk/x/staking" diff --git a/x/auth/simulation/fake.go b/x/auth/simulation/fake.go index 4b073e7b1739..57b6887d2a83 100644 --- a/x/auth/simulation/fake.go +++ b/x/auth/simulation/fake.go @@ -9,7 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/mock/simulation" + "github.com/cosmos/cosmos-sdk/x/simulation" ) // SimulateDeductFee diff --git a/x/bank/simulation/msgs.go b/x/bank/simulation/msgs.go index 489b2ec94cd1..b31c033babeb 100644 --- a/x/bank/simulation/msgs.go +++ b/x/bank/simulation/msgs.go @@ -13,7 +13,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/mock" - "github.com/cosmos/cosmos-sdk/x/mock/simulation" + "github.com/cosmos/cosmos-sdk/x/simulation" ) // SendTx tests and runs a single msg send where both diff --git a/x/distribution/simulation/msgs.go b/x/distribution/simulation/msgs.go index ffb97971ac6e..5c822d97333b 100644 --- a/x/distribution/simulation/msgs.go +++ b/x/distribution/simulation/msgs.go @@ -8,7 +8,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/distribution" - "github.com/cosmos/cosmos-sdk/x/mock/simulation" + "github.com/cosmos/cosmos-sdk/x/simulation" ) // SimulateMsgSetWithdrawAddress diff --git a/x/gov/simulation/msgs.go b/x/gov/simulation/msgs.go index 33263c830e72..560ecc31c66e 100644 --- a/x/gov/simulation/msgs.go +++ b/x/gov/simulation/msgs.go @@ -9,7 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/gov" - "github.com/cosmos/cosmos-sdk/x/mock/simulation" + "github.com/cosmos/cosmos-sdk/x/simulation" ) // SimulateSubmittingVotingAndSlashingForProposal simulates creating a msg Submit Proposal diff --git a/x/mock/simulation/account.go b/x/simulation/account.go similarity index 100% rename from x/mock/simulation/account.go rename to x/simulation/account.go diff --git a/x/mock/simulation/doc.go b/x/simulation/doc.go similarity index 100% rename from x/mock/simulation/doc.go rename to x/simulation/doc.go diff --git a/x/mock/simulation/event_stats.go b/x/simulation/event_stats.go similarity index 100% rename from x/mock/simulation/event_stats.go rename to x/simulation/event_stats.go diff --git a/x/mock/simulation/mock_tendermint.go b/x/simulation/mock_tendermint.go similarity index 100% rename from x/mock/simulation/mock_tendermint.go rename to x/simulation/mock_tendermint.go diff --git a/x/mock/simulation/operation.go b/x/simulation/operation.go similarity index 100% rename from x/mock/simulation/operation.go rename to x/simulation/operation.go diff --git a/x/mock/simulation/params.go b/x/simulation/params.go similarity index 100% rename from x/mock/simulation/params.go rename to x/simulation/params.go diff --git a/x/mock/simulation/rand_util.go b/x/simulation/rand_util.go similarity index 89% rename from x/mock/simulation/rand_util.go rename to x/simulation/rand_util.go index 1a5e4566f652..a9b32ca48dd4 100644 --- a/x/mock/simulation/rand_util.go +++ b/x/simulation/rand_util.go @@ -6,7 +6,6 @@ import ( "time" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/mock" ) const ( @@ -66,15 +65,6 @@ func RandomDecAmount(r *rand.Rand, max sdk.Dec) sdk.Dec { return sdk.NewDecFromBigIntWithPrec(randInt, sdk.Precision) } -// RandomSetGenesis wraps mock.RandomSetGenesis, but using simulation accounts -func RandomSetGenesis(r *rand.Rand, app *mock.App, accs []Account, denoms []string) { - addrs := make([]sdk.AccAddress, len(accs)) - for i := 0; i < len(accs); i++ { - addrs[i] = accs[i].Address - } - mock.RandomSetGenesis(r, app, addrs, denoms) -} - // RandTimestamp generates a random timestamp func RandTimestamp(r *rand.Rand) time.Time { // json.Marshal breaks for timestamps greater with year greater than 9999 diff --git a/x/mock/simulation/simulate.go b/x/simulation/simulate.go similarity index 100% rename from x/mock/simulation/simulate.go rename to x/simulation/simulate.go diff --git a/x/mock/simulation/transition_matrix.go b/x/simulation/transition_matrix.go similarity index 100% rename from x/mock/simulation/transition_matrix.go rename to x/simulation/transition_matrix.go diff --git a/x/mock/simulation/util.go b/x/simulation/util.go similarity index 100% rename from x/mock/simulation/util.go rename to x/simulation/util.go diff --git a/x/slashing/simulation/msgs.go b/x/slashing/simulation/msgs.go index a7cb45cf3ce4..17bde0e24434 100644 --- a/x/slashing/simulation/msgs.go +++ b/x/slashing/simulation/msgs.go @@ -6,7 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/mock/simulation" + "github.com/cosmos/cosmos-sdk/x/simulation" "github.com/cosmos/cosmos-sdk/x/slashing" ) diff --git a/x/staking/simulation/msgs.go b/x/staking/simulation/msgs.go index 2c3ad5fcbf81..6e455495d60c 100644 --- a/x/staking/simulation/msgs.go +++ b/x/staking/simulation/msgs.go @@ -7,7 +7,7 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/mock/simulation" + "github.com/cosmos/cosmos-sdk/x/simulation" "github.com/cosmos/cosmos-sdk/x/staking" "github.com/cosmos/cosmos-sdk/x/staking/keeper" ) From b57a7718fc47609c1389f2bcefda540b4bd03249 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 26 Feb 2019 22:37:14 -0500 Subject: [PATCH 07/19] move simulation log code to log.go --- x/simulation/log.go | 75 ++++++++++++++++++++++++++++++++++++++++++++ x/simulation/util.go | 70 ----------------------------------------- 2 files changed, 75 insertions(+), 70 deletions(-) create mode 100644 x/simulation/log.go diff --git a/x/simulation/log.go b/x/simulation/log.go new file mode 100644 index 000000000000..5bf024d7fab3 --- /dev/null +++ b/x/simulation/log.go @@ -0,0 +1,75 @@ +package simulation + +import ( + "fmt" + "os" + "path" + "strings" + "time" +) + +// Builds a function to add logs for this particular block +func addLogMessage(testingmode bool, + blockLogBuilders []*strings.Builder, height int) func(string) { + + if !testingmode { + return func(_ string) {} + } + + blockLogBuilders[height] = &strings.Builder{} + return func(x string) { + (*blockLogBuilders[height]).WriteString(x) + (*blockLogBuilders[height]).WriteString("\n") + } +} + +// Creates a function to print out the logs +func logPrinter(testingmode bool, logs []*strings.Builder) func() { + if !testingmode { + return func() {} + } + + return func() { + numLoggers := 0 + for i := 0; i < len(logs); i++ { + // We're passed the last created block + if logs[i] == nil { + numLoggers = i + break + } + } + + var f *os.File + if numLoggers > 10 { + fileName := fmt.Sprintf("%s.txt", time.Now().Format("2006-01-02_15:04:05")) + + folderPath := os.ExpandEnv("$HOME/.gaiad/simulations") + filePath := path.Join(folderPath, fileName) + fmt.Printf("Too many logs to display, instead writing to %s\n", filePath) + + err := os.MkdirAll(folderPath, os.ModePerm) + if err != nil { + panic(err) + } + f, _ = os.Create(filePath) + } + + for i := 0; i < numLoggers; i++ { + if f == nil { + fmt.Printf("Begin block %d\n", i+1) + fmt.Println((*logs[i]).String()) + continue + } + + _, err := f.WriteString(fmt.Sprintf("Begin block %d\n", i+1)) + if err != nil { + panic("Failed to write logs to file") + } + + _, err = f.WriteString((*logs[i]).String()) + if err != nil { + panic("Failed to write logs to file") + } + } + } +} diff --git a/x/simulation/util.go b/x/simulation/util.go index 21e63470d709..20939ab3cdd9 100644 --- a/x/simulation/util.go +++ b/x/simulation/util.go @@ -3,11 +3,7 @@ package simulation import ( "fmt" "math/rand" - "os" - "path" - "strings" "testing" - "time" "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" @@ -40,72 +36,6 @@ func getTestingMode(tb testing.TB) (testingMode bool, t *testing.T, b *testing.B return testingMode, t, b } -// Builds a function to add logs for this particular block -func addLogMessage(testingmode bool, - blockLogBuilders []*strings.Builder, height int) func(string) { - - if !testingmode { - return func(_ string) {} - } - - blockLogBuilders[height] = &strings.Builder{} - return func(x string) { - (*blockLogBuilders[height]).WriteString(x) - (*blockLogBuilders[height]).WriteString("\n") - } -} - -// Creates a function to print out the logs -func logPrinter(testingmode bool, logs []*strings.Builder) func() { - if !testingmode { - return func() {} - } - - return func() { - numLoggers := 0 - for i := 0; i < len(logs); i++ { - // We're passed the last created block - if logs[i] == nil { - numLoggers = i - break - } - } - - var f *os.File - if numLoggers > 10 { - fileName := fmt.Sprintf("%s.txt", time.Now().Format("2006-01-02_15:04:05")) - - folderPath := os.ExpandEnv("$HOME/.gaiad/simulations") - filePath := path.Join(folderPath, fileName) - fmt.Printf("Too many logs to display, instead writing to %s\n", filePath) - - err := os.MkdirAll(folderPath, os.ModePerm) - if err != nil { - panic(err) - } - f, _ = os.Create(filePath) - } - - for i := 0; i < numLoggers; i++ { - if f == nil { - fmt.Printf("Begin block %d\n", i+1) - fmt.Println((*logs[i]).String()) - continue - } - - _, err := f.WriteString(fmt.Sprintf("Begin block %d\n", i+1)) - if err != nil { - panic("Failed to write logs to file") - } - - _, err = f.WriteString((*logs[i]).String()) - if err != nil { - panic("Failed to write logs to file") - } - } - } -} - // getBlockSize returns a block size as determined from the transition matrix. // It targets making average block size the provided parameter. The three // states it moves between are: From dea0ce1da70a3daf9c2cca7916da5bcade15305f Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 27 Feb 2019 01:56:15 -0500 Subject: [PATCH 08/19] logger overhaul int --- x/simulation/log.go | 130 +++++++++++++++++++++++--------------- x/simulation/operation.go | 71 +++++++++++++++++++++ x/simulation/simulate.go | 75 ++++++++++------------ x/simulation/util.go | 4 +- 4 files changed, 183 insertions(+), 97 deletions(-) diff --git a/x/simulation/log.go b/x/simulation/log.go index 5bf024d7fab3..50ce03624bd2 100644 --- a/x/simulation/log.go +++ b/x/simulation/log.go @@ -4,72 +4,98 @@ import ( "fmt" "os" "path" - "strings" "time" ) -// Builds a function to add logs for this particular block -func addLogMessage(testingmode bool, - blockLogBuilders []*strings.Builder, height int) func(string) { +// log writter +type LogWriter interface { + AddEntry(OperationEntry) + PrintLogs() +} +// LogWriter - return a dummy or standard log writer given the testingmode +func NewLogWriter(testingmode bool) LogWriter { if !testingmode { - return func(_ string) {} + return &DummyLogWriter{} } + return &StandardLogWriter{} +} - blockLogBuilders[height] = &strings.Builder{} - return func(x string) { - (*blockLogBuilders[height]).WriteString(x) - (*blockLogBuilders[height]).WriteString("\n") - } +// log writter +type StandardLogWriter struct { + OpEntries []OperationEntry `json:"op_entries"` } -// Creates a function to print out the logs -func logPrinter(testingmode bool, logs []*strings.Builder) func() { - if !testingmode { - return func() {} - } +// add an entry to the log writter +func (lw *StandardLogWriter) AddEntry(opEntry OperationEntry) { + lw.OpEntries = append(lw.OpEntries, opEntry) +} - return func() { - numLoggers := 0 - for i := 0; i < len(logs); i++ { - // We're passed the last created block - if logs[i] == nil { - numLoggers = i - break - } +// PrintLogs - print the logs to a simulation file +func (lw *StandardLogWriter) PrintLogs() { + f := createLogFile() + for i := 0; i < len(lw.OpEntries); i++ { + writeEntry := fmt.Sprintf("%s\n", (lw.OpEntries[i]).MustMarshal()) + _, err := f.WriteString(writeEntry) + if err != nil { + panic("Failed to write logs to file") } + } +} - var f *os.File - if numLoggers > 10 { - fileName := fmt.Sprintf("%s.txt", time.Now().Format("2006-01-02_15:04:05")) +//// Builds a function to append logs +//func getLogWriter(testingmode bool, opEntries []*OperationEntry) func(OperationEntry) { - folderPath := os.ExpandEnv("$HOME/.gaiad/simulations") - filePath := path.Join(folderPath, fileName) - fmt.Printf("Too many logs to display, instead writing to %s\n", filePath) +//if !testingmode { +//return func(_ OperationEntry) {} +//} - err := os.MkdirAll(folderPath, os.ModePerm) - if err != nil { - panic(err) - } - f, _ = os.Create(filePath) - } +//return func(opEntry OperationEntry) { +//opEntries = append(opEntries, opEntry) +//} +//} - for i := 0; i < numLoggers; i++ { - if f == nil { - fmt.Printf("Begin block %d\n", i+1) - fmt.Println((*logs[i]).String()) - continue - } - - _, err := f.WriteString(fmt.Sprintf("Begin block %d\n", i+1)) - if err != nil { - panic("Failed to write logs to file") - } - - _, err = f.WriteString((*logs[i]).String()) - if err != nil { - panic("Failed to write logs to file") - } - } +//// Creates a function to print out the logs +//func logPrinter(testingmode bool, opEntries []*OperationEntry) func() { +//if !testingmode { +//return func() {} +//} + +//return func() { +//f := createLogFile() +//_, _ = f.WriteString(fmt.Sprintf("debug opEntries: %v\n", opEntries)) +//for i := 0; i < len(opEntries); i++ { +//writeEntry := fmt.Sprintf("%s\n", (*opEntries[i]).MustMarshal()) +//_, err := f.WriteString(writeEntry) +//if err != nil { +//panic("Failed to write logs to file") +//} +//} +//} +//} + +func createLogFile() *os.File { + var f *os.File + fileName := fmt.Sprintf("%s.log", time.Now().Format("2006-01-02_15:04:05")) + + folderPath := os.ExpandEnv("$HOME/.gaiad/simulations") + filePath := path.Join(folderPath, fileName) + + err := os.MkdirAll(folderPath, os.ModePerm) + if err != nil { + panic(err) } + f, _ = os.Create(filePath) + fmt.Printf("Logs to writing to %s\n", filePath) + return f } + +//_____________________ +// dummy log writter +type DummyLogWriter struct{} + +// do nothing +func (lw *DummyLogWriter) AddEntry(_ OperationEntry) {} + +// do nothing +func (lw *DummyLogWriter) PrintLogs() {} diff --git a/x/simulation/operation.go b/x/simulation/operation.go index 86121a6f12e0..a44aa5b2b3fa 100644 --- a/x/simulation/operation.go +++ b/x/simulation/operation.go @@ -23,6 +23,68 @@ type Operation func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, event func(string)) ( OperationMsg OperationMsg, futureOps []FutureOperation, err error) +// entry kinds for use within OperationEntry +const ( + BeginBlockEntryKind = "begin_block" + EndBlockEntryKind = "end_block" + MsgEntryKind = "msg" + QueuedsgMsgEntryKind = "queued_msg" +) + +// OperationEntry - an operation entry for logging (ex. BeginBlock, EndBlock, XxxMsg, etc) +type OperationEntry struct { + EntryKind string + Height int64 + Operation json.RawMessage // typically OperationMsg +} + +// BeginBlockEntry - operation entry for begin block +func BeginBlockEntry(height int64) OperationEntry { + return OperationEntry{ + EntryKind: BeginBlockEntryKind, + Height: height, + Operation: nil, + } +} + +// EndBlockEntry - operation entry for end block +func EndBlockEntry(height int64) OperationEntry { + return OperationEntry{ + EntryKind: EndBlockEntryKind, + Height: height, + Operation: nil, + } +} + +// MsgEntry - operation entry for standard msg +func MsgEntry(height int64, opMsg OperationMsg) OperationEntry { + return OperationEntry{ + EntryKind: MsgEntryKind, + Height: height, + Operation: opMsg.MustMarshal(), + } +} + +// MsgEntry - operation entry for queued msg +func QueuedMsgEntry(height int64, opMsg OperationMsg) OperationEntry { + return OperationEntry{ + EntryKind: QueuedsgMsgEntryKind, + Height: height, + Operation: opMsg.MustMarshal(), + } +} + +// OperationEntry - log entry text for this operation entry +func (oe OperationEntry) MustMarshal() json.RawMessage { + out, err := json.Marshal(oe) + if err != nil { + panic(err) + } + return out +} + +//_____________________________________________________________________ + // OperationMsg - structure for operation output type OperationMsg struct { Route string `json:"route"` @@ -75,6 +137,15 @@ func (om OperationMsg) String() string { return string(out) } +// LogEntry - log entry text for this operation msg +func (om OperationMsg) MustMarshal() json.RawMessage { + out, err := json.Marshal(om) + if err != nil { + panic(err) + } + return out +} + // queue of operations type OperationQueue map[int][]Operation diff --git a/x/simulation/simulate.go b/x/simulation/simulate.go index e5e2c3affe7c..8bfc85b69154 100644 --- a/x/simulation/simulate.go +++ b/x/simulation/simulate.go @@ -7,7 +7,6 @@ import ( "os" "os/signal" "runtime/debug" - "strings" "syscall" "testing" "time" @@ -24,7 +23,7 @@ type AppStateFn func(r *rand.Rand, accs []Account, genesisTimestamp time.Time) ( // Simulate tests application by sending random messages. func Simulate(t *testing.T, app *baseapp.BaseApp, appStateFn AppStateFn, ops WeightedOperations, - invariants sdk.Invariants, numBlocks int, blockSize int, commit, lean bool) (bool, error) { + invariants sdk.Invariants, numBlocks, blockSize int, commit, lean bool) (bool, error) { time := time.Now().UnixNano() return SimulateFromSeed(t, app, appStateFn, time, ops, @@ -110,16 +109,13 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, // These are operations which have been queued by previous operations operationQueue := newOperationQueue() timeOperationQueue := []FutureOperation{} - var blockLogBuilders []*strings.Builder - if testingMode { - blockLogBuilders = make([]*strings.Builder, numBlocks) - } - displayLogs := logPrinter(testingMode, blockLogBuilders) + logWriter := NewLogWriter(testingMode) + blockSimulator := createBlockSimulator( testingMode, tb, t, params, eventStats.tally, invariants, ops, operationQueue, timeOperationQueue, - numBlocks, blockSize, displayLogs, lean) + numBlocks, blockSize, logWriter, lean) if !testingMode { b.ResetTimer() @@ -130,7 +126,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, fmt.Println("Panic with err\n", r) stackTrace := string(debug.Stack()) fmt.Println(stackTrace) - displayLogs() + logWriter.PrintLogs() simError = fmt.Errorf( "Simulation halted due to panic on block %d", header.Height) @@ -139,46 +135,40 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, } // TODO split up the contents of this for loop into new functions - for i := 0; i < numBlocks && !stopEarly; i++ { + for height := 1; height <= numBlocks && !stopEarly; height++ { // Log the header time for future lookup pastTimes = append(pastTimes, header.Time) pastVoteInfos = append(pastVoteInfos, request.LastCommitInfo.Votes) - // Construct log writer - logWriter := addLogMessage(testingMode, blockLogBuilders, i) - // Run the BeginBlock handler - logWriter("BeginBlock") + logWriter.AddEntry(BeginBlockEntry(int64(height))) app.BeginBlock(request) if testingMode { - assertAllInvariants(t, app, invariants, "BeginBlock", displayLogs) + assertAllInvariants(t, app, invariants, "BeginBlock", logWriter) } ctx := app.NewContext(false, header) // Run queued operations. Ignores blocksize if blocksize is too small - logWriter("Queued operations") numQueuedOpsRan := runQueuedOperations( operationQueue, int(header.Height), - tb, r, app, ctx, accs, logWriter, - displayLogs, eventStats.tally, lean) + tb, r, app, ctx, accs, logWriter, eventStats.tally, lean) numQueuedTimeOpsRan := runQueuedTimeOperations( - timeOperationQueue, header.Time, - tb, r, app, ctx, accs, - logWriter, displayLogs, eventStats.tally, lean) + timeOperationQueue, int(header.Height), header.Time, + tb, r, app, ctx, accs, logWriter, eventStats.tally, lean) if testingMode && onOperation { - assertAllInvariants(t, app, invariants, "QueuedOperations", displayLogs) + assertAllInvariants(t, app, invariants, "QueuedOperations", logWriter) } - logWriter("Standard operations") - operations := blockSimulator(r, app, ctx, accs, header, logWriter) + // run standard operations + operations := blockSimulator(r, app, ctx, accs, header) opCount += operations + numQueuedOpsRan + numQueuedTimeOpsRan if testingMode { - assertAllInvariants(t, app, invariants, "StandardOperations", displayLogs) + assertAllInvariants(t, app, invariants, "StandardOperations", logWriter) } res := app.EndBlock(abci.RequestEndBlock{}) @@ -188,10 +178,10 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, header.Time = header.Time.Add( time.Duration(int64(r.Intn(int(timeDiff)))) * time.Second) header.ProposerAddress = validators.randomProposer(r) - logWriter("EndBlock") + logWriter.AddEntry(EndBlockEntry(int64(height))) if testingMode { - assertAllInvariants(t, app, invariants, "EndBlock", displayLogs) + assertAllInvariants(t, app, invariants, "EndBlock", logWriter) } if commit { app.Commit() @@ -231,21 +221,21 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, //______________________________________________________________________________ type blockSimFn func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accounts []Account, header abci.Header, logWriter func(string)) (opCount int) + accounts []Account, header abci.Header) (opCount int) // Returns a function to simulate blocks. Written like this to avoid constant // parameters being passed everytime, to minimize memory overhead. func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params Params, event func(string), invariants sdk.Invariants, ops WeightedOperations, operationQueue OperationQueue, timeOperationQueue []FutureOperation, - totalNumBlocks int, avgBlockSize int, displayLogs func(), lean bool) blockSimFn { + totalNumBlocks, avgBlockSize int, logWriter LogWriter, lean bool) blockSimFn { - var lastBlocksizeState = 0 // state for [4 * uniform distribution] - var blocksize int + lastBlocksizeState := 0 // state for [4 * uniform distribution] + blocksize := 0 selectOp := ops.getSelectOpFn() return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accounts []Account, header abci.Header, logWriter func(string)) (opCount int) { + accounts []Account, header abci.Header) (opCount int) { fmt.Printf("\rSimulating... block %d/%d, operation %d/%d. ", header.Height, totalNumBlocks, opCount, blocksize) @@ -271,10 +261,10 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params op, r2 := opAndR.op, opAndR.rand opMsg, futureOps, err := op(r2, app, ctx, accounts, event) if !lean || (opMsg.OK && lean) { - logWriter(opMsg.String()) + logWriter.AddEntry(MsgEntry(header.Height, opMsg)) } if err != nil { - displayLogs() + logWriter.PrintLogs() tb.Fatalf("error on operation %d within block %d, %v", header.Height, opCount, err) } @@ -283,7 +273,7 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params if testingMode { if onOperation { eventStr := fmt.Sprintf("operation: %v", opMsg.String()) - assertAllInvariants(t, app, invariants, eventStr, displayLogs) + assertAllInvariants(t, app, invariants, eventStr, logWriter) } if opCount%50 == 0 { fmt.Printf("\rSimulating... block %d/%d, operation %d/%d. ", @@ -299,8 +289,7 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params // nolint: errcheck func runQueuedOperations(queueOps map[int][]Operation, height int, tb testing.TB, r *rand.Rand, app *baseapp.BaseApp, - ctx sdk.Context, accounts []Account, logWriter func(string), - displayLogs func(), tallyEvent func(string), lean bool) (numOpsRan int) { + ctx sdk.Context, accounts []Account, logWriter LogWriter, tallyEvent func(string), lean bool) (numOpsRan int) { queuedOp, ok := queueOps[height] if !ok { @@ -315,10 +304,10 @@ func runQueuedOperations(queueOps map[int][]Operation, // be changed. opMsg, _, err := queuedOp[i](r, app, ctx, accounts, tallyEvent) if !lean || (opMsg.OK && lean) { - logWriter(opMsg.String()) + logWriter.AddEntry((QueuedMsgEntry(int64(height), opMsg))) } if err != nil { - displayLogs() + logWriter.PrintLogs() tb.FailNow() } } @@ -327,9 +316,9 @@ func runQueuedOperations(queueOps map[int][]Operation, } func runQueuedTimeOperations(queueOps []FutureOperation, - currentTime time.Time, tb testing.TB, r *rand.Rand, + height int, currentTime time.Time, tb testing.TB, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, - logWriter func(string), displayLogs func(), tallyEvent func(string), lean bool) (numOpsRan int) { + logWriter LogWriter, tallyEvent func(string), lean bool) (numOpsRan int) { numOpsRan = 0 for len(queueOps) > 0 && currentTime.After(queueOps[0].BlockTime) { @@ -339,10 +328,10 @@ func runQueuedTimeOperations(queueOps []FutureOperation, // be changed. opMsg, _, err := queueOps[0].Op(r, app, ctx, accounts, tallyEvent) if !lean || (opMsg.OK && lean) { - logWriter(opMsg.String()) + logWriter.AddEntry(QueuedMsgEntry(int64(height), opMsg)) } if err != nil { - displayLogs() + logWriter.PrintLogs() tb.FailNow() } diff --git a/x/simulation/util.go b/x/simulation/util.go index 20939ab3cdd9..14df0f6c648c 100644 --- a/x/simulation/util.go +++ b/x/simulation/util.go @@ -12,14 +12,14 @@ import ( // assertAll asserts the all invariants against application state func assertAllInvariants(t *testing.T, app *baseapp.BaseApp, invs sdk.Invariants, - event string, displayLogs func()) { + event string, logWriter LogWriter) { ctx := app.NewContext(false, abci.Header{Height: app.LastBlockHeight() + 1}) for i := 0; i < len(invs); i++ { if err := invs[i](ctx); err != nil { fmt.Printf("Invariants broken after %s\n%s\n", event, err.Error()) - displayLogs() + logWriter.PrintLogs() t.Fatal() } } From 9add266df75c86724992d05577d1226ce05f0b5d Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 6 Mar 2019 22:27:56 -0500 Subject: [PATCH 09/19] distribution comments --- x/distribution/abci_app.go | 8 ++--- x/distribution/keeper/allocation.go | 45 ++++++++++++++--------------- x/simulation/operation.go | 13 ++++++--- x/simulation/simulate.go | 2 +- 4 files changed, 36 insertions(+), 32 deletions(-) diff --git a/x/distribution/abci_app.go b/x/distribution/abci_app.go index aae399062320..0a2ef5627a90 100644 --- a/x/distribution/abci_app.go +++ b/x/distribution/abci_app.go @@ -11,11 +11,11 @@ import ( func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) { // determine the total power signing the block - var totalPower, sumPrecommitPower int64 + var previousTotalPower, sumPreviousPrecommitPower int64 for _, voteInfo := range req.LastCommitInfo.GetVotes() { - totalPower += voteInfo.Validator.Power + previousTotalPower += voteInfo.Validator.Power if voteInfo.SignedLastBlock { - sumPrecommitPower += voteInfo.Validator.Power + sumPreviousPrecommitPower += voteInfo.Validator.Power } } @@ -23,7 +23,7 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) // ref https://github.com/cosmos/cosmos-sdk/issues/3095 if ctx.BlockHeight() > 1 { previousProposer := k.GetPreviousProposerConsAddr(ctx) - k.AllocateTokens(ctx, sumPrecommitPower, totalPower, previousProposer, req.LastCommitInfo.GetVotes()) + k.AllocateTokens(ctx, sumPreviousPrecommitPower, previousTotalPower, previousProposer, req.LastCommitInfo.GetVotes()) } // record the proposer for when we payout on the next block diff --git a/x/distribution/keeper/allocation.go b/x/distribution/keeper/allocation.go index 50dcd2e93d79..bdc93bc87faa 100644 --- a/x/distribution/keeper/allocation.go +++ b/x/distribution/keeper/allocation.go @@ -9,67 +9,66 @@ import ( ) // allocate fees handles distribution of the collected fees -func (k Keeper) AllocateTokens(ctx sdk.Context, sumPrecommitPower, totalPower int64, proposer sdk.ConsAddress, votes []abci.VoteInfo) { - logger := ctx.Logger().With("module", "x/distribution") +func (k Keeper) AllocateTokens(ctx sdk.Context, sumPreviousPrecommitPower, totalPreviousPower int64, + previousProposer sdk.ConsAddress, previousVotes []abci.VoteInfo) { - // fetch collected fees & fee pool + // fetch and clear the collected fees for distribution, since this is + // called in BeginBlock, collected fees will be from the previous block + // (and distributed to the previous proposer) feesCollectedInt := k.feeCollectionKeeper.GetCollectedFees(ctx) feesCollected := sdk.NewDecCoins(feesCollectedInt) - feePool := k.GetFeePool(ctx) - - // clear collected fees, which will now be distributed k.feeCollectionKeeper.ClearCollectedFees(ctx) // temporary workaround to keep CanWithdrawInvariant happy // general discussions here: https://github.com/cosmos/cosmos-sdk/issues/2906#issuecomment-441867634 - if totalPower == 0 { + feePool := k.GetFeePool(ctx) + if totalPreviousPower == 0 { feePool.CommunityPool = feePool.CommunityPool.Add(feesCollected) k.SetFeePool(ctx, feePool) return } // calculate fraction votes - fractionVotes := sdk.NewDec(sumPrecommitPower).Quo(sdk.NewDec(totalPower)) + previousFractionVotes := sdk.NewDec(sumPreviousPrecommitPower).Quo(sdk.NewDec(totalPreviousPower)) - // calculate proposer reward + // calculate previous proposer reward baseProposerReward := k.GetBaseProposerReward(ctx) bonusProposerReward := k.GetBonusProposerReward(ctx) - proposerMultiplier := baseProposerReward.Add(bonusProposerReward.MulTruncate(fractionVotes)) + proposerMultiplier := baseProposerReward.Add(bonusProposerReward.MulTruncate(previousFractionVotes)) proposerReward := feesCollected.MulDecTruncate(proposerMultiplier) + fmt.Printf("\ndebug proposerReward: %v\n", proposerReward) - // pay proposer + // pay previous proposer remaining := feesCollected - proposerValidator := k.stakingKeeper.ValidatorByConsAddr(ctx, proposer) + proposerValidator := k.stakingKeeper.ValidatorByConsAddr(ctx, previousProposer) + fmt.Printf("debug proposerValidator: %v\n", proposerValidator) + if proposerValidator != nil { k.AllocateTokensToValidator(ctx, proposerValidator, proposerReward) remaining = remaining.Sub(proposerReward) } else { - // proposer can be unknown if say, the unbonding period is 1 block, so + // previous proposer can be unknown if say, the unbonding period is 1 block, so // e.g. a validator undelegates at block X, it's removed entirely by // block X+1's endblock, then X+2 we need to refer to the previous // proposer for X+1, but we've forgotten about them. - logger.Error(fmt.Sprintf( - "WARNING: Attempt to allocate proposer rewards to unknown proposer %s. "+ - "This should happen only if the proposer unbonded completely within a single block, "+ - "which generally should not happen except in exceptional circumstances (or fuzz testing). "+ - "We recommend you investigate immediately.", - proposer.String())) } // calculate fraction allocated to validators communityTax := k.GetCommunityTax(ctx) voteMultiplier := sdk.OneDec().Sub(proposerMultiplier).Sub(communityTax) + fmt.Printf("debug communityTax: %v\n", communityTax) + fmt.Printf("debug proposerMultiplier: %v\n", proposerMultiplier) + fmt.Printf("debug voteMultiplier: %v\n", voteMultiplier) // allocate tokens proportionally to voting power // TODO consider parallelizing later, ref https://github.com/cosmos/cosmos-sdk/pull/3099#discussion_r246276376 - for _, vote := range votes { + for _, vote := range previousVotes { validator := k.stakingKeeper.ValidatorByConsAddr(ctx, vote.Validator.Address) - // TODO consider microslashing for missing votes. + // TODO likely we should only reward validators who actually signed the block. // ref https://github.com/cosmos/cosmos-sdk/issues/2525#issuecomment-430838701 - powerFraction := sdk.NewDec(vote.Validator.Power).QuoTruncate(sdk.NewDec(totalPower)) + powerFraction := sdk.NewDec(vote.Validator.Power).QuoTruncate(sdk.NewDec(totalPreviousPower)) reward := feesCollected.MulDecTruncate(voteMultiplier).MulDecTruncate(powerFraction) - reward = reward.Intersect(remaining) k.AllocateTokensToValidator(ctx, validator, reward) remaining = remaining.Sub(reward) } diff --git a/x/simulation/operation.go b/x/simulation/operation.go index a44aa5b2b3fa..86f599ae3508 100644 --- a/x/simulation/operation.go +++ b/x/simulation/operation.go @@ -33,9 +33,10 @@ const ( // OperationEntry - an operation entry for logging (ex. BeginBlock, EndBlock, XxxMsg, etc) type OperationEntry struct { - EntryKind string - Height int64 - Operation json.RawMessage // typically OperationMsg + EntryKind string `json:"entry_kind"` + Height int64 `json:"height"` + Order int64 `json:"order"` + Operation json.RawMessage `json:"operation"` } // BeginBlockEntry - operation entry for begin block @@ -43,6 +44,7 @@ func BeginBlockEntry(height int64) OperationEntry { return OperationEntry{ EntryKind: BeginBlockEntryKind, Height: height, + Order: -1, Operation: nil, } } @@ -52,15 +54,17 @@ func EndBlockEntry(height int64) OperationEntry { return OperationEntry{ EntryKind: EndBlockEntryKind, Height: height, + Order: -1, Operation: nil, } } // MsgEntry - operation entry for standard msg -func MsgEntry(height int64, opMsg OperationMsg) OperationEntry { +func MsgEntry(height int64, opMsg OperationMsg, order int64) OperationEntry { return OperationEntry{ EntryKind: MsgEntryKind, Height: height, + Order: order, Operation: opMsg.MustMarshal(), } } @@ -70,6 +74,7 @@ func QueuedMsgEntry(height int64, opMsg OperationMsg) OperationEntry { return OperationEntry{ EntryKind: QueuedsgMsgEntryKind, Height: height, + Order: -1, Operation: opMsg.MustMarshal(), } } diff --git a/x/simulation/simulate.go b/x/simulation/simulate.go index 8bfc85b69154..5e4a82f3f278 100644 --- a/x/simulation/simulate.go +++ b/x/simulation/simulate.go @@ -261,7 +261,7 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params op, r2 := opAndR.op, opAndR.rand opMsg, futureOps, err := op(r2, app, ctx, accounts, event) if !lean || (opMsg.OK && lean) { - logWriter.AddEntry(MsgEntry(header.Height, opMsg)) + logWriter.AddEntry(MsgEntry(header.Height, opMsg, int64(i))) } if err != nil { logWriter.PrintLogs() From 81f28ba0676247d2bdc67766a656b0a47f28ce99 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 6 Mar 2019 22:45:01 -0500 Subject: [PATCH 10/19] fix compiling --- x/staking/simulation/msgs.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/staking/simulation/msgs.go b/x/staking/simulation/msgs.go index 6e455495d60c..90c8eaff8378 100644 --- a/x/staking/simulation/msgs.go +++ b/x/staking/simulation/msgs.go @@ -77,7 +77,7 @@ func SimulateMsgEditValidator(k staking.Keeper) simulation.Operation { } if len(k.GetAllValidators(ctx)) == 0 { - return noOperation, nil, nil + return simulation.NoOpMsg(), nil, nil } val := keeper.RandomValidator(r, k, ctx) address := val.GetOperator() @@ -108,7 +108,7 @@ func SimulateMsgDelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Oper denom := k.GetParams(ctx).BondDenom if len(k.GetAllValidators(ctx)) == 0 { - return noOperation, nil, nil + return simulation.NoOpMsg(), nil, nil } val := keeper.RandomValidator(r, k, ctx) validatorAddress := val.GetOperator() @@ -187,7 +187,7 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulati denom := k.GetParams(ctx).BondDenom if len(k.GetAllValidators(ctx)) == 0 { - return noOperation, nil, nil + return simulation.NoOpMsg(), nil, nil } srcVal := keeper.RandomValidator(r, k, ctx) srcValidatorAddress := srcVal.GetOperator() From e1d80fb8448831ef0e03f076e3756b2e3767b1de Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 7 Mar 2019 00:39:24 -0500 Subject: [PATCH 11/19] cleanup modifications to x/distribution/keeper/allocation.go int int int --- x/distribution/keeper/allocation.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/x/distribution/keeper/allocation.go b/x/distribution/keeper/allocation.go index bdc93bc87faa..df5be3b9234b 100644 --- a/x/distribution/keeper/allocation.go +++ b/x/distribution/keeper/allocation.go @@ -12,6 +12,8 @@ import ( func (k Keeper) AllocateTokens(ctx sdk.Context, sumPreviousPrecommitPower, totalPreviousPower int64, previousProposer sdk.ConsAddress, previousVotes []abci.VoteInfo) { + logger := ctx.Logger().With("module", "x/distribution") + // fetch and clear the collected fees for distribution, since this is // called in BeginBlock, collected fees will be from the previous block // (and distributed to the previous proposer) @@ -36,12 +38,10 @@ func (k Keeper) AllocateTokens(ctx sdk.Context, sumPreviousPrecommitPower, total bonusProposerReward := k.GetBonusProposerReward(ctx) proposerMultiplier := baseProposerReward.Add(bonusProposerReward.MulTruncate(previousFractionVotes)) proposerReward := feesCollected.MulDecTruncate(proposerMultiplier) - fmt.Printf("\ndebug proposerReward: %v\n", proposerReward) // pay previous proposer remaining := feesCollected proposerValidator := k.stakingKeeper.ValidatorByConsAddr(ctx, previousProposer) - fmt.Printf("debug proposerValidator: %v\n", proposerValidator) if proposerValidator != nil { k.AllocateTokensToValidator(ctx, proposerValidator, proposerReward) @@ -51,21 +51,24 @@ func (k Keeper) AllocateTokens(ctx sdk.Context, sumPreviousPrecommitPower, total // e.g. a validator undelegates at block X, it's removed entirely by // block X+1's endblock, then X+2 we need to refer to the previous // proposer for X+1, but we've forgotten about them. + logger.Error(fmt.Sprintf( + "WARNING: Attempt to allocate proposer rewards to unknown proposer %s. "+ + "This should happen only if the proposer unbonded completely within a single block, "+ + "which generally should not happen except in exceptional circumstances (or fuzz testing). "+ + "We recommend you investigate immediately.", + previousProposer.String())) } // calculate fraction allocated to validators communityTax := k.GetCommunityTax(ctx) voteMultiplier := sdk.OneDec().Sub(proposerMultiplier).Sub(communityTax) - fmt.Printf("debug communityTax: %v\n", communityTax) - fmt.Printf("debug proposerMultiplier: %v\n", proposerMultiplier) - fmt.Printf("debug voteMultiplier: %v\n", voteMultiplier) // allocate tokens proportionally to voting power // TODO consider parallelizing later, ref https://github.com/cosmos/cosmos-sdk/pull/3099#discussion_r246276376 for _, vote := range previousVotes { validator := k.stakingKeeper.ValidatorByConsAddr(ctx, vote.Validator.Address) - // TODO likely we should only reward validators who actually signed the block. + // TODO consider microslashing for missing votes. // ref https://github.com/cosmos/cosmos-sdk/issues/2525#issuecomment-430838701 powerFraction := sdk.NewDec(vote.Validator.Power).QuoTruncate(sdk.NewDec(totalPreviousPower)) reward := feesCollected.MulDecTruncate(voteMultiplier).MulDecTruncate(powerFraction) From 3f8e5a39beb43e1b3bfde89b0f10c4635d34721a Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 7 Mar 2019 00:45:00 -0500 Subject: [PATCH 12/19] gov bug --- x/gov/simulation/msgs.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/x/gov/simulation/msgs.go b/x/gov/simulation/msgs.go index 560ecc31c66e..f8a2358cd105 100644 --- a/x/gov/simulation/msgs.go +++ b/x/gov/simulation/msgs.go @@ -96,8 +96,7 @@ func SimulateMsgSubmitProposal(k gov.Keeper) simulation.Operation { func simulateHandleMsgSubmitProposal(msg gov.MsgSubmitProposal, handler sdk.Handler, ctx sdk.Context, event func(string)) (ok bool) { ctx, write := ctx.CacheContext() - result := handler(ctx, msg) - ok = result.IsOK() + ok = handler(ctx, msg).IsOK() if ok { write() } @@ -136,11 +135,10 @@ func SimulateMsgDeposit(k gov.Keeper) simulation.Operation { return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() - result := gov.NewHandler(k)(ctx, msg) - if result.IsOK() { + ok = gov.NewHandler(k)(ctx, msg).IsOK() + if ok { write() } - ok = gov.NewHandler(k)(ctx, msg).IsOK() event(fmt.Sprintf("gov/MsgDeposit/%v", ok)) opMsg = simulation.NewOperationMsg(msg, ok, "") From 95b6b8b6c98b14f92a28d435cf5d89a0143b2d6e Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 7 Mar 2019 01:00:10 -0500 Subject: [PATCH 13/19] result.IsOK() minimization --- x/slashing/simulation/msgs.go | 8 +++---- x/staking/simulation/msgs.go | 40 +++++++++++++++++------------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/x/slashing/simulation/msgs.go b/x/slashing/simulation/msgs.go index 17bde0e24434..5b3c243734f8 100644 --- a/x/slashing/simulation/msgs.go +++ b/x/slashing/simulation/msgs.go @@ -23,12 +23,12 @@ func SimulateMsgUnjail(k slashing.Keeper) simulation.Operation { return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() - result := slashing.NewHandler(k)(ctx, msg) - if result.IsOK() { + ok := slashing.NewHandler(k)(ctx, msg).IsOK() + if ok { write() } - event(fmt.Sprintf("slashing/MsgUnjail/%v", result.IsOK())) - opMsg = simulation.NewOperationMsg(msg, result.IsOK(), "") + event(fmt.Sprintf("slashing/MsgUnjail/%v", ok)) + opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } } diff --git a/x/staking/simulation/msgs.go b/x/staking/simulation/msgs.go index 90c8eaff8378..f67a462a63d7 100644 --- a/x/staking/simulation/msgs.go +++ b/x/staking/simulation/msgs.go @@ -51,13 +51,13 @@ func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulati } ctx, write := ctx.CacheContext() - result := handler(ctx, msg) - if result.IsOK() { + ok := handler(ctx, msg).IsOK() + if ok { write() } - event(fmt.Sprintf("staking/MsgCreateValidator/%v", result.IsOK())) - opMsg = simulation.NewOperationMsg(msg, result.IsOK(), "") + event(fmt.Sprintf("staking/MsgCreateValidator/%v", ok)) + opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } } @@ -89,12 +89,12 @@ func SimulateMsgEditValidator(k staking.Keeper) simulation.Operation { return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() - result := handler(ctx, msg) - if result.IsOK() { + ok := handler(ctx, msg).IsOK() + if ok { write() } - event(fmt.Sprintf("staking/MsgEditValidator/%v", result.IsOK())) - opMsg = simulation.NewOperationMsg(msg, result.IsOK(), "") + event(fmt.Sprintf("staking/MsgEditValidator/%v", ok)) + opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } } @@ -129,12 +129,12 @@ func SimulateMsgDelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Oper return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() - result := handler(ctx, msg) - if result.IsOK() { + ok := handler(ctx, msg).IsOK() + if ok { write() } - event(fmt.Sprintf("staking/MsgDelegate/%v", result.IsOK())) - opMsg = simulation.NewOperationMsg(msg, result.IsOK(), "") + event(fmt.Sprintf("staking/MsgDelegate/%v", ok)) + opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } } @@ -168,12 +168,12 @@ func SimulateMsgUndelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Op msg.GetSignBytes(), msg.ValidateBasic()) } ctx, write := ctx.CacheContext() - result := handler(ctx, msg) - if result.IsOK() { + ok := handler(ctx, msg).IsOK() + if ok { write() } - event(fmt.Sprintf("staking/MsgUndelegate/%v", result.IsOK())) - opMsg = simulation.NewOperationMsg(msg, result.IsOK(), "") + event(fmt.Sprintf("staking/MsgUndelegate/%v", ok)) + opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } } @@ -213,12 +213,12 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulati return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } ctx, write := ctx.CacheContext() - result := handler(ctx, msg) - if result.IsOK() { + ok := handler(ctx, msg).IsOK() + if ok { write() } - event(fmt.Sprintf("staking/MsgBeginRedelegate/%v", result.IsOK())) - opMsg = simulation.NewOperationMsg(msg, result.IsOK(), "") + event(fmt.Sprintf("staking/MsgBeginRedelegate/%v", ok)) + opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } } From bfeb0de4ba9d3ce45de243e22903a5c9c2218720 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 7 Mar 2019 01:14:23 -0500 Subject: [PATCH 14/19] importExport typo bug --- cmd/gaia/app/sim_test.go | 2 +- x/gov/keeper.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/gaia/app/sim_test.go b/cmd/gaia/app/sim_test.go index a183579dd7f1..a19d6418d92d 100644 --- a/cmd/gaia/app/sim_test.go +++ b/cmd/gaia/app/sim_test.go @@ -523,7 +523,7 @@ func TestGaiaSimulationAfterImport(t *testing.T) { }) // Run randomized simulation on imported app - _, err = simulation.SimulateFromSeed(getSimulateFromSeedInput(t, app)) + _, err = simulation.SimulateFromSeed(getSimulateFromSeedInput(t, newApp)) require.Nil(t, err) } diff --git a/x/gov/keeper.go b/x/gov/keeper.go index 2ea5079774f4..5bb8869a5361 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -1,6 +1,7 @@ package gov import ( + "fmt" "time" codec "github.com/cosmos/cosmos-sdk/codec" @@ -203,6 +204,9 @@ func (keeper Keeper) setInitialProposalID(ctx sdk.Context, proposalID uint64) sd store := ctx.KVStore(keeper.storeKey) bz := store.Get(KeyNextProposalID) if bz != nil { + var proposalID uint64 + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalID) + fmt.Printf("debug proposalID: %v\n", proposalID) return ErrInvalidGenesis(keeper.codespace, "Initial ProposalID already set") } bz = keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID) From 3d9498751205e91e3e5212f9553d06cc8c800d94 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 7 Mar 2019 01:17:33 -0500 Subject: [PATCH 15/19] pending --- PENDING.md | 1 + 1 file changed, 1 insertion(+) diff --git a/PENDING.md b/PENDING.md index e39b15537c6d..dee9e602be38 100644 --- a/PENDING.md +++ b/PENDING.md @@ -102,6 +102,7 @@ CLI flag. * [\#3694] Push tagged docker images on docker hub when tag is created. * [\#3716] Update file permissions the client keys directory and contents to `0700`. * [\#3681](https://github.com/cosmos/cosmos-sdk/issues/3681) Migrate ledger-cosmos-go from ZondaX to Cosmos organization +* \#3819 simulation refactor, log output now stored in ~/.gaiad/simulation/ ### Tendermint From 640623400d9b76504e5cb40b9d0e324e7ed0698d Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Mon, 11 Mar 2019 13:46:52 -0400 Subject: [PATCH 16/19] address @alexanderbez comments --- x/simulation/log.go | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/x/simulation/log.go b/x/simulation/log.go index 50ce03624bd2..9bdb76fded8c 100644 --- a/x/simulation/log.go +++ b/x/simulation/log.go @@ -43,37 +43,6 @@ func (lw *StandardLogWriter) PrintLogs() { } } -//// Builds a function to append logs -//func getLogWriter(testingmode bool, opEntries []*OperationEntry) func(OperationEntry) { - -//if !testingmode { -//return func(_ OperationEntry) {} -//} - -//return func(opEntry OperationEntry) { -//opEntries = append(opEntries, opEntry) -//} -//} - -//// Creates a function to print out the logs -//func logPrinter(testingmode bool, opEntries []*OperationEntry) func() { -//if !testingmode { -//return func() {} -//} - -//return func() { -//f := createLogFile() -//_, _ = f.WriteString(fmt.Sprintf("debug opEntries: %v\n", opEntries)) -//for i := 0; i < len(opEntries); i++ { -//writeEntry := fmt.Sprintf("%s\n", (*opEntries[i]).MustMarshal()) -//_, err := f.WriteString(writeEntry) -//if err != nil { -//panic("Failed to write logs to file") -//} -//} -//} -//} - func createLogFile() *os.File { var f *os.File fileName := fmt.Sprintf("%s.log", time.Now().Format("2006-01-02_15:04:05")) From c52264a614092b97091aa70e6c5a800921da9519 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Mon, 11 Mar 2019 13:55:27 -0400 Subject: [PATCH 17/19] simple @cwgoes comments addressed --- PENDING.md | 8 +++++++- x/gov/keeper.go | 4 ---- x/simulation/simulate.go | 6 +++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/PENDING.md b/PENDING.md index dee9e602be38..5eb58568154f 100644 --- a/PENDING.md +++ b/PENDING.md @@ -102,7 +102,13 @@ CLI flag. * [\#3694] Push tagged docker images on docker hub when tag is created. * [\#3716] Update file permissions the client keys directory and contents to `0700`. * [\#3681](https://github.com/cosmos/cosmos-sdk/issues/3681) Migrate ledger-cosmos-go from ZondaX to Cosmos organization -* \#3819 simulation refactor, log output now stored in ~/.gaiad/simulation/ +* \#3819 Simulation refactor, log output now stored in ~/.gaiad/simulation/ + * Simulation moved to its own module (not a part of mock) + * Logger type instead of passing function variables everywhere + * Logger json output (for reloadable simulation running) + * Cleanup bank simulation messages / remove dup code in bank simulation + * Simulations saved in `~/.gaiad/simulations/` + * "Lean" simulation output option to exclude No-ops and !ok functions (`--SimulationLean` flag) ### Tendermint diff --git a/x/gov/keeper.go b/x/gov/keeper.go index 5bb8869a5361..2ea5079774f4 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -1,7 +1,6 @@ package gov import ( - "fmt" "time" codec "github.com/cosmos/cosmos-sdk/codec" @@ -204,9 +203,6 @@ func (keeper Keeper) setInitialProposalID(ctx sdk.Context, proposalID uint64) sd store := ctx.KVStore(keeper.storeKey) bz := store.Get(KeyNextProposalID) if bz != nil { - var proposalID uint64 - keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalID) - fmt.Printf("debug proposalID: %v\n", proposalID) return ErrInvalidGenesis(keeper.codespace, "Initial ProposalID already set") } bz = keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID) diff --git a/x/simulation/simulate.go b/x/simulation/simulate.go index 5e4a82f3f278..b79a01ff8e5b 100644 --- a/x/simulation/simulate.go +++ b/x/simulation/simulate.go @@ -260,7 +260,7 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params opAndR := opAndRz[i] op, r2 := opAndR.op, opAndR.rand opMsg, futureOps, err := op(r2, app, ctx, accounts, event) - if !lean || (opMsg.OK && lean) { + if !lean || opMsg.OK { logWriter.AddEntry(MsgEntry(header.Height, opMsg, int64(i))) } if err != nil { @@ -303,7 +303,7 @@ func runQueuedOperations(queueOps map[int][]Operation, // If a need arises for us to support queued messages to queue more messages, this can // be changed. opMsg, _, err := queuedOp[i](r, app, ctx, accounts, tallyEvent) - if !lean || (opMsg.OK && lean) { + if !lean || opMsg.OK { logWriter.AddEntry((QueuedMsgEntry(int64(height), opMsg))) } if err != nil { @@ -327,7 +327,7 @@ func runQueuedTimeOperations(queueOps []FutureOperation, // If a need arises for us to support queued messages to queue more messages, this can // be changed. opMsg, _, err := queueOps[0].Op(r, app, ctx, accounts, tallyEvent) - if !lean || (opMsg.OK && lean) { + if !lean || opMsg.OK { logWriter.AddEntry(QueuedMsgEntry(int64(height), opMsg)) } if err != nil { From 1ac01876ab9055a0f8cb4a21b2402ae7e5b4ce72 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Mon, 11 Mar 2019 14:35:00 -0400 Subject: [PATCH 18/19] event logging unified approach --- x/auth/simulation/fake.go | 8 +------- x/bank/simulation/msgs.go | 8 ++------ x/distribution/simulation/msgs.go | 12 +++--------- x/gov/simulation/msgs.go | 17 +++++++---------- x/simulation/operation.go | 18 ++++++++++++++---- x/simulation/simulate.go | 9 ++++++--- x/slashing/simulation/msgs.go | 4 +--- x/staking/simulation/msgs.go | 19 +++++-------------- 8 files changed, 39 insertions(+), 56 deletions(-) diff --git a/x/auth/simulation/fake.go b/x/auth/simulation/fake.go index 57b6887d2a83..7849bd807848 100644 --- a/x/auth/simulation/fake.go +++ b/x/auth/simulation/fake.go @@ -2,7 +2,6 @@ package simulation import ( "errors" - "fmt" "math/big" "math/rand" @@ -15,7 +14,7 @@ import ( // SimulateDeductFee func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulation.Operation { return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accs []simulation.Account, event func(string)) ( + accs []simulation.Account) ( opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { account := simulation.RandomAcc(r, accs) @@ -24,7 +23,6 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat opMsg = simulation.NewOperationMsgBasic("auth", "deduct_fee", "", false, nil) if len(initCoins) == 0 { - event(fmt.Sprintf("auth/SimulateDeductFee/false")) return opMsg, nil, nil } @@ -33,7 +31,6 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat amt, err := randPositiveInt(r, randCoin.Amount) if err != nil { - event(fmt.Sprintf("auth/SimulateDeductFee/false")) return opMsg, nil, nil } @@ -42,14 +39,12 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat fees := sdk.Coins{sdk.NewCoin(randCoin.Denom, amt)} spendableCoins := stored.SpendableCoins(ctx.BlockHeader().Time) if _, hasNeg := spendableCoins.SafeSub(fees); hasNeg { - event(fmt.Sprintf("auth/SimulateDeductFee/false")) return opMsg, nil, nil } // get the new account balance newCoins, hasNeg := initCoins.SafeSub(fees) if hasNeg { - event(fmt.Sprintf("auth/SimulateDeductFee/false")) return opMsg, nil, nil } @@ -59,7 +54,6 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat m.SetAccount(ctx, stored) f.AddCollectedFees(ctx, fees) - event(fmt.Sprintf("auth/SimulateDeductFee/true")) opMsg.OK = true return opMsg, nil, nil diff --git a/x/bank/simulation/msgs.go b/x/bank/simulation/msgs.go index b31c033babeb..69ca7f99e8a8 100644 --- a/x/bank/simulation/msgs.go +++ b/x/bank/simulation/msgs.go @@ -20,7 +20,7 @@ import ( // accounts already exist. func SimulateMsgSend(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Operation { handler := bank.NewHandler(bk) - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) ( opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { fromAcc, comment, msg, ok := createMsgSend(r, ctx, accs, mapper) @@ -32,7 +32,6 @@ func SimulateMsgSend(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Opera if err != nil { return opMsg, nil, err } - event("bank/sendAndVerifyTxSend/ok") return opMsg, nil, nil } } @@ -115,7 +114,7 @@ func sendAndVerifyMsgSend(app *baseapp.BaseApp, mapper auth.AccountKeeper, msg b // accounts already exist. func SimulateSingleInputMsgMultiSend(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Operation { handler := bank.NewHandler(bk) - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) ( opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { fromAcc, comment, msg, ok := createSingleInputMsgMultiSend(r, ctx, accs, mapper) @@ -127,9 +126,6 @@ func SimulateSingleInputMsgMultiSend(mapper auth.AccountKeeper, bk bank.Keeper) if err != nil { return opMsg, nil, err } - event("bank/sendAndVerifyMsgMultiSend/ok") - - opMsg = simulation.NewOperationMsg(msg, ok, comment) return opMsg, nil, nil } } diff --git a/x/distribution/simulation/msgs.go b/x/distribution/simulation/msgs.go index 5c822d97333b..ba610343dacd 100644 --- a/x/distribution/simulation/msgs.go +++ b/x/distribution/simulation/msgs.go @@ -15,8 +15,7 @@ import ( func SimulateMsgSetWithdrawAddress(m auth.AccountKeeper, k distribution.Keeper) simulation.Operation { handler := distribution.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accs []simulation.Account, event func(string)) ( - opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { + accs []simulation.Account) (opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { accountOrigin := simulation.RandomAcc(r, accs) accountDestination := simulation.RandomAcc(r, accs) @@ -32,7 +31,6 @@ func SimulateMsgSetWithdrawAddress(m auth.AccountKeeper, k distribution.Keeper) write() } - event(fmt.Sprintf("distribution/MsgSetWithdrawAddress/%v", ok)) opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } @@ -42,8 +40,7 @@ func SimulateMsgSetWithdrawAddress(m auth.AccountKeeper, k distribution.Keeper) func SimulateMsgWithdrawDelegatorReward(m auth.AccountKeeper, k distribution.Keeper) simulation.Operation { handler := distribution.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accs []simulation.Account, event func(string)) ( - opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { + accs []simulation.Account) (opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { delegatorAccount := simulation.RandomAcc(r, accs) validatorAccount := simulation.RandomAcc(r, accs) @@ -59,7 +56,6 @@ func SimulateMsgWithdrawDelegatorReward(m auth.AccountKeeper, k distribution.Kee write() } - event(fmt.Sprintf("distribution/MsgWithdrawDelegatorReward/%v", ok)) opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } @@ -69,8 +65,7 @@ func SimulateMsgWithdrawDelegatorReward(m auth.AccountKeeper, k distribution.Kee func SimulateMsgWithdrawValidatorCommission(m auth.AccountKeeper, k distribution.Keeper) simulation.Operation { handler := distribution.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accs []simulation.Account, event func(string)) ( - opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { + accs []simulation.Account) (opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { account := simulation.RandomAcc(r, accs) msg := distribution.NewMsgWithdrawValidatorCommission(sdk.ValAddress(account.Address)) @@ -85,7 +80,6 @@ func SimulateMsgWithdrawValidatorCommission(m auth.AccountKeeper, k distribution write() } - event(fmt.Sprintf("distribution/MsgWithdrawValidatorCommission/%v", ok)) opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } diff --git a/x/gov/simulation/msgs.go b/x/gov/simulation/msgs.go index f8a2358cd105..5541fb2bd228 100644 --- a/x/gov/simulation/msgs.go +++ b/x/gov/simulation/msgs.go @@ -38,7 +38,7 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Ope }) statePercentageArray := []float64{1, .9, .75, .4, .15, 0} curNumVotesState := 1 - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) ( opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { // 1) submit proposal now @@ -47,7 +47,7 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Ope if err != nil { return simulation.NoOpMsg(), nil, err } - ok := simulateHandleMsgSubmitProposal(msg, handler, ctx, event) + ok := simulateHandleMsgSubmitProposal(msg, handler, ctx) opMsg = simulation.NewOperationMsg(msg, ok, "") // don't schedule votes if proposal failed if !ok { @@ -80,7 +80,7 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Ope // Note: Currently doesn't ensure that the proposal txt is in JSON form func SimulateMsgSubmitProposal(k gov.Keeper) simulation.Operation { handler := gov.NewHandler(k) - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) ( opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { sender := simulation.RandomAcc(r, accs) @@ -88,19 +88,18 @@ func SimulateMsgSubmitProposal(k gov.Keeper) simulation.Operation { if err != nil { return simulation.NoOpMsg(), nil, err } - ok := simulateHandleMsgSubmitProposal(msg, handler, ctx, event) + ok := simulateHandleMsgSubmitProposal(msg, handler, ctx) opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } } -func simulateHandleMsgSubmitProposal(msg gov.MsgSubmitProposal, handler sdk.Handler, ctx sdk.Context, event func(string)) (ok bool) { +func simulateHandleMsgSubmitProposal(msg gov.MsgSubmitProposal, handler sdk.Handler, ctx sdk.Context) (ok bool) { ctx, write := ctx.CacheContext() ok = handler(ctx, msg).IsOK() if ok { write() } - event(fmt.Sprintf("gov/MsgSubmitProposal/%v", ok)) return ok } @@ -121,7 +120,7 @@ func simulationCreateMsgSubmitProposal(r *rand.Rand, sender simulation.Account) // SimulateMsgDeposit func SimulateMsgDeposit(k gov.Keeper) simulation.Operation { - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) ( opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { acc := simulation.RandomAcc(r, accs) @@ -140,7 +139,6 @@ func SimulateMsgDeposit(k gov.Keeper) simulation.Operation { write() } - event(fmt.Sprintf("gov/MsgDeposit/%v", ok)) opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } @@ -154,7 +152,7 @@ func SimulateMsgVote(k gov.Keeper) simulation.Operation { // nolint: unparam func operationSimulateMsgVote(k gov.Keeper, acc simulation.Account, proposalID uint64) simulation.Operation { - return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) ( + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) ( opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { if acc.Equals(simulation.Account{}) { @@ -181,7 +179,6 @@ func operationSimulateMsgVote(k gov.Keeper, acc simulation.Account, proposalID u write() } - event(fmt.Sprintf("gov/MsgVote/%v", ok)) opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } diff --git a/x/simulation/operation.go b/x/simulation/operation.go index 86f599ae3508..31bedc758430 100644 --- a/x/simulation/operation.go +++ b/x/simulation/operation.go @@ -2,6 +2,7 @@ package simulation import ( "encoding/json" + "fmt" "math/rand" "sort" "time" @@ -20,7 +21,7 @@ import ( // Operations can optionally provide a list of "FutureOperations" to run later // These will be ran at the beginning of the corresponding block. type Operation func(r *rand.Rand, app *baseapp.BaseApp, - ctx sdk.Context, accounts []Account, event func(string)) ( + ctx sdk.Context, accounts []Account) ( OperationMsg OperationMsg, futureOps []FutureOperation, err error) // entry kinds for use within OperationEntry @@ -122,7 +123,7 @@ func NewOperationMsgBasic(route, name, comment string, ok bool, msg []byte) Oper } } -// OperationMsg - create a new operation message from sdk.Msg +// NoOpMsg - create a no-operation message func NoOpMsg() OperationMsg { return OperationMsg{ Route: "", @@ -133,7 +134,7 @@ func NoOpMsg() OperationMsg { } } -// LogEntry - log entry text for this operation msg +// log entry text for this operation msg func (om OperationMsg) String() string { out, err := json.Marshal(om) if err != nil { @@ -142,7 +143,7 @@ func (om OperationMsg) String() string { return string(out) } -// LogEntry - log entry text for this operation msg +// Marshal the operation msg, panic on error func (om OperationMsg) MustMarshal() json.RawMessage { out, err := json.Marshal(om) if err != nil { @@ -151,6 +152,15 @@ func (om OperationMsg) MustMarshal() json.RawMessage { return out } +// add event for event stats +func (om OperationMsg) LogEvent(eventLogger func(string)) { + pass := "ok" + if !om.OK { + pass = "failure" + } + eventLogger(fmt.Sprintf("%v/%v/%v", om.Route, om.Name, pass)) +} + // queue of operations type OperationQueue map[int][]Operation diff --git a/x/simulation/simulate.go b/x/simulation/simulate.go index b79a01ff8e5b..3eb25ccbc626 100644 --- a/x/simulation/simulate.go +++ b/x/simulation/simulate.go @@ -259,7 +259,8 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params // NOTE: the Rand 'r' should not be used here. opAndR := opAndRz[i] op, r2 := opAndR.op, opAndR.rand - opMsg, futureOps, err := op(r2, app, ctx, accounts, event) + opMsg, futureOps, err := op(r2, app, ctx, accounts) + opMsg.LogEvent(event) if !lean || opMsg.OK { logWriter.AddEntry(MsgEntry(header.Height, opMsg, int64(i))) } @@ -302,7 +303,8 @@ func runQueuedOperations(queueOps map[int][]Operation, // For now, queued operations cannot queue more operations. // If a need arises for us to support queued messages to queue more messages, this can // be changed. - opMsg, _, err := queuedOp[i](r, app, ctx, accounts, tallyEvent) + opMsg, _, err := queuedOp[i](r, app, ctx, accounts) + opMsg.LogEvent(tallyEvent) if !lean || opMsg.OK { logWriter.AddEntry((QueuedMsgEntry(int64(height), opMsg))) } @@ -326,7 +328,8 @@ func runQueuedTimeOperations(queueOps []FutureOperation, // For now, queued operations cannot queue more operations. // If a need arises for us to support queued messages to queue more messages, this can // be changed. - opMsg, _, err := queueOps[0].Op(r, app, ctx, accounts, tallyEvent) + opMsg, _, err := queueOps[0].Op(r, app, ctx, accounts) + opMsg.LogEvent(tallyEvent) if !lean || opMsg.OK { logWriter.AddEntry(QueuedMsgEntry(int64(height), opMsg)) } diff --git a/x/slashing/simulation/msgs.go b/x/slashing/simulation/msgs.go index 5b3c243734f8..9aaa8382d204 100644 --- a/x/slashing/simulation/msgs.go +++ b/x/slashing/simulation/msgs.go @@ -13,8 +13,7 @@ import ( // SimulateMsgUnjail func SimulateMsgUnjail(k slashing.Keeper) simulation.Operation { return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accs []simulation.Account, event func(string)) ( - opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { + accs []simulation.Account) (opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { acc := simulation.RandomAcc(r, accs) address := sdk.ValAddress(acc.Address) @@ -27,7 +26,6 @@ func SimulateMsgUnjail(k slashing.Keeper) simulation.Operation { if ok { write() } - event(fmt.Sprintf("slashing/MsgUnjail/%v", ok)) opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } diff --git a/x/staking/simulation/msgs.go b/x/staking/simulation/msgs.go index f67a462a63d7..05241dbdccab 100644 --- a/x/staking/simulation/msgs.go +++ b/x/staking/simulation/msgs.go @@ -16,7 +16,7 @@ import ( func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulation.Operation { handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accs []simulation.Account, event func(string)) ( + accs []simulation.Account) ( opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { denom := k.GetParams(ctx).BondDenom @@ -56,7 +56,6 @@ func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulati write() } - event(fmt.Sprintf("staking/MsgCreateValidator/%v", ok)) opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } @@ -66,8 +65,7 @@ func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulati func SimulateMsgEditValidator(k staking.Keeper) simulation.Operation { handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accs []simulation.Account, event func(string)) ( - opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { + accs []simulation.Account) (opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { description := staking.Description{ Moniker: simulation.RandStringOfLength(r, 10), @@ -93,7 +91,6 @@ func SimulateMsgEditValidator(k staking.Keeper) simulation.Operation { if ok { write() } - event(fmt.Sprintf("staking/MsgEditValidator/%v", ok)) opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } @@ -103,8 +100,7 @@ func SimulateMsgEditValidator(k staking.Keeper) simulation.Operation { func SimulateMsgDelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Operation { handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accs []simulation.Account, event func(string)) ( - opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { + accs []simulation.Account) (opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { denom := k.GetParams(ctx).BondDenom if len(k.GetAllValidators(ctx)) == 0 { @@ -133,7 +129,6 @@ func SimulateMsgDelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Oper if ok { write() } - event(fmt.Sprintf("staking/MsgDelegate/%v", ok)) opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } @@ -143,8 +138,7 @@ func SimulateMsgDelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Oper func SimulateMsgUndelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Operation { handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accs []simulation.Account, event func(string)) ( - opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { + accs []simulation.Account) (opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { delegatorAcc := simulation.RandomAcc(r, accs) delegatorAddress := delegatorAcc.Address @@ -172,7 +166,6 @@ func SimulateMsgUndelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Op if ok { write() } - event(fmt.Sprintf("staking/MsgUndelegate/%v", ok)) opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } @@ -182,8 +175,7 @@ func SimulateMsgUndelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Op func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Operation { handler := staking.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accs []simulation.Account, event func(string)) ( - opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { + accs []simulation.Account) (opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { denom := k.GetParams(ctx).BondDenom if len(k.GetAllValidators(ctx)) == 0 { @@ -217,7 +209,6 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulati if ok { write() } - event(fmt.Sprintf("staking/MsgBeginRedelegate/%v", ok)) opMsg = simulation.NewOperationMsg(msg, ok, "") return opMsg, nil, nil } From 8df1223942160c9134703c0e4b402c25c1a30523 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 14 Mar 2019 10:50:34 -0400 Subject: [PATCH 19/19] pending update --- .../improvements/gaia/3819-Simulation-refa | 7 ++ PENDING.md | 73 ------------------- 2 files changed, 7 insertions(+), 73 deletions(-) create mode 100644 .pending/improvements/gaia/3819-Simulation-refa delete mode 100644 PENDING.md diff --git a/.pending/improvements/gaia/3819-Simulation-refa b/.pending/improvements/gaia/3819-Simulation-refa new file mode 100644 index 000000000000..bc4531f962bc --- /dev/null +++ b/.pending/improvements/gaia/3819-Simulation-refa @@ -0,0 +1,7 @@ +\#3819 Simulation refactor, log output now stored in ~/.gaiad/simulation/ + * Simulation moved to its own module (not a part of mock) + * Logger type instead of passing function variables everywhere + * Logger json output (for reloadable simulation running) + * Cleanup bank simulation messages / remove dup code in bank simulation + * Simulations saved in `~/.gaiad/simulations/` + * "Lean" simulation output option to exclude No-ops and !ok functions (`--SimulationLean` flag) diff --git a/PENDING.md b/PENDING.md deleted file mode 100644 index 5fce328b8109..000000000000 --- a/PENDING.md +++ /dev/null @@ -1,73 +0,0 @@ -# PENDING CHANGELOG - - - -## BREAKING CHANGES - -### Gaia REST API - -### Gaia CLI - -### Gaia - -### SDK - -### Tendermint - - - -## FEATURES - -### Gaia REST API - -### Gaia CLI - -### Gaia - -### SDK - -* [\3813](https://github.com/cosmos/cosmos-sdk/pull/3813) New sdk.NewCoins safe constructor to replace bare - sdk.Coins{} declarations. - -### Tendermint - - - -## IMPROVEMENTS - -### Gaia REST API - -### Gaia CLI - -### Gaia - -* #3808 `gaiad` and `gaiacli` integration tests use ./build/ binaries. -* \#3819 Simulation refactor, log output now stored in ~/.gaiad/simulation/ - * Simulation moved to its own module (not a part of mock) - * Logger type instead of passing function variables everywhere - * Logger json output (for reloadable simulation running) - * Cleanup bank simulation messages / remove dup code in bank simulation - * Simulations saved in `~/.gaiad/simulations/` - * "Lean" simulation output option to exclude No-ops and !ok functions (`--SimulationLean` flag) - -### SDK -* #3801 `baseapp` saftey improvements - -### Tendermint - -### CI/CD -* [\198](https://github.com/cosmos/cosmos-sdk/pull/3832) - - - -## BUG FIXES - -### Gaia REST API - -### Gaia CLI - -### Gaia - -### SDK - -### Tendermint