github.com/MetalBlockchain/subnet-evm@v0.4.9/consensus/consensus.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2017 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  // Package consensus implements different Ethereum consensus engines.
    28  package consensus
    29  
    30  import (
    31  	"math/big"
    32  
    33  	"github.com/MetalBlockchain/subnet-evm/commontype"
    34  	"github.com/MetalBlockchain/subnet-evm/core/state"
    35  	"github.com/MetalBlockchain/subnet-evm/core/types"
    36  	"github.com/MetalBlockchain/subnet-evm/params"
    37  	"github.com/ethereum/go-ethereum/common"
    38  )
    39  
    40  // ChainHeaderReader defines a small collection of methods needed to access the local
    41  // blockchain during header verification.
    42  type ChainHeaderReader interface {
    43  	// Config retrieves the blockchain's chain configuration.
    44  	Config() *params.ChainConfig
    45  
    46  	// CurrentHeader retrieves the current header from the local chain.
    47  	CurrentHeader() *types.Header
    48  
    49  	// GetHeader retrieves a block header from the database by hash and number.
    50  	GetHeader(hash common.Hash, number uint64) *types.Header
    51  
    52  	// GetHeaderByNumber retrieves a block header from the database by number.
    53  	GetHeaderByNumber(number uint64) *types.Header
    54  
    55  	// GetHeaderByHash retrieves a block header from the database by its hash.
    56  	GetHeaderByHash(hash common.Hash) *types.Header
    57  
    58  	// GetFeeConfigAt retrieves the fee config and last changed block number at block header.
    59  	GetFeeConfigAt(parent *types.Header) (commontype.FeeConfig, *big.Int, error)
    60  
    61  	// GetCoinbaseAt retrieves the configured coinbase address at [parent].
    62  	// If fee recipients are allowed, returns true in the second return value and a predefined address in the first value.
    63  	GetCoinbaseAt(parent *types.Header) (common.Address, bool, error)
    64  }
    65  
    66  // ChainReader defines a small collection of methods needed to access the local
    67  // blockchain during header and/or uncle verification.
    68  type ChainReader interface {
    69  	ChainHeaderReader
    70  
    71  	// GetBlock retrieves a block from the database by hash and number.
    72  	GetBlock(hash common.Hash, number uint64) *types.Block
    73  }
    74  
    75  // Engine is an algorithm agnostic consensus engine.
    76  type Engine interface {
    77  	// Author retrieves the Ethereum address of the account that minted the given
    78  	// block, which may be different from the header's coinbase if a consensus
    79  	// engine is based on signatures.
    80  	Author(header *types.Header) (common.Address, error)
    81  
    82  	// VerifyHeader checks whether a header conforms to the consensus rules of a
    83  	// given engine.
    84  	//
    85  	// NOTE: VerifyHeader does not validate the correctness of fields that rely
    86  	// on the contents of the block (as opposed to the current and/or parent
    87  	// header).
    88  	VerifyHeader(chain ChainHeaderReader, header *types.Header) error
    89  
    90  	// VerifyUncles verifies that the given block's uncles conform to the consensus
    91  	// rules of a given engine.
    92  	VerifyUncles(chain ChainReader, block *types.Block) error
    93  
    94  	// Prepare initializes the consensus fields of a block header according to the
    95  	// rules of a particular engine. The changes are executed inline.
    96  	Prepare(chain ChainHeaderReader, header *types.Header) error
    97  
    98  	// Finalize runs any post-transaction state modifications (e.g. block rewards)
    99  	// but does not assemble the block.
   100  	//
   101  	// Note: The block header and state database might be updated to reflect any
   102  	// consensus rules that happen at finalization (e.g. block rewards).
   103  	Finalize(chain ChainHeaderReader, block *types.Block, parent *types.Header, state *state.StateDB, receipts []*types.Receipt) error
   104  
   105  	// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
   106  	// rewards) and assembles the final block.
   107  	//
   108  	// Note: The block header and state database might be updated to reflect any
   109  	// consensus rules that happen at finalization (e.g. block rewards).
   110  	FinalizeAndAssemble(chain ChainHeaderReader, header *types.Header, parent *types.Header, state *state.StateDB, txs []*types.Transaction,
   111  		uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error)
   112  
   113  	// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
   114  	// that a new block should have.
   115  	CalcDifficulty(chain ChainHeaderReader, time uint64, parent *types.Header) *big.Int
   116  
   117  	// Close terminates any background threads maintained by the consensus engine.
   118  	Close() error
   119  }