remove duplication of EIP1559 base fee calculation

fix #1193
This commit is contained in:
jangko 2022-08-29 17:07:53 +07:00
parent 112a6f7d76
commit 1ab5c29530
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9

View File

@ -17,22 +17,12 @@ import
../../../constants, ../../../constants,
../../../forks, ../../../forks,
../tx_item, ../tx_item,
eth/[common] eth/[common, eip1559]
{.push raises: [Defect].} {.push raises: [Defect].}
const const
EIP1559_BASE_FEE_CHANGE_DENOMINATOR = ##\ INITIAL_BASE_FEE = EIP1559_INITIAL_BASE_FEE.truncate(uint64)
## Bounds the amount the base fee can change between blocks.
8
EIP1559_ELASTICITY_MULTIPLIER = ##\
## Bounds the maximum gas limit an EIP-1559 block may have.
2
EIP1559_INITIAL_BASE_FEE = ##\
## Initial base fee for Eip1559 blocks.
1_000_000_000
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Public functions # Public functions
@ -46,9 +36,6 @@ proc baseFeeGet*(config: ChainConfig; parent: BlockHeader): GasPrice =
# Note that the baseFee is calculated for the next header # Note that the baseFee is calculated for the next header
let let
parentGasUsed = parent.gasUsed
parentGasLimit = parent.gasLimit
parentBaseFee = parent.baseFee.truncate(uint64)
parentFork = config.toFork(parent.blockNumber) parentFork = config.toFork(parent.blockNumber)
nextFork = config.toFork(parent.blockNumber + 1) nextFork = config.toFork(parent.blockNumber + 1)
@ -57,35 +44,14 @@ proc baseFeeGet*(config: ChainConfig; parent: BlockHeader): GasPrice =
# If the new block is the first EIP-1559 block, return initial base fee. # If the new block is the first EIP-1559 block, return initial base fee.
if parentFork < FkLondon: if parentFork < FkLondon:
return EIP1559_INITIAL_BASE_FEE.GasPrice return INITIAL_BASE_FEE.GasPrice
let # TODO: which one is better?
parGasTrg = parentGasLimit div EIP1559_ELASTICITY_MULTIPLIER # truncate parent.baseFee to uint64 first and do the operation in uint64
parGasDenom = (parGasTrg * EIP1559_BASE_FEE_CHANGE_DENOMINATOR).uint64 # or truncate the result?
calcEip1599BaseFee(parent.gasLimit,
# If parent gasUsed is the same as the target, the baseFee remains unchanged. parent.gasUsed,
if parentGasUsed == parGasTrg: parent.baseFee).truncate(uint64).GasPrice
return parentBaseFee.GasPrice
if parGasTrg < parentGasUsed:
# If the parent block used more gas than its target, the baseFee should
# increase.
let
gasUsedDelta = (parentGasUsed - parGasTrg).uint64
baseFeeDelta = (parentBaseFee * gasUsedDelta) div parGasDenom
return (parentBaseFee + max(1u64, baseFeeDelta)).GasPrice
# Otherwise if the parent block used less gas than its target, the
# baseFee should decrease.
let
gasUsedDelta = (parGasTrg - parentGasUsed).uint64
baseFeeDelta = (parentBaseFee * gasUsedDelta) div parGasDenom
if baseFeeDelta < parentBaseFee:
return (parentBaseFee - baseFeeDelta).GasPrice
0.GasPrice
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# End # End