github.com/ethereum/go-ethereum@v1.16.1/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 "errors" 21 "fmt" 22 23 "github.com/ethereum/go-ethereum/consensus" 24 "github.com/ethereum/go-ethereum/core/state" 25 "github.com/ethereum/go-ethereum/core/types" 26 "github.com/ethereum/go-ethereum/params" 27 "github.com/ethereum/go-ethereum/trie" 28 ) 29 30 // BlockValidator is responsible for validating block headers, uncles and 31 // processed state. 32 // 33 // BlockValidator implements Validator. 34 type BlockValidator struct { 35 config *params.ChainConfig // Chain configuration options 36 bc *BlockChain // Canonical block chain 37 } 38 39 // NewBlockValidator returns a new block validator which is safe for re-use 40 func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain) *BlockValidator { 41 validator := &BlockValidator{ 42 config: config, 43 bc: blockchain, 44 } 45 return validator 46 } 47 48 // ValidateBody validates the given block's uncles and verifies the block 49 // header's transaction and uncle roots. 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 is already imported. 53 if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) { 54 return ErrKnownBlock 55 } 56 57 // Header validity is known at this point. Here we verify that uncles, transactions 58 // and withdrawals given in the block body match the header. 59 header := block.Header() 60 if err := v.bc.engine.VerifyUncles(v.bc, block); err != nil { 61 return err 62 } 63 if hash := types.CalcUncleHash(block.Uncles()); hash != header.UncleHash { 64 return fmt.Errorf("uncle root hash mismatch (header value %x, calculated %x)", header.UncleHash, hash) 65 } 66 if hash := types.DeriveSha(block.Transactions(), trie.NewStackTrie(nil)); hash != header.TxHash { 67 return fmt.Errorf("transaction root hash mismatch (header value %x, calculated %x)", header.TxHash, hash) 68 } 69 70 // Withdrawals are present after the Shanghai fork. 71 if header.WithdrawalsHash != nil { 72 // Withdrawals list must be present in body after Shanghai. 73 if block.Withdrawals() == nil { 74 return errors.New("missing withdrawals in block body") 75 } 76 if hash := types.DeriveSha(block.Withdrawals(), trie.NewStackTrie(nil)); hash != *header.WithdrawalsHash { 77 return fmt.Errorf("withdrawals root hash mismatch (header value %x, calculated %x)", *header.WithdrawalsHash, hash) 78 } 79 } else if block.Withdrawals() != nil { 80 // Withdrawals are not allowed prior to Shanghai fork 81 return errors.New("withdrawals present in block body") 82 } 83 84 // Blob transactions may be present after the Cancun fork. 85 var blobs int 86 for i, tx := range block.Transactions() { 87 // Count the number of blobs to validate against the header's blobGasUsed 88 blobs += len(tx.BlobHashes()) 89 90 // If the tx is a blob tx, it must NOT have a sidecar attached to be valid in a block. 91 if tx.BlobTxSidecar() != nil { 92 return fmt.Errorf("unexpected blob sidecar in transaction at index %d", i) 93 } 94 95 // The individual checks for blob validity (version-check + not empty) 96 // happens in state transition. 97 } 98 99 // Check blob gas usage. 100 if header.BlobGasUsed != nil { 101 if want := *header.BlobGasUsed / params.BlobTxBlobGasPerBlob; uint64(blobs) != want { // div because the header is surely good vs the body might be bloated 102 return fmt.Errorf("blob gas used mismatch (header %v, calculated %v)", *header.BlobGasUsed, blobs*params.BlobTxBlobGasPerBlob) 103 } 104 } else { 105 if blobs > 0 { 106 return errors.New("data blobs present in block body") 107 } 108 } 109 110 // Ancestor block must be known. 111 if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) { 112 if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) { 113 return consensus.ErrUnknownAncestor 114 } 115 return consensus.ErrPrunedAncestor 116 } 117 return nil 118 } 119 120 // ValidateState validates the various changes that happen after a state transition, 121 // such as amount of used gas, the receipt roots and the state root itself. 122 func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateDB, res *ProcessResult, stateless bool) error { 123 if res == nil { 124 return errors.New("nil ProcessResult value") 125 } 126 header := block.Header() 127 if block.GasUsed() != res.GasUsed { 128 return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), res.GasUsed) 129 } 130 // Validate the received block's bloom with the one derived from the generated receipts. 131 // For valid blocks this should always validate to true. 132 // 133 // Receipts must go through MakeReceipt to calculate the receipt's bloom 134 // already. Merge the receipt's bloom together instead of recalculating 135 // everything. 136 rbloom := types.MergeBloom(res.Receipts) 137 if rbloom != header.Bloom { 138 return fmt.Errorf("invalid bloom (remote: %x local: %x)", header.Bloom, rbloom) 139 } 140 // In stateless mode, return early because the receipt and state root are not 141 // provided through the witness, rather the cross validator needs to return it. 142 if stateless { 143 return nil 144 } 145 // The receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, Rn]])) 146 receiptSha := types.DeriveSha(res.Receipts, trie.NewStackTrie(nil)) 147 if receiptSha != header.ReceiptHash { 148 return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha) 149 } 150 // Validate the parsed requests match the expected header value. 151 if header.RequestsHash != nil { 152 reqhash := types.CalcRequestsHash(res.Requests) 153 if reqhash != *header.RequestsHash { 154 return fmt.Errorf("invalid requests hash (remote: %x local: %x)", *header.RequestsHash, reqhash) 155 } 156 } else if res.Requests != nil { 157 return errors.New("block has requests before prague fork") 158 } 159 // Validate the state root against the received state root and throw 160 // an error if they don't match. 161 if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root { 162 return fmt.Errorf("invalid merkle root (remote: %x local: %x) dberr: %w", header.Root, root, statedb.Error()) 163 } 164 return nil 165 } 166 167 // CalcGasLimit computes the gas limit of the next block after parent. It aims 168 // to keep the baseline gas close to the provided target, and increase it towards 169 // the target if the baseline gas is lower. 170 func CalcGasLimit(parentGasLimit, desiredLimit uint64) uint64 { 171 delta := parentGasLimit/params.GasLimitBoundDivisor - 1 172 limit := parentGasLimit 173 if desiredLimit < params.MinGasLimit { 174 desiredLimit = params.MinGasLimit 175 } 176 // If we're outside our allowed gas range, we try to hone towards them 177 if limit < desiredLimit { 178 limit = parentGasLimit + delta 179 if limit > desiredLimit { 180 limit = desiredLimit 181 } 182 return limit 183 } 184 if limit > desiredLimit { 185 limit = parentGasLimit - delta 186 if limit < desiredLimit { 187 limit = desiredLimit 188 } 189 } 190 return limit 191 }