Skip to content

test_exchange_with_push_sequence()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_exchange.py::test_exchange_with_push_sequence@2119b382.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8024_dupn_swapn_exchange/test_exchange.py::test_exchange_with_push_sequence --fork Amsterdam

Test EXCHANGE swapping positions 10 and 17 with a push sequence.

Build 17 stack items with markers at positions 10 and 17. EXCHANGE[0x00]: decode_pair(0) = (9, 16), swaps positions 10 and 17.

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_exchange.py
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
def test_exchange_with_push_sequence(
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """
    Test EXCHANGE swapping positions 10 and 17 with a push sequence.

    Build 17 stack items with markers at positions 10 and 17.
    EXCHANGE[0x00]: decode_pair(0) = (9, 16), swaps positions 10 and 17.
    """
    sender = pre.fund_eoa()

    # Build 17 items with markers at positions 10 and 17
    code = Bytecode()
    for i in range(17):
        stack_pos = 17 - i  # Position from top (1-indexed)
        if stack_pos == 10:
            code += Op.PUSH2(0xAAAA)
        elif stack_pos == 17:
            code += Op.PUSH2(0xBBBB)
        else:
            code += Op.PUSH1(0)

    # EXCHANGE with immediate 0 (decode_pair(0) = (9, 16))
    # swaps pos 10 and 17
    code += Op.EXCHANGE[b"\x00"]

    # Store all stack values to verify
    for i in range(17):
        code += Op.PUSH1(i) + Op.SSTORE

    code += Op.STOP

    contract_address = pre.deploy_contract(code=code)

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

    # Expected: position 9 has 0xBBBB (from pos 17), position 16 has
    # 0xAAAA (from pos 10), rest = 0
    expected_storage = {}
    for i in range(17):
        stack_pos = i + 1  # 1-indexed position from top
        if stack_pos == 10:
            expected_storage[i] = 0xBBBB  # Swapped from position 17
        elif stack_pos == 17:
            expected_storage[i] = 0xAAAA  # Swapped from position 10
        else:
            expected_storage[i] = 0

    post = {contract_address: Account(storage=expected_storage)}

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

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.