diff --git a/CHANGELOG.md b/CHANGELOG.md index 829887e9e95..9a61b7bfd93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -84,6 +84,7 @@ respectively, and the latter defines the height interval in which versions are d **Note, there are some client-facing API breaking changes with regard to IAVL, stores, and pruning settings.** * (x/distribution) [\#6210](https://github.com/cosmos/cosmos-sdk/pull/6210) Register `MsgFundCommunityPool` in distribution amino codec. * (types) [\#5741](https://github.com/cosmos/cosmos-sdk/issues/5741) Prevent `ChainAnteDecorators()` from panicking when empty `AnteDecorator` slice is supplied. +* (baseapp) [\#6306](https://github.com/cosmos/cosmos-sdk/issues/6306) Prevent events emitted by the antehandler from being persisted between transactions. ## [v0.38.4] - 2020-05-21 diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index cc1f37cb4fc..3fd121e08a6 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -604,8 +604,9 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (gInfo sdk. // writes do not happen if aborted/failed. This may have some // performance benefits, but it'll be more difficult to get right. anteCtx, msCache = app.cacheTxContext(ctx, txBytes) - + anteCtx = anteCtx.WithEventManager(sdk.NewEventManager()) newCtx, err := app.anteHandler(anteCtx, tx, mode == runTxModeSimulate) + if !newCtx.IsZero() { // At this point, newCtx.MultiStore() is cache-wrapped, or something else // replaced by the AnteHandler. We want the original multistore, not one diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index e298a44311f..75f5e5b617b 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -658,20 +658,33 @@ 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) { + return func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) { store := ctx.KVStore(capKey) txTest := tx.(txTest) if txTest.FailOnAnte { - return newCtx, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "ante handler failure") + return ctx, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "ante handler failure") } - _, err = incrementingCounter(t, store, storeKey, txTest.Counter) + _, err := incrementingCounter(t, store, storeKey, txTest.Counter) if err != nil { - return newCtx, err + return ctx, err } - return newCtx, nil + ctx.EventManager().EmitEvents( + counterEvent("ante_handler", txTest.Counter), + ) + + return ctx, nil + } +} + +func counterEvent(evType string, msgCount int64) sdk.Events { + return sdk.Events{ + sdk.NewEvent( + evType, + sdk.NewAttribute("update_counter", fmt.Sprintf("%d", msgCount)), + ), } } @@ -1288,6 +1301,7 @@ func TestBaseAppAnteHandler(t *testing.T) { txBytes, err := cdc.MarshalBinaryLengthPrefixed(tx) require.NoError(t, err) res := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes}) + require.Empty(t, res.Events) require.False(t, res.IsOK(), fmt.Sprintf("%v", res)) ctx := app.getState(runTxModeDeliver).ctx @@ -1303,6 +1317,7 @@ func TestBaseAppAnteHandler(t *testing.T) { require.NoError(t, err) res = app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes}) + require.Empty(t, res.Events) require.False(t, res.IsOK(), fmt.Sprintf("%v", res)) ctx = app.getState(runTxModeDeliver).ctx @@ -1318,6 +1333,7 @@ func TestBaseAppAnteHandler(t *testing.T) { require.NoError(t, err) res = app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes}) + require.NotEmpty(t, res.Events) require.True(t, res.IsOK(), fmt.Sprintf("%v", res)) ctx = app.getState(runTxModeDeliver).ctx