diff options
Diffstat (limited to 'core/block_validator.go')
-rw-r--r-- | core/block_validator.go | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/core/block_validator.go b/core/block_validator.go index e9cfd0482..143728bb8 100644 --- a/core/block_validator.go +++ b/core/block_validator.go @@ -18,9 +18,7 @@ package core import ( "fmt" - "math/big" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" @@ -76,10 +74,10 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error { // transition, such as amount of used gas, the receipt roots and the state root // itself. ValidateState returns a database batch if the validation was a success // otherwise nil and an error is returned. -func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas *big.Int) error { +func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64) error { header := block.Header() - if block.GasUsed().Cmp(usedGas) != 0 { - return fmt.Errorf("invalid gas used (remote: %v local: %v)", block.GasUsed(), usedGas) + if block.GasUsed() != usedGas { + return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), usedGas) } // Validate the received block's bloom with the one derived from the generated receipts. // For valid blocks this should always validate to true. @@ -101,17 +99,13 @@ func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *stat } // 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 { +func CalcGasLimit(parent *types.Block) uint64 { // 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) + contrib := (parent.GasUsed() + parent.GasUsed()/2) / params.GasLimitBoundDivisor // decay = parentGasLimit / 1024 -1 - decay := new(big.Int).Div(parent.GasLimit(), params.GasLimitBoundDivisor) - decay.Sub(decay, big.NewInt(1)) + decay := parent.GasLimit()/params.GasLimitBoundDivisor - 1 /* strategy: gasLimit of block-to-mine is set based on parent's @@ -120,15 +114,17 @@ func CalcGasLimit(parent *types.Block) *big.Int { 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) - gl.Set(math.BigMax(gl, params.MinGasLimit)) - + limit := parent.GasLimit() - decay + contrib + if limit < params.MinGasLimit { + limit = params.MinGasLimit + } // however, if we're now below the target (TargetGasLimit) we increase the // limit as much as we can (parentGasLimit / 1024 -1) - if gl.Cmp(params.TargetGasLimit) < 0 { - gl.Add(parent.GasLimit(), decay) - gl.Set(math.BigMin(gl, params.TargetGasLimit)) + if limit < params.TargetGasLimit { + limit = parent.GasLimit() + decay + if limit > params.TargetGasLimit { + limit = params.TargetGasLimit + } } - return gl + return limit } |