Skip to content

Commit

Permalink
Revert "consensus/clique: use slices package for sorting (ethereum#27488
Browse files Browse the repository at this point in the history
)"

This reverts commit cd00ee3.
  • Loading branch information
devopsbo3 authored Nov 10, 2023
1 parent 9865d22 commit 5a8dd2a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
5 changes: 0 additions & 5 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,6 @@ func IsHexAddress(s string) bool {
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.
func (a Address) Bytes() []byte { return a[:] }

Expand Down
11 changes: 9 additions & 2 deletions consensus/clique/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package clique
import (
"bytes"
"encoding/json"
"sort"
"time"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -28,7 +29,6 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"golang.org/x/exp/slices"
)

// Vote represents a single vote that an authorized signer made to modify the
Expand Down Expand Up @@ -62,6 +62,13 @@ type Snapshot struct {
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
// method does not initialize the set of recent signers, so only ever use if for
// the genesis block.
Expand Down Expand Up @@ -308,7 +315,7 @@ func (s *Snapshot) signers() []common.Address {
for sig := range s.Signers {
sigs = append(sigs, sig)
}
slices.SortFunc(sigs, common.Address.Less)
sort.Sort(signersAscending(sigs))
return sigs
}

Expand Down
4 changes: 2 additions & 2 deletions consensus/clique/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/ecdsa"
"fmt"
"math/big"
"sort"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -30,7 +31,6 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"golang.org/x/exp/slices"
)

// testerAccountPool is a pool to maintain currently active tester accounts,
Expand All @@ -53,7 +53,7 @@ func (ap *testerAccountPool) checkpoint(header *types.Header, signers []string)
for i, signer := range signers {
auths[i] = ap.address(signer)
}
slices.SortFunc(auths, common.Address.Less)
sort.Sort(signersAscending(auths))
for i, auth := range auths {
copy(header.Extra[extraVanity+i*common.AddressLength:], auth.Bytes())
}
Expand Down

0 comments on commit 5a8dd2a

Please sign in to comment.