eth: use slices package for sorting #27490 (#1702)

This commit is contained in:
Daniel Liu 2025-12-07 18:13:37 +08:00 committed by GitHub
parent 62dbd3bceb
commit b4b6328544
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -21,7 +21,7 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"reflect" "reflect"
"sort" "slices"
"testing" "testing"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
@ -59,12 +59,6 @@ func accountRangeTest(t *testing.T, trie *state.Trie, statedb *state.StateDB, st
return result 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) { func TestAccountRange(t *testing.T) {
t.Parallel() t.Parallel()
@ -98,7 +92,7 @@ func TestAccountRange(t *testing.T) {
firstResult := accountRangeTest(t, &trie, sdb, common.Hash{}, AccountRangeMaxResults, AccountRangeMaxResults) firstResult := accountRangeTest(t, &trie, sdb, common.Hash{}, AccountRangeMaxResults, AccountRangeMaxResults)
secondResult := accountRangeTest(t, &trie, sdb, common.BytesToHash(firstResult.Next), AccountRangeMaxResults, AccountRangeMaxResults) secondResult := accountRangeTest(t, &trie, sdb, common.BytesToHash(firstResult.Next), AccountRangeMaxResults, AccountRangeMaxResults)
hList := make(resultHash, 0) hList := make([]common.Hash, 0)
for addr1 := range firstResult.Accounts { for addr1 := range firstResult.Accounts {
// If address is empty, then it makes no sense to compare // If address is empty, then it makes no sense to compare
// them as they might be two different accounts. // them as they might be two different accounts.
@ -112,7 +106,7 @@ func TestAccountRange(t *testing.T) {
} }
// Test to see if it's possible to recover from the middle of the previous // 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. // set and get an even split between the first and second sets.
sort.Sort(hList) slices.SortFunc(hList, common.Hash.Cmp)
middleH := hList[AccountRangeMaxResults/2] middleH := hList[AccountRangeMaxResults/2]
middleResult := accountRangeTest(t, &trie, sdb, middleH, AccountRangeMaxResults, AccountRangeMaxResults) middleResult := accountRangeTest(t, &trie, sdb, middleH, AccountRangeMaxResults, AccountRangeMaxResults)
missing, infirst, insecond := 0, 0, 0 missing, infirst, insecond := 0, 0, 0