github.com/humaniq/go-ethereum@v1.6.8-0.20171225131628-061223a13848/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  	"math/big"
    22  
    23  	"github.com/ethereum/go-ethereum/common/math"
    24  	"github.com/ethereum/go-ethereum/consensus"
    25  	"github.com/ethereum/go-ethereum/core/state"
    26  	"github.com/ethereum/go-ethereum/core/types"
    27  	"github.com/ethereum/go-ethereum/params"
    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  	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 validates the given block's uncles and verifies the the block
    51  // header's transaction and uncle roots. The headers are assumed to be already
    52  // validated at this point.
    53  func (v *BlockValidator) ValidateBody(block *types.Block) error {
    54  	// Check whether the block's known, and if not, that it's linkable
    55  	if v.bc.HasBlockAndState(block.Hash()) {
    56  		return ErrKnownBlock
    57  	}
    58  	if !v.bc.HasBlockAndState(block.ParentHash()) {
    59  		return consensus.ErrUnknownAncestor
    60  	}
    61  	// Header validity is known at this point, check the uncles and transactions
    62  	header := block.Header()
    63  	if err := v.engine.VerifyUncles(v.bc, block); err != nil {
    64  		return err
    65  	}
    66  	if hash := types.CalcUncleHash(block.Uncles()); hash != header.UncleHash {
    67  		return fmt.Errorf("uncle root hash mismatch: have %x, want %x", hash, header.UncleHash)
    68  	}
    69  	if hash := types.DeriveSha(block.Transactions()); hash != header.TxHash {
    70  		return fmt.Errorf("transaction root hash mismatch: have %x, want %x", hash, header.TxHash)
    71  	}
    72  	return nil
    73  }
    74  
    75  // ValidateState validates the various changes that happen after a state
    76  // transition, such as amount of used gas, the receipt roots and the state root
    77  // itself. ValidateState returns a database batch if the validation was a success
    78  // otherwise nil and an error is returned.
    79  func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas *big.Int) error {
    80  	header := block.Header()
    81  	if block.GasUsed().Cmp(usedGas) != 0 {
    82  		return fmt.Errorf("invalid gas used (remote: %v local: %v)", block.GasUsed(), usedGas)
    83  	}
    84  	// Validate the received block's bloom with the one derived from the generated receipts.
    85  	// For valid blocks this should always validate to true.
    86  	rbloom := types.CreateBloom(receipts)
    87  	if rbloom != header.Bloom {
    88  		return fmt.Errorf("invalid bloom (remote: %x  local: %x)", header.Bloom, rbloom)
    89  	}
    90  	// Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, R1]]))
    91  	receiptSha := types.DeriveSha(receipts)
    92  	if receiptSha != header.ReceiptHash {
    93  		return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha)
    94  	}
    95  	// Validate the state root against the received state root and throw
    96  	// an error if they don't match.
    97  	if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root {
    98  		return fmt.Errorf("invalid merkle root (remote: %x local: %x)", header.Root, root)
    99  	}
   100  	return nil
   101  }
   102  
   103  // CalcGasLimit computes the gas limit of the next block after parent.
   104  // The result may be modified by the caller.
   105  // This is miner strategy, not consensus protocol.
   106  func CalcGasLimit(parent *types.Block) *big.Int {
   107  	// contrib = (parentGasUsed * 3 / 2) / 1024
   108  	contrib := new(big.Int).Mul(parent.GasUsed(), big.NewInt(3))
   109  	contrib = contrib.Div(contrib, big.NewInt(2))
   110  	contrib = contrib.Div(contrib, params.GasLimitBoundDivisor)
   111  
   112  	// decay = parentGasLimit / 1024 -1
   113  	decay := new(big.Int).Div(parent.GasLimit(), params.GasLimitBoundDivisor)
   114  	decay.Sub(decay, big.NewInt(1))
   115  
   116  	/*
   117  		strategy: gasLimit of block-to-mine is set based on parent's
   118  		gasUsed value.  if parentGasUsed > parentGasLimit * (2/3) then we
   119  		increase it, otherwise lower it (or leave it unchanged if it's right
   120  		at that usage) the amount increased/decreased depends on how far away
   121  		from parentGasLimit * (2/3) parentGasUsed is.
   122  	*/
   123  	gl := new(big.Int).Sub(parent.GasLimit(), decay)
   124  	gl = gl.Add(gl, contrib)
   125  	gl.Set(math.BigMax(gl, params.MinGasLimit))
   126  
   127  	// however, if we're now below the target (TargetGasLimit) we increase the
   128  	// limit as much as we can (parentGasLimit / 1024 -1)
   129  	if gl.Cmp(params.TargetGasLimit) < 0 {
   130  		gl.Add(parent.GasLimit(), decay)
   131  		gl.Set(math.BigMin(gl, params.TargetGasLimit))
   132  	}
   133  	return gl
   134  }