mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.6 KiB
96 lines
3.6 KiB
4 years ago
|
// 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/>.
|
||
|
|
||
1 year ago
|
package eip1559
|
||
4 years ago
|
|
||
|
import (
|
||
2 years ago
|
"errors"
|
||
4 years ago
|
"fmt"
|
||
|
"math/big"
|
||
|
|
||
|
"github.com/ethereum/go-ethereum/common"
|
||
|
"github.com/ethereum/go-ethereum/common/math"
|
||
1 year ago
|
"github.com/ethereum/go-ethereum/consensus/misc"
|
||
4 years ago
|
"github.com/ethereum/go-ethereum/core/types"
|
||
|
"github.com/ethereum/go-ethereum/params"
|
||
|
)
|
||
|
|
||
2 years ago
|
// VerifyEIP1559Header verifies some header attributes which were changed in EIP-1559,
|
||
4 years ago
|
// - gas limit check
|
||
|
// - basefee check
|
||
2 years ago
|
func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Header) error {
|
||
4 years ago
|
// Verify that the gas limit remains within allowed bounds
|
||
|
parentGasLimit := parent.GasLimit
|
||
|
if !config.IsLondon(parent.Number) {
|
||
2 years ago
|
parentGasLimit = parent.GasLimit * config.ElasticityMultiplier()
|
||
4 years ago
|
}
|
||
1 year ago
|
if err := misc.VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil {
|
||
4 years ago
|
return err
|
||
|
}
|
||
|
// Verify the header is not malformed
|
||
|
if header.BaseFee == nil {
|
||
2 years ago
|
return errors.New("header is missing baseFee")
|
||
4 years ago
|
}
|
||
|
// 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",
|
||
3 years ago
|
header.BaseFee, expectedBaseFee, parent.BaseFee, parent.GasUsed)
|
||
4 years ago
|
}
|
||
|
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)
|
||
|
}
|
||
|
|
||
2 years ago
|
parentGasTarget := parent.GasLimit / config.ElasticityMultiplier()
|
||
4 years ago
|
// 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)
|
||
|
}
|
||
3 years ago
|
|
||
|
var (
|
||
|
num = new(big.Int)
|
||
|
denom = new(big.Int)
|
||
|
)
|
||
|
|
||
4 years ago
|
if parent.GasUsed > parentGasTarget {
|
||
|
// If the parent block used more gas than its target, the baseFee should increase.
|
||
3 years ago
|
// max(1, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
|
||
|
num.SetUint64(parent.GasUsed - parentGasTarget)
|
||
|
num.Mul(num, parent.BaseFee)
|
||
|
num.Div(num, denom.SetUint64(parentGasTarget))
|
||
2 years ago
|
num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator()))
|
||
3 years ago
|
baseFeeDelta := math.BigMax(num, common.Big1)
|
||
4 years ago
|
|
||
3 years ago
|
return num.Add(parent.BaseFee, baseFeeDelta)
|
||
4 years ago
|
} else {
|
||
|
// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
|
||
3 years ago
|
// max(0, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
|
||
|
num.SetUint64(parentGasTarget - parent.GasUsed)
|
||
|
num.Mul(num, parent.BaseFee)
|
||
|
num.Div(num, denom.SetUint64(parentGasTarget))
|
||
2 years ago
|
num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator()))
|
||
3 years ago
|
baseFee := num.Sub(parent.BaseFee, num)
|
||
4 years ago
|
|
||
3 years ago
|
return math.BigMax(baseFee, common.Big0)
|
||
4 years ago
|
}
|
||
|
}
|