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