Skip to content

test_vector_exchange_0x9d()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_eip_vectors.py::test_vector_exchange_0x9d@9c2813ee.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8024_dupn_swapn_exchange/test_eip_vectors.py::test_vector_exchange_0x9d --fork Amsterdam

Test vector: e89d [EXCHANGE 2 3].

EXCHANGE with immediate 0x9d (157 decimal). decode_pair(0x9d) = (2, 3), swaps positions 3 and 4.

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_eip_vectors.py
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
def test_vector_exchange_0x9d(
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """
    Test vector: e89d [EXCHANGE 2 3].

    EXCHANGE with immediate 0x9d (157 decimal).
    decode_pair(0x9d) = (2, 3), swaps positions 3 and 4.
    """
    sender = pre.fund_eoa()

    # Build stack with 4 items: positions 1, 2, 3, 4 from top
    # Values: [top=0x1111, pos2=0x2222, pos3=0x3333, pos4=0x4444]
    code = Bytecode()
    code += Op.PUSH2(0x4444)  # Position 4 (will be swapped)
    code += Op.PUSH2(0x3333)  # Position 3 (will be swapped)
    code += Op.PUSH2(0x2222)  # Position 2
    code += Op.PUSH2(0x1111)  # Position 1 (top)

    # EXCHANGE with immediate 0x9d - swaps positions 3 and 4
    # Hex: e8 9d
    code += Op.EXCHANGE[b"\x9d"]

    # Store all values to verify the swap
    code += Op.PUSH1(0) + Op.SSTORE  # Position 1 (top)
    code += Op.PUSH1(1) + Op.SSTORE  # Position 2
    code += Op.PUSH1(2) + Op.SSTORE  # Position 3 (was 4)
    code += Op.PUSH1(3) + Op.SSTORE  # Position 4 (was 3)
    code += Op.STOP

    contract_address = pre.deploy_contract(code=code)
    tx = Transaction(to=contract_address, sender=sender, gas_limit=1_000_000)

    # After EXCHANGE[0x9d]: positions 3 and 4 are swapped
    post = {
        contract_address: Account(
            storage={
                0: 0x1111,  # Position 1 unchanged
                1: 0x2222,  # Position 2 unchanged
                2: 0x4444,  # Position 3 now has value from position 4
                3: 0x3333,  # Position 4 now has value from position 3
            }
        )
    }

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

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.