Skip to content

test_vector_exchange_0x2f()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_eip_vectors.py::test_vector_exchange_0x2f@a9abd46e.

Generate fixtures for these test cases for Amsterdam with:

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

Test vector: e82f [EXCHANGE 1 19].

EXCHANGE with immediate 0x2f (47 decimal). decode_pair(0x2f) = (1, 19), swaps positions 2 and 20.

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_eip_vectors.py
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
def test_vector_exchange_0x2f(
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """
    Test vector: e82f [EXCHANGE 1 19].

    EXCHANGE with immediate 0x2f (47 decimal).
    decode_pair(0x2f) = (1, 19), swaps positions 2 and 20.
    """
    sender = pre.fund_eoa()

    # Build stack with 20 items
    # Position 2 and position 20 will be swapped
    code = Bytecode()
    code += Op.PUSH2(0xBBBB)  # Position 20 (bottom, will be swapped)
    for i in range(17):
        code += Op.PUSH2(0x1000 + i)  # Positions 3-19
    code += Op.PUSH2(0xAAAA)  # Position 2 (will be swapped)
    code += Op.PUSH2(0x1111)  # Position 1 (top)

    # EXCHANGE with immediate 0x2f - swaps positions 2 and 20
    # Hex: e8 2f
    code += Op.EXCHANGE[b"\x2f"]

    # Store position 1, 2, and 20 to verify
    code += Op.PUSH1(0) + Op.SSTORE  # Position 1 (top, unchanged)
    code += Op.PUSH1(1) + Op.SSTORE  # Position 2 (was 0xBBBB from pos 20)

    # Pop to get to position 20
    for _ in range(17):
        code += Op.POP

    code += Op.PUSH1(2) + Op.SSTORE  # Position 20 (was 0xAAAA from pos 2)
    code += Op.STOP

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

    # After EXCHANGE[0x2f]: positions 2 and 20 are swapped
    post = {
        contract_address: Account(
            storage={
                0: 0x1111,  # Position 1 unchanged
                1: 0xBBBB,  # Position 2 now has value from position 20
                2: 0xAAAA,  # Position 20 now has value from position 2
            }
        )
    }

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

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.