Skip to content

Commit

Permalink
feat: deterministic map iteration (backport cosmos#12781) (cosmos#12943)
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Aug 18, 2022
1 parent f3c1d8c commit d2a5018
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/params) [#12615](https://github.com/cosmos/cosmos-sdk/pull/12615) Add `GetParamSetIfExists` function to params `Subspace` to prevent panics on breaking changes.
* (x/bank) [#12674](https://github.com/cosmos/cosmos-sdk/pull/12674) Add convenience function `CreatePrefixedAccountStoreKey()` to construct key to access account's balance for a given denom.
* [#12877](https://github.com/cosmos/cosmos-sdk/pull/12877) Bumped cosmossdk.io/math to v1.0.0-beta.3
* [#12693](https://github.com/cosmos/cosmos-sdk/pull/12693) Make sure the order of each node is consistent when emitting proto events.

### Bug Fixes

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ require (
github.com/tendermint/tendermint v0.34.20
github.com/tendermint/tm-db v0.6.7
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd
google.golang.org/grpc v1.48.0
google.golang.org/protobuf v1.28.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,8 @@ golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20200331195152-e8c3332aa8e5/go.mod h1:4M0jN8W1tt0AVLNr8HDosyJCDCDuyL9N9+3m7wDWgKw=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
Expand Down
9 changes: 8 additions & 1 deletion types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types
import (
"encoding/json"
"fmt"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -87,8 +89,13 @@ func TypedEventToEvent(tev proto.Message) (Event, error) {
return Event{}, err
}

// sort the keys to ensure the order is always the same
keys := maps.Keys(attrMap)
slices.Sort(keys)

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: []byte(k),
Value: v,
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(string(attrs[0].Key), "amount")
s.Require().Equal(string(attrs[1].Key), "denom")
}
})
}

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

Expand Down

0 comments on commit d2a5018

Please sign in to comment.