Skip to content

test_call_pre_charged_costs_excluded_from_forwarding()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_call_pre_charged_costs_excluded_from_forwarding@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_call_pre_charged_costs_excluded_from_forwarding --fork Amsterdam

Verify pre-charged CALL costs do not reduce the 63/64 forwarding budget.

CALL charges access gas and memory expansion up front, before computing the 63/64 sub-call gas. Those costs must not be subtracted again during the forwarding calculation.

A wrapper contract receives a precise gas budget and calls a child with maximum gas and a large ret_size (triggering memory expansion). The child does a cold zero-to-nonzero SSTORE as proof of execution. The gas budget is tight enough that any double-counting of the pre-charged costs (access gas, memory expansion, or both) causes the child to OOG and the SSTORE to revert.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
@pytest.mark.valid_from("EIP8037")
def test_call_pre_charged_costs_excluded_from_forwarding(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Verify pre-charged CALL costs do not reduce the 63/64 forwarding budget.

    CALL charges access gas and memory expansion up front, before
    computing the 63/64 sub-call gas.  Those costs must not be
    subtracted again during the forwarding calculation.

    A wrapper contract receives a precise gas budget and calls a child
    with maximum gas and a large ret_size (triggering memory expansion).
    The child does a cold zero-to-nonzero SSTORE as proof of execution.
    The gas budget is tight enough that any double-counting of the
    pre-charged costs (access gas, memory expansion, or both) causes
    the child to OOG and the SSTORE to revert.
    """
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    # Child: SSTORE(0, 1) as proof of execution
    child_storage = Storage()
    child_code = Op.SSTORE(child_storage.store_next(1, "child_ran"), 1)
    child = pre.deploy_contract(child_code)

    child_execution_gas = child_code.execution_cost(fork)

    # Memory expansion triggered by ret_size on the wrapper's CALL
    ret_size = 512 * 32  # 512 words
    memory_cost = fork.memory_expansion_gas_calculator()(new_bytes=ret_size)

    # Wrapper: CALL child requesting max gas with memory expansion. The
    # memory metadata makes `wrapper_code.execution_cost(fork)` fold the
    # cold access, the 7 argument pushes and the memory expansion.
    wrapper_code = Op.CALL(
        gas=0xFFFFFFFF,
        address=child,
        value=0,
        args_offset=0,
        args_size=0,
        ret_offset=0,
        ret_size=ret_size,
        new_memory_size=ret_size,
    )
    wrapper = pre.deploy_contract(wrapper_code)

    # After the up-front pre-charge, the wrapper has gas_remaining left.
    # The 63/64 rule should forward gas_remaining * 63/64 to the child —
    # just enough for its SSTORE.
    gas_remaining = child_execution_gas * 64 // 63 + memory_cost // 2

    wrapper_gas = wrapper_code.execution_cost(fork) + gas_remaining

    caller = pre.deploy_contract(
        Op.POP(Op.CALL(gas=wrapper_gas, address=wrapper))
    )

    sender = pre.fund_eoa()
    tx = Transaction(
        sender=sender,
        to=caller,
        state_gas_reservoir=sstore_state_gas,
    )

    post = {
        child: Account(storage=child_storage),
    }

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

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.