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