From d14c22388193ffcac0e8ff6b354680568e9b1abe Mon Sep 17 00:00:00 2001 From: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com> Date: Wed, 5 Jan 2022 10:36:24 +0100 Subject: [PATCH] Simplify miner gas limit post EIP-1559. Default to 30M (#3205) --- cmd/integration/commands/flags.go | 3 +- cmd/utils/flags.go | 20 ++------- core/block_validator.go | 45 ++++++++------------- core/chain_makers.go | 6 ++- eth/ethconfig/config.go | 3 +- eth/stagedsync/stage_mining_create_block.go | 4 +- params/mining.go | 3 +- turbo/cli/default_flags.go | 1 - 8 files changed, 31 insertions(+), 54 deletions(-) diff --git a/cmd/integration/commands/flags.go b/cmd/integration/commands/flags.go index de7705a11..4be25758f 100644 --- a/cmd/integration/commands/flags.go +++ b/cmd/integration/commands/flags.go @@ -40,8 +40,7 @@ func must(err error) { func withMining(cmd *cobra.Command) { cmd.Flags().Bool("mine", false, "Enable mining") cmd.Flags().StringArray("miner.notify", nil, "Comma separated HTTP URL list to notify of new work packages") - cmd.Flags().Uint64("miner.gastarget", ethconfig.Defaults.Miner.GasFloor, "Target gas floor for mined blocks") - cmd.Flags().Uint64("miner.gaslimit", ethconfig.Defaults.Miner.GasCeil, "Target gas ceiling for mined blocks") + cmd.Flags().Uint64("miner.gaslimit", ethconfig.Defaults.Miner.GasLimit, "Target gas limit for mined blocks") cmd.Flags().Int64("miner.gasprice", ethconfig.Defaults.Miner.GasPrice.Int64(), "Target gas price for mined blocks") cmd.Flags().String("miner.etherbase", "0", "Public address for block mining rewards (default = first account") cmd.Flags().String("miner.extradata", "", "Block extra data set by the miner (default = client version)") diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 5b0895ea0..78fcd4f49 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -233,15 +233,10 @@ var ( Name: "miner.notify", Usage: "Comma separated HTTP URL list to notify of new work packages", } - MinerGasTargetFlag = cli.Uint64Flag{ - Name: "miner.gastarget", - Usage: "Target gas floor for mined blocks", - Value: ethconfig.Defaults.Miner.GasFloor, - } MinerGasLimitFlag = cli.Uint64Flag{ Name: "miner.gaslimit", - Usage: "Target gas ceiling for mined blocks", - Value: ethconfig.Defaults.Miner.GasCeil, + Usage: "Target gas limit for mined blocks", + Value: ethconfig.Defaults.Miner.GasLimit, } MinerGasPriceFlag = BigFlag{ Name: "miner.gasprice", @@ -1098,11 +1093,7 @@ func SetupMinerCobra(cmd *cobra.Command, cfg *params.MiningConfig) { panic(err) } cfg.ExtraData = []byte(extraDataStr) - cfg.GasFloor, err = flags.GetUint64(MinerGasTargetFlag.Name) - if err != nil { - panic(err) - } - cfg.GasCeil, err = flags.GetUint64(MinerGasLimitFlag.Name) + cfg.GasLimit, err = flags.GetUint64(MinerGasLimitFlag.Name) if err != nil { panic(err) } @@ -1166,11 +1157,8 @@ func setMiner(ctx *cli.Context, cfg *params.MiningConfig) { if ctx.GlobalIsSet(MinerExtraDataFlag.Name) { cfg.ExtraData = []byte(ctx.GlobalString(MinerExtraDataFlag.Name)) } - if ctx.GlobalIsSet(MinerGasTargetFlag.Name) { - cfg.GasFloor = ctx.GlobalUint64(MinerGasTargetFlag.Name) - } if ctx.GlobalIsSet(MinerGasLimitFlag.Name) { - cfg.GasCeil = ctx.GlobalUint64(MinerGasLimitFlag.Name) + cfg.GasLimit = ctx.GlobalUint64(MinerGasLimitFlag.Name) } if ctx.GlobalIsSet(MinerGasPriceFlag.Name) { cfg.GasPrice = GlobalBig(ctx, MinerGasPriceFlag.Name) diff --git a/core/block_validator.go b/core/block_validator.go index a88b7d8f5..57dd9bbb3 100644 --- a/core/block_validator.go +++ b/core/block_validator.go @@ -21,37 +21,26 @@ import ( ) // CalcGasLimit computes the gas limit of the next block after parent. It aims -// to keep the baseline gas above the provided floor, and increase it towards the -// ceil if the blocks are full. If the ceil is exceeded, it will always decrease -// the gas allowance. -func CalcGasLimit(parentGasUsed, parentGasLimit, gasFloor, gasCeil uint64) uint64 { - // contrib = (parentGasUsed * 3 / 2) / 1024 - contrib := (parentGasUsed + parentGasUsed/2) / params.GasLimitBoundDivisor - - // decay = parentGasLimit / 1024 -1 - decay := parentGasLimit/params.GasLimitBoundDivisor - 1 - - /* - strategy: gasLimit of block-to-mine is set based on parent's - gasUsed value. if parentGasUsed > parentGasLimit * (2/3) then we - increase it, otherwise lower it (or leave it unchanged if it's right - at that usage) the amount increased/decreased depends on how far away - from parentGasLimit * (2/3) parentGasUsed is. - */ - limit := parentGasLimit - decay + contrib - if limit < params.MinGasLimit { - limit = params.MinGasLimit +// to keep the baseline gas close to the provided target, and increase it towards +// the target if the baseline gas is lower. +func CalcGasLimit(parentGasLimit, desiredLimit uint64) uint64 { + delta := parentGasLimit/params.GasLimitBoundDivisor - 1 + limit := parentGasLimit + if desiredLimit < params.MinGasLimit { + desiredLimit = params.MinGasLimit } // If we're outside our allowed gas range, we try to hone towards them - if limit < gasFloor { - limit = parentGasLimit + decay - if limit > gasFloor { - limit = gasFloor + if limit < desiredLimit { + limit = parentGasLimit + delta + if limit > desiredLimit { + limit = desiredLimit } - } else if limit > gasCeil { - limit = parentGasLimit - decay - if limit < gasCeil { - limit = gasCeil + return limit + } + if limit > desiredLimit { + limit = parentGasLimit - delta + if limit < desiredLimit { + limit = desiredLimit } } return limit diff --git a/core/chain_makers.go b/core/chain_makers.go index d903b83fa..80ae95073 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -413,7 +413,7 @@ func makeHeader(chain consensus.ChainReader, parent *types.Block, state *state.I parent.UncleHash(), parent.Seal(), ), - GasLimit: CalcGasLimit(parent.GasUsed(), parent.GasLimit(), parent.GasLimit(), parent.GasLimit()), + GasLimit: CalcGasLimit(parent.GasLimit(), parent.GasLimit()), Number: new(big.Int).Add(parent.Number(), common.Big1), Time: time, } @@ -422,6 +422,10 @@ func makeHeader(chain consensus.ChainReader, parent *types.Block, state *state.I if chain.Config().IsLondon(header.Number.Uint64()) { header.BaseFee = misc.CalcBaseFee(chain.Config(), parent.Header()) header.Eip1559 = true + if !chain.Config().IsLondon(parent.NumberU64()) { + parentGasLimit := parent.GasLimit() * params.ElasticityMultiplier + header.GasLimit = CalcGasLimit(parentGasLimit, parentGasLimit) + } } header.WithSeal = chain.Config().IsHeaderWithSeal() diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index 3e6df2d4f..0ba1b7b49 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -78,8 +78,7 @@ var Defaults = Config{ NetworkID: 1, Prune: prune.DefaultMode, Miner: params.MiningConfig{ - GasFloor: 8000000, - GasCeil: 8000000, + GasLimit: 30_000_000, GasPrice: big.NewInt(params.GWei), Recommit: 3 * time.Second, }, diff --git a/eth/stagedsync/stage_mining_create_block.go b/eth/stagedsync/stage_mining_create_block.go index 204d62f23..f4d489b53 100644 --- a/eth/stagedsync/stage_mining_create_block.go +++ b/eth/stagedsync/stage_mining_create_block.go @@ -193,7 +193,7 @@ func SpawnMiningCreateBlockStage(s *StageState, tx kv.RwTx, cfg MiningCreateBloc header := &types.Header{ ParentHash: parent.Hash(), Number: num.Add(num, common.Big1), - GasLimit: core.CalcGasLimit(parent.GasUsed, parent.GasLimit, cfg.miner.MiningConfig.GasFloor, cfg.miner.MiningConfig.GasCeil), + GasLimit: core.CalcGasLimit(parent.GasLimit, cfg.miner.MiningConfig.GasLimit), Extra: cfg.miner.MiningConfig.ExtraData, Time: uint64(timestamp), } @@ -204,7 +204,7 @@ func SpawnMiningCreateBlockStage(s *StageState, tx kv.RwTx, cfg MiningCreateBloc header.BaseFee = misc.CalcBaseFee(&cfg.chainConfig, parent) if !cfg.chainConfig.IsLondon(parent.Number.Uint64()) { parentGasLimit := parent.GasLimit * params.ElasticityMultiplier - header.GasLimit = core.CalcGasLimit(parent.GasUsed, parentGasLimit, cfg.miner.MiningConfig.GasFloor, cfg.miner.MiningConfig.GasCeil) + header.GasLimit = core.CalcGasLimit(parentGasLimit, cfg.miner.MiningConfig.GasLimit) } } log.Info(fmt.Sprintf("[%s] Start mine", logPrefix), "block", executionAt+1, "baseFee", header.BaseFee, "gasLimit", header.GasLimit) diff --git a/params/mining.go b/params/mining.go index 1e9c9a956..ef92a0a51 100644 --- a/params/mining.go +++ b/params/mining.go @@ -18,8 +18,7 @@ type MiningConfig struct { SigKey *ecdsa.PrivateKey // ECDSA private key for signing blocks Notify []string `toml:",omitempty"` // HTTP URL list to be notified of new work packages(only useful in ethash). ExtraData hexutil.Bytes `toml:",omitempty"` // Block extra data set by the miner - GasFloor uint64 // Target gas floor for mined blocks. - GasCeil uint64 // Target gas ceiling for mined blocks. + 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. } diff --git a/turbo/cli/default_flags.go b/turbo/cli/default_flags.go index 58ec3ca30..98cf59de8 100644 --- a/turbo/cli/default_flags.go +++ b/turbo/cli/default_flags.go @@ -79,7 +79,6 @@ var DefaultFlags = []cli.Flag{ utils.MiningEnabledFlag, utils.ProposingEnabledFlag, utils.MinerNotifyFlag, - utils.MinerGasTargetFlag, utils.MinerGasLimitFlag, utils.MinerEtherbaseFlag, utils.MinerExtraDataFlag,