Skip to content

test_sstore_restoration_cross_frame()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py::test_sstore_restoration_cross_frame@c74f1a67.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py::test_sstore_restoration_cross_frame --fork Amsterdam

Verify restoration refund across frames for CALL / CALLCODE / DELEGATECALL.

Callee performs the full 0 to x to 0 cycle within its call. For CALL the slot lives in callee's storage; for CALLCODE/DELEGATECALL it lives in caller's. The reservoir is tx-level, so the refund applies regardless of storage ownership. Net block state gas is zero.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
@pytest.mark.with_all_call_opcodes(
    selector=lambda call_opcode: call_opcode != Op.STATICCALL
)
@pytest.mark.valid_from("EIP8037")
def test_sstore_restoration_cross_frame(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    call_opcode: Op,
) -> None:
    """
    Verify restoration refund across frames for CALL / CALLCODE / DELEGATECALL.

    Callee performs the full 0 to x to 0 cycle within its call. For
    CALL the slot lives in callee's storage; for CALLCODE/DELEGATECALL
    it lives in caller's.  The reservoir is tx-level, so the refund
    applies regardless of storage ownership.  Net block state gas is
    zero.
    """
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)
    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()()

    child_code = (
        Op.SSTORE(0, 1)
        + Op.SSTORE.with_metadata(
            key_warm=True,
            original_value=0,
            current_value=1,
            new_value=0,
        )(0, 0)
        + Op.STOP
    )
    # Callee's regular gas excludes the state gas (refunded at x to 0).
    child_regular = child_code.gas_cost(fork) - sstore_state_gas
    child = pre.deploy_contract(code=child_code)

    parent_code = Op.POP(call_opcode(gas=child_regular, address=child))
    parent = pre.deploy_contract(code=parent_code)

    tx_regular = intrinsic_gas + parent_code.gas_cost(fork) + child_regular

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

    # CALL targets callee's storage; CALLCODE/DELEGATECALL target caller's.
    slot_owner = child if call_opcode == Op.CALL else parent
    blockchain_test(
        pre=pre,
        blocks=[Block(txs=[tx], header_verify=Header(gas_used=tx_regular))],
        post={slot_owner: Account(storage={0: 0})},
    )

Parametrized Test Cases

This test generates 3 parametrized test cases across 1 fork.