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 -10
View File
@@ -17,7 +17,6 @@
package tracers
import (
"bytes"
"context"
"crypto/ecdsa"
"encoding/json"
@@ -25,7 +24,6 @@ import (
"fmt"
"math/big"
"reflect"
"sort"
"sync/atomic"
"testing"
"time"
@@ -45,6 +43,7 @@ import (
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"golang.org/x/exp/slices"
)
var (
@@ -785,19 +784,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) bool { return a.addr.Less(b.addr) })
return accounts
}