mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
352 lines
14 KiB
Go
352 lines
14 KiB
Go
// Copyright 2017 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// Package clique implements the proof-of-authority consensus engine.
|
|
package clique
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
"golang.org/x/crypto/sha3"
|
|
"io"
|
|
"math/big"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
"github.com/ethereum/go-ethereum/common/lru"
|
|
"github.com/ethereum/go-ethereum/consensus"
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
"github.com/ethereum/go-ethereum/params"
|
|
)
|
|
|
|
const (
|
|
checkpointInterval = 1024 // Number of blocks after which to save the vote snapshot to the database
|
|
inmemorySnapshots = 128 // Number of recent vote snapshots to keep in memory
|
|
inmemorySignatures = 4096 // Number of recent block signatures to keep in memory
|
|
|
|
wiggleTime = 500 * time.Millisecond // Random delay (per signer) to allow concurrent signers
|
|
)
|
|
|
|
// Clique proof-of-authority protocol constants.
|
|
var (
|
|
epochLength = uint64(30000) // Default number of blocks after which to checkpoint and reset the pending votes
|
|
|
|
extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity
|
|
extraSeal = crypto.SignatureLength // Fixed number of extra-data suffix bytes reserved for signer seal
|
|
|
|
nonceAuthVote = hexutil.MustDecode("0xffffffffffffffff") // Magic nonce number to vote on adding a new signer
|
|
nonceDropVote = hexutil.MustDecode("0x0000000000000000") // Magic nonce number to vote on removing a signer.
|
|
|
|
uncleHash = types.CalcUncleHash(nil) // Always Keccak256(RLP([])) as uncles are meaningless outside of PoW.
|
|
|
|
diffInTurn = big.NewInt(2) // Block difficulty for in-turn signatures
|
|
diffNoTurn = big.NewInt(1) // Block difficulty for out-of-turn signatures
|
|
)
|
|
|
|
// Various error messages to mark blocks invalid. These should be private to
|
|
// prevent engine specific errors from being referenced in the remainder of the
|
|
// codebase, inherently breaking if the engine is swapped out. Please put common
|
|
// error types into the consensus package.
|
|
var (
|
|
// errUnknownBlock is returned when the list of signers is requested for a block
|
|
// that is not part of the local blockchain.
|
|
errUnknownBlock = errors.New("unknown block")
|
|
|
|
// errInvalidCheckpointBeneficiary is returned if a checkpoint/epoch transition
|
|
// block has a beneficiary set to non-zeroes.
|
|
errInvalidCheckpointBeneficiary = errors.New("beneficiary in checkpoint block non-zero")
|
|
|
|
// errInvalidVote is returned if a nonce value is something else that the two
|
|
// allowed constants of 0x00..0 or 0xff..f.
|
|
errInvalidVote = errors.New("vote nonce not 0x00..0 or 0xff..f")
|
|
|
|
// errInvalidCheckpointVote is returned if a checkpoint/epoch transition block
|
|
// has a vote nonce set to non-zeroes.
|
|
errInvalidCheckpointVote = errors.New("vote nonce in checkpoint block non-zero")
|
|
|
|
// errMissingVanity is returned if a block's extra-data section is shorter than
|
|
// 32 bytes, which is required to store the signer vanity.
|
|
errMissingVanity = errors.New("extra-data 32 byte vanity prefix missing")
|
|
|
|
// errMissingSignature is returned if a block's extra-data section doesn't seem
|
|
// to contain a 65 byte secp256k1 signature.
|
|
errMissingSignature = errors.New("extra-data 65 byte signature suffix missing")
|
|
|
|
// errExtraSigners is returned if non-checkpoint block contain signer data in
|
|
// their extra-data fields.
|
|
errExtraSigners = errors.New("non-checkpoint block contains extra signer list")
|
|
|
|
// errInvalidCheckpointSigners is returned if a checkpoint block contains an
|
|
// invalid list of signers (i.e. non divisible by 20 bytes).
|
|
errInvalidCheckpointSigners = errors.New("invalid signer list on checkpoint block")
|
|
|
|
// errMismatchingCheckpointSigners is returned if a checkpoint block contains a
|
|
// list of signers different than the one the local node calculated.
|
|
errMismatchingCheckpointSigners = errors.New("mismatching signer list on checkpoint block")
|
|
|
|
// errInvalidMixDigest is returned if a block's mix digest is non-zero.
|
|
errInvalidMixDigest = errors.New("non-zero mix digest")
|
|
|
|
// errInvalidUncleHash is returned if a block contains an non-empty uncle list.
|
|
errInvalidUncleHash = errors.New("non empty uncle hash")
|
|
|
|
// errInvalidDifficulty is returned if the difficulty of a block neither 1 or 2.
|
|
errInvalidDifficulty = errors.New("invalid difficulty")
|
|
|
|
// errWrongDifficulty is returned if the difficulty of a block doesn't match the
|
|
// turn of the signer.
|
|
errWrongDifficulty = errors.New("wrong difficulty")
|
|
|
|
// errInvalidTimestamp is returned if the timestamp of a block is lower than
|
|
// the previous block's timestamp + the minimum block period.
|
|
errInvalidTimestamp = errors.New("invalid timestamp")
|
|
|
|
// errInvalidVotingChain is returned if an authorization list is attempted to
|
|
// be modified via out-of-range or non-contiguous headers.
|
|
errInvalidVotingChain = errors.New("invalid voting chain")
|
|
|
|
// errUnauthorizedSigner is returned if a header is signed by a non-authorized entity.
|
|
errUnauthorizedSigner = errors.New("unauthorized signer")
|
|
|
|
// errRecentlySigned is returned if a header is signed by an authorized entity
|
|
// that already signed a header recently, thus is temporarily not allowed to.
|
|
errRecentlySigned = errors.New("recently signed")
|
|
)
|
|
|
|
// SignerFn hashes and signs the data to be signed by a backing account.
|
|
type SignerFn func(signer accounts.Account, mimeType string, message []byte) ([]byte, error)
|
|
|
|
// ecrecover extracts the Ethereum account address from a signed header.
|
|
func ecrecover(header *types.Header, sigcache *sigLRU) (common.Address, error) {
|
|
// If the signature's already cached, return that
|
|
hash := header.Hash()
|
|
if address, known := sigcache.Get(hash); known {
|
|
return address, nil
|
|
}
|
|
// Retrieve the signature from the header extra-data
|
|
if len(header.Extra) < extraSeal {
|
|
return common.Address{}, errMissingSignature
|
|
}
|
|
signature := header.Extra[len(header.Extra)-extraSeal:]
|
|
|
|
// Recover the public key and the Ethereum address
|
|
pubkey, err := crypto.Ecrecover(SealHash(header).Bytes(), signature)
|
|
if err != nil {
|
|
return common.Address{}, err
|
|
}
|
|
var signer common.Address
|
|
copy(signer[:], crypto.Keccak256(pubkey[1:])[12:])
|
|
|
|
sigcache.Add(hash, signer)
|
|
return signer, nil
|
|
}
|
|
|
|
// Clique is the proof-of-authority consensus engine proposed to support the
|
|
// Ethereum testnet following the Ropsten attacks.
|
|
type Clique struct {
|
|
config *params.CliqueConfig // Consensus engine configuration parameters
|
|
db ethdb.Database // Database to store and retrieve snapshot checkpoints
|
|
|
|
recents *lru.Cache[common.Hash, *Snapshot] // Snapshots for recent block to speed up reorgs
|
|
signatures *sigLRU // Signatures of recent blocks to speed up mining
|
|
|
|
proposals map[common.Address]bool // Current list of proposals we are pushing
|
|
|
|
signer common.Address // Ethereum address of the signing key
|
|
signFn SignerFn // Signer function to authorize hashes with
|
|
lock sync.RWMutex // Protects the signer and proposals fields
|
|
|
|
// The fields below are for testing only
|
|
fakeDiff bool // Skip difficulty verifications
|
|
}
|
|
|
|
// New creates a Clique proof-of-authority consensus engine with the initial
|
|
// signers set to the ones provided by the user.
|
|
func New(config *params.CliqueConfig, db ethdb.Database) *Clique {
|
|
// Set any missing consensus parameters to their defaults
|
|
conf := *config
|
|
if conf.Epoch == 0 {
|
|
conf.Epoch = epochLength
|
|
}
|
|
// Allocate the snapshot caches and create the engine
|
|
recents := lru.NewCache[common.Hash, *Snapshot](inmemorySnapshots)
|
|
signatures := lru.NewCache[common.Hash, common.Address](inmemorySignatures)
|
|
|
|
return &Clique{
|
|
config: &conf,
|
|
db: db,
|
|
recents: recents,
|
|
signatures: signatures,
|
|
}
|
|
}
|
|
|
|
// Author implements consensus.Engine, returning the Ethereum address recovered
|
|
// from the signature in the header's extra-data section.
|
|
func (c *Clique) Author(header *types.Header) (common.Address, error) {
|
|
return ecrecover(header, c.signatures)
|
|
}
|
|
|
|
// VerifyHeader checks whether a header conforms to the consensus rules.
|
|
func (c *Clique) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header) error {
|
|
// header chain contiguity (checked elsewhere) is the only verification we care about
|
|
return nil
|
|
}
|
|
|
|
// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers. The
|
|
// method returns a quit channel to abort the operations and a results channel to
|
|
// retrieve the async verifications (the order is that of the input slice).
|
|
func (c *Clique) VerifyHeaders(_ consensus.ChainHeaderReader, headers []*types.Header) (chan<- struct{}, <-chan error) {
|
|
abort := make(chan struct{})
|
|
results := make(chan error, len(headers))
|
|
|
|
go func() {
|
|
select {
|
|
case <-abort:
|
|
return
|
|
case results <- nil:
|
|
}
|
|
}()
|
|
return abort, results
|
|
}
|
|
|
|
// verifyHeader checks whether a header conforms to the consensus rules.The
|
|
// caller may optionally pass in a batch of parents (ascending order) to avoid
|
|
// looking those up from the database. This is useful for concurrently verifying
|
|
// a batch of new headers.
|
|
func (c *Clique) verifyHeader(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) error {
|
|
// The genesis block is the always valid dead-end
|
|
number := header.Number.Uint64()
|
|
if number == 0 {
|
|
return nil
|
|
}
|
|
// Ensure that the block's timestamp isn't too close to its parent
|
|
var parent *types.Header
|
|
if len(parents) > 0 {
|
|
parent = parents[len(parents)-1]
|
|
} else {
|
|
parent = chain.GetHeader(header.ParentHash, number-1)
|
|
}
|
|
if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash {
|
|
return consensus.ErrUnknownAncestor
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// VerifyUncles implements consensus.Engine, always returning an error for any
|
|
// uncles as this consensus mechanism doesn't permit uncles.
|
|
func (c *Clique) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
|
|
return nil
|
|
}
|
|
|
|
// Prepare implements consensus.Engine, preparing all the consensus fields of the
|
|
// header for running the transactions on top.
|
|
func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {
|
|
panic("block construction not supported for clique")
|
|
}
|
|
|
|
// Finalize implements consensus.Engine. There is no post-transaction
|
|
// consensus rules in clique, do nothing here.
|
|
func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) {
|
|
// No block rewards in PoA, so the state remains as is
|
|
}
|
|
|
|
// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
|
|
// nor block rewards given, and returns the final block.
|
|
func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt) (*types.Block, error) {
|
|
panic("block construction not supported for clique")
|
|
}
|
|
|
|
// Authorize injects a private key into the consensus engine to mint new blocks
|
|
// with.
|
|
func (c *Clique) Authorize(signer common.Address, signFn SignerFn) {
|
|
panic("sealing blocks not supported in clique")
|
|
}
|
|
|
|
// Seal implements consensus.Engine, attempting to create a sealed block using
|
|
// the local signing credentials.
|
|
func (c *Clique) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
|
|
panic("sealing blocks not supported in clique")
|
|
}
|
|
|
|
// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
|
|
// that a new block should have:
|
|
// * DIFF_NOTURN(2) if BLOCK_NUMBER % SIGNER_COUNT != SIGNER_INDEX
|
|
// * DIFF_INTURN(1) if BLOCK_NUMBER % SIGNER_COUNT == SIGNER_INDEX
|
|
func (c *Clique) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
|
|
panic("not supported")
|
|
}
|
|
|
|
// SealHash returns the hash of a block prior to it being sealed.
|
|
func (c *Clique) SealHash(header *types.Header) common.Hash {
|
|
panic("not supported")
|
|
}
|
|
|
|
// Close implements consensus.Engine. It's a noop for clique as there are no background threads.
|
|
func (c *Clique) Close() error {
|
|
return nil
|
|
}
|
|
|
|
// SealHash returns the hash of a block prior to it being sealed.
|
|
func SealHash(header *types.Header) (hash common.Hash) {
|
|
hasher := sha3.NewLegacyKeccak256()
|
|
encodeSigHeader(hasher, header)
|
|
hasher.(crypto.KeccakState).Read(hash[:])
|
|
return hash
|
|
}
|
|
|
|
func encodeSigHeader(w io.Writer, header *types.Header) {
|
|
enc := []interface{}{
|
|
header.ParentHash,
|
|
header.UncleHash,
|
|
header.Coinbase,
|
|
header.Root,
|
|
header.TxHash,
|
|
header.ReceiptHash,
|
|
header.Bloom,
|
|
header.Difficulty,
|
|
header.Number,
|
|
header.GasLimit,
|
|
header.GasUsed,
|
|
header.Time,
|
|
header.Extra[:len(header.Extra)-crypto.SignatureLength], // Yes, this will panic if extra is too short
|
|
header.MixDigest,
|
|
header.Nonce,
|
|
}
|
|
if header.BaseFee != nil {
|
|
enc = append(enc, header.BaseFee)
|
|
}
|
|
if header.WithdrawalsHash != nil {
|
|
panic("unexpected withdrawal hash value in clique")
|
|
}
|
|
if header.ExcessBlobGas != nil {
|
|
panic("unexpected excess blob gas value in clique")
|
|
}
|
|
if header.BlobGasUsed != nil {
|
|
panic("unexpected blob gas used value in clique")
|
|
}
|
|
if header.ParentBeaconRoot != nil {
|
|
panic("unexpected parent beacon root value in clique")
|
|
}
|
|
if err := rlp.Encode(w, enc); err != nil {
|
|
panic("can't encode: " + err.Error())
|
|
}
|
|
}
|