github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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 INT Chain consensus engines.
    18  package consensus
    19  
    20  import (
    21  	"github.com/intfoundation/intchain/common"
    22  	"github.com/intfoundation/intchain/consensus/ipbft/epoch"
    23  	"github.com/intfoundation/intchain/core/state"
    24  	"github.com/intfoundation/intchain/core/types"
    25  	"github.com/intfoundation/intchain/params"
    26  	"github.com/intfoundation/intchain/rpc"
    27  	"math/big"
    28  )
    29  
    30  // ChainReader defines a small collection of methods needed to access the local
    31  // blockchain during header and/or uncle verification.
    32  type ChainReader 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  	// GetBlock retrieves a block from the database by hash and number.
    49  	GetBlock(hash common.Hash, number uint64) *types.Block
    50  
    51  	// GetBlockByNumber retrieves a block from the database by number, caching it
    52  	// (associated with its hash) if found.
    53  	GetBlockByNumber(number uint64) *types.Block
    54  
    55  	// GetTd retrieves a block's total difficulty in the canonical chain from the
    56  	// database by hash and number, caching it if found.
    57  	GetTd(hash common.Hash, number uint64) *big.Int
    58  
    59  	// CurrentBlock retrieves the current head block of the canonical chain. The
    60  	// block is retrieved from the blockchain's internal cache.
    61  	CurrentBlock() *types.Block
    62  
    63  	// State retrieves the current state of the canonical chain.
    64  	State() (*state.StateDB, error)
    65  }
    66  
    67  // ChainValidator execute and validate the block with the current latest block as parent.
    68  type ChainValidator interface {
    69  	ValidateBlock(block *types.Block) (*state.StateDB, types.Receipts, *types.PendingOps, error)
    70  }
    71  
    72  // Engine is an algorithm agnostic consensus engine.
    73  type Engine interface {
    74  	// Author retrieves the Ethereum address of the account that minted the given
    75  	// block, which may be different from the header's coinbase if a consensus
    76  	// engine is based on signatures.
    77  	Author(header *types.Header) (common.Address, error)
    78  
    79  	// VerifyHeader checks whether a header conforms to the consensus rules of a
    80  	// given engine. Verifying the seal may be done optionally here, or explicitly
    81  	// via the VerifySeal method.
    82  	VerifyHeader(chain ChainReader, header *types.Header, seal bool) error
    83  
    84  	// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
    85  	// concurrently. The method returns a quit channel to abort the operations and
    86  	// a results channel to retrieve the async verifications (the order is that of
    87  	// the input slice).
    88  	VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan 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  	// VerifySeal checks whether the crypto seal on a header is valid according to
    95  	// the consensus rules of the given engine.
    96  	VerifySeal(chain ChainReader, header *types.Header) error
    97  
    98  	// Prepare initializes the consensus fields of a block header according to the
    99  	// rules of a particular engine. The changes are executed inline.
   100  	Prepare(chain ChainReader, header *types.Header) error
   101  
   102  	// Finalize runs any post-transaction state modifications (e.g. block rewards)
   103  	// and assembles the final block.
   104  	// Note: The block header and state database might be updated to reflect any
   105  	// consensus rules that happen at finalization (e.g. block rewards).
   106  	Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, totalGasFee *big.Int,
   107  		uncles []*types.Header, receipts []*types.Receipt, ops *types.PendingOps) (*types.Block, error)
   108  
   109  	// Seal generates a new block for the given input block with the local miner's
   110  	// seal place on top.
   111  	Seal(chain ChainReader, block *types.Block, stop <-chan struct{}) (interface{}, error)
   112  
   113  	// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
   114  	// that a new block should have.
   115  	CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int
   116  
   117  	// APIs returns the RPC APIs this consensus engine provides.
   118  	APIs(chain ChainReader) []rpc.API
   119  
   120  	// Close terminates any background threads maintained by the consensus engine.
   121  	Close() error
   122  
   123  	// Protocol returns the protocol for this consensus
   124  	Protocol() Protocol
   125  }
   126  
   127  // Handler should be implemented is the consensus needs to handle and send peer's message
   128  type Handler interface {
   129  	// NewChainHead handles a new head block comes
   130  	NewChainHead(block *types.Block) error
   131  
   132  	// HandleMsg handles a message from peer
   133  	HandleMsg(chID uint64, src Peer, msgBytes []byte) (bool, error)
   134  
   135  	// SetBroadcaster sets the broadcaster to send message to peers
   136  	SetBroadcaster(Broadcaster)
   137  
   138  	// GetBroadcaster gets the broadcaster to send message to peers
   139  	GetBroadcaster() Broadcaster
   140  
   141  	AddPeer(src Peer)
   142  
   143  	RemovePeer(src Peer)
   144  }
   145  
   146  type EngineStartStop interface {
   147  	// Start starts the engine
   148  	Start(chain ChainReader, currentBlock func() *types.Block, hasBadBlock func(hash common.Hash) bool) error
   149  
   150  	// Stop stops the engine
   151  	Stop() error
   152  }
   153  
   154  // IPBFT is a consensus engine to avoid byzantine failure
   155  type IPBFT interface {
   156  	Engine
   157  
   158  	EngineStartStop
   159  
   160  	ShouldStart() bool
   161  
   162  	IsStarted() bool
   163  
   164  	// Normally Should Start flag will be set depends on the validator set
   165  	// Force Start only set the Should Start Flag to true, when node join the validator before epoch switch
   166  	ForceStart()
   167  
   168  	GetEpoch() *epoch.Epoch
   169  
   170  	SetEpoch(ep *epoch.Epoch)
   171  
   172  	PrivateValidator() common.Address
   173  
   174  	// VerifyHeader checks whether a header conforms to the consensus rules of a given engine.
   175  	VerifyHeaderBeforeConsensus(chain ChainReader, header *types.Header, seal bool) error
   176  }