Skip to content

test_exchange_with_push_sequence()

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

Generate fixtures for these test cases for Bogota with:

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

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
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 2 forks.