Skip to content

Commit f656c7a

Browse files
authored
fix: check event determinism on original modules (#963)
* x/token * x/collection * x/foundation * chore * Update CHANGELOG.md * Update unit tests on event * Add tests on x/collection CreateContract * Add tests on x/foundation Begin/EndBlocker * Lint
1 parent 631ca58 commit f656c7a

File tree

10 files changed

+535
-87
lines changed

10 files changed

+535
-87
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4545
* (x/foundation) [\#933](https://github.com/line/lbm-sdk/pull/933) Clean up x/foundation apis
4646
* (x/collection) [\#938](https://github.com/line/lbm-sdk/pull/938) Add progress log into x/collection import-genesis
4747
* (x/foundation) [\#952](https://github.com/line/lbm-sdk/pull/952) Address generation of the empty coins in x/foundation
48+
* (x/collection,token,foundation) [\#963](https://github.com/line/lbm-sdk/pull/963) Check event determinism on original modules
4849

4950
### Bug Fixes
5051
* (swagger) [\#898](https://github.com/line/lbm-sdk/pull/898) fix a bug not added `lbm.tx.v1beta1.Service/GetBlockWithTxs` in swagger

x/collection/event.go

+73-30
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package collection
22

33
import (
44
"fmt"
5+
"sort"
56
"strings"
67

78
sdk "github.com/line/lbm-sdk/types"
@@ -33,6 +34,18 @@ func AttributeKeyFromString(name string) AttributeKey {
3334
return AttributeKey(AttributeKey_value[attributeKeyName])
3435
}
3536

37+
func sortedAttributeKeys(m map[AttributeKey]string) []AttributeKey {
38+
keys := make([]AttributeKey, 0, len(m))
39+
for key := range m {
40+
keys = append(keys, key)
41+
}
42+
43+
sort.Slice(keys, func(i, j int) bool {
44+
return keys[i] < keys[j]
45+
})
46+
return keys
47+
}
48+
3649
// Deprecated: use EventCreatedContract.
3750
func NewEventCreateCollection(event EventCreatedContract) sdk.Event {
3851
eventType := EventTypeCreateCollection.String()
@@ -46,7 +59,8 @@ func NewEventCreateCollection(event EventCreatedContract) sdk.Event {
4659
}
4760

4861
res := sdk.NewEvent(eventType)
49-
for key, value := range attributes {
62+
for _, key := range sortedAttributeKeys(attributes) {
63+
value := attributes[key]
5064
attribute := sdk.NewAttribute(key.String(), value)
5165
res = res.AppendAttributes(attribute)
5266
}
@@ -70,7 +84,8 @@ func NewEventIssueFT(event EventCreatedFTClass, to sdk.AccAddress, amount sdk.In
7084
}
7185

7286
res := sdk.NewEvent(eventType)
73-
for key, value := range attributes {
87+
for _, key := range sortedAttributeKeys(attributes) {
88+
value := attributes[key]
7489
attribute := sdk.NewAttribute(key.String(), value)
7590
res = res.AppendAttributes(attribute)
7691
}
@@ -88,7 +103,8 @@ func NewEventIssueNFT(event EventCreatedNFTClass) sdk.Event {
88103
}
89104

90105
res := sdk.NewEvent(eventType)
91-
for key, value := range attributes {
106+
for _, key := range sortedAttributeKeys(attributes) {
107+
value := attributes[key]
92108
attribute := sdk.NewAttribute(key.String(), value)
93109
res = res.AppendAttributes(attribute)
94110
}
@@ -106,7 +122,8 @@ func NewEventMintFT(event EventMintedFT) sdk.Event {
106122
}
107123

108124
res := sdk.NewEvent(eventType)
109-
for key, value := range attributes {
125+
for _, key := range sortedAttributeKeys(attributes) {
126+
value := attributes[key]
110127
attribute := sdk.NewAttribute(key.String(), value)
111128
res = res.AppendAttributes(attribute)
112129
}
@@ -129,7 +146,8 @@ func NewEventMintNFT(event EventMintedNFT) sdk.Events {
129146
AttributeKeyName: token.Name,
130147
AttributeKeyMeta: token.Meta,
131148
}
132-
for key, value := range attributes {
149+
for _, key := range sortedAttributeKeys(attributes) {
150+
value := attributes[key]
133151
attribute := sdk.NewAttribute(key.String(), value)
134152
e = e.AppendAttributes(attribute)
135153
}
@@ -161,7 +179,8 @@ func NewEventBurnFT(event EventBurned) *sdk.Event {
161179
}
162180

163181
res := sdk.NewEvent(eventType)
164-
for key, value := range attributes {
182+
for _, key := range sortedAttributeKeys(attributes) {
183+
value := attributes[key]
165184
attribute := sdk.NewAttribute(key.String(), value)
166185
res = res.AppendAttributes(attribute)
167186
}
@@ -189,7 +208,8 @@ func NewEventBurnNFT(event EventBurned) sdk.Events {
189208
AttributeKeyFrom: event.From,
190209
}
191210
head := sdk.NewEvent(eventType)
192-
for key, value := range attributes {
211+
for _, key := range sortedAttributeKeys(attributes) {
212+
value := attributes[key]
193213
attribute := sdk.NewAttribute(key.String(), value)
194214
head = head.AppendAttributes(attribute)
195215
}
@@ -226,7 +246,8 @@ func NewEventBurnFTFrom(event EventBurned) *sdk.Event {
226246
}
227247

228248
res := sdk.NewEvent(eventType)
229-
for key, value := range attributes {
249+
for _, key := range sortedAttributeKeys(attributes) {
250+
value := attributes[key]
230251
attribute := sdk.NewAttribute(key.String(), value)
231252
res = res.AppendAttributes(attribute)
232253
}
@@ -255,7 +276,8 @@ func NewEventBurnNFTFrom(event EventBurned) sdk.Events {
255276
AttributeKeyFrom: event.From,
256277
}
257278
head := sdk.NewEvent(eventType)
258-
for key, value := range attributes {
279+
for _, key := range sortedAttributeKeys(attributes) {
280+
value := attributes[key]
259281
attribute := sdk.NewAttribute(key.String(), value)
260282
head = head.AppendAttributes(attribute)
261283
}
@@ -279,7 +301,8 @@ func NewEventModifyCollection(event EventModifiedContract) sdk.Events {
279301
AttributeKeyContractID: event.ContractId,
280302
}
281303
head := sdk.NewEvent(eventType)
282-
for key, value := range attributes {
304+
for _, key := range sortedAttributeKeys(attributes) {
305+
value := attributes[key]
283306
attribute := sdk.NewAttribute(key.String(), value)
284307
head = head.AppendAttributes(attribute)
285308
}
@@ -303,7 +326,8 @@ func NewEventModifyTokenType(event EventModifiedTokenClass) sdk.Events {
303326
AttributeKeyTokenType: event.TokenType,
304327
}
305328
head := sdk.NewEvent(eventType)
306-
for key, value := range attributes {
329+
for _, key := range sortedAttributeKeys(attributes) {
330+
value := attributes[key]
307331
attribute := sdk.NewAttribute(key.String(), value)
308332
head = head.AppendAttributes(attribute)
309333
}
@@ -328,7 +352,8 @@ func NewEventModifyTokenOfFTClass(event EventModifiedTokenClass) sdk.Events {
328352
AttributeKeyTokenID: tokenID,
329353
}
330354
head := sdk.NewEvent(eventType)
331-
for key, value := range attributes {
355+
for _, key := range sortedAttributeKeys(attributes) {
356+
value := attributes[key]
332357
attribute := sdk.NewAttribute(key.String(), value)
333358
head = head.AppendAttributes(attribute)
334359
}
@@ -352,7 +377,8 @@ func NewEventModifyTokenOfNFT(event EventModifiedNFT) sdk.Events {
352377
AttributeKeyTokenID: event.TokenId,
353378
}
354379
head := sdk.NewEvent(eventType)
355-
for key, value := range attributes {
380+
for _, key := range sortedAttributeKeys(attributes) {
381+
value := attributes[key]
356382
attribute := sdk.NewAttribute(key.String(), value)
357383
head = head.AppendAttributes(attribute)
358384
}
@@ -388,7 +414,8 @@ func NewEventTransferFT(event EventSent) *sdk.Event {
388414
}
389415

390416
res := sdk.NewEvent(eventType)
391-
for key, value := range attributes {
417+
for _, key := range sortedAttributeKeys(attributes) {
418+
value := attributes[key]
392419
attribute := sdk.NewAttribute(key.String(), value)
393420
res = res.AppendAttributes(attribute)
394421
}
@@ -417,7 +444,8 @@ func NewEventTransferNFT(event EventSent) sdk.Events {
417444
AttributeKeyTo: event.To,
418445
}
419446
head := sdk.NewEvent(eventType)
420-
for key, value := range attributes {
447+
for _, key := range sortedAttributeKeys(attributes) {
448+
value := attributes[key]
421449
attribute := sdk.NewAttribute(key.String(), value)
422450
head = head.AppendAttributes(attribute)
423451
}
@@ -454,7 +482,8 @@ func NewEventTransferFTFrom(event EventSent) *sdk.Event {
454482
}
455483

456484
res := sdk.NewEvent(eventType)
457-
for key, value := range attributes {
485+
for _, key := range sortedAttributeKeys(attributes) {
486+
value := attributes[key]
458487
attribute := sdk.NewAttribute(key.String(), value)
459488
res = res.AppendAttributes(attribute)
460489
}
@@ -484,7 +513,8 @@ func NewEventTransferNFTFrom(event EventSent) sdk.Events {
484513
AttributeKeyTo: event.To,
485514
}
486515
head := sdk.NewEvent(eventType)
487-
for key, value := range attributes {
516+
for _, key := range sortedAttributeKeys(attributes) {
517+
value := attributes[key]
488518
attribute := sdk.NewAttribute(key.String(), value)
489519
head = head.AppendAttributes(attribute)
490520
}
@@ -511,7 +541,8 @@ func NewEventGrantPermToken(event EventGranted) sdk.Event {
511541
}
512542

513543
res := sdk.NewEvent(eventType)
514-
for key, value := range attributes {
544+
for _, key := range sortedAttributeKeys(attributes) {
545+
value := attributes[key]
515546
attribute := sdk.NewAttribute(key.String(), value)
516547
res = res.AppendAttributes(attribute)
517548
}
@@ -530,7 +561,8 @@ func NewEventGrantPermTokenHead(event EventGranted) sdk.Event {
530561
}
531562

532563
res := sdk.NewEvent(eventType)
533-
for key, value := range attributes {
564+
for _, key := range sortedAttributeKeys(attributes) {
565+
value := attributes[key]
534566
attribute := sdk.NewAttribute(key.String(), value)
535567
res = res.AppendAttributes(attribute)
536568
}
@@ -545,7 +577,8 @@ func NewEventGrantPermTokenBody(event EventGranted) sdk.Event {
545577
}
546578

547579
res := sdk.NewEvent(eventType)
548-
for key, value := range attributes {
580+
for _, key := range sortedAttributeKeys(attributes) {
581+
value := attributes[key]
549582
attribute := sdk.NewAttribute(key.String(), value)
550583
res = res.AppendAttributes(attribute)
551584
}
@@ -562,7 +595,8 @@ func NewEventRevokePermToken(event EventRenounced) sdk.Event {
562595
}
563596

564597
res := sdk.NewEvent(eventType)
565-
for key, value := range attributes {
598+
for _, key := range sortedAttributeKeys(attributes) {
599+
value := attributes[key]
566600
attribute := sdk.NewAttribute(key.String(), value)
567601
res = res.AppendAttributes(attribute)
568602
}
@@ -579,7 +613,8 @@ func NewEventApproveCollection(event EventAuthorizedOperator) sdk.Event {
579613
}
580614

581615
res := sdk.NewEvent(eventType)
582-
for key, value := range attributes {
616+
for _, key := range sortedAttributeKeys(attributes) {
617+
value := attributes[key]
583618
attribute := sdk.NewAttribute(key.String(), value)
584619
res = res.AppendAttributes(attribute)
585620
}
@@ -596,7 +631,8 @@ func NewEventDisapproveCollection(event EventRevokedOperator) sdk.Event {
596631
}
597632

598633
res := sdk.NewEvent(eventType)
599-
for key, value := range attributes {
634+
for _, key := range sortedAttributeKeys(attributes) {
635+
value := attributes[key]
600636
attribute := sdk.NewAttribute(key.String(), value)
601637
res = res.AppendAttributes(attribute)
602638
}
@@ -617,7 +653,8 @@ func NewEventAttachToken(event EventAttached, newRoot string) sdk.Event {
617653
}
618654

619655
res := sdk.NewEvent(eventType)
620-
for key, value := range attributes {
656+
for _, key := range sortedAttributeKeys(attributes) {
657+
value := attributes[key]
621658
attribute := sdk.NewAttribute(key.String(), value)
622659
res = res.AppendAttributes(attribute)
623660
}
@@ -637,7 +674,8 @@ func NewEventDetachToken(event EventDetached, oldRoot string) sdk.Event {
637674
}
638675

639676
res := sdk.NewEvent(eventType)
640-
for key, value := range attributes {
677+
for _, key := range sortedAttributeKeys(attributes) {
678+
value := attributes[key]
641679
attribute := sdk.NewAttribute(key.String(), value)
642680
res = res.AppendAttributes(attribute)
643681
}
@@ -659,7 +697,8 @@ func NewEventAttachFrom(event EventAttached, newRoot string) sdk.Event {
659697
}
660698

661699
res := sdk.NewEvent(eventType)
662-
for key, value := range attributes {
700+
for _, key := range sortedAttributeKeys(attributes) {
701+
value := attributes[key]
663702
attribute := sdk.NewAttribute(key.String(), value)
664703
res = res.AppendAttributes(attribute)
665704
}
@@ -680,7 +719,8 @@ func NewEventDetachFrom(event EventDetached, oldRoot string) sdk.Event {
680719
}
681720

682721
res := sdk.NewEvent(eventType)
683-
for key, value := range attributes {
722+
for _, key := range sortedAttributeKeys(attributes) {
723+
value := attributes[key]
684724
attribute := sdk.NewAttribute(key.String(), value)
685725
res = res.AppendAttributes(attribute)
686726
}
@@ -696,7 +736,8 @@ func NewEventOperationTransferNFT(event EventOwnerChanged) sdk.Event {
696736
}
697737

698738
res := sdk.NewEvent(eventType)
699-
for key, value := range attributes {
739+
for _, key := range sortedAttributeKeys(attributes) {
740+
value := attributes[key]
700741
attribute := sdk.NewAttribute(key.String(), value)
701742
res = res.AppendAttributes(attribute)
702743
}
@@ -712,7 +753,8 @@ func NewEventOperationBurnNFT(contractID string, tokenID string) sdk.Event {
712753
}
713754

714755
res := sdk.NewEvent(eventType)
715-
for key, value := range attributes {
756+
for _, key := range sortedAttributeKeys(attributes) {
757+
value := attributes[key]
716758
attribute := sdk.NewAttribute(key.String(), value)
717759
res = res.AppendAttributes(attribute)
718760
}
@@ -728,7 +770,8 @@ func NewEventOperationRootChanged(event EventRootChanged) sdk.Event {
728770
}
729771

730772
res := sdk.NewEvent(eventType)
731-
for key, value := range attributes {
773+
for _, key := range sortedAttributeKeys(attributes) {
774+
value := attributes[key]
732775
attribute := sdk.NewAttribute(key.String(), value)
733776
res = res.AppendAttributes(attribute)
734777
}

0 commit comments

Comments
 (0)