Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve baseapp event emission #14356

Merged
merged 9 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/consensus) [#12905](https://github.com/cosmos/cosmos-sdk/pull/12905) Create a new `x/consensus` module that is now responsible for maintaining Tendermint consensus parameters instead of `x/param`. Legacy types remain in order to facilitate parameter migration from the deprecated `x/params`. App developers should ensure that they execute `baseapp.MigrateParams` during their chain upgrade. These legacy types will be removed in a future release.
* (client/tx) [#13670](https://github.com/cosmos/cosmos-sdk/pull/13670) Add validation in `BuildUnsignedTx` to prevent simple inclusion of valid mnemonics
* [#13473](https://github.com/cosmos/cosmos-sdk/pull/13473) ADR-038: Go plugin system proposal
* [#14356](https://github.com/cosmos/cosmos-sdk/pull/14356) Add `events.GetAttributes` and `event.GetAttribute` methods to simplify the retrieval of an attribute from event(s).

### Improvements

* (types) [#14354](https://github.com/cosmos/cosmos-sdk/pull/14354) - improve performance on Context.KVStore and Context.TransientStore by 40%
* (crypto/keyring) [#14151](https://github.com/cosmos/cosmos-sdk/pull/14151) Move keys presentation from `crypto/keyring` to `client/keys`
* (types) [#14163](https://github.com/cosmos/cosmos-sdk/pull/14163) Refactor `(coins Coins) Validate()` to avoid unnecessary map.
Expand Down
4 changes: 4 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ ctx.EventManager().EmitEvent(
)
```

The module name is assumed by `baseapp` to be the second element of the message route: `"cosmos.bank.v1beta1.MsgSend" -> "bank"`.
In case a module does not follow the standard message path, (e.g. IBC), it is advised to keep emitting the module name event.
`Baseapp` only emits that event if the module have not already done so.

#### `x/gov`

##### Minimum Proposal Deposit At Time of Submission
Expand Down
19 changes: 11 additions & 8 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (*s
}

// create message events
msgEvents := createEvents(msg).AppendEvents(msgResult.GetEvents())
msgEvents := createEvents(msgResult.GetEvents(), msg)

// append message events, data and logs
//
Expand Down Expand Up @@ -838,7 +838,7 @@ func makeABCIData(msgResponses []*codectypes.Any) ([]byte, error) {
return proto.Marshal(&sdk.TxMsgData{MsgResponses: msgResponses})
}

func createEvents(msg sdk.Msg) sdk.Events {
func createEvents(events sdk.Events, msg sdk.Msg) sdk.Events {
eventMsgName := sdk.MsgTypeURL(msg)
msgEvent := sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyAction, eventMsgName))

Expand All @@ -847,14 +847,17 @@ func createEvents(msg sdk.Msg) sdk.Events {
msgEvent = msgEvent.AppendAttributes(sdk.NewAttribute(sdk.AttributeKeySender, msg.GetSigners()[0].String()))
}

// here we assume that routes module name is the second element of the route
// e.g. "cosmos.bank.v1beta1.MsgSend" => "bank"
moduleName := strings.Split(eventMsgName, ".")
if len(moduleName) > 1 {
msgEvent = msgEvent.AppendAttributes(sdk.NewAttribute(sdk.AttributeKeyModule, moduleName[1]))
// verify that events have no module attribute set
if _, found := events.GetAttributes(sdk.AttributeKeyModule); !found {
// here we assume that routes module name is the second element of the route
// e.g. "cosmos.bank.v1beta1.MsgSend" => "bank"
moduleName := strings.Split(eventMsgName, ".")
if len(moduleName) > 1 {
msgEvent = msgEvent.AppendAttributes(sdk.NewAttribute(sdk.AttributeKeyModule, moduleName[1]))
}
}

return sdk.Events{msgEvent}
return sdk.Events{msgEvent}.AppendEvents(events)
}

// DefaultPrepareProposal returns the default implementation for processing an
Expand Down
24 changes: 24 additions & 0 deletions types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ func (e Event) AppendAttributes(attrs ...Attribute) Event {
return e
}

// GetAttribute returns an attribute for a given key present in an event.
// If the key is not found, the boolean value will be false.
func (e Event) GetAttribute(key string) (Attribute, bool) {
for _, attr := range e.Attributes {
if attr.Key == key {
return Attribute{Key: attr.Key, Value: attr.Value}, true
}
}
return Attribute{}, false
}

// AppendEvent adds an Event to a slice of events.
func (e Events) AppendEvent(event Event) Events {
return append(e, event)
Expand All @@ -218,6 +229,19 @@ func (e Events) ToABCIEvents() []abci.Event {
return res
}

// GetAttributes returns all attributes matching a given key present in events.
// If the key is not found, the boolean value will be false.
func (e Events) GetAttributes(key string) ([]Attribute, bool) {
attrs := make([]Attribute, 0)
for _, event := range e {
if attr, found := event.GetAttribute(key); found {
attrs = append(attrs, attr)
}
}

return attrs, len(attrs) > 0
}

// Common event types and attribute keys
const (
EventTypeTx = "tx"
Expand Down
19 changes: 19 additions & 0 deletions types/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ func (s *eventsTestSuite) TestAppendAttributes() {
s.Require().Equal(e, sdk.NewEvent("transfer", sdk.NewAttribute("sender", "foo"), sdk.NewAttribute("recipient", "bar")))
}

func (s *eventsTestSuite) TestGetAttributes() {
e := sdk.NewEvent("transfer", sdk.NewAttribute("sender", "foo"))
e = e.AppendAttributes(sdk.NewAttribute("recipient", "bar"))
attr, found := e.GetAttribute("recipient")
s.Require().True(found)
s.Require().Equal(attr, sdk.NewAttribute("recipient", "bar"))
_, found = e.GetAttribute("foo")
s.Require().False(found)

events := sdk.Events{e}.AppendEvent(sdk.NewEvent("message", sdk.NewAttribute("sender", "bar")))
attrs, found := events.GetAttributes("sender")
s.Require().True(found)
s.Require().Len(attrs, 2)
s.Require().Equal(attrs[0], sdk.NewAttribute("sender", "foo"))
s.Require().Equal(attrs[1], sdk.NewAttribute("sender", "bar"))
_, found = events.GetAttributes("foo")
s.Require().False(found)
}

func (s *eventsTestSuite) TestEmptyEvents() {
s.Require().Equal(sdk.EmptyEvents(), sdk.Events{})
}
Expand Down