From d0a36a349a622e9e691e0ac204d45d8953fd3713 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 16 Jul 2025 01:15:54 +0200 Subject: [PATCH] consensus/misc/eip1559: port to config2 --- consensus/misc/eip1559/eip1559.go | 41 +++++++++++++++++++++----- consensus/misc/eip1559/eip1559_test.go | 34 +++++---------------- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/consensus/misc/eip1559/eip1559.go b/consensus/misc/eip1559/eip1559.go index a90bd744b2..a9981d1958 100644 --- a/consensus/misc/eip1559/eip1559.go +++ b/consensus/misc/eip1559/eip1559.go @@ -25,16 +25,39 @@ import ( "github.com/ethereum/go-ethereum/consensus/misc" "github.com/ethereum/go-ethereum/core/types" "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, // - gas limit 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 parentGasLimit := parent.GasLimit - if !config.IsLondon(parent.Number) { - parentGasLimit = parent.GasLimit * config.ElasticityMultiplier() + if !config.Active(forks.London, parent.Number.Uint64(), parent.Time) { + parentGasLimit = parent.GasLimit * feecfg.ElasticityMultiplier } if err := misc.VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil { return err @@ -53,13 +76,15 @@ func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Heade } // 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 !config.IsLondon(parent.Number) { + if !config.Active(forks.London, parent.Number.Uint64(), parent.Time) { 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 parent.GasUsed == parentGasTarget { 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.Mul(num, parent.BaseFee) 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 { 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.Mul(num, parent.BaseFee) 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) if baseFee.Cmp(common.Big0) < 0 { diff --git a/consensus/misc/eip1559/eip1559_test.go b/consensus/misc/eip1559/eip1559_test.go index b5afdf0fe5..2a9841b234 100644 --- a/consensus/misc/eip1559/eip1559_test.go +++ b/consensus/misc/eip1559/eip1559_test.go @@ -23,35 +23,15 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "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 -// do not use e.g. SetInt() on the numbers. For testing only -func copyConfig(original *params.ChainConfig) *params.ChainConfig { - return ¶ms.ChainConfig{ - 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) +func config() *params.Config2 { + config := presets.AllEthashProtocolChanges + config = config.SetActivations(params.Activations{ + forks.London: 5, + }) return config }