Skip to content

test_exchange_gas_cost_boundary()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_exchange.py::test_exchange_gas_cost_boundary@2119b382.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8024_dupn_swapn_exchange/test_exchange.py::test_exchange_gas_cost_boundary --fork Amsterdam

Test EXCHANGE at the gas cost boundary.

EXCHANGE 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 EXCHANGE 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_exchange.py
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
@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_exchange_gas_cost_boundary(
    gas_cost_delta: int,
    pre: Alloc,
    fork: Fork,
    state_test: StateTestFiller,
) -> None:
    """
    Test EXCHANGE at the gas cost boundary.

    EXCHANGE 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 EXCHANGE of its base gas (3) and the sub-call
    runs out of gas (result 0); a zero or positive delta succeeds
    (result 1).
    """
    # EXCHANGE with decoded (n, m) swaps position (n+1) with position
    # (m+1); since n < m it needs m + 1 items on the stack. Use the
    # smallest valid pair.
    n = Spec.EXCHANGE_MIN_N  # 1
    m = n + 1

    code = Bytecode()
    for i in range(m + 1):
        code += Op.PUSH1(i)
    code += Op.EXCHANGE[n, m]

    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.