Skip to content

Commit 9bc682d

Browse files
committed
fix(tests): Use parametrize_by_fork instead
1 parent c513f66 commit 9bc682d

2 files changed

Lines changed: 77 additions & 113 deletions

File tree

Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,38 @@
11
"""Test the tx type validation for EIP-2930."""
22

3+
from typing import Generator
4+
35
import pytest
46
from execution_testing import (
57
Account,
68
Alloc,
7-
Environment,
89
Fork,
10+
ParameterSet,
911
StateTestFiller,
1012
Transaction,
1113
TransactionException,
1214
)
1315
from execution_testing import Opcodes as Op
14-
from execution_testing.forks import Berlin, Byzantium
15-
from requests_cache import Callable
16+
from execution_testing.forks import Byzantium
1617

1718
from .spec import ref_spec_2930
1819

1920
REFERENCE_SPEC_GIT_PATH = ref_spec_2930.git_path
2021
REFERENCE_SPEC_VERSION = ref_spec_2930.version
2122

23+
TX_TYPE = 1
24+
2225

23-
@pytest.fixture
24-
def eip2930_tx_validity_test(
25-
state_test: StateTestFiller, pre: Alloc, fork: Fork, env: Environment
26-
) -> Callable[[], None]:
26+
def tx_validity(fork: Fork) -> Generator[ParameterSet, None, None]:
2727
"""
28-
Returns a function which applies a `state_test`, where a typed transaction
29-
is either valid and has an effect on state or not, depending on the fork.
28+
Return a generator of parameters for the tx validity test.
3029
"""
31-
32-
def test_function() -> None:
33-
valid = fork >= Berlin
34-
35-
account = pre.deploy_contract(
36-
code=Op.SSTORE(0, 1),
37-
storage={0: 0xDEADBEEF},
38-
)
39-
sender = pre.fund_eoa()
40-
41-
tx = Transaction(
42-
to=account,
43-
sender=sender,
44-
gas_limit=100_000,
45-
access_list=[],
46-
protected=fork >= Byzantium,
47-
error=TransactionException.TYPE_1_TX_PRE_FORK
48-
if not valid
49-
else None,
50-
)
51-
52-
post = {account: Account(storage={0: 0xDEADBEEF if not valid else 1})}
53-
if not valid:
54-
post[sender] = pre[sender] # type: ignore
55-
56-
state_test(env=env, pre=pre, post=post, tx=tx)
57-
58-
return test_function
30+
valid = TX_TYPE in fork.tx_types()
31+
yield pytest.param(
32+
valid,
33+
marks=[pytest.mark.exception_test] if not valid else [],
34+
id="valid" if valid else "invalid",
35+
)
5936

6037

