Skip to content

Commit

Permalink
fix: do not flatten events attributes by event types (backport #14691) (
Browse files Browse the repository at this point in the history
#14702)

Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
mergify[bot] and julienrbrt authored Jan 20, 2023
1 parent ffff320 commit 425ee43
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 30 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

## Improvements

* [#14691](https://github.com/cosmos/cosmos-sdk/pull/14691) Change behavior of `sdk.StringifyEvents` to not flatten events attributes by events type.
* This change only affects ABCI message logs, and not the actual events.
* [#14692](https://github.com/cosmos/cosmos-sdk/pull/14692) Improve RPC queries error message when app is at height 0.
* [#14017](https://github.com/cosmos/cosmos-sdk/pull/14017) Simplify ADR-028 and `address.Module`.
* This updates the [ADR-028](https://docs.cosmos.network/main/architecture/adr-028-public-key-addresses) and enhance the `address.Module` API to support module addresses and sub-module addresses in a backward compatible way.
Expand Down
26 changes: 1 addition & 25 deletions types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"reflect"
"sort"
"strings"

"golang.org/x/exp/maps"
Expand Down Expand Up @@ -278,29 +277,6 @@ func (se StringEvents) String() string {
return strings.TrimRight(sb.String(), "\n")
}

// Flatten returns a flattened version of StringEvents by grouping all attributes
// per unique event type.
func (se StringEvents) Flatten() StringEvents {
flatEvents := make(map[string][]Attribute)

for _, e := range se {
flatEvents[e.Type] = append(flatEvents[e.Type], e.Attributes...)
}
keys := make([]string, 0, len(flatEvents))
res := make(StringEvents, 0, len(flatEvents)) // appeneded to keys, same length of what is allocated to keys

for ty := range flatEvents {
keys = append(keys, ty)
}

sort.Strings(keys)
for _, ty := range keys {
res = append(res, StringEvent{Type: ty, Attributes: flatEvents[ty]})
}

return res
}

// StringifyEvent converts an Event object to a StringEvent object.
func StringifyEvent(e abci.Event) StringEvent {
res := StringEvent{Type: e.Type}
Expand All @@ -324,7 +300,7 @@ func StringifyEvents(events []abci.Event) StringEvents {
res = append(res, StringifyEvent(e))
}

return res.Flatten()
return res
}

// MarkEventsToIndex returns the set of ABCI events, where each event's attribute
Expand Down
8 changes: 4 additions & 4 deletions types/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ func (s *eventsTestSuite) TestStringifyEvents() {
sdk.NewEvent("message", sdk.NewAttribute(sdk.AttributeKeySender, "foo")),
sdk.NewEvent("message", sdk.NewAttribute(sdk.AttributeKeyModule, "bank")),
},
expTxtStr: "\t\t- message\n\t\t\t- sender: foo\n\t\t\t- module: bank",
expJSONStr: "[{\"type\":\"message\",\"attributes\":[{\"key\":\"sender\",\"value\":\"foo\"},{\"key\":\"module\",\"value\":\"bank\"}]}]",
expTxtStr: "\t\t- message\n\t\t\t- sender: foo\n\t\t- message\n\t\t\t- module: bank",
expJSONStr: "[{\"type\":\"message\",\"attributes\":[{\"key\":\"sender\",\"value\":\"foo\"}]},{\"type\":\"message\",\"attributes\":[{\"key\":\"module\",\"value\":\"bank\"}]}]",
},
{
name: "multiple events with same attributes",
Expand All @@ -158,8 +158,8 @@ func (s *eventsTestSuite) TestStringifyEvents() {
),
sdk.NewEvent("message", sdk.NewAttribute(sdk.AttributeKeySender, "foo")),
},
expTxtStr: "\t\t- message\n\t\t\t- module: staking\n\t\t\t- sender: cosmos1foo\n\t\t\t- sender: foo",
expJSONStr: `[{"type":"message","attributes":[{"key":"module","value":"staking"},{"key":"sender","value":"cosmos1foo"},{"key":"sender","value":"foo"}]}]`,
expTxtStr: "\t\t- message\n\t\t\t- module: staking\n\t\t\t- sender: cosmos1foo\n\t\t- message\n\t\t\t- sender: foo",
expJSONStr: `[{"type":"message","attributes":[{"key":"module","value":"staking"},{"key":"sender","value":"cosmos1foo"}]},{"type":"message","attributes":[{"key":"sender","value":"foo"}]}]`,
},
}

Expand Down
6 changes: 5 additions & 1 deletion types/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ func (s *resultTestSuite) TestParseABCILog() {

func (s *resultTestSuite) TestABCIMessageLog() {
cdc := codec.NewLegacyAmino()
events := sdk.Events{sdk.NewEvent("transfer", sdk.NewAttribute("sender", "foo"))}
events := sdk.Events{
sdk.NewEvent("transfer", sdk.NewAttribute("sender", "foo")),
sdk.NewEvent("transfer", sdk.NewAttribute("sender", "bar")),
}
msgLog := sdk.NewABCIMessageLog(0, "", events)
msgLogs := sdk.ABCIMessageLogs{msgLog}
bz, err := cdc.MarshalJSON(msgLogs)

s.Require().NoError(err)
s.Require().Equal(string(bz), msgLogs.String())
s.Require().Equal(`[{"msg_index":0,"events":[{"type":"transfer","attributes":[{"key":"sender","value":"foo"}]},{"type":"transfer","attributes":[{"key":"sender","value":"bar"}]}]}]`, msgLogs.String())
}

func (s *resultTestSuite) TestNewSearchTxsResult() {
Expand Down

0 comments on commit 425ee43

Please sign in to comment.