Skip to content

test_child_failure_refunds_state_gas_to_reservoir_not_gas_left()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_child_failure_refunds_state_gas_to_reservoir_not_gas_left@2119b382.

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_child_failure_refunds_state_gas_to_reservoir_not_gas_left --fork Amsterdam

Verify state gas from a failing child is restored to the reservoir, so a sibling probe SSTORE can draw from it under a tight regular stipend. Covers SSTORE and CALL-value (new account) state-gas charge paths.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
@pytest.mark.parametrize(
    "call_opcode,charge_via",
    [
        pytest.param(Op.CALL, "sstore", id="call_sstore_charge"),
        pytest.param(
            Op.DELEGATECALL, "sstore", id="delegatecall_sstore_charge"
        ),
        pytest.param(
            Op.CALL,
            "call_value_new_account",
            id="call_call_value_new_account_charge",
        ),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_child_failure_refunds_state_gas_to_reservoir_not_gas_left(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    call_opcode: Op,
    charge_via: str,
) -> None:
    """
    Verify state gas from a failing child is restored to the
    reservoir, so a sibling probe SSTORE can draw from it under a
    tight regular stipend. Covers SSTORE and CALL-value (new
    account) state-gas charge paths.
    """
    gas_costs = fork.gas_costs()
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    probe_storage = Storage()
    probe_code = Op.SSTORE(probe_storage.store_next(1, "probe_ran"), 1)

    if charge_via == "sstore":
        child_code: Bytecode = Op.SSTORE(0, 1) + Op.REVERT(0, 0)
        child_balance = 0
        child_state_charge = sstore_state_gas
    else:
        fresh_target = pre.fund_eoa(amount=0)
        child_code = Op.POP(
            Op.CALL(gas=Op.GAS, address=fresh_target, value=1)
        ) + Op.REVERT(0, 0)
        child_balance = 1
        child_state_charge = gas_costs.NEW_ACCOUNT

    child = pre.deploy_contract(code=child_code, balance=child_balance)
    probe = pre.deploy_contract(probe_code)
    probe_stipend = probe_code.regular_cost(fork)

    parent = pre.deploy_contract(
        code=(
            Op.POP(call_opcode(gas=Op.GAS, address=child))
            + Op.POP(call_opcode(gas=probe_stipend, address=probe))
        ),
    )

    # Reservoir must cover the child's state charge (refunded on
    # REVERT) so the probe SSTORE can draw from it afterwards.
    reservoir = max(child_state_charge, sstore_state_gas)

    tx = Transaction(
        to=parent,
        state_gas_reservoir=reservoir,
        sender=pre.fund_eoa(),
    )

    # DELEGATECALL executes the callee in the caller's storage
    # context, so the probe's SSTORE lands on `parent` instead of
    # `probe`.
    if call_opcode == Op.DELEGATECALL:
        post: dict = {parent: Account(storage=probe_storage)}
    else:
        post = {probe: Account(storage=probe_storage)}

    state_test(
        pre=pre,
        post=post,
        tx=tx,
        blockchain_test_header_verify=Header(gas_used=sstore_state_gas),
    )

Parametrized Test Cases

This test generates 3 parametrized test cases across 1 fork.