Skip to content

Commit

Permalink
baseapp: fix sender events accumulation
Browse files Browse the repository at this point in the history
closes: #6306
original PR: #6307
  • Loading branch information
fedekunze authored and Alessio Treglia committed Jul 10, 2020
1 parent 864c621 commit c47c1b4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 21 additions & 5 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
),
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c47c1b4

Please sign in to comment.