Skip to content

Commit

Permalink
move isSorted to types/tx.go
Browse files Browse the repository at this point in the history
it is directly related to EVMEvent so it should be defined in tx.go
  • Loading branch information
zsystm committed Sep 9, 2024
1 parent 881f7ea commit 68a4a5f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
38 changes: 1 addition & 37 deletions client/x/evmengine/keeper/evmmsgs_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package keeper

import (
"bytes"
"math/big"
"slices"
"testing"

"github.com/cometbft/cometbft/crypto"
Expand Down Expand Up @@ -121,7 +119,7 @@ func TestKeeper_evmEvents(t *testing.T) {
} else {
require.NoError(t, err)
require.Equal(t, tc.expectedResult, events)
require.True(t, isSorted(events), "events are not sorted")
require.True(t, types.IsSortedEVMEvents(events), "events are not sorted")
}
})
}
Expand Down Expand Up @@ -191,37 +189,3 @@ func mustGetABI(metadata *bind.MetaData) *abi.ABI {

return abi
}

// Helper function to check if the events are sorted by ascending order of address, topics, and data.
func isSorted(events []*types.EVMEvent) bool {
for i := 1; i < len(events); i++ {
// Compare addresses first
addressComparison := bytes.Compare(events[i-1].Address, events[i].Address)
if addressComparison > 0 {
// it is not sorted by ascending order of address
return false
}

if addressComparison == 0 {
// If addresses are equal, compare by topics
previousTopic := slices.Concat(events[i-1].Topics...)
currentTopic := slices.Concat(events[i].Topics...)
topicComparison := bytes.Compare(previousTopic, currentTopic)

if topicComparison > 0 {
// it is not sorted by ascending order of topics
return false
}

if topicComparison == 0 {
// If topics are also equal, compare by data
if bytes.Compare(events[i-1].Data, events[i].Data) > 0 {
// it is not sorted by ascending order of data
return false
}
}
}
}

return true
}
34 changes: 34 additions & 0 deletions client/x/evmengine/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,37 @@ func SortEVMEvents(events []*EVMEvent) {
return bytes.Compare(events[i].Data, events[j].Data) < 0
})
}

// IsSortedEVMEvents check if the events are sorted by ascending order of address, topics, and data.
func IsSortedEVMEvents(events []*EVMEvent) bool {
for i := 1; i < len(events); i++ {
// Compare addresses first
addressComparison := bytes.Compare(events[i-1].Address, events[i].Address)
if addressComparison > 0 {
// it is not sorted by ascending order of address
return false
}

if addressComparison == 0 {
// If addresses are equal, compare by topics
previousTopic := slices.Concat(events[i-1].Topics...)
currentTopic := slices.Concat(events[i].Topics...)
topicComparison := bytes.Compare(previousTopic, currentTopic)

if topicComparison > 0 {
// it is not sorted by ascending order of topics
return false
}

if topicComparison == 0 {
// If topics are also equal, compare by data
if bytes.Compare(events[i-1].Data, events[i].Data) > 0 {
// it is not sorted by ascending order of data
return false
}
}
}
}

return true
}
1 change: 1 addition & 0 deletions client/x/evmengine/types/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ func TestSortEVMEvents(t *testing.T) {
t.Parallel()
types.SortEVMEvents(tc.evmEvents)
require.Equal(t, tc.expectedResult, tc.evmEvents)
require.True(t, types.IsSortedEVMEvents(tc.evmEvents))
})
}
}
Expand Down

0 comments on commit 68a4a5f

Please sign in to comment.