diff --git a/CHANGELOG.md b/CHANGELOG.md index e68d733b9208..e33d2fba0c03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* [#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). * (types) [#14332](https://github.com/cosmos/cosmos-sdk/issues/14332) Reduce state export time by 50%. * (types) [#14163](https://github.com/cosmos/cosmos-sdk/pull/14163) Refactor `(coins Coins) Validate()` to avoid unnecessary map. * [#13881](https://github.com/cosmos/cosmos-sdk/pull/13881) Optimize iteration on nested cached KV stores and other operations in general. diff --git a/UPGRADING.md b/UPGRADING.md index afd029a2f6b5..c3526a9cfc00 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -100,6 +100,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 diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 312300183835..568470861a4e 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -797,7 +797,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 // @@ -839,7 +839,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)) @@ -848,14 +848,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 diff --git a/types/events.go b/types/events.go index 3415927516b4..0d8fafc02c5e 100644 --- a/types/events.go +++ b/types/events.go @@ -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) @@ -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" diff --git a/types/events_test.go b/types/events_test.go index f95f8a1612d1..966157d10158 100644 --- a/types/events_test.go +++ b/types/events_test.go @@ -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{}) }