Skip to content

Commit 8451823

Browse files
committed
refactor(tests) Changes from comments on pr ethereum#1890:
- Move default transaction logic to a ``conftest.py`` setup. - Add ``conftest.py`` files for each fork with the respective transaction pytest fixtures. - Opt for `Type | None` over `Optional[Type]` to avoid an extra import.
1 parent eab5984 commit 8451823

File tree

9 files changed

+188
-217
lines changed

9 files changed

+188
-217
lines changed

src/pytest_plugins/forks/forks.py

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@
2525
transition_fork_to,
2626
)
2727

28-
# Import transaction fixtures to make them available when this plugin is loaded
29-
from .transaction_fixtures import ( # noqa: F401
30-
type_0_default_transaction,
31-
type_1_default_transaction,
32-
type_2_default_transaction,
33-
type_3_default_transaction,
34-
type_4_default_transaction,
35-
typed_transaction,
36-
)
37-
3828

3929
def pytest_addoption(parser):
4030
"""Add command-line options to pytest."""
@@ -349,13 +339,6 @@ def covariant_decorator(
349339
fork_attribute_name="tx_types",
350340
argnames=["tx_type"],
351341
),
352-
covariant_decorator(
353-
marker_name="with_all_typed_transactions",
354-
description="marks a test to be parametrized with transaction types, the typed_tx fixture "
355-
"will provide the corresponding Transaction object for each type",
356-
fork_attribute_name="tx_types",
357-
argnames=["typed_transaction"],
358-
),
359342
covariant_decorator(
360343
marker_name="with_all_contract_creating_tx_types",
361344
description="marks a test to be parametrized for all tx types that can create a contract"
@@ -989,26 +972,9 @@ def add_fork_covariant_parameters(
989972
metafunc: Metafunc, fork_parametrizers: List[ForkParametrizer]
990973
) -> None:
991974
"""Iterate over the fork covariant descriptors and add their values to the test function."""
992-
# Special handling for with_all_typed_transactions
993-
if list(metafunc.definition.iter_markers("with_all_typed_transactions")):
994-
# This marker needs special handling for indirect parametrization
975+
for covariant_descriptor in fork_covariant_decorators:
995976
for fork_parametrizer in fork_parametrizers:
996-
fork = fork_parametrizer.fork
997-
tx_types = fork.tx_types(block_number=0, timestamp=0)
998-
values = [pytest.param(tx_type) for tx_type in tx_types]
999-
fork_parametrizer.fork_covariant_parameters.append(
1000-
ForkCovariantParameter(names=["typed_transaction"], values=values)
1001-
)
1002-
else:
1003-
# Regular covariant descriptors
1004-
for covariant_descriptor in fork_covariant_decorators:
1005-
# Skip with_all_typed_transactions as it's handled above
1006-
if covariant_descriptor.marker_name == "with_all_typed_transactions":
1007-
continue
1008-
for fork_parametrizer in fork_parametrizers:
1009-
covariant_descriptor(metafunc=metafunc).add_values(
1010-
fork_parametrizer=fork_parametrizer
1011-
)
977+
covariant_descriptor(metafunc=metafunc).add_values(fork_parametrizer=fork_parametrizer)
1012978

1013979
for marker in metafunc.definition.iter_markers():
1014980
if marker.name == "parametrize_by_fork":
@@ -1063,11 +1029,6 @@ def parameters_from_fork_parametrizer_list(
10631029

10641030
def parametrize_fork(metafunc: Metafunc, fork_parametrizers: List[ForkParametrizer]) -> None:
10651031
"""Add the fork parameters to the test function."""
1066-
param_names, param_values = parameters_from_fork_parametrizer_list(fork_parametrizers)
1067-
1068-
# Check if typed_transaction is in the parameters and needs indirect parametrization
1069-
indirect = []
1070-
if "typed_transaction" in param_names:
1071-
indirect.append("typed_transaction")
1072-
1073-
metafunc.parametrize(param_names, param_values, scope="function", indirect=indirect)
1032+
metafunc.parametrize(
1033+
*parameters_from_fork_parametrizer_list(fork_parametrizers), scope="function"
1034+
)

src/pytest_plugins/forks/transaction_fixtures.py

Lines changed: 0 additions & 166 deletions
This file was deleted.

tests/berlin/conftest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Fixtures for Berlin fork tests."""
2+
3+
import pytest
4+
5+
from ethereum_test_base_types import AccessList
6+
from ethereum_test_types import Transaction
7+
8+
9+
@pytest.fixture
10+
def type_1_default_transaction(sender):
11+
"""Type 1 (access list) default transaction introduced in Berlin fork."""
12+
return Transaction(
13+
ty=1,
14+
sender=sender,
15+
gas_price=10**9,
16+
gas_limit=100_000,
17+
data=b"\x00" * 100,
18+
access_list=[
19+
AccessList(address=0x1234, storage_keys=[0, 1, 2]),
20+
AccessList(address=0x5678, storage_keys=[3, 4, 5]),
21+
AccessList(address=0x9ABC, storage_keys=[]),
22+
],
23+
)

tests/cancun/conftest.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Fixtures for Cancun fork tests."""
2+
3+
import pytest
4+
5+
from ethereum_test_base_types import AccessList
6+
from ethereum_test_types import Transaction, add_kzg_version
7+
8+
9+
@pytest.fixture
10+
def type_3_default_transaction(sender):
11+
"""Type 3 (blob) default transaction introduced in Cancun fork."""
12+
return Transaction(
13+
ty=3,
14+
sender=sender,
15+
max_fee_per_gas=10**10,
16+
max_priority_fee_per_gas=10**9,
17+
max_fee_per_blob_gas=10**9,
18+
gas_limit=100_000,
19+
data=b"\x00" * 150,
20+
access_list=[
21+
AccessList(address=0x3690, storage_keys=[100, 200]),
22+
AccessList(address=0xBEEF, storage_keys=[300]),
23+
],
24+
blob_versioned_hashes=add_kzg_version(
25+
[
26+
0x1111111111111111111111111111111111111111111111111111111111111111,
27+
0x2222222222222222222222222222222222222222222222222222222222222222,
28+
],
29+
0x01,
30+
),
31+
)

tests/conftest.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Global fixtures available to all fork tests."""
2+
3+
import pytest
4+
5+
from ethereum_test_types import Transaction
6+
7+
8+
@pytest.fixture
9+
def type_0_default_transaction(pre):
10+
"""Type 0 (legacy) default transaction available in all forks."""
11+
sender = pre.fund_eoa()
12+
return Transaction(
13+
ty=0,
14+
sender=sender,
15+
gas_price=10**9,
16+
gas_limit=100_000,
17+
data=b"\x00" * 100,
18+
)
19+
20+
21+
@pytest.fixture
22+
def typed_transaction(
23+
tx_type,
24+
type_0_default_transaction,
25+
type_1_default_transaction,
26+
type_2_default_transaction,
27+
type_3_default_transaction,
28+
type_4_default_transaction,
29+
):
30+
"""
31+
Fixture that provides a Transaction object based on the parametrized tx type.
32+
33+
This fixture works with the @pytest.mark.with_all_tx_types marker,
34+
which parametrizes the test with all transaction types supported by the fork.
35+
"""
36+
if tx_type == 0:
37+
return type_0_default_transaction
38+
elif tx_type == 1:
39+
return type_1_default_transaction
40+
elif tx_type == 2:
41+
return type_2_default_transaction
42+
elif tx_type == 3:
43+
return type_3_default_transaction
44+
elif tx_type == 4:
45+
return type_4_default_transaction
46+
else:
47+
raise ValueError(
48+
f"Transaction type `{tx_type}` needs `typed_transaction` fixture support."
49+
)

tests/london/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Test cases for EVM functionality introduced in London."""

tests/london/conftest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Fixtures for London fork tests."""
2+
3+
import pytest
4+
5+
from ethereum_test_base_types import AccessList
6+
from ethereum_test_types import Transaction
7+
8+
9+
@pytest.fixture
10+
def type_2_default_transaction(sender):
11+
"""Type 2 (dynamic fee) default transaction introduced in London fork."""
12+
return Transaction(
13+
ty=2,
14+
sender=sender,
15+
max_fee_per_gas=10**10,
16+
max_priority_fee_per_gas=10**9,
17+
gas_limit=100_000,
18+
data=b"\x00" * 200,
19+
access_list=[
20+
AccessList(address=0x2468, storage_keys=[10, 20, 30]),
21+
AccessList(address=0xACE0, storage_keys=[40, 50]),
22+
],
23+
)

0 commit comments

Comments
 (0)