Skip to content

Commit 5622c65

Browse files
committed
enforce 1559 total gas limit; don't allow BaseFee to drop to 0
1 parent 630cc0b commit 5622c65

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

consensus/ethash/consensus.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,18 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
270270
if header.GasLimit > max {
271271
return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, max)
272272
}
273-
// Verify that the gasUsed is <= gasLimit
274-
if header.GasUsed > header.GasLimit {
275-
return fmt.Errorf("invalid gasUsed: have %d, gasLimit %d", header.GasUsed, header.GasLimit)
273+
274+
// Verify that total gas usage is below the gas limit
275+
// Before EIP1559 activation, the "gasLimit" field in the header directly represents the gas limit
276+
// After EIP1559 activation, the "gasLimit" field in the header represents the total gas target
277+
// After EIP1559 activation, the *total* gas limit is equal to total gas target * slack coefficient
278+
// The individual limits on legacy and EIP1559 gas usage during the transition period are enforced in the core/block_validator.go
279+
var slackCoefficient uint64 = 1
280+
if chain.Config().IsEIP1559(header.Number) {
281+
slackCoefficient = chain.Config().EIP1559.EIP1559SlackCoefficient
282+
}
283+
if header.GasUsed > header.GasLimit*slackCoefficient {
284+
return fmt.Errorf("invalid gasUsed: have %d, gasLimit %d, slack coefficient %d", header.GasUsed, header.GasLimit, chain.Config().EIP1559.EIP1559SlackCoefficient)
276285
}
277286

278287
// Verify that the gas limit remains within allowed bounds

consensus/misc/basefee.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package misc
1818

1919
import (
2020
"errors"
21-
"fmt"
2221
"math/big"
2322

2423
"github.com/ethereum/go-ethereum/common"
@@ -30,7 +29,7 @@ var (
3029
errInvalidBaseFee = errors.New("invalid BaseFee")
3130
errMissingParentBaseFee = errors.New("parent header is missing BaseFee")
3231
errMissingBaseFee = errors.New("current header is missing BaseFee")
33-
errHaveBaseFee = fmt.Errorf("BaseFee should not be set before block %d", params.EIP1559ForkBlockNumber)
32+
errHaveBaseFee = errors.New("BaseFee should not be set before EIP1559 activation %d")
3433
)
3534

3635
// VerifyEIP1559BaseFee verifies that the EIP1559 BaseFee field is valid for the current block height
@@ -79,7 +78,6 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
7978
mul := new(big.Int).Mul(parent.BaseFee, delta)
8079
div := new(big.Int).Div(mul, parentGasTarget)
8180
div2 := new(big.Int).Div(div, new(big.Int).SetUint64(config.EIP1559.EIP1559BaseFeeMaxChangeDenominator))
82-
8381
baseFee := new(big.Int).Add(parent.BaseFee, div2)
8482

8583
// A valid BASEFEE is one such that abs(BASEFEE - PARENT_BASEFEE) <= max(1, PARENT_BASEFEE // BASEFEE_MAX_CHANGE_DENOMINATOR)
@@ -100,6 +98,10 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
10098
}
10199
baseFee.Set(new(big.Int).Add(parent.BaseFee, max))
102100
}
101+
// Prevent BaseFee from dropping to zero
102+
if baseFee.Cmp(common.Big0) == 0 {
103+
baseFee.Set(common.Big1)
104+
}
103105
return baseFee
104106
}
105107

core/gaspool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func NewLegacyGasPool(chainConfig *params.ChainConfig, height, gasLimit *big.Int
3737

3838
// NewEIP1559GasPool returns a GasPool filled to the EIP1559 gas limit
3939
func NewEIP1559GasPool(chainConfig *params.ChainConfig, height, gasLimit *big.Int) *GasPool {
40-
// EIP1559 gas limit is 2x the EIP1559GasTarget
40+
// EIP1559 gas limit is slack coefficient * EIP1559GasTarget
4141
eip1559GasTarget := misc.CalcEIP1559GasTarget(chainConfig, height, gasLimit)
42-
return new(GasPool).AddGas(2 * eip1559GasTarget.Uint64())
42+
return new(GasPool).AddGas(chainConfig.EIP1559.EIP1559SlackCoefficient * eip1559GasTarget.Uint64())
4343
}
4444

4545
// AddGas makes gas available for execution.

0 commit comments

Comments
 (0)