eth/tracers: use slices package for sorting #27490 (#1337)

This commit is contained in:
Daniel Liu 2025-09-09 15:12:22 +08:00 committed by GitHub
parent 525a84eae4
commit 31dcbc7645
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 10 deletions

View file

@ -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[:] }

View file

@ -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
}