github.com/phillinzzz/newBsc@v1.1.6/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 "github.com/phillinzzz/newBsc/consensus" 23 "github.com/phillinzzz/newBsc/core/state" 24 "github.com/phillinzzz/newBsc/core/types" 25 "github.com/phillinzzz/newBsc/params" 26 "github.com/phillinzzz/newBsc/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 66 validateFuns := []func() error{ 67 func() error { 68 if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) { 69 return ErrKnownBlock 70 } 71 return nil 72 }, 73 func() error { 74 if hash := types.DeriveSha(block.Transactions(), trie.NewStackTrie(nil)); hash != header.TxHash { 75 return fmt.Errorf("transaction root hash mismatch: have %x, want %x", hash, header.TxHash) 76 } 77 return nil 78 }, 79 func() error { 80 if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) { 81 if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) { 82 return consensus.ErrUnknownAncestor 83 } 84 return consensus.ErrPrunedAncestor 85 } 86 return nil 87 }, 88 } 89 validateRes := make(chan error, len(validateFuns)) 90 for _, f := range validateFuns { 91 tmpFunc := f 92 go func() { 93 validateRes <- tmpFunc() 94 }() 95 } 96 for i := 0; i < len(validateFuns); i++ { 97 r := <-validateRes 98 if r != nil { 99 return r 100 } 101 } 102 return nil 103 } 104 105 // ValidateState validates the various changes that happen after a state 106 // transition, such as amount of used gas, the receipt roots and the state root 107 // itself. ValidateState returns a database batch if the validation was a success 108 // otherwise nil and an error is returned. 109 func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64) error { 110 header := block.Header() 111 if block.GasUsed() != usedGas { 112 return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), usedGas) 113 } 114 // Validate the received block's bloom with the one derived from the generated receipts. 115 // For valid blocks this should always validate to true. 116 validateFuns := []func() error{ 117 func() error { 118 rbloom := types.CreateBloom(receipts) 119 if rbloom != header.Bloom { 120 return fmt.Errorf("invalid bloom (remote: %x local: %x)", header.Bloom, rbloom) 121 } 122 return nil 123 }, 124 func() error { 125 receiptSha := types.DeriveSha(receipts, trie.NewStackTrie(nil)) 126 if receiptSha != header.ReceiptHash { 127 return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha) 128 } else { 129 return nil 130 } 131 }, 132 func() error { 133 if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root { 134 return fmt.Errorf("invalid merkle root (remote: %x local: %x)", header.Root, root) 135 } else { 136 return nil 137 } 138 }, 139 } 140 validateRes := make(chan error, len(validateFuns)) 141 for _, f := range validateFuns { 142 tmpFunc := f 143 go func() { 144 validateRes <- tmpFunc() 145 }() 146 } 147 for i := 0; i < len(validateFuns); i++ { 148 r := <-validateRes 149 if r != nil { 150 return r 151 } 152 } 153 return nil 154 } 155 156 // CalcGasLimit computes the gas limit of the next block after parent. It aims 157 // to keep the baseline gas above the provided floor, and increase it towards the 158 // ceil if the blocks are full. If the ceil is exceeded, it will always decrease 159 // the gas allowance. 160 func CalcGasLimit(parent *types.Block, gasFloor, gasCeil uint64) uint64 { 161 // contrib = (parentGasUsed * 3 / 2) / 256 162 contrib := (parent.GasUsed() + parent.GasUsed()/2) / params.GasLimitBoundDivisor 163 164 // decay = parentGasLimit / 256 -1 165 decay := parent.GasLimit()/params.GasLimitBoundDivisor - 1 166 167 /* 168 strategy: gasLimit of block-to-mine is set based on parent's 169 gasUsed value. if parentGasUsed > parentGasLimit * (2/3) then we 170 increase it, otherwise lower it (or leave it unchanged if it's right 171 at that usage) the amount increased/decreased depends on how far away 172 from parentGasLimit * (2/3) parentGasUsed is. 173 */ 174 limit := parent.GasLimit() - decay + contrib 175 if limit < params.MinGasLimit { 176 limit = params.MinGasLimit 177 } 178 // If we're outside our allowed gas range, we try to hone towards them 179 if limit < gasFloor { 180 limit = parent.GasLimit() + decay 181 if limit > gasFloor { 182 limit = gasFloor 183 } 184 } else if limit > gasCeil { 185 limit = parent.GasLimit() - decay 186 if limit < gasCeil { 187 limit = gasCeil 188 } 189 } 190 return limit 191 }