Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: implement eip-1559 #22837

Merged
merged 19 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions consensus/misc/eip1559.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func VerifyEip1559Header(config *params.ChainConfig, parent, header *types.Heade
}

// Verify that the gasUsed is <= gasTarget*elasticityMultiplier
if header.GasUsed > header.GasLimit*params.ElasticityMultiplier {
if header.GasUsed > header.GasLimit {
return fmt.Errorf("invalid gasUsed: have %d, gasLimit %d", header.GasUsed, header.GasLimit*params.ElasticityMultiplier)
}

Expand All @@ -52,21 +52,22 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
return new(big.Int).SetUint64(params.InitialBaseFee)
}

// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
if parent.GasUsed == parent.GasLimit {
return new(big.Int).Set(parent.BaseFee)
}

var (
gasLimit = new(big.Int).SetUint64(parent.GasLimit)
gasTarget = parent.GasLimit / params.ElasticityMultiplier
gasTargetBig = new(big.Int).SetUint64(gasTarget)
holiman marked this conversation as resolved.
Show resolved Hide resolved
baseFeeChangeDenominator = new(big.Int).SetUint64(params.BaseFeeChangeDenominator)
)

if parent.GasUsed > parent.GasLimit {
// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
if parent.GasUsed == gasTarget {
return new(big.Int).Set(parent.BaseFee)
}

if parent.GasUsed > gasTarget {
// If the parent block used more gas than its target, the baseFee should increase.
gasUsedDelta := new(big.Int).SetUint64(parent.GasUsed - parent.GasLimit)
gasUsedDelta := new(big.Int).SetUint64(parent.GasUsed - gasTarget)
x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta)
y := x.Div(x, gasLimit)
y := x.Div(x, gasTargetBig)
baseFeeDelta := math.BigMax(
x.Div(y, baseFeeChangeDenominator),
common.Big1,
Expand All @@ -75,9 +76,9 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
return x.Add(parent.BaseFee, baseFeeDelta)
} else {
// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
gasUsedDelta := new(big.Int).SetUint64(parent.GasLimit - parent.GasUsed)
gasUsedDelta := new(big.Int).SetUint64(gasTarget - parent.GasUsed)
x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta)
y := x.Div(x, gasLimit)
y := x.Div(x, gasTargetBig)
baseFeeDelta := x.Div(y, baseFeeChangeDenominator)

return math.BigMax(
Expand Down
10 changes: 5 additions & 5 deletions consensus/misc/eip1559_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func TestBlockElasticity(t *testing.T) {
initial := new(big.Int).SetUint64(params.InitialBaseFee)
parent := &types.Header{
GasUsed: 10000000,
GasLimit: 10000000,
GasLimit: 20000000,
BaseFee: initial,
}
header := &types.Header{
GasUsed: 20000000,
GasLimit: 10000000,
GasLimit: 20000000,
BaseFee: initial,
}
if err := VerifyEip1559Header(config(), parent, header); err != nil {
Expand All @@ -64,21 +64,21 @@ func TestCalcBaseFee(t *testing.T) {
// baseFee should remain unchaned when the gasUsed is equal to the gasTarget
{
new(big.Int).SetUint64(params.InitialBaseFee),
10000000,
20000000,
10000000,
new(big.Int).SetUint64(params.InitialBaseFee),
},
// baseFee should decrease when the gasUsed is below the gasTarget
{
new(big.Int).SetUint64(params.InitialBaseFee),
10000000,
20000000,
9000000,
new(big.Int).SetUint64(987500000),
},
// baseFee should increase when the gasUsed is below the gasTarget
{
new(big.Int).SetUint64(params.InitialBaseFee),
10000000,
20000000,
11000000,
new(big.Int).SetUint64(1012500000),
},
Expand Down
8 changes: 1 addition & 7 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
usedGas = new(uint64)
header = block.Header()
allLogs []*types.Log
gp *GasPool
gp = new(GasPool).AddGas(block.GasLimit())
)
// Allow EIP-1559 to expand blocks larger than the gas target
if p.config.IsLondon(block.Number()) {
gp = new(GasPool).AddGas(block.GasLimit() * params.ElasticityMultiplier)
} else {
gp = new(GasPool).AddGas(block.GasLimit())
}
// Mutate the block and state according to any hard-fork specs
if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
misc.ApplyDAOHardFork(statedb)
Expand Down