github.com/dominant-strategies/go-quai@v0.28.2/consensus/consensus.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Package consensus implements different Quai consensus engines. 18 package consensus 19 20 import ( 21 "math/big" 22 23 "github.com/dominant-strategies/go-quai/common" 24 "github.com/dominant-strategies/go-quai/core/state" 25 "github.com/dominant-strategies/go-quai/core/types" 26 "github.com/dominant-strategies/go-quai/params" 27 "github.com/dominant-strategies/go-quai/rpc" 28 ) 29 30 // ChainHeaderReader defines a small collection of methods needed to access the local 31 // blockchain during header verification. 32 type ChainHeaderReader 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 // GetTerminiByHash retrieves the termini for a given header hash 49 GetTerminiByHash(hash common.Hash) *types.Termini 50 51 // ProcessingState returns true for slices that are running 52 ProcessingState() bool 53 } 54 55 // ChainReader defines a small collection of methods needed to access the local 56 // blockchain during header and/or uncle verification. 57 type ChainReader interface { 58 ChainHeaderReader 59 60 // GetBlock retrieves a block from the database by hash and number. 61 GetBlock(hash common.Hash, number uint64) *types.Block 62 } 63 64 // Engine is an algorithm agnostic consensus engine. 65 type Engine interface { 66 // Author retrieves the Quai address of the account that minted the given 67 // block, which may be different from the header's coinbase if a consensus 68 // engine is based on signatures. 69 Author(header *types.Header) (common.Address, error) 70 71 // IntrinsicLogS returns the logarithm of the intrinsic entropy reduction of a PoW hash 72 IntrinsicLogS(powHash common.Hash) *big.Int 73 74 // CalcOrder returns the order of the block within the hierarchy of chains 75 CalcOrder(header *types.Header) (*big.Int, int, error) 76 77 // TotalLogS returns the log of the total entropy reduction if the chain since genesis to the given header 78 TotalLogS(header *types.Header) *big.Int 79 80 // TotalLogPhS returns the log of the total entropy reduction if the chain since genesis for a pending header 81 TotalLogPhS(header *types.Header) *big.Int 82 83 // DeltaLogS returns the log of the entropy delta for a chain since its prior coincidence 84 DeltaLogS(header *types.Header) *big.Int 85 86 ComputePowLight(header *types.Header) (mixHash, powHash common.Hash) 87 88 // VerifyHeader checks whether a header conforms to the consensus rules of a 89 // given engine. Verifying the seal may be done optionally here, or explicitly 90 // via the VerifySeal method. 91 VerifyHeader(chain ChainHeaderReader, header *types.Header) error 92 93 // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers 94 // concurrently. The method returns a quit channel to abort the operations and 95 // a results channel to retrieve the async verifications (the order is that of 96 // the input slice). 97 VerifyHeaders(chain ChainHeaderReader, headers []*types.Header) (chan<- struct{}, <-chan error) 98 99 // VerifyUncles verifies that the given block's uncles conform to the consensus 100 // rules of a given engine. 101 VerifyUncles(chain ChainReader, block *types.Block) error 102 103 // Prepare initializes the consensus fields of a block header according to the 104 // rules of a particular engine. The changes are executed inline. 105 Prepare(chain ChainHeaderReader, header *types.Header, parent *types.Header) error 106 107 // Finalize runs any post-transaction state modifications (e.g. block rewards) 108 // but does not assemble the block. 109 // 110 // Note: The block header and state database might be updated to reflect any 111 // consensus rules that happen at finalization (e.g. block rewards). 112 Finalize(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, 113 uncles []*types.Header) 114 115 // FinalizeAndAssemble runs any post-transaction state modifications (e.g. block 116 // rewards) and assembles the final block. 117 // 118 // Note: The block header and state database might be updated to reflect any 119 // consensus rules that happen at finalization (e.g. block rewards). 120 FinalizeAndAssemble(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, etxs []*types.Transaction, manifest types.BlockManifest, receipts []*types.Receipt) (*types.Block, error) 121 122 // Hashrate returns the measured rate of the search invocations 123 // per second over the last minute. 124 Hashrate() float64 125 126 // Seal generates a new sealing request for the given input block and pushes 127 // the result into the given channel. 128 // 129 // Note, the method returns immediately and will send the result async. More 130 // than one result may also be returned depending on the consensus algorithm. 131 Seal(header *types.Header, results chan<- *types.Header, stop <-chan struct{}) error 132 133 // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty 134 // that a new block should have. 135 CalcDifficulty(chain ChainHeaderReader, parent *types.Header) *big.Int 136 137 // IsDomCoincident returns true if this block satisfies the difficulty order 138 // of a dominant chain. If this node does not have a dominant chain (i.e. 139 // if this is a prime node), then the function will always return false. 140 // 141 // Importantly, this check does NOT mean the block is canonical in the 142 // dominant chain, or even that the claimed dominant difficulty is valid. 143 IsDomCoincident(chain ChainHeaderReader, header *types.Header) bool 144 145 // VerifySeal computes the PowHash and checks if work meets the difficulty 146 // requirement specified in header 147 VerifySeal(header *types.Header) (common.Hash, error) 148 149 // APIs returns the RPC APIs this consensus engine provides. 150 APIs(chain ChainHeaderReader) []rpc.API 151 152 // Close terminates any background threads maintained by the consensus engine. 153 Close() error 154 } 155 156 func TargetToDifficulty(target *big.Int) *big.Int { 157 big2e256 := new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0)) // 2^256 158 return new(big.Int).Div(big2e256, target) 159 } 160 161 func DifficultyToTarget(difficulty *big.Int) *big.Int { 162 return TargetToDifficulty(difficulty) 163 } 164 165 // PoW is a consensus engine based on proof-of-work. 166 type PoW interface { 167 Engine 168 169 // Hashrate returns the current mining hashrate of a PoW consensus engine. 170 Hashrate() float64 171 }