github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/consensus/consensus.go (about)

     1  // Copyright 2017 The go-simplechain Authors
     2  // This file is part of the go-simplechain library.
     3  //
     4  // The go-simplechain 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-simplechain 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-simplechain 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  	"github.com/bigzoro/my_simplechain/p2p"
    22  	"math/big"
    23  
    24  	"github.com/bigzoro/my_simplechain/common"
    25  	"github.com/bigzoro/my_simplechain/core/state"
    26  	"github.com/bigzoro/my_simplechain/core/types"
    27  	"github.com/bigzoro/my_simplechain/params"
    28  	"github.com/bigzoro/my_simplechain/rpc"
    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  
    53  // Engine is an algorithm agnostic consensus engine.
    54  type Engine interface {
    55  	// Author retrieves the Ethereum address of the account that minted the given
    56  	// block, which may be different from the header's coinbase if a consensus
    57  	// engine is based on signatures.
    58  	Author(header *types.Header) (common.Address, error)
    59  
    60  	// VerifyHeader checks whether a header conforms to the consensus rules of a
    61  	// given engine. Verifying the seal may be done optionally here, or explicitly
    62  	// via the VerifySeal method.
    63  	VerifyHeader(chain ChainReader, header *types.Header, seal bool) error
    64  
    65  	// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
    66  	// concurrently. The method returns a quit channel to abort the operations and
    67  	// a results channel to retrieve the async verifications (the order is that of
    68  	// the input slice).
    69  	VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error)
    70  
    71  	// VerifyUncles verifies that the given block's uncles conform to the consensus
    72  	// rules of a given engine.
    73  	VerifyUncles(chain ChainReader, block *types.Block) error
    74  
    75  	// VerifySeal checks whether the crypto seal on a header is valid according to
    76  	// the consensus rules of the given engine.
    77  	VerifySeal(chain ChainReader, header *types.Header) error
    78  
    79  	// Prepare initializes the consensus fields of a block header according to the
    80  	// rules of a particular engine. The changes are executed inline.
    81  	Prepare(chain ChainReader, header *types.Header) error
    82  
    83  	// Finalize runs any post-transaction state modifications (e.g. block rewards)
    84  	// but does not assemble the block.
    85  	//
    86  	// Note: The block header and state database might be updated to reflect any
    87  	// consensus rules that happen at finalization (e.g. block rewards).
    88  	Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
    89  		uncles []*types.Header, receipts []*types.Receipt) error
    90  
    91  	// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
    92  	// rewards) and assembles the final block.
    93  	//
    94  	// Note: The block header and state database might be updated to reflect any
    95  	// consensus rules that happen at finalization (e.g. block rewards).
    96  	FinalizeAndAssemble(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
    97  		uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error)
    98  
    99  	// Seal generates a new sealing request for the given input block and pushes
   100  	// the result into the given channel.
   101  	//
   102  	// Note, the method returns immediately and will send the result async. More
   103  	// than one result may also be returned depending on the consensus algorithm.
   104  	Seal(chain ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error
   105  
   106  	// SealHash returns the hash of a block prior to it being sealed.
   107  	SealHash(header *types.Header) common.Hash
   108  
   109  	// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
   110  	// that a new block should have.
   111  	CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int
   112  
   113  	// APIs returns the RPC APIs this consensus engine provides.
   114  	APIs(chain ChainReader) []rpc.API
   115  
   116  	// Close terminates any background threads maintained by the consensus engine.
   117  	Close() error
   118  }
   119  
   120  // PoW is a consensus engine based on proof-of-work.
   121  type PoW interface {
   122  	Engine
   123  
   124  	// Hashrate returns the current mining hashrate of a PoW consensus engine.
   125  	Hashrate() float64
   126  }
   127  
   128  type Byzantine interface {
   129  	Engine
   130  
   131  	// Stop stops the engine
   132  	Stop() error
   133  
   134  	// handle p2p msg, return whether engine was handled
   135  	HandleMsg(addr common.Address, msg p2p.Msg) (bool, error)
   136  
   137  	// report new block head is committed to blockchain
   138  	NewChainHead(block *types.Block) error
   139  
   140  	SetBroadcaster(broadcaster Broadcaster)
   141  	IsBlockHashLocked(hash common.Hash) bool
   142  }
   143  
   144  // Istanbul is a consensus engine to avoid byzantine failure
   145  type Istanbul interface {
   146  	Byzantine
   147  }
   148  
   149  type Pbft interface {
   150  	Byzantine
   151  	// Start starts the engine
   152  	Start(chain ChainReader, chainWriter ChainWriter, txPool TxPool, sealer Sealer, currentBlock func() *types.Block, hasBadBlock func(hash common.Hash) bool) error
   153  
   154  	//SetSealer(sealer Sealer)
   155  	//SetTxPool(pool TxPool)
   156  	//SetChainWriter(writer ChainWriter)
   157  
   158  	GetCurrentBlock() *types.Block
   159  }
   160  
   161  type Predictable interface {
   162  	// get block validators and self index(return -1 if not a validator) of current blockchain height
   163  	CurrentValidators() ([]common.Address, int)
   164  }
   165  
   166  type Clique interface {
   167  	Engine
   168  	IsSigner(chain ChainReader, header *types.Header, addr common.Address) bool
   169  	Signers(chain ChainReader, header *types.Header) []common.Address
   170  }