Skip to content

test_swapn_invalid_immediate_aborts()

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

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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
@EIPChecklist.Opcode.Test.ExceptionalAbort()
@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.