diff --git a/consensus/beacon/consensus.go b/consensus/beacon/consensus.go
deleted file mode 100644
index e856f4e6ce..0000000000
--- a/consensus/beacon/consensus.go
+++ /dev/null
@@ -1,472 +0,0 @@
-// Copyright 2021 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 .
-
-package beacon
-
-import (
- "errors"
- "fmt"
- "math/big"
-
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/consensus"
- "github.com/ethereum/go-ethereum/consensus/misc/eip1559"
- "github.com/ethereum/go-ethereum/consensus/misc/eip4844"
- "github.com/ethereum/go-ethereum/core/state"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/params"
- "github.com/ethereum/go-ethereum/rpc"
- "github.com/ethereum/go-ethereum/trie"
-)
-
-// Proof-of-stake protocol constants.
-var (
- beaconDifficulty = common.Big0 // The default block difficulty in the beacon consensus
- beaconNonce = types.EncodeNonce(0) // The default block nonce in the beacon consensus
-)
-
-// 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 (
- errTooManyUncles = errors.New("too many uncles")
- errInvalidNonce = errors.New("invalid nonce")
- errInvalidUncleHash = errors.New("invalid uncle hash")
- errInvalidTimestamp = errors.New("invalid timestamp")
-)
-
-// Beacon is a consensus engine that combines the eth1 consensus and proof-of-stake
-// algorithm. There is a special flag inside to decide whether to use legacy consensus
-// rules or new rules. The transition rule is described in the eth1/2 merge spec.
-// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3675.md
-//
-// The beacon here is a half-functional consensus engine with partial functions which
-// is only used for necessary consensus checks. The legacy consensus engine can be any
-// engine implements the consensus interface (except the beacon itself).
-type Beacon struct {
- ethone consensus.Engine // Original consensus engine used in eth1, e.g. ethash or clique
-}
-
-// New creates a consensus engine with the given embedded eth1 engine.
-func New(ethone consensus.Engine) *Beacon {
- if _, ok := ethone.(*Beacon); ok {
- panic("nested consensus engine")
- }
- return &Beacon{ethone: ethone}
-}
-
-// Author implements consensus.Engine, returning the verified author of the block.
-func (beacon *Beacon) Author(header *types.Header) (common.Address, error) {
- if !beacon.IsPoSHeader(header) {
- return beacon.ethone.Author(header)
- }
- return header.Coinbase, nil
-}
-
-// VerifyHeader checks whether a header conforms to the consensus rules of the
-// stock Ethereum consensus engine.
-func (beacon *Beacon) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header) error {
- reached, err := IsTTDReached(chain, header.ParentHash, header.Number.Uint64()-1)
- if err != nil {
- return err
- }
- if !reached {
- return beacon.ethone.VerifyHeader(chain, header)
- }
- // Short circuit if the parent is not known
- parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
- if parent == nil {
- return consensus.ErrUnknownAncestor
- }
- // Sanity checks passed, do a proper verification
- return beacon.verifyHeader(chain, header, parent)
-}
-
-// errOut constructs an error channel with prefilled errors inside.
-func errOut(n int, err error) chan error {
- errs := make(chan error, n)
- for i := 0; i < n; i++ {
- errs <- err
- }
- return errs
-}
-
-// splitHeaders splits the provided header batch into two parts according to
-// the configured ttd. It requires the parent of header batch along with its
-// td are stored correctly in chain. If ttd is not configured yet, all headers
-// will be treated legacy PoW headers.
-// Note, this function will not verify the header validity but just split them.
-func (beacon *Beacon) splitHeaders(chain consensus.ChainHeaderReader, headers []*types.Header) ([]*types.Header, []*types.Header, error) {
- // TTD is not defined yet, all headers should be in legacy format.
- ttd := chain.Config().TerminalTotalDifficulty
- if ttd == nil {
- return headers, nil, nil
- }
- ptd := chain.GetTd(headers[0].ParentHash, headers[0].Number.Uint64()-1)
- if ptd == nil {
- return nil, nil, consensus.ErrUnknownAncestor
- }
- // The entire header batch already crosses the transition.
- if ptd.Cmp(ttd) >= 0 {
- return nil, headers, nil
- }
- var (
- preHeaders = headers
- postHeaders []*types.Header
- td = new(big.Int).Set(ptd)
- tdPassed bool
- )
- for i, header := range headers {
- if tdPassed {
- preHeaders = headers[:i]
- postHeaders = headers[i:]
- break
- }
- td = td.Add(td, header.Difficulty)
- if td.Cmp(ttd) >= 0 {
- // This is the last PoW header, it still belongs to
- // the preHeaders, so we cannot split+break yet.
- tdPassed = true
- }
- }
- return preHeaders, postHeaders, nil
-}
-
-// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
-// concurrently. The method returns a quit channel to abort the operations and
-// a results channel to retrieve the async verifications.
-// VerifyHeaders expect the headers to be ordered and continuous.
-func (beacon *Beacon) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header) (chan<- struct{}, <-chan error) {
- preHeaders, postHeaders, err := beacon.splitHeaders(chain, headers)
- if err != nil {
- return make(chan struct{}), errOut(len(headers), err)
- }
- if len(postHeaders) == 0 {
- return beacon.ethone.VerifyHeaders(chain, headers)
- }
- if len(preHeaders) == 0 {
- return beacon.verifyHeaders(chain, headers, nil)
- }
- // The transition point exists in the middle, separate the headers
- // into two batches and apply different verification rules for them.
- var (
- abort = make(chan struct{})
- results = make(chan error, len(headers))
- )
- go func() {
- var (
- old, new, out = 0, len(preHeaders), 0
- errors = make([]error, len(headers))
- done = make([]bool, len(headers))
- oldDone, oldResult = beacon.ethone.VerifyHeaders(chain, preHeaders)
- newDone, newResult = beacon.verifyHeaders(chain, postHeaders, preHeaders[len(preHeaders)-1])
- )
- // Collect the results
- for {
- for ; done[out]; out++ {
- results <- errors[out]
- if out == len(headers)-1 {
- return
- }
- }
- select {
- case err := <-oldResult:
- if !done[old] { // skip TTD-verified failures
- errors[old], done[old] = err, true
- }
- old++
- case err := <-newResult:
- errors[new], done[new] = err, true
- new++
- case <-abort:
- close(oldDone)
- close(newDone)
- return
- }
- }
- }()
- return abort, results
-}
-
-// VerifyUncles verifies that the given block's uncles conform to the consensus
-// rules of the Ethereum consensus engine.
-func (beacon *Beacon) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
- if !beacon.IsPoSHeader(block.Header()) {
- return beacon.ethone.VerifyUncles(chain, block)
- }
- // Verify that there is no uncle block. It's explicitly disabled in the beacon
- if len(block.Uncles()) > 0 {
- return errTooManyUncles
- }
- return nil
-}
-
-// verifyHeader checks whether a header conforms to the consensus rules of the
-// stock Ethereum consensus engine. The difference between the beacon and classic is
-// (a) The following fields are expected to be constants:
-// - difficulty is expected to be 0
-// - nonce is expected to be 0
-// - unclehash is expected to be Hash(emptyHeader)
-// to be the desired constants
-//
-// (b) we don't verify if a block is in the future anymore
-// (c) the extradata is limited to 32 bytes
-func (beacon *Beacon) verifyHeader(chain consensus.ChainHeaderReader, header, parent *types.Header) error {
- // Ensure that the header's extra-data section is of a reasonable size
- if len(header.Extra) > 32 {
- return fmt.Errorf("extra-data longer than 32 bytes (%d)", len(header.Extra))
- }
- // Verify the seal parts. Ensure the nonce and uncle hash are the expected value.
- if header.Nonce != beaconNonce {
- return errInvalidNonce
- }
- if header.UncleHash != types.EmptyUncleHash {
- return errInvalidUncleHash
- }
- // Verify the timestamp
- if header.Time <= parent.Time {
- return errInvalidTimestamp
- }
- // Verify the block's difficulty to ensure it's the default constant
- if beaconDifficulty.Cmp(header.Difficulty) != 0 {
- return fmt.Errorf("invalid difficulty: have %v, want %v", header.Difficulty, beaconDifficulty)
- }
- // Verify that the gas limit is <= 2^63-1
- if header.GasLimit > params.MaxGasLimit {
- return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, params.MaxGasLimit)
- }
- // Verify that the gasUsed is <= gasLimit
- if header.GasUsed > header.GasLimit {
- return fmt.Errorf("invalid gasUsed: have %d, gasLimit %d", header.GasUsed, header.GasLimit)
- }
- // Verify that the block number is parent's +1
- if diff := new(big.Int).Sub(header.Number, parent.Number); diff.Cmp(common.Big1) != 0 {
- return consensus.ErrInvalidNumber
- }
- // Verify the header's EIP-1559 attributes.
- if err := eip1559.VerifyEIP1559Header(chain.Config(), parent, header); err != nil {
- return err
- }
- // Verify existence / non-existence of withdrawalsHash.
- shanghai := chain.Config().IsShanghai(header.Number, header.Time)
- if shanghai && header.WithdrawalsHash == nil {
- return errors.New("missing withdrawalsHash")
- }
- if !shanghai && header.WithdrawalsHash != nil {
- return fmt.Errorf("invalid withdrawalsHash: have %x, expected nil", header.WithdrawalsHash)
- }
- // Verify the existence / non-existence of cancun-specific header fields
- cancun := chain.Config().IsCancun(header.Number, header.Time)
- if !cancun {
- switch {
- case header.ExcessBlobGas != nil:
- return fmt.Errorf("invalid excessBlobGas: have %d, expected nil", header.ExcessBlobGas)
- case header.BlobGasUsed != nil:
- return fmt.Errorf("invalid blobGasUsed: have %d, expected nil", header.BlobGasUsed)
- case header.ParentBeaconRoot != nil:
- return fmt.Errorf("invalid parentBeaconRoot, have %#x, expected nil", header.ParentBeaconRoot)
- }
- } else {
- if header.ParentBeaconRoot == nil {
- return errors.New("header is missing beaconRoot")
- }
- if err := eip4844.VerifyEIP4844Header(parent, header); err != nil {
- return err
- }
- }
- return nil
-}
-
-// verifyHeaders is similar to verifyHeader, but verifies a batch of headers
-// concurrently. The method returns a quit channel to abort the operations and
-// a results channel to retrieve the async verifications. An additional parent
-// header will be passed if the relevant header is not in the database yet.
-func (beacon *Beacon) verifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header, ancestor *types.Header) (chan<- struct{}, <-chan error) {
- var (
- abort = make(chan struct{})
- results = make(chan error, len(headers))
- )
- go func() {
- for i, header := range headers {
- var parent *types.Header
- if i == 0 {
- if ancestor != nil {
- parent = ancestor
- } else {
- parent = chain.GetHeader(headers[0].ParentHash, headers[0].Number.Uint64()-1)
- }
- } else if headers[i-1].Hash() == headers[i].ParentHash {
- parent = headers[i-1]
- }
- if parent == nil {
- select {
- case <-abort:
- return
- case results <- consensus.ErrUnknownAncestor:
- }
- continue
- }
- err := beacon.verifyHeader(chain, header, parent)
- select {
- case <-abort:
- return
- case results <- err:
- }
- }
- }()
- return abort, results
-}
-
-// Prepare implements consensus.Engine, initializing the difficulty field of a
-// header to conform to the beacon protocol. The changes are done inline.
-func (beacon *Beacon) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {
- // Transition isn't triggered yet, use the legacy rules for preparation.
- reached, err := IsTTDReached(chain, header.ParentHash, header.Number.Uint64()-1)
- if err != nil {
- return err
- }
- if !reached {
- return beacon.ethone.Prepare(chain, header)
- }
- header.Difficulty = beaconDifficulty
- return nil
-}
-
-// Finalize implements consensus.Engine and processes withdrawals on top.
-func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, withdrawals []*types.Withdrawal) {
- if !beacon.IsPoSHeader(header) {
- beacon.ethone.Finalize(chain, header, state, txs, uncles, nil)
- return
- }
- // Withdrawals processing.
- for _, w := range withdrawals {
- // Convert amount from gwei to wei.
- amount := new(big.Int).SetUint64(w.Amount)
- amount = amount.Mul(amount, big.NewInt(params.GWei))
- state.AddBalance(w.Address, amount)
- }
- // No block reward which is issued by consensus layer instead.
-}
-
-// FinalizeAndAssemble implements consensus.Engine, setting the final state and
-// assembling the block.
-func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt, withdrawals []*types.Withdrawal) (*types.Block, error) {
- if !beacon.IsPoSHeader(header) {
- return beacon.ethone.FinalizeAndAssemble(chain, header, state, txs, uncles, receipts, nil)
- }
- shanghai := chain.Config().IsShanghai(header.Number, header.Time)
- if shanghai {
- // All blocks after Shanghai must include a withdrawals root.
- if withdrawals == nil {
- withdrawals = make([]*types.Withdrawal, 0)
- }
- } else {
- if len(withdrawals) > 0 {
- return nil, errors.New("withdrawals set before Shanghai activation")
- }
- }
- // Finalize and assemble the block.
- beacon.Finalize(chain, header, state, txs, uncles, withdrawals)
-
- // Assign the final state root to header.
- header.Root = state.IntermediateRoot(true)
-
- // Assemble and return the final block.
- return types.NewBlockWithWithdrawals(header, txs, uncles, receipts, withdrawals, trie.NewStackTrie(nil)), nil
-}
-
-// Seal generates a new sealing request for the given input block and pushes
-// the result into the given channel.
-//
-// Note, the method returns immediately and will send the result async. More
-// than one result may also be returned depending on the consensus algorithm.
-func (beacon *Beacon) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
- if !beacon.IsPoSHeader(block.Header()) {
- return beacon.ethone.Seal(chain, block, results, stop)
- }
- // The seal verification is done by the external consensus engine,
- // return directly without pushing any block back. In another word
- // beacon won't return any result by `results` channel which may
- // blocks the receiver logic forever.
- return nil
-}
-
-// SealHash returns the hash of a block prior to it being sealed.
-func (beacon *Beacon) SealHash(header *types.Header) common.Hash {
- return beacon.ethone.SealHash(header)
-}
-
-// CalcDifficulty is the difficulty adjustment algorithm. It returns
-// the difficulty that a new block should have when created at time
-// given the parent block's time and difficulty.
-func (beacon *Beacon) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
- // Transition isn't triggered yet, use the legacy rules for calculation
- if reached, _ := IsTTDReached(chain, parent.Hash(), parent.Number.Uint64()); !reached {
- return beacon.ethone.CalcDifficulty(chain, time, parent)
- }
- return beaconDifficulty
-}
-
-// APIs implements consensus.Engine, returning the user facing RPC APIs.
-func (beacon *Beacon) APIs(chain consensus.ChainHeaderReader) []rpc.API {
- return beacon.ethone.APIs(chain)
-}
-
-// Close shutdowns the consensus engine
-func (beacon *Beacon) Close() error {
- return beacon.ethone.Close()
-}
-
-// IsPoSHeader reports the header belongs to the PoS-stage with some special fields.
-// This function is not suitable for a part of APIs like Prepare or CalcDifficulty
-// because the header difficulty is not set yet.
-func (beacon *Beacon) IsPoSHeader(header *types.Header) bool {
- if header.Difficulty == nil {
- panic("IsPoSHeader called with invalid difficulty")
- }
- return header.Difficulty.Cmp(beaconDifficulty) == 0
-}
-
-// InnerEngine returns the embedded eth1 consensus engine.
-func (beacon *Beacon) InnerEngine() consensus.Engine {
- return beacon.ethone
-}
-
-// SetThreads updates the mining threads. Delegate the call
-// to the eth1 engine if it's threaded.
-func (beacon *Beacon) SetThreads(threads int) {
- type threaded interface {
- SetThreads(threads int)
- }
- if th, ok := beacon.ethone.(threaded); ok {
- th.SetThreads(threads)
- }
-}
-
-// IsTTDReached checks if the TotalTerminalDifficulty has been surpassed on the `parentHash` block.
-// It depends on the parentHash already being stored in the database.
-// If the parentHash is not stored in the database a UnknownAncestor error is returned.
-func IsTTDReached(chain consensus.ChainHeaderReader, parentHash common.Hash, parentNumber uint64) (bool, error) {
- if chain.Config().TerminalTotalDifficulty == nil {
- return false, nil
- }
- td := chain.GetTd(parentHash, parentNumber)
- if td == nil {
- return false, consensus.ErrUnknownAncestor
- }
- return td.Cmp(chain.Config().TerminalTotalDifficulty) >= 0, nil
-}
diff --git a/consensus/beacon/faker.go b/consensus/beacon/faker.go
deleted file mode 100644
index 981e345e3e..0000000000
--- a/consensus/beacon/faker.go
+++ /dev/null
@@ -1,41 +0,0 @@
-// Copyright 2023 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 .
-
-package beacon
-
-import (
- "math/big"
-
- "github.com/ethereum/go-ethereum/consensus"
- "github.com/ethereum/go-ethereum/core/types"
-)
-
-// NewFaker creates a fake consensus engine for testing.
-// The fake engine simulates a merged network.
-// It can not be used to test the merge transition.
-// This type is needed since the fakeChainReader can not be used with
-// a normal beacon consensus engine.
-func NewFaker() consensus.Engine {
- return new(faker)
-}
-
-type faker struct {
- Beacon
-}
-
-func (f *faker) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
- return beaconDifficulty
-}
diff --git a/consensus/clique/api.go b/consensus/clique/api.go
deleted file mode 100644
index 374b50692d..0000000000
--- a/consensus/clique/api.go
+++ /dev/null
@@ -1,235 +0,0 @@
-// 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 .
-
-package clique
-
-import (
- "encoding/json"
- "fmt"
-
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/common/hexutil"
- "github.com/ethereum/go-ethereum/consensus"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/rlp"
- "github.com/ethereum/go-ethereum/rpc"
-)
-
-// API is a user facing RPC API to allow controlling the signer and voting
-// mechanisms of the proof-of-authority scheme.
-type API struct {
- chain consensus.ChainHeaderReader
- clique *Clique
-}
-
-// GetSnapshot retrieves the state snapshot at a given block.
-func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) {
- // Retrieve the requested block number (or current if none requested)
- var header *types.Header
- if number == nil || *number == rpc.LatestBlockNumber {
- header = api.chain.CurrentHeader()
- } else {
- header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
- }
- // Ensure we have an actually valid block and return its snapshot
- if header == nil {
- return nil, errUnknownBlock
- }
- return api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
-}
-
-// GetSnapshotAtHash retrieves the state snapshot at a given block.
-func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) {
- header := api.chain.GetHeaderByHash(hash)
- if header == nil {
- return nil, errUnknownBlock
- }
- return api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
-}
-
-// GetSigners retrieves the list of authorized signers at the specified block.
-func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) {
- // Retrieve the requested block number (or current if none requested)
- var header *types.Header
- if number == nil || *number == rpc.LatestBlockNumber {
- header = api.chain.CurrentHeader()
- } else {
- header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
- }
- // Ensure we have an actually valid block and return the signers from its snapshot
- if header == nil {
- return nil, errUnknownBlock
- }
- snap, err := api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
- if err != nil {
- return nil, err
- }
- return snap.signers(), nil
-}
-
-// GetSignersAtHash retrieves the list of authorized signers at the specified block.
-func (api *API) GetSignersAtHash(hash common.Hash) ([]common.Address, error) {
- header := api.chain.GetHeaderByHash(hash)
- if header == nil {
- return nil, errUnknownBlock
- }
- snap, err := api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
- if err != nil {
- return nil, err
- }
- return snap.signers(), nil
-}
-
-// Proposals returns the current proposals the node tries to uphold and vote on.
-func (api *API) Proposals() map[common.Address]bool {
- api.clique.lock.RLock()
- defer api.clique.lock.RUnlock()
-
- proposals := make(map[common.Address]bool)
- for address, auth := range api.clique.proposals {
- proposals[address] = auth
- }
- return proposals
-}
-
-// Propose injects a new authorization proposal that the signer will attempt to
-// push through.
-func (api *API) Propose(address common.Address, auth bool) {
- api.clique.lock.Lock()
- defer api.clique.lock.Unlock()
-
- api.clique.proposals[address] = auth
-}
-
-// Discard drops a currently running proposal, stopping the signer from casting
-// further votes (either for or against).
-func (api *API) Discard(address common.Address) {
- api.clique.lock.Lock()
- defer api.clique.lock.Unlock()
-
- delete(api.clique.proposals, address)
-}
-
-type status struct {
- InturnPercent float64 `json:"inturnPercent"`
- SigningStatus map[common.Address]int `json:"sealerActivity"`
- NumBlocks uint64 `json:"numBlocks"`
-}
-
-// Status returns the status of the last N blocks,
-// - the number of active signers,
-// - the number of signers,
-// - the percentage of in-turn blocks
-func (api *API) Status() (*status, error) {
- var (
- numBlocks = uint64(64)
- header = api.chain.CurrentHeader()
- diff = uint64(0)
- optimals = 0
- )
- snap, err := api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
- if err != nil {
- return nil, err
- }
- var (
- signers = snap.signers()
- end = header.Number.Uint64()
- start = end - numBlocks
- )
- if numBlocks > end {
- start = 1
- numBlocks = end - start
- }
- signStatus := make(map[common.Address]int)
- for _, s := range signers {
- signStatus[s] = 0
- }
- for n := start; n < end; n++ {
- h := api.chain.GetHeaderByNumber(n)
- if h == nil {
- return nil, fmt.Errorf("missing block %d", n)
- }
- if h.Difficulty.Cmp(diffInTurn) == 0 {
- optimals++
- }
- diff += h.Difficulty.Uint64()
- sealer, err := api.clique.Author(h)
- if err != nil {
- return nil, err
- }
- signStatus[sealer]++
- }
- return &status{
- InturnPercent: float64(100*optimals) / float64(numBlocks),
- SigningStatus: signStatus,
- NumBlocks: numBlocks,
- }, nil
-}
-
-type blockNumberOrHashOrRLP struct {
- *rpc.BlockNumberOrHash
- RLP hexutil.Bytes `json:"rlp,omitempty"`
-}
-
-func (sb *blockNumberOrHashOrRLP) UnmarshalJSON(data []byte) error {
- bnOrHash := new(rpc.BlockNumberOrHash)
- // Try to unmarshal bNrOrHash
- if err := bnOrHash.UnmarshalJSON(data); err == nil {
- sb.BlockNumberOrHash = bnOrHash
- return nil
- }
- // Try to unmarshal RLP
- var input string
- if err := json.Unmarshal(data, &input); err != nil {
- return err
- }
- blob, err := hexutil.Decode(input)
- if err != nil {
- return err
- }
- sb.RLP = blob
- return nil
-}
-
-// GetSigner returns the signer for a specific clique block.
-// Can be called with a block number, a block hash or a rlp encoded blob.
-// The RLP encoded blob can either be a block or a header.
-func (api *API) GetSigner(rlpOrBlockNr *blockNumberOrHashOrRLP) (common.Address, error) {
- if len(rlpOrBlockNr.RLP) == 0 {
- blockNrOrHash := rlpOrBlockNr.BlockNumberOrHash
- var header *types.Header
- if blockNrOrHash == nil {
- header = api.chain.CurrentHeader()
- } else if hash, ok := blockNrOrHash.Hash(); ok {
- header = api.chain.GetHeaderByHash(hash)
- } else if number, ok := blockNrOrHash.Number(); ok {
- header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
- }
- if header == nil {
- return common.Address{}, fmt.Errorf("missing block %v", blockNrOrHash.String())
- }
- return api.clique.Author(header)
- }
- block := new(types.Block)
- if err := rlp.DecodeBytes(rlpOrBlockNr.RLP, block); err == nil {
- return api.clique.Author(block.Header())
- }
- header := new(types.Header)
- if err := rlp.DecodeBytes(rlpOrBlockNr.RLP, header); err != nil {
- return common.Address{}, err
- }
- return api.clique.Author(header)
-}
diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go
deleted file mode 100644
index c693189ea5..0000000000
--- a/consensus/clique/clique.go
+++ /dev/null
@@ -1,781 +0,0 @@
-// 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 .
-
-// Package clique implements the proof-of-authority consensus engine.
-package clique
-
-import (
- "bytes"
- "errors"
- "fmt"
- "io"
- "math/big"
- "math/rand"
- "sync"
- "time"
-
- "github.com/ethereum/go-ethereum/accounts"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/common/hexutil"
- lru "github.com/ethereum/go-ethereum/common/lru"
- "github.com/ethereum/go-ethereum/consensus"
- "github.com/ethereum/go-ethereum/consensus/misc"
- "github.com/ethereum/go-ethereum/consensus/misc/eip1559"
- "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/log"
- "github.com/ethereum/go-ethereum/params"
- "github.com/ethereum/go-ethereum/rlp"
- "github.com/ethereum/go-ethereum/rpc"
- "github.com/ethereum/go-ethereum/trie"
- "golang.org/x/crypto/sha3"
-)
-
-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,
- proposals: make(map[common.Address]bool),
- }
-}
-
-// 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 {
- return c.verifyHeader(chain, header, 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(chain consensus.ChainHeaderReader, headers []*types.Header) (chan<- struct{}, <-chan error) {
- abort := make(chan struct{})
- results := make(chan error, len(headers))
-
- go func() {
- for i, header := range headers {
- err := c.verifyHeader(chain, header, headers[:i])
-
- select {
- case <-abort:
- return
- case results <- err:
- }
- }
- }()
- 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 {
- if header.Number == nil {
- return errUnknownBlock
- }
- number := header.Number.Uint64()
-
- // Don't waste time checking blocks from the future
- if header.Time > uint64(time.Now().Unix()) {
- return consensus.ErrFutureBlock
- }
- // Checkpoint blocks need to enforce zero beneficiary
- checkpoint := (number % c.config.Epoch) == 0
- if checkpoint && header.Coinbase != (common.Address{}) {
- return errInvalidCheckpointBeneficiary
- }
- // Nonces must be 0x00..0 or 0xff..f, zeroes enforced on checkpoints
- if !bytes.Equal(header.Nonce[:], nonceAuthVote) && !bytes.Equal(header.Nonce[:], nonceDropVote) {
- return errInvalidVote
- }
- if checkpoint && !bytes.Equal(header.Nonce[:], nonceDropVote) {
- return errInvalidCheckpointVote
- }
- // Check that the extra-data contains both the vanity and signature
- if len(header.Extra) < extraVanity {
- return errMissingVanity
- }
- if len(header.Extra) < extraVanity+extraSeal {
- return errMissingSignature
- }
- // Ensure that the extra-data contains a signer list on checkpoint, but none otherwise
- signersBytes := len(header.Extra) - extraVanity - extraSeal
- if !checkpoint && signersBytes != 0 {
- return errExtraSigners
- }
- if checkpoint && signersBytes%common.AddressLength != 0 {
- return errInvalidCheckpointSigners
- }
- // Ensure that the mix digest is zero as we don't have fork protection currently
- if header.MixDigest != (common.Hash{}) {
- return errInvalidMixDigest
- }
- // Ensure that the block doesn't contain any uncles which are meaningless in PoA
- if header.UncleHash != uncleHash {
- return errInvalidUncleHash
- }
- // Ensure that the block's difficulty is meaningful (may not be correct at this point)
- if number > 0 {
- if header.Difficulty == nil || (header.Difficulty.Cmp(diffInTurn) != 0 && header.Difficulty.Cmp(diffNoTurn) != 0) {
- return errInvalidDifficulty
- }
- }
- // Verify that the gas limit is <= 2^63-1
- if header.GasLimit > params.MaxGasLimit {
- return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, params.MaxGasLimit)
- }
- if chain.Config().IsShanghai(header.Number, header.Time) {
- return errors.New("clique does not support shanghai fork")
- }
- // Verify the non-existence of withdrawalsHash.
- if header.WithdrawalsHash != nil {
- return fmt.Errorf("invalid withdrawalsHash: have %x, expected nil", header.WithdrawalsHash)
- }
- if chain.Config().IsCancun(header.Number, header.Time) {
- return errors.New("clique does not support cancun fork")
- }
- // Verify the non-existence of cancun-specific header fields
- switch {
- case header.ExcessBlobGas != nil:
- return fmt.Errorf("invalid excessBlobGas: have %d, expected nil", header.ExcessBlobGas)
- case header.BlobGasUsed != nil:
- return fmt.Errorf("invalid blobGasUsed: have %d, expected nil", header.BlobGasUsed)
- case header.ParentBeaconRoot != nil:
- return fmt.Errorf("invalid parentBeaconRoot, have %#x, expected nil", header.ParentBeaconRoot)
- }
- // All basic checks passed, verify cascading fields
- return c.verifyCascadingFields(chain, header, parents)
-}
-
-// verifyCascadingFields verifies all the header fields that are not standalone,
-// rather depend on a batch of previous headers. 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) verifyCascadingFields(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
- }
- if parent.Time+c.config.Period > header.Time {
- return errInvalidTimestamp
- }
- // Verify that the gasUsed is <= gasLimit
- if header.GasUsed > header.GasLimit {
- return fmt.Errorf("invalid gasUsed: have %d, gasLimit %d", header.GasUsed, header.GasLimit)
- }
- if !chain.Config().IsLondon(header.Number) {
- // Verify BaseFee not present before EIP-1559 fork.
- if header.BaseFee != nil {
- return fmt.Errorf("invalid baseFee before fork: have %d, want ", header.BaseFee)
- }
- if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil {
- return err
- }
- } else if err := eip1559.VerifyEIP1559Header(chain.Config(), parent, header); err != nil {
- // Verify the header's EIP-1559 attributes.
- return err
- }
- // Retrieve the snapshot needed to verify this header and cache it
- snap, err := c.snapshot(chain, number-1, header.ParentHash, parents)
- if err != nil {
- return err
- }
- // If the block is a checkpoint block, verify the signer list
- if number%c.config.Epoch == 0 {
- signers := make([]byte, len(snap.Signers)*common.AddressLength)
- for i, signer := range snap.signers() {
- copy(signers[i*common.AddressLength:], signer[:])
- }
- extraSuffix := len(header.Extra) - extraSeal
- if !bytes.Equal(header.Extra[extraVanity:extraSuffix], signers) {
- return errMismatchingCheckpointSigners
- }
- }
- // All basic checks passed, verify the seal and return
- return c.verifySeal(snap, header, parents)
-}
-
-// snapshot retrieves the authorization snapshot at a given point in time.
-func (c *Clique) snapshot(chain consensus.ChainHeaderReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) {
- // Search for a snapshot in memory or on disk for checkpoints
- var (
- headers []*types.Header
- snap *Snapshot
- )
- for snap == nil {
- // If an in-memory snapshot was found, use that
- if s, ok := c.recents.Get(hash); ok {
- snap = s
- break
- }
- // If an on-disk checkpoint snapshot can be found, use that
- if number%checkpointInterval == 0 {
- if s, err := loadSnapshot(c.config, c.signatures, c.db, hash); err == nil {
- log.Trace("Loaded voting snapshot from disk", "number", number, "hash", hash)
- snap = s
- break
- }
- }
- // If we're at the genesis, snapshot the initial state. Alternatively if we're
- // at a checkpoint block without a parent (light client CHT), or we have piled
- // up more headers than allowed to be reorged (chain reinit from a freezer),
- // consider the checkpoint trusted and snapshot it.
- if number == 0 || (number%c.config.Epoch == 0 && (len(headers) > params.FullImmutabilityThreshold || chain.GetHeaderByNumber(number-1) == nil)) {
- checkpoint := chain.GetHeaderByNumber(number)
- if checkpoint != nil {
- hash := checkpoint.Hash()
-
- signers := make([]common.Address, (len(checkpoint.Extra)-extraVanity-extraSeal)/common.AddressLength)
- for i := 0; i < len(signers); i++ {
- copy(signers[i][:], checkpoint.Extra[extraVanity+i*common.AddressLength:])
- }
- snap = newSnapshot(c.config, c.signatures, number, hash, signers)
- if err := snap.store(c.db); err != nil {
- return nil, err
- }
- log.Info("Stored checkpoint snapshot to disk", "number", number, "hash", hash)
- break
- }
- }
- // No snapshot for this header, gather the header and move backward
- var header *types.Header
- if len(parents) > 0 {
- // If we have explicit parents, pick from there (enforced)
- header = parents[len(parents)-1]
- if header.Hash() != hash || header.Number.Uint64() != number {
- return nil, consensus.ErrUnknownAncestor
- }
- parents = parents[:len(parents)-1]
- } else {
- // No explicit parents (or no more left), reach out to the database
- header = chain.GetHeader(hash, number)
- if header == nil {
- return nil, consensus.ErrUnknownAncestor
- }
- }
- headers = append(headers, header)
- number, hash = number-1, header.ParentHash
- }
- // Previous snapshot found, apply any pending headers on top of it
- for i := 0; i < len(headers)/2; i++ {
- headers[i], headers[len(headers)-1-i] = headers[len(headers)-1-i], headers[i]
- }
- snap, err := snap.apply(headers)
- if err != nil {
- return nil, err
- }
- c.recents.Add(snap.Hash, snap)
-
- // If we've generated a new checkpoint snapshot, save to disk
- if snap.Number%checkpointInterval == 0 && len(headers) > 0 {
- if err = snap.store(c.db); err != nil {
- return nil, err
- }
- log.Trace("Stored voting snapshot to disk", "number", snap.Number, "hash", snap.Hash)
- }
- return snap, err
-}
-
-// 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 {
- if len(block.Uncles()) > 0 {
- return errors.New("uncles not allowed")
- }
- return nil
-}
-
-// verifySeal checks whether the signature contained in the header satisfies the
-// consensus protocol requirements. The method accepts an optional list of parent
-// headers that aren't yet part of the local blockchain to generate the snapshots
-// from.
-func (c *Clique) verifySeal(snap *Snapshot, header *types.Header, parents []*types.Header) error {
- // Verifying the genesis block is not supported
- number := header.Number.Uint64()
- if number == 0 {
- return errUnknownBlock
- }
- // Resolve the authorization key and check against signers
- signer, err := ecrecover(header, c.signatures)
- if err != nil {
- return err
- }
- if _, ok := snap.Signers[signer]; !ok {
- return errUnauthorizedSigner
- }
- for seen, recent := range snap.Recents {
- if recent == signer {
- // Signer is among recents, only fail if the current block doesn't shift it out
- if limit := uint64(len(snap.Signers)/2 + 1); seen > number-limit {
- return errRecentlySigned
- }
- }
- }
- // Ensure that the difficulty corresponds to the turn-ness of the signer
- if !c.fakeDiff {
- inturn := snap.inturn(header.Number.Uint64(), signer)
- if inturn && header.Difficulty.Cmp(diffInTurn) != 0 {
- return errWrongDifficulty
- }
- if !inturn && header.Difficulty.Cmp(diffNoTurn) != 0 {
- return errWrongDifficulty
- }
- }
- 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 {
- // If the block isn't a checkpoint, cast a random vote (good enough for now)
- header.Coinbase = common.Address{}
- header.Nonce = types.BlockNonce{}
-
- number := header.Number.Uint64()
- // Assemble the voting snapshot to check which votes make sense
- snap, err := c.snapshot(chain, number-1, header.ParentHash, nil)
- if err != nil {
- return err
- }
- c.lock.RLock()
- if number%c.config.Epoch != 0 {
- // Gather all the proposals that make sense voting on
- addresses := make([]common.Address, 0, len(c.proposals))
- for address, authorize := range c.proposals {
- if snap.validVote(address, authorize) {
- addresses = append(addresses, address)
- }
- }
- // If there's pending proposals, cast a vote on them
- if len(addresses) > 0 {
- header.Coinbase = addresses[rand.Intn(len(addresses))]
- if c.proposals[header.Coinbase] {
- copy(header.Nonce[:], nonceAuthVote)
- } else {
- copy(header.Nonce[:], nonceDropVote)
- }
- }
- }
-
- // Copy signer protected by mutex to avoid race condition
- signer := c.signer
- c.lock.RUnlock()
-
- // Set the correct difficulty
- header.Difficulty = calcDifficulty(snap, signer)
-
- // Ensure the extra data has all its components
- if len(header.Extra) < extraVanity {
- header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, extraVanity-len(header.Extra))...)
- }
- header.Extra = header.Extra[:extraVanity]
-
- if number%c.config.Epoch == 0 {
- for _, signer := range snap.signers() {
- header.Extra = append(header.Extra, signer[:]...)
- }
- }
- header.Extra = append(header.Extra, make([]byte, extraSeal)...)
-
- // Mix digest is reserved for now, set to empty
- header.MixDigest = common.Hash{}
-
- // Ensure the timestamp has the correct delay
- parent := chain.GetHeader(header.ParentHash, number-1)
- if parent == nil {
- return consensus.ErrUnknownAncestor
- }
- header.Time = parent.Time + c.config.Period
- if header.Time < uint64(time.Now().Unix()) {
- header.Time = uint64(time.Now().Unix())
- }
- return nil
-}
-
-// 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, txs []*types.Transaction, uncles []*types.Header, withdrawals []*types.Withdrawal) {
- // 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, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt, withdrawals []*types.Withdrawal) (*types.Block, error) {
- if len(withdrawals) > 0 {
- return nil, errors.New("clique does not support withdrawals")
- }
- // Finalize block
- c.Finalize(chain, header, state, txs, uncles, nil)
-
- // Assign the final state root to header.
- header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
-
- // Assemble and return the final block for sealing.
- return types.NewBlock(header, txs, nil, receipts, trie.NewStackTrie(nil)), nil
-}
-
-// Authorize injects a private key into the consensus engine to mint new blocks
-// with.
-func (c *Clique) Authorize(signer common.Address, signFn SignerFn) {
- c.lock.Lock()
- defer c.lock.Unlock()
-
- c.signer = signer
- c.signFn = signFn
-}
-
-// 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 {
- header := block.Header()
-
- // Sealing the genesis block is not supported
- number := header.Number.Uint64()
- if number == 0 {
- return errUnknownBlock
- }
- // For 0-period chains, refuse to seal empty blocks (no reward but would spin sealing)
- if c.config.Period == 0 && len(block.Transactions()) == 0 {
- return errors.New("sealing paused while waiting for transactions")
- }
- // Don't hold the signer fields for the entire sealing procedure
- c.lock.RLock()
- signer, signFn := c.signer, c.signFn
- c.lock.RUnlock()
-
- // Bail out if we're unauthorized to sign a block
- snap, err := c.snapshot(chain, number-1, header.ParentHash, nil)
- if err != nil {
- return err
- }
- if _, authorized := snap.Signers[signer]; !authorized {
- return errUnauthorizedSigner
- }
- // If we're amongst the recent signers, wait for the next block
- for seen, recent := range snap.Recents {
- if recent == signer {
- // Signer is among recents, only wait if the current block doesn't shift it out
- if limit := uint64(len(snap.Signers)/2 + 1); number < limit || seen > number-limit {
- return errors.New("signed recently, must wait for others")
- }
- }
- }
- // Sweet, the protocol permits us to sign the block, wait for our time
- delay := time.Unix(int64(header.Time), 0).Sub(time.Now()) // nolint: gosimple
- if header.Difficulty.Cmp(diffNoTurn) == 0 {
- // It's not our turn explicitly to sign, delay it a bit
- wiggle := time.Duration(len(snap.Signers)/2+1) * wiggleTime
- delay += time.Duration(rand.Int63n(int64(wiggle)))
-
- log.Trace("Out-of-turn signing requested", "wiggle", common.PrettyDuration(wiggle))
- }
- // Sign all the things!
- sighash, err := signFn(accounts.Account{Address: signer}, accounts.MimetypeClique, CliqueRLP(header))
- if err != nil {
- return err
- }
- copy(header.Extra[len(header.Extra)-extraSeal:], sighash)
- // Wait until sealing is terminated or delay timeout.
- log.Trace("Waiting for slot to sign and propagate", "delay", common.PrettyDuration(delay))
- go func() {
- select {
- case <-stop:
- return
- case <-time.After(delay):
- }
-
- select {
- case results <- block.WithSeal(header):
- default:
- log.Warn("Sealing result is not read by miner", "sealhash", SealHash(header))
- }
- }()
-
- return nil
-}
-
-// 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 {
- snap, err := c.snapshot(chain, parent.Number.Uint64(), parent.Hash(), nil)
- if err != nil {
- return nil
- }
- c.lock.RLock()
- signer := c.signer
- c.lock.RUnlock()
- return calcDifficulty(snap, signer)
-}
-
-func calcDifficulty(snap *Snapshot, signer common.Address) *big.Int {
- if snap.inturn(snap.Number+1, signer) {
- return new(big.Int).Set(diffInTurn)
- }
- return new(big.Int).Set(diffNoTurn)
-}
-
-// SealHash returns the hash of a block prior to it being sealed.
-func (c *Clique) SealHash(header *types.Header) common.Hash {
- return SealHash(header)
-}
-
-// Close implements consensus.Engine. It's a noop for clique as there are no background threads.
-func (c *Clique) Close() error {
- return nil
-}
-
-// APIs implements consensus.Engine, returning the user facing RPC API to allow
-// controlling the signer voting.
-func (c *Clique) APIs(chain consensus.ChainHeaderReader) []rpc.API {
- return []rpc.API{{
- Namespace: "clique",
- Service: &API{chain: chain, clique: c},
- }}
-}
-
-// 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
-}
-
-// CliqueRLP returns the rlp bytes which needs to be signed for the proof-of-authority
-// sealing. The RLP to sign consists of the entire header apart from the 65 byte signature
-// contained at the end of the extra data.
-//
-// Note, the method requires the extra data to be at least 65 bytes, otherwise it
-// panics. This is done to avoid accidentally using both forms (signature present
-// or not), which could be abused to produce different hashes for the same header.
-func CliqueRLP(header *types.Header) []byte {
- b := new(bytes.Buffer)
- encodeSigHeader(b, header)
- return b.Bytes()
-}
-
-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())
- }
-}
diff --git a/consensus/clique/clique_test.go b/consensus/clique/clique_test.go
deleted file mode 100644
index 7cd5919c5e..0000000000
--- a/consensus/clique/clique_test.go
+++ /dev/null
@@ -1,125 +0,0 @@
-// Copyright 2019 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 .
-
-package clique
-
-import (
- "math/big"
- "testing"
-
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/core"
- "github.com/ethereum/go-ethereum/core/rawdb"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/core/vm"
- "github.com/ethereum/go-ethereum/crypto"
- "github.com/ethereum/go-ethereum/params"
-)
-
-// This test case is a repro of an annoying bug that took us forever to catch.
-// In Clique PoA networks (Görli, etc), consecutive blocks might have
-// the same state root (no block subsidy, empty block). If a node crashes, the
-// chain ends up losing the recent state and needs to regenerate it from blocks
-// already in the database. The bug was that processing the block *prior* to an
-// empty one **also completes** the empty one, ending up in a known-block error.
-func TestReimportMirroredState(t *testing.T) {
- // Initialize a Clique chain with a single signer
- var (
- db = rawdb.NewMemoryDatabase()
- key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
- addr = crypto.PubkeyToAddress(key.PublicKey)
- engine = New(params.AllCliqueProtocolChanges.Clique, db)
- signer = new(types.HomesteadSigner)
- )
- genspec := &core.Genesis{
- Config: params.AllCliqueProtocolChanges,
- ExtraData: make([]byte, extraVanity+common.AddressLength+extraSeal),
- Alloc: map[common.Address]core.GenesisAccount{
- addr: {Balance: big.NewInt(10000000000000000)},
- },
- BaseFee: big.NewInt(params.InitialBaseFee),
- }
- copy(genspec.ExtraData[extraVanity:], addr[:])
-
- // Generate a batch of blocks, each properly signed
- chain, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, genspec, nil, engine, vm.Config{}, nil, nil)
- defer chain.Stop()
-
- _, blocks, _ := core.GenerateChainWithGenesis(genspec, engine, 3, func(i int, block *core.BlockGen) {
- // The chain maker doesn't have access to a chain, so the difficulty will be
- // lets unset (nil). Set it here to the correct value.
- block.SetDifficulty(diffInTurn)
-
- // We want to simulate an empty middle block, having the same state as the
- // first one. The last is needs a state change again to force a reorg.
- if i != 1 {
- tx, err := types.SignTx(types.NewTransaction(block.TxNonce(addr), common.Address{0x00}, new(big.Int), params.TxGas, block.BaseFee(), nil), signer, key)
- if err != nil {
- panic(err)
- }
- block.AddTxWithChain(chain, tx)
- }
- })
- for i, block := range blocks {
- header := block.Header()
- if i > 0 {
- header.ParentHash = blocks[i-1].Hash()
- }
- header.Extra = make([]byte, extraVanity+extraSeal)
- header.Difficulty = diffInTurn
-
- sig, _ := crypto.Sign(SealHash(header).Bytes(), key)
- copy(header.Extra[len(header.Extra)-extraSeal:], sig)
- blocks[i] = block.WithSeal(header)
- }
- // Insert the first two blocks and make sure the chain is valid
- db = rawdb.NewMemoryDatabase()
- chain, _ = core.NewBlockChain(db, nil, genspec, nil, engine, vm.Config{}, nil, nil)
- defer chain.Stop()
-
- if _, err := chain.InsertChain(blocks[:2]); err != nil {
- t.Fatalf("failed to insert initial blocks: %v", err)
- }
- if head := chain.CurrentBlock().Number.Uint64(); head != 2 {
- t.Fatalf("chain head mismatch: have %d, want %d", head, 2)
- }
-
- // Simulate a crash by creating a new chain on top of the database, without
- // flushing the dirty states out. Insert the last block, triggering a sidechain
- // reimport.
- chain, _ = core.NewBlockChain(db, nil, genspec, nil, engine, vm.Config{}, nil, nil)
- defer chain.Stop()
-
- if _, err := chain.InsertChain(blocks[2:]); err != nil {
- t.Fatalf("failed to insert final block: %v", err)
- }
- if head := chain.CurrentBlock().Number.Uint64(); head != 3 {
- t.Fatalf("chain head mismatch: have %d, want %d", head, 3)
- }
-}
-
-func TestSealHash(t *testing.T) {
- have := SealHash(&types.Header{
- Difficulty: new(big.Int),
- Number: new(big.Int),
- Extra: make([]byte, 32+65),
- BaseFee: new(big.Int),
- })
- want := common.HexToHash("0xbd3d1fa43fbc4c5bfcc91b179ec92e2861df3654de60468beb908ff805359e8f")
- if have != want {
- t.Errorf("have %x, want %x", have, want)
- }
-}
diff --git a/consensus/clique/snapshot.go b/consensus/clique/snapshot.go
deleted file mode 100644
index a97115121b..0000000000
--- a/consensus/clique/snapshot.go
+++ /dev/null
@@ -1,322 +0,0 @@
-// 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 .
-
-package clique
-
-import (
- "bytes"
- "encoding/json"
- "time"
-
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/common/lru"
- "github.com/ethereum/go-ethereum/core/rawdb"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/ethdb"
- "github.com/ethereum/go-ethereum/log"
- "github.com/ethereum/go-ethereum/params"
- "golang.org/x/exp/slices"
-)
-
-// Vote represents a single vote that an authorized signer made to modify the
-// list of authorizations.
-type Vote struct {
- Signer common.Address `json:"signer"` // Authorized signer that cast this vote
- Block uint64 `json:"block"` // Block number the vote was cast in (expire old votes)
- Address common.Address `json:"address"` // Account being voted on to change its authorization
- Authorize bool `json:"authorize"` // Whether to authorize or deauthorize the voted account
-}
-
-// Tally is a simple vote tally to keep the current score of votes. Votes that
-// go against the proposal aren't counted since it's equivalent to not voting.
-type Tally struct {
- Authorize bool `json:"authorize"` // Whether the vote is about authorizing or kicking someone
- Votes int `json:"votes"` // Number of votes until now wanting to pass the proposal
-}
-
-type sigLRU = lru.Cache[common.Hash, common.Address]
-
-// Snapshot is the state of the authorization voting at a given point in time.
-type Snapshot struct {
- config *params.CliqueConfig // Consensus engine parameters to fine tune behavior
- sigcache *sigLRU // Cache of recent block signatures to speed up ecrecover
-
- Number uint64 `json:"number"` // Block number where the snapshot was created
- Hash common.Hash `json:"hash"` // Block hash where the snapshot was created
- Signers map[common.Address]struct{} `json:"signers"` // Set of authorized signers at this moment
- Recents map[uint64]common.Address `json:"recents"` // Set of recent signers for spam protections
- Votes []*Vote `json:"votes"` // List of votes cast in chronological order
- Tally map[common.Address]Tally `json:"tally"` // Current vote tally to avoid recalculating
-}
-
-// 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
-// the genesis block.
-func newSnapshot(config *params.CliqueConfig, sigcache *sigLRU, number uint64, hash common.Hash, signers []common.Address) *Snapshot {
- snap := &Snapshot{
- config: config,
- sigcache: sigcache,
- Number: number,
- Hash: hash,
- Signers: make(map[common.Address]struct{}),
- Recents: make(map[uint64]common.Address),
- Tally: make(map[common.Address]Tally),
- }
- for _, signer := range signers {
- snap.Signers[signer] = struct{}{}
- }
- return snap
-}
-
-// loadSnapshot loads an existing snapshot from the database.
-func loadSnapshot(config *params.CliqueConfig, sigcache *sigLRU, db ethdb.Database, hash common.Hash) (*Snapshot, error) {
- blob, err := db.Get(append(rawdb.CliqueSnapshotPrefix, hash[:]...))
- if err != nil {
- return nil, err
- }
- snap := new(Snapshot)
- if err := json.Unmarshal(blob, snap); err != nil {
- return nil, err
- }
- snap.config = config
- snap.sigcache = sigcache
-
- return snap, nil
-}
-
-// store inserts the snapshot into the database.
-func (s *Snapshot) store(db ethdb.Database) error {
- blob, err := json.Marshal(s)
- if err != nil {
- return err
- }
- return db.Put(append(rawdb.CliqueSnapshotPrefix, s.Hash[:]...), blob)
-}
-
-// copy creates a deep copy of the snapshot, though not the individual votes.
-func (s *Snapshot) copy() *Snapshot {
- cpy := &Snapshot{
- config: s.config,
- sigcache: s.sigcache,
- Number: s.Number,
- Hash: s.Hash,
- Signers: make(map[common.Address]struct{}),
- Recents: make(map[uint64]common.Address),
- Votes: make([]*Vote, len(s.Votes)),
- Tally: make(map[common.Address]Tally),
- }
- for signer := range s.Signers {
- cpy.Signers[signer] = struct{}{}
- }
- for block, signer := range s.Recents {
- cpy.Recents[block] = signer
- }
- for address, tally := range s.Tally {
- cpy.Tally[address] = tally
- }
- copy(cpy.Votes, s.Votes)
-
- return cpy
-}
-
-// validVote returns whether it makes sense to cast the specified vote in the
-// given snapshot context (e.g. don't try to add an already authorized signer).
-func (s *Snapshot) validVote(address common.Address, authorize bool) bool {
- _, signer := s.Signers[address]
- return (signer && !authorize) || (!signer && authorize)
-}
-
-// cast adds a new vote into the tally.
-func (s *Snapshot) cast(address common.Address, authorize bool) bool {
- // Ensure the vote is meaningful
- if !s.validVote(address, authorize) {
- return false
- }
- // Cast the vote into an existing or new tally
- if old, ok := s.Tally[address]; ok {
- old.Votes++
- s.Tally[address] = old
- } else {
- s.Tally[address] = Tally{Authorize: authorize, Votes: 1}
- }
- return true
-}
-
-// uncast removes a previously cast vote from the tally.
-func (s *Snapshot) uncast(address common.Address, authorize bool) bool {
- // If there's no tally, it's a dangling vote, just drop
- tally, ok := s.Tally[address]
- if !ok {
- return false
- }
- // Ensure we only revert counted votes
- if tally.Authorize != authorize {
- return false
- }
- // Otherwise revert the vote
- if tally.Votes > 1 {
- tally.Votes--
- s.Tally[address] = tally
- } else {
- delete(s.Tally, address)
- }
- return true
-}
-
-// apply creates a new authorization snapshot by applying the given headers to
-// the original one.
-func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
- // Allow passing in no headers for cleaner code
- if len(headers) == 0 {
- return s, nil
- }
- // Sanity check that the headers can be applied
- for i := 0; i < len(headers)-1; i++ {
- if headers[i+1].Number.Uint64() != headers[i].Number.Uint64()+1 {
- return nil, errInvalidVotingChain
- }
- }
- if headers[0].Number.Uint64() != s.Number+1 {
- return nil, errInvalidVotingChain
- }
- // Iterate through the headers and create a new snapshot
- snap := s.copy()
-
- var (
- start = time.Now()
- logged = time.Now()
- )
- for i, header := range headers {
- // Remove any votes on checkpoint blocks
- number := header.Number.Uint64()
- if number%s.config.Epoch == 0 {
- snap.Votes = nil
- snap.Tally = make(map[common.Address]Tally)
- }
- // Delete the oldest signer from the recent list to allow it signing again
- if limit := uint64(len(snap.Signers)/2 + 1); number >= limit {
- delete(snap.Recents, number-limit)
- }
- // Resolve the authorization key and check against signers
- signer, err := ecrecover(header, s.sigcache)
- if err != nil {
- return nil, err
- }
- if _, ok := snap.Signers[signer]; !ok {
- return nil, errUnauthorizedSigner
- }
- for _, recent := range snap.Recents {
- if recent == signer {
- return nil, errRecentlySigned
- }
- }
- snap.Recents[number] = signer
-
- // Header authorized, discard any previous votes from the signer
- for i, vote := range snap.Votes {
- if vote.Signer == signer && vote.Address == header.Coinbase {
- // Uncast the vote from the cached tally
- snap.uncast(vote.Address, vote.Authorize)
-
- // Uncast the vote from the chronological list
- snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
- break // only one vote allowed
- }
- }
- // Tally up the new vote from the signer
- var authorize bool
- switch {
- case bytes.Equal(header.Nonce[:], nonceAuthVote):
- authorize = true
- case bytes.Equal(header.Nonce[:], nonceDropVote):
- authorize = false
- default:
- return nil, errInvalidVote
- }
- if snap.cast(header.Coinbase, authorize) {
- snap.Votes = append(snap.Votes, &Vote{
- Signer: signer,
- Block: number,
- Address: header.Coinbase,
- Authorize: authorize,
- })
- }
- // If the vote passed, update the list of signers
- if tally := snap.Tally[header.Coinbase]; tally.Votes > len(snap.Signers)/2 {
- if tally.Authorize {
- snap.Signers[header.Coinbase] = struct{}{}
- } else {
- delete(snap.Signers, header.Coinbase)
-
- // Signer list shrunk, delete any leftover recent caches
- if limit := uint64(len(snap.Signers)/2 + 1); number >= limit {
- delete(snap.Recents, number-limit)
- }
- // Discard any previous votes the deauthorized signer cast
- for i := 0; i < len(snap.Votes); i++ {
- if snap.Votes[i].Signer == header.Coinbase {
- // Uncast the vote from the cached tally
- snap.uncast(snap.Votes[i].Address, snap.Votes[i].Authorize)
-
- // Uncast the vote from the chronological list
- snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
-
- i--
- }
- }
- }
- // Discard any previous votes around the just changed account
- for i := 0; i < len(snap.Votes); i++ {
- if snap.Votes[i].Address == header.Coinbase {
- snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
- i--
- }
- }
- delete(snap.Tally, header.Coinbase)
- }
- // If we're taking too much time (ecrecover), notify the user once a while
- if time.Since(logged) > 8*time.Second {
- log.Info("Reconstructing voting history", "processed", i, "total", len(headers), "elapsed", common.PrettyDuration(time.Since(start)))
- logged = time.Now()
- }
- }
- if time.Since(start) > 8*time.Second {
- log.Info("Reconstructed voting history", "processed", len(headers), "elapsed", common.PrettyDuration(time.Since(start)))
- }
- snap.Number += uint64(len(headers))
- snap.Hash = headers[len(headers)-1].Hash()
-
- return snap, nil
-}
-
-// signers retrieves the list of authorized signers in ascending order.
-func (s *Snapshot) signers() []common.Address {
- sigs := make([]common.Address, 0, len(s.Signers))
- for sig := range s.Signers {
- sigs = append(sigs, sig)
- }
- slices.SortFunc(sigs, common.Address.Cmp)
- return sigs
-}
-
-// inturn returns if a signer at a given block height is in-turn or not.
-func (s *Snapshot) inturn(number uint64, signer common.Address) bool {
- signers, offset := s.signers(), 0
- for offset < len(signers) && signers[offset] != signer {
- offset++
- }
- return (number % uint64(len(signers))) == uint64(offset)
-}
diff --git a/consensus/clique/snapshot_test.go b/consensus/clique/snapshot_test.go
deleted file mode 100644
index 26cebe008a..0000000000
--- a/consensus/clique/snapshot_test.go
+++ /dev/null
@@ -1,508 +0,0 @@
-// 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 .
-
-package clique
-
-import (
- "bytes"
- "crypto/ecdsa"
- "fmt"
- "math/big"
- "testing"
-
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/core"
- "github.com/ethereum/go-ethereum/core/rawdb"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/core/vm"
- "github.com/ethereum/go-ethereum/crypto"
- "github.com/ethereum/go-ethereum/params"
- "golang.org/x/exp/slices"
-)
-
-// testerAccountPool is a pool to maintain currently active tester accounts,
-// mapped from textual names used in the tests below to actual Ethereum private
-// keys capable of signing transactions.
-type testerAccountPool struct {
- accounts map[string]*ecdsa.PrivateKey
-}
-
-func newTesterAccountPool() *testerAccountPool {
- return &testerAccountPool{
- accounts: make(map[string]*ecdsa.PrivateKey),
- }
-}
-
-// checkpoint creates a Clique checkpoint signer section from the provided list
-// of authorized signers and embeds it into the provided header.
-func (ap *testerAccountPool) checkpoint(header *types.Header, signers []string) {
- auths := make([]common.Address, len(signers))
- for i, signer := range signers {
- auths[i] = ap.address(signer)
- }
- slices.SortFunc(auths, common.Address.Cmp)
- for i, auth := range auths {
- copy(header.Extra[extraVanity+i*common.AddressLength:], auth.Bytes())
- }
-}
-
-// address retrieves the Ethereum address of a tester account by label, creating
-// a new account if no previous one exists yet.
-func (ap *testerAccountPool) address(account string) common.Address {
- // Return the zero account for non-addresses
- if account == "" {
- return common.Address{}
- }
- // Ensure we have a persistent key for the account
- if ap.accounts[account] == nil {
- ap.accounts[account], _ = crypto.GenerateKey()
- }
- // Resolve and return the Ethereum address
- return crypto.PubkeyToAddress(ap.accounts[account].PublicKey)
-}
-
-// sign calculates a Clique digital signature for the given block and embeds it
-// back into the header.
-func (ap *testerAccountPool) sign(header *types.Header, signer string) {
- // Ensure we have a persistent key for the signer
- if ap.accounts[signer] == nil {
- ap.accounts[signer], _ = crypto.GenerateKey()
- }
- // Sign the header and embed the signature in extra data
- sig, _ := crypto.Sign(SealHash(header).Bytes(), ap.accounts[signer])
- copy(header.Extra[len(header.Extra)-extraSeal:], sig)
-}
-
-// testerVote represents a single block signed by a particular account, where
-// the account may or may not have cast a Clique vote.
-type testerVote struct {
- signer string
- voted string
- auth bool
- checkpoint []string
- newbatch bool
-}
-
-type cliqueTest struct {
- epoch uint64
- signers []string
- votes []testerVote
- results []string
- failure error
-}
-
-// Tests that Clique signer voting is evaluated correctly for various simple and
-// complex scenarios, as well as that a few special corner cases fail correctly.
-func TestClique(t *testing.T) {
- // Define the various voting scenarios to test
- tests := []cliqueTest{
- {
- // Single signer, no votes cast
- signers: []string{"A"},
- votes: []testerVote{{signer: "A"}},
- results: []string{"A"},
- }, {
- // Single signer, voting to add two others (only accept first, second needs 2 votes)
- signers: []string{"A"},
- votes: []testerVote{
- {signer: "A", voted: "B", auth: true},
- {signer: "B"},
- {signer: "A", voted: "C", auth: true},
- },
- results: []string{"A", "B"},
- }, {
- // Two signers, voting to add three others (only accept first two, third needs 3 votes already)
- signers: []string{"A", "B"},
- votes: []testerVote{
- {signer: "A", voted: "C", auth: true},
- {signer: "B", voted: "C", auth: true},
- {signer: "A", voted: "D", auth: true},
- {signer: "B", voted: "D", auth: true},
- {signer: "C"},
- {signer: "A", voted: "E", auth: true},
- {signer: "B", voted: "E", auth: true},
- },
- results: []string{"A", "B", "C", "D"},
- }, {
- // Single signer, dropping itself (weird, but one less cornercase by explicitly allowing this)
- signers: []string{"A"},
- votes: []testerVote{
- {signer: "A", voted: "A", auth: false},
- },
- results: []string{},
- }, {
- // Two signers, actually needing mutual consent to drop either of them (not fulfilled)
- signers: []string{"A", "B"},
- votes: []testerVote{
- {signer: "A", voted: "B", auth: false},
- },
- results: []string{"A", "B"},
- }, {
- // Two signers, actually needing mutual consent to drop either of them (fulfilled)
- signers: []string{"A", "B"},
- votes: []testerVote{
- {signer: "A", voted: "B", auth: false},
- {signer: "B", voted: "B", auth: false},
- },
- results: []string{"A"},
- }, {
- // Three signers, two of them deciding to drop the third
- signers: []string{"A", "B", "C"},
- votes: []testerVote{
- {signer: "A", voted: "C", auth: false},
- {signer: "B", voted: "C", auth: false},
- },
- results: []string{"A", "B"},
- }, {
- // Four signers, consensus of two not being enough to drop anyone
- signers: []string{"A", "B", "C", "D"},
- votes: []testerVote{
- {signer: "A", voted: "C", auth: false},
- {signer: "B", voted: "C", auth: false},
- },
- results: []string{"A", "B", "C", "D"},
- }, {
- // Four signers, consensus of three already being enough to drop someone
- signers: []string{"A", "B", "C", "D"},
- votes: []testerVote{
- {signer: "A", voted: "D", auth: false},
- {signer: "B", voted: "D", auth: false},
- {signer: "C", voted: "D", auth: false},
- },
- results: []string{"A", "B", "C"},
- }, {
- // Authorizations are counted once per signer per target
- signers: []string{"A", "B"},
- votes: []testerVote{
- {signer: "A", voted: "C", auth: true},
- {signer: "B"},
- {signer: "A", voted: "C", auth: true},
- {signer: "B"},
- {signer: "A", voted: "C", auth: true},
- },
- results: []string{"A", "B"},
- }, {
- // Authorizing multiple accounts concurrently is permitted
- signers: []string{"A", "B"},
- votes: []testerVote{
- {signer: "A", voted: "C", auth: true},
- {signer: "B"},
- {signer: "A", voted: "D", auth: true},
- {signer: "B"},
- {signer: "A"},
- {signer: "B", voted: "D", auth: true},
- {signer: "A"},
- {signer: "B", voted: "C", auth: true},
- },
- results: []string{"A", "B", "C", "D"},
- }, {
- // Deauthorizations are counted once per signer per target
- signers: []string{"A", "B"},
- votes: []testerVote{
- {signer: "A", voted: "B", auth: false},
- {signer: "B"},
- {signer: "A", voted: "B", auth: false},
- {signer: "B"},
- {signer: "A", voted: "B", auth: false},
- },
- results: []string{"A", "B"},
- }, {
- // Deauthorizing multiple accounts concurrently is permitted
- signers: []string{"A", "B", "C", "D"},
- votes: []testerVote{
- {signer: "A", voted: "C", auth: false},
- {signer: "B"},
- {signer: "C"},
- {signer: "A", voted: "D", auth: false},
- {signer: "B"},
- {signer: "C"},
- {signer: "A"},
- {signer: "B", voted: "D", auth: false},
- {signer: "C", voted: "D", auth: false},
- {signer: "A"},
- {signer: "B", voted: "C", auth: false},
- },
- results: []string{"A", "B"},
- }, {
- // Votes from deauthorized signers are discarded immediately (deauth votes)
- signers: []string{"A", "B", "C"},
- votes: []testerVote{
- {signer: "C", voted: "B", auth: false},
- {signer: "A", voted: "C", auth: false},
- {signer: "B", voted: "C", auth: false},
- {signer: "A", voted: "B", auth: false},
- },
- results: []string{"A", "B"},
- }, {
- // Votes from deauthorized signers are discarded immediately (auth votes)
- signers: []string{"A", "B", "C"},
- votes: []testerVote{
- {signer: "C", voted: "D", auth: true},
- {signer: "A", voted: "C", auth: false},
- {signer: "B", voted: "C", auth: false},
- {signer: "A", voted: "D", auth: true},
- },
- results: []string{"A", "B"},
- }, {
- // Cascading changes are not allowed, only the account being voted on may change
- signers: []string{"A", "B", "C", "D"},
- votes: []testerVote{
- {signer: "A", voted: "C", auth: false},
- {signer: "B"},
- {signer: "C"},
- {signer: "A", voted: "D", auth: false},
- {signer: "B", voted: "C", auth: false},
- {signer: "C"},
- {signer: "A"},
- {signer: "B", voted: "D", auth: false},
- {signer: "C", voted: "D", auth: false},
- },
- results: []string{"A", "B", "C"},
- }, {
- // Changes reaching consensus out of bounds (via a deauth) execute on touch
- signers: []string{"A", "B", "C", "D"},
- votes: []testerVote{
- {signer: "A", voted: "C", auth: false},
- {signer: "B"},
- {signer: "C"},
- {signer: "A", voted: "D", auth: false},
- {signer: "B", voted: "C", auth: false},
- {signer: "C"},
- {signer: "A"},
- {signer: "B", voted: "D", auth: false},
- {signer: "C", voted: "D", auth: false},
- {signer: "A"},
- {signer: "C", voted: "C", auth: true},
- },
- results: []string{"A", "B"},
- }, {
- // Changes reaching consensus out of bounds (via a deauth) may go out of consensus on first touch
- signers: []string{"A", "B", "C", "D"},
- votes: []testerVote{
- {signer: "A", voted: "C", auth: false},
- {signer: "B"},
- {signer: "C"},
- {signer: "A", voted: "D", auth: false},
- {signer: "B", voted: "C", auth: false},
- {signer: "C"},
- {signer: "A"},
- {signer: "B", voted: "D", auth: false},
- {signer: "C", voted: "D", auth: false},
- {signer: "A"},
- {signer: "B", voted: "C", auth: true},
- },
- results: []string{"A", "B", "C"},
- }, {
- // Ensure that pending votes don't survive authorization status changes. This
- // corner case can only appear if a signer is quickly added, removed and then
- // re-added (or the inverse), while one of the original voters dropped. If a
- // past vote is left cached in the system somewhere, this will interfere with
- // the final signer outcome.
- signers: []string{"A", "B", "C", "D", "E"},
- votes: []testerVote{
- {signer: "A", voted: "F", auth: true}, // Authorize F, 3 votes needed
- {signer: "B", voted: "F", auth: true},
- {signer: "C", voted: "F", auth: true},
- {signer: "D", voted: "F", auth: false}, // Deauthorize F, 4 votes needed (leave A's previous vote "unchanged")
- {signer: "E", voted: "F", auth: false},
- {signer: "B", voted: "F", auth: false},
- {signer: "C", voted: "F", auth: false},
- {signer: "D", voted: "F", auth: true}, // Almost authorize F, 2/3 votes needed
- {signer: "E", voted: "F", auth: true},
- {signer: "B", voted: "A", auth: false}, // Deauthorize A, 3 votes needed
- {signer: "C", voted: "A", auth: false},
- {signer: "D", voted: "A", auth: false},
- {signer: "B", voted: "F", auth: true}, // Finish authorizing F, 3/3 votes needed
- },
- results: []string{"B", "C", "D", "E", "F"},
- }, {
- // Epoch transitions reset all votes to allow chain checkpointing
- epoch: 3,
- signers: []string{"A", "B"},
- votes: []testerVote{
- {signer: "A", voted: "C", auth: true},
- {signer: "B"},
- {signer: "A", checkpoint: []string{"A", "B"}},
- {signer: "B", voted: "C", auth: true},
- },
- results: []string{"A", "B"},
- }, {
- // An unauthorized signer should not be able to sign blocks
- signers: []string{"A"},
- votes: []testerVote{
- {signer: "B"},
- },
- failure: errUnauthorizedSigner,
- }, {
- // An authorized signer that signed recently should not be able to sign again
- signers: []string{"A", "B"},
- votes: []testerVote{
- {signer: "A"},
- {signer: "A"},
- },
- failure: errRecentlySigned,
- }, {
- // Recent signatures should not reset on checkpoint blocks imported in a batch
- epoch: 3,
- signers: []string{"A", "B", "C"},
- votes: []testerVote{
- {signer: "A"},
- {signer: "B"},
- {signer: "A", checkpoint: []string{"A", "B", "C"}},
- {signer: "A"},
- },
- failure: errRecentlySigned,
- }, {
- // Recent signatures should not reset on checkpoint blocks imported in a new
- // batch (https://github.com/ethereum/go-ethereum/issues/17593). Whilst this
- // seems overly specific and weird, it was a Rinkeby consensus split.
- epoch: 3,
- signers: []string{"A", "B", "C"},
- votes: []testerVote{
- {signer: "A"},
- {signer: "B"},
- {signer: "A", checkpoint: []string{"A", "B", "C"}},
- {signer: "A", newbatch: true},
- },
- failure: errRecentlySigned,
- },
- }
-
- // Run through the scenarios and test them
- for i, tt := range tests {
- t.Run(fmt.Sprint(i), tt.run)
- }
-}
-
-func (tt *cliqueTest) run(t *testing.T) {
- // Create the account pool and generate the initial set of signers
- accounts := newTesterAccountPool()
-
- signers := make([]common.Address, len(tt.signers))
- for j, signer := range tt.signers {
- signers[j] = accounts.address(signer)
- }
- for j := 0; j < len(signers); j++ {
- for k := j + 1; k < len(signers); k++ {
- if bytes.Compare(signers[j][:], signers[k][:]) > 0 {
- signers[j], signers[k] = signers[k], signers[j]
- }
- }
- }
- // Create the genesis block with the initial set of signers
- genesis := &core.Genesis{
- ExtraData: make([]byte, extraVanity+common.AddressLength*len(signers)+extraSeal),
- BaseFee: big.NewInt(params.InitialBaseFee),
- }
- for j, signer := range signers {
- copy(genesis.ExtraData[extraVanity+j*common.AddressLength:], signer[:])
- }
-
- // Assemble a chain of headers from the cast votes
- config := *params.TestChainConfig
- config.Clique = ¶ms.CliqueConfig{
- Period: 1,
- Epoch: tt.epoch,
- }
- genesis.Config = &config
-
- engine := New(config.Clique, rawdb.NewMemoryDatabase())
- engine.fakeDiff = true
-
- _, blocks, _ := core.GenerateChainWithGenesis(genesis, engine, len(tt.votes), func(j int, gen *core.BlockGen) {
- // Cast the vote contained in this block
- gen.SetCoinbase(accounts.address(tt.votes[j].voted))
- if tt.votes[j].auth {
- var nonce types.BlockNonce
- copy(nonce[:], nonceAuthVote)
- gen.SetNonce(nonce)
- }
- })
- // Iterate through the blocks and seal them individually
- for j, block := range blocks {
- // Get the header and prepare it for signing
- header := block.Header()
- if j > 0 {
- header.ParentHash = blocks[j-1].Hash()
- }
- header.Extra = make([]byte, extraVanity+extraSeal)
- if auths := tt.votes[j].checkpoint; auths != nil {
- header.Extra = make([]byte, extraVanity+len(auths)*common.AddressLength+extraSeal)
- accounts.checkpoint(header, auths)
- }
- header.Difficulty = diffInTurn // Ignored, we just need a valid number
-
- // Generate the signature, embed it into the header and the block
- accounts.sign(header, tt.votes[j].signer)
- blocks[j] = block.WithSeal(header)
- }
- // Split the blocks up into individual import batches (cornercase testing)
- batches := [][]*types.Block{nil}
- for j, block := range blocks {
- if tt.votes[j].newbatch {
- batches = append(batches, nil)
- }
- batches[len(batches)-1] = append(batches[len(batches)-1], block)
- }
- // Pass all the headers through clique and ensure tallying succeeds
- chain, err := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, genesis, nil, engine, vm.Config{}, nil, nil)
- if err != nil {
- t.Fatalf("failed to create test chain: %v", err)
- }
- defer chain.Stop()
-
- for j := 0; j < len(batches)-1; j++ {
- if k, err := chain.InsertChain(batches[j]); err != nil {
- t.Fatalf("failed to import batch %d, block %d: %v", j, k, err)
- break
- }
- }
- if _, err = chain.InsertChain(batches[len(batches)-1]); err != tt.failure {
- t.Errorf("failure mismatch: have %v, want %v", err, tt.failure)
- }
- if tt.failure != nil {
- return
- }
-
- // No failure was produced or requested, generate the final voting snapshot
- head := blocks[len(blocks)-1]
-
- snap, err := engine.snapshot(chain, head.NumberU64(), head.Hash(), nil)
- if err != nil {
- t.Fatalf("failed to retrieve voting snapshot: %v", err)
- }
- // Verify the final list of signers against the expected ones
- signers = make([]common.Address, len(tt.results))
- for j, signer := range tt.results {
- signers[j] = accounts.address(signer)
- }
- for j := 0; j < len(signers); j++ {
- for k := j + 1; k < len(signers); k++ {
- if bytes.Compare(signers[j][:], signers[k][:]) > 0 {
- signers[j], signers[k] = signers[k], signers[j]
- }
- }
- }
- result := snap.signers()
- if len(result) != len(signers) {
- t.Fatalf("signers mismatch: have %x, want %x", result, signers)
- }
- for j := 0; j < len(result); j++ {
- if !bytes.Equal(result[j][:], signers[j][:]) {
- t.Fatalf("signer %d: signer mismatch: have %x, want %x", j, result[j], signers[j])
- }
- }
-}
diff --git a/consensus/consensus.go b/consensus/consensus.go
deleted file mode 100644
index 3a2c2d2229..0000000000
--- a/consensus/consensus.go
+++ /dev/null
@@ -1,129 +0,0 @@
-// 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 .
-
-// Package consensus implements different Ethereum consensus engines.
-package consensus
-
-import (
- "math/big"
-
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/core/state"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/params"
- "github.com/ethereum/go-ethereum/rpc"
-)
-
-// ChainHeaderReader defines a small collection of methods needed to access the local
-// blockchain during header verification.
-type ChainHeaderReader interface {
- // Config retrieves the blockchain's chain configuration.
- Config() *params.ChainConfig
-
- // CurrentHeader retrieves the current header from the local chain.
- CurrentHeader() *types.Header
-
- // GetHeader retrieves a block header from the database by hash and number.
- GetHeader(hash common.Hash, number uint64) *types.Header
-
- // GetHeaderByNumber retrieves a block header from the database by number.
- GetHeaderByNumber(number uint64) *types.Header
-
- // GetHeaderByHash retrieves a block header from the database by its hash.
- GetHeaderByHash(hash common.Hash) *types.Header
-
- // GetTd retrieves the total difficulty from the database by hash and number.
- GetTd(hash common.Hash, number uint64) *big.Int
-}
-
-// ChainReader defines a small collection of methods needed to access the local
-// blockchain during header and/or uncle verification.
-type ChainReader interface {
- ChainHeaderReader
-
- // GetBlock retrieves a block from the database by hash and number.
- GetBlock(hash common.Hash, number uint64) *types.Block
-}
-
-// Engine is an algorithm agnostic consensus engine.
-type Engine interface {
- // Author retrieves the Ethereum address of the account that minted the given
- // block, which may be different from the header's coinbase if a consensus
- // engine is based on signatures.
- Author(header *types.Header) (common.Address, error)
-
- // VerifyHeader checks whether a header conforms to the consensus rules of a
- // given engine.
- VerifyHeader(chain ChainHeaderReader, header *types.Header) error
-
- // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
- // concurrently. 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).
- VerifyHeaders(chain ChainHeaderReader, headers []*types.Header) (chan<- struct{}, <-chan error)
-
- // VerifyUncles verifies that the given block's uncles conform to the consensus
- // rules of a given engine.
- VerifyUncles(chain ChainReader, block *types.Block) error
-
- // Prepare initializes the consensus fields of a block header according to the
- // rules of a particular engine. The changes are executed inline.
- Prepare(chain ChainHeaderReader, header *types.Header) error
-
- // Finalize runs any post-transaction state modifications (e.g. block rewards
- // or process withdrawals) but does not assemble the block.
- //
- // Note: The state database might be updated to reflect any consensus rules
- // that happen at finalization (e.g. block rewards).
- Finalize(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
- uncles []*types.Header, withdrawals []*types.Withdrawal)
-
- // FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
- // rewards or process withdrawals) and assembles the final block.
- //
- // Note: The block header and state database might be updated to reflect any
- // consensus rules that happen at finalization (e.g. block rewards).
- FinalizeAndAssemble(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
- uncles []*types.Header, receipts []*types.Receipt, withdrawals []*types.Withdrawal) (*types.Block, error)
-
- // Seal generates a new sealing request for the given input block and pushes
- // the result into the given channel.
- //
- // Note, the method returns immediately and will send the result async. More
- // than one result may also be returned depending on the consensus algorithm.
- Seal(chain ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error
-
- // SealHash returns the hash of a block prior to it being sealed.
- SealHash(header *types.Header) common.Hash
-
- // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
- // that a new block should have.
- CalcDifficulty(chain ChainHeaderReader, time uint64, parent *types.Header) *big.Int
-
- // APIs returns the RPC APIs this consensus engine provides.
- APIs(chain ChainHeaderReader) []rpc.API
-
- // Close terminates any background threads maintained by the consensus engine.
- Close() error
-}
-
-// PoW is a consensus engine based on proof-of-work.
-type PoW interface {
- Engine
-
- // Hashrate returns the current mining hashrate of a PoW consensus engine.
- Hashrate() float64
-}
diff --git a/consensus/errors.go b/consensus/errors.go
deleted file mode 100644
index d508b6580f..0000000000
--- a/consensus/errors.go
+++ /dev/null
@@ -1,41 +0,0 @@
-// 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 .
-
-package consensus
-
-import "errors"
-
-var (
- // ErrUnknownAncestor is returned when validating a block requires an ancestor
- // that is unknown.
- ErrUnknownAncestor = errors.New("unknown ancestor")
-
- // ErrPrunedAncestor is returned when validating a block requires an ancestor
- // that is known, but the state of which is not available.
- ErrPrunedAncestor = errors.New("pruned ancestor")
-
- // ErrFutureBlock is returned when a block's timestamp is in the future according
- // to the current node.
- ErrFutureBlock = errors.New("block in the future")
-
- // ErrInvalidNumber is returned if a block's number doesn't equal its parent's
- // plus one.
- ErrInvalidNumber = errors.New("invalid block number")
-
- // ErrInvalidTerminalBlock is returned if a block is invalid wrt. the terminal
- // total difficulty.
- ErrInvalidTerminalBlock = errors.New("invalid terminal block")
-)
diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go
deleted file mode 100644
index 130dfdf213..0000000000
--- a/consensus/ethash/consensus.go
+++ /dev/null
@@ -1,595 +0,0 @@
-// 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 .
-
-package ethash
-
-import (
- "errors"
- "fmt"
- "math/big"
- "time"
-
- mapset "github.com/deckarep/golang-set/v2"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/common/math"
- "github.com/ethereum/go-ethereum/consensus"
- "github.com/ethereum/go-ethereum/consensus/misc"
- "github.com/ethereum/go-ethereum/consensus/misc/eip1559"
- "github.com/ethereum/go-ethereum/core/state"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/params"
- "github.com/ethereum/go-ethereum/rlp"
- "github.com/ethereum/go-ethereum/trie"
- "golang.org/x/crypto/sha3"
-)
-
-// Ethash proof-of-work protocol constants.
-var (
- FrontierBlockReward = big.NewInt(5e+18) // Block reward in wei for successfully mining a block
- ByzantiumBlockReward = big.NewInt(3e+18) // Block reward in wei for successfully mining a block upward from Byzantium
- ConstantinopleBlockReward = big.NewInt(2e+18) // Block reward in wei for successfully mining a block upward from Constantinople
- maxUncles = 2 // Maximum number of uncles allowed in a single block
- allowedFutureBlockTimeSeconds = int64(15) // Max seconds from current time allowed for blocks, before they're considered future blocks
-
- // calcDifficultyEip5133 is the difficulty adjustment algorithm as specified by EIP 5133.
- // It offsets the bomb a total of 11.4M blocks.
- // Specification EIP-5133: https://eips.ethereum.org/EIPS/eip-5133
- calcDifficultyEip5133 = makeDifficultyCalculator(big.NewInt(11_400_000))
-
- // calcDifficultyEip4345 is the difficulty adjustment algorithm as specified by EIP 4345.
- // It offsets the bomb a total of 10.7M blocks.
- // Specification EIP-4345: https://eips.ethereum.org/EIPS/eip-4345
- calcDifficultyEip4345 = makeDifficultyCalculator(big.NewInt(10_700_000))
-
- // calcDifficultyEip3554 is the difficulty adjustment algorithm as specified by EIP 3554.
- // It offsets the bomb a total of 9.7M blocks.
- // Specification EIP-3554: https://eips.ethereum.org/EIPS/eip-3554
- calcDifficultyEip3554 = makeDifficultyCalculator(big.NewInt(9700000))
-
- // calcDifficultyEip2384 is the difficulty adjustment algorithm as specified by EIP 2384.
- // It offsets the bomb 4M blocks from Constantinople, so in total 9M blocks.
- // Specification EIP-2384: https://eips.ethereum.org/EIPS/eip-2384
- calcDifficultyEip2384 = makeDifficultyCalculator(big.NewInt(9000000))
-
- // calcDifficultyConstantinople is the difficulty adjustment algorithm for Constantinople.
- // It returns the difficulty that a new block should have when created at time given the
- // parent block's time and difficulty. The calculation uses the Byzantium rules, but with
- // bomb offset 5M.
- // Specification EIP-1234: https://eips.ethereum.org/EIPS/eip-1234
- calcDifficultyConstantinople = makeDifficultyCalculator(big.NewInt(5000000))
-
- // calcDifficultyByzantium is the difficulty adjustment algorithm. It returns
- // the difficulty that a new block should have when created at time given the
- // parent block's time and difficulty. The calculation uses the Byzantium rules.
- // Specification EIP-649: https://eips.ethereum.org/EIPS/eip-649
- calcDifficultyByzantium = makeDifficultyCalculator(big.NewInt(3000000))
-)
-
-// 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 (
- errOlderBlockTime = errors.New("timestamp older than parent")
- errTooManyUncles = errors.New("too many uncles")
- errDuplicateUncle = errors.New("duplicate uncle")
- errUncleIsAncestor = errors.New("uncle is ancestor")
- errDanglingUncle = errors.New("uncle's parent is not ancestor")
-)
-
-// Author implements consensus.Engine, returning the header's coinbase as the
-// proof-of-work verified author of the block.
-func (ethash *Ethash) Author(header *types.Header) (common.Address, error) {
- return header.Coinbase, nil
-}
-
-// VerifyHeader checks whether a header conforms to the consensus rules of the
-// stock Ethereum ethash engine.
-func (ethash *Ethash) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header) error {
- // Short circuit if the header is known, or its parent not
- number := header.Number.Uint64()
- if chain.GetHeader(header.Hash(), number) != nil {
- return nil
- }
- parent := chain.GetHeader(header.ParentHash, number-1)
- if parent == nil {
- return consensus.ErrUnknownAncestor
- }
- // Sanity checks passed, do a proper verification
- return ethash.verifyHeader(chain, header, parent, false, time.Now().Unix())
-}
-
-// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
-// concurrently. The method returns a quit channel to abort the operations and
-// a results channel to retrieve the async verifications.
-func (ethash *Ethash) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header) (chan<- struct{}, <-chan error) {
- // If we're running a full engine faking, accept any input as valid
- if ethash.fakeFull || len(headers) == 0 {
- abort, results := make(chan struct{}), make(chan error, len(headers))
- for i := 0; i < len(headers); i++ {
- results <- nil
- }
- return abort, results
- }
- abort := make(chan struct{})
- results := make(chan error, len(headers))
- unixNow := time.Now().Unix()
-
- go func() {
- for i, header := range headers {
- var parent *types.Header
- if i == 0 {
- parent = chain.GetHeader(headers[0].ParentHash, headers[0].Number.Uint64()-1)
- } else if headers[i-1].Hash() == headers[i].ParentHash {
- parent = headers[i-1]
- }
- var err error
- if parent == nil {
- err = consensus.ErrUnknownAncestor
- } else {
- err = ethash.verifyHeader(chain, header, parent, false, unixNow)
- }
- select {
- case <-abort:
- return
- case results <- err:
- }
- }
- }()
- return abort, results
-}
-
-// VerifyUncles verifies that the given block's uncles conform to the consensus
-// rules of the stock Ethereum ethash engine.
-func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
- // If we're running a full engine faking, accept any input as valid
- if ethash.fakeFull {
- return nil
- }
- // Verify that there are at most 2 uncles included in this block
- if len(block.Uncles()) > maxUncles {
- return errTooManyUncles
- }
- if len(block.Uncles()) == 0 {
- return nil
- }
- // Gather the set of past uncles and ancestors
- uncles, ancestors := mapset.NewSet[common.Hash](), make(map[common.Hash]*types.Header)
-
- number, parent := block.NumberU64()-1, block.ParentHash()
- for i := 0; i < 7; i++ {
- ancestorHeader := chain.GetHeader(parent, number)
- if ancestorHeader == nil {
- break
- }
- ancestors[parent] = ancestorHeader
- // If the ancestor doesn't have any uncles, we don't have to iterate them
- if ancestorHeader.UncleHash != types.EmptyUncleHash {
- // Need to add those uncles to the banned list too
- ancestor := chain.GetBlock(parent, number)
- if ancestor == nil {
- break
- }
- for _, uncle := range ancestor.Uncles() {
- uncles.Add(uncle.Hash())
- }
- }
- parent, number = ancestorHeader.ParentHash, number-1
- }
- ancestors[block.Hash()] = block.Header()
- uncles.Add(block.Hash())
-
- // Verify each of the uncles that it's recent, but not an ancestor
- for _, uncle := range block.Uncles() {
- // Make sure every uncle is rewarded only once
- hash := uncle.Hash()
- if uncles.Contains(hash) {
- return errDuplicateUncle
- }
- uncles.Add(hash)
-
- // Make sure the uncle has a valid ancestry
- if ancestors[hash] != nil {
- return errUncleIsAncestor
- }
- if ancestors[uncle.ParentHash] == nil || uncle.ParentHash == block.ParentHash() {
- return errDanglingUncle
- }
- if err := ethash.verifyHeader(chain, uncle, ancestors[uncle.ParentHash], true, time.Now().Unix()); err != nil {
- return err
- }
- }
- return nil
-}
-
-// verifyHeader checks whether a header conforms to the consensus rules of the
-// stock Ethereum ethash engine.
-// See YP section 4.3.4. "Block Header Validity"
-func (ethash *Ethash) verifyHeader(chain consensus.ChainHeaderReader, header, parent *types.Header, uncle bool, unixNow int64) error {
- // Ensure that the header's extra-data section is of a reasonable size
- if uint64(len(header.Extra)) > params.MaximumExtraDataSize {
- return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), params.MaximumExtraDataSize)
- }
- // Verify the header's timestamp
- if !uncle {
- if header.Time > uint64(unixNow+allowedFutureBlockTimeSeconds) {
- return consensus.ErrFutureBlock
- }
- }
- if header.Time <= parent.Time {
- return errOlderBlockTime
- }
- // Verify the block's difficulty based on its timestamp and parent's difficulty
- expected := ethash.CalcDifficulty(chain, header.Time, parent)
-
- if expected.Cmp(header.Difficulty) != 0 {
- return fmt.Errorf("invalid difficulty: have %v, want %v", header.Difficulty, expected)
- }
- // Verify that the gas limit is <= 2^63-1
- if header.GasLimit > params.MaxGasLimit {
- return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, params.MaxGasLimit)
- }
- // Verify that the gasUsed is <= gasLimit
- if header.GasUsed > header.GasLimit {
- return fmt.Errorf("invalid gasUsed: have %d, gasLimit %d", header.GasUsed, header.GasLimit)
- }
- // Verify the block's gas usage and (if applicable) verify the base fee.
- if !chain.Config().IsLondon(header.Number) {
- // Verify BaseFee not present before EIP-1559 fork.
- if header.BaseFee != nil {
- return fmt.Errorf("invalid baseFee before fork: have %d, expected 'nil'", header.BaseFee)
- }
- if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil {
- return err
- }
- } else if err := eip1559.VerifyEIP1559Header(chain.Config(), parent, header); err != nil {
- // Verify the header's EIP-1559 attributes.
- return err
- }
- // Verify that the block number is parent's +1
- if diff := new(big.Int).Sub(header.Number, parent.Number); diff.Cmp(big.NewInt(1)) != 0 {
- return consensus.ErrInvalidNumber
- }
- if chain.Config().IsShanghai(header.Number, header.Time) {
- return errors.New("ethash does not support shanghai fork")
- }
- // Verify the non-existence of withdrawalsHash.
- if header.WithdrawalsHash != nil {
- return fmt.Errorf("invalid withdrawalsHash: have %x, expected nil", header.WithdrawalsHash)
- }
- if chain.Config().IsCancun(header.Number, header.Time) {
- return errors.New("ethash does not support cancun fork")
- }
- // Verify the non-existence of cancun-specific header fields
- switch {
- case header.ExcessBlobGas != nil:
- return fmt.Errorf("invalid excessBlobGas: have %d, expected nil", header.ExcessBlobGas)
- case header.BlobGasUsed != nil:
- return fmt.Errorf("invalid blobGasUsed: have %d, expected nil", header.BlobGasUsed)
- case header.ParentBeaconRoot != nil:
- return fmt.Errorf("invalid parentBeaconRoot, have %#x, expected nil", header.ParentBeaconRoot)
- }
- // Add some fake checks for tests
- if ethash.fakeDelay != nil {
- time.Sleep(*ethash.fakeDelay)
- }
- if ethash.fakeFail != nil && *ethash.fakeFail == header.Number.Uint64() {
- return errors.New("invalid tester pow")
- }
- // If all checks passed, validate any special fields for hard forks
- if err := misc.VerifyDAOHeaderExtraData(chain.Config(), header); err != nil {
- return err
- }
- return nil
-}
-
-// CalcDifficulty is the difficulty adjustment algorithm. It returns
-// the difficulty that a new block should have when created at time
-// given the parent block's time and difficulty.
-func (ethash *Ethash) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
- return CalcDifficulty(chain.Config(), time, parent)
-}
-
-// CalcDifficulty is the difficulty adjustment algorithm. It returns
-// the difficulty that a new block should have when created at time
-// given the parent block's time and difficulty.
-func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Header) *big.Int {
- next := new(big.Int).Add(parent.Number, big1)
- switch {
- case config.IsGrayGlacier(next):
- return calcDifficultyEip5133(time, parent)
- case config.IsArrowGlacier(next):
- return calcDifficultyEip4345(time, parent)
- case config.IsLondon(next):
- return calcDifficultyEip3554(time, parent)
- case config.IsMuirGlacier(next):
- return calcDifficultyEip2384(time, parent)
- case config.IsConstantinople(next):
- return calcDifficultyConstantinople(time, parent)
- case config.IsByzantium(next):
- return calcDifficultyByzantium(time, parent)
- case config.IsHomestead(next):
- return calcDifficultyHomestead(time, parent)
- default:
- return calcDifficultyFrontier(time, parent)
- }
-}
-
-// Some weird constants to avoid constant memory allocs for them.
-var (
- expDiffPeriod = big.NewInt(100000)
- big1 = big.NewInt(1)
- big2 = big.NewInt(2)
- big9 = big.NewInt(9)
- big10 = big.NewInt(10)
- bigMinus99 = big.NewInt(-99)
-)
-
-// makeDifficultyCalculator creates a difficultyCalculator with the given bomb-delay.
-// the difficulty is calculated with Byzantium rules, which differs from Homestead in
-// how uncles affect the calculation
-func makeDifficultyCalculator(bombDelay *big.Int) func(time uint64, parent *types.Header) *big.Int {
- // Note, the calculations below looks at the parent number, which is 1 below
- // the block number. Thus we remove one from the delay given
- bombDelayFromParent := new(big.Int).Sub(bombDelay, big1)
- return func(time uint64, parent *types.Header) *big.Int {
- // https://github.com/ethereum/EIPs/issues/100.
- // algorithm:
- // diff = (parent_diff +
- // (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
- // ) + 2^(periodCount - 2)
-
- bigTime := new(big.Int).SetUint64(time)
- bigParentTime := new(big.Int).SetUint64(parent.Time)
-
- // holds intermediate values to make the algo easier to read & audit
- x := new(big.Int)
- y := new(big.Int)
-
- // (2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9
- x.Sub(bigTime, bigParentTime)
- x.Div(x, big9)
- if parent.UncleHash == types.EmptyUncleHash {
- x.Sub(big1, x)
- } else {
- x.Sub(big2, x)
- }
- // max((2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9, -99)
- if x.Cmp(bigMinus99) < 0 {
- x.Set(bigMinus99)
- }
- // parent_diff + (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
- y.Div(parent.Difficulty, params.DifficultyBoundDivisor)
- x.Mul(y, x)
- x.Add(parent.Difficulty, x)
-
- // minimum difficulty can ever be (before exponential factor)
- if x.Cmp(params.MinimumDifficulty) < 0 {
- x.Set(params.MinimumDifficulty)
- }
- // calculate a fake block number for the ice-age delay
- // Specification: https://eips.ethereum.org/EIPS/eip-1234
- fakeBlockNumber := new(big.Int)
- if parent.Number.Cmp(bombDelayFromParent) >= 0 {
- fakeBlockNumber = fakeBlockNumber.Sub(parent.Number, bombDelayFromParent)
- }
- // for the exponential factor
- periodCount := fakeBlockNumber
- periodCount.Div(periodCount, expDiffPeriod)
-
- // the exponential factor, commonly referred to as "the bomb"
- // diff = diff + 2^(periodCount - 2)
- if periodCount.Cmp(big1) > 0 {
- y.Sub(periodCount, big2)
- y.Exp(big2, y, nil)
- x.Add(x, y)
- }
- return x
- }
-}
-
-// calcDifficultyHomestead is the difficulty adjustment algorithm. It returns
-// the difficulty that a new block should have when created at time given the
-// parent block's time and difficulty. The calculation uses the Homestead rules.
-func calcDifficultyHomestead(time uint64, parent *types.Header) *big.Int {
- // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.md
- // algorithm:
- // diff = (parent_diff +
- // (parent_diff / 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
- // ) + 2^(periodCount - 2)
-
- bigTime := new(big.Int).SetUint64(time)
- bigParentTime := new(big.Int).SetUint64(parent.Time)
-
- // holds intermediate values to make the algo easier to read & audit
- x := new(big.Int)
- y := new(big.Int)
-
- // 1 - (block_timestamp - parent_timestamp) // 10
- x.Sub(bigTime, bigParentTime)
- x.Div(x, big10)
- x.Sub(big1, x)
-
- // max(1 - (block_timestamp - parent_timestamp) // 10, -99)
- if x.Cmp(bigMinus99) < 0 {
- x.Set(bigMinus99)
- }
- // (parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
- y.Div(parent.Difficulty, params.DifficultyBoundDivisor)
- x.Mul(y, x)
- x.Add(parent.Difficulty, x)
-
- // minimum difficulty can ever be (before exponential factor)
- if x.Cmp(params.MinimumDifficulty) < 0 {
- x.Set(params.MinimumDifficulty)
- }
- // for the exponential factor
- periodCount := new(big.Int).Add(parent.Number, big1)
- periodCount.Div(periodCount, expDiffPeriod)
-
- // the exponential factor, commonly referred to as "the bomb"
- // diff = diff + 2^(periodCount - 2)
- if periodCount.Cmp(big1) > 0 {
- y.Sub(periodCount, big2)
- y.Exp(big2, y, nil)
- x.Add(x, y)
- }
- return x
-}
-
-// calcDifficultyFrontier is the difficulty adjustment algorithm. It returns the
-// difficulty that a new block should have when created at time given the parent
-// block's time and difficulty. The calculation uses the Frontier rules.
-func calcDifficultyFrontier(time uint64, parent *types.Header) *big.Int {
- diff := new(big.Int)
- adjust := new(big.Int).Div(parent.Difficulty, params.DifficultyBoundDivisor)
- bigTime := new(big.Int)
- bigParentTime := new(big.Int)
-
- bigTime.SetUint64(time)
- bigParentTime.SetUint64(parent.Time)
-
- if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 {
- diff.Add(parent.Difficulty, adjust)
- } else {
- diff.Sub(parent.Difficulty, adjust)
- }
- if diff.Cmp(params.MinimumDifficulty) < 0 {
- diff.Set(params.MinimumDifficulty)
- }
-
- periodCount := new(big.Int).Add(parent.Number, big1)
- periodCount.Div(periodCount, expDiffPeriod)
- if periodCount.Cmp(big1) > 0 {
- // diff = diff + 2^(periodCount - 2)
- expDiff := periodCount.Sub(periodCount, big2)
- expDiff.Exp(big2, expDiff, nil)
- diff.Add(diff, expDiff)
- diff = math.BigMax(diff, params.MinimumDifficulty)
- }
- return diff
-}
-
-// Exported for fuzzing
-var FrontierDifficultyCalculator = calcDifficultyFrontier
-var HomesteadDifficultyCalculator = calcDifficultyHomestead
-var DynamicDifficultyCalculator = makeDifficultyCalculator
-
-// Prepare implements consensus.Engine, initializing the difficulty field of a
-// header to conform to the ethash protocol. The changes are done inline.
-func (ethash *Ethash) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {
- parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
- if parent == nil {
- return consensus.ErrUnknownAncestor
- }
- header.Difficulty = ethash.CalcDifficulty(chain, header.Time, parent)
- return nil
-}
-
-// Finalize implements consensus.Engine, accumulating the block and uncle rewards.
-func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, withdrawals []*types.Withdrawal) {
- // Accumulate any block and uncle rewards
- accumulateRewards(chain.Config(), state, header, uncles)
-}
-
-// FinalizeAndAssemble implements consensus.Engine, accumulating the block and
-// uncle rewards, setting the final state and assembling the block.
-func (ethash *Ethash) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt, withdrawals []*types.Withdrawal) (*types.Block, error) {
- if len(withdrawals) > 0 {
- return nil, errors.New("ethash does not support withdrawals")
- }
- // Finalize block
- ethash.Finalize(chain, header, state, txs, uncles, nil)
-
- // Assign the final state root to header.
- header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
-
- // Header seems complete, assemble into a block and return
- return types.NewBlock(header, txs, uncles, receipts, trie.NewStackTrie(nil)), nil
-}
-
-// SealHash returns the hash of a block prior to it being sealed.
-func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
- hasher := sha3.NewLegacyKeccak256()
-
- 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,
- }
- if header.BaseFee != nil {
- enc = append(enc, header.BaseFee)
- }
- if header.WithdrawalsHash != nil {
- panic("withdrawal hash set on ethash")
- }
- if header.ExcessBlobGas != nil {
- panic("excess blob gas set on ethash")
- }
- if header.BlobGasUsed != nil {
- panic("blob gas used set on ethash")
- }
- if header.ParentBeaconRoot != nil {
- panic("parent beacon root set on ethash")
- }
- rlp.Encode(hasher, enc)
- hasher.Sum(hash[:0])
- return hash
-}
-
-// Some weird constants to avoid constant memory allocs for them.
-var (
- big8 = big.NewInt(8)
- big32 = big.NewInt(32)
-)
-
-// AccumulateRewards credits the coinbase of the given block with the mining
-// reward. The total reward consists of the static block reward and rewards for
-// included uncles. The coinbase of each uncle block is also rewarded.
-func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
- // Select the correct block reward based on chain progression
- blockReward := FrontierBlockReward
- if config.IsByzantium(header.Number) {
- blockReward = ByzantiumBlockReward
- }
- if config.IsConstantinople(header.Number) {
- blockReward = ConstantinopleBlockReward
- }
- // Accumulate the rewards for the miner and any included uncles
- reward := new(big.Int).Set(blockReward)
- r := new(big.Int)
- for _, uncle := range uncles {
- r.Add(uncle.Number, big8)
- r.Sub(r, header.Number)
- r.Mul(r, blockReward)
- r.Div(r, big8)
- state.AddBalance(uncle.Coinbase, r)
-
- r.Div(blockReward, big32)
- reward.Add(reward, r)
- }
- state.AddBalance(header.Coinbase, reward)
-}
diff --git a/consensus/ethash/consensus_test.go b/consensus/ethash/consensus_test.go
deleted file mode 100644
index e3793cd1b0..0000000000
--- a/consensus/ethash/consensus_test.go
+++ /dev/null
@@ -1,188 +0,0 @@
-// 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 .
-
-package ethash
-
-import (
- crand "crypto/rand"
- "encoding/binary"
- "encoding/json"
- "math/big"
- "math/rand"
- "os"
- "path/filepath"
- "testing"
-
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/common/math"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/params"
-)
-
-type diffTest struct {
- ParentTimestamp uint64
- ParentDifficulty *big.Int
- CurrentTimestamp uint64
- CurrentBlocknumber *big.Int
- CurrentDifficulty *big.Int
-}
-
-func (d *diffTest) UnmarshalJSON(b []byte) (err error) {
- var ext struct {
- ParentTimestamp string
- ParentDifficulty string
- CurrentTimestamp string
- CurrentBlocknumber string
- CurrentDifficulty string
- }
- if err := json.Unmarshal(b, &ext); err != nil {
- return err
- }
-
- d.ParentTimestamp = math.MustParseUint64(ext.ParentTimestamp)
- d.ParentDifficulty = math.MustParseBig256(ext.ParentDifficulty)
- d.CurrentTimestamp = math.MustParseUint64(ext.CurrentTimestamp)
- d.CurrentBlocknumber = math.MustParseBig256(ext.CurrentBlocknumber)
- d.CurrentDifficulty = math.MustParseBig256(ext.CurrentDifficulty)
-
- return nil
-}
-
-func TestCalcDifficulty(t *testing.T) {
- file, err := os.Open(filepath.Join("..", "..", "tests", "testdata", "BasicTests", "difficulty.json"))
- if err != nil {
- t.Skip(err)
- }
- defer file.Close()
-
- tests := make(map[string]diffTest)
- err = json.NewDecoder(file).Decode(&tests)
- if err != nil {
- t.Fatal(err)
- }
-
- config := ¶ms.ChainConfig{HomesteadBlock: big.NewInt(1150000)}
-
- for name, test := range tests {
- number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1))
- diff := CalcDifficulty(config, test.CurrentTimestamp, &types.Header{
- Number: number,
- Time: test.ParentTimestamp,
- Difficulty: test.ParentDifficulty,
- })
- if diff.Cmp(test.CurrentDifficulty) != 0 {
- t.Error(name, "failed. Expected", test.CurrentDifficulty, "and calculated", diff)
- }
- }
-}
-
-func randSlice(min, max uint32) []byte {
- var b = make([]byte, 4)
- crand.Read(b)
- a := binary.LittleEndian.Uint32(b)
- size := min + a%(max-min)
- out := make([]byte, size)
- crand.Read(out)
- return out
-}
-
-func TestDifficultyCalculators(t *testing.T) {
- for i := 0; i < 5000; i++ {
- // 1 to 300 seconds diff
- var timeDelta = uint64(1 + rand.Uint32()%3000)
- diffBig := new(big.Int).SetBytes(randSlice(2, 10))
- if diffBig.Cmp(params.MinimumDifficulty) < 0 {
- diffBig.Set(params.MinimumDifficulty)
- }
- //rand.Read(difficulty)
- header := &types.Header{
- Difficulty: diffBig,
- Number: new(big.Int).SetUint64(rand.Uint64() % 50_000_000),
- Time: rand.Uint64() - timeDelta,
- }
- if rand.Uint32()&1 == 0 {
- header.UncleHash = types.EmptyUncleHash
- }
- bombDelay := new(big.Int).SetUint64(rand.Uint64() % 50_000_000)
- for i, pair := range []struct {
- bigFn func(time uint64, parent *types.Header) *big.Int
- u256Fn func(time uint64, parent *types.Header) *big.Int
- }{
- {FrontierDifficultyCalculator, CalcDifficultyFrontierU256},
- {HomesteadDifficultyCalculator, CalcDifficultyHomesteadU256},
- {DynamicDifficultyCalculator(bombDelay), MakeDifficultyCalculatorU256(bombDelay)},
- } {
- time := header.Time + timeDelta
- want := pair.bigFn(time, header)
- have := pair.u256Fn(time, header)
- if want.BitLen() > 256 {
- continue
- }
- if want.Cmp(have) != 0 {
- t.Fatalf("pair %d: want %x have %x\nparent.Number: %x\np.Time: %x\nc.Time: %x\nBombdelay: %v\n", i, want, have,
- header.Number, header.Time, time, bombDelay)
- }
- }
- }
-}
-
-func BenchmarkDifficultyCalculator(b *testing.B) {
- x1 := makeDifficultyCalculator(big.NewInt(1000000))
- x2 := MakeDifficultyCalculatorU256(big.NewInt(1000000))
- h := &types.Header{
- ParentHash: common.Hash{},
- UncleHash: types.EmptyUncleHash,
- Difficulty: big.NewInt(0xffffff),
- Number: big.NewInt(500000),
- Time: 1000000,
- }
- b.Run("big-frontier", func(b *testing.B) {
- b.ReportAllocs()
- for i := 0; i < b.N; i++ {
- calcDifficultyFrontier(1000014, h)
- }
- })
- b.Run("u256-frontier", func(b *testing.B) {
- b.ReportAllocs()
- for i := 0; i < b.N; i++ {
- CalcDifficultyFrontierU256(1000014, h)
- }
- })
- b.Run("big-homestead", func(b *testing.B) {
- b.ReportAllocs()
- for i := 0; i < b.N; i++ {
- calcDifficultyHomestead(1000014, h)
- }
- })
- b.Run("u256-homestead", func(b *testing.B) {
- b.ReportAllocs()
- for i := 0; i < b.N; i++ {
- CalcDifficultyHomesteadU256(1000014, h)
- }
- })
- b.Run("big-generic", func(b *testing.B) {
- b.ReportAllocs()
- for i := 0; i < b.N; i++ {
- x1(1000014, h)
- }
- })
- b.Run("u256-generic", func(b *testing.B) {
- b.ReportAllocs()
- for i := 0; i < b.N; i++ {
- x2(1000014, h)
- }
- })
-}
diff --git a/consensus/ethash/difficulty.go b/consensus/ethash/difficulty.go
deleted file mode 100644
index 66a18059c6..0000000000
--- a/consensus/ethash/difficulty.go
+++ /dev/null
@@ -1,191 +0,0 @@
-// Copyright 2020 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 .
-
-package ethash
-
-import (
- "math/big"
-
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/holiman/uint256"
-)
-
-const (
- // frontierDurationLimit is for Frontier:
- // The decision boundary on the blocktime duration used to determine
- // whether difficulty should go up or down.
- frontierDurationLimit = 13
- // minimumDifficulty The minimum that the difficulty may ever be.
- minimumDifficulty = 131072
- // expDiffPeriod is the exponential difficulty period
- expDiffPeriodUint = 100000
- // difficultyBoundDivisorBitShift is the bound divisor of the difficulty (2048),
- // This constant is the right-shifts to use for the division.
- difficultyBoundDivisor = 11
-)
-
-// CalcDifficultyFrontierU256 is the difficulty adjustment algorithm. It returns the
-// difficulty that a new block should have when created at time given the parent
-// block's time and difficulty. The calculation uses the Frontier rules.
-func CalcDifficultyFrontierU256(time uint64, parent *types.Header) *big.Int {
- /*
- Algorithm
- block_diff = pdiff + pdiff / 2048 * (1 if time - ptime < 13 else -1) + int(2^((num // 100000) - 2))
-
- Where:
- - pdiff = parent.difficulty
- - ptime = parent.time
- - time = block.timestamp
- - num = block.number
- */
-
- pDiff, _ := uint256.FromBig(parent.Difficulty) // pDiff: pdiff
- adjust := pDiff.Clone()
- adjust.Rsh(adjust, difficultyBoundDivisor) // adjust: pDiff / 2048
-
- if time-parent.Time < frontierDurationLimit {
- pDiff.Add(pDiff, adjust)
- } else {
- pDiff.Sub(pDiff, adjust)
- }
- if pDiff.LtUint64(minimumDifficulty) {
- pDiff.SetUint64(minimumDifficulty)
- }
- // 'pdiff' now contains:
- // pdiff + pdiff / 2048 * (1 if time - ptime < 13 else -1)
-
- if periodCount := (parent.Number.Uint64() + 1) / expDiffPeriodUint; periodCount > 1 {
- // diff = diff + 2^(periodCount - 2)
- expDiff := adjust.SetOne()
- expDiff.Lsh(expDiff, uint(periodCount-2)) // expdiff: 2 ^ (periodCount -2)
- pDiff.Add(pDiff, expDiff)
- }
- return pDiff.ToBig()
-}
-
-// CalcDifficultyHomesteadU256 is the difficulty adjustment algorithm. It returns
-// the difficulty that a new block should have when created at time given the
-// parent block's time and difficulty. The calculation uses the Homestead rules.
-func CalcDifficultyHomesteadU256(time uint64, parent *types.Header) *big.Int {
- /*
- https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.md
- Algorithm:
- block_diff = pdiff + pdiff / 2048 * max(1 - (time - ptime) / 10, -99) + 2 ^ int((num / 100000) - 2))
-
- Our modification, to use unsigned ints:
- block_diff = pdiff - pdiff / 2048 * max((time - ptime) / 10 - 1, 99) + 2 ^ int((num / 100000) - 2))
-
- Where:
- - pdiff = parent.difficulty
- - ptime = parent.time
- - time = block.timestamp
- - num = block.number
- */
-
- pDiff, _ := uint256.FromBig(parent.Difficulty) // pDiff: pdiff
- adjust := pDiff.Clone()
- adjust.Rsh(adjust, difficultyBoundDivisor) // adjust: pDiff / 2048
-
- x := (time - parent.Time) / 10 // (time - ptime) / 10)
- var neg = true
- if x == 0 {
- x = 1
- neg = false
- } else if x >= 100 {
- x = 99
- } else {
- x = x - 1
- }
- z := new(uint256.Int).SetUint64(x)
- adjust.Mul(adjust, z) // adjust: (pdiff / 2048) * max((time - ptime) / 10 - 1, 99)
- if neg {
- pDiff.Sub(pDiff, adjust) // pdiff - pdiff / 2048 * max((time - ptime) / 10 - 1, 99)
- } else {
- pDiff.Add(pDiff, adjust) // pdiff + pdiff / 2048 * max((time - ptime) / 10 - 1, 99)
- }
- if pDiff.LtUint64(minimumDifficulty) {
- pDiff.SetUint64(minimumDifficulty)
- }
- // for the exponential factor, a.k.a "the bomb"
- // diff = diff + 2^(periodCount - 2)
- if periodCount := (1 + parent.Number.Uint64()) / expDiffPeriodUint; periodCount > 1 {
- expFactor := adjust.Lsh(adjust.SetOne(), uint(periodCount-2))
- pDiff.Add(pDiff, expFactor)
- }
- return pDiff.ToBig()
-}
-
-// MakeDifficultyCalculatorU256 creates a difficultyCalculator with the given bomb-delay.
-// the difficulty is calculated with Byzantium rules, which differs from Homestead in
-// how uncles affect the calculation
-func MakeDifficultyCalculatorU256(bombDelay *big.Int) func(time uint64, parent *types.Header) *big.Int {
- // Note, the calculations below looks at the parent number, which is 1 below
- // the block number. Thus we remove one from the delay given
- bombDelayFromParent := bombDelay.Uint64() - 1
- return func(time uint64, parent *types.Header) *big.Int {
- /*
- https://github.com/ethereum/EIPs/issues/100
- pDiff = parent.difficulty
- BLOCK_DIFF_FACTOR = 9
- a = pDiff + (pDiff // BLOCK_DIFF_FACTOR) * adj_factor
- b = min(parent.difficulty, MIN_DIFF)
- child_diff = max(a,b )
- */
- x := (time - parent.Time) / 9 // (block_timestamp - parent_timestamp) // 9
- c := uint64(1) // if parent.unclehash == emptyUncleHashHash
- if parent.UncleHash != types.EmptyUncleHash {
- c = 2
- }
- xNeg := x >= c
- if xNeg {
- // x is now _negative_ adjustment factor
- x = x - c // - ( (t-p)/p -( 2 or 1) )
- } else {
- x = c - x // (2 or 1) - (t-p)/9
- }
- if x > 99 {
- x = 99 // max(x, 99)
- }
- // parent_diff + (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
- y := new(uint256.Int)
- y.SetFromBig(parent.Difficulty) // y: p_diff
- pDiff := y.Clone() // pdiff: p_diff
- z := new(uint256.Int).SetUint64(x) //z : +-adj_factor (either pos or negative)
- y.Rsh(y, difficultyBoundDivisor) // y: p__diff / 2048
- z.Mul(y, z) // z: (p_diff / 2048 ) * (+- adj_factor)
-
- if xNeg {
- y.Sub(pDiff, z) // y: parent_diff + parent_diff/2048 * adjustment_factor
- } else {
- y.Add(pDiff, z) // y: parent_diff + parent_diff/2048 * adjustment_factor
- }
- // minimum difficulty can ever be (before exponential factor)
- if y.LtUint64(minimumDifficulty) {
- y.SetUint64(minimumDifficulty)
- }
- // calculate a fake block number for the ice-age delay
- // Specification: https://eips.ethereum.org/EIPS/eip-1234
- var pNum = parent.Number.Uint64()
- if pNum >= bombDelayFromParent {
- if fakeBlockNumber := pNum - bombDelayFromParent; fakeBlockNumber >= 2*expDiffPeriodUint {
- z.SetOne()
- z.Lsh(z, uint(fakeBlockNumber/expDiffPeriodUint-2))
- y.Add(z, y)
- }
- }
- return y.ToBig()
- }
-}
diff --git a/consensus/ethash/ethash.go b/consensus/ethash/ethash.go
deleted file mode 100644
index f37ec26056..0000000000
--- a/consensus/ethash/ethash.go
+++ /dev/null
@@ -1,85 +0,0 @@
-// 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 .
-
-// Package ethash implements the ethash proof-of-work consensus engine.
-package ethash
-
-import (
- "time"
-
- "github.com/ethereum/go-ethereum/consensus"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/rpc"
-)
-
-// Ethash is a consensus engine based on proof-of-work implementing the ethash
-// algorithm.
-type Ethash struct {
- fakeFail *uint64 // Block number which fails PoW check even in fake mode
- fakeDelay *time.Duration // Time delay to sleep for before returning from verify
- fakeFull bool // Accepts everything as valid
-}
-
-// NewFaker creates an ethash consensus engine with a fake PoW scheme that accepts
-// all blocks' seal as valid, though they still have to conform to the Ethereum
-// consensus rules.
-func NewFaker() *Ethash {
- return new(Ethash)
-}
-
-// NewFakeFailer creates a ethash consensus engine with a fake PoW scheme that
-// accepts all blocks as valid apart from the single one specified, though they
-// still have to conform to the Ethereum consensus rules.
-func NewFakeFailer(fail uint64) *Ethash {
- return &Ethash{
- fakeFail: &fail,
- }
-}
-
-// NewFakeDelayer creates a ethash consensus engine with a fake PoW scheme that
-// accepts all blocks as valid, but delays verifications by some time, though
-// they still have to conform to the Ethereum consensus rules.
-func NewFakeDelayer(delay time.Duration) *Ethash {
- return &Ethash{
- fakeDelay: &delay,
- }
-}
-
-// NewFullFaker creates an ethash consensus engine with a full fake scheme that
-// accepts all blocks as valid, without checking any consensus rules whatsoever.
-func NewFullFaker() *Ethash {
- return &Ethash{
- fakeFull: true,
- }
-}
-
-// Close closes the exit channel to notify all backend threads exiting.
-func (ethash *Ethash) Close() error {
- return nil
-}
-
-// APIs implements consensus.Engine, returning no APIs as ethash is an empty
-// shell in the post-merge world.
-func (ethash *Ethash) APIs(chain consensus.ChainHeaderReader) []rpc.API {
- return []rpc.API{}
-}
-
-// Seal generates a new sealing request for the given input block and pushes
-// the result into the given channel. For the ethash engine, this method will
-// just panic as sealing is not supported anymore.
-func (ethash *Ethash) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
- panic("ethash (pow) sealing not supported any more")
-}
diff --git a/consensus/merger.go b/consensus/merger.go
deleted file mode 100644
index ffbcbf2b85..0000000000
--- a/consensus/merger.go
+++ /dev/null
@@ -1,110 +0,0 @@
-// Copyright 2021 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 .
-
-package consensus
-
-import (
- "fmt"
- "sync"
-
- "github.com/ethereum/go-ethereum/core/rawdb"
- "github.com/ethereum/go-ethereum/ethdb"
- "github.com/ethereum/go-ethereum/log"
- "github.com/ethereum/go-ethereum/rlp"
-)
-
-// transitionStatus describes the status of eth1/2 transition. This switch
-// between modes is a one-way action which is triggered by corresponding
-// consensus-layer message.
-type transitionStatus struct {
- LeftPoW bool // The flag is set when the first NewHead message received
- EnteredPoS bool // The flag is set when the first FinalisedBlock message received
-}
-
-// Merger is an internal help structure used to track the eth1/2 transition status.
-// It's a common structure can be used in both full node and light client.
-type Merger struct {
- db ethdb.KeyValueStore
- status transitionStatus
- mu sync.RWMutex
-}
-
-// NewMerger creates a new Merger which stores its transition status in the provided db.
-func NewMerger(db ethdb.KeyValueStore) *Merger {
- var status transitionStatus
- blob := rawdb.ReadTransitionStatus(db)
- if len(blob) != 0 {
- if err := rlp.DecodeBytes(blob, &status); err != nil {
- log.Crit("Failed to decode the transition status", "err", err)
- }
- }
- return &Merger{
- db: db,
- status: status,
- }
-}
-
-// ReachTTD is called whenever the first NewHead message received
-// from the consensus-layer.
-func (m *Merger) ReachTTD() {
- m.mu.Lock()
- defer m.mu.Unlock()
-
- if m.status.LeftPoW {
- return
- }
- m.status = transitionStatus{LeftPoW: true}
- blob, err := rlp.EncodeToBytes(m.status)
- if err != nil {
- panic(fmt.Sprintf("Failed to encode the transition status: %v", err))
- }
- rawdb.WriteTransitionStatus(m.db, blob)
- log.Info("Left PoW stage")
-}
-
-// FinalizePoS is called whenever the first FinalisedBlock message received
-// from the consensus-layer.
-func (m *Merger) FinalizePoS() {
- m.mu.Lock()
- defer m.mu.Unlock()
-
- if m.status.EnteredPoS {
- return
- }
- m.status = transitionStatus{LeftPoW: true, EnteredPoS: true}
- blob, err := rlp.EncodeToBytes(m.status)
- if err != nil {
- panic(fmt.Sprintf("Failed to encode the transition status: %v", err))
- }
- rawdb.WriteTransitionStatus(m.db, blob)
- log.Info("Entered PoS stage")
-}
-
-// TDDReached reports whether the chain has left the PoW stage.
-func (m *Merger) TDDReached() bool {
- m.mu.RLock()
- defer m.mu.RUnlock()
-
- return m.status.LeftPoW
-}
-
-// PoSFinalized reports whether the chain has entered the PoS stage.
-func (m *Merger) PoSFinalized() bool {
- m.mu.RLock()
- defer m.mu.RUnlock()
-
- return m.status.EnteredPoS
-}
diff --git a/consensus/misc/dao.go b/consensus/misc/dao.go
deleted file mode 100644
index 96995616de..0000000000
--- a/consensus/misc/dao.go
+++ /dev/null
@@ -1,86 +0,0 @@
-// Copyright 2016 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 .
-
-package misc
-
-import (
- "bytes"
- "errors"
- "math/big"
-
- "github.com/ethereum/go-ethereum/core/state"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/params"
-)
-
-var (
- // ErrBadProDAOExtra is returned if a header doesn't support the DAO fork on a
- // pro-fork client.
- ErrBadProDAOExtra = errors.New("bad DAO pro-fork extra-data")
-
- // ErrBadNoDAOExtra is returned if a header does support the DAO fork on a no-
- // fork client.
- ErrBadNoDAOExtra = errors.New("bad DAO no-fork extra-data")
-)
-
-// VerifyDAOHeaderExtraData validates the extra-data field of a block header to
-// ensure it conforms to DAO hard-fork rules.
-//
-// DAO hard-fork extension to the header validity:
-//
-// - if the node is no-fork, do not accept blocks in the [fork, fork+10) range
-// with the fork specific extra-data set.
-// - if the node is pro-fork, require blocks in the specific range to have the
-// unique extra-data set.
-func VerifyDAOHeaderExtraData(config *params.ChainConfig, header *types.Header) error {
- // Short circuit validation if the node doesn't care about the DAO fork
- if config.DAOForkBlock == nil {
- return nil
- }
- // Make sure the block is within the fork's modified extra-data range
- limit := new(big.Int).Add(config.DAOForkBlock, params.DAOForkExtraRange)
- if header.Number.Cmp(config.DAOForkBlock) < 0 || header.Number.Cmp(limit) >= 0 {
- return nil
- }
- // Depending on whether we support or oppose the fork, validate the extra-data contents
- if config.DAOForkSupport {
- if !bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
- return ErrBadProDAOExtra
- }
- } else {
- if bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
- return ErrBadNoDAOExtra
- }
- }
- // All ok, header has the same extra-data we expect
- return nil
-}
-
-// ApplyDAOHardFork modifies the state database according to the DAO hard-fork
-// rules, transferring all balances of a set of DAO accounts to a single refund
-// contract.
-func ApplyDAOHardFork(statedb *state.StateDB) {
- // Retrieve the contract to refund balances into
- if !statedb.Exist(params.DAORefundContract) {
- statedb.CreateAccount(params.DAORefundContract)
- }
-
- // Move every DAO account and extra-balance account funds into the refund contract
- for _, addr := range params.DAODrainList() {
- statedb.AddBalance(params.DAORefundContract, statedb.GetBalance(addr))
- statedb.SetBalance(addr, new(big.Int))
- }
-}
diff --git a/consensus/misc/eip1559/eip1559.go b/consensus/misc/eip1559/eip1559.go
deleted file mode 100644
index 84b82c4c49..0000000000
--- a/consensus/misc/eip1559/eip1559.go
+++ /dev/null
@@ -1,95 +0,0 @@
-// Copyright 2021 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 .
-
-package eip1559
-
-import (
- "errors"
- "fmt"
- "math/big"
-
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/common/math"
- "github.com/ethereum/go-ethereum/consensus/misc"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/params"
-)
-
-// VerifyEIP1559Header verifies some header attributes which were changed in EIP-1559,
-// - gas limit check
-// - basefee check
-func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Header) error {
- // Verify that the gas limit remains within allowed bounds
- parentGasLimit := parent.GasLimit
- if !config.IsLondon(parent.Number) {
- parentGasLimit = parent.GasLimit * config.ElasticityMultiplier()
- }
- if err := misc.VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil {
- return err
- }
- // Verify the header is not malformed
- if header.BaseFee == nil {
- return errors.New("header is missing baseFee")
- }
- // Verify the baseFee is correct based on the parent header.
- expectedBaseFee := CalcBaseFee(config, parent)
- if header.BaseFee.Cmp(expectedBaseFee) != 0 {
- return fmt.Errorf("invalid baseFee: have %s, want %s, parentBaseFee %s, parentGasUsed %d",
- header.BaseFee, expectedBaseFee, parent.BaseFee, parent.GasUsed)
- }
- return nil
-}
-
-// CalcBaseFee calculates the basefee of the header.
-func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
- // If the current block is the first EIP-1559 block, return the InitialBaseFee.
- if !config.IsLondon(parent.Number) {
- return new(big.Int).SetUint64(params.InitialBaseFee)
- }
-
- parentGasTarget := parent.GasLimit / config.ElasticityMultiplier()
- // If the parent gasUsed is the same as the target, the baseFee remains unchanged.
- if parent.GasUsed == parentGasTarget {
- return new(big.Int).Set(parent.BaseFee)
- }
-
- var (
- num = new(big.Int)
- denom = new(big.Int)
- )
-
- if parent.GasUsed > parentGasTarget {
- // If the parent block used more gas than its target, the baseFee should increase.
- // max(1, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
- num.SetUint64(parent.GasUsed - parentGasTarget)
- num.Mul(num, parent.BaseFee)
- num.Div(num, denom.SetUint64(parentGasTarget))
- num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator()))
- baseFeeDelta := math.BigMax(num, common.Big1)
-
- return num.Add(parent.BaseFee, baseFeeDelta)
- } else {
- // Otherwise if the parent block used less gas than its target, the baseFee should decrease.
- // max(0, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
- num.SetUint64(parentGasTarget - parent.GasUsed)
- num.Mul(num, parent.BaseFee)
- num.Div(num, denom.SetUint64(parentGasTarget))
- num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator()))
- baseFee := num.Sub(parent.BaseFee, num)
-
- return math.BigMax(baseFee, common.Big0)
- }
-}
diff --git a/consensus/misc/eip1559/eip1559_test.go b/consensus/misc/eip1559/eip1559_test.go
deleted file mode 100644
index b5afdf0fe5..0000000000
--- a/consensus/misc/eip1559/eip1559_test.go
+++ /dev/null
@@ -1,131 +0,0 @@
-// Copyright 2021 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 .
-
-package eip1559
-
-import (
- "math/big"
- "testing"
-
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/params"
-)
-
-// copyConfig does a _shallow_ copy of a given config. Safe to set new values, but
-// do not use e.g. SetInt() on the numbers. For testing only
-func copyConfig(original *params.ChainConfig) *params.ChainConfig {
- return ¶ms.ChainConfig{
- ChainID: original.ChainID,
- HomesteadBlock: original.HomesteadBlock,
- DAOForkBlock: original.DAOForkBlock,
- DAOForkSupport: original.DAOForkSupport,
- EIP150Block: original.EIP150Block,
- EIP155Block: original.EIP155Block,
- EIP158Block: original.EIP158Block,
- ByzantiumBlock: original.ByzantiumBlock,
- ConstantinopleBlock: original.ConstantinopleBlock,
- PetersburgBlock: original.PetersburgBlock,
- IstanbulBlock: original.IstanbulBlock,
- MuirGlacierBlock: original.MuirGlacierBlock,
- BerlinBlock: original.BerlinBlock,
- LondonBlock: original.LondonBlock,
- TerminalTotalDifficulty: original.TerminalTotalDifficulty,
- Ethash: original.Ethash,
- Clique: original.Clique,
- }
-}
-
-func config() *params.ChainConfig {
- config := copyConfig(params.TestChainConfig)
- config.LondonBlock = big.NewInt(5)
- return config
-}
-
-// TestBlockGasLimits tests the gasLimit checks for blocks both across
-// the EIP-1559 boundary and post-1559 blocks
-func TestBlockGasLimits(t *testing.T) {
- initial := new(big.Int).SetUint64(params.InitialBaseFee)
-
- for i, tc := range []struct {
- pGasLimit uint64
- pNum int64
- gasLimit uint64
- ok bool
- }{
- // Transitions from non-london to london
- {10000000, 4, 20000000, true}, // No change
- {10000000, 4, 20019530, true}, // Upper limit
- {10000000, 4, 20019531, false}, // Upper +1
- {10000000, 4, 19980470, true}, // Lower limit
- {10000000, 4, 19980469, false}, // Lower limit -1
- // London to London
- {20000000, 5, 20000000, true},
- {20000000, 5, 20019530, true}, // Upper limit
- {20000000, 5, 20019531, false}, // Upper limit +1
- {20000000, 5, 19980470, true}, // Lower limit
- {20000000, 5, 19980469, false}, // Lower limit -1
- {40000000, 5, 40039061, true}, // Upper limit
- {40000000, 5, 40039062, false}, // Upper limit +1
- {40000000, 5, 39960939, true}, // lower limit
- {40000000, 5, 39960938, false}, // Lower limit -1
- } {
- parent := &types.Header{
- GasUsed: tc.pGasLimit / 2,
- GasLimit: tc.pGasLimit,
- BaseFee: initial,
- Number: big.NewInt(tc.pNum),
- }
- header := &types.Header{
- GasUsed: tc.gasLimit / 2,
- GasLimit: tc.gasLimit,
- BaseFee: initial,
- Number: big.NewInt(tc.pNum + 1),
- }
- err := VerifyEIP1559Header(config(), parent, header)
- if tc.ok && err != nil {
- t.Errorf("test %d: Expected valid header: %s", i, err)
- }
- if !tc.ok && err == nil {
- t.Errorf("test %d: Expected invalid header", i)
- }
- }
-}
-
-// TestCalcBaseFee assumes all blocks are 1559-blocks
-func TestCalcBaseFee(t *testing.T) {
- tests := []struct {
- parentBaseFee int64
- parentGasLimit uint64
- parentGasUsed uint64
- expectedBaseFee int64
- }{
- {params.InitialBaseFee, 20000000, 10000000, params.InitialBaseFee}, // usage == target
- {params.InitialBaseFee, 20000000, 9000000, 987500000}, // usage below target
- {params.InitialBaseFee, 20000000, 11000000, 1012500000}, // usage above target
- }
- for i, test := range tests {
- parent := &types.Header{
- Number: common.Big32,
- GasLimit: test.parentGasLimit,
- GasUsed: test.parentGasUsed,
- BaseFee: big.NewInt(test.parentBaseFee),
- }
- if have, want := CalcBaseFee(config(), parent), big.NewInt(test.expectedBaseFee); have.Cmp(want) != 0 {
- t.Errorf("test %d: have %d want %d, ", i, have, want)
- }
- }
-}
diff --git a/consensus/misc/eip4844/eip4844.go b/consensus/misc/eip4844/eip4844.go
deleted file mode 100644
index 2dad9a0cd3..0000000000
--- a/consensus/misc/eip4844/eip4844.go
+++ /dev/null
@@ -1,98 +0,0 @@
-// Copyright 2023 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 .
-
-package eip4844
-
-import (
- "errors"
- "fmt"
- "math/big"
-
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/params"
-)
-
-var (
- minBlobGasPrice = big.NewInt(params.BlobTxMinBlobGasprice)
- blobGaspriceUpdateFraction = big.NewInt(params.BlobTxBlobGaspriceUpdateFraction)
-)
-
-// VerifyEIP4844Header verifies the presence of the excessBlobGas field and that
-// if the current block contains no transactions, the excessBlobGas is updated
-// accordingly.
-func VerifyEIP4844Header(parent, header *types.Header) error {
- // Verify the header is not malformed
- if header.ExcessBlobGas == nil {
- return errors.New("header is missing excessBlobGas")
- }
- if header.BlobGasUsed == nil {
- return errors.New("header is missing blobGasUsed")
- }
- // Verify that the blob gas used remains within reasonable limits.
- if *header.BlobGasUsed > params.MaxBlobGasPerBlock {
- return fmt.Errorf("blob gas used %d exceeds maximum allowance %d", *header.BlobGasUsed, params.MaxBlobGasPerBlock)
- }
- if *header.BlobGasUsed%params.BlobTxBlobGasPerBlob != 0 {
- return fmt.Errorf("blob gas used %d not a multiple of blob gas per blob %d", header.BlobGasUsed, params.BlobTxBlobGasPerBlob)
- }
- // Verify the excessBlobGas is correct based on the parent header
- var (
- parentExcessBlobGas uint64
- parentBlobGasUsed uint64
- )
- if parent.ExcessBlobGas != nil {
- parentExcessBlobGas = *parent.ExcessBlobGas
- parentBlobGasUsed = *parent.BlobGasUsed
- }
- expectedExcessBlobGas := CalcExcessBlobGas(parentExcessBlobGas, parentBlobGasUsed)
- if *header.ExcessBlobGas != expectedExcessBlobGas {
- return fmt.Errorf("invalid excessBlobGas: have %d, want %d, parent excessBlobGas %d, parent blobDataUsed %d",
- *header.ExcessBlobGas, expectedExcessBlobGas, parentExcessBlobGas, parentBlobGasUsed)
- }
- return nil
-}
-
-// CalcExcessBlobGas calculates the excess blob gas after applying the set of
-// blobs on top of the excess blob gas.
-func CalcExcessBlobGas(parentExcessBlobGas uint64, parentBlobGasUsed uint64) uint64 {
- excessBlobGas := parentExcessBlobGas + parentBlobGasUsed
- if excessBlobGas < params.BlobTxTargetBlobGasPerBlock {
- return 0
- }
- return excessBlobGas - params.BlobTxTargetBlobGasPerBlock
-}
-
-// CalcBlobFee calculates the blobfee from the header's excess blob gas field.
-func CalcBlobFee(excessBlobGas uint64) *big.Int {
- return fakeExponential(minBlobGasPrice, new(big.Int).SetUint64(excessBlobGas), blobGaspriceUpdateFraction)
-}
-
-// fakeExponential approximates factor * e ** (numerator / denominator) using
-// Taylor expansion.
-func fakeExponential(factor, numerator, denominator *big.Int) *big.Int {
- var (
- output = new(big.Int)
- accum = new(big.Int).Mul(factor, denominator)
- )
- for i := 1; accum.Sign() > 0; i++ {
- output.Add(output, accum)
-
- accum.Mul(accum, numerator)
- accum.Div(accum, denominator)
- accum.Div(accum, big.NewInt(int64(i)))
- }
- return output.Div(output, denominator)
-}
diff --git a/consensus/misc/eip4844/eip4844_test.go b/consensus/misc/eip4844/eip4844_test.go
deleted file mode 100644
index ec417380fc..0000000000
--- a/consensus/misc/eip4844/eip4844_test.go
+++ /dev/null
@@ -1,114 +0,0 @@
-// Copyright 2023 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 .
-
-package eip4844
-
-import (
- "fmt"
- "math/big"
- "testing"
-
- "github.com/ethereum/go-ethereum/params"
-)
-
-func TestCalcExcessBlobGas(t *testing.T) {
- var tests = []struct {
- excess uint64
- blobs uint64
- want uint64
- }{
- // The excess blob gas should not increase from zero if the used blob
- // slots are below - or equal - to the target.
- {0, 0, 0},
- {0, 1, 0},
- {0, params.BlobTxTargetBlobGasPerBlock / params.BlobTxBlobGasPerBlob, 0},
-
- // If the target blob gas is exceeded, the excessBlobGas should increase
- // by however much it was overshot
- {0, (params.BlobTxTargetBlobGasPerBlock / params.BlobTxBlobGasPerBlob) + 1, params.BlobTxBlobGasPerBlob},
- {1, (params.BlobTxTargetBlobGasPerBlock / params.BlobTxBlobGasPerBlob) + 1, params.BlobTxBlobGasPerBlob + 1},
- {1, (params.BlobTxTargetBlobGasPerBlock / params.BlobTxBlobGasPerBlob) + 2, 2*params.BlobTxBlobGasPerBlob + 1},
-
- // The excess blob gas should decrease by however much the target was
- // under-shot, capped at zero.
- {params.BlobTxTargetBlobGasPerBlock, params.BlobTxTargetBlobGasPerBlock / params.BlobTxBlobGasPerBlob, params.BlobTxTargetBlobGasPerBlock},
- {params.BlobTxTargetBlobGasPerBlock, (params.BlobTxTargetBlobGasPerBlock / params.BlobTxBlobGasPerBlob) - 1, params.BlobTxTargetBlobGasPerBlock - params.BlobTxBlobGasPerBlob},
- {params.BlobTxTargetBlobGasPerBlock, (params.BlobTxTargetBlobGasPerBlock / params.BlobTxBlobGasPerBlob) - 2, params.BlobTxTargetBlobGasPerBlock - (2 * params.BlobTxBlobGasPerBlob)},
- {params.BlobTxBlobGasPerBlob - 1, (params.BlobTxTargetBlobGasPerBlock / params.BlobTxBlobGasPerBlob) - 1, 0},
- }
- for i, tt := range tests {
- result := CalcExcessBlobGas(tt.excess, tt.blobs*params.BlobTxBlobGasPerBlob)
- if result != tt.want {
- t.Errorf("test %d: excess blob gas mismatch: have %v, want %v", i, result, tt.want)
- }
- }
-}
-
-func TestCalcBlobFee(t *testing.T) {
- tests := []struct {
- excessBlobGas uint64
- blobfee int64
- }{
- {0, 1},
- {2314057, 1},
- {2314058, 2},
- {10 * 1024 * 1024, 23},
- }
- for i, tt := range tests {
- have := CalcBlobFee(tt.excessBlobGas)
- if have.Int64() != tt.blobfee {
- t.Errorf("test %d: blobfee mismatch: have %v want %v", i, have, tt.blobfee)
- }
- }
-}
-
-func TestFakeExponential(t *testing.T) {
- tests := []struct {
- factor int64
- numerator int64
- denominator int64
- want int64
- }{
- // When numerator == 0 the return value should always equal the value of factor
- {1, 0, 1, 1},
- {38493, 0, 1000, 38493},
- {0, 1234, 2345, 0}, // should be 0
- {1, 2, 1, 6}, // approximate 7.389
- {1, 4, 2, 6},
- {1, 3, 1, 16}, // approximate 20.09
- {1, 6, 2, 18},
- {1, 4, 1, 49}, // approximate 54.60
- {1, 8, 2, 50},
- {10, 8, 2, 542}, // approximate 540.598
- {11, 8, 2, 596}, // approximate 600.58
- {1, 5, 1, 136}, // approximate 148.4
- {1, 5, 2, 11}, // approximate 12.18
- {2, 5, 2, 23}, // approximate 24.36
- {1, 50000000, 2225652, 5709098764},
- }
- for i, tt := range tests {
- f, n, d := big.NewInt(tt.factor), big.NewInt(tt.numerator), big.NewInt(tt.denominator)
- original := fmt.Sprintf("%d %d %d", f, n, d)
- have := fakeExponential(f, n, d)
- if have.Int64() != tt.want {
- t.Errorf("test %d: fake exponential mismatch: have %v want %v", i, have, tt.want)
- }
- later := fmt.Sprintf("%d %d %d", f, n, d)
- if original != later {
- t.Errorf("test %d: fake exponential modified arguments: have\n%v\nwant\n%v", i, later, original)
- }
- }
-}
diff --git a/consensus/misc/gaslimit.go b/consensus/misc/gaslimit.go
deleted file mode 100644
index dfcabd9a80..0000000000
--- a/consensus/misc/gaslimit.go
+++ /dev/null
@@ -1,41 +0,0 @@
-// Copyright 2021 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 .
-
-package misc
-
-import (
- "fmt"
-
- "github.com/ethereum/go-ethereum/params"
-)
-
-// VerifyGaslimit verifies the header gas limit according increase/decrease
-// in relation to the parent gas limit.
-func VerifyGaslimit(parentGasLimit, headerGasLimit uint64) error {
- // Verify that the gas limit remains within allowed bounds
- diff := int64(parentGasLimit) - int64(headerGasLimit)
- if diff < 0 {
- diff *= -1
- }
- limit := parentGasLimit / params.GasLimitBoundDivisor
- if uint64(diff) >= limit {
- return fmt.Errorf("invalid gas limit: have %d, want %d +-= %d", headerGasLimit, parentGasLimit, limit-1)
- }
- if headerGasLimit < params.MinGasLimit {
- return fmt.Errorf("invalid gas limit below %d", params.MinGasLimit)
- }
- return nil
-}