Skip to content

Commit

Permalink
Add event aggregator function call in baseapp (#268)
Browse files Browse the repository at this point in the history
* Add event aggregator function call in baseapp

* fix comments

* remove unneeded line addition

* update aggregator and return

* remove returning of error

* add change log entry
  • Loading branch information
nullpointer0x00 authored Sep 30, 2022
1 parent 1f62e5a commit b08ec4b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

---

Expand Down
26 changes: 21 additions & 5 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit b08ec4b

Please sign in to comment.