github.com/dim4egster/coreth@v0.10.2/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/dim4egster/coreth/core/state"
    34  	"github.com/dim4egster/coreth/core/types"
    35  	"github.com/dim4egster/coreth/params"
    36  	"github.com/ethereum/go-ethereum/common"
    37  )
    38  
    39  // ChainHeaderReader defines a small collection of methods needed to access the local
    40  // blockchain during header verification.
    41  type ChainHeaderReader interface {
    42  	// Config retrieves the blockchain's chain configuration.
    43  	Config() *params.ChainConfig
    44  
    45  	// CurrentHeader retrieves the current header from the local chain.
    46  	CurrentHeader() *types.Header
    47  
    48  	// GetHeader retrieves a block header from the database by hash and number.
    49  	GetHeader(hash common.Hash, number uint64) *types.Header
    50  
    51  	// GetHeaderByNumber retrieves a block header from the database by number.
    52  	GetHeaderByNumber(number uint64) *types.Header
    53  
    54  	// GetHeaderByHash retrieves a block header from the database by its hash.
    55  	GetHeaderByHash(hash common.Hash) *types.Header
    56  }
    57  
    58  // ChainReader defines a small collection of methods needed to access the local
    59  // blockchain during header and/or uncle verification.
    60  type ChainReader interface {
    61  	ChainHeaderReader
    62  
    63  	// GetBlock retrieves a block from the database by hash and number.
    64  	GetBlock(hash common.Hash, number uint64) *types.Block
    65  }
    66  
    67  // Engine is an algorithm agnostic consensus engine.
    68  type Engine interface {
    69  	// Author retrieves the Ethereum address of the account that minted the given
    70  	// block, which may be different from the header's coinbase if a consensus
    71  	// engine is based on signatures.
    72  	Author(header *types.Header) (common.Address, error)
    73  
    74  	// VerifyHeader checks whether a header conforms to the consensus rules of a
    75  	// given engine.
    76  	//
    77  	// NOTE: VerifyHeader does not validate the correctness of fields that rely
    78  	// on the contents of the block (as opposed to the current and/or parent
    79  	// header).
    80  	VerifyHeader(chain ChainHeaderReader, header *types.Header) error
    81  
    82  	// VerifyUncles verifies that the given block's uncles conform to the consensus
    83  	// rules of a given engine.
    84  	VerifyUncles(chain ChainReader, block *types.Block) error
    85  
    86  	// Prepare initializes the consensus fields of a block header according to the
    87  	// rules of a particular engine. The changes are executed inline.
    88  	Prepare(chain ChainHeaderReader, header *types.Header) error
    89  
    90  	// Finalize runs any post-transaction state modifications (e.g. block rewards)
    91  	// but does not assemble the block.
    92  	//
    93  	// Note: The block header and state database might be updated to reflect any
    94  	// consensus rules that happen at finalization (e.g. block rewards).
    95  	Finalize(chain ChainHeaderReader, block *types.Block, parent *types.Header, state *state.StateDB, receipts []*types.Receipt) error
    96  
    97  	// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
    98  	// rewards) and assembles the final block.
    99  	//
   100  	// Note: The block header and state database might be updated to reflect any
   101  	// consensus rules that happen at finalization (e.g. block rewards).
   102  	FinalizeAndAssemble(chain ChainHeaderReader, header *types.Header, parent *types.Header, state *state.StateDB, txs []*types.Transaction,
   103  		uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error)
   104  
   105  	// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
   106  	// that a new block should have.
   107  	CalcDifficulty(chain ChainHeaderReader, time uint64, parent *types.Header) *big.Int
   108  
   109  	// Close terminates any background threads maintained by the consensus engine.
   110  	Close() error
   111  }