From 31dcbc7645c94c62ff6c5e92a3c4285c8de60a7e Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Tue, 9 Sep 2025 15:12:22 +0800 Subject: [PATCH] eth/tracers: use slices package for sorting #27490 (#1337) --- common/types.go | 5 +++++ eth/tracers/api_test.go | 13 +++---------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/common/types.go b/common/types.go index c51b1a9645..d6a68c8ba9 100644 --- a/common/types.go +++ b/common/types.go @@ -251,6 +251,11 @@ func (a Address) IsZero() bool { return a == Address{} } // Str gets the string representation of the underlying address func (a Address) Str() string { return string(a[:]) } +// Cmp compares two addresses. +func (a Address) Cmp(other Address) int { + return bytes.Compare(a[:], other[:]) +} + // Bytes gets the string representation of the underlying address. func (a Address) Bytes() []byte { return a[:] } diff --git a/eth/tracers/api_test.go b/eth/tracers/api_test.go index b1c6d9422d..1c633c9e05 100644 --- a/eth/tracers/api_test.go +++ b/eth/tracers/api_test.go @@ -17,7 +17,6 @@ package tracers import ( - "bytes" "context" "crypto/ecdsa" "encoding/json" @@ -25,7 +24,7 @@ import ( "fmt" "math/big" "reflect" - "sort" + "slices" "sync/atomic" "testing" "time" @@ -635,19 +634,13 @@ type Account struct { addr common.Address } -type Accounts []Account - -func (a Accounts) Len() int { return len(a) } -func (a Accounts) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a Accounts) Less(i, j int) bool { return bytes.Compare(a[i].addr.Bytes(), a[j].addr.Bytes()) < 0 } - -func newAccounts(n int) (accounts Accounts) { +func newAccounts(n int) (accounts []Account) { for i := 0; i < n; i++ { key, _ := crypto.GenerateKey() addr := crypto.PubkeyToAddress(key.PublicKey) accounts = append(accounts, Account{key: key, addr: addr}) } - sort.Sort(accounts) + slices.SortFunc(accounts, func(a, b Account) int { return a.addr.Cmp(b.addr) }) return accounts }