2021-05-17 16:13:22 +03:00
|
|
|
// Copyright 2021 The go-ethereum Authors
|
|
|
|
// 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/>.
|
|
|
|
|
2023-08-01 12:58:45 +03:00
|
|
|
package eip1559
|
2021-05-17 16:13:22 +03:00
|
|
|
|
|
|
|
import (
|
2023-05-25 09:57:34 +03:00
|
|
|
"errors"
|
2021-05-17 16:13:22 +03:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-08-01 12:58:45 +03:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/misc"
|
2021-05-17 16:13:22 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
2023-05-31 10:21:13 +03:00
|
|
|
// VerifyEIP1559Header verifies some header attributes which were changed in EIP-1559,
|
2021-05-17 16:13:22 +03:00
|
|
|
// - gas limit check
|
|
|
|
// - basefee check
|
2023-05-31 10:21:13 +03:00
|
|
|
func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Header) error {
|
2021-05-17 16:13:22 +03:00
|
|
|
// Verify that the gas limit remains within allowed bounds
|
|
|
|
parentGasLimit := parent.GasLimit
|
|
|
|
if !config.IsLondon(parent.Number) {
|
2022-11-04 18:27:15 +03:00
|
|
|
parentGasLimit = parent.GasLimit * config.ElasticityMultiplier()
|
2021-05-17 16:13:22 +03:00
|
|
|
}
|
2023-08-01 12:58:45 +03:00
|
|
|
if err := misc.VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil {
|
2021-05-17 16:13:22 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Verify the header is not malformed
|
|
|
|
if header.BaseFee == nil {
|
2023-05-25 09:57:34 +03:00
|
|
|
return errors.New("header is missing baseFee")
|
2021-05-17 16:13:22 +03:00
|
|
|
}
|
|
|
|
// Verify the baseFee is correct based on the parent header.
|
|
|
|
expectedBaseFee := CalcBaseFee(config, parent)
|
|
|
|
if header.BaseFee.Cmp(expectedBaseFee) != 0 {
|
|
|
|
return fmt.Errorf("invalid baseFee: have %s, want %s, parentBaseFee %s, parentGasUsed %d",
|
2022-04-27 12:45:14 +03:00
|
|
|
header.BaseFee, expectedBaseFee, parent.BaseFee, parent.GasUsed)
|
2021-05-17 16:13:22 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalcBaseFee calculates the basefee of the header.
|
|
|
|
func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
|
|
|
|
// If the current block is the first EIP-1559 block, return the InitialBaseFee.
|
|
|
|
if !config.IsLondon(parent.Number) {
|
|
|
|
return new(big.Int).SetUint64(params.InitialBaseFee)
|
|
|
|
}
|
|
|
|
|
2022-11-04 18:27:15 +03:00
|
|
|
parentGasTarget := parent.GasLimit / config.ElasticityMultiplier()
|
2021-05-17 16:13:22 +03:00
|
|
|
// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
|
|
|
|
if parent.GasUsed == parentGasTarget {
|
|
|
|
return new(big.Int).Set(parent.BaseFee)
|
|
|
|
}
|
2022-06-02 21:25:12 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
num = new(big.Int)
|
|
|
|
denom = new(big.Int)
|
|
|
|
)
|
|
|
|
|
2021-05-17 16:13:22 +03:00
|
|
|
if parent.GasUsed > parentGasTarget {
|
|
|
|
// If the parent block used more gas than its target, the baseFee should increase.
|
2022-06-02 21:25:12 +03:00
|
|
|
// max(1, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
|
|
|
|
num.SetUint64(parent.GasUsed - parentGasTarget)
|
|
|
|
num.Mul(num, parent.BaseFee)
|
|
|
|
num.Div(num, denom.SetUint64(parentGasTarget))
|
2022-11-04 18:27:15 +03:00
|
|
|
num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator()))
|
2024-10-21 12:45:33 +03:00
|
|
|
if num.Cmp(common.Big1) < 0 {
|
|
|
|
return num.Add(parent.BaseFee, common.Big1)
|
|
|
|
}
|
|
|
|
return num.Add(parent.BaseFee, num)
|
2021-05-17 16:13:22 +03:00
|
|
|
} else {
|
|
|
|
// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
|
2022-06-02 21:25:12 +03:00
|
|
|
// max(0, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
|
|
|
|
num.SetUint64(parentGasTarget - parent.GasUsed)
|
|
|
|
num.Mul(num, parent.BaseFee)
|
|
|
|
num.Div(num, denom.SetUint64(parentGasTarget))
|
2022-11-04 18:27:15 +03:00
|
|
|
num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator()))
|
2021-05-17 16:13:22 +03:00
|
|
|
|
2024-10-21 12:45:33 +03:00
|
|
|
baseFee := num.Sub(parent.BaseFee, num)
|
|
|
|
if baseFee.Cmp(common.Big0) < 0 {
|
|
|
|
baseFee = common.Big0
|
|
|
|
}
|
|
|
|
return baseFee
|
2021-05-17 16:13:22 +03:00
|
|
|
}
|
|
|
|
}
|