github.com/ethereum/go-ethereum@v1.14.4-0.20240516095835-473ee8fc07a3/consensus/consensus.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package consensus implements different Ethereum consensus engines.
    18  package consensus
    19  
    20  import (
    21  	"math/big"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/core/state"
    25  	"github.com/ethereum/go-ethereum/core/types"
    26  	"github.com/ethereum/go-ethereum/params"
    27  	"github.com/ethereum/go-ethereum/rpc"
    28  )
    29  
    30  // ChainHeaderReader defines a small collection of methods needed to access the local
    31  // blockchain during header verification.
    32  type ChainHeaderReader interface {
    33  	// Config retrieves the blockchain's chain configuration.
    34  	Config() *params.ChainConfig
    35  
    36  	// CurrentHeader retrieves the current header from the local chain.
    37  	CurrentHeader() *types.Header
    38  
    39  	// GetHeader retrieves a block header from the database by hash and number.
    40  	GetHeader(hash common.Hash, number uint64) *types.Header
    41  
    42  	// GetHeaderByNumber retrieves a block header from the database by number.
    43  	GetHeaderByNumber(number uint64) *types.Header
    44  
    45  	// GetHeaderByHash retrieves a block header from the database by its hash.
    46  	GetHeaderByHash(hash common.Hash) *types.Header
    47  
    48  	// GetTd retrieves the total difficulty from the database by hash and number.
    49  	GetTd(hash common.Hash, number uint64) *big.Int
    50  }
    51  
    52  // ChainReader defines a small collection of methods needed to access the local
    53  // blockchain during header and/or uncle verification.
    54  type ChainReader interface {
    55  	ChainHeaderReader
    56  
    57  	// GetBlock retrieves a block from the database by hash and number.
    58  	GetBlock(hash common.Hash, number uint64) *types.Block
    59  }
    60  
    61  // Engine is an algorithm agnostic consensus engine.
    62  type Engine interface {
    63  	// Author retrieves the Ethereum address of the account that minted the given
    64  	// block, which may be different from the header's coinbase if a consensus
    65  	// engine is based on signatures.
    66  	Author(header *types.Header) (common.Address, error)
    67  
    68  	// VerifyHeader checks whether a header conforms to the consensus rules of a
    69  	// given engine.
    70  	VerifyHeader(chain ChainHeaderReader, header *types.Header) error
    71  
    72  	// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
    73  	// concurrently. The method returns a quit channel to abort the operations and
    74  	// a results channel to retrieve the async verifications (the order is that of
    75  	// the input slice).
    76  	VerifyHeaders(chain ChainHeaderReader, headers []*types.Header) (chan<- struct{}, <-chan error)
    77  
    78  	// VerifyUncles verifies that the given block's uncles conform to the consensus
    79  	// rules of a given engine.
    80  	VerifyUncles(chain ChainReader, block *types.Block) error
    81  
    82  	// Prepare initializes the consensus fields of a block header according to the
    83  	// rules of a particular engine. The changes are executed inline.
    84  	Prepare(chain ChainHeaderReader, header *types.Header) error
    85  
    86  	// Finalize runs any post-transaction state modifications (e.g. block rewards
    87  	// or process withdrawals) but does not assemble the block.
    88  	//
    89  	// Note: The state database might be updated to reflect any consensus rules
    90  	// that happen at finalization (e.g. block rewards).
    91  	Finalize(chain ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body)
    92  
    93  	// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
    94  	// rewards or process withdrawals) and assembles the final block.
    95  	//
    96  	// Note: The block header and state database might be updated to reflect any
    97  	// consensus rules that happen at finalization (e.g. block rewards).
    98  	FinalizeAndAssemble(chain ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt) (*types.Block, error)
    99  
   100  	// Seal generates a new sealing request for the given input block and pushes
   101  	// the result into the given channel.
   102  	//
   103  	// Note, the method returns immediately and will send the result async. More
   104  	// than one result may also be returned depending on the consensus algorithm.
   105  	Seal(chain ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error
   106  
   107  	// SealHash returns the hash of a block prior to it being sealed.
   108  	SealHash(header *types.Header) common.Hash
   109  
   110  	// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
   111  	// that a new block should have.
   112  	CalcDifficulty(chain ChainHeaderReader, time uint64, parent *types.Header) *big.Int
   113  
   114  	// APIs returns the RPC APIs this consensus engine provides.
   115  	APIs(chain ChainHeaderReader) []rpc.API
   116  
   117  	// Close terminates any background threads maintained by the consensus engine.
   118  	Close() error
   119  }