Skip to content

test_swapn_stack_underflow()

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

Generate fixtures for these test cases for Amsterdam with:

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

Test SWAPN causes transaction failure on stack underflow.

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
def test_swapn_stack_underflow(
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """Test SWAPN causes transaction failure on stack underflow."""
    sender = pre.fund_eoa()

    # SWAPN with immediate 0x80 (n=17) swaps position 1 with position 18
    # Need 18 items, so push only 17 to trigger underflow
    code = Bytecode()
    for i in range(17):
        code += Op.PUSH1(i)
    # Pass immediate as bytes (raw immediate byte for testing)
    # decode_single(0x80) = 17, needs 18 items but only 17
    code += Op.SWAPN[b"\x80"]
    code += Op.STOP

    contract_address = pre.deploy_contract(code=code)

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

    # Transaction should fail, contract storage unchanged
    post = {contract_address: Account(storage={})}

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

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.