|
| 1 | +""" |
| 2 | +Pytest plugin providing default transaction fixtures for each transaction type. |
| 3 | +
|
| 4 | +Each fixture can be overridden in test files to customize transaction behavior. |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from ethereum_test_base_types import AccessList |
| 10 | +from ethereum_test_tools import Opcodes as Op |
| 11 | +from ethereum_test_types import AuthorizationTuple, Transaction, add_kzg_version |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def type_0_default_transaction(pre): |
| 16 | + """Type 0 (legacy) default transaction available in all forks.""" |
| 17 | + sender = pre.fund_eoa() |
| 18 | + return Transaction( |
| 19 | + ty=0, |
| 20 | + sender=sender, |
| 21 | + gas_price=10**9, |
| 22 | + gas_limit=100_000, |
| 23 | + data=b"\x00" * 100, |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def type_1_default_transaction(pre): |
| 29 | + """Type 1 (access list) default transaction introduced in Berlin fork.""" |
| 30 | + sender = pre.fund_eoa() |
| 31 | + return Transaction( |
| 32 | + ty=1, |
| 33 | + sender=sender, |
| 34 | + gas_price=10**9, |
| 35 | + gas_limit=100_000, |
| 36 | + data=b"\x00" * 100, |
| 37 | + access_list=[ |
| 38 | + AccessList(address=0x1234, storage_keys=[0, 1, 2]), |
| 39 | + AccessList(address=0x5678, storage_keys=[3, 4, 5]), |
| 40 | + AccessList(address=0x9ABC, storage_keys=[]), |
| 41 | + ], |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def type_2_default_transaction(pre): |
| 47 | + """Type 2 (dynamic fee) default transaction introduced in London fork.""" |
| 48 | + sender = pre.fund_eoa() |
| 49 | + return Transaction( |
| 50 | + ty=2, |
| 51 | + sender=sender, |
| 52 | + max_fee_per_gas=10**10, |
| 53 | + max_priority_fee_per_gas=10**9, |
| 54 | + gas_limit=100_000, |
| 55 | + data=b"\x00" * 200, |
| 56 | + access_list=[ |
| 57 | + AccessList(address=0x2468, storage_keys=[10, 20, 30]), |
| 58 | + AccessList(address=0xACE0, storage_keys=[40, 50]), |
| 59 | + ], |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.fixture |
| 64 | +def type_3_default_transaction(pre): |
| 65 | + """Type 3 (blob) default transaction introduced in Cancun fork.""" |
| 66 | + sender = pre.fund_eoa() |
| 67 | + return Transaction( |
| 68 | + ty=3, |
| 69 | + sender=sender, |
| 70 | + max_fee_per_gas=10**10, |
| 71 | + max_priority_fee_per_gas=10**9, |
| 72 | + max_fee_per_blob_gas=10**9, |
| 73 | + gas_limit=100_000, |
| 74 | + data=b"\x00" * 150, |
| 75 | + access_list=[ |
| 76 | + AccessList(address=0x3690, storage_keys=[100, 200]), |
| 77 | + AccessList(address=0xBEEF, storage_keys=[300]), |
| 78 | + ], |
| 79 | + blob_versioned_hashes=add_kzg_version( |
| 80 | + [ |
| 81 | + 0x1111111111111111111111111111111111111111111111111111111111111111, |
| 82 | + 0x2222222222222222222222222222222222222222222222222222222222222222, |
| 83 | + ], |
| 84 | + 0x01, |
| 85 | + ), |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.fixture |
| 90 | +def type_4_default_transaction(pre): |
| 91 | + """Type 4 (set code) default transaction introduced in Prague fork.""" |
| 92 | + sender = pre.fund_eoa() |
| 93 | + |
| 94 | + # Create authorized accounts with funds |
| 95 | + auth_signer1 = pre.fund_eoa(amount=10**18) |
| 96 | + auth_signer2 = pre.fund_eoa(amount=10**18) |
| 97 | + |
| 98 | + # Create target addresses that will be authorized |
| 99 | + target1 = pre.deploy_contract(Op.SSTORE(0, 1)) |
| 100 | + target2 = pre.deploy_contract(Op.SSTORE(0, 1)) |
| 101 | + |
| 102 | + return Transaction( |
| 103 | + ty=4, |
| 104 | + sender=sender, |
| 105 | + max_fee_per_gas=10**10, |
| 106 | + max_priority_fee_per_gas=10**9, |
| 107 | + gas_limit=150_000, |
| 108 | + data=b"\x00" * 200, |
| 109 | + access_list=[ |
| 110 | + AccessList(address=0x4567, storage_keys=[1000, 2000, 3000]), |
| 111 | + AccessList(address=0xCDEF, storage_keys=[4000, 5000]), |
| 112 | + ], |
| 113 | + authorization_list=[ |
| 114 | + AuthorizationTuple( |
| 115 | + chain_id=1, |
| 116 | + address=target1, |
| 117 | + nonce=0, |
| 118 | + signer=auth_signer1, |
| 119 | + ), |
| 120 | + AuthorizationTuple( |
| 121 | + chain_id=1, |
| 122 | + address=target2, |
| 123 | + nonce=0, |
| 124 | + signer=auth_signer2, |
| 125 | + ), |
| 126 | + ], |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +@pytest.fixture |
| 131 | +def typed_transaction(request, fork): |
| 132 | + """ |
| 133 | + Fixture that provides a Transaction object based on the parametrized tx type. |
| 134 | +
|
| 135 | + This fixture works with the @pytest.mark.with_all_typed_transactions marker, |
| 136 | + which parametrizes the test with all transaction types supported by the fork. |
| 137 | +
|
| 138 | + The actual transaction type value comes from the marker's parametrization. |
| 139 | + """ |
| 140 | + # The marker parametrizes 'typed_transaction' with tx type integers |
| 141 | + # Get the parametrized tx_type value |
| 142 | + if hasattr(request, "param"): |
| 143 | + # When parametrized by the marker, request.param contains the tx type |
| 144 | + tx_type = request.param |
| 145 | + else: |
| 146 | + raise ValueError( |
| 147 | + "`typed_transaction` fixture must be used with " |
| 148 | + "`@pytest.mark.with_all_typed_transactions` marker" |
| 149 | + ) |
| 150 | + |
| 151 | + fixture_name = f"type_{tx_type}_default_transaction" |
| 152 | + |
| 153 | + # Check if fixture exists - try to get it first |
| 154 | + try: |
| 155 | + # This will find fixtures defined in the test file or plugin |
| 156 | + return request.getfixturevalue(fixture_name) |
| 157 | + except pytest.FixtureLookupError as e: |
| 158 | + # Get all supported tx types for better error message |
| 159 | + supported_types = fork.tx_types() |
| 160 | + raise NotImplementedError( |
| 161 | + f"Fork {fork} supports transaction type {tx_type} but " |
| 162 | + f"fixture '{fixture_name}' is not implemented!\n" |
| 163 | + f"Fork {fork} supports transaction types: {supported_types}\n" |
| 164 | + f"Please add the missing fixture to " |
| 165 | + f"src/pytest_plugins/pytest_fixtures/transaction_fixtures.py" |
| 166 | + ) from e |
0 commit comments