Skip to content

test_swapn_invalid_immediate_aborts()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py::test_swapn_invalid_immediate_aborts@c74f1a67.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py::test_swapn_invalid_immediate_aborts --fork Amsterdam

Test SWAPN with invalid immediate values (90 < x < 128) aborts.

Per EIP-8024, immediate values in range [91, 127] (0x5b-0x7f) are invalid because they correspond to JUMPDEST (0x5b) and PUSH opcodes (0x60-0x7f). Attempting to execute SWAPN with these immediates should abort.

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
@pytest.mark.parametrize(
    "invalid_immediate",
    list(range(91, 128)),  # 0x5b to 0x7f (JUMPDEST and PUSH opcodes)
    ids=lambda x: f"swapn_invalid_imm_0x{x:02x}",
)
def test_swapn_invalid_immediate_aborts(
    invalid_immediate: int,
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """
    Test SWAPN with invalid immediate values (90 < x < 128) aborts.

    Per EIP-8024, immediate values in range [91, 127] (0x5b-0x7f) are
    invalid because they correspond to JUMPDEST (0x5b) and PUSH opcodes
    (0x60-0x7f). Attempting to execute SWAPN with these immediates should
    abort.
    """
    sender = pre.fund_eoa()

    # Build a stack tall enough for any valid immediate (max decoded index
    # is 235, SWAPN needs index + 1 items) so the abort is caused by the
    # invalid immediate, never an underflow.
    code = Bytecode()
    for i in range(Spec.MAX_STACK_INDEX + 1):
        code += Op.PUSH1(i % 256)

    # Attempt SWAPN with invalid immediate - should abort.
    # Pass as bytes (raw immediate byte for testing invalid ranges).
    code += Op.SWAPN[invalid_immediate.to_bytes(1, "big")]

    # This should never execute.
    code += Op.PUSH1(0x42) + Op.PUSH1(0) + Op.SSTORE
    code += Op.STOP

    contract_address = pre.deploy_contract(code=code)

    tx = Transaction(to=contract_address, sender=sender)

    # Transaction should fail - invalid immediate causes abort.
    post = {contract_address: Account(storage={})}

    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 37 parametrized test cases across 1 fork.