Skip to content

test_nested_create_code_deposit_cannot_borrow_parent_gas()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_nested_create_code_deposit_cannot_borrow_parent_gas@87aba1a3.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_nested_create_code_deposit_cannot_borrow_parent_gas --fork Amsterdam

Test nested CREATE code deposit does not borrow parent gas.

Provide just enough gas for CREATE to start (new account state gas + regular gas) but not enough for the child frame to cover code deposit after init code runs. The CREATE increments the factory nonce but code deposit fails, so no contract is deployed.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
@pytest.mark.valid_from("EIP8037")
def test_nested_create_code_deposit_cannot_borrow_parent_gas(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Test nested CREATE code deposit does not borrow parent gas.

    Provide just enough gas for CREATE to start (new account state
    gas + regular gas) but not enough for the child frame to cover
    code deposit after init code runs. The CREATE increments the
    factory nonce but code deposit fails, so no contract is deployed.
    """
    init_code = Op.RETURN(0, 1)
    gas_costs = fork.gas_costs()
    code_deposit_state = fork.code_deposit_state_gas(code_size=1)

    factory_mstore = Op.MSTORE(
        0, Op.PUSH32(bytes(init_code)), new_memory_size=32
    )
    factory_create = Op.CREATE(
        value=0,
        offset=32 - len(init_code),
        size=len(init_code),
        init_code_size=len(init_code),
    )
    factory = pre.deploy_contract(
        code=factory_mstore + Op.POP(factory_create),
    )
    created = compute_create_address(address=factory, nonce=1)

    # Init code child execution: PUSH1 + PUSH1 + RETURN's mem_exp.
    # Code deposit (keccak + state) is charged AFTER the child returns.
    init_cost = 2 * gas_costs.VERY_LOW + gas_costs.MEMORY_PER_WORD
    # Target child: enough for init, not enough for code deposit state.
    target_child = (init_cost + code_deposit_state) // 2
    # Invert EIP-150 63/64ths rule: ceil(target_child * 64 / 63).
    factory_remaining = (target_child * 64 + 62) // 63

    # NEW_ACCOUNT state gas spills into gas_left (no reservoir at the
    # top level), so it must be funded out of the regular budget.
    intrinsic_cost = fork.transaction_intrinsic_cost_calculator()()
    gas_limit = (
        intrinsic_cost
        + factory_mstore.regular_cost(fork)
        + factory_create.regular_cost(fork)
        + gas_costs.NEW_ACCOUNT
        + factory_remaining
    )

    tx = Transaction(
        to=factory,
        gas_limit=gas_limit,
        sender=pre.fund_eoa(),
    )

    post = {
        factory: Account(nonce=2),
        created: Account.NONEXISTENT,
    }
    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.