consensus/misc/eip1559: port to config2

This commit is contained in:
Felix Lange 2025-07-16 01:15:54 +02:00
parent 9268b84142
commit d0a36a349a
2 changed files with 40 additions and 35 deletions

View file

@ -25,16 +25,39 @@ import (
"github.com/ethereum/go-ethereum/consensus/misc" "github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/params/forks"
) )
type FeeConfig struct {
ElasticityMultiplier uint64 `json:"elasticityMultiplier"`
BaseFeeChangeDenominator uint64 `json:"baseFeeChangeDenominator"`
}
func (fc FeeConfig) Validate(config *params.Config2) error {
return nil
}
func init() {
params.Define(params.Parameter[FeeConfig]{
Name: "eip1559Config",
Optional: true,
Default: FeeConfig{
ElasticityMultiplier: params.DefaultElasticityMultiplier,
BaseFeeChangeDenominator: params.DefaultBaseFeeChangeDenominator,
},
})
}
// VerifyEIP1559Header verifies some header attributes which were changed in EIP-1559, // VerifyEIP1559Header verifies some header attributes which were changed in EIP-1559,
// - gas limit check // - gas limit check
// - basefee check // - basefee check
func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Header) error { func VerifyEIP1559Header(config *params.Config2, parent, header *types.Header) error {
feecfg := params.Get[FeeConfig](config)
// Verify that the gas limit remains within allowed bounds // Verify that the gas limit remains within allowed bounds
parentGasLimit := parent.GasLimit parentGasLimit := parent.GasLimit
if !config.IsLondon(parent.Number) { if !config.Active(forks.London, parent.Number.Uint64(), parent.Time) {
parentGasLimit = parent.GasLimit * config.ElasticityMultiplier() parentGasLimit = parent.GasLimit * feecfg.ElasticityMultiplier
} }
if err := misc.VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil { if err := misc.VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil {
return err return err
@ -53,13 +76,15 @@ func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Heade
} }
// CalcBaseFee calculates the basefee of the header. // CalcBaseFee calculates the basefee of the header.
func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int { func CalcBaseFee(config *params.Config2, parent *types.Header) *big.Int {
feecfg := params.Get[FeeConfig](config)
// If the current block is the first EIP-1559 block, return the InitialBaseFee. // If the current block is the first EIP-1559 block, return the InitialBaseFee.
if !config.IsLondon(parent.Number) { if !config.Active(forks.London, parent.Number.Uint64(), parent.Time) {
return new(big.Int).SetUint64(params.InitialBaseFee) return new(big.Int).SetUint64(params.InitialBaseFee)
} }
parentGasTarget := parent.GasLimit / config.ElasticityMultiplier() parentGasTarget := parent.GasLimit / feecfg.ElasticityMultiplier
// If the parent gasUsed is the same as the target, the baseFee remains unchanged. // If the parent gasUsed is the same as the target, the baseFee remains unchanged.
if parent.GasUsed == parentGasTarget { if parent.GasUsed == parentGasTarget {
return new(big.Int).Set(parent.BaseFee) return new(big.Int).Set(parent.BaseFee)
@ -76,7 +101,7 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
num.SetUint64(parent.GasUsed - parentGasTarget) num.SetUint64(parent.GasUsed - parentGasTarget)
num.Mul(num, parent.BaseFee) num.Mul(num, parent.BaseFee)
num.Div(num, denom.SetUint64(parentGasTarget)) num.Div(num, denom.SetUint64(parentGasTarget))
num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator())) num.Div(num, denom.SetUint64(feecfg.BaseFeeChangeDenominator))
if num.Cmp(common.Big1) < 0 { if num.Cmp(common.Big1) < 0 {
return num.Add(parent.BaseFee, common.Big1) return num.Add(parent.BaseFee, common.Big1)
} }
@ -87,7 +112,7 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
num.SetUint64(parentGasTarget - parent.GasUsed) num.SetUint64(parentGasTarget - parent.GasUsed)
num.Mul(num, parent.BaseFee) num.Mul(num, parent.BaseFee)
num.Div(num, denom.SetUint64(parentGasTarget)) num.Div(num, denom.SetUint64(parentGasTarget))
num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator())) num.Div(num, denom.SetUint64(feecfg.BaseFeeChangeDenominator))
baseFee := num.Sub(parent.BaseFee, num) baseFee := num.Sub(parent.BaseFee, num)
if baseFee.Cmp(common.Big0) < 0 { if baseFee.Cmp(common.Big0) < 0 {

View file

@ -23,35 +23,15 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/params/forks"
"github.com/ethereum/go-ethereum/params/presets"
) )
// copyConfig does a _shallow_ copy of a given config. Safe to set new values, but func config() *params.Config2 {
// do not use e.g. SetInt() on the numbers. For testing only config := presets.AllEthashProtocolChanges
func copyConfig(original *params.ChainConfig) *params.ChainConfig { config = config.SetActivations(params.Activations{
return &params.ChainConfig{ forks.London: 5,
ChainID: original.ChainID, })
HomesteadBlock: original.HomesteadBlock,
DAOForkBlock: original.DAOForkBlock,
DAOForkSupport: original.DAOForkSupport,
EIP150Block: original.EIP150Block,
EIP155Block: original.EIP155Block,
EIP158Block: original.EIP158Block,
ByzantiumBlock: original.ByzantiumBlock,
ConstantinopleBlock: original.ConstantinopleBlock,
PetersburgBlock: original.PetersburgBlock,
IstanbulBlock: original.IstanbulBlock,
MuirGlacierBlock: original.MuirGlacierBlock,
BerlinBlock: original.BerlinBlock,
LondonBlock: original.LondonBlock,
TerminalTotalDifficulty: original.TerminalTotalDifficulty,
Ethash: original.Ethash,
Clique: original.Clique,
}
}
func config() *params.ChainConfig {
config := copyConfig(params.TestChainConfig)
config.LondonBlock = big.NewInt(5)
return config return config
} }