Skip to content

test_repaid_credit_funds_execution()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_cross_frame_refund.py::test_repaid_credit_funds_execution@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_repaid_credit_funds_execution --fork Amsterdam

Test the repaid credit funds execution work.

The gas limit covers the transaction only up to the clearing child's merge plus a sliver. An execution tail worth more than the sliver but no more than the repaid credit follows and completes: the merge repaid the spill into gas_left, where the tail spends it, and the receipt bills the tail against the repayment.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_cross_frame_refund.py
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
@pytest.mark.valid_from("EIP8037")
def test_repaid_credit_funds_execution(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Test the repaid credit funds execution work.

    The gas limit covers the transaction only up to the clearing
    child's merge plus a sliver. An execution tail worth more than the
    sliver but no more than the repaid credit follows and completes:
    the merge repaid the spill into `gas_left`, where the tail spends
    it, and the receipt bills the tail against the repayment.
    """
    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)

    head = (
        FRESH_SET(SLOT_MARKER, 1)
        + FRESH_SET(SLOT_X, 1)
        + Op.POP(
            Op.DELEGATECALL(gas=Op.GAS, address=clearer, address_warm=False)
        )
    )
    # TODO: The tail spends a set amount of execution gas; a JUMPDEST
    # run is the most future-proof inline way until a fork util exists.
    tail_ops = min(
        sstore_state_gas // Op.JUMPDEST.gas_cost(fork),
        fork.max_code_size() - len(head),
    )
    tail = Op.JUMPDEST * tail_ops
    code = head + tail
    contract = pre.deploy_contract(code=code)

    # A sliver covering the child's SSTORE stipend sentry through the
    # one-in-64 withholding. It survives the merge unspent.
    sliver = budget_above_sstore_stipend(fork, clearer_code) * 64 // 63 + 1
    tail_cost = tail.gas_cost(fork)
    # The tail must overrun the sliver yet fit inside the repaid
    # credit, or the completion stops demonstrating the repayment buys
    # execution.
    assert sliver < tail_cost <= sstore_state_gas

    gas_limit = (
        intrinsic_cost
        + head.gas_cost(fork)
        + clearer_code.gas_cost(fork)
        + sliver
    )

    # The sliver and the repayment fund the tail and the rest of the
    # credit returns at settlement. A repayment that fails to debit the
    # reservoir refunds the sender twice and lowers this value.
    before_refund = gas_limit - (sliver + sstore_state_gas - tail_cost)
    # 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
    )

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

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

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.