Skip to content

test_swapn_pc_advances_by_2()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_pc_advancement.py::test_swapn_pc_advances_by_2@9c2813ee.

Generate fixtures for these test cases for Amsterdam with:

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

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

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

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_pc_advancement.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 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
def test_swapn_pc_advances_by_2(
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """
    Verify PC advances by 2 after SWAPN (opcode + immediate byte).

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

    code = Bytecode()

    # Push 18 values so SWAPN[17] will work (needs 18 items on stack)
    for i in range(18):
        code += Op.PUSH1(i)

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

    # Execute SWAPN - should advance PC by 2 (opcode + immediate)
    code += Op.SWAPN[17]

    # Capture PC after SWAPN 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, gas_limit=1_000_000)

    post = {
        contract_address: Account(
            storage={
                0: 6,  # PUSH1(2) + SSTORE(1) + SWAPN(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.