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 3 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
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 adviced to keep emit the module name event.
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
`Baseapp` only emits that event if the module have not already done so.

#### `x/gov`

##### Minimum Proposal Deposit At Time of Submission
Expand Down
32 changes: 24 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,30 @@ 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]))
// we verify the the events have no module attribute
hasModuleNameEvent := false
for _, event := range events {
if event.Type != sdk.EventTypeMessage {
continue
}

for _, attr := range event.Attributes {
if attr.Key == sdk.AttributeKeyModule {
hasModuleNameEvent = true
}
}
}

if !hasModuleNameEvent {
// 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