Skip to content

Commit

Permalink
fix: check event determinism on original modules (#963)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
0Tech authored Apr 13, 2023
1 parent 631ca58 commit f656c7a
Show file tree
Hide file tree
Showing 10 changed files with 535 additions and 87 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/foundation) [\#933](https://github.com/line/lbm-sdk/pull/933) Clean up x/foundation apis
* (x/collection) [\#938](https://github.com/line/lbm-sdk/pull/938) Add progress log into x/collection import-genesis
* (x/foundation) [\#952](https://github.com/line/lbm-sdk/pull/952) Address generation of the empty coins in x/foundation
* (x/collection,token,foundation) [\#963](https://github.com/line/lbm-sdk/pull/963) Check event determinism on original modules

### Bug Fixes
* (swagger) [\#898](https://github.com/line/lbm-sdk/pull/898) fix a bug not added `lbm.tx.v1beta1.Service/GetBlockWithTxs` in swagger
Expand Down
103 changes: 73 additions & 30 deletions x/collection/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package collection

import (
"fmt"
"sort"
"strings"

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

func sortedAttributeKeys(m map[AttributeKey]string) []AttributeKey {
keys := make([]AttributeKey, 0, len(m))
for key := range m {
keys = append(keys, key)
}

sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
return keys
}

// Deprecated: use EventCreatedContract.
func NewEventCreateCollection(event EventCreatedContract) sdk.Event {
eventType := EventTypeCreateCollection.String()
Expand All @@ -46,7 +59,8 @@ func NewEventCreateCollection(event EventCreatedContract) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -70,7 +84,8 @@ func NewEventIssueFT(event EventCreatedFTClass, to sdk.AccAddress, amount sdk.In
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -88,7 +103,8 @@ func NewEventIssueNFT(event EventCreatedNFTClass) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -106,7 +122,8 @@ func NewEventMintFT(event EventMintedFT) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -129,7 +146,8 @@ func NewEventMintNFT(event EventMintedNFT) sdk.Events {
AttributeKeyName: token.Name,
AttributeKeyMeta: token.Meta,
}
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
e = e.AppendAttributes(attribute)
}
Expand Down Expand Up @@ -161,7 +179,8 @@ func NewEventBurnFT(event EventBurned) *sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand Down Expand Up @@ -189,7 +208,8 @@ func NewEventBurnNFT(event EventBurned) sdk.Events {
AttributeKeyFrom: event.From,
}
head := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
head = head.AppendAttributes(attribute)
}
Expand Down Expand Up @@ -226,7 +246,8 @@ func NewEventBurnFTFrom(event EventBurned) *sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand Down Expand Up @@ -255,7 +276,8 @@ func NewEventBurnNFTFrom(event EventBurned) sdk.Events {
AttributeKeyFrom: event.From,
}
head := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
head = head.AppendAttributes(attribute)
}
Expand All @@ -279,7 +301,8 @@ func NewEventModifyCollection(event EventModifiedContract) sdk.Events {
AttributeKeyContractID: event.ContractId,
}
head := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
head = head.AppendAttributes(attribute)
}
Expand All @@ -303,7 +326,8 @@ func NewEventModifyTokenType(event EventModifiedTokenClass) sdk.Events {
AttributeKeyTokenType: event.TokenType,
}
head := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
head = head.AppendAttributes(attribute)
}
Expand All @@ -328,7 +352,8 @@ func NewEventModifyTokenOfFTClass(event EventModifiedTokenClass) sdk.Events {
AttributeKeyTokenID: tokenID,
}
head := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
head = head.AppendAttributes(attribute)
}
Expand All @@ -352,7 +377,8 @@ func NewEventModifyTokenOfNFT(event EventModifiedNFT) sdk.Events {
AttributeKeyTokenID: event.TokenId,
}
head := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
head = head.AppendAttributes(attribute)
}
Expand Down Expand Up @@ -388,7 +414,8 @@ func NewEventTransferFT(event EventSent) *sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand Down Expand Up @@ -417,7 +444,8 @@ func NewEventTransferNFT(event EventSent) sdk.Events {
AttributeKeyTo: event.To,
}
head := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
head = head.AppendAttributes(attribute)
}
Expand Down Expand Up @@ -454,7 +482,8 @@ func NewEventTransferFTFrom(event EventSent) *sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand Down Expand Up @@ -484,7 +513,8 @@ func NewEventTransferNFTFrom(event EventSent) sdk.Events {
AttributeKeyTo: event.To,
}
head := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
head = head.AppendAttributes(attribute)
}
Expand All @@ -511,7 +541,8 @@ func NewEventGrantPermToken(event EventGranted) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -530,7 +561,8 @@ func NewEventGrantPermTokenHead(event EventGranted) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -545,7 +577,8 @@ func NewEventGrantPermTokenBody(event EventGranted) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -562,7 +595,8 @@ func NewEventRevokePermToken(event EventRenounced) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -579,7 +613,8 @@ func NewEventApproveCollection(event EventAuthorizedOperator) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -596,7 +631,8 @@ func NewEventDisapproveCollection(event EventRevokedOperator) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -617,7 +653,8 @@ func NewEventAttachToken(event EventAttached, newRoot string) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -637,7 +674,8 @@ func NewEventDetachToken(event EventDetached, oldRoot string) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -659,7 +697,8 @@ func NewEventAttachFrom(event EventAttached, newRoot string) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -680,7 +719,8 @@ func NewEventDetachFrom(event EventDetached, oldRoot string) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -696,7 +736,8 @@ func NewEventOperationTransferNFT(event EventOwnerChanged) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -712,7 +753,8 @@ func NewEventOperationBurnNFT(contractID string, tokenID string) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand All @@ -728,7 +770,8 @@ func NewEventOperationRootChanged(event EventRootChanged) sdk.Event {
}

res := sdk.NewEvent(eventType)
for key, value := range attributes {
for _, key := range sortedAttributeKeys(attributes) {
value := attributes[key]
attribute := sdk.NewAttribute(key.String(), value)
res = res.AppendAttributes(attribute)
}
Expand Down
Loading

0 comments on commit f656c7a

Please sign in to comment.