Skip to content

test_reservoir_restored_after_child_spill_and_halt()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_reservoir_restored_after_child_spill_and_halt@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_reservoir_restored_after_child_spill_and_halt --fork Amsterdam

Test parent gets reservoir back after child spill + halt.

The child performs two SSTOREs (zero-to-nonzero), exhausting the reservoir and spilling into gas_left, then hits INVALID causing an exceptional halt. The child's halt resets its frame to (0, R0_child) — only the reservoir-portion is returned to the parent; the spilled gas stays burned (re-classified as execution). The parent does two SSTOREs: the first drains the recovered reservoir, the second spills from the parent's own gas_left. The receipt pins the halted child's whole budget as consumed, so a credit of the burned spill back to the parent is caught.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
@pytest.mark.parametrize(
    "call_opcode",
    [Op.CALL, Op.CALLCODE, Op.DELEGATECALL],
)
@pytest.mark.valid_from("EIP8037")
def test_reservoir_restored_after_child_spill_and_halt(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    call_opcode: Op,
) -> None:
    """
    Test parent gets reservoir back after child spill + halt.

    The child performs two SSTOREs (zero-to-nonzero), exhausting the
    reservoir and spilling into `gas_left`, then hits INVALID causing
    an exceptional halt. The child's halt resets its frame to (0,
    R0_child) — only the reservoir-portion is returned to the
    parent; the spilled gas stays burned (re-classified as execution).
    The parent does two SSTOREs: the first drains the recovered
    reservoir, the second spills from the parent's own `gas_left`.
    The receipt pins the halted child's whole budget as consumed, so
    a credit of the burned spill back to the parent is caught.
    """
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    child_code = Op.SSTORE(0xDEAD, 1) + Op.SSTORE(0xBEAF, 1) + Op.INVALID
    child = pre.deploy_contract(code=child_code)
    # Exactly enough for both SSTOREs: the reservoir funds the first and
    # the second spills, leaving nothing for the INVALID to burn.
    child_gas = child_code.execution_cost(fork) + sstore_state_gas

    probe_code = Op.SSTORE(0, 1)
    probe_gas = probe_code.execution_cost(fork)
    funded_probe = pre.deploy_contract(code=probe_code)
    starved_probe = pre.deploy_contract(code=probe_code)

    parent_storage = Storage()
    funded_slot = parent_storage.store_next(1)
    starved_slot = parent_storage.store_next(0)
    parent_code = (
        Op.POP(call_opcode(gas=child_gas, address=child))
        + Op.SSTORE(
            funded_slot,
            Op.CALL(gas=probe_gas, address=funded_probe),
            # gas accounting
            original_value=1,
            current_value=1,
            new_value=1,
            key_warm=False,
        )
        + Op.SSTORE(
            starved_slot,
            Op.CALL(gas=probe_gas, address=starved_probe),
            # gas accounting
            original_value=1,
            current_value=1,
            new_value=0,
            key_warm=False,
        )
    )
    parent = pre.deploy_contract(
        code=parent_code, storage={funded_slot: 1, starved_slot: 1}
    )

    execution_gas = (
        fork.transaction_intrinsic_cost_calculator()()
        + parent_code.execution_cost(fork)
        + child_gas
        + 2 * probe_gas
    )
    expected_gas_used = max(execution_gas, sstore_state_gas)

    # Recording the starved probe's failure clears its slot, which earns
    # an execution refund the sender-facing receipt is net of.
    gas_used_before_refund = execution_gas + sstore_state_gas
    refund = min(
        gas_used_before_refund // fork.max_refund_quotient(),
        parent_code.refund(fork),
    )

    tx = Transaction(
        to=parent,
        state_gas_reservoir=sstore_state_gas,
        sender=pre.fund_eoa(),
        expected_receipt=TransactionReceipt(
            cumulative_gas_used=gas_used_before_refund - refund,
        ),
    )

    post = {
        parent: Account(storage=parent_storage),
        funded_probe: Account(storage={funded_slot: 1}),
        starved_probe: Account(storage={starved_slot: 0}),
    }
    state_test(
        pre=pre,
        post=post,
        tx=tx,
        blockchain_test_header_verify=Header(gas_used=expected_gas_used),
    )

Parametrized Test Cases

This test generates 3 parametrized test cases across 1 fork.