Skip to content

Commit 5b5003f

Browse files
committed
Update the effective gas price calculation + validation + transaction param cleanup
1 parent d90ef79 commit 5b5003f

File tree

8 files changed

+57
-23
lines changed

8 files changed

+57
-23
lines changed

eth_tester/backends/mock/common.py

-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
def extract_transaction_type(transaction):
2-
return (
3-
'0x2' if 'max_fee_per_gas' in transaction
4-
else '0x1' if 'max_fee_per_gas' not in transaction and 'access_list' in transaction
5-
else '0x0' # legacy transactions being '0x0' taken from current geth version v1.10.10
6-
)
7-
8-
91
def calculate_effective_gas_price(transaction, block):
102
return (
113
min(

eth_tester/backends/mock/factory.py

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
from eth_tester.backends.mock.common import (
55
calculate_effective_gas_price,
6+
)
7+
from eth_tester.utils.transactions import (
68
extract_transaction_type,
79
)
810
from eth_utils import (

eth_tester/backends/mock/serializers.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from eth_tester.backends.mock.common import (
22
calculate_effective_gas_price,
3+
)
4+
from eth_tester.utils.transactions import (
35
extract_transaction_type,
46
)
57
from eth_utils.toolz import (
@@ -42,11 +44,14 @@ def serialize_full_transaction(transaction, block, transaction_index, is_pending
4244
if 'gas_price' in transaction:
4345
return serialized_transaction
4446
else:
45-
return assoc(
46-
serialized_transaction,
47-
'gas_price',
48-
calculate_effective_gas_price(transaction, block)
47+
# TODO: Sometime in 2022 the inclusion of gas_price may be removed from dynamic fee
48+
# transactions and we can get rid of this behavior.
49+
# https://github.com/ethereum/execution-specs/pull/251
50+
gas_price = (
51+
transaction['max_fee_per_gas'] if is_pending
52+
else calculate_effective_gas_price(transaction, block)
4953
)
54+
return assoc(serialized_transaction, 'gas_price', gas_price)
5055

5156

5257
def serialize_receipt(receipt, transaction, block, transaction_index, is_pending):

eth_tester/backends/pyevm/main.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,9 @@ def get_base_fee(self, block_number='latest'):
499499
@to_dict
500500
def _normalize_transaction(self, transaction, block_number='latest'):
501501
is_dynamic_fee_transaction = (
502-
any(_ in transaction for _ in DYNAMIC_FEE_TRANSACTION_PARAMS)
503-
or not any(_ in transaction for _ in DYNAMIC_FEE_TRANSACTION_PARAMS + ('gas_price',))
502+
any(_ in transaction for _ in DYNAMIC_FEE_TRANSACTION_PARAMS) or
503+
# if no fee params exist, default to dynamic fee transaction:
504+
not any(_ in transaction for _ in DYNAMIC_FEE_TRANSACTION_PARAMS + ('gas_price',))
504505
)
505506

506507
for key in transaction:

eth_tester/backends/pyevm/serializers.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,13 @@ def serialize_transaction(block, transaction, transaction_index, is_pending):
114114
'access_list': transaction.access_list or (),
115115
'y_parity': transaction.y_parity,
116116

117-
# TODO: Sometime in early 2022 (the merge?), the inclusion of gas_price will be removed
118-
# from dynamic fee transactions and we can get rid of this behavior.
117+
# TODO: Sometime in 2022 the inclusion of gas_price may be removed from dynamic fee
118+
# transactions and we can get rid of this behavior.
119119
# https://github.com/ethereum/execution-specs/pull/251
120-
'gas_price': _calculate_effective_gas_price(transaction, block, txn_type),
120+
'gas_price': (
121+
transaction.max_fee_per_gas if is_pending
122+
else _calculate_effective_gas_price(transaction, block, txn_type)
123+
),
121124
}
122125
else:
123126
raise ValidationError('Invariant: code path should be unreachable')

eth_tester/main.py

+27-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
check_if_log_matches,
4646
)
4747
from eth_tester.utils.transactions import (
48+
extract_transaction_type,
4849
extract_valid_transaction_params,
4950
remove_matching_transaction_from_list,
5051
)
@@ -71,7 +72,7 @@ def func_wrapper(self, *args, **kwargs):
7172
try:
7273
transaction_hash = func(self, *args, **kwargs)
7374
pending_transaction = self.get_transaction_by_hash(transaction_hash)
74-
pending_transaction = _clean_pending_transaction_for_sending(pending_transaction)
75+
pending_transaction = _clean_pending_transaction(pending_transaction)
7576
# Remove any pending transactions with the same nonce
7677
self._pending_transactions = remove_matching_transaction_from_list(
7778
self._pending_transactions, pending_transaction)
@@ -80,17 +81,17 @@ def func_wrapper(self, *args, **kwargs):
8081
self.revert_to_snapshot(snapshot)
8182
return transaction_hash
8283

83-
def _clean_pending_transaction_for_sending(pending_transaction):
84+
def _clean_pending_transaction(pending_transaction):
8485
cleaned_transaction = dissoc(pending_transaction, 'type')
8586

8687
# TODO: Sometime in early 2022 (the merge?), the inclusion of gas_price will be removed
8788
# from dynamic fee transactions and we can get rid of this behavior.
8889
# https://github.com/ethereum/execution-specs/pull/251
90+
# remove gas_price for dynamic fee transactions
8991
if 'gas_price' and 'max_fee_per_gas' in pending_transaction:
9092
cleaned_transaction = dissoc(cleaned_transaction, 'gas_price')
9193

9294
return cleaned_transaction
93-
9495
return func_wrapper
9596

9697

@@ -264,6 +265,27 @@ def get_nonce(self, account, block_number="latest"):
264265
# Blocks, Transactions, Receipts
265266
#
266267

268+
@staticmethod
269+
def _normalize_pending_transaction(pending_transaction):
270+
"""
271+
Add the transaction type and, if a dynamic fee transaction, add gas_price =
272+
max_fee_per_gas as highlighted in the execution-specs link below.
273+
"""
274+
_type = extract_transaction_type(pending_transaction)
275+
pending_transaction = assoc(pending_transaction, 'type', _type)
276+
277+
# TODO: Sometime in 2022 the inclusion of gas_price may be removed from dynamic fee
278+
# transactions and we can get rid of this behavior.
279+
# https://github.com/ethereum/execution-specs/pull/251
280+
# add gas_price = max_fee_per_gas to pending dynamic fee transactions
281+
if _type == '0x2':
282+
pending_transaction = assoc(
283+
pending_transaction,
284+
'gas_price',
285+
pending_transaction['max_fee_per_gas']
286+
)
287+
return pending_transaction
288+
267289
def _get_pending_transaction_by_hash(self, transaction_hash):
268290
for transaction in self._pending_transactions:
269291
if transaction['hash'] == transaction_hash:
@@ -275,7 +297,8 @@ def _get_pending_transaction_by_hash(self, transaction_hash):
275297
def get_transaction_by_hash(self, transaction_hash):
276298
self.validator.validate_inbound_transaction_hash(transaction_hash)
277299
try:
278-
return self._get_pending_transaction_by_hash(transaction_hash)
300+
pending_transaction = self._get_pending_transaction_by_hash(transaction_hash)
301+
return self._normalize_pending_transaction(pending_transaction)
279302
except TransactionNotFound:
280303
raw_transaction_hash = self.normalizer.normalize_inbound_transaction_hash(
281304
transaction_hash,

eth_tester/utils/transactions.py

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ def extract_valid_transaction_params(transaction_params):
2929
for key in VALID_TRANSACTION_PARAMS if key in transaction_params}
3030

3131

32+
def extract_transaction_type(transaction):
33+
return (
34+
'0x2' if 'max_fee_per_gas' in transaction
35+
else '0x1' if 'max_fee_per_gas' not in transaction and 'access_list' in transaction
36+
else '0x0' # legacy transactions being '0x0' taken from current geth version v1.10.10
37+
)
38+
39+
3240
@to_list
3341
def remove_matching_transaction_from_list(transaction_list, transaction):
3442
for tx in transaction_list:

tests/core/validation/test_outbound_validation.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ def _make_access_list_txn(chain_id=131277322940537, access_list=[], y_parity=0,
100100

101101
# This is an outbound transaction so we still keep the gas_price for now since the gas_price is
102102
# the min(max_fee_per_gas, base_fee_per_gas + max_priority_fee_per_gas).
103-
# TODO: Sometime in early 2022 (the merge?), the inclusion of gas_price will be removed
104-
# from dynamic fee transactions and we can get rid of this behavior.
103+
# TODO: Sometime in 2022 the inclusion of gas_price may be removed from dynamic fee
104+
# transactions and we can get rid of this behavior.
105105
# https://github.com/ethereum/execution-specs/pull/251
106106
def _make_dynamic_fee_txn(
107107
chain_id=131277322940537,

0 commit comments

Comments
 (0)