|
| 1 | +""" |
| 2 | +SSTORE combination tests for all initial storage states. |
| 3 | +
|
| 4 | +Exercises every combination of call types across four call slots, |
| 5 | +varying the update-contract's initial storage state (0, 1, or 2). |
| 6 | +
|
| 7 | +Ported from all ``sstore_combinations_initial*_ParisFiller.json`` |
| 8 | +in ``state_tests/stTimeConsuming/``. |
| 9 | +""" |
| 10 | + |
| 11 | +from enum import StrEnum |
| 12 | + |
| 13 | +import pytest |
| 14 | +from execution_testing import ( |
| 15 | + Account, |
| 16 | + Alloc, |
| 17 | + Bytecode, |
| 18 | + StateTestFiller, |
| 19 | + Transaction, |
| 20 | + compute_create_address, |
| 21 | +) |
| 22 | +from execution_testing.forks import Fork |
| 23 | +from execution_testing.vm import Op |
| 24 | + |
| 25 | +REFERENCE_SPEC_GIT_PATH = "N/A" |
| 26 | +REFERENCE_SPEC_VERSION = "N/A" |
| 27 | + |
| 28 | +pytestmark = [ |
| 29 | + pytest.mark.ported_from( |
| 30 | + "state_tests/stTimeConsuming/sstore_combinations_initial00_ParisFiller.json", |
| 31 | + "state_tests/stTimeConsuming/sstore_combinations_initial00_2_ParisFiller.json", |
| 32 | + "state_tests/stTimeConsuming/sstore_combinations_initial01_ParisFiller.json", |
| 33 | + "state_tests/stTimeConsuming/sstore_combinations_initial01_2_ParisFiller.json", |
| 34 | + "state_tests/stTimeConsuming/sstore_combinations_initial10_ParisFiller.json", |
| 35 | + "state_tests/stTimeConsuming/sstore_combinations_initial10_2_ParisFiller.json", |
| 36 | + "state_tests/stTimeConsuming/sstore_combinations_initial11_ParisFiller.json", |
| 37 | + "state_tests/stTimeConsuming/sstore_combinations_initial11_2_ParisFiller.json", |
| 38 | + "state_tests/stTimeConsuming/sstore_combinations_initial20_ParisFiller.json", |
| 39 | + "state_tests/stTimeConsuming/sstore_combinations_initial20_2_ParisFiller.json", |
| 40 | + "state_tests/stTimeConsuming/sstore_combinations_initial21_ParisFiller.json", |
| 41 | + "state_tests/stTimeConsuming/sstore_combinations_initial21_2_ParisFiller.json", |
| 42 | + ), |
| 43 | + pytest.mark.valid_from("Byzantium"), |
| 44 | + pytest.mark.slow, |
| 45 | +] |
| 46 | + |
| 47 | + |
| 48 | +class MidContractActions(StrEnum): |
| 49 | + """List of actions the middle contracts can perform.""" |
| 50 | + |
| 51 | + NOOP = "noop" |
| 52 | + SSTORE_TOGGLE = "sstore-toggle" |
| 53 | + REVERT = "revert" |
| 54 | + |
| 55 | + |
| 56 | +# Middle-action combinations: (call_opcode, side_contract_index). |
| 57 | +# Side-contract indices: 0=noop, 1=sstore-toggle, 2=reverting. |
| 58 | +MIDDLE_ACTIONS = [ |
| 59 | + (op, t) |
| 60 | + for op in [ |
| 61 | + Op.CALL, |
| 62 | + Op.CALLCODE, |
| 63 | + Op.DELEGATECALL, |
| 64 | + Op.STATICCALL, |
| 65 | + ] |
| 66 | + for t in MidContractActions |
| 67 | +] |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.parametrize( |
| 71 | + "update_storage_initial_value", |
| 72 | + range(3), |
| 73 | + ids=["initial0", "initial1", "initial2"], |
| 74 | +) |
| 75 | +@pytest.mark.parametrize( |
| 76 | + "call_4, call_4_target", |
| 77 | + MIDDLE_ACTIONS, |
| 78 | + ids=[f"call_4_{op}_{target}" for op, target in MIDDLE_ACTIONS], |
| 79 | +) |
| 80 | +@pytest.mark.parametrize( |
| 81 | + "call_3", |
| 82 | + [Op.STATICCALL, Op.CALL, Op.CALLCODE, Op.DELEGATECALL], |
| 83 | +) |
| 84 | +@pytest.mark.parametrize( |
| 85 | + "call_2, call_2_target", |
| 86 | + MIDDLE_ACTIONS, |
| 87 | + ids=[f"call_2_{op}_{target}" for op, target in MIDDLE_ACTIONS], |
| 88 | +) |
| 89 | +@pytest.mark.parametrize( |
| 90 | + "call_1", |
| 91 | + [Op.CALL, Op.CALLCODE, Op.DELEGATECALL], |
| 92 | +) |
| 93 | +def test_sstore_combinations_initial( |
| 94 | + state_test: StateTestFiller, |
| 95 | + pre: Alloc, |
| 96 | + fork: Fork, |
| 97 | + update_storage_initial_value: int, |
| 98 | + call_1: Op, |
| 99 | + call_2: Op, |
| 100 | + call_2_target: MidContractActions, |
| 101 | + call_3: Op, |
| 102 | + call_4: Op, |
| 103 | + call_4_target: MidContractActions, |
| 104 | +) -> None: |
| 105 | + """Test SSTORE with four interleaved calls.""" |
| 106 | + sender = pre.fund_eoa() |
| 107 | + side = { |
| 108 | + # Noop / balance-only (no executable code) |
| 109 | + MidContractActions.NOOP: pre.deploy_contract( |
| 110 | + Bytecode(), |
| 111 | + balance=10, |
| 112 | + storage={0: 1, 1: 1, 2: 1}, |
| 113 | + ), |
| 114 | + # SSTORE-toggle: flip slots 1..16, then set slot 1 = 1 |
| 115 | + MidContractActions.SSTORE_TOGGLE: pre.deploy_contract( |
| 116 | + code=sum( |
| 117 | + Op.SSTORE(key=i, value=0x1) + Op.SSTORE(key=i, value=0x0) |
| 118 | + for i in range(0x1, 0x10 + 1) |
| 119 | + ) |
| 120 | + + Op.SSTORE(key=0x1, value=0x1) |
| 121 | + + Op.STOP, |
| 122 | + ), |
| 123 | + # Reverting contract |
| 124 | + MidContractActions.REVERT: pre.deploy_contract( |
| 125 | + code=Op.REVERT(offset=0x0, size=0x20) + Op.STOP, |
| 126 | + storage={0: 2, 1: 2, 2: 2}, |
| 127 | + ), |
| 128 | + } |
| 129 | + |
| 130 | + update_contract = pre.deploy_contract( |
| 131 | + code=Op.SSTORE(key=0x0, value=0x0) |
| 132 | + + Op.SSTORE(key=0x1, value=0x1) |
| 133 | + + Op.SSTORE(key=0x2, value=0x2) |
| 134 | + + Op.STOP, |
| 135 | + storage={ |
| 136 | + 0: update_storage_initial_value, |
| 137 | + 1: update_storage_initial_value, |
| 138 | + 2: update_storage_initial_value, |
| 139 | + } |
| 140 | + if update_storage_initial_value > 0 |
| 141 | + else {}, |
| 142 | + ) |
| 143 | + sstore_toggle = side[MidContractActions.SSTORE_TOGGLE] |
| 144 | + |
| 145 | + call_gas = 0x493E0 |
| 146 | + |
| 147 | + initcode = ( |
| 148 | + Op.MSTORE(offset=0x64, value=0x0) |
| 149 | + + Op.POP( |
| 150 | + call_1( |
| 151 | + gas=call_gas, |
| 152 | + address=update_contract, |
| 153 | + args_size=0x20, |
| 154 | + ) |
| 155 | + ) |
| 156 | + + Op.POP(call_2(gas=call_gas, address=side[call_2_target])) |
| 157 | + + Op.POP( |
| 158 | + call_3( |
| 159 | + gas=call_gas, |
| 160 | + address=update_contract, |
| 161 | + args_size=0x20, |
| 162 | + ) |
| 163 | + ) |
| 164 | + + Op.POP(call_4(gas=call_gas, address=side[call_4_target])) |
| 165 | + + Op.CALL(gas=call_gas * 2, address=sstore_toggle) |
| 166 | + + Op.STOP |
| 167 | + ) |
| 168 | + |
| 169 | + tx = Transaction( |
| 170 | + sender=sender, |
| 171 | + to=None, |
| 172 | + data=initcode, |
| 173 | + gas_limit=2_000_000, |
| 174 | + value=1, |
| 175 | + protected=fork.supports_protected_txs(), |
| 176 | + ) |
| 177 | + |
| 178 | + post = { |
| 179 | + sstore_toggle: Account(storage={1: 1}), |
| 180 | + compute_create_address(address=sender, nonce=0): Account(nonce=1), |
| 181 | + } |
| 182 | + |
| 183 | + state_test(pre=pre, post=post, tx=tx) |
| 184 | + |
| 185 | + |
| 186 | +@pytest.mark.parametrize( |
| 187 | + "update_storage_initial_value", |
| 188 | + range(3), |
| 189 | + ids=["initial0", "initial1", "initial2"], |
| 190 | +) |
| 191 | +def test_sstore_combinations_initial_staticcall_only( |
| 192 | + state_test: StateTestFiller, |
| 193 | + pre: Alloc, |
| 194 | + fork: Fork, |
| 195 | + update_storage_initial_value: int, |
| 196 | +) -> None: |
| 197 | + """Base case: STATICCALL to update-contract only.""" |
| 198 | + sender = pre.fund_eoa() |
| 199 | + |
| 200 | + update_contract = pre.deploy_contract( |
| 201 | + code=Op.SSTORE(key=0x0, value=0x0) |
| 202 | + + Op.SSTORE(key=0x1, value=0x1) |
| 203 | + + Op.SSTORE(key=0x2, value=0x2) |
| 204 | + + Op.STOP, |
| 205 | + storage={ |
| 206 | + 0: update_storage_initial_value, |
| 207 | + 1: update_storage_initial_value, |
| 208 | + 2: update_storage_initial_value, |
| 209 | + } |
| 210 | + if update_storage_initial_value > 0 |
| 211 | + else {}, |
| 212 | + ) |
| 213 | + sstore_toggle = pre.deploy_contract( |
| 214 | + code=sum( |
| 215 | + Op.SSTORE(key=i, value=0x1) + Op.SSTORE(key=i, value=0x0) |
| 216 | + for i in range(0x1, 0x10 + 1) |
| 217 | + ) |
| 218 | + + Op.SSTORE(key=0x1, value=0x1) |
| 219 | + + Op.STOP, |
| 220 | + ) |
| 221 | + |
| 222 | + call_gas = 0x493E0 |
| 223 | + |
| 224 | + initcode = ( |
| 225 | + Op.MSTORE(offset=0x64, value=0x0) |
| 226 | + + Op.POP( |
| 227 | + Op.STATICCALL( |
| 228 | + gas=call_gas, |
| 229 | + address=update_contract, |
| 230 | + args_size=0x20, |
| 231 | + ) |
| 232 | + ) |
| 233 | + + Op.CALL(gas=call_gas * 2, address=sstore_toggle) |
| 234 | + + Op.STOP |
| 235 | + ) |
| 236 | + |
| 237 | + tx = Transaction( |
| 238 | + sender=sender, |
| 239 | + to=None, |
| 240 | + data=initcode, |
| 241 | + gas_limit=2_000_000, |
| 242 | + value=1, |
| 243 | + protected=fork.supports_protected_txs(), |
| 244 | + ) |
| 245 | + |
| 246 | + post = { |
| 247 | + sstore_toggle: Account(storage={1: 1}), |
| 248 | + compute_create_address(address=sender, nonce=0): Account(nonce=1), |
| 249 | + } |
| 250 | + |
| 251 | + state_test(pre=pre, post=post, tx=tx) |
0 commit comments