github.com/codingfuture/orig-energi3@v0.8.4/consensus/consensus.go (about) 1 // Copyright 2018 The Energi Core Authors 2 // Copyright 2017 The go-ethereum Authors 3 // This file is part of the Energi Core library. 4 // 5 // The Energi Core library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The Energi Core library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>. 17 18 // Package consensus implements different Ethereum consensus engines. 19 package consensus 20 21 import ( 22 "math/big" 23 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/core/state" 26 "github.com/ethereum/go-ethereum/core/types" 27 "github.com/ethereum/go-ethereum/params" 28 "github.com/ethereum/go-ethereum/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 // Retrieve or calculate block state 53 CalculateBlockState(hash common.Hash, number uint64) *state.StateDB 54 } 55 56 // Engine is an algorithm agnostic consensus engine. 57 type Engine interface { 58 // Author retrieves the Ethereum address of the account that minted the given 59 // block, which may be different from the header's coinbase if a consensus 60 // engine is based on signatures. 61 Author(header *types.Header) (common.Address, error) 62 63 // VerifyHeader checks whether a header conforms to the consensus rules of a 64 // given engine. Verifying the seal may be done optionally here, or explicitly 65 // via the VerifySeal method. 66 VerifyHeader(chain ChainReader, header *types.Header, seal bool) error 67 68 // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers 69 // concurrently. The method returns a quit channel to abort the operations and 70 // a results channel to retrieve the async verifications (the order is that of 71 // the input slice). 72 VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error, chan<- bool) 73 74 // VerifyUncles verifies that the given block's uncles conform to the consensus 75 // rules of a given engine. 76 VerifyUncles(chain ChainReader, block *types.Block) error 77 78 // VerifySeal checks whether the crypto seal on a header is valid according to 79 // the consensus rules of the given engine. 80 VerifySeal(chain ChainReader, header *types.Header) error 81 82 // Prepare initializes the consensus fields of a block header according to the 83 // rules of a particular engine. The changes are executed inline. 84 Prepare(chain ChainReader, header *types.Header) error 85 86 // Finalize runs any post-transaction state modifications (e.g. block rewards) 87 // and assembles the final block. 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, receipts []*types.Receipt) (*types.Block, []*types.Receipt, error) 92 93 // Seal generates a new sealing request for the given input block and pushes 94 // the result into the given channel. 95 // 96 // Note, the method returns immediately and will send the result async. More 97 // than one result may also be returned depending on the consensus algorithm. 98 Seal(chain ChainReader, block *types.Block, results chan<- *SealResult, stop <-chan struct{}) error 99 100 // SealHash returns the hash of a block prior to it being sealed. 101 SealHash(header *types.Header) common.Hash 102 103 // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty 104 // that a new block should have. 105 CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int 106 107 // APIs returns the RPC APIs this consensus engine provides. 108 APIs(chain ChainReader) []rpc.API 109 110 // Close terminates any background threads maintained by the consensus engine. 111 Close() error 112 } 113 114 // PoW is a consensus engine based on proof-of-work. 115 type PoW interface { 116 Engine 117 118 // Hashrate returns the current mining hashrate of a PoW consensus engine. 119 Hashrate() float64 120 } 121 122 type SealResult struct { 123 Block *types.Block 124 NewState *state.StateDB 125 Receipts types.Receipts 126 } 127 128 func NewSealResult(block *types.Block, state *state.StateDB, receipts types.Receipts) *SealResult { 129 return &SealResult{ 130 Block: block, 131 NewState: state, 132 Receipts: receipts, 133 } 134 }