eth: use slices package for sorting (#27490)

Also adds Hash.Less method for sorting purposes.

---------

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
Dan Laine
2023-06-19 23:38:57 +02:00
committed by GitHub
co-authored by Felix Lange
parent 9a167c45d1
commit b1ef0bfe03
6 changed files with 61 additions and 104 deletions
+3 -9
View File
@@ -21,7 +21,6 @@ import (
"fmt"
"math/big"
"reflect"
"sort"
"testing"
"github.com/davecgh/go-spew/spew"
@@ -31,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/trie"
"golang.org/x/exp/slices"
)
var dumper = spew.ConfigState{Indent: " "}
@@ -58,12 +58,6 @@ func accountRangeTest(t *testing.T, trie *state.Trie, statedb *state.StateDB, st
return result
}
type resultHash []common.Hash
func (h resultHash) Len() int { return len(h) }
func (h resultHash) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h resultHash) Less(i, j int) bool { return bytes.Compare(h[i].Bytes(), h[j].Bytes()) < 0 }
func TestAccountRange(t *testing.T) {
t.Parallel()
@@ -97,7 +91,7 @@ func TestAccountRange(t *testing.T) {
firstResult := accountRangeTest(t, &trie, state, common.Hash{}, AccountRangeMaxResults, AccountRangeMaxResults)
secondResult := accountRangeTest(t, &trie, state, common.BytesToHash(firstResult.Next), AccountRangeMaxResults, AccountRangeMaxResults)
hList := make(resultHash, 0)
hList := make([]common.Hash, 0)
for addr1 := range firstResult.Accounts {
// If address is empty, then it makes no sense to compare
// them as they might be two different accounts.
@@ -111,7 +105,7 @@ func TestAccountRange(t *testing.T) {
}
// Test to see if it's possible to recover from the middle of the previous
// set and get an even split between the first and second sets.
sort.Sort(hList)
slices.SortFunc(hList, common.Hash.Less)
middleH := hList[AccountRangeMaxResults/2]
middleResult := accountRangeTest(t, &trie, state, middleH, AccountRangeMaxResults, AccountRangeMaxResults)
missing, infirst, insecond := 0, 0, 0