From 80be50319ca5e0e70a4c8325a8c5972a2bd2edf9 Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Mon, 11 May 2020 18:23:41 -0400 Subject: [PATCH] baseapp: track events emitted from AnteHandler (#6187) * baseapp: append AnteHandler events to tx result * changelog * baseapp tests * fix test * update changelog * update log * move event out from conditional * minor update * fix panic * add switch case * remove result on error * change conditional * check for event len * update tests --- CHANGELOG.md | 3 ++- baseapp/baseapp.go | 8 ++++++++ baseapp/baseapp_test.go | 39 ++++++++++++++++++++++++++++++++++----- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7557f89ad2..6ad2f0e626 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -206,6 +206,7 @@ Buffers for state serialization instead of Amino. ### Improvements +* (baseapp) [\#6186](https://github.com/cosmos/cosmos-sdk/issues/6186) Support emitting events during `AnteHandler` execution. * (x/auth) [\#5702](https://github.com/cosmos/cosmos-sdk/pull/5702) Add parameter querying support for `x/auth`. * (types) [\#5581](https://github.com/cosmos/cosmos-sdk/pull/5581) Add convenience functions {,Must}Bech32ifyAddressBytes. * (staking) [\#5584](https://github.com/cosmos/cosmos-sdk/pull/5584) Add util function `ToTmValidator` that converts a `staking.Validator` type to `*tmtypes.Validator`. @@ -231,7 +232,7 @@ functionality that requires an online connection. * (simulation) [\#6002](https://github.com/cosmos/cosmos-sdk/pull/6002) Add randomized consensus params into simulation. * (x/staking) [\#6059](https://github.com/cosmos/cosmos-sdk/pull/6059) Updated `HistoricalEntries` parameter default to 100. * (x/ibc) [\#5948](https://github.com/cosmos/cosmos-sdk/issues/5948) Add `InitGenesis` and `ExportGenesis` functions for `ibc` module. -* (types) [\#6128](https://github.com/cosmos/cosmos-sdk/pull/6137) Add String() method to GasMeter +* (types) [\#6128](https://github.com/cosmos/cosmos-sdk/pull/6137) Add `String()` method to `GasMeter`. ## [v0.38.3] - 2020-04-09 diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 43160a3849..e9d0d2baa0 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -536,6 +536,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (gInfo sdk. return sdk.GasInfo{}, nil, err } + var events sdk.Events if app.anteHandler != nil { var ( anteCtx sdk.Context @@ -563,6 +564,8 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (gInfo sdk. ctx = newCtx.WithMultiStore(ms) } + events = ctx.EventManager().Events() + // GasMeter expected to be set in AnteHandler gasWanted = ctx.GasMeter().Limit() @@ -584,6 +587,11 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (gInfo sdk. result, err = app.runMsgs(runMsgCtx, msgs, mode) if err == nil && mode == runTxModeDeliver { msCache.Write() + + if len(events) > 0 { + // append the events in the order of occurrence + result.Events = append(events.ToABCIEvents(), result.Events...) + } } return gInfo, result, err diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index b65d12f1a7..79d6cb5f5a 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -665,7 +665,8 @@ func testTxDecoder(cdc *codec.Codec) sdk.TxDecoder { func anteHandlerTxTest(t *testing.T, capKey sdk.StoreKey, storeKey []byte) sdk.AnteHandler { return func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, err error) { - store := ctx.KVStore(capKey) + newCtx = ctx.WithEventManager(sdk.NewEventManager()) + store := newCtx.KVStore(capKey) txTest := tx.(txTest) if txTest.FailOnAnte { @@ -677,12 +678,26 @@ func anteHandlerTxTest(t *testing.T, capKey sdk.StoreKey, storeKey []byte) sdk.A return newCtx, err } + newCtx.EventManager().EmitEvents( + counterEvent("ante_handler", txTest.Counter), + ) + return newCtx, nil } } +func counterEvent(evType string, msgCount int64) sdk.Events { + return sdk.Events{ + sdk.NewEvent( + evType, + sdk.NewAttribute("update_counter", fmt.Sprintf("%d", msgCount)), + ), + } +} + func handlerMsgCounter(t *testing.T, capKey sdk.StoreKey, deliverKey []byte) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + ctx = ctx.WithEventManager(sdk.NewEventManager()) store := ctx.KVStore(capKey) var msgCount int64 @@ -693,12 +708,21 @@ func handlerMsgCounter(t *testing.T, capKey sdk.StoreKey, deliverKey []byte) sdk } msgCount = m.Counter - case *msgCounter2: msgCount = m.Counter } - return incrementingCounter(t, store, deliverKey, msgCount) + ctx.EventManager().EmitEvents( + counterEvent(sdk.EventTypeMessage, msgCount), + ) + + res, err := incrementingCounter(t, store, deliverKey, msgCount) + if err != nil { + return nil, err + } + + res.Events = ctx.EventManager().Events().ToABCIEvents() + return res, nil } } @@ -761,11 +785,12 @@ func TestCheckTx(t *testing.T) { registerTestCodec(codec) for i := int64(0); i < nTxs; i++ { - tx := newTxCounter(i, 0) + tx := newTxCounter(i, 0) // no messages txBytes, err := codec.MarshalBinaryBare(tx) require.NoError(t, err) r := app.CheckTx(abci.RequestCheckTx{Tx: txBytes}) - assert.True(t, r.IsOK(), fmt.Sprintf("%v", r)) + require.Empty(t, r.GetEvents()) + require.True(t, r.IsOK(), fmt.Sprintf("%v", r)) } checkStateStore := app.checkState.ctx.KVStore(capKey1) @@ -821,6 +846,10 @@ func TestDeliverTx(t *testing.T) { res := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes}) require.True(t, res.IsOK(), fmt.Sprintf("%v", res)) + events := res.GetEvents() + require.Len(t, events, 3, "should contain ante handler, message type and counter events respectively") + require.Equal(t, counterEvent("ante_handler", counter).ToABCIEvents()[0], events[0], "ante handler event") + require.Equal(t, counterEvent(sdk.EventTypeMessage, counter).ToABCIEvents()[0], events[2], "msg handler update counter event") } app.EndBlock(abci.RequestEndBlock{})