consensus/clique: use slices package for sorting (#27488)

Also adds Address.Less for sorting use in other packages.

---------

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
Dan Laine 2023-06-19 03:04:16 -04:00 committed by GitHub
parent 4367ab499f
commit a848212709
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 11 deletions

View File

@ -226,6 +226,11 @@ func IsHexAddress(s string) bool {
return len(s) == 2*AddressLength && isHex(s) return len(s) == 2*AddressLength && isHex(s)
} }
// Less compares two addresses.
func (a Address) Less(other Address) bool {
return bytes.Compare(a[:], other[:]) < 0
}
// Bytes gets the string representation of the underlying address. // Bytes gets the string representation of the underlying address.
func (a Address) Bytes() []byte { return a[:] } func (a Address) Bytes() []byte { return a[:] }

View File

@ -19,7 +19,6 @@ package clique
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"sort"
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -29,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"golang.org/x/exp/slices"
) )
// Vote represents a single vote that an authorized signer made to modify the // Vote represents a single vote that an authorized signer made to modify the
@ -62,13 +62,6 @@ type Snapshot struct {
Tally map[common.Address]Tally `json:"tally"` // Current vote tally to avoid recalculating Tally map[common.Address]Tally `json:"tally"` // Current vote tally to avoid recalculating
} }
// signersAscending implements the sort interface to allow sorting a list of addresses
type signersAscending []common.Address
func (s signersAscending) Len() int { return len(s) }
func (s signersAscending) Less(i, j int) bool { return bytes.Compare(s[i][:], s[j][:]) < 0 }
func (s signersAscending) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// newSnapshot creates a new snapshot with the specified startup parameters. This // newSnapshot creates a new snapshot with the specified startup parameters. This
// method does not initialize the set of recent signers, so only ever use if for // method does not initialize the set of recent signers, so only ever use if for
// the genesis block. // the genesis block.
@ -315,7 +308,7 @@ func (s *Snapshot) signers() []common.Address {
for sig := range s.Signers { for sig := range s.Signers {
sigs = append(sigs, sig) sigs = append(sigs, sig)
} }
sort.Sort(signersAscending(sigs)) slices.SortFunc(sigs, common.Address.Less)
return sigs return sigs
} }

View File

@ -21,7 +21,6 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"fmt" "fmt"
"math/big" "math/big"
"sort"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -31,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"golang.org/x/exp/slices"
) )
// testerAccountPool is a pool to maintain currently active tester accounts, // testerAccountPool is a pool to maintain currently active tester accounts,
@ -53,7 +53,7 @@ func (ap *testerAccountPool) checkpoint(header *types.Header, signers []string)
for i, signer := range signers { for i, signer := range signers {
auths[i] = ap.address(signer) auths[i] = ap.address(signer)
} }
sort.Sort(signersAscending(auths)) slices.SortFunc(auths, common.Address.Less)
for i, auth := range auths { for i, auth := range auths {
copy(header.Extra[extraVanity+i*common.AddressLength:], auth.Bytes()) copy(header.Extra[extraVanity+i*common.AddressLength:], auth.Bytes())
} }