Skip to content

test_exchange_valid_immediates()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_exchange.py::test_exchange_valid_immediates@b314d18e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8024_dupn_swapn_exchange/test_exchange.py::test_exchange_valid_immediates --fork Amsterdam

Test EXCHANGE with valid immediate values (0-81 and 128-255).

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_exchange.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
@pytest.mark.parametrize(
    "immediate",
    [0, 1, 15, 78, 79, 80, 81, 128, 129, 200, 255],
    ids=lambda x: f"exchange_imm_{x}",
)
def test_exchange_valid_immediates(
    immediate: int,
    pre: Alloc,
    fork: Fork,
    state_test: StateTestFiller,
) -> None:
    """Test EXCHANGE with valid immediate values (0-81 and 128-255)."""
    sender = pre.fund_eoa()

    # Decode the immediate to get the stack indices
    # EXCHANGE with decoded (n, m) swaps position (n+1) with position (m+1)
    n, m = decode_pair(immediate)
    stack_height = m + 1  # Need at least m+1 items
    value_at_n_plus_1 = 0xAAAA
    value_at_m_plus_1 = 0xBBBB

    # Build stack
    code = Bytecode()
    for i in range(stack_height):
        stack_pos = stack_height - i
        if stack_pos == n + 1:
            code += Op.PUSH2(value_at_n_plus_1)
        elif stack_pos == m + 1:
            code += Op.PUSH2(value_at_m_plus_1)
        else:
            code += Op.PUSH2(0x1000 + i)

    # Pass immediate as bytes (raw immediate byte for testing)
    code += Op.EXCHANGE[immediate.to_bytes(1, "big")]

    # Store the swapped values
    for i in range(stack_height):
        code += Op.PUSH1(i) + Op.SSTORE

    code += Op.STOP

    contract_address = pre.deploy_contract(code=code)

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

    # Build expected storage
    expected_storage = {}
    for i in range(stack_height):
        stack_pos = i + 1
        if stack_pos == n + 1:
            expected_storage[i] = value_at_m_plus_1
        elif stack_pos == m + 1:
            expected_storage[i] = value_at_n_plus_1
        else:
            original_i = stack_height - stack_pos
            expected_storage[i] = 0x1000 + original_i

    post = {contract_address: Account(storage=expected_storage)}

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

Parametrized Test Cases

This test generates 11 parametrized test cases across 1 fork.