mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 21:56:43 +00:00
consensus/misc/eip1559: port to config2
This commit is contained in:
parent
9268b84142
commit
d0a36a349a
2 changed files with 40 additions and 35 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue