github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/block_validator.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package core 13 14 import ( 15 "fmt" 16 17 "github.com/Sberex/go-sberex/consensus" 18 "github.com/Sberex/go-sberex/core/state" 19 "github.com/Sberex/go-sberex/core/types" 20 "github.com/Sberex/go-sberex/params" 21 ) 22 23 // BlockValidator is responsible for validating block headers, uncles and 24 // processed state. 25 // 26 // BlockValidator implements Validator. 27 type BlockValidator struct { 28 config *params.ChainConfig // Chain configuration options 29 bc *BlockChain // Canonical block chain 30 engine consensus.Engine // Consensus engine used for validating 31 } 32 33 // NewBlockValidator returns a new block validator which is safe for re-use 34 func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine) *BlockValidator { 35 validator := &BlockValidator{ 36 config: config, 37 engine: engine, 38 bc: blockchain, 39 } 40 return validator 41 } 42 43 // ValidateBody validates the given block's uncles and verifies the the block 44 // header's transaction and uncle roots. The headers are assumed to be already 45 // validated at this point. 46 func (v *BlockValidator) ValidateBody(block *types.Block) error { 47 // Check whether the block's known, and if not, that it's linkable 48 if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) { 49 return ErrKnownBlock 50 } 51 if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) { 52 if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) { 53 return consensus.ErrUnknownAncestor 54 } 55 return consensus.ErrPrunedAncestor 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()); hash != header.TxHash { 66 return fmt.Errorf("transaction root hash mismatch: have %x, want %x", hash, header.TxHash) 67 } 68 return nil 69 } 70 71 // ValidateState validates the various changes that happen after a state 72 // transition, such as amount of used gas, the receipt roots and the state root 73 // itself. ValidateState returns a database batch if the validation was a success 74 // otherwise nil and an error is returned. 75 func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64) error { 76 header := block.Header() 77 if block.GasUsed() != usedGas { 78 return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), usedGas) 79 } 80 // Validate the received block's bloom with the one derived from the generated receipts. 81 // For valid blocks this should always validate to true. 82 rbloom := types.CreateBloom(receipts) 83 if rbloom != header.Bloom { 84 return fmt.Errorf("invalid bloom (remote: %x local: %x)", header.Bloom, rbloom) 85 } 86 // Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, R1]])) 87 receiptSha := types.DeriveSha(receipts) 88 if receiptSha != header.ReceiptHash { 89 return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha) 90 } 91 // Validate the state root against the received state root and throw 92 // an error if they don't match. 93 if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root { 94 return fmt.Errorf("invalid merkle root (remote: %x local: %x)", header.Root, root) 95 } 96 return nil 97 } 98 99 // CalcGasLimit computes the gas limit of the next block after parent. 100 // This is miner strategy, not consensus protocol. 101 func CalcGasLimit(parent *types.Block) uint64 { 102 // contrib = (parentGasUsed * 3 / 2) / 256 103 contrib := (parent.GasUsed() + parent.GasUsed()/2) / params.GasLimitBoundDivisor 104 105 // decay = parentGasLimit / 256 - 1 106 decay := parent.GasLimit()/params.GasLimitBoundDivisor - 1 107 108 /* 109 strategy: gasLimit of block-to-mine is set based on parent's 110 gasUsed value. if parentGasUsed > parentGasLimit * (2/3) then we 111 increase it, otherwise lower it (or leave it unchanged if it's right 112 at that usage) the amount increased/decreased depends on how far away 113 from parentGasLimit * (2/3) parentGasUsed is. 114 */ 115 limit := parent.GasLimit() - decay + contrib 116 if limit < params.MinGasLimit { 117 limit = params.MinGasLimit 118 } 119 // however, if we're now below the target (TargetGasLimit) we increase the 120 // limit as much as we can (parentGasLimit / 256 - 1) 121 if limit < params.TargetGasLimit { 122 limit = parent.GasLimit() + decay 123 if limit > params.TargetGasLimit { 124 limit = params.TargetGasLimit 125 } 126 } 127 return limit 128 }