mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 04:36:42 +00:00
consensus: Verify gaslimit bounds when accepting blocks (#1619)
* Verify gaslimit bounds when accepting blocks * fix tests
This commit is contained in:
parent
73a2d3254c
commit
3f285f1b5d
5 changed files with 24 additions and 8 deletions
1
commits
1
commits
|
|
@ -5,6 +5,7 @@ b49f6cb0f - Wanwiset Peerapatanapokin - consensus: verify header hash is same as
|
|||
e0c987f45 - Wanwiset Peerapatanapokin - impose size limit for DecodeBytesExtraFields (#1637)
|
||||
53f6a8d6d - Wanwiset Peerapatanapokin - consensus: optimize CompareSignersLists
|
||||
6abb4f88d - Wanwiset Peerapatanapokin - consensus: Verify gaslimit bounds when accepting blocks (#1619)
|
||||
|
||||
c95d25805 - Wanwiset Peerapatanapokin - separate fullVerifies array into v1fullVerifies and v2fullVerifies (#1618)
|
||||
25a70e877 - Wanwiset Peerapatanapokin - consensus: skip gas limit verification for genesis block (#1646)
|
||||
da92ad7cc - Wanwiset Peerapatanapokin - update package version: (#1612)
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ import (
|
|||
"github.com/XinFinOrg/XDPoSChain/consensus"
|
||||
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
|
||||
"github.com/XinFinOrg/XDPoSChain/consensus/clique"
|
||||
"github.com/XinFinOrg/XDPoSChain/consensus/misc"
|
||||
"github.com/XinFinOrg/XDPoSChain/core/state"
|
||||
"github.com/XinFinOrg/XDPoSChain/core/types"
|
||||
"github.com/XinFinOrg/XDPoSChain/ethdb"
|
||||
|
|
@ -330,11 +331,18 @@ func (x *XDPoS_v2) Prepare(chain consensus.ChainReader, header *types.Header) er
|
|||
number := header.Number.Uint64()
|
||||
parent := chain.GetHeader(header.ParentHash, number-1)
|
||||
|
||||
log.Info("Preparing new block!", "Number", number, "Parent Hash", parent.Hash())
|
||||
if parent == nil {
|
||||
return consensus.ErrUnknownAncestor
|
||||
}
|
||||
// Ensure gas settings are bounded
|
||||
if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil {
|
||||
return err
|
||||
}
|
||||
if header.GasUsed > header.GasLimit {
|
||||
return fmt.Errorf("gas used exceeded gaslimit, gas used: %d, gas limit: %d", header.GasUsed, header.GasLimit)
|
||||
}
|
||||
|
||||
log.Info("Preparing new block!", "Number", number, "Parent Hash", parent.Hash())
|
||||
x.signLock.RLock()
|
||||
signer := x.signer
|
||||
x.signLock.RUnlock()
|
||||
|
|
@ -413,6 +421,17 @@ func (x *XDPoS_v2) Finalize(chain consensus.ChainReader, header *types.Header, s
|
|||
}
|
||||
}
|
||||
}
|
||||
parentHeader := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
|
||||
if parentHeader == nil {
|
||||
return nil, consensus.ErrUnknownAncestor
|
||||
}
|
||||
// Ensure gas settings are bounded
|
||||
if err := misc.VerifyGaslimit(parentHeader.GasLimit, header.GasLimit); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if header.GasUsed > header.GasLimit {
|
||||
return nil, fmt.Errorf("gas used exceeded gaslimit, gas used: %d, gas limit: %d", header.GasUsed, header.GasLimit)
|
||||
}
|
||||
|
||||
// the state remains as is and uncles are dropped
|
||||
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
|
||||
|
|
|
|||
|
|
@ -66,8 +66,7 @@ func (x *XDPoS_v2) verifyHeader(chain consensus.ChainReader, header *types.Heade
|
|||
}
|
||||
|
||||
// Ensure gas limit is consistent with parent
|
||||
err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit)
|
||||
if err != nil && parent.Number.Uint64() != 0 { // skip genesis block
|
||||
if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil {
|
||||
return err
|
||||
}
|
||||
// Ensure gas used is less than or equal to gas limit
|
||||
|
|
@ -113,10 +112,6 @@ func (x *XDPoS_v2) verifyHeader(chain consensus.ChainReader, header *types.Heade
|
|||
if header.UncleHash != utils.UncleHash {
|
||||
return utils.ErrInvalidUncleHash
|
||||
}
|
||||
// Verify that the gas limit remains within allowed bounds
|
||||
if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil {
|
||||
return err
|
||||
}
|
||||
// Verify the header's EIP-1559 attributes.
|
||||
if err := eip1559.VerifyEip1559Header(chain.Config(), header); err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ func TestHookPenaltyV2Mining(t *testing.T) {
|
|||
headerMining := &types.Header{
|
||||
ParentHash: header2100.ParentHash,
|
||||
Number: header2100.Number,
|
||||
GasLimit: params.TargetGasLimit,
|
||||
GasLimit: params.V2TestsGasLimit,
|
||||
Time: header2100.Time,
|
||||
Coinbase: acc1Addr,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import "math/big"
|
|||
|
||||
var (
|
||||
TargetGasLimit uint64 = XDCGenesisGasLimit // The artificial target
|
||||
V2TestsGasLimit uint64 = 1200000000 // The gas limit used in the v2 consensus tests
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
|
|||
Loading…
Reference in a new issue