@@ -40,6 +40,7 @@ import (
40
40
"github.com/ethereum/go-ethereum/ethdb"
41
41
"github.com/ethereum/go-ethereum/log"
42
42
"github.com/ethereum/go-ethereum/params"
43
+ "github.com/ethereum/go-ethereum/params/forks"
43
44
"github.com/ethereum/go-ethereum/rlp"
44
45
"github.com/ethereum/go-ethereum/trie"
45
46
"golang.org/x/crypto/sha3"
@@ -51,20 +52,25 @@ const (
51
52
inmemorySignatures = 4096 // Number of recent block signatures to keep in memory
52
53
)
53
54
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
+
54
63
// Clique proof-of-authority protocol constants.
55
64
var (
56
65
epochLength = uint64 (30000 ) // Default number of blocks after which to checkpoint and reset the pending votes
57
66
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
-
61
67
nonceAuthVote = hexutil .MustDecode ("0xffffffffffffffff" ) // Magic nonce number to vote on adding a new signer
62
68
nonceDropVote = hexutil .MustDecode ("0x0000000000000000" ) // Magic nonce number to vote on removing a signer.
63
69
64
70
uncleHash = types .CalcUncleHash (nil ) // Always Keccak256(RLP([])) as uncles are meaningless outside of PoW.
65
71
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 )
68
74
)
69
75
70
76
// 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) {
145
151
return address , nil
146
152
}
147
153
// Retrieve the signature from the header extra-data
148
- if len (header .Extra ) < extraSeal {
154
+ if len (header .Extra ) < ExtraSeal {
149
155
return common.Address {}, errMissingSignature
150
156
}
151
- signature := header .Extra [len (header .Extra )- extraSeal :]
157
+ signature := header .Extra [len (header .Extra )- ExtraSeal :]
152
158
153
159
// Recover the public key and the Ethereum address
154
160
pubkey , err := crypto .Ecrecover (SealHash (header ).Bytes (), signature )
@@ -165,8 +171,8 @@ func ecrecover(header *types.Header, sigcache *sigLRU) (common.Address, error) {
165
171
// Clique is the proof-of-authority consensus engine proposed to support the
166
172
// Ethereum testnet following the Ropsten attacks.
167
173
type 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
170
176
171
177
recents * lru.Cache [common.Hash , * Snapshot ] // Snapshots for recent block to speed up reorgs
172
178
signatures * sigLRU // Signatures of recent blocks to speed up mining
@@ -182,18 +188,17 @@ type Clique struct {
182
188
183
189
// New creates a Clique proof-of-authority consensus engine with the initial
184
190
// 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 {
186
192
// 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
190
195
}
191
196
// Allocate the snapshot caches and create the engine
192
197
recents := lru.NewCache [common.Hash , * Snapshot ](inmemorySnapshots )
193
198
signatures := lru.NewCache [common.Hash , common.Address ](inmemorySignatures )
194
199
195
200
return & Clique {
196
- config : & conf ,
201
+ config : & config ,
197
202
db : db ,
198
203
recents : recents ,
199
204
signatures : signatures ,
@@ -260,14 +265,14 @@ func (c *Clique) verifyHeader(chain consensus.ChainHeaderReader, header *types.H
260
265
return errInvalidCheckpointVote
261
266
}
262
267
// Check that the extra-data contains both the vanity and signature
263
- if len (header .Extra ) < extraVanity {
268
+ if len (header .Extra ) < ExtraVanity {
264
269
return errMissingVanity
265
270
}
266
- if len (header .Extra ) < extraVanity + extraSeal {
271
+ if len (header .Extra ) < ExtraVanity + ExtraSeal {
267
272
return errMissingSignature
268
273
}
269
274
// 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
271
276
if ! checkpoint && signersBytes != 0 {
272
277
return errExtraSigners
273
278
}
@@ -292,14 +297,14 @@ func (c *Clique) verifyHeader(chain consensus.ChainHeaderReader, header *types.H
292
297
if header .GasLimit > params .MaxGasLimit {
293
298
return fmt .Errorf ("invalid gasLimit: have %v, max %v" , header .GasLimit , params .MaxGasLimit )
294
299
}
295
- if chain .Config ().IsShanghai ( header .Number , header .Time ) {
300
+ if chain .Config ().Active ( forks . Shanghai , header .Number . Uint64 () , header .Time ) {
296
301
return errors .New ("clique does not support shanghai fork" )
297
302
}
298
303
// Verify the non-existence of withdrawalsHash.
299
304
if header .WithdrawalsHash != nil {
300
305
return fmt .Errorf ("invalid withdrawalsHash: have %x, expected nil" , header .WithdrawalsHash )
301
306
}
302
- if chain .Config ().IsCancun ( header .Number , header .Time ) {
307
+ if chain .Config ().Active ( forks . Cancun , header .Number . Uint64 () , header .Time ) {
303
308
return errors .New ("clique does not support cancun fork" )
304
309
}
305
310
// Verify the non-existence of cancun-specific header fields
@@ -342,7 +347,7 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainHeaderReader, header
342
347
if header .GasUsed > header .GasLimit {
343
348
return fmt .Errorf ("invalid gasUsed: have %d, gasLimit %d" , header .GasUsed , header .GasLimit )
344
349
}
345
- if ! chain .Config ().IsLondon ( header .Number ) {
350
+ if ! chain .Config ().Active ( forks . London , header .Number . Uint64 (), header . Time ) {
346
351
// Verify BaseFee not present before EIP-1559 fork.
347
352
if header .BaseFee != nil {
348
353
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
365
370
for i , signer := range snap .signers () {
366
371
copy (signers [i * common .AddressLength :], signer [:])
367
372
}
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 ) {
370
375
return errMismatchingCheckpointSigners
371
376
}
372
377
}
@@ -404,9 +409,9 @@ func (c *Clique) snapshot(chain consensus.ChainHeaderReader, number uint64, hash
404
409
if checkpoint != nil {
405
410
hash := checkpoint .Hash ()
406
411
407
- signers := make ([]common.Address , (len (checkpoint .Extra )- extraVanity - extraSeal )/ common .AddressLength )
412
+ signers := make ([]common.Address , (len (checkpoint .Extra )- ExtraVanity - ExtraSeal )/ common .AddressLength )
408
413
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 :])
410
415
}
411
416
snap = newSnapshot (c .config , c .signatures , number , hash , signers )
412
417
if err := snap .store (c .db ); err != nil {
@@ -544,17 +549,17 @@ func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header
544
549
header .Difficulty = calcDifficulty (snap , signer )
545
550
546
551
// 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 ))... )
549
554
}
550
- header .Extra = header .Extra [:extraVanity ]
555
+ header .Extra = header .Extra [:ExtraVanity ]
551
556
552
557
if number % c .config .Epoch == 0 {
553
558
for _ , signer := range snap .signers () {
554
559
header .Extra = append (header .Extra , signer [:]... )
555
560
}
556
561
}
557
- header .Extra = append (header .Extra , make ([]byte , extraSeal )... )
562
+ header .Extra = append (header .Extra , make ([]byte , ExtraSeal )... )
558
563
559
564
// Mix digest is reserved for now, set to empty
560
565
header .MixDigest = common.Hash {}
@@ -587,7 +592,8 @@ func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
587
592
c .Finalize (chain , header , state , body )
588
593
589
594
// 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 )
591
597
592
598
// Assemble and return the final block for sealing.
593
599
return types .NewBlock (header , & types.Body {Transactions : body .Transactions }, receipts , trie .NewStackTrie (nil )), nil
0 commit comments