Skip to content

test_swapn_basic()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py::test_swapn_basic@b314d18e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py::test_swapn_basic --fork Amsterdam

Test SWAPN with various stack indices (17-235).

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
@pytest.mark.parametrize(
    "stack_index",
    [17, 18, 32, 64, 107, 108, 200, 235],
    ids=lambda x: f"swapn_stack_{x}",
)
def test_swapn_basic(
    stack_index: int,
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """Test SWAPN with various stack indices (17-235)."""
    sender = pre.fund_eoa()

    # SWAPN with decoded value n swaps position 1 with position (n+1)
    # So we need stack_height = stack_index + 1 items
    stack_height = stack_index + 1
    top_value = 0xAAAA
    swap_target_value = 0xBBBB

    # Build stack with known values at top and swap position (n+1)
    code = Bytecode()

    # First push ends up at position (stack_index+1) from top
    code += Op.PUSH2(swap_target_value)

    # Pushes in-between
    for i in range(1, stack_height - 1):
        code += Op.PUSH2(0x1000 + i)

    # Last push ends up at top
    code += Op.PUSH2(top_value)

    # Pass stack index directly - encoder will handle encoding
    code += Op.SWAPN[stack_index]

    # Store both swapped values to verify
    code += Op.PUSH1(0) + Op.SSTORE  # New top (was swap_target_value)

    # Pop intermediate values to get to the swapped position
    for _ in range(stack_index - 1):
        code += Op.POP

    code += Op.PUSH1(1) + Op.SSTORE  # New position (was top_value)
    code += Op.STOP

    contract_address = pre.deploy_contract(code=code)

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

    post = {
        contract_address: Account(
            storage={
                0: swap_target_value,  # Top now has the swapped value
                1: top_value,  # Position (stack_index+1) now has original top
            }
        )
    }

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

Parametrized Test Cases

This test generates 8 parametrized test cases across 1 fork.