Skip to content

test_inner_create_fail_refunds_in_creation_tx()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_inner_create_fail_refunds_in_creation_tx@ffe48572.

Generate fixtures for these test cases for Bogota with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_inner_create_fail_refunds_in_creation_tx --fork Bogota

Verify failed inner CREATEs inside a creation tx refund state gas so only the outer intrinsic state gas remains.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
@pytest.mark.parametrize(
    "num_inner_ops",
    [
        pytest.param(1, id="single"),
        pytest.param(3, id="accumulate"),
    ],
)
@pytest.mark.parametrize(
    "outer_outcome",
    [
        pytest.param("succeeds", id="outer_succeeds"),
        pytest.param("reverts", id="outer_reverts"),
    ],
)
@pytest.mark.with_all_create_opcodes()
@pytest.mark.valid_from("EIP8037")
def test_inner_create_fail_refunds_in_creation_tx(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
    outer_outcome: str,
    num_inner_ops: int,
) -> None:
    """
    Verify failed inner CREATEs inside a creation tx refund state
    gas so only the outer intrinsic state gas remains.
    """
    outer_state_gas = fork.create_state_gas(code_size=0)

    inner_initcode = bytes(Op.REVERT(0, 0))

    setup = Op.MSTORE(
        0,
        int.from_bytes(inner_initcode, "big")
        << (256 - 8 * len(inner_initcode)),
    )

    inner_ops = Bytecode()
    for i in range(num_inner_ops):
        inner_ops += Op.POP(
            create_op(create_opcode, size=len(inner_initcode), salt=i)
        )

    if outer_outcome == "succeeds":
        termination = Op.RETURN(0, 0)
    else:
        termination = Op.REVERT(0, 0)

    initcode = setup + inner_ops + termination

    sender = pre.fund_eoa()
    intrinsic_calc = fork.transaction_intrinsic_cost_calculator()
    intrinsic_total = intrinsic_calc(
        calldata=bytes(initcode), contract_creation=True
    )

    initcode_gas = initcode.gas_cost(fork)
    per_inner_slack = 2_000
    new_account = create_opcode(account_new=True).state_cost(fork)
    gas_limit = (
        intrinsic_total
        + initcode_gas
        + num_inner_ops * (new_account + per_inner_slack)
    )

    create_address = compute_create_address(address=sender, nonce=0)

    tx = Transaction(
        sender=sender,
        to=None,
        data=initcode,
        gas_limit=gas_limit,
    )

    if outer_outcome == "succeeds":
        post: dict = {create_address: Account(code=b"")}
        block = Block(
            txs=[tx],
            header_verify=Header(gas_used=outer_state_gas),
        )
    else:
        post = {create_address: Account.NONEXISTENT}
        block = Block(txs=[tx])

    blockchain_test(pre=pre, blocks=[block], post=post)

Parametrized Test Cases

This test generates 8 parametrized test cases across 2 forks.