|
| 1 | +"""Tests for EIP-7928 for EIP-2930 transactions.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from ethereum_test_tools import ( |
| 6 | + AccessList, |
| 7 | + Account, |
| 8 | + Alloc, |
| 9 | + Block, |
| 10 | + BlockchainTestFiller, |
| 11 | + Hash, |
| 12 | + Transaction, |
| 13 | +) |
| 14 | +from ethereum_test_tools.vm.opcode import Opcodes as Op |
| 15 | +from ethereum_test_types.block_access_list import ( |
| 16 | + BalAccountExpectation, |
| 17 | + BalNonceChange, |
| 18 | + BalStorageChange, |
| 19 | + BalStorageSlot, |
| 20 | + BlockAccessListExpectation, |
| 21 | +) |
| 22 | + |
| 23 | +from .spec import ref_spec_7928 |
| 24 | + |
| 25 | +REFERENCE_SPEC_GIT_PATH = ref_spec_7928.git_path |
| 26 | +REFERENCE_SPEC_VERSION = ref_spec_7928.version |
| 27 | + |
| 28 | +pytestmark = pytest.mark.valid_from("Amsterdam") |
| 29 | + |
| 30 | + |
| 31 | +def test_bal_2930_slot_listed_but_untouched( |
| 32 | + pre: Alloc, |
| 33 | + blockchain_test: BlockchainTestFiller, |
| 34 | + fork, |
| 35 | +): |
| 36 | + """Ensure BAL excludes untouched access list storage slots.""" |
| 37 | + alice = pre.fund_eoa() |
| 38 | + pure_calculator = pre.deploy_contract( |
| 39 | + # Pure add operation |
| 40 | + Op.ADD(35, 7) |
| 41 | + ) |
| 42 | + |
| 43 | + access_list = AccessList( |
| 44 | + address=pure_calculator, |
| 45 | + storage_keys=[Hash(0x1)], |
| 46 | + ) |
| 47 | + |
| 48 | + intrinsic_gas_calculator = fork.transaction_intrinsic_cost_calculator() |
| 49 | + gas_limit = ( |
| 50 | + intrinsic_gas_calculator( |
| 51 | + calldata=b"", |
| 52 | + contract_creation=False, |
| 53 | + access_list=[access_list], |
| 54 | + ) |
| 55 | + + 1000 |
| 56 | + ) # intrinsic + buffer |
| 57 | + |
| 58 | + tx = Transaction( |
| 59 | + ty=1, sender=alice, to=pure_calculator, gas_limit=gas_limit, access_list=[access_list] |
| 60 | + ) |
| 61 | + |
| 62 | + block = Block( |
| 63 | + txs=[tx], |
| 64 | + expected_block_access_list=BlockAccessListExpectation( |
| 65 | + account_expectations={ |
| 66 | + alice: BalAccountExpectation( |
| 67 | + nonce_changes=[BalNonceChange(tx_index=1, post_nonce=1)], |
| 68 | + ), |
| 69 | + # The address excluded from BAL since state is not accessed |
| 70 | + pure_calculator: None, |
| 71 | + } |
| 72 | + ), |
| 73 | + ) |
| 74 | + |
| 75 | + blockchain_test( |
| 76 | + pre=pre, |
| 77 | + blocks=[block], |
| 78 | + post={ |
| 79 | + alice: Account(nonce=1), |
| 80 | + }, |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +def test_bal_2930_slot_listed_and_unlisted_writes( |
| 85 | + pre: Alloc, |
| 86 | + blockchain_test: BlockchainTestFiller, |
| 87 | + fork, |
| 88 | +): |
| 89 | + """Ensure BAL includes storage writes regardless of access list presence.""" |
| 90 | + alice = pre.fund_eoa() |
| 91 | + storage_writer = pre.deploy_contract(code=Op.SSTORE(0x01, 0x42) + Op.SSTORE(0x02, 0x43)) |
| 92 | + |
| 93 | + # Access list only includes slot 0x01, but contract writes to both 0x01 and 0x02 |
| 94 | + access_list = AccessList( |
| 95 | + address=storage_writer, |
| 96 | + storage_keys=[Hash(0x01)], |
| 97 | + ) |
| 98 | + |
| 99 | + intrinsic_gas_calculator = fork.transaction_intrinsic_cost_calculator() |
| 100 | + gas_limit = ( |
| 101 | + intrinsic_gas_calculator( |
| 102 | + calldata=b"", |
| 103 | + contract_creation=False, |
| 104 | + access_list=[access_list], |
| 105 | + ) |
| 106 | + + 50000 |
| 107 | + ) # intrinsic + buffer for storage writes |
| 108 | + |
| 109 | + tx = Transaction( |
| 110 | + ty=1, sender=alice, to=storage_writer, gas_limit=gas_limit, access_list=[access_list] |
| 111 | + ) |
| 112 | + |
| 113 | + block = Block( |
| 114 | + txs=[tx], |
| 115 | + expected_block_access_list=BlockAccessListExpectation( |
| 116 | + account_expectations={ |
| 117 | + alice: BalAccountExpectation( |
| 118 | + nonce_changes=[BalNonceChange(tx_index=1, post_nonce=1)], |
| 119 | + ), |
| 120 | + storage_writer: BalAccountExpectation( |
| 121 | + storage_changes=[ |
| 122 | + BalStorageSlot( |
| 123 | + slot=0x01, |
| 124 | + slot_changes=[BalStorageChange(tx_index=1, post_value=0x42)], |
| 125 | + ), |
| 126 | + BalStorageSlot( |
| 127 | + slot=0x02, |
| 128 | + slot_changes=[BalStorageChange(tx_index=1, post_value=0x43)], |
| 129 | + ), |
| 130 | + ], |
| 131 | + ), |
| 132 | + } |
| 133 | + ), |
| 134 | + ) |
| 135 | + |
| 136 | + blockchain_test( |
| 137 | + pre=pre, |
| 138 | + blocks=[block], |
| 139 | + post={ |
| 140 | + alice: Account(nonce=1), |
| 141 | + storage_writer: Account(storage={0x01: 0x42, 0x02: 0x43}), |
| 142 | + }, |
| 143 | + ) |
| 144 | + |
| 145 | + |
| 146 | +def test_bal_2930_slot_listed_and_unlisted_reads( |
| 147 | + pre: Alloc, |
| 148 | + blockchain_test: BlockchainTestFiller, |
| 149 | + fork, |
| 150 | +): |
| 151 | + """Ensure BAL includes storage reads regardless of access list presence.""" |
| 152 | + alice = pre.fund_eoa() |
| 153 | + storage_reader = pre.deploy_contract( |
| 154 | + code=Op.SLOAD(0x01) + Op.SLOAD(0x02), |
| 155 | + storage={0x01: 0x42, 0x02: 0x43}, # Pre-populate storage with values |
| 156 | + ) |
| 157 | + |
| 158 | + # Access list only includes slot 0x01, but contract reads from both 0x01 and 0x02 |
| 159 | + access_list = AccessList( |
| 160 | + address=storage_reader, |
| 161 | + storage_keys=[Hash(0x01)], |
| 162 | + ) |
| 163 | + |
| 164 | + intrinsic_gas_calculator = fork.transaction_intrinsic_cost_calculator() |
| 165 | + gas_limit = ( |
| 166 | + intrinsic_gas_calculator( |
| 167 | + calldata=b"", |
| 168 | + contract_creation=False, |
| 169 | + access_list=[access_list], |
| 170 | + ) |
| 171 | + + 50000 |
| 172 | + ) # intrinsic + buffer for storage reads |
| 173 | + |
| 174 | + tx = Transaction( |
| 175 | + ty=1, sender=alice, to=storage_reader, gas_limit=gas_limit, access_list=[access_list] |
| 176 | + ) |
| 177 | + |
| 178 | + block = Block( |
| 179 | + txs=[tx], |
| 180 | + expected_block_access_list=BlockAccessListExpectation( |
| 181 | + account_expectations={ |
| 182 | + alice: BalAccountExpectation( |
| 183 | + nonce_changes=[BalNonceChange(tx_index=1, post_nonce=1)], |
| 184 | + ), |
| 185 | + storage_reader: BalAccountExpectation( |
| 186 | + storage_reads=[0x01, 0x02], |
| 187 | + ), |
| 188 | + } |
| 189 | + ), |
| 190 | + ) |
| 191 | + |
| 192 | + blockchain_test( |
| 193 | + pre=pre, |
| 194 | + blocks=[block], |
| 195 | + post={ |
| 196 | + alice: Account(nonce=1), |
| 197 | + storage_reader: Account(storage={0x01: 0x42, 0x02: 0x43}), |
| 198 | + }, |
| 199 | + ) |
0 commit comments