mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-12 05:14:14 +00:00
parent
112a6f7d76
commit
1ab5c29530
@ -17,22 +17,12 @@ import
|
||||
../../../constants,
|
||||
../../../forks,
|
||||
../tx_item,
|
||||
eth/[common]
|
||||
eth/[common, eip1559]
|
||||
|
||||
{.push raises: [Defect].}
|
||||
|
||||
const
|
||||
EIP1559_BASE_FEE_CHANGE_DENOMINATOR = ##\
|
||||
## 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
|
||||
INITIAL_BASE_FEE = EIP1559_INITIAL_BASE_FEE.truncate(uint64)
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Public functions
|
||||
@ -46,9 +36,6 @@ proc baseFeeGet*(config: ChainConfig; parent: BlockHeader): GasPrice =
|
||||
|
||||
# Note that the baseFee is calculated for the next header
|
||||
let
|
||||
parentGasUsed = parent.gasUsed
|
||||
parentGasLimit = parent.gasLimit
|
||||
parentBaseFee = parent.baseFee.truncate(uint64)
|
||||
parentFork = config.toFork(parent.blockNumber)
|
||||
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 parentFork < FkLondon:
|
||||
return EIP1559_INITIAL_BASE_FEE.GasPrice
|
||||
return INITIAL_BASE_FEE.GasPrice
|
||||
|
||||
let
|
||||
parGasTrg = parentGasLimit div EIP1559_ELASTICITY_MULTIPLIER
|
||||
parGasDenom = (parGasTrg * EIP1559_BASE_FEE_CHANGE_DENOMINATOR).uint64
|
||||
|
||||
# If parent gasUsed is the same as the target, the baseFee remains unchanged.
|
||||
if parentGasUsed == parGasTrg:
|
||||
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
|
||||
# TODO: which one is better?
|
||||
# truncate parent.baseFee to uint64 first and do the operation in uint64
|
||||
# or truncate the result?
|
||||
calcEip1599BaseFee(parent.gasLimit,
|
||||
parent.gasUsed,
|
||||
parent.baseFee).truncate(uint64).GasPrice
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# End
|
||||
|
Loading…
x
Reference in New Issue
Block a user