Skip to content

Commit d2a5018

Browse files
authored
feat: deterministic map iteration (backport cosmos#12781) (cosmos#12943)
1 parent f3c1d8c commit d2a5018

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4444
* (x/params) [#12615](https://github.com/cosmos/cosmos-sdk/pull/12615) Add `GetParamSetIfExists` function to params `Subspace` to prevent panics on breaking changes.
4545
* (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.
4646
* [#12877](https://github.com/cosmos/cosmos-sdk/pull/12877) Bumped cosmossdk.io/math to v1.0.0-beta.3
47+
* [#12693](https://github.com/cosmos/cosmos-sdk/pull/12693) Make sure the order of each node is consistent when emitting proto events.
4748

4849
### Bug Fixes
4950

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ require (
5252
github.com/tendermint/tendermint v0.34.20
5353
github.com/tendermint/tm-db v0.6.7
5454
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
55+
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
5556
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd
5657
google.golang.org/grpc v1.48.0
5758
google.golang.org/protobuf v1.28.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,8 @@ golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u0
14941494
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
14951495
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
14961496
golang.org/x/exp v0.0.0-20200331195152-e8c3332aa8e5/go.mod h1:4M0jN8W1tt0AVLNr8HDosyJCDCDuyL9N9+3m7wDWgKw=
1497+
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
1498+
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
14971499
golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
14981500
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
14991501
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=

types/events.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package types
33
import (
44
"encoding/json"
55
"fmt"
6+
"golang.org/x/exp/maps"
7+
"golang.org/x/exp/slices"
68
"reflect"
79
"sort"
810
"strings"
@@ -87,8 +89,13 @@ func TypedEventToEvent(tev proto.Message) (Event, error) {
8789
return Event{}, err
8890
}
8991

92+
// sort the keys to ensure the order is always the same
93+
keys := maps.Keys(attrMap)
94+
slices.Sort(keys)
95+
9096
attrs := make([]abci.EventAttribute, 0, len(attrMap))
91-
for k, v := range attrMap {
97+
for _, k := range keys {
98+
v := attrMap[k]
9299
attrs = append(attrs, abci.EventAttribute{
93100
Key: []byte(k),
94101
Value: v,

types/events_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ func (s *eventsTestSuite) TestEventManager() {
6767
s.Require().Equal(em.Events(), events.AppendEvent(event))
6868
}
6969

70+
func (s *eventsTestSuite) TestEmitTypedEvent() {
71+
s.Run("deterministic key-value order", func() {
72+
for i := 0; i < 10; i++ {
73+
em := sdk.NewEventManager()
74+
coin := sdk.NewCoin("fakedenom", sdk.NewInt(1999999))
75+
s.Require().NoError(em.EmitTypedEvent(&coin))
76+
s.Require().Len(em.Events(), 1)
77+
attrs := em.Events()[0].Attributes
78+
s.Require().Len(attrs, 2)
79+
s.Require().Equal(string(attrs[0].Key), "amount")
80+
s.Require().Equal(string(attrs[1].Key), "denom")
81+
}
82+
})
83+
}
84+
7085
func (s *eventsTestSuite) TestEventManagerTypedEvents() {
7186
em := sdk.NewEventManager()
7287

0 commit comments

Comments
 (0)