2016-04-14 19:18:24 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-10-19 17:08:17 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2017-04-05 01:16:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
2015-10-19 17:08:17 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2015-11-27 16:40:29 +02:00
|
|
|
)
|
|
|
|
|
2015-10-19 17:08:17 +03:00
|
|
|
// BlockValidator is responsible for validating block headers, uncles and
|
|
|
|
// processed state.
|
|
|
|
//
|
|
|
|
// BlockValidator implements Validator.
|
|
|
|
type BlockValidator struct {
|
2016-10-20 14:36:29 +03:00
|
|
|
config *params.ChainConfig // Chain configuration options
|
|
|
|
bc *BlockChain // Canonical block chain
|
2017-04-05 01:16:29 +03:00
|
|
|
engine consensus.Engine // Consensus engine used for validating
|
2015-10-19 17:08:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBlockValidator returns a new block validator which is safe for re-use
|
2017-04-05 01:16:29 +03:00
|
|
|
func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine) *BlockValidator {
|
2015-10-19 17:08:17 +03:00
|
|
|
validator := &BlockValidator{
|
2016-03-02 00:32:43 +02:00
|
|
|
config: config,
|
2017-04-05 01:16:29 +03:00
|
|
|
engine: engine,
|
2016-03-02 00:32:43 +02:00
|
|
|
bc: blockchain,
|
2015-10-19 17:08:17 +03:00
|
|
|
}
|
|
|
|
return validator
|
|
|
|
}
|
|
|
|
|
2017-04-05 01:16:29 +03:00
|
|
|
// ValidateBody validates the given block's uncles and verifies the the block
|
|
|
|
// header's transaction and uncle roots. The headers are assumed to be already
|
|
|
|
// validated at this point.
|
|
|
|
func (v *BlockValidator) ValidateBody(block *types.Block) error {
|
|
|
|
// Check whether the block's known, and if not, that it's linkable
|
2015-10-19 17:08:17 +03:00
|
|
|
if v.bc.HasBlock(block.Hash()) {
|
|
|
|
if _, err := state.New(block.Root(), v.bc.chainDb); err == nil {
|
2017-04-06 14:58:03 +03:00
|
|
|
return ErrKnownBlock
|
2015-10-19 17:08:17 +03:00
|
|
|
}
|
|
|
|
}
|
2016-04-05 16:22:04 +03:00
|
|
|
parent := v.bc.GetBlock(block.ParentHash(), block.NumberU64()-1)
|
2015-10-19 17:08:17 +03:00
|
|
|
if parent == nil {
|
2017-04-06 14:58:03 +03:00
|
|
|
return consensus.ErrUnknownAncestor
|
2015-10-19 17:08:17 +03:00
|
|
|
}
|
|
|
|
if _, err := state.New(parent.Root(), v.bc.chainDb); err != nil {
|
2017-04-06 14:58:03 +03:00
|
|
|
return consensus.ErrUnknownAncestor
|
2015-10-19 17:08:17 +03:00
|
|
|
}
|
2017-04-05 01:16:29 +03:00
|
|
|
// Header validity is known at this point, check the uncles and transactions
|
2015-10-19 17:08:17 +03:00
|
|
|
header := block.Header()
|
2017-04-05 01:16:29 +03:00
|
|
|
if err := v.engine.VerifyUncles(v.bc, block); err != nil {
|
2015-10-19 17:08:17 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-04-05 01:16:29 +03:00
|
|
|
if hash := types.CalcUncleHash(block.Uncles()); hash != header.UncleHash {
|
|
|
|
return fmt.Errorf("uncle root hash mismatch: have %x, want %x", hash, header.UncleHash)
|
2015-10-19 17:08:17 +03:00
|
|
|
}
|
2017-04-05 01:16:29 +03:00
|
|
|
if hash := types.DeriveSha(block.Transactions()); hash != header.TxHash {
|
|
|
|
return fmt.Errorf("transaction root hash mismatch: have %x, want %x", hash, header.TxHash)
|
2015-10-19 17:08:17 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateState validates the various changes that happen after a state
|
|
|
|
// transition, such as amount of used gas, the receipt roots and the state root
|
2016-03-15 20:08:18 +02:00
|
|
|
// itself. ValidateState returns a database batch if the validation was a success
|
2015-10-19 17:08:17 +03:00
|
|
|
// otherwise nil and an error is returned.
|
2017-04-06 14:58:03 +03:00
|
|
|
func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas *big.Int) error {
|
2015-10-19 17:08:17 +03:00
|
|
|
header := block.Header()
|
|
|
|
if block.GasUsed().Cmp(usedGas) != 0 {
|
2017-04-06 14:58:03 +03:00
|
|
|
return fmt.Errorf("invalid gas used (remote: %v local: %v)", block.GasUsed(), usedGas)
|
2015-10-19 17:08:17 +03:00
|
|
|
}
|
|
|
|
// Validate the received block's bloom with the one derived from the generated receipts.
|
|
|
|
// For valid blocks this should always validate to true.
|
|
|
|
rbloom := types.CreateBloom(receipts)
|
|
|
|
if rbloom != header.Bloom {
|
2016-11-23 15:32:25 +03:00
|
|
|
return fmt.Errorf("invalid bloom (remote: %x local: %x)", header.Bloom, rbloom)
|
2015-10-19 17:08:17 +03:00
|
|
|
}
|
|
|
|
// Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, R1]]))
|
|
|
|
receiptSha := types.DeriveSha(receipts)
|
|
|
|
if receiptSha != header.ReceiptHash {
|
2016-11-23 15:32:25 +03:00
|
|
|
return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha)
|
2015-10-19 17:08:17 +03:00
|
|
|
}
|
|
|
|
// Validate the state root against the received state root and throw
|
|
|
|
// an error if they don't match.
|
2016-10-20 14:36:29 +03:00
|
|
|
if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root {
|
2016-11-23 15:32:25 +03:00
|
|
|
return fmt.Errorf("invalid merkle root (remote: %x local: %x)", header.Root, root)
|
2015-10-19 17:08:17 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-27 16:40:29 +02:00
|
|
|
// CalcGasLimit computes the gas limit of the next block after parent.
|
|
|
|
// The result may be modified by the caller.
|
|
|
|
// This is miner strategy, not consensus protocol.
|
|
|
|
func CalcGasLimit(parent *types.Block) *big.Int {
|
|
|
|
// contrib = (parentGasUsed * 3 / 2) / 1024
|
|
|
|
contrib := new(big.Int).Mul(parent.GasUsed(), big.NewInt(3))
|
|
|
|
contrib = contrib.Div(contrib, big.NewInt(2))
|
|
|
|
contrib = contrib.Div(contrib, params.GasLimitBoundDivisor)
|
|
|
|
|
|
|
|
// decay = parentGasLimit / 1024 -1
|
|
|
|
decay := new(big.Int).Div(parent.GasLimit(), params.GasLimitBoundDivisor)
|
|
|
|
decay.Sub(decay, big.NewInt(1))
|
|
|
|
|
|
|
|
/*
|
|
|
|
strategy: gasLimit of block-to-mine is set based on parent's
|
|
|
|
gasUsed value. if parentGasUsed > parentGasLimit * (2/3) then we
|
|
|
|
increase it, otherwise lower it (or leave it unchanged if it's right
|
|
|
|
at that usage) the amount increased/decreased depends on how far away
|
|
|
|
from parentGasLimit * (2/3) parentGasUsed is.
|
|
|
|
*/
|
|
|
|
gl := new(big.Int).Sub(parent.GasLimit(), decay)
|
|
|
|
gl = gl.Add(gl, contrib)
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
gl.Set(math.BigMax(gl, params.MinGasLimit))
|
2015-11-27 16:40:29 +02:00
|
|
|
|
2016-03-02 00:32:43 +02:00
|
|
|
// however, if we're now below the target (TargetGasLimit) we increase the
|
2015-11-27 16:40:29 +02:00
|
|
|
// limit as much as we can (parentGasLimit / 1024 -1)
|
2016-03-02 00:32:43 +02:00
|
|
|
if gl.Cmp(params.TargetGasLimit) < 0 {
|
2015-11-27 16:40:29 +02:00
|
|
|
gl.Add(parent.GasLimit(), decay)
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
gl.Set(math.BigMin(gl, params.TargetGasLimit))
|
2015-11-27 16:40:29 +02:00
|
|
|
}
|
|
|
|
return gl
|
|
|
|
}
|