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
This commit is contained in:
Federico Kunze 2020-05-11 18:23:41 -04:00 committed by GitHub
parent 138e0b074f
commit 80be50319c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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{})