Skip to content

test_cross_frame_credit_returns_at_settlement()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_cross_frame_refund.py::test_cross_frame_credit_returns_at_settlement@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_cross_frame_refund.py::test_cross_frame_credit_returns_at_settlement --fork Amsterdam

Test the cross-frame credit refunds the sender at settlement.

The frame's spilled set is cleared by a child and the merge repays the credit. Settlement sums gas_left and the reservoir, so the spilled charge and the credit cancel wherever the credit sits and the receipt carries no state term at all.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_cross_frame_refund.py
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
@pytest.mark.valid_from("EIP8037")
def test_cross_frame_credit_returns_at_settlement(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Test the cross-frame credit refunds the sender at settlement.

    The frame's spilled set is cleared by a child and the merge repays
    the credit. Settlement sums `gas_left` and the reservoir, so the
    spilled charge and the credit cancel wherever the credit sits and
    the receipt carries no state term at all.
    """
    intrinsic_cost = fork.transaction_intrinsic_cost_calculator()()
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    clearer_code = WARM_CLEAR(SLOT_X, 0)
    clearer = pre.deploy_contract(code=clearer_code)

    # A budget covering the child's SSTORE stipend sentry through the
    # clear, so the child succeeds and returns the sentry unspent.
    child_budget = budget_above_sstore_stipend(fork, clearer_code)
    code = Op.SSTORE(
        SLOT_X,
        1,
        key_warm=False,
        original_value=0,
        current_value=0,
        new_value=1,
    ) + Op.POP(
        Op.DELEGATECALL(gas=child_budget, address=clearer, address_warm=False)
    )
    contract = pre.deploy_contract(code=code)

    before_refund = (
        intrinsic_cost
        + code.execution_cost(fork)
        + clearer_code.execution_cost(fork)
    )
    # Clearing the slot back to its original value also refunds the
    # write cost through the classic refund counter at settlement.
    restore_refund = clearer_code.refund(fork) - sstore_state_gas
    expected_gas_used = before_refund - min(
        before_refund // fork.max_refund_quotient(), restore_refund
    )
    # The post-refund usage must clear the calldata floor, or the
    # floor masks a lost or doubled credit.
    assert expected_gas_used > fork.transaction_data_floor_cost_calculator()(
        data=b""
    )

    tx = Transaction(
        to=contract,
        state_gas_reservoir=0,
        sender=pre.fund_eoa(),
        expected_receipt=TransactionReceipt(
            cumulative_gas_used=expected_gas_used
        ),
    )

    post = {contract: Account(storage={SLOT_X: 0})}
    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.