@@ -40,6 +40,7 @@ import (
4040 "github.com/ethereum/go-ethereum/ethdb"
4141 "github.com/ethereum/go-ethereum/log"
4242 "github.com/ethereum/go-ethereum/params"
43+ "github.com/ethereum/go-ethereum/params/forks"
4344 "github.com/ethereum/go-ethereum/rlp"
4445 "github.com/ethereum/go-ethereum/trie"
4546 "golang.org/x/crypto/sha3"
@@ -51,20 +52,25 @@ const (
5152 inmemorySignatures = 4096 // Number of recent block signatures to keep in memory
5253)
5354
55+ const (
56+ ExtraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity
57+ ExtraSeal = crypto .SignatureLength // Fixed number of extra-data suffix bytes reserved for signer seal
58+
59+ DiffInTurn = 2 // Block difficulty for in-turn signatures
60+ DiffNoTurn = 1 // Block difficulty for out-of-turn signatures
61+ )
62+
5463// Clique proof-of-authority protocol constants.
5564var (
5665 epochLength = uint64 (30000 ) // Default number of blocks after which to checkpoint and reset the pending votes
5766
58- extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity
59- extraSeal = crypto .SignatureLength // Fixed number of extra-data suffix bytes reserved for signer seal
60-
6167 nonceAuthVote = hexutil .MustDecode ("0xffffffffffffffff" ) // Magic nonce number to vote on adding a new signer
6268 nonceDropVote = hexutil .MustDecode ("0x0000000000000000" ) // Magic nonce number to vote on removing a signer.
6369
6470 uncleHash = types .CalcUncleHash (nil ) // Always Keccak256(RLP([])) as uncles are meaningless outside of PoW.
6571
66- diffInTurn = big .NewInt (2 ) // Block difficulty for in-turn signatures
67- diffNoTurn = big .NewInt (1 ) // Block difficulty for out-of-turn signatures
72+ diffInTurn = big .NewInt (DiffInTurn )
73+ diffNoTurn = big .NewInt (DiffNoTurn )
6874)
6975
7076// Various error messages to mark blocks invalid. These should be private to
@@ -145,10 +151,10 @@ func ecrecover(header *types.Header, sigcache *sigLRU) (common.Address, error) {
145151 return address , nil
146152 }
147153 // Retrieve the signature from the header extra-data
148- if len (header .Extra ) < extraSeal {
154+ if len (header .Extra ) < ExtraSeal {
149155 return common.Address {}, errMissingSignature
150156 }
151- signature := header .Extra [len (header .Extra )- extraSeal :]
157+ signature := header .Extra [len (header .Extra )- ExtraSeal :]
152158
153159 // Recover the public key and the Ethereum address
154160 pubkey , err := crypto .Ecrecover (SealHash (header ).Bytes (), signature )
@@ -165,8 +171,8 @@ func ecrecover(header *types.Header, sigcache *sigLRU) (common.Address, error) {
165171// Clique is the proof-of-authority consensus engine proposed to support the
166172// Ethereum testnet following the Ropsten attacks.
167173type Clique struct {
168- config * params. CliqueConfig // Consensus engine configuration parameters
169- db ethdb.Database // Database to store and retrieve snapshot checkpoints
174+ config * Config // Consensus engine configuration parameters
175+ db ethdb.Database // Database to store and retrieve snapshot checkpoints
170176
171177 recents * lru.Cache [common.Hash , * Snapshot ] // Snapshots for recent block to speed up reorgs
172178 signatures * sigLRU // Signatures of recent blocks to speed up mining
@@ -182,18 +188,17 @@ type Clique struct {
182188
183189// New creates a Clique proof-of-authority consensus engine with the initial
184190// signers set to the ones provided by the user.
185- func New (config * params. CliqueConfig , db ethdb.Database ) * Clique {
191+ func New (config Config , db ethdb.Database ) * Clique {
186192 // Set any missing consensus parameters to their defaults
187- conf := * config
188- if conf .Epoch == 0 {
189- conf .Epoch = epochLength
193+ if config .Epoch == 0 {
194+ config .Epoch = epochLength
190195 }
191196 // Allocate the snapshot caches and create the engine
192197 recents := lru.NewCache [common.Hash , * Snapshot ](inmemorySnapshots )
193198 signatures := lru.NewCache [common.Hash , common.Address ](inmemorySignatures )
194199
195200 return & Clique {
196- config : & conf ,
201+ config : & config ,
197202 db : db ,
198203 recents : recents ,
199204 signatures : signatures ,
@@ -260,14 +265,14 @@ func (c *Clique) verifyHeader(chain consensus.ChainHeaderReader, header *types.H
260265 return errInvalidCheckpointVote
261266 }
262267 // Check that the extra-data contains both the vanity and signature
263- if len (header .Extra ) < extraVanity {
268+ if len (header .Extra ) < ExtraVanity {
264269 return errMissingVanity
265270 }
266- if len (header .Extra ) < extraVanity + extraSeal {
271+ if len (header .Extra ) < ExtraVanity + ExtraSeal {
267272 return errMissingSignature
268273 }
269274 // Ensure that the extra-data contains a signer list on checkpoint, but none otherwise
270- signersBytes := len (header .Extra ) - extraVanity - extraSeal
275+ signersBytes := len (header .Extra ) - ExtraVanity - ExtraSeal
271276 if ! checkpoint && signersBytes != 0 {
272277 return errExtraSigners
273278 }
@@ -292,14 +297,14 @@ func (c *Clique) verifyHeader(chain consensus.ChainHeaderReader, header *types.H
292297 if header .GasLimit > params .MaxGasLimit {
293298 return fmt .Errorf ("invalid gasLimit: have %v, max %v" , header .GasLimit , params .MaxGasLimit )
294299 }
295- if chain .Config ().IsShanghai ( header .Number , header .Time ) {
300+ if chain .Config ().Active ( forks . Shanghai , header .Number . Uint64 () , header .Time ) {
296301 return errors .New ("clique does not support shanghai fork" )
297302 }
298303 // Verify the non-existence of withdrawalsHash.
299304 if header .WithdrawalsHash != nil {
300305 return fmt .Errorf ("invalid withdrawalsHash: have %x, expected nil" , header .WithdrawalsHash )
301306 }
302- if chain .Config ().IsCancun ( header .Number , header .Time ) {
307+ if chain .Config ().Active ( forks . Cancun , header .Number . Uint64 () , header .Time ) {
303308 return errors .New ("clique does not support cancun fork" )
304309 }
305310 // Verify the non-existence of cancun-specific header fields
@@ -342,7 +347,7 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainHeaderReader, header
342347 if header .GasUsed > header .GasLimit {
343348 return fmt .Errorf ("invalid gasUsed: have %d, gasLimit %d" , header .GasUsed , header .GasLimit )
344349 }
345- if ! chain .Config ().IsLondon ( header .Number ) {
350+ if ! chain .Config ().Active ( forks . London , header .Number . Uint64 (), header . Time ) {
346351 // Verify BaseFee not present before EIP-1559 fork.
347352 if header .BaseFee != nil {
348353 return fmt .Errorf ("invalid baseFee before fork: have %d, want <nil>" , header .BaseFee )
@@ -365,8 +370,8 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainHeaderReader, header
365370 for i , signer := range snap .signers () {
366371 copy (signers [i * common .AddressLength :], signer [:])
367372 }
368- extraSuffix := len (header .Extra ) - extraSeal
369- if ! bytes .Equal (header .Extra [extraVanity :extraSuffix ], signers ) {
373+ extraSuffix := len (header .Extra ) - ExtraSeal
374+ if ! bytes .Equal (header .Extra [ExtraVanity :extraSuffix ], signers ) {
370375 return errMismatchingCheckpointSigners
371376 }
372377 }
@@ -404,9 +409,9 @@ func (c *Clique) snapshot(chain consensus.ChainHeaderReader, number uint64, hash
404409 if checkpoint != nil {
405410 hash := checkpoint .Hash ()
406411
407- signers := make ([]common.Address , (len (checkpoint .Extra )- extraVanity - extraSeal )/ common .AddressLength )
412+ signers := make ([]common.Address , (len (checkpoint .Extra )- ExtraVanity - ExtraSeal )/ common .AddressLength )
408413 for i := 0 ; i < len (signers ); i ++ {
409- copy (signers [i ][:], checkpoint .Extra [extraVanity + i * common .AddressLength :])
414+ copy (signers [i ][:], checkpoint .Extra [ExtraVanity + i * common .AddressLength :])
410415 }
411416 snap = newSnapshot (c .config , c .signatures , number , hash , signers )
412417 if err := snap .store (c .db ); err != nil {
@@ -544,17 +549,17 @@ func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header
544549 header .Difficulty = calcDifficulty (snap , signer )
545550
546551 // Ensure the extra data has all its components
547- if len (header .Extra ) < extraVanity {
548- header .Extra = append (header .Extra , bytes .Repeat ([]byte {0x00 }, extraVanity - len (header .Extra ))... )
552+ if len (header .Extra ) < ExtraVanity {
553+ header .Extra = append (header .Extra , bytes .Repeat ([]byte {0x00 }, ExtraVanity - len (header .Extra ))... )
549554 }
550- header .Extra = header .Extra [:extraVanity ]
555+ header .Extra = header .Extra [:ExtraVanity ]
551556
552557 if number % c .config .Epoch == 0 {
553558 for _ , signer := range snap .signers () {
554559 header .Extra = append (header .Extra , signer [:]... )
555560 }
556561 }
557- header .Extra = append (header .Extra , make ([]byte , extraSeal )... )
562+ header .Extra = append (header .Extra , make ([]byte , ExtraSeal )... )
558563
559564 // Mix digest is reserved for now, set to empty
560565 header .MixDigest = common.Hash {}
@@ -587,7 +592,8 @@ func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
587592 c .Finalize (chain , header , state , body )
588593
589594 // Assign the final state root to header.
590- header .Root = state .IntermediateRoot (chain .Config ().IsEIP158 (header .Number ))
595+ deleteEmptyObjects := chain .Config ().Active (forks .SpuriousDragon , header .Number .Uint64 (), header .Time )
596+ header .Root = state .IntermediateRoot (deleteEmptyObjects )
591597
592598 // Assemble and return the final block for sealing.
593599 return types .NewBlock (header , & types.Body {Transactions : body .Transactions }, receipts , trie .NewStackTrie (nil )), nil
0 commit comments