gitee.com/liu-zhao234568/cntest@v1.0.0/core/block_validator.go (about) 1 // Copyright 2015 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 core 18 19 import ( 20 "fmt" 21 22 "gitee.com/liu-zhao234568/cntest/consensus" 23 "gitee.com/liu-zhao234568/cntest/core/state" 24 "gitee.com/liu-zhao234568/cntest/core/types" 25 "gitee.com/liu-zhao234568/cntest/params" 26 "gitee.com/liu-zhao234568/cntest/trie" 27 ) 28 29 // BlockValidator is responsible for validating block headers, uncles and 30 // processed state. 31 // 32 // BlockValidator implements Validator. 33 type BlockValidator struct { 34 config *params.ChainConfig // Chain configuration options 35 bc *BlockChain // Canonical block chain 36 engine consensus.Engine // Consensus engine used for validating 37 } 38 39 // NewBlockValidator returns a new block validator which is safe for re-use 40 func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine) *BlockValidator { 41 validator := &BlockValidator{ 42 config: config, 43 engine: engine, 44 bc: blockchain, 45 } 46 return validator 47 } 48 49 // ValidateBody validates the given block's uncles and verifies the block 50 // header's transaction and uncle roots. The headers are assumed to be already 51 // validated at this point. 52 func (v *BlockValidator) ValidateBody(block *types.Block) error { 53 // Check whether the block's known, and if not, that it's linkable 54 if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) { 55 return ErrKnownBlock 56 } 57 // Header validity is known at this point, check the uncles and transactions 58 header := block.Header() 59 if err := v.engine.VerifyUncles(v.bc, block); err != nil { 60 return err 61 } 62 if hash := types.CalcUncleHash(block.Uncles()); hash != header.UncleHash { 63 return fmt.Errorf("uncle root hash mismatch: have %x, want %x", hash, header.UncleHash) 64 } 65 if hash := types.DeriveSha(block.Transactions(), trie.NewStackTrie(nil)); hash != header.TxHash { 66 return fmt.Errorf("transaction root hash mismatch: have %x, want %x", hash, header.TxHash) 67 } 68 if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) { 69 if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) { 70 return consensus.ErrUnknownAncestor 71 } 72 return consensus.ErrPrunedAncestor 73 } 74 return nil 75 } 76 77 // ValidateState validates the various changes that happen after a state 78 // transition, such as amount of used gas, the receipt roots and the state root 79 // itself. ValidateState returns a database batch if the validation was a success 80 // otherwise nil and an error is returned. 81 func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64) error { 82 header := block.Header() 83 if block.GasUsed() != usedGas { 84 return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), usedGas) 85 } 86 // Validate the received block's bloom with the one derived from the generated receipts. 87 // For valid blocks this should always validate to true. 88 rbloom := types.CreateBloom(receipts) 89 if rbloom != header.Bloom { 90 return fmt.Errorf("invalid bloom (remote: %x local: %x)", header.Bloom, rbloom) 91 } 92 // Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, Rn]])) 93 receiptSha := types.DeriveSha(receipts, trie.NewStackTrie(nil)) 94 if receiptSha != header.ReceiptHash { 95 return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha) 96 } 97 // Validate the state root against the received state root and throw 98 // an error if they don't match. 99 if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root { 100 return fmt.Errorf("invalid merkle root (remote: %x local: %x)", header.Root, root) 101 } 102 return nil 103 } 104 105 // CalcGasLimit computes the gas limit of the next block after parent. It aims 106 // to keep the baseline gas above the provided floor, and increase it towards the 107 // ceil if the blocks are full. If the ceil is exceeded, it will always decrease 108 // the gas allowance. 109 func CalcGasLimit(parentGasUsed, parentGasLimit, gasFloor, gasCeil uint64) uint64 { 110 // contrib = (parentGasUsed * 3 / 2) / 1024 111 contrib := (parentGasUsed + parentGasUsed/2) / params.GasLimitBoundDivisor 112 113 // decay = parentGasLimit / 1024 -1 114 decay := parentGasLimit/params.GasLimitBoundDivisor - 1 115 116 /* 117 strategy: gasLimit of block-to-mine is set based on parent's 118 gasUsed value. if parentGasUsed > parentGasLimit * (2/3) then we 119 increase it, otherwise lower it (or leave it unchanged if it's right 120 at that usage) the amount increased/decreased depends on how far away 121 from parentGasLimit * (2/3) parentGasUsed is. 122 */ 123 limit := parentGasLimit - decay + contrib 124 if limit < params.MinGasLimit { 125 limit = params.MinGasLimit 126 } 127 // If we're outside our allowed gas range, we try to hone towards them 128 if limit < gasFloor { 129 limit = parentGasLimit + decay 130 if limit > gasFloor { 131 limit = gasFloor 132 } 133 } else if limit > gasCeil { 134 limit = parentGasLimit - decay 135 if limit < gasCeil { 136 limit = gasCeil 137 } 138 } 139 return limit 140 } 141 142 // CalcGasLimit1559 calculates the next block gas limit under 1559 rules. 143 func CalcGasLimit1559(parentGasLimit, desiredLimit uint64) uint64 { 144 delta := parentGasLimit/params.GasLimitBoundDivisor - 1 145 limit := parentGasLimit 146 if desiredLimit < params.MinGasLimit { 147 desiredLimit = params.MinGasLimit 148 } 149 // If we're outside our allowed gas range, we try to hone towards them 150 if limit < desiredLimit { 151 limit = parentGasLimit + delta 152 if limit > desiredLimit { 153 limit = desiredLimit 154 } 155 return limit 156 } 157 if limit > desiredLimit { 158 limit = parentGasLimit - delta 159 if limit < desiredLimit { 160 limit = desiredLimit 161 } 162 } 163 return limit 164 }