6138
@pytest.mark.ported_from(
@@ -64,28 +41,33 @@ def test_function() -> None:
6441
],
6542
pr=["https://github.com/ethereum/execution-specs/pull/1754"],
6643
)
67-
@pytest.mark.exception_test
68-
@pytest.mark.valid_until("Istanbul")
69-
def test_eip2930_tx_invalid(
70-
eip2930_tx_validity_test: Callable[[], None],
44+
@pytest.mark.parametrize_by_fork("valid", tx_validity)
45+
def test_eip2930_tx_validity(
46+
state_test: StateTestFiller,
47+
fork: Fork,
48+
pre: Alloc,
49+
valid: bool,
7150
) -> None:
7251
"""
73-
Tests that an EIP-2930 tx has no effect before Berlin.
52+
Tests that an EIP-2930 tx is correctly rejected before fork activation.
7453
"""
75-
eip2930_tx_validity_test()
54+
account = pre.deploy_contract(
55+
code=Op.SSTORE(0, 1),
56+
storage={0: 0xDEADBEEF},
57+
)
58+
sender = pre.fund_eoa()
7659

60+
tx = Transaction(
61+
to=account,
62+
sender=sender,
63+
gas_limit=100_000,
64+
access_list=[],
65+
protected=fork >= Byzantium,
66+
error=TransactionException.TYPE_1_TX_PRE_FORK if not valid else None,
67+
)
7768

78-
@pytest.mark.ported_from(
79-
[
80-
"https://github.com/ethereum/legacytests/blob/master/src/LegacyTests/Cancun/GeneralStateTestsFiller/stExample/accessListExampleFiller.yml"
81-
],
82-
pr=["https://github.com/ethereum/execution-specs/pull/1754"],
83-
)
84-
@pytest.mark.valid_from("Berlin")
85-
def test_eip2930_tx_valid(
86-
eip2930_tx_validity_test: Callable[[], None],
87-
) -> None:
88-
"""
89-
Tests that an EIP-2930 tx has an effect after Berlin.
90-
"""
91-
eip2930_tx_validity_test()
69+
post = {account: Account(storage={0: 0xDEADBEEF if not valid else 1})}
70+
if not valid:
71+
post[sender] = pre[sender] # type: ignore
72+
73+
state_test(pre=pre, post=post, tx=tx)
Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,38 @@
11
"""Test the tx type validation for EIP-1559."""
22

3+
from typing import Generator
4+
35
import pytest
46
from execution_testing import (
57
Account,
68
Alloc,
7-
Environment,
89
Fork,
10+
ParameterSet,
911
StateTestFiller,
1012
Transaction,
1113
TransactionException,
1214
)
1315
from execution_testing import Opcodes as Op
14-
from execution_testing.forks import Byzantium, London
15-
from requests_cache import Callable
16+
from execution_testing.forks import Byzantium
1617

1718
from .spec import ref_spec_1559
1819

1920
REFERENCE_SPEC_GIT_PATH = ref_spec_1559.git_path
2021
REFERENCE_SPEC_VERSION = ref_spec_1559.version
2122

23+
TX_TYPE = 2
24+
2225

23-
@pytest.fixture
24-
def eip1559_tx_validity_test(
25-
state_test: StateTestFiller, pre: Alloc, fork: Fork, env: Environment
26-
) -> Callable[[], None]:
26+
def tx_validity(fork: Fork) -> Generator[ParameterSet, None, None]:
2727
"""
28-
Returns a function which applies a `state_test`, where a typed transaction
29-
is either valid and has an effect on state or not, depending on the fork.
28+
Return a generator of parameters for the tx validity test.
3029
"""
31-
32-
def test_function() -> None:
33-
valid = fork >= London
34-
35-
account = pre.deploy_contract(
36-
code=Op.SSTORE(0, 1),
37-
storage={0: 0xDEADBEEF},
38-
)
39-
sender = pre.fund_eoa()
40-
41-
tx = Transaction(
42-
to=account,
43-
sender=sender,
44-
gas_limit=100_000,
45-
max_priority_fee_per_gas=1,
46-
protected=fork >= Byzantium,
47-
error=TransactionException.TYPE_2_TX_PRE_FORK
48-
if not valid
49-
else None,
50-
)
51-
52-
post = {account: Account(storage={0: 0xDEADBEEF if not valid else 1})}
53-
if not valid:
54-
post[sender] = pre[sender] # type: ignore
55-
56-
state_test(env=env, pre=pre, post=post, tx=tx)
57-
58-
return test_function
30+
valid = TX_TYPE in fork.tx_types()
31+
yield pytest.param(
32+
valid,
33+
marks=[pytest.mark.exception_test] if not valid else [],
34+
id="valid" if valid else "invalid",
35+
)
5936

6037

6138
@pytest.mark.ported_from(
@@ -64,28 +41,33 @@ def test_function() -> None:
6441
],
6542
pr=["https://github.com/ethereum/execution-specs/pull/1754"],
6643
)
67-
@pytest.mark.exception_test
68-
@pytest.mark.valid_until("Berlin")
69-
def test_eip1559_tx_invalid(
70-
eip1559_tx_validity_test: Callable[[], None],
44+
@pytest.mark.parametrize_by_fork("valid", tx_validity)
45+
def test_eip1559_tx_validity(
46+
state_test: StateTestFiller,
47+
fork: Fork,
48+
pre: Alloc,
49+
valid: bool,
7150
) -> None:
7251
"""
7352
Tests that an EIP-1559 tx has no effect before London.
7453
"""
75-
eip1559_tx_validity_test()
54+
account = pre.deploy_contract(
55+
code=Op.SSTORE(0, 1),
56+
storage={0: 0xDEADBEEF},
57+
)
58+
sender = pre.fund_eoa()
7659

60+
tx = Transaction(
61+
to=account,
62+
sender=sender,
63+
gas_limit=100_000,
64+
max_priority_fee_per_gas=1,
65+
protected=fork >= Byzantium,
66+
error=TransactionException.TYPE_2_TX_PRE_FORK if not valid else None,
67+
)
7768

78-
@pytest.mark.ported_from(
79-
[
80-
"https://github.com/ethereum/legacytests/blob/master/Cancun/GeneralStateTests/stEIP1559/typeTwoBerlin.json"
81-
],
82-
pr=["https://github.com/ethereum/execution-specs/pull/1754"],
83-
)
84-
@pytest.mark.valid_from("London")
85-
def test_eip1559_tx_valid(
86-
eip1559_tx_validity_test: Callable[[], None],
87-
) -> None:
88-
"""
89-
Tests that an EIP-1559 tx has an effect after London.
90-
"""
91-
eip1559_tx_validity_test()
69+
post = {account: Account(storage={0: 0xDEADBEEF if not valid else 1})}
70+
if not valid:
71+
post[sender] = pre[sender] # type: ignore
72+
73+
state_test(pre=pre, post=post, tx=tx)

0 commit comments

Comments
 (0)