mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-02-03 15:55:47 +00:00
Extract the EIP1559 gas fee calculation in nim-eth, so it can be reused in nimbus-eth2
This commit is contained in:
parent
1e4f138574
commit
daac75796f
@ -28,8 +28,6 @@ const
|
|||||||
MAX_UNCLE_DEPTH* = 6.u256
|
MAX_UNCLE_DEPTH* = 6.u256
|
||||||
MAX_UNCLES* = 2
|
MAX_UNCLES* = 2
|
||||||
|
|
||||||
EMPTY_UNCLE_HASH* = "1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347".toDigest
|
|
||||||
|
|
||||||
GENESIS_BLOCK_NUMBER* = 0.toBlockNumber
|
GENESIS_BLOCK_NUMBER* = 0.toBlockNumber
|
||||||
GENESIS_DIFFICULTY* = 131_072.u256
|
GENESIS_DIFFICULTY* = 131_072.u256
|
||||||
GENESIS_GAS_LIMIT* = 3_141_592
|
GENESIS_GAS_LIMIT* = 3_141_592
|
||||||
@ -42,7 +40,6 @@ const
|
|||||||
GAS_LIMIT_MAXIMUM* = high(GasInt)
|
GAS_LIMIT_MAXIMUM* = high(GasInt)
|
||||||
DEFAULT_GAS_LIMIT* = 8_000_000
|
DEFAULT_GAS_LIMIT* = 8_000_000
|
||||||
|
|
||||||
BLANK_ROOT_HASH* = "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421".toDigest
|
|
||||||
EMPTY_SHA3* = "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470".toDigest
|
EMPTY_SHA3* = "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470".toDigest
|
||||||
|
|
||||||
GAS_MOD_EXP_QUADRATIC_DENOMINATOR* = 20.u256
|
GAS_MOD_EXP_QUADRATIC_DENOMINATOR* = 20.u256
|
||||||
|
@ -11,24 +11,14 @@
|
|||||||
import
|
import
|
||||||
std/strformat,
|
std/strformat,
|
||||||
stew/results,
|
stew/results,
|
||||||
eth/common,
|
eth/[common, eip1559],
|
||||||
../db/db_chain,
|
../db/db_chain,
|
||||||
../constants,
|
../constants,
|
||||||
../chain_config,
|
../chain_config,
|
||||||
../forks
|
../forks
|
||||||
|
|
||||||
const
|
export
|
||||||
EIP1559_BASE_FEE_CHANGE_DENOMINATOR* = ##\
|
eip1559
|
||||||
## 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.
|
|
||||||
1000000000.u256
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# Pre Eip 1559 gas limit validation
|
# Pre Eip 1559 gas limit validation
|
||||||
@ -75,40 +65,10 @@ proc calcEip1599BaseFee*(c: ChainConfig; parent: BlockHeader): UInt256 =
|
|||||||
|
|
||||||
# If the current block is the first EIP-1559 block, return the
|
# If the current block is the first EIP-1559 block, return the
|
||||||
# initial base fee.
|
# initial base fee.
|
||||||
if not c.isLondonOrLater(parent.blockNumber):
|
if c.isLondonOrLater(parent.blockNumber):
|
||||||
return EIP1559_INITIAL_BASE_FEE
|
eip1559.calcEip1599BaseFee(parent.gasLimit, parent.gasUsed, parent.baseFee)
|
||||||
|
|
||||||
let parentGasTarget = parent.gasLimit div EIP1559_ELASTICITY_MULTIPLIER
|
|
||||||
|
|
||||||
# If the parent gasUsed is the same as the target, the baseFee remains
|
|
||||||
# unchanged.
|
|
||||||
if parent.gasUsed == parentGasTarget:
|
|
||||||
return parent.baseFee
|
|
||||||
|
|
||||||
let parentGasDenom = parentGasTarget.u256 *
|
|
||||||
EIP1559_BASE_FEE_CHANGE_DENOMINATOR.u256
|
|
||||||
|
|
||||||
# baseFee is an Option[T]
|
|
||||||
let parentBaseFee = parent.baseFee
|
|
||||||
|
|
||||||
if parentGasTarget < parent.gasUsed:
|
|
||||||
# If the parent block used more gas than its target, the baseFee should
|
|
||||||
# increase.
|
|
||||||
let
|
|
||||||
gasUsedDelta = (parent.gasUsed - parentGasTarget).u256
|
|
||||||
baseFeeDelta = (parentBaseFee * gasUsedDelta) div parentGasDenom
|
|
||||||
|
|
||||||
return parentBaseFee + max(baseFeeDelta, 1.u256)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Otherwise if the parent block used less gas than its target, the
|
EIP1559_INITIAL_BASE_FEE
|
||||||
# baseFee should decrease.
|
|
||||||
let
|
|
||||||
gasUsedDelta = (parentGasTarget - parent.gasUsed).u256
|
|
||||||
baseFeeDelta = (parentBaseFee * gasUsedDelta) div parentGasDenom
|
|
||||||
|
|
||||||
return max(parentBaseFee - baseFeeDelta, 0.u256)
|
|
||||||
|
|
||||||
|
|
||||||
# consensus/misc/eip1559.go(32): func VerifyEip1559Header(config [..]
|
# consensus/misc/eip1559.go(32): func VerifyEip1559Header(config [..]
|
||||||
proc verifyEip1559Header(c: ChainConfig;
|
proc verifyEip1559Header(c: ChainConfig;
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
import
|
import
|
||||||
std/[sequtils, strformat, strutils, tables, times],
|
std/[sequtils, strformat, strutils, tables, times],
|
||||||
nimcrypto/hash,
|
nimcrypto/hash,
|
||||||
|
eth/common,
|
||||||
../../nimbus/constants
|
../../nimbus/constants
|
||||||
|
|
||||||
export
|
export
|
||||||
|
2
vendor/nim-eth
vendored
2
vendor/nim-eth
vendored
@ -1 +1 @@
|
|||||||
Subproject commit 2556b090ea78416410098172b1ad3cc4d21a6474
|
Subproject commit 4c702938834d492a96593ea97ec2c813b4a80044
|
Loading…
x
Reference in New Issue
Block a user