move sealing engine gaslimit calculator to txpool

fix #1032
This commit is contained in:
jangko 2022-08-29 16:54:59 +07:00
parent c6f35142a8
commit 112a6f7d76
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
4 changed files with 37 additions and 26 deletions

View File

@ -70,9 +70,6 @@ proc validateSealer*(conf: NimbusConf, ctx: EthContext, chain: Chain): Result[vo
ok() ok()
proc isLondon(c: ChainConfig, number: BlockNumber): bool {.inline.} =
number >= c.londonBlock
proc prepareBlock(engine: SealingEngineRef, proc prepareBlock(engine: SealingEngineRef,
parent: BlockHeader, parent: BlockHeader,
time: Time, time: Time,
@ -86,22 +83,6 @@ proc prepareBlock(engine: SealingEngineRef,
var blk = engine.txPool.ethBlock() 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): if engine.chain.isBlockAfterTtd(blk.header):
blk.header.difficulty = DifficultyInt.zero blk.header.difficulty = DifficultyInt.zero
blk.header.mixDigest = prevRandao blk.header.mixDigest = prevRandao

View File

@ -719,7 +719,12 @@ proc `baseFee=`*(xp: TxPoolRef; val: GasPrice)
proc `lwmTrgPercent=`*(xp: TxPoolRef; val: int) = proc `lwmTrgPercent=`*(xp: TxPoolRef; val: int) =
## Setter, `val` arguments outside `0..100` are ignored ## Setter, `val` arguments outside `0..100` are ignored
if 0 <= val and val <= 100: 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]) proc `flags=`*(xp: TxPoolRef; val: set[TxPoolFlags])
{.gcsafe,raises: [Defect,CatchableError].} = {.gcsafe,raises: [Defect,CatchableError].} =
@ -729,7 +734,12 @@ proc `flags=`*(xp: TxPoolRef; val: set[TxPoolFlags])
proc `hwmMaxPercent=`*(xp: TxPoolRef; val: int) = proc `hwmMaxPercent=`*(xp: TxPoolRef; val: int) =
## Setter, `val` arguments outside `0..100` are ignored ## Setter, `val` arguments outside `0..100` are ignored
if 0 <= val and val <= 100: 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) = proc `maxRejects=`*(xp: TxPoolRef; val: int) =
## Setter, the size of the waste basket. This setting becomes effective with ## Setter, the size of the waste basket. This setting becomes effective with

View File

@ -128,6 +128,8 @@ proc new*(T: type TxChainRef; db: BaseChainDB; miner: EthAddress): T
result.miner = miner result.miner = miner
result.lhwm.lwmTrg = TRG_THRESHOLD_PER_CENT result.lhwm.lwmTrg = TRG_THRESHOLD_PER_CENT
result.lhwm.hwmMax = MAX_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): result.calcDifficulty = proc(timeStamp: EthTime, parent: BlockHeader):
DifficultyInt {.gcsafe, raises:[].} = DifficultyInt {.gcsafe, raises:[].} =
try: try:

View File

@ -17,8 +17,8 @@ import
../../../chain_config, ../../../chain_config,
../../../db/db_chain, ../../../db/db_chain,
../../../constants, ../../../constants,
../../../forks, ../../../utils/header,
eth/[common] eth/[common, eip1559]
{.push raises: [Defect].} {.push raises: [Defect].}
@ -30,6 +30,10 @@ type
hwmMax: int ##\ hwmMax: int ##\
## VM executor may stop if this per centage of `maxLimit` has ## VM executor may stop if this per centage of `maxLimit` has
## been reached. ## been reached.
gasFloor: GasInt
## minimum desired gas limit
gasCeil: GasInt
## maximum desired gas limit
TxChainGasLimits* = tuple TxChainGasLimits* = tuple
gasLimit: GasInt ## Parent gas limit, used as a base for others 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.minLimit = gl.gasLimit - delta
gl.trgLimit = gl.gasLimit gl.trgLimit = gl.gasLimit
proc isLondon(c: ChainConfig, number: BlockNumber): bool {.inline.} =
number >= c.londonBlock
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Public functions # Public functions
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -92,9 +99,7 @@ proc gasLimitsGet*(db: BaseChainDB; parent: BlockHeader; parentLimit: GasInt;
## Calculate gas limits for the next block header. ## Calculate gas limits for the next block header.
result.gasLimit = parentLimit result.gasLimit = parentLimit
let nextFork = db.config.toFork(parent.blockNumber + 1) if isLondon(db.config, parent.blockNumber+1):
if FkLondon <= nextFork:
result.setPostLondonLimits result.setPostLondonLimits
else: else:
result.setPreLondonLimits result.setPreLondonLimits
@ -106,6 +111,19 @@ proc gasLimitsGet*(db: BaseChainDB; parent: BlockHeader; parentLimit: GasInt;
result.hwmLimit = max( result.hwmLimit = max(
result.trgLimit, (result.maxLimit * pc.hwmMax + 50) div 100) 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; proc gasLimitsGet*(db: BaseChainDB; parent: BlockHeader;
pc: TxChainGasLimitsPc): TxChainGasLimits = pc: TxChainGasLimitsPc): TxChainGasLimits =