github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.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/vntchain/go-vnt/consensus" 23 "github.com/vntchain/go-vnt/core/state" 24 "github.com/vntchain/go-vnt/core/types" 25 "github.com/vntchain/go-vnt/params" 26 ) 27 28 // BlockValidator is responsible for validating block headers and 29 // processed state. 30 // 31 // BlockValidator implements Validator. 32 type BlockValidator struct { 33 config *params.ChainConfig // Chain configuration options 34 bc *BlockChain // Canonical block chain 35 engine consensus.Engine // Consensus engine used for validating 36 } 37 38 // NewBlockValidator returns a new block validator which is safe for re-use 39 func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine) *BlockValidator { 40 validator := &BlockValidator{ 41 config: config, 42 engine: engine, 43 bc: blockchain, 44 } 45 return validator 46 } 47 48 // ValidateBody validates the the block 49 // header's transaction. The headers are assumed to be already 50 // validated at this point. 51 func (v *BlockValidator) ValidateBody(block *types.Block) error { 52 // Check whether the block's known, and if not, that it's linkable 53 if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) { 54 return ErrKnownBlock 55 } 56 if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) { 57 if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) { 58 return consensus.ErrUnknownAncestor 59 } 60 return consensus.ErrPrunedAncestor 61 } 62 // Header validity is known at this point, check the transactions 63 header := block.Header() 64 if hash := types.DeriveSha(block.Transactions()); hash != header.TxHash { 65 return fmt.Errorf("transaction root hash mismatch: have %x, want %x", hash, header.TxHash) 66 } 67 return nil 68 } 69 70 // ValidateState validates the various changes that happen after a state 71 // transition, such as amount of used gas, the receipt roots and the state root 72 // itself. ValidateState returns a database batch if the validation was a success 73 // otherwise nil and an error is returned. 74 func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64) error { 75 header := block.Header() 76 if block.GasUsed() != usedGas { 77 return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), usedGas) 78 } 79 // Validate the received block's bloom with the one derived from the generated receipts. 80 // For valid blocks this should always validate to true. 81 rbloom := types.CreateBloom(receipts) 82 if rbloom != header.Bloom { 83 return fmt.Errorf("invalid bloom (remote: %x local: %x)", header.Bloom, rbloom) 84 } 85 // Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, R1]])) 86 receiptSha := types.DeriveSha(receipts) 87 if receiptSha != header.ReceiptHash { 88 return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha) 89 } 90 // Validate the state root against the received state root and throw 91 // an error if they don't match. 92 if root := statedb.IntermediateRoot(true); header.Root != root { 93 return fmt.Errorf("invalid merkle root (remote: %x local: %x)", header.Root, root) 94 } 95 return nil 96 } 97 98 // CalcGasLimit computes the gas limit of the next block after parent. 99 // This is producer strategy, not consensus protocol. 100 func CalcGasLimit(parent *types.Block) uint64 { 101 // contrib = (parentGasUsed * 3 / 2) / 1024 102 contrib := (parent.GasUsed() + parent.GasUsed()/2) / params.GasLimitBoundDivisor 103 104 // decay = parentGasLimit / 1024 -1 105 decay := parent.GasLimit()/params.GasLimitBoundDivisor - 1 106 107 /* 108 strategy: gasLimit of block-to-produce is set based on parent's 109 gasUsed value. if parentGasUsed > parentGasLimit * (2/3) then we 110 increase it, otherwise lower it (or leave it unchanged if it's right 111 at that usage) the amount increased/decreased depends on how far away 112 from parentGasLimit * (2/3) parentGasUsed is. 113 */ 114 limit := parent.GasLimit() - decay + contrib 115 if limit < params.MinGasLimit { 116 limit = params.MinGasLimit 117 } 118 // however, if we're now below the target (TargetGasLimit) we increase the 119 // limit as much as we can (parentGasLimit / 1024 -1) 120 if limit < params.TargetGasLimit { 121 limit = parent.GasLimit() + decay 122 if limit > params.TargetGasLimit { 123 limit = params.TargetGasLimit 124 } 125 } 126 return limit 127 }