github.com/ebakus/go-ebakus@v1.0.5-0.20200520105415-dbccef9ec421/consensus/consensus.go (about) 1 // Copyright 2019 The ebakus/go-ebakus Authors 2 // This file is part of the ebakus/go-ebakus library. 3 // 4 // The ebakus/go-ebakus 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 ebakus/go-ebakus 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 ebakus/go-ebakus library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Package consensus implements different Ebakus consensus engines. 18 package consensus 19 20 import ( 21 "math/big" 22 23 "github.com/ebakus/go-ebakus/common" 24 "github.com/ebakus/go-ebakus/core/state" 25 "github.com/ebakus/go-ebakus/core/types" 26 "github.com/ebakus/go-ebakus/params" 27 "github.com/ebakus/go-ebakus/rpc" 28 "github.com/ebakus/ebakusdb" 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 // CurrentHeader retrieves the current block from the local chain. 41 CurrentBlock() *types.Block 42 43 // GetHeader retrieves a block header from the database by hash and number. 44 GetHeader(hash common.Hash, number uint64) *types.Header 45 46 // GetHeaderByNumber retrieves a block header from the database by number. 47 GetHeaderByNumber(number uint64) *types.Header 48 49 // GetHeaderByHash retrieves a block header from the database by its hash. 50 GetHeaderByHash(hash common.Hash) *types.Header 51 52 // GetBlock retrieves a block from the database by hash and number. 53 GetBlock(hash common.Hash, number uint64) *types.Block 54 55 // StateAt retrieves the state with a give root 56 StateAt(hash common.Hash) (*state.StateDB, error) 57 58 // EbakusStateAt retrieves the ebakus state with a given block 59 EbakusStateAt(hash common.Hash, number uint64) (*ebakusdb.Snapshot, error) 60 } 61 62 // Engine is an algorithm agnostic consensus engine. 63 type Engine interface { 64 // Author retrieves the Ebakus address of the account that minted the given 65 // block, which may be different from the header's coinbase if a consensus 66 // engine is based on signatures. 67 Author(header *types.Header) (common.Address, error) 68 69 // VerifyHeader checks whether a header conforms to the consensus rules of a 70 // given engine. Verifying the seal may be done optionally here, or explicitly 71 // via the VerifySeal method. 72 VerifyHeader(chain ChainReader, header *types.Header, seal bool) error 73 74 // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers 75 // concurrently. The method returns a quit channel to abort the operations and 76 // a results channel to retrieve the async verifications (the order is that of 77 // the input slice). 78 VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) 79 80 // VerifyBlock verifies that the given block conform to the consensus 81 // rules of a given engine. 82 VerifyBlock(chain ChainReader, block *types.Block) error 83 84 // VerifySeal checks whether the crypto seal on a header is valid according to 85 // the consensus rules of the given engine. 86 VerifySeal(chain ChainReader, header *types.Header) error 87 88 // Prepare initializes the consensus fields of a block header according to the 89 // rules of a particular engine. The changes are executed inline. 90 Prepare(chain ChainReader, stop <-chan struct{}) (*types.Block, *types.Header, error) 91 92 // Finalize runs any post-transaction state modifications (e.g. block rewards) 93 // but does not assemble the block. 94 // 95 // Note: The block header and state database might be updated to reflect any 96 // consensus rules that happen at finalization (e.g. block rewards). 97 Finalize(chain ChainReader, header *types.Header, state *state.StateDB, ebakusState *ebakusdb.Snapshot, coinbase common.Address, txs []*types.Transaction) 98 99 // FinalizeAndAssemble runs any post-transaction state modifications (e.g. block 100 // rewards) and assembles the final block. 101 // 102 // Note: The block header and state database might be updated to reflect any 103 // consensus rules that happen at finalization (e.g. block rewards). 104 FinalizeAndAssemble(chain ChainReader, header *types.Header, state *state.StateDB, ebakusState *ebakusdb.Snapshot, coinbase common.Address, txs []*types.Transaction, receipts []*types.Receipt) (*types.Block, error) 105 106 // Seal generates a new sealing request for the given input block and pushes 107 // the result into the given channel. 108 // 109 // Note, the method returns immediately and will send the result async. More 110 // than one result may also be returned depending on the consensus algorithm. 111 Seal(chain ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error 112 113 // SealHash returns the hash of a block prior to it being sealed. 114 SealHash(header *types.Header) common.Hash 115 116 // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty 117 // that a new block should have. 118 CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int 119 120 // APIs returns the RPC APIs this consensus engine provides. 121 APIs(chain ChainReader) []rpc.API 122 123 // Close terminates any background threads maintained by the consensus engine. 124 Close() error 125 } 126 127 // PoW is a consensus engine based on proof-of-work. 128 type PoW interface { 129 Engine 130 131 // Hashrate returns the current mining hashrate of a PoW consensus engine. 132 Hashrate() float64 133 }