From 40cd18d2bfc9f3d098ac2cf4574d6059c6a43a26 Mon Sep 17 00:00:00 2001 From: Giulio Date: Thu, 10 Oct 2024 22:25:00 +0200 Subject: [PATCH] save --- cmd/utils/flags.go | 39 +++++++++++++++++++-- consensus/misc/eip7783.go | 19 ++++++++++ consensus/misc/eip7783_test.go | 29 +++++++++++++++ eth/ethconfig/config.go | 9 +++-- eth/stagedsync/stage_mining_create_block.go | 17 +++++++-- params/mining.go | 6 ++++ turbo/cli/default_flags.go | 4 +++ 7 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 consensus/misc/eip7783.go create mode 100644 consensus/misc/eip7783_test.go diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 57eaeb76051..ca7deaa5198 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -245,8 +245,27 @@ var ( } MinerGasLimitFlag = cli.Uint64Flag{ Name: "miner.gaslimit", - Usage: "Target gas limit for mined blocks", - Value: ethconfig.Defaults.Miner.GasLimit, + Usage: "Target gas limit for mined blocks (fixed value)", + } + Eip7783IncreaseRateFlag = cli.Uint64Flag{ + Name: "eip7783.increase-rate", + Usage: "The rate of gas limit increase per block", + Value: ethconfig.Defaults.Miner.Eip7783IncreaseRate, + } + Eip7783BlockNumStartFlag = cli.Uint64Flag{ + Name: "eip7783.start-block", + Usage: "The block number to start using EIP-7783 gas limit calculation", + Value: ethconfig.Defaults.Miner.EIP7783BlockNumStart, + } + Eip7783InitialGasFlag = cli.Uint64Flag{ + Name: "eip7783.initial-gas", + Usage: "The initial gas limit to use before EIP-7783 calculation", + Value: ethconfig.Defaults.Miner.EIP7783InitialGas, + } + Eip7783GasLimitCapFlag = cli.Uint64Flag{ + Name: "eip7783.gas-limit-cap", + Usage: "The maximum gas limit to use after EIP-7783 calculation", + Value: ethconfig.Defaults.Miner.EIP7783GasLimitCap, } MinerGasPriceFlag = flags.BigFlag{ Name: "miner.gasprice", @@ -1604,6 +1623,22 @@ func SetupMinerCobra(cmd *cobra.Command, cfg *params.MiningConfig) { if err != nil { panic(err) } + cfg.EIP7783BlockNumStart, err = flags.GetUint64(Eip7783BlockNumStartFlag.Name) + if err != nil { + panic(err) + } + cfg.EIP7783GasLimitCap, err = flags.GetUint64(Eip7783GasLimitCapFlag.Name) + if err != nil { + panic(err) + } + cfg.EIP7783InitialGas, err = flags.GetUint64(Eip7783InitialGasFlag.Name) + if err != nil { + panic(err) + } + cfg.Eip7783IncreaseRate, err = flags.GetUint64(Eip7783IncreaseRateFlag.Name) + if err != nil { + panic(err) + } price, err := flags.GetInt64(MinerGasPriceFlag.Name) if err != nil { panic(err) diff --git a/consensus/misc/eip7783.go b/consensus/misc/eip7783.go new file mode 100644 index 00000000000..28f451abb27 --- /dev/null +++ b/consensus/misc/eip7783.go @@ -0,0 +1,19 @@ +package misc + +/* +Implementation of EIP-7783: + +def compute_gas_limit(blockNum: int, blockNumStart: int, initialGasLimit: int, r: int, gasLimitCap: int) -> int: + + if blockNum < blockNumStart: + return initialGasLimit + else: + return min(gasLimitCap, initialGasLimit + r * (blockNum - blockNumStart)) +*/ +func CalcGasLimitEIP7783(blockNum, startBlockNum, initialGasLimit, gasIncreaseRate, gasLimitCap uint64) uint64 { + if blockNum < startBlockNum { + return initialGasLimit + } else { + return min(gasLimitCap, initialGasLimit+gasIncreaseRate*(blockNum-startBlockNum)) + } +} diff --git a/consensus/misc/eip7783_test.go b/consensus/misc/eip7783_test.go new file mode 100644 index 00000000000..9051dd83370 --- /dev/null +++ b/consensus/misc/eip7783_test.go @@ -0,0 +1,29 @@ +package misc + +import "testing" + +func TestCalcGasLimitEIP7783(t *testing.T) { + tests := []struct { + blockNum uint64 + startBlockNum uint64 + initialGasLimit uint64 + gasIncreaseRate uint64 + gasLimitCap uint64 + expectedGasLimit uint64 + }{ + {1, 1, 100000, 1000, 1000000, 100000}, + {1, 2, 100000, 1000, 1000000, 100000}, + {2, 1, 100000, 1000, 1000000, 101000}, + {2, 2, 100000, 1000, 1000000, 100000}, + {3, 2, 100000, 1000, 1000000, 101000}, + {10, 2, 100000, 1000, 1000000, 108000}, + {10, 2, 100000, 10000, 1000000, 180000}, + } + + for i, test := range tests { + got := CalcGasLimitEIP7783(test.blockNum, test.startBlockNum, test.initialGasLimit, test.gasIncreaseRate, test.gasLimitCap) + if got != test.expectedGasLimit { + t.Errorf("Test %d: expected %d, got %d", i, test.expectedGasLimit, got) + } + } +} diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index fca80e7df87..bab715a2b6f 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -90,9 +90,12 @@ var Defaults = Config{ NetworkID: 1, Prune: prune.DefaultMode, Miner: params.MiningConfig{ - GasLimit: 30_000_000, - GasPrice: big.NewInt(params.GWei), - Recommit: 3 * time.Second, + GasPrice: big.NewInt(params.GWei), + Recommit: 3 * time.Second, + Eip7783IncreaseRate: 6, + EIP7783BlockNumStart: 21400000, + EIP7783InitialGas: 30_000_000, + EIP7783GasLimitCap: 60_000_000, }, DeprecatedTxPool: DeprecatedDefaultTxPoolConfig, TxPool: txpoolcfg.DefaultConfig, diff --git a/eth/stagedsync/stage_mining_create_block.go b/eth/stagedsync/stage_mining_create_block.go index 8e9c32fbfdd..412e2ffc387 100644 --- a/eth/stagedsync/stage_mining_create_block.go +++ b/eth/stagedsync/stage_mining_create_block.go @@ -34,6 +34,7 @@ import ( "github.com/erigontech/erigon-lib/kv" "github.com/erigontech/erigon/common/debug" "github.com/erigontech/erigon/consensus" + "github.com/erigontech/erigon/consensus/misc" "github.com/erigontech/erigon/core" "github.com/erigontech/erigon/core/rawdb" "github.com/erigontech/erigon/core/state" @@ -179,8 +180,20 @@ func SpawnMiningCreateBlockStage(s *StageState, txc wrap.TxContainer, cfg Mining family: mapset.NewSet[libcommon.Hash](), uncles: mapset.NewSet[libcommon.Hash](), } - - header := core.MakeEmptyHeader(parent, &cfg.chainConfig, timestamp, &cfg.miner.MiningConfig.GasLimit) + var header *types.Header + useEip7783 := cfg.miner.MiningConfig.GasLimit == 0 + if useEip7783 { + gasLimit := misc.CalcGasLimitEIP7783( + parent.Number.Uint64()+1, + cfg.miner.MiningConfig.EIP7783BlockNumStart, + cfg.miner.MiningConfig.EIP7783InitialGas, + cfg.miner.MiningConfig.Eip7783IncreaseRate, + cfg.miner.MiningConfig.EIP7783GasLimitCap, + ) + header = core.MakeEmptyHeader(parent, &cfg.chainConfig, timestamp, &gasLimit) + } else { + header = core.MakeEmptyHeader(parent, &cfg.chainConfig, timestamp, &cfg.miner.MiningConfig.GasLimit) + } header.Coinbase = coinbase header.Extra = cfg.miner.MiningConfig.ExtraData diff --git a/params/mining.go b/params/mining.go index 7993d3be43a..a9b590a02a8 100644 --- a/params/mining.go +++ b/params/mining.go @@ -37,4 +37,10 @@ type MiningConfig struct { GasLimit uint64 // Target gas limit for mined blocks. GasPrice *big.Int // Minimum gas price for mining a transaction Recommit time.Duration // The time interval for miner to re-create mining work. + + // EIP-7783 parameters + EIP7783BlockNumStart uint64 // The block number to start using EIP-7783 gas limit calculation + EIP7783InitialGas uint64 // The initial gas limit to use before EIP-7783 calculation + Eip7783IncreaseRate uint64 // The rate of gas limit increase per block + EIP7783GasLimitCap uint64 // The maximum gas limit to use after EIP-7783 calculation } diff --git a/turbo/cli/default_flags.go b/turbo/cli/default_flags.go index 74209d0898d..aa5947dc846 100644 --- a/turbo/cli/default_flags.go +++ b/turbo/cli/default_flags.go @@ -153,6 +153,10 @@ var DefaultFlags = []cli.Flag{ &utils.ProposingDisableFlag, &utils.MinerNotifyFlag, &utils.MinerGasLimitFlag, + &utils.Eip7783BlockNumStartFlag, + &utils.Eip7783GasLimitCapFlag, + &utils.Eip7783IncreaseRateFlag, + &utils.Eip7783InitialGasFlag, &utils.MinerEtherbaseFlag, &utils.MinerExtraDataFlag, &utils.MinerNoVerfiyFlag,