github.com/DxChainNetwork/dxc@v0.8.1-0.20220824085222-1162e304b6e7/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/DxChainNetwork/dxc/common"
    24  	"github.com/DxChainNetwork/dxc/core/state"
    25  	"github.com/DxChainNetwork/dxc/core/types"
    26  	"github.com/DxChainNetwork/dxc/core/vm"
    27  	"github.com/DxChainNetwork/dxc/params"
    28  	"github.com/DxChainNetwork/dxc/rpc"
    29  )
    30  
    31  var (
    32  	FeeRecoder = common.HexToAddress("0xffffffffffffffffffffffffffffffffffffffff")
    33  )
    34  
    35  // ChainHeaderReader defines a small collection of methods needed to access the local
    36  // blockchain during header verification.
    37  type ChainHeaderReader interface {
    38  	// Config retrieves the blockchain's chain configuration.
    39  	Config() *params.ChainConfig
    40  
    41  	// CurrentHeader retrieves the current header from the local chain.
    42  	CurrentHeader() *types.Header
    43  
    44  	// GetHeader retrieves a block header from the database by hash and number.
    45  	GetHeader(hash common.Hash, number uint64) *types.Header
    46  
    47  	// GetHeaderByNumber retrieves a block header from the database by number.
    48  	GetHeaderByNumber(number uint64) *types.Header
    49  
    50  	// GetHeaderByHash retrieves a block header from the database by its hash.
    51  	GetHeaderByHash(hash common.Hash) *types.Header
    52  }
    53  
    54  // ChainReader defines a small collection of methods needed to access the local
    55  // blockchain during header and/or uncle verification.
    56  type ChainReader interface {
    57  	ChainHeaderReader
    58  
    59  	// GetBlock retrieves a block from the database by hash and number.
    60  	GetBlock(hash common.Hash, number uint64) *types.Block
    61  }
    62  
    63  // Engine is an algorithm agnostic consensus engine.
    64  type Engine interface {
    65  	// Author retrieves the Ethereum address of the account that minted the given
    66  	// block, which may be different from the header's coinbase if a consensus
    67  	// engine is based on signatures.
    68  	Author(header *types.Header) (common.Address, error)
    69  
    70  	// VerifyHeader checks whether a header conforms to the consensus rules of a
    71  	// given engine. Verifying the seal may be done optionally here, or explicitly
    72  	// via the VerifySeal method.
    73  	VerifyHeader(chain ChainHeaderReader, header *types.Header, seal bool) error
    74  
    75  	// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
    76  	// concurrently. The method returns a quit channel to abort the operations and
    77  	// a results channel to retrieve the async verifications (the order is that of
    78  	// the input slice).
    79  	VerifyHeaders(chain ChainHeaderReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error)
    80  
    81  	// VerifyUncles verifies that the given block's uncles conform to the consensus
    82  	// rules of a given engine.
    83  	VerifyUncles(chain ChainReader, block *types.Block) error
    84  
    85  	// Prepare initializes the consensus fields of a block header according to the
    86  	// rules of a particular engine. The changes are executed inline.
    87  	Prepare(chain ChainHeaderReader, header *types.Header) error
    88  
    89  	// Finalize runs any post-transaction state modifications (e.g. block rewards)
    90  	// but does not assemble the block.
    91  	//
    92  	// Note: The block header and state database might be updated to reflect any
    93  	// consensus rules that happen at finalization (e.g. block rewards).
    94  	Finalize(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs *[]*types.Transaction,
    95  		uncles []*types.Header, receipts *[]*types.Receipt, systemTxs []*types.Transaction) 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, state *state.StateDB, txs []*types.Transaction,
   103  		uncles []*types.Header, receipts []*types.Receipt) (*types.Block, []*types.Receipt, error)
   104  
   105  	// Seal generates a new sealing request for the given input block and pushes
   106  	// the result into the given channel.
   107  	//
   108  	// Note, the method returns immediately and will send the result async. More
   109  	// than one result may also be returned depending on the consensus algorithm.
   110  	Seal(chain ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error
   111  
   112  	// SealHash returns the hash of a block prior to it being sealed.
   113  	SealHash(header *types.Header) common.Hash
   114  
   115  	// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
   116  	// that a new block should have.
   117  	CalcDifficulty(chain ChainHeaderReader, time uint64, parent *types.Header) *big.Int
   118  
   119  	// APIs returns the RPC APIs this consensus engine provides.
   120  	APIs(chain ChainHeaderReader) []rpc.API
   121  
   122  	// Close terminates any background threads maintained by the consensus engine.
   123  	Close() error
   124  }
   125  
   126  // PoW is a consensus engine based on proof-of-work.
   127  type PoW interface {
   128  	Engine
   129  
   130  	// Hashrate returns the current mining hashrate of a PoW consensus engine.
   131  	Hashrate() float64
   132  }
   133  
   134  // PoSA is a consensus engine based on proof-of-stake-authority.
   135  type PoSA interface {
   136  	Engine
   137  
   138  	// PreHandle runs any pre-transaction state modifications (e.g. apply hard fork rules).
   139  	//
   140  	// Note: The block header and state database might be updated to reflect any
   141  	// consensus rules that happen at pre-handling.
   142  	PreHandle(chain ChainHeaderReader, header *types.Header, state *state.StateDB) error
   143  
   144  	// IsSysTransaction checks whether a specific transaction is a system transaction.
   145  	IsSysTransaction(sender common.Address, tx *types.Transaction, header *types.Header) (bool, error)
   146  
   147  	// CanCreate determines where a given address can create a new contract.
   148  	CanCreate(state StateReader, addr common.Address, height *big.Int) bool
   149  
   150  	// ValidateTx do a consensus-related validation on the given transaction at the given header and state.
   151  	ValidateTx(sender common.Address, tx *types.Transaction, header *types.Header, parentState *state.StateDB) error
   152  
   153  	// CreateEvmExtraValidator returns a EvmExtraValidator if necessary.
   154  	CreateEvmExtraValidator(header *types.Header, parentState *state.StateDB) types.EvmExtraValidator
   155  
   156  	//Methods for debug trace
   157  
   158  	// ApplySysTx applies a system-transaction using a given evm,
   159  	// the main purpose of this method is for tracing a system-transaction.
   160  	ApplySysTx(evm *vm.EVM, state *state.StateDB, txIndex int, sender common.Address, tx *types.Transaction) (ret []byte, vmerr error, err error)
   161  }
   162  
   163  type StateReader interface {
   164  	GetState(addr common.Address, hash common.Hash) common.Hash
   165  }