gitlab.com/flarenetwork/coreth@v0.1.1/consensus/consensus.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2017 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 // Package consensus implements different Ethereum consensus engines. 28 package consensus 29 30 import ( 31 "math/big" 32 33 "github.com/ethereum/go-ethereum/common" 34 "gitlab.com/flarenetwork/coreth/core/state" 35 "gitlab.com/flarenetwork/coreth/core/types" 36 "gitlab.com/flarenetwork/coreth/params" 37 "gitlab.com/flarenetwork/coreth/rpc" 38 ) 39 40 // ChainHeaderReader defines a small collection of methods needed to access the local 41 // blockchain during header verification. 42 type ChainHeaderReader interface { 43 // Config retrieves the blockchain's chain configuration. 44 Config() *params.ChainConfig 45 46 // CurrentHeader retrieves the current header from the local chain. 47 CurrentHeader() *types.Header 48 49 // GetHeader retrieves a block header from the database by hash and number. 50 GetHeader(hash common.Hash, number uint64) *types.Header 51 52 // GetHeaderByNumber retrieves a block header from the database by number. 53 GetHeaderByNumber(number uint64) *types.Header 54 55 // GetHeaderByHash retrieves a block header from the database by its hash. 56 GetHeaderByHash(hash common.Hash) *types.Header 57 } 58 59 // ChainReader defines a small collection of methods needed to access the local 60 // blockchain during header and/or uncle verification. 61 type ChainReader interface { 62 ChainHeaderReader 63 64 // GetBlock retrieves a block from the database by hash and number. 65 GetBlock(hash common.Hash, number uint64) *types.Block 66 } 67 68 // Engine is an algorithm agnostic consensus engine. 69 type Engine interface { 70 // Author retrieves the Ethereum address of the account that minted the given 71 // block, which may be different from the header's coinbase if a consensus 72 // engine is based on signatures. 73 Author(header *types.Header) (common.Address, error) 74 75 // VerifyHeader checks whether a header conforms to the consensus rules of a 76 // given engine. Verifying the seal may be done optionally here, or explicitly 77 // via the VerifySeal method. 78 VerifyHeader(chain ChainHeaderReader, header *types.Header) error 79 80 // VerifyUncles verifies that the given block's uncles conform to the consensus 81 // rules of a given engine. 82 VerifyUncles(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 ChainHeaderReader, 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 ChainHeaderReader, header *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 ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, receipts []*types.Receipt, 98 uncles []*types.Header) error 99 100 // FinalizeAndAssemble runs any post-transaction state modifications (e.g. block 101 // rewards) and assembles the final block. 102 // 103 // Note: The block header and state database might be updated to reflect any 104 // consensus rules that happen at finalization (e.g. block rewards). 105 FinalizeAndAssemble(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, 106 uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) 107 108 // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty 109 // that a new block should have. 110 CalcDifficulty(chain ChainHeaderReader, time uint64, parent *types.Header) *big.Int 111 112 // APIs returns the RPC APIs this consensus engine provides. 113 APIs(chain ChainHeaderReader) []rpc.API 114 115 // Close terminates any background threads maintained by the consensus engine. 116 Close() error 117 118 ExtraStateChange(block *types.Block, statedb *state.StateDB) error 119 }