Skip to content

Commit

Permalink
Update the effective gas price calculation + validation + transaction…
Browse files Browse the repository at this point in the history
… param cleanup
  • Loading branch information
fselmo committed Nov 3, 2021
1 parent 8b0f4c3 commit 066a8fa
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
11 changes: 7 additions & 4 deletions eth_tester/backends/mock/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ def serialize_full_transaction(transaction, block, transaction_index, is_pending
if 'gas_price' in transaction:
return serialized_transaction
else:
return assoc(
serialized_transaction,
'gas_price',
calculate_effective_gas_price(transaction, block)
# TODO: Sometime in 2022 the inclusion of gas_price may be removed from dynamic fee
# transactions and we can get rid of this behavior.
# https://github.com/ethereum/execution-specs/pull/251
gas_price = (
transaction['max_fee_per_gas'] if is_pending
else calculate_effective_gas_price(transaction, block)
)
return assoc(serialized_transaction, 'gas_price', gas_price)


def serialize_receipt(receipt, transaction, block, transaction_index, is_pending):
Expand Down
14 changes: 12 additions & 2 deletions eth_tester/backends/pyevm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,24 @@ def get_base_fee(self, block_number='latest'):
#
@to_dict
def _normalize_transaction(self, transaction, block_number='latest'):
# legacy and dynamic fee params
all_fee_params = DYNAMIC_FEE_TRANSACTION_PARAMS + ('gas_price',)

is_dynamic_fee_transaction = (
any(_ in transaction for _ in DYNAMIC_FEE_TRANSACTION_PARAMS)
or not any(_ in transaction for _ in DYNAMIC_FEE_TRANSACTION_PARAMS + ('gas_price',))
any(_ in transaction for _ in DYNAMIC_FEE_TRANSACTION_PARAMS) or
# if no fee params exist, default to dynamic fee transaction:
not any(_ in transaction for _ in all_fee_params)
)

for key in transaction:
if key in ('from', 'type'):
continue
if key == 'gas_price' and is_dynamic_fee_transaction:
# TODO: Sometime in 2022 the inclusion of gas_price may be removed from dynamic fee
# transactions and we can get rid of this behavior.
# https://github.com/ethereum/execution-specs/pull/251
# take out gas_price from transaction if this is a dynamic fee transaction
continue
yield key, transaction[key]

if 'nonce' not in transaction:
Expand Down
9 changes: 6 additions & 3 deletions eth_tester/backends/pyevm/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,13 @@ def serialize_transaction(block, transaction, transaction_index, is_pending):
'access_list': transaction.access_list or (),
'y_parity': transaction.y_parity,

# TODO: Sometime in early 2022 (the merge?), the inclusion of gas_price will be removed
# from dynamic fee transactions and we can get rid of this behavior.
# TODO: Sometime in 2022 the inclusion of gas_price may be removed from dynamic fee
# transactions and we can get rid of this behavior.
# https://github.com/ethereum/execution-specs/pull/251
'gas_price': _calculate_effective_gas_price(transaction, block, txn_type),
'gas_price': (
transaction.max_fee_per_gas if is_pending
else _calculate_effective_gas_price(transaction, block, txn_type)
),
}
else:
raise ValidationError('Invariant: code path should be unreachable')
Expand Down
13 changes: 0 additions & 13 deletions eth_tester/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,13 @@ def func_wrapper(self, *args, **kwargs):
try:
transaction_hash = func(self, *args, **kwargs)
pending_transaction = self.get_transaction_by_hash(transaction_hash)
pending_transaction = _clean_pending_transaction_for_sending(pending_transaction)
# Remove any pending transactions with the same nonce
self._pending_transactions = remove_matching_transaction_from_list(
self._pending_transactions, pending_transaction)
self._pending_transactions.append(pending_transaction)
finally:
self.revert_to_snapshot(snapshot)
return transaction_hash

def _clean_pending_transaction_for_sending(pending_transaction):
cleaned_transaction = dissoc(pending_transaction, 'type')

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

return cleaned_transaction

return func_wrapper


Expand Down
9 changes: 7 additions & 2 deletions eth_tester/validation/inbound.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,17 @@ def validate_transaction(value, txn_internal_type):

if 'max_fee_per_gas' in value:
validate_uint256(value['max_fee_per_gas'])
if 'gas_price' in value:
# TODO: Sometime in 2022 the inclusion of gas_price may be removed from dynamic fee
# transactions and we can get rid of this behavior.
# https://github.com/ethereum/execution-specs/pull/251
# If inbound dynamic fee transaction with gas_price, gas_price value must be equal to the
# max_fee_per_gas.
if 'gas_price' in value and value['gas_price'] != value['max_fee_per_gas']:
raise ValidationError('Mixed legacy and dynamic fee transaction values')

if 'max_priority_fee_per_gas' in value:
validate_uint256(value['max_priority_fee_per_gas'])
if 'gas_price' in value:
if 'gas_price' in value and 'max_fee_per_gas' not in value:
raise ValidationError('Mixed legacy and dynamic fee transaction values')

if 'value' in value:
Expand Down
4 changes: 2 additions & 2 deletions tests/core/validation/test_outbound_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def _make_access_list_txn(chain_id=131277322940537, access_list=[], y_parity=0,

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

0 comments on commit 066a8fa

Please sign in to comment.