github.com/elastos/Elastos.ELA.SideChain.ETH@v0.2.2/consensus/consensus.go (about) 1 // Copyright 2017 The Elastos.ELA.SideChain.ESC Authors 2 // This file is part of the Elastos.ELA.SideChain.ESC library. 3 // 4 // The Elastos.ELA.SideChain.ESC 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 Elastos.ELA.SideChain.ESC 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 Elastos.ELA.SideChain.ESC 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/elastos/Elastos.ELA.SideChain.ESC/common" 24 "github.com/elastos/Elastos.ELA.SideChain.ESC/core/state" 25 "github.com/elastos/Elastos.ELA.SideChain.ESC/core/types" 26 "github.com/elastos/Elastos.ELA.SideChain.ESC/params" 27 "github.com/elastos/Elastos.ELA.SideChain.ESC/rpc" 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 // IsDangerChain return whether to many evil singers. 52 IsDangerChain() bool 53 } 54 55 // Engine is an algorithm agnostic consensus engine. 56 type Engine interface { 57 // Author retrieves the Ethereum address of the account that minted the given 58 // block, which may be different from the header's coinbase if a consensus 59 // engine is based on signatures. 60 Author(header *types.Header) (common.Address, error) 61 62 // VerifyHeader checks whether a header conforms to the consensus rules of a 63 // given engine. Verifying the seal may be done optionally here, or explicitly 64 // via the VerifySeal method. 65 VerifyHeader(chain ChainReader, header *types.Header, seal bool) error 66 67 // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers 68 // concurrently. The method returns a quit channel to abort the operations and 69 // a results channel to retrieve the async verifications (the order is that of 70 // the input slice). 71 VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) 72 73 // VerifyUncles verifies that the given block's uncles conform to the consensus 74 // rules of a given engine. 75 VerifyUncles(chain ChainReader, block *types.Block) error 76 77 // VerifySeal checks whether the crypto seal on a header is valid according to 78 // the consensus rules of the given engine. 79 VerifySeal(chain ChainReader, header *types.Header) error 80 81 // Prepare initializes the consensus fields of a block header according to the 82 // rules of a particular engine. The changes are executed inline. 83 Prepare(chain ChainReader, header *types.Header) error 84 85 // Finalize runs any post-transaction state modifications (e.g. block rewards) 86 // but does not assemble the block. 87 // 88 // Note: The block header and state database might be updated to reflect any 89 // consensus rules that happen at finalization (e.g. block rewards). 90 Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, 91 uncles []*types.Header) 92 93 // FinalizeAndAssemble runs any post-transaction state modifications (e.g. block 94 // rewards) and assembles the final block. 95 // 96 // Note: The block header and state database might be updated to reflect any 97 // consensus rules that happen at finalization (e.g. block rewards). 98 FinalizeAndAssemble(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, 99 uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) 100 101 // Seal generates a new sealing request for the given input block and pushes 102 // the result into the given channel. 103 // 104 // Note, the method returns immediately and will send the result async. More 105 // than one result may also be returned depending on the consensus algorithm. 106 Seal(chain ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error 107 108 // SealHash returns the hash of a block prior to it being sealed. 109 SealHash(header *types.Header) common.Hash 110 111 // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty 112 // that a new block should have. 113 CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int 114 115 // APIs returns the RPC APIs this consensus engine provides. 116 APIs(chain ChainReader) []rpc.API 117 118 // Close terminates any background threads maintained by the consensus engine. 119 Close() error 120 121 // SignersCount returns signers count by this engine 122 SignersCount() int 123 124 IsInBlockPool(hash common.Hash) bool 125 126 GetCurrentProducers() [][]byte 127 } 128 129 // PoW is a consensus engine based on proof-of-work. 130 type PoW interface { 131 Engine 132 133 // Hashrate returns the current mining hashrate of a PoW consensus engine. 134 Hashrate() float64 135 } 136 137 type IPbftEngine interface { 138 Engine 139 GetPbftConfig() params.PbftConfig 140 CurrentBlock() *types.Block 141 GetBlockByHeight(height uint64) *types.Block 142 }