unify eip1559TxNormalization

result.gasPrice = baseFee + min(result.maxPriorityFee, result.maxFee - baseFee)
cannot be simplified into
result.gasPrice = min(result.maxPriorityFee + baseFee, result.maxFee)
the expression is not commutative
This commit is contained in:
jangko 2022-04-05 17:22:46 +07:00
parent 64c2227c7c
commit fdbabf0b80
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
4 changed files with 18 additions and 31 deletions

View File

@ -13,6 +13,7 @@ import
../../db/accounts_cache,
../../forks,
../../transaction/call_evm,
../../transaction,
../../vm_state,
../../vm_types,
../validate,
@ -27,12 +28,6 @@ import
# Private functions
# ------------------------------------------------------------------------------
proc eip1559TxNormalization(tx: Transaction): Transaction =
result = tx
if tx.txType < TxEip1559:
result.maxPriorityFee = tx.gasPrice
result.maxFee = tx.gasPrice
proc eip1559BaseFee(header: BlockHeader; fork: Fork): UInt256 =
## Actually, `baseFee` should be 0 for pre-London headers already. But this
## function just plays safe. In particular, the `test_general_state_json.nim`
@ -54,18 +49,13 @@ proc processTransactionImpl(
#trace "Sender", sender
#trace "txHash", rlpHash = ty.rlpHash
var
tx = eip1559TxNormalization(tx)
let
roDB = vmState.readOnlyStateDB
baseFee256 = header.eip1559BaseFee(fork)
baseFee = baseFee256.truncate(int64)
baseFee = baseFee256.truncate(GasInt)
tx = eip1559TxNormalization(tx, baseFee, fork)
priorityFee = min(tx.maxPriorityFee, tx.maxFee - baseFee)
miner = vmState.coinbase()
if FkLondon <= fork:
# The signer pays both the priority fee and the base fee. So tx.gasPrice
# now is the effective gasPrice which also effects the `stateRoot` value.
tx.gasPrice = priorityFee + baseFee
# Return failure unless explicitely set `ok()`
result = err()

View File

@ -153,3 +153,15 @@ proc signTransaction*(tx: Transaction, privateKey: PrivateKey, chainId: ChainId,
result.R = Uint256.fromBytesBE(sig[0..31])
result.S = Uint256.fromBytesBE(sig[32..63])
func eip1559TxNormalization*(tx: Transaction;
baseFee: GasInt; fork: Fork): Transaction =
## This function adjusts a legacy transaction to EIP-1559 standard. This
## is needed particularly when using the `validateTransaction()` utility
## with legacy transactions.
result = tx
if tx.txType < TxEip1559:
result.maxPriorityFee = tx.gasPrice
result.maxFee = tx.gasPrice
if FkLondon <= fork:
result.gasPrice = baseFee + min(result.maxPriorityFee, result.maxFee - baseFee)

View File

@ -173,22 +173,6 @@ proc txPostLondonAcceptableTipAndFees(xp: TxPoolRef; item: TxItemRef): bool =
return false
true
# ------------------------------------------------------------------------------
# Public helper function, needed here and in the packer
# ------------------------------------------------------------------------------
func eip1559TxNormalization*(tx: Transaction;
baseFee: GasPrice; fork: Fork): Transaction =
## This function adjusts a legacy transaction to EIP-1559 standard. This
## is needed particularly when using the `validateTransaction()` utility
## with legacy transactions.
result = tx
if tx.txType < TxEip1559:
result.maxPriorityFee = tx.gasPrice
result.maxFee = tx.gasPrice
if FkLondon <= fork:
result.gasPrice = min(result.maxPriorityFee + baseFee.int64, result.maxFee)
# ------------------------------------------------------------------------------
# Public functionss
# ------------------------------------------------------------------------------
@ -248,7 +232,7 @@ proc classifyValidatePacked*(xp: TxPoolRef;
xp.chain.limits.maxLimit
else:
xp.chain.limits.trgLimit
tx = item.tx.eip1559TxNormalization(xp.chain.baseFee, fork)
tx = item.tx.eip1559TxNormalization(xp.chain.baseFee.GasInt, fork)
roDB.validateTransaction(tx, item.sender, gasLimit, baseFee, fork)

View File

@ -18,6 +18,7 @@ import
../../../forks,
../../../p2p/[dao, executor, validate],
../../../transaction/call_evm,
../../../transaction,
../../../vm_state,
../../../vm_types,
../tx_chain,
@ -84,7 +85,7 @@ proc runTx(pst: TxPackerStateRef; item: TxItemRef): GasInt
let
fork = pst.xp.chain.nextFork
baseFee = pst.xp.chain.baseFee
tx = item.tx.eip1559TxNormalization(baseFee, fork)
tx = item.tx.eip1559TxNormalization(baseFee.GasInt, fork)
safeExecutor "tx_packer.runTx":
# Execute transaction, may return a wildcard `Exception`