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 non-deterministic map iteration #12693

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -118,6 +118,7 @@ empty coins slice before it is used to create `banktype.MsgSend`.
* (x/auth/tx) [#12474](https://github.com/cosmos/cosmos-sdk/pull/12474) Remove condition in GetTxsEvent that disallowed multiple equal signs, which would break event queries with base64 strings (i.e. query by signature).
* [#12448](https://github.com/cosmos/cosmos-sdk/pull/12448) Start telemetry independently from the API server.
* (store/rootmulti) [#12487](https://github.com/cosmos/cosmos-sdk/pull/12487) Fix non-deterministic map iteration.
* (types) [#12693](https://github.com/cosmos/cosmos-sdk/pull/12693) Fix non-deterministic map iteration.

## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23

Expand Down
15 changes: 14 additions & 1 deletion types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ func TypedEventToEvent(tev proto.Message) (Event, error) {
return Event{}, err
}

// sort the keys to ensure the order is always the same
keys := orderedKeys(attrMap)

attrs := make([]abci.EventAttribute, 0, len(attrMap))
for k, v := range attrMap {
for _, k := range keys {
v := attrMap[k]
attrs = append(attrs, abci.EventAttribute{
Key: k,
Value: string(v),
Expand All @@ -101,6 +105,15 @@ func TypedEventToEvent(tev proto.Message) (Event, error) {
}, nil
}

func orderedKeys(m map[string]json.RawMessage) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}

// ParseTypedEvent converts abci.Event back to typed event
func ParseTypedEvent(event abci.Event) (proto.Message, error) {
concreteGoType := proto.MessageType(event.Type)
Expand Down
15 changes: 15 additions & 0 deletions types/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ func (s *eventsTestSuite) TestEventManager() {
s.Require().Equal(em.Events(), events.AppendEvent(event))
}

func (s *eventsTestSuite) TestEmitTypedEvent() {
s.Run("deterministic key-value order", func() {
for i := 0; i < 10; i++ {
em := sdk.NewEventManager()
coin := sdk.NewCoin("fakedenom", sdk.NewInt(1999999))
s.Require().NoError(em.EmitTypedEvent(&coin))
s.Require().Len(em.Events(), 1)
attrs := em.Events()[0].Attributes
s.Require().Len(attrs, 2)
s.Require().Equal(attrs[0].Key, "amount")
s.Require().Equal(attrs[1].Key, "denom")
}
})
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

would be good to add rapid / fuzzy test


func (s *eventsTestSuite) TestEventManagerTypedEvents() {
em := sdk.NewEventManager()

Expand Down