Skip to content

Commit 49f63de

Browse files
committed
consensus/clique: replace bubble sort by golang stable sort
1 parent afd8b84 commit 49f63de

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

‎consensus/clique/snapshot.go‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package clique
1919
import (
2020
"bytes"
2121
"encoding/json"
22+
"sort"
2223

2324
"github.com/ethereum/go-ethereum/common"
2425
"github.com/ethereum/go-ethereum/core/types"
@@ -56,6 +57,13 @@ type Snapshot struct {
5657
Tally map[common.Address]Tally `json:"tally"` // Current vote tally to avoid recalculating
5758
}
5859

60+
// signers implements the sort interface to allow sorting a list of addresses
61+
type signers []common.Address
62+
63+
func (s signers) Len() int { return len(s) }
64+
func (s signers) Less(i, j int) bool { return bytes.Compare(s[i][:], s[j][:]) < 0 }
65+
func (s signers) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
66+
5967
// newSnapshot creates a new snapshot with the specified startup parameters. This
6068
// method does not initialize the set of recent signers, so only ever use if for
6169
// the genesis block.
@@ -286,18 +294,12 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
286294

287295
// signers retrieves the list of authorized signers in ascending order.
288296
func (s *Snapshot) signers() []common.Address {
289-
signers := make([]common.Address, 0, len(s.Signers))
290-
for signer := range s.Signers {
291-
signers = append(signers, signer)
292-
}
293-
for i := 0; i < len(signers); i++ {
294-
for j := i + 1; j < len(signers); j++ {
295-
if bytes.Compare(signers[i][:], signers[j][:]) > 0 {
296-
signers[i], signers[j] = signers[j], signers[i]
297-
}
298-
}
297+
sigs := make([]common.Address, 0, len(s.Signers))
298+
for sig := range s.Signers {
299+
sigs = append(sigs, sig)
299300
}
300-
return signers
301+
sort.Sort(signers(sigs))
302+
return sigs
301303
}
302304

303305
// inturn returns if a signer at a given block height is in-turn or not.

0 commit comments

Comments
 (0)