Skip to content

test_exchange_pc_advances_by_2()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_pc_advancement.py::test_exchange_pc_advances_by_2@2119b382.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8024_dupn_swapn_exchange/test_pc_advancement.py::test_exchange_pc_advances_by_2 --fork Amsterdam

Verify PC advances by 2 after EXCHANGE (opcode + immediate byte).

Tests that EXCHANGE consumes the immediate byte and advances PC correctly.

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_pc_advancement.py
143
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
190
191
192
193
194
195
196
def test_exchange_pc_advances_by_2(
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """
    Verify PC advances by 2 after EXCHANGE (opcode + immediate byte).

    Tests that EXCHANGE consumes the immediate byte and advances PC correctly.
    """
    sender = pre.fund_eoa()

    code = Bytecode()

    # Push 6 values so EXCHANGE[1, 5] will work (needs 6 items on stack)
    for i in range(6):
        code += Op.PUSH1(i)

    # Capture PC before EXCHANGE and store it
    code += Op.PC
    code += Op.PUSH1(1) + Op.SSTORE

    # Execute EXCHANGE - should advance PC by 2 (opcode + immediate)
    code += Op.EXCHANGE[1, 5]

    # Capture PC after EXCHANGE and store it
    code += Op.PC
    code += Op.PUSH1(2) + Op.SSTORE

    # Calculate difference
    code += Op.PUSH1(1) + Op.SLOAD  # Load PC_before
    code += Op.PUSH1(2) + Op.SLOAD  # Load PC_after
    code += Op.SUB  # PC_after - PC_before

    # Store the difference
    code += Op.PUSH1(0) + Op.SSTORE

    # Clean up intermediate storage
    code += Op.PUSH1(0) + Op.PUSH1(1) + Op.SSTORE  # Clear key 1
    code += Op.PUSH1(0) + Op.PUSH1(2) + Op.SSTORE  # Clear key 2

    code += Op.STOP

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

    post = {
        contract_address: Account(
            storage={
                0: 6,  # PUSH1(2) + SSTORE(1) + EXCHANGE(2) + PC(1) = 6
            }
        )
    }

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

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.