Merge pull request #17206 from hadv/master

consensus/clique: replace bubble sort by golang stable sort
This commit is contained in:
Péter Szilágyi 2018-07-24 13:46:37 +03:00 committed by GitHub
commit 62467e4405
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,6 +19,7 @@ package clique
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"sort"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
@ -56,6 +57,13 @@ 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
} }
// signers implements the sort interface to allow sorting a list of addresses
type signers []common.Address
func (s signers) Len() int { return len(s) }
func (s signers) Less(i, j int) bool { return bytes.Compare(s[i][:], s[j][:]) < 0 }
func (s signers) 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.
@ -286,18 +294,12 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
// signers retrieves the list of authorized signers in ascending order. // signers retrieves the list of authorized signers in ascending order.
func (s *Snapshot) signers() []common.Address { func (s *Snapshot) signers() []common.Address {
signers := make([]common.Address, 0, len(s.Signers)) sigs := make([]common.Address, 0, len(s.Signers))
for signer := range s.Signers { for sig := range s.Signers {
signers = append(signers, signer) sigs = append(sigs, sig)
} }
for i := 0; i < len(signers); i++ { sort.Sort(signers(sigs))
for j := i + 1; j < len(signers); j++ { return sigs
if bytes.Compare(signers[i][:], signers[j][:]) > 0 {
signers[i], signers[j] = signers[j], signers[i]
}
}
}
return signers
} }
// inturn returns if a signer at a given block height is in-turn or not. // inturn returns if a signer at a given block height is in-turn or not.