Skip to content

test_code_deposit_halt_discards_initcode_state_gas()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_code_deposit_halt_discards_initcode_state_gas@622620d8.

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_code_deposit_halt_discards_initcode_state_gas --fork Amsterdam

Verify deposit halt discards all state gas charged by initcode.

A CREATE tx runs initcode that first performs a state-creating operation (charging GAS_NEW_ACCOUNT state gas), then returns code that triggers a deposit failure (oversized, OOG, or an EIP-3541 0xEF prefix). The exceptional halt reverts all initcode state changes including the new account. The reverted GAS_NEW_ACCOUNT must not count in block state gas. The deposit halt burns the full execution grant, so the exact receipt and header both equal the transaction gas-limit cap. Any surviving state charge would split that fixed total across the two dimensions and lower the header below the cap.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
@pytest.mark.parametrize(
    "state_opcode",
    [
        pytest.param(Op.CALL, id="call_new_account"),
        pytest.param(Op.CREATE, id="inner_create"),
        pytest.param(Op.SSTORE, id="initcode_sstore"),
    ],
)
@pytest.mark.parametrize(
    "deposit_fail_mode",
    [
        pytest.param("oversized_code", id="oversized_code"),
        pytest.param("oog_deposit", id="oog_deposit"),
        pytest.param("ef_prefix", id="ef_prefix"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_code_deposit_halt_discards_initcode_state_gas(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    state_opcode: Op,
    deposit_fail_mode: str,
) -> None:
    """
    Verify deposit halt discards all state gas charged by initcode.

    A CREATE tx runs initcode that first performs a state-creating
    operation (charging GAS_NEW_ACCOUNT state gas), then returns
    code that triggers a deposit failure (oversized, OOG, or an
    EIP-3541 0xEF prefix). The exceptional halt reverts all initcode
    state changes including the new account. The reverted
    GAS_NEW_ACCOUNT must not count in block state gas. The deposit halt burns
    the full execution grant, so the exact receipt and header both equal the
    transaction gas-limit cap. Any surviving state charge would split that
    fixed total across the two dimensions and lower the header below the cap.
    """
    subcall_forwarded_value = 1
    entry_account_value = 1
    if state_opcode == Op.CALL:
        state_op = Op.POP(
            Op.CALL(
                address=pre.nonexistent_account(),
                value=subcall_forwarded_value,
                # gas accounting
                value_transfer=True,
                account_new=True,
            )
        )
    elif state_opcode == Op.CREATE:
        state_op = Op.POP(Op.CREATE(value=0, offset=0, size=1))
    else:
        state_op = Op.SSTORE(0, 1)
        subcall_forwarded_value = 0

    assert state_op.state_cost(fork) > 0, (
        "initcode must perform a non-zero state-gas operation"
    )

    if deposit_fail_mode == "oversized_code":
        deposit_fail = Op.RETURN(0, fork.max_code_size() + 1)
    elif deposit_fail_mode == "oog_deposit":
        # Return code at max size: passes the size check but code
        # deposit state gas (max_code_size * cost_per_state_byte)
        # exceeds available state gas in the child frame, causing OOG.
        deposit_fail = Op.RETURN(0, fork.max_code_size())
    else:
        # Return single 0xEF byte: EIP-3541 rejects the code before
        # the size check or any deposit charging, halting the deposit.
        deposit_fail = Op.MSTORE8(0, 0xEF) + Op.RETURN(0, 1)

    initcode = state_op + deposit_fail
    gas_limit_cap = fork.transaction_gas_limit_cap()
    assert gas_limit_cap is not None

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[
                    Transaction(
                        to=None,
                        data=initcode,
                        value=entry_account_value + subcall_forwarded_value,
                        state_gas_reservoir=0,
                        sender=pre.fund_eoa(),
                        expected_receipt=TransactionReceipt(
                            status=0,
                            cumulative_gas_used=gas_limit_cap,
                        ),
                    ),
                ],
                header_verify=Header(gas_used=gas_limit_cap),
            ),
        ],
        post={},
    )

Parametrized Test Cases

This test generates 9 parametrized test cases across 1 fork.