Skip to content

test_swapn_gas_cost_boundary()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py::test_swapn_gas_cost_boundary@c74f1a67.

Generate fixtures for these test cases for Amsterdam with:

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

Test SWAPN at the gas cost boundary.

SWAPN is invoked in a callee that receives exactly its execution cost plus gas_cost_delta. The caller records the CALL result: a negative delta starves SWAPN of its base gas (3) and the sub-call runs out of gas (result 0); a zero or positive delta succeeds (result 1).

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
@EIPChecklist.Opcode.Test.GasUsage.Normal()
@EIPChecklist.Opcode.Test.GasUsage.OutOfGasExecution()
@EIPChecklist.Opcode.Test.GasUsage.ExtraGas()
@pytest.mark.parametrize("gas_cost_delta", [-2, -1, 0, 1, 2])
def test_swapn_gas_cost_boundary(
    gas_cost_delta: int,
    pre: Alloc,
    fork: Fork,
    state_test: StateTestFiller,
) -> None:
    """
    Test SWAPN at the gas cost boundary.

    SWAPN is invoked in a callee that receives exactly its execution cost
    plus `gas_cost_delta`. The caller records the CALL result: a negative
    delta starves SWAPN of its base gas (3) and the sub-call runs out of
    gas (result 0); a zero or positive delta succeeds (result 1).
    """
    # SWAPN with decoded value n swaps position 1 with position (n+1), so
    # it needs stack_index + 1 items on the stack.
    stack_index = Spec.MIN_STACK_INDEX  # 17

    code = Bytecode()
    for i in range(stack_index + 1):
        code += Op.PUSH1(i)
    code += Op.SWAPN[stack_index]

    contract_address = pre.deploy_contract(code=code)

    call_code = Op.SSTORE(
        0,
        Op.CALL(
            gas=code.gas_cost(fork) + gas_cost_delta,
            address=contract_address,
        ),
    )
    call_address = pre.deploy_contract(
        code=call_code,
        storage={0: 0xDEADBEEF},
    )

    tx = Transaction(to=call_address, sender=pre.fund_eoa())

    post = {call_address: Account(storage={0: 0 if gas_cost_delta < 0 else 1})}

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

Parametrized Test Cases

This test generates 5 parametrized test cases across 1 fork.