github.com/Elemental-core/elementalcore@v0.0.0-20191206075037-63891242267a/consensus/consensus.go (about) 1 // Copyright 2017 The elementalcore Authors 2 // This file is part of the elementalcore library. 3 // 4 // The elementalcore 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 elementalcore 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 elementalcore 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/Elemental-core/elementalcore/common" 22 "github.com/Elemental-core/elementalcore/core/state" 23 "github.com/Elemental-core/elementalcore/core/types" 24 "github.com/Elemental-core/elementalcore/params" 25 "github.com/Elemental-core/elementalcore/rpc" 26 ) 27 28 // ChainReader defines a small collection of methods needed to access the local 29 // blockchain during header and/or uncle verification. 30 type ChainReader interface { 31 // Config retrieves the blockchain's chain configuration. 32 Config() *params.ChainConfig 33 34 // CurrentHeader retrieves the current header from the local chain. 35 CurrentHeader() *types.Header 36 37 // GetHeader retrieves a block header from the database by hash and number. 38 GetHeader(hash common.Hash, number uint64) *types.Header 39 40 // GetHeaderByNumber retrieves a block header from the database by number. 41 GetHeaderByNumber(number uint64) *types.Header 42 43 // GetHeaderByHash retrieves a block header from the database by its hash. 44 GetHeaderByHash(hash common.Hash) *types.Header 45 46 // GetBlock retrieves a block from the database by hash and number. 47 GetBlock(hash common.Hash, number uint64) *types.Block 48 } 49 50 // Engine is an algorithm agnostic consensus engine. 51 type Engine interface { 52 // Author retrieves the Ethereum address of the account that minted the given 53 // block, which may be different from the header's coinbase if a consensus 54 // engine is based on signatures. 55 Author(header *types.Header) (common.Address, error) 56 57 // Coinbase return the benefits owner of the given block 58 Coinbase(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 // and assembles the final block. 85 // Note: The block header and state database might be updated to reflect any 86 // consensus rules that happen at finalization (e.g. block rewards). 87 Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, 88 uncles []*types.Header, receipts []*types.Receipt, dposContext *types.DposContext) (*types.Block, error) 89 90 // Seal generates a new block for the given input block with the local miner's 91 // seal place on top. 92 Seal(chain ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) 93 94 // APIs returns the RPC APIs this consensus engine provides. 95 APIs(chain ChainReader) []rpc.API 96 } 97 98 // PoW is a consensus engine based on proof-of-work. 99 type PoW interface { 100 Engine 101 102 // Hashrate returns the current mining hashrate of a PoW consensus engine. 103 Hashrate() float64 104 }