Skip to content

Commit

Permalink
move test helper function to test file
Browse files Browse the repository at this point in the history
  • Loading branch information
zsystm committed Sep 19, 2024
1 parent 5027d58 commit 828cb40
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
34 changes: 0 additions & 34 deletions client/x/evmengine/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,37 +87,3 @@ 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
}
37 changes: 36 additions & 1 deletion client/x/evmengine/types/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types_test
import (
"bytes"
"math/big"
"slices"
"testing"

"github.com/cometbft/cometbft/crypto"
Expand Down Expand Up @@ -428,7 +429,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))
require.True(t, isSortedEVMEvents(tc.evmEvents))
})
}
}
Expand All @@ -448,3 +449,37 @@ func createAddresses(count int) ([]crypto.PubKey, []sdk.AccAddress, []sdk.ValAdd

return pubKeys, accAddrs, valAddrs
}

// isSortedEVMEvents check if the events are sorted by ascending order of address, topics, and data.
func isSortedEVMEvents(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
}

0 comments on commit 828cb40

Please sign in to comment.