Skip to content

test_dupn_invalid_immediate_aborts()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_dupn.py::test_dupn_invalid_immediate_aborts@2119b382.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8024_dupn_swapn_exchange/test_dupn.py::test_dupn_invalid_immediate_aborts --fork Amsterdam

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

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 DUPN with these immediates should abort.

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_dupn.py
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
@pytest.mark.parametrize(
    "invalid_immediate",
    list(range(91, 128)),  # 0x5b to 0x7f (JUMPDEST and PUSH opcodes)
    ids=lambda x: f"dupn_invalid_imm_0x{x:02x}",
)
def test_dupn_invalid_immediate_aborts(
    invalid_immediate: int,
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """
    Test DUPN with invalid immediate values (90 < x < 128) aborts execution.

    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 DUPN with these immediates should abort.
    """
    sender = pre.fund_eoa()

    # Build stack with enough items for any valid immediate
    # Maximum stack index is 235, so push 235 items
    code = Bytecode()
    for i in range(235):
        code += Op.PUSH1(i % 256)

    # Attempt DUPN with invalid immediate - should abort
    # Pass as bytes (raw immediate byte for testing invalid ranges)
    code += Op.DUPN[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.