From 828cb405aa5786f3324a346a24d55b965cb1cf03 Mon Sep 17 00:00:00 2001 From: zsystm Date: Thu, 19 Sep 2024 14:51:40 +0900 Subject: [PATCH] move test helper function to test file --- client/x/evmengine/types/tx.go | 34 -------------------------- client/x/evmengine/types/tx_test.go | 37 ++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/client/x/evmengine/types/tx.go b/client/x/evmengine/types/tx.go index 99a90292..5ed1369d 100644 --- a/client/x/evmengine/types/tx.go +++ b/client/x/evmengine/types/tx.go @@ -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 -} diff --git a/client/x/evmengine/types/tx_test.go b/client/x/evmengine/types/tx_test.go index 26a1da94..1c745f36 100644 --- a/client/x/evmengine/types/tx_test.go +++ b/client/x/evmengine/types/tx_test.go @@ -3,6 +3,7 @@ package types_test import ( "bytes" "math/big" + "slices" "testing" "github.com/cometbft/cometbft/crypto" @@ -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)) }) } } @@ -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 +}