github.com/theQRL/go-zond@v0.2.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/theQRL/go-zond/consensus"
    24  	"github.com/theQRL/go-zond/core/state"
    25  	"github.com/theQRL/go-zond/core/types"
    26  	"github.com/theQRL/go-zond/params"
    27  	"github.com/theQRL/go-zond/trie"
    28  )
    29  
    30  // BlockValidator is responsible for validating block headers 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  	engine consensus.Engine    // Consensus engine used for validating
    38  }
    39  
    40  // NewBlockValidator returns a new block validator which is safe for re-use
    41  func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine) *BlockValidator {
    42  	validator := &BlockValidator{
    43  		config: config,
    44  		engine: engine,
    45  		bc:     blockchain,
    46  	}
    47  	return validator
    48  }
    49  
    50  // ValidateBody verifies the block header's transaction root. The
    51  // headers are assumed to be already validated at this point.
    52  func (v *BlockValidator) ValidateBody(block *types.Block) error {
    53  	// Check whether the block is already imported.
    54  	if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) {
    55  		return ErrKnownBlock
    56  	}
    57  
    58  	// Header validity is known at this point. Here we verify that transactions
    59  	// and withdrawals given in the block body match the header.
    60  	header := block.Header()
    61  	if hash := types.DeriveSha(block.Transactions(), trie.NewStackTrie(nil)); hash != header.TxHash {
    62  		return fmt.Errorf("transaction root hash mismatch (header value %x, calculated %x)", header.TxHash, hash)
    63  	}
    64  
    65  	// Withdrawals are present after the Shanghai fork.
    66  	if header.WithdrawalsHash != nil {
    67  		// Withdrawals list must be present in body after Shanghai.
    68  		if block.Withdrawals() == nil {
    69  			return errors.New("missing withdrawals in block body")
    70  		}
    71  		if hash := types.DeriveSha(block.Withdrawals(), trie.NewStackTrie(nil)); hash != *header.WithdrawalsHash {
    72  			return fmt.Errorf("withdrawals root hash mismatch (header value %x, calculated %x)", *header.WithdrawalsHash, hash)
    73  		}
    74  	} else if block.Withdrawals() != nil {
    75  		// Withdrawals are not allowed prior to Shanghai fork
    76  		return errors.New("withdrawals present in block body")
    77  	}
    78  
    79  	// Ancestor block must be known.
    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  // ValidateState validates the various changes that happen after a state transition,
    90  // such as amount of used gas, the receipt roots and the state root itself.
    91  func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64) error {
    92  	header := block.Header()
    93  	if block.GasUsed() != usedGas {
    94  		return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), usedGas)
    95  	}
    96  	// Validate the received block's bloom with the one derived from the generated receipts.
    97  	// For valid blocks this should always validate to true.
    98  	rbloom := types.CreateBloom(receipts)
    99  	if rbloom != header.Bloom {
   100  		return fmt.Errorf("invalid bloom (remote: %x  local: %x)", header.Bloom, rbloom)
   101  	}
   102  	// Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, Rn]]))
   103  	receiptSha := types.DeriveSha(receipts, trie.NewStackTrie(nil))
   104  	if receiptSha != header.ReceiptHash {
   105  		return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha)
   106  	}
   107  	// Validate the state root against the received state root and throw
   108  	// an error if they don't match.
   109  	if root := statedb.IntermediateRoot(true); header.Root != root {
   110  		return fmt.Errorf("invalid merkle root (remote: %x local: %x) dberr: %w", header.Root, root, statedb.Error())
   111  	}
   112  	return nil
   113  }
   114  
   115  // CalcGasLimit computes the gas limit of the next block after parent. It aims
   116  // to keep the baseline gas close to the provided target, and increase it towards
   117  // the target if the baseline gas is lower.
   118  func CalcGasLimit(parentGasLimit, desiredLimit uint64) uint64 {
   119  	delta := parentGasLimit/params.GasLimitBoundDivisor - 1
   120  	limit := parentGasLimit
   121  	if desiredLimit < params.MinGasLimit {
   122  		desiredLimit = params.MinGasLimit
   123  	}
   124  	// If we're outside our allowed gas range, we try to hone towards them
   125  	if limit < desiredLimit {
   126  		limit = parentGasLimit + delta
   127  		if limit > desiredLimit {
   128  			limit = desiredLimit
   129  		}
   130  		return limit
   131  	}
   132  	if limit > desiredLimit {
   133  		limit = parentGasLimit - delta
   134  		if limit < desiredLimit {
   135  			limit = desiredLimit
   136  		}
   137  	}
   138  	return limit
   139  }