Extract the EIP1559 gas fee calculation in nim-eth, so it can be reused in nimbus-eth2

This commit is contained in:
Zahary Karadjov 2022-08-18 23:41:03 +03:00 committed by jangko
parent 1e4f138574
commit daac75796f
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
4 changed files with 9 additions and 51 deletions

View File

@ -28,8 +28,6 @@ const
MAX_UNCLE_DEPTH* = 6.u256
MAX_UNCLES* = 2
EMPTY_UNCLE_HASH* = "1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347".toDigest
GENESIS_BLOCK_NUMBER* = 0.toBlockNumber
GENESIS_DIFFICULTY* = 131_072.u256
GENESIS_GAS_LIMIT* = 3_141_592
@ -42,7 +40,6 @@ const
GAS_LIMIT_MAXIMUM* = high(GasInt)
DEFAULT_GAS_LIMIT* = 8_000_000
BLANK_ROOT_HASH* = "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421".toDigest
EMPTY_SHA3* = "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470".toDigest
GAS_MOD_EXP_QUADRATIC_DENOMINATOR* = 20.u256

View File

@ -11,25 +11,15 @@
import
std/strformat,
stew/results,
eth/common,
eth/[common, eip1559],
../db/db_chain,
../constants,
../chain_config,
../forks
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.
1000000000.u256
export
eip1559
# ------------------------------------------------------------------------------
# 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
# initial base fee.
if not c.isLondonOrLater(parent.blockNumber):
return EIP1559_INITIAL_BASE_FEE
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)
if c.isLondonOrLater(parent.blockNumber):
eip1559.calcEip1599BaseFee(parent.gasLimit, parent.gasUsed, parent.baseFee)
else:
# Otherwise if the parent block used less gas than its target, the
# baseFee should decrease.
let
gasUsedDelta = (parentGasTarget - parent.gasUsed).u256
baseFeeDelta = (parentBaseFee * gasUsedDelta) div parentGasDenom
return max(parentBaseFee - baseFeeDelta, 0.u256)
EIP1559_INITIAL_BASE_FEE
# consensus/misc/eip1559.go(32): func VerifyEip1559Header(config [..]
proc verifyEip1559Header(c: ChainConfig;

View File

@ -16,6 +16,7 @@
import
std/[sequtils, strformat, strutils, tables, times],
nimcrypto/hash,
eth/common,
../../nimbus/constants
export

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit 2556b090ea78416410098172b1ad3cc4d21a6474
Subproject commit 4c702938834d492a96593ea97ec2c813b4a80044