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: do not flatten events attributes by event types #14691

Merged
merged 6 commits into from
Jan 19, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* [#14691](https://github.com/cosmos/cosmos-sdk/pull/14691) Change behavior of `sdk.StringifyEvents` to do not flatten events attributes by events type.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are log used in the state machine?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is worth mentioning that this only affects the Log string of the tx execution result, not the Events

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, is it okay to include in a point version, then? Or better directly in v0.47?
Someone could query the logs and store it right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you ask me, this does not belong in a patch release. I think clients also deserve stable interfaces.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you ask me, this does not belong in a patch release. I think clients also deserve stable interfaces.

Didn't think about that sorry. Yeah, that makes sense then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Having it in the 0.47 upgrade is great. I'd assume some clients adapted to the current behaviour (as you can see by the comments explaining how to split the merged events into individual events by counting positions). I doubt this change is urgent.

julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
* [#14609](https://github.com/cosmos/cosmos-sdk/pull/14609) Add RetryForBlocks method to use in tests that require waiting for a transaction to be included in a block.
* [#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
2 changes: 1 addition & 1 deletion math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ func BenchmarkLegacyQuoRoundupMut(b *testing.B) {
func TestFormatDec(t *testing.T) {
type decimalTest []string
var testcases []decimalTest
raw, err := os.ReadFile("../tx/textual/internal/testdata/decimals.json")
raw, err := os.ReadFile("../x/tx/textual/internal/testdata/decimals.json")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR, but I think it does not need another PR to be fixed.
Noticed it was failing while running make test: due to #14634

require.NoError(t, err)
err = json.Unmarshal(raw, &testcases)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion math/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func TestRoundTripMarshalToInt(t *testing.T) {
func TestFormatInt(t *testing.T) {
type integerTest []string
var testcases []integerTest
raw, err := os.ReadFile("../tx/textual/internal/testdata/integers.json")
raw, err := os.ReadFile("../x/tx/textual/internal/testdata/integers.json")
require.NoError(t, err)
err = json.Unmarshal(raw, &testcases)
require.NoError(t, err)
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 @@ -289,29 +288,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 @@ -335,7 +311,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