From 112a6f7d76e0ce02e394c677a7cc492ea88cfdf5 Mon Sep 17 00:00:00 2001 From: jangko Date: Mon, 29 Aug 2022 16:54:59 +0700 Subject: [PATCH] move sealing engine gaslimit calculator to txpool fix #1032 --- nimbus/sealer.nim | 19 ------------- nimbus/utils/tx_pool.nim | 14 ++++++++-- nimbus/utils/tx_pool/tx_chain.nim | 2 ++ .../utils/tx_pool/tx_chain/tx_gaslimits.nim | 28 +++++++++++++++---- 4 files changed, 37 insertions(+), 26 deletions(-) diff --git a/nimbus/sealer.nim b/nimbus/sealer.nim index b5105a2f7..3aca8ae3d 100644 --- a/nimbus/sealer.nim +++ b/nimbus/sealer.nim @@ -70,9 +70,6 @@ proc validateSealer*(conf: NimbusConf, ctx: EthContext, chain: Chain): Result[vo ok() -proc isLondon(c: ChainConfig, number: BlockNumber): bool {.inline.} = - number >= c.londonBlock - proc prepareBlock(engine: SealingEngineRef, parent: BlockHeader, time: Time, @@ -86,22 +83,6 @@ proc prepareBlock(engine: SealingEngineRef, var blk = engine.txPool.ethBlock() - # TODO: fix txPool GasLimit to match this GasLimit - let conf = engine.chain.db.config - if isLondon(conf, blk.header.blockNumber): - var parentGasLimit = parent.gasLimit - if not isLondon(conf, parent.blockNumber): - # Bump by 2x - parentGasLimit = parent.gasLimit * EIP1559_ELASTICITY_MULTIPLIER - # TODO: desiredLimit can be configured by user, gasCeil - blk.header.gasLimit = calcGasLimit1559(parentGasLimit, desiredLimit = DEFAULT_GAS_LIMIT) - else: - blk.header.gasLimit = computeGasLimit( - parent.gasUsed, - parent.gasLimit, - gasFloor = DEFAULT_GAS_LIMIT, - gasCeil = DEFAULT_GAS_LIMIT) - if engine.chain.isBlockAfterTtd(blk.header): blk.header.difficulty = DifficultyInt.zero blk.header.mixDigest = prevRandao diff --git a/nimbus/utils/tx_pool.nim b/nimbus/utils/tx_pool.nim index 3ee3b82b0..28d77aa51 100644 --- a/nimbus/utils/tx_pool.nim +++ b/nimbus/utils/tx_pool.nim @@ -719,7 +719,12 @@ proc `baseFee=`*(xp: TxPoolRef; val: GasPrice) proc `lwmTrgPercent=`*(xp: TxPoolRef; val: int) = ## Setter, `val` arguments outside `0..100` are ignored if 0 <= val and val <= 100: - xp.chain.lhwm = (lwmTrg: val, hwmMax: xp.chain.lhwm.hwmMax) + xp.chain.lhwm = ( + lwmTrg: val, + hwmMax: xp.chain.lhwm.hwmMax, + gasFloor: xp.chain.lhwm.gasFloor, + gasCeil: xp.chain.lhwm.gasCeil + ) proc `flags=`*(xp: TxPoolRef; val: set[TxPoolFlags]) {.gcsafe,raises: [Defect,CatchableError].} = @@ -729,7 +734,12 @@ proc `flags=`*(xp: TxPoolRef; val: set[TxPoolFlags]) proc `hwmMaxPercent=`*(xp: TxPoolRef; val: int) = ## Setter, `val` arguments outside `0..100` are ignored if 0 <= val and val <= 100: - xp.chain.lhwm = (lwmTrg: xp.chain.lhwm.lwmTrg, hwmMax: val) + xp.chain.lhwm = ( + lwmTrg: xp.chain.lhwm.lwmTrg, + hwmMax: val, + gasFloor: xp.chain.lhwm.gasFloor, + gasCeil: xp.chain.lhwm.gasCeil + ) proc `maxRejects=`*(xp: TxPoolRef; val: int) = ## Setter, the size of the waste basket. This setting becomes effective with diff --git a/nimbus/utils/tx_pool/tx_chain.nim b/nimbus/utils/tx_pool/tx_chain.nim index 346c5eb80..40f3e47eb 100644 --- a/nimbus/utils/tx_pool/tx_chain.nim +++ b/nimbus/utils/tx_pool/tx_chain.nim @@ -128,6 +128,8 @@ proc new*(T: type TxChainRef; db: BaseChainDB; miner: EthAddress): T result.miner = miner result.lhwm.lwmTrg = TRG_THRESHOLD_PER_CENT result.lhwm.hwmMax = MAX_THRESHOLD_PER_CENT + result.lhwm.gasFloor = DEFAULT_GAS_LIMIT + result.lhwm.gasCeil = DEFAULT_GAS_LIMIT result.calcDifficulty = proc(timeStamp: EthTime, parent: BlockHeader): DifficultyInt {.gcsafe, raises:[].} = try: diff --git a/nimbus/utils/tx_pool/tx_chain/tx_gaslimits.nim b/nimbus/utils/tx_pool/tx_chain/tx_gaslimits.nim index 57076e4f3..1d7d52e06 100644 --- a/nimbus/utils/tx_pool/tx_chain/tx_gaslimits.nim +++ b/nimbus/utils/tx_pool/tx_chain/tx_gaslimits.nim @@ -17,8 +17,8 @@ import ../../../chain_config, ../../../db/db_chain, ../../../constants, - ../../../forks, - eth/[common] + ../../../utils/header, + eth/[common, eip1559] {.push raises: [Defect].} @@ -30,6 +30,10 @@ type hwmMax: int ##\ ## VM executor may stop if this per centage of `maxLimit` has ## been reached. + gasFloor: GasInt + ## minimum desired gas limit + gasCeil: GasInt + ## maximum desired gas limit TxChainGasLimits* = tuple gasLimit: GasInt ## Parent gas limit, used as a base for others @@ -83,6 +87,9 @@ proc setPreLondonLimits(gl: var TxChainGasLimits) = gl.minLimit = gl.gasLimit - delta gl.trgLimit = gl.gasLimit +proc isLondon(c: ChainConfig, number: BlockNumber): bool {.inline.} = + number >= c.londonBlock + # ------------------------------------------------------------------------------ # Public functions # ------------------------------------------------------------------------------ @@ -92,9 +99,7 @@ proc gasLimitsGet*(db: BaseChainDB; parent: BlockHeader; parentLimit: GasInt; ## Calculate gas limits for the next block header. result.gasLimit = parentLimit - let nextFork = db.config.toFork(parent.blockNumber + 1) - - if FkLondon <= nextFork: + if isLondon(db.config, parent.blockNumber+1): result.setPostLondonLimits else: result.setPreLondonLimits @@ -106,6 +111,19 @@ proc gasLimitsGet*(db: BaseChainDB; parent: BlockHeader; parentLimit: GasInt; result.hwmLimit = max( result.trgLimit, (result.maxLimit * pc.hwmMax + 50) div 100) + # override trgLimit, see https://github.com/status-im/nimbus-eth1/issues/1032 + if isLondon(db.config, parent.blockNumber+1): + var parentGasLimit = parent.gasLimit + if not isLondon(db.config, parent.blockNumber): + # Bump by 2x + parentGasLimit = parent.gasLimit * EIP1559_ELASTICITY_MULTIPLIER + result.trgLimit = calcGasLimit1559(parentGasLimit, desiredLimit = pc.gasCeil) + else: + result.trgLimit = computeGasLimit( + parent.gasUsed, + parent.gasLimit, + gasFloor = pc.gasFloor, + gasCeil = pc.gasCeil) proc gasLimitsGet*(db: BaseChainDB; parent: BlockHeader; pc: TxChainGasLimitsPc): TxChainGasLimits =