diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f6459e39159..9981384aa15e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,7 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## Unreleased -* nothing +* (baseapp) Add a optional function to baseapp to manipulate events [\#1092](https://github.com/provenance-io/provenance/issues/1092) --- diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index e254ed089030..fe0345db684a 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -136,6 +136,8 @@ type BaseApp struct { // nolint: maligned abciListeners []ABCIListener feeHandler sdk.FeeHandler + + aggregateEventsFunc func(anteEvents []abci.Event, resultEvents []abci.Event) ([]abci.Event, []abci.Event) } // NewBaseApp returns a reference to an initialized BaseApp. It accepts a @@ -734,17 +736,31 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re // append the events in the order of occurrence result.Events = append(anteEvents, result.Events...) } - // additional fee events - if len(feeEvents) > 0 { - // append the fee events at the end of the other events, since they get charged at the end of the Tx - result.Events = append(result.Events, feeEvents.ToABCIEvents()...) - } } + // additional fee events + if len(feeEvents) > 0 { + // append the fee events at the end of the other events, since they get charged at the end of the Tx + result.Events = append(result.Events, feeEvents.ToABCIEvents()...) + } + } + + if result != nil { // tx was successful run aggregator for ante and result events + anteEvents, result.Events = AggregateEvents(app, anteEvents, result.Events) + } else { // tx failed run aggregator for ante events only since result object is nil + anteEvents, _ = AggregateEvents(app, anteEvents, nil) } return gInfo, result, anteEvents, priority, ctx, err } +// AggregateEvents aggregation logic of result events (ante and postHander events) with feeEvents +func AggregateEvents(app *BaseApp, anteEvents []abci.Event, resultEvents []abci.Event) ([]abci.Event, []abci.Event) { + if app.aggregateEventsFunc != nil { + return app.aggregateEventsFunc(anteEvents, resultEvents) + } + return anteEvents, resultEvents +} + // FeeInvoke apply fee logic and append events func FeeInvoke(mode runTxMode, app *BaseApp, runMsgCtx sdk.Context) (sdk.Events, error) { if app.feeHandler != nil { diff --git a/baseapp/options.go b/baseapp/options.go index b1559ab32079..e6e992a76d20 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -4,6 +4,7 @@ import ( "fmt" "io" + abci "github.com/tendermint/tendermint/abci/types" dbm "github.com/tendermint/tm-db" "github.com/cosmos/cosmos-sdk/codec/types" @@ -247,3 +248,12 @@ func (app *BaseApp) SetFeeHandler(feeHandler sdk.FeeHandler) { app.feeHandler = feeHandler } + +// SetAggregateEventsFunc sets the function that aggregates events from baseapp result events and feehandler events +func (app *BaseApp) SetAggregateEventsFunc(aggregateEventsFunc func(resultEvents []abci.Event, feeEvents []abci.Event) ([]abci.Event, []abci.Event)) { + if app.sealed { + panic("SetAggregateEventsFunc() on sealed BaseApp") + } + + app.aggregateEventsFunc = aggregateEventsFunc +}