github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/consensus/consensus.go (about) 1 // Package consensus implements different Ethereum consensus engines. 2 package consensus 3 4 import ( 5 "math/big" 6 7 "github.com/quickchainproject/quickchain/common" 8 "github.com/quickchainproject/quickchain/core/state" 9 "github.com/quickchainproject/quickchain/core/types" 10 "github.com/quickchainproject/quickchain/p2p" 11 "github.com/quickchainproject/quickchain/params" 12 "github.com/quickchainproject/quickchain/rpc" 13 ) 14 15 // ChainReader defines a small collection of methods needed to access the local 16 // blockchain during header and/or uncle verification. 17 type ChainReader interface { 18 // Config retrieves the blockchain's chain configuration. 19 Config() *params.ChainConfig 20 21 // CurrentHeader retrieves the current header from the local chain. 22 CurrentHeader() *types.Header 23 24 // GetHeader retrieves a block header from the database by hash and number. 25 GetHeader(hash common.Hash, number uint64) *types.Header 26 27 // GetHeaderByNumber retrieves a block header from the database by number. 28 GetHeaderByNumber(number uint64) *types.Header 29 30 // GetHeaderByHash retrieves a block header from the database by its hash. 31 GetHeaderByHash(hash common.Hash) *types.Header 32 33 // GetBlock retrieves a block from the database by hash and number. 34 GetBlock(hash common.Hash, number uint64) *types.Block 35 } 36 37 // Engine is an algorithm agnostic consensus engine. 38 type Engine interface { 39 // Author retrieves the Ethereum address of the account that minted the given 40 // block, which may be different from the header's coinbase if a consensus 41 // engine is based on signatures. 42 Author(header *types.Header) (common.Address, error) 43 44 // VerifyHeader checks whether a header conforms to the consensus rules of a 45 // given engine. Verifying the seal may be done optionally here, or explicitly 46 // via the VerifySeal method. 47 VerifyHeader(chain ChainReader, header *types.Header, seal bool) error 48 49 // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers 50 // concurrently. The method returns a quit channel to abort the operations and 51 // a results channel to retrieve the async verifications (the order is that of 52 // the input slice). 53 VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) 54 55 // VerifyUncles verifies that the given block's uncles conform to the consensus 56 // rules of a given engine. 57 VerifyUncles(chain ChainReader, block *types.Block) error 58 59 // VerifySeal checks whether the crypto seal on a header is valid according to 60 // the consensus rules of the given engine. 61 VerifySeal(chain ChainReader, header *types.Header) error 62 63 // Prepare initializes the consensus fields of a block header according to the 64 // rules of a particular engine. The changes are executed inline. 65 Prepare(chain ChainReader, header *types.Header) error 66 67 // Finalize runs any post-transaction state modifications (e.g. block rewards) 68 // and assembles the final block. 69 // Note: The block header and state database might be updated to reflect any 70 // consensus rules that happen at finalization (e.g. block rewards). 71 Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, 72 uncles []*types.Header, receipts []*types.Receipt, dposContext *types.DposContext) (*types.Block, error) 73 74 // Seal generates a new block for the given input block with the local miner's 75 // seal place on top. 76 Seal(chain ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) 77 78 // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty 79 // that a new block should have. 80 CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int 81 82 // APIs returns the RPC APIs this consensus engine provides. 83 APIs(chain ChainReader) []rpc.API 84 85 // Judge the create contract Transaction from a no authorized account for clique engine. 86 JudgeTx(chain ChainReader, header *types.Header, tx *types.Transaction, from common.Address) error 87 } 88 89 // Handler should be implemented is the consensus needs to handle and send peer's message 90 type Handler interface { 91 // NewChainHead handles a new head block comes 92 NewChainHead() error 93 94 // HandleMsg handles a message from peer 95 HandleMsg(address common.Address, data p2p.Msg) (bool, error) 96 97 // SetBroadcaster sets the broadcaster to send message to peers 98 SetBroadcaster(Broadcaster) 99 } 100 101 // PoW is a consensus engine based on proof-of-work. 102 type PoW interface { 103 Engine 104 105 // Hashrate returns the current mining hashrate of a PoW consensus engine. 106 Hashrate() float64 107 } 108 109 // BFT is a consensus engine to avoid byzantine failure 110 type BFT interface { 111 Engine 112 113 // Start starts the engine 114 Start(chain ChainReader, currentBlock func() *types.Block, hasBadBlock func(hash common.Hash) bool) error 115 116 // Stop stops the engine 117 Stop() error 118 } 119 120 // DBFT is a consensus engine to avoid byzantine failure 121 type DBFT interface { 122 Engine 123 124 // Start starts the engine 125 Start(chain ChainReader, currentBlock func() *types.Block, hasBadBlock func(hash common.Hash) bool) error 126 127 // Stop stops the engine 128 Stop() error 129 }