|
| 1 | +"""Test the tx type validation for EIP-2930.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from execution_testing import ( |
| 5 | + Account, |
| 6 | + Alloc, |
| 7 | + Environment, |
| 8 | + Fork, |
| 9 | + StateTestFiller, |
| 10 | + Transaction, |
| 11 | + TransactionException, |
| 12 | +) |
| 13 | +from execution_testing import Opcodes as Op |
| 14 | +from execution_testing.forks import Berlin, Byzantium |
| 15 | +from requests_cache import Callable |
| 16 | + |
| 17 | +from .spec import ref_spec_2930 |
| 18 | + |
| 19 | +REFERENCE_SPEC_GIT_PATH = ref_spec_2930.git_path |
| 20 | +REFERENCE_SPEC_VERSION = ref_spec_2930.version |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def eip2930_tx_validity_test( |
| 25 | + state_test: StateTestFiller, pre: Alloc, fork: Fork, env: Environment |
| 26 | +) -> Callable[[], None]: |
| 27 | + """ |
| 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. |
| 30 | + """ |
| 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 |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.ported_from( |
| 62 | + [ |
| 63 | + "https://github.com/ethereum/legacytests/blob/master/src/LegacyTests/Cancun/GeneralStateTestsFiller/stExample/accessListExampleFiller.yml" |
| 64 | + ], |
| 65 | + pr=["https://github.com/ethereum/execution-specs/pull/1754"], |
| 66 | +) |
| 67 | +@pytest.mark.exception_test |
| 68 | +@pytest.mark.valid_until("Istanbul") |
| 69 | +def test_eip2930_tx_invalid( |
| 70 | + eip2930_tx_validity_test: Callable[[], None], |
| 71 | +) -> None: |
| 72 | + """ |
| 73 | + Tests that an EIP-2930 tx has no effect before Berlin. |
| 74 | + """ |
| 75 | + eip2930_tx_validity_test() |
| 76 | + |
| 77 | + |
| 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() |
0 commit comments