Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement EIP7783: Controlled gas limit increase #12273

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions consensus/misc/eip7783.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
29 changes: 29 additions & 0 deletions consensus/misc/eip7783_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
9 changes: 6 additions & 3 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 14 additions & 1 deletion eth/stagedsync/stage_mining_create_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,21 @@ func SpawnMiningCreateBlockStage(s *StageState, txc wrap.TxContainer, cfg Mining
family: mapset.NewSet[libcommon.Hash](),
uncles: mapset.NewSet[libcommon.Hash](),
}
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 := core.MakeEmptyHeader(parent, &cfg.chainConfig, timestamp, &cfg.miner.MiningConfig.GasLimit)
if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil {
logger.Warn("Failed to verify gas limit given by the validator, defaulting to parent gas limit", "err", err)
header.GasLimit = parent.GasLimit
Expand Down
6 changes: 6 additions & 0 deletions params/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions turbo/cli/default_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading