Skip to content

test_swapn_preserves_other_stack_items()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py::test_swapn_preserves_other_stack_items@9c2813ee.

Generate fixtures for these test cases for Amsterdam with:

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

Test SWAPN only swaps the specified items, leaving others unchanged.

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
def test_swapn_preserves_other_stack_items(
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """Test SWAPN only swaps the specified items, leaving others unchanged."""
    sender = pre.fund_eoa()

    # Use stack index 17 (smallest valid)
    # SWAPN with n=17 swaps position 1 with position 18, so need 18 items
    stack_index = 17
    stack_height = stack_index + 1  # Need 18 items

    # Create a stack with 18 distinct values
    code = Bytecode()
    for i in range(stack_height):
        code += Op.PUSH2(0x1000 + i)

    # SWAPN swaps top (position 1) with position 18
    # Pass stack index directly - encoder will handle encoding
    code += Op.SWAPN[stack_index]

    # Store all values to verify only the swapped ones changed
    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, gas_limit=1_000_000)

    # After swap: position 1 and position 18 are swapped
    # Original stack (top to bottom): 0x1011, 0x1010, ..., 0x1001, 0x1000
    # After SWAPN[0]: 0x1000, 0x1010, ..., 0x1001, 0x1011
    expected_storage = {}
    for i in range(stack_height):
        if i == 0:
            expected_storage[i] = 0x1000  # Was at bottom, now at top
        elif i == stack_height - 1:
            expected_storage[i] = 0x1011  # Was at top, now at bottom
        else:
            expected_storage[i] = 0x1000 + (stack_height - 1 - i)

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

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

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.