Skip to content

Commit cc21928

Browse files
authored
Merge pull request #17622 from karalabe/chain-maker-seal
consensus/clique, core: chain maker clique + error tests
2 parents 7c71e93 + 4bb2504 commit cc21928

5 files changed

Lines changed: 193 additions & 73 deletions

File tree

‎consensus/clique/clique.go‎

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,33 @@ var (
9393

9494
// errMissingSignature is returned if a block's extra-data section doesn't seem
9595
// to contain a 65 byte secp256k1 signature.
96-
errMissingSignature = errors.New("extra-data 65 byte suffix signature missing")
96+
errMissingSignature = errors.New("extra-data 65 byte signature suffix missing")
9797

9898
// errExtraSigners is returned if non-checkpoint block contain signer data in
9999
// their extra-data fields.
100100
errExtraSigners = errors.New("non-checkpoint block contains extra signer list")
101101

102102
// errInvalidCheckpointSigners is returned if a checkpoint block contains an
103-
// invalid list of signers (i.e. non divisible by 20 bytes, or not the correct
104-
// ones).
103+
// invalid list of signers (i.e. non divisible by 20 bytes).
105104
errInvalidCheckpointSigners = errors.New("invalid signer list on checkpoint block")
106105

106+
// errMismatchingCheckpointSigners is returned if a checkpoint block contains a
107+
// list of signers different than the one the local node calculated.
108+
errMismatchingCheckpointSigners = errors.New("mismatching signer list on checkpoint block")
109+
107110
// errInvalidMixDigest is returned if a block's mix digest is non-zero.
108111
errInvalidMixDigest = errors.New("non-zero mix digest")
109112

110113
// errInvalidUncleHash is returned if a block contains an non-empty uncle list.
111114
errInvalidUncleHash = errors.New("non empty uncle hash")
112115

113-
// errInvalidDifficulty is returned if the difficulty of a block is not either
114-
// of 1 or 2, or if the value does not match the turn of the signer.
116+
// errInvalidDifficulty is returned if the difficulty of a block neither 1 or 2.
115117
errInvalidDifficulty = errors.New("invalid difficulty")
116118

119+
// errWrongDifficulty is returned if the difficulty of a block doesn't match the
120+
// turn of the signer.
121+
errWrongDifficulty = errors.New("wrong difficulty")
122+
117123
// ErrInvalidTimestamp is returned if the timestamp of a block is lower than
118124
// the previous block's timestamp + the minimum block period.
119125
ErrInvalidTimestamp = errors.New("invalid timestamp")
@@ -122,8 +128,12 @@ var (
122128
// be modified via out-of-range or non-contiguous headers.
123129
errInvalidVotingChain = errors.New("invalid voting chain")
124130

125-
// errUnauthorized is returned if a header is signed by a non-authorized entity.
126-
errUnauthorized = errors.New("unauthorized")
131+
// errUnauthorizedSigner is returned if a header is signed by a non-authorized entity.
132+
errUnauthorizedSigner = errors.New("unauthorized signer")
133+
134+
// errRecentlySigned is returned if a header is signed by an authorized entity
135+
// that already signed a header recently, thus is temporarily not allowed to.
136+
errRecentlySigned = errors.New("recently signed")
127137

128138
// errWaitTransactions is returned if an empty block is attempted to be sealed
129139
// on an instant chain (0 second period). It's important to refuse these as the
@@ -205,6 +215,9 @@ type Clique struct {
205215
signer common.Address // Ethereum address of the signing key
206216
signFn SignerFn // Signer function to authorize hashes with
207217
lock sync.RWMutex // Protects the signer fields
218+
219+
// The fields below are for testing only
220+
fakeDiff bool // Skip difficulty verifications
208221
}
209222

210223
// New creates a Clique proof-of-authority consensus engine with the initial
@@ -359,7 +372,7 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainReader, header *type
359372
}
360373
extraSuffix := len(header.Extra) - extraSeal
361374
if !bytes.Equal(header.Extra[extraVanity:extraSuffix], signers) {
362-
return errInvalidCheckpointSigners
375+
return errMismatchingCheckpointSigners
363376
}
364377
}
365378
// All basic checks passed, verify the seal and return
@@ -481,23 +494,25 @@ func (c *Clique) verifySeal(chain consensus.ChainReader, header *types.Header, p
481494
return err
482495
}
483496
if _, ok := snap.Signers[signer]; !ok {
484-
return errUnauthorized
497+
return errUnauthorizedSigner
485498
}
486499
for seen, recent := range snap.Recents {
487500
if recent == signer {
488501
// Signer is among recents, only fail if the current block doesn't shift it out
489502
if limit := uint64(len(snap.Signers)/2 + 1); seen > number-limit {
490-
return errUnauthorized
503+
return errRecentlySigned
491504
}
492505
}
493506
}
494507
// Ensure that the difficulty corresponds to the turn-ness of the signer
495-
inturn := snap.inturn(header.Number.Uint64(), signer)
496-
if inturn && header.Difficulty.Cmp(diffInTurn) != 0 {
497-
return errInvalidDifficulty
498-
}
499-
if !inturn && header.Difficulty.Cmp(diffNoTurn) != 0 {
500-
return errInvalidDifficulty
508+
if !c.fakeDiff {
509+
inturn := snap.inturn(header.Number.Uint64(), signer)
510+
if inturn && header.Difficulty.Cmp(diffInTurn) != 0 {
511+
return errWrongDifficulty
512+
}
513+
if !inturn && header.Difficulty.Cmp(diffNoTurn) != 0 {
514+
return errWrongDifficulty
515+
}
501516
}
502517
return nil
503518
}
@@ -613,7 +628,7 @@ func (c *Clique) Seal(chain consensus.ChainReader, block *types.Block, results c
613628
return err
614629
}
615630
if _, authorized := snap.Signers[signer]; !authorized {
616-
return errUnauthorized
631+
return errUnauthorizedSigner
617632
}
618633
// If we're amongst the recent signers, wait for the next block
619634
for seen, recent := range snap.Recents {

‎consensus/clique/snapshot.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ type Snapshot struct {
5757
Tally map[common.Address]Tally `json:"tally"` // Current vote tally to avoid recalculating
5858
}
5959

60-
// signers implements the sort interface to allow sorting a list of addresses
61-
type signers []common.Address
60+
// signersAscending implements the sort interface to allow sorting a list of addresses
61+
type signersAscending []common.Address
6262

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] }
63+
func (s signersAscending) Len() int { return len(s) }
64+
func (s signersAscending) Less(i, j int) bool { return bytes.Compare(s[i][:], s[j][:]) < 0 }
65+
func (s signersAscending) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
6666

6767
// newSnapshot creates a new snapshot with the specified startup parameters. This
6868
// method does not initialize the set of recent signers, so only ever use if for
@@ -214,11 +214,11 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
214214
return nil, err
215215
}
216216
if _, ok := snap.Signers[signer]; !ok {
217-
return nil, errUnauthorized
217+
return nil, errUnauthorizedSigner
218218
}
219219
for _, recent := range snap.Recents {
220220
if recent == signer {
221-
return nil, errUnauthorized
221+
return nil, errRecentlySigned
222222
}
223223
}
224224
snap.Recents[number] = signer
@@ -298,7 +298,7 @@ func (s *Snapshot) signers() []common.Address {
298298
for sig := range s.Signers {
299299
sigs = append(sigs, sig)
300300
}
301-
sort.Sort(signers(sigs))
301+
sort.Sort(signersAscending(sigs))
302302
return sigs
303303
}
304304

0 commit comments

Comments
 (0)