Simplify txpool baseFeeGet (#2416)

* Simplify txpool baseFeeGet

- Avoid using toEVMFork because we are not in EVM
- Rename `isLondon` to `isLondonOrLater`

* Remove timestamp from isLondonOrLater
This commit is contained in:
andri lim 2024-06-27 12:54:36 +07:00 committed by GitHub
parent c8cdffa775
commit 27339e9520
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 21 additions and 41 deletions

View File

@ -302,14 +302,10 @@ func toEVMFork*(com: CommonRef, forkDeterminer: ForkDeterminationInfo): EVMFork
func toEVMFork*(com: CommonRef): EVMFork =
ToEVMFork[com.currentFork]
func isLondon*(com: CommonRef, number: BlockNumber): bool =
func isLondonOrLater*(com: CommonRef, number: BlockNumber): bool =
# TODO: Fixme, use only London comparator
com.toHardFork(number.forkDeterminationInfo) >= London
func isLondon*(com: CommonRef, number: BlockNumber, timestamp: EthTime): bool =
# TODO: Fixme, use only London comparator
com.toHardFork(forkDeterminationInfo(number, timestamp)) >= London
func forkGTE*(com: CommonRef, fork: HardFork): bool =
com.currentFork >= fork

View File

@ -51,7 +51,7 @@ proc calcEip1599BaseFee*(com: CommonRef; parent: BlockHeader): UInt256 =
# If the current block is the first EIP-1559 block, return the
# initial base fee.
if com.isLondon(parent.number):
if com.isLondonOrLater(parent.number):
eip1559.calcEip1599BaseFee(parent.gasLimit, parent.gasUsed, parent.baseFeePerGas.get(0.u256))
else:
EIP1559_INITIAL_BASE_FEE
@ -61,7 +61,7 @@ proc verifyEip1559Header(com: CommonRef;
parent, header: BlockHeader): Result[void, string]
{.raises: [].} =
## Verify that the gas limit remains within allowed bounds
let limit = if com.isLondon(parent.number):
let limit = if com.isLondonOrLater(parent.number):
parent.gasLimit
else:
parent.gasLimit * EIP1559_ELASTICITY_MULTIPLIER
@ -91,7 +91,7 @@ proc verifyEip1559Header(com: CommonRef;
proc validateGasLimitOrBaseFee*(com: CommonRef;
header, parent: BlockHeader): Result[void, string] =
if not com.isLondon(header.number):
if not com.isLondonOrLater(header.number):
# Verify BaseFee not present before EIP-1559 fork.
let baseFeePerGas = header.baseFeePerGas.get(0.u256)
if not baseFeePerGas.isZero:

View File

@ -123,13 +123,9 @@ proc update(dh: TxChainRef; parent: BlockHeader)
{.gcsafe,raises: [].} =
let
timestamp = dh.getTimestamp(parent)
db = dh.com.db
acc = LedgerRef.init(db, parent.stateRoot)
fee = if dh.com.isLondon(parent.number + 1, timestamp):
Opt.some(dh.com.baseFeeGet(parent).uint64.u256)
else:
Opt.none UInt256
fee = baseFeeGet(dh.com, parent)
# Keep a separate accounts descriptor positioned at the sync point
dh.roAcc = ReadOnlyStateDB(acc)
@ -293,7 +289,7 @@ func `baseFee=`*(dh: TxChainRef; val: GasPrice) =
## Setter, temorarily overwrites parameter until next `head=` update. This
## function would be called in exceptional cases only as this parameter is
## determined by the `head=` update.
if 0 < val or dh.com.isLondon(dh.txEnv.vmState.blockNumber):
if 0 < val or dh.com.isLondonOrLater(dh.txEnv.vmState.blockNumber):
dh.txEnv.vmState.blockCtx.baseFeePerGas = Opt.some(val.uint64.u256)
else:
dh.txEnv.vmState.blockCtx.baseFeePerGas = Opt.none UInt256

View File

@ -15,43 +15,31 @@
import
../../../common/common,
../../../constants,
../tx_item,
eth/eip1559
{.push raises: [].}
const
INITIAL_BASE_FEE = EIP1559_INITIAL_BASE_FEE.truncate(uint64)
# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------
proc baseFeeGet*(com: CommonRef; parent: BlockHeader): GasPrice =
proc baseFeeGet*(com: CommonRef;
parent: BlockHeader): Opt[UInt256] =
## Calculates the `baseFee` of the head assuming this is the parent of a
## new block header to generate. This function is derived from
## `p2p/gaslimit.calcEip1599BaseFee()` which in turn has its origins on
## `consensus/misc/eip1559.go` of geth.
## new block header to generate.
# Note that the baseFee is calculated for the next header
let
forkDeterminer = forkDeterminationInfo(parent)
parentFork = com.toEVMFork(forkDeterminer)
nextFork = com.toEVMFork(forkDeterminer.adjustForNextBlock)
if nextFork < FkLondon:
return 0.GasPrice
if not com.isLondonOrLater(parent.number+1):
return Opt.none(UInt256)
# If the new block is the first EIP-1559 block, return initial base fee.
if parentFork < FkLondon:
return INITIAL_BASE_FEE.GasPrice
if not com.isLondonOrLater(parent.number):
return Opt.some(EIP1559_INITIAL_BASE_FEE)
# TODO: which one is better?
# truncate parent.baseFee to uint64 first and do the operation in uint64
# or truncate the result?
calcEip1599BaseFee(parent.gasLimit,
Opt.some calcEip1599BaseFee(
parent.gasLimit,
parent.gasUsed,
parent.baseFeePerGas.get(0.u256)).truncate(uint64).GasPrice
parent.baseFeePerGas.get(0.u256))
# ------------------------------------------------------------------------------
# End

View File

@ -95,7 +95,7 @@ proc gasLimitsGet*(com: CommonRef; parent: BlockHeader; parentLimit: GasInt;
## Calculate gas limits for the next block header.
result.gasLimit = parentLimit
if com.isLondon(parent.number+1):
if com.isLondonOrLater(parent.number+1):
result.setPostLondonLimits
else:
result.setPreLondonLimits
@ -108,9 +108,9 @@ proc gasLimitsGet*(com: CommonRef; parent: BlockHeader; parentLimit: GasInt;
result.trgLimit, (result.maxLimit * pc.hwmMax + 50) div 100)
# override trgLimit, see https://github.com/status-im/nimbus-eth1/issues/1032
if com.isLondon(parent.number+1):
if com.isLondonOrLater(parent.number+1):
var parentGasLimit = parent.gasLimit
if not com.isLondon(parent.number):
if not com.isLondonOrLater(parent.number):
# Bump by 2x
parentGasLimit = parent.gasLimit * EIP1559_ELASTICITY_MULTIPLIER
result.trgLimit = calcGasLimit1559(parentGasLimit, desiredLimit = pc.gasCeil)

View File

@ -85,7 +85,7 @@ func toBytes(list: openArray[float64]): seq[byte] =
result.add(cast[uint64](x).toBytesLE)
func calcBaseFee(com: CommonRef, bc: BlockContent): UInt256 =
if com.isLondon(bc.blockNumber + 1):
if com.isLondonOrLater(bc.blockNumber + 1):
calcEip1599BaseFee(
bc.header.gasLimit,
bc.header.gasUsed,

View File

@ -436,7 +436,7 @@ proc transitionAction*(ctx: var TransContext, conf: T8NConf) =
)
# Sanity check, to not `panic` in state_transition
if com.isLondon(ctx.env.currentNumber):
if com.isLondonOrLater(ctx.env.currentNumber):
if ctx.env.currentBaseFee.isSome:
# Already set, currentBaseFee has precedent over parentBaseFee.
discard