Skip to content

test_vector_exchange_valid_0x51()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_eip_vectors.py::test_vector_exchange_valid_0x51@5c024cbb.

Generate fixtures for these test cases for Amsterdam with:

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

Test vector: e851 [EXCHANGE 14 15].

EXCHANGE with immediate 0x51 (81 decimal) is valid. decode_pair(0x51) = (14, 15), swaps positions 15 and 16.

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_eip_vectors.py
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
def test_vector_exchange_valid_0x51(
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """
    Test vector: e851 [EXCHANGE 14 15].

    EXCHANGE with immediate 0x51 (81 decimal) is valid.
    decode_pair(0x51) = (14, 15), swaps positions 15 and 16.
    """
    sender = pre.fund_eoa()

    # Build stack with 16 items, with known values at positions 15 and 16
    code = Bytecode()
    for i in range(16):
        stack_pos = 16 - i
        if stack_pos == 15:
            code += Op.PUSH2(0xAAAA)
        elif stack_pos == 16:
            code += Op.PUSH2(0xBBBB)
        else:
            code += Op.PUSH2(0x1000 + i)

    # EXCHANGE with immediate 0x51 - swaps positions 15 and 16
    code += Op.EXCHANGE[b"\x51"]

    # Pop to position 15 (pop 14 items)
    for _ in range(14):
        code += Op.POP
    code += Op.PUSH1(0) + Op.SSTORE  # Position 15 (was 0xBBBB)
    code += Op.PUSH1(1) + Op.SSTORE  # Position 16 (was 0xAAAA)
    code += Op.STOP

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

    post = {
        contract_address: Account(
            storage={
                0: 0xBBBB,  # Position 15 now has value from 16
                1: 0xAAAA,  # Position 16 now has value from 15
            }
        )
    }

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

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.