Skip to content

test_create_mixed_success_and_failure_block_accounting()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_create_mixed_success_and_failure_block_accounting@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_create_mixed_success_and_failure_block_accounting --fork Amsterdam

Verify block state gas excludes refunded charges from failed CREATE.

One successful CREATE plus one failed CREATE (REVERT): block state gas reflects only the successful charges.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
@pytest.mark.with_all_create_opcodes()
@pytest.mark.valid_from("EIP8037")
def test_create_mixed_success_and_failure_block_accounting(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
) -> None:
    """
    Verify block state gas excludes refunded charges from failed CREATE.

    One successful CREATE plus one failed CREATE (REVERT): block
    state gas reflects only the successful charges.
    """
    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()()
    create_account_state_gas = fork.create_state_gas(code_size=0)

    success_value, success_size = init_code_at_high_bytes(Op.STOP)
    fail_value, fail_size = init_code_at_high_bytes(Op.REVERT(0, 0))

    def call(size: int, salt: int) -> Bytecode:
        if create_opcode == Op.CREATE2:
            return create_opcode(value=0, offset=0, size=size, salt=salt)
        return create_opcode(value=0, offset=0, size=size)

    factory_code = (
        Op.MSTORE(0, success_value)
        + Op.POP(call(size=success_size, salt=0))
        + Op.MSTORE(0, fail_value)
        + Op.POP(call(size=fail_size, salt=1))
    )
    factory = pre.deploy_contract(code=factory_code)

    # STOP deploys empty code, so only GAS_NEW_ACCOUNT counts for
    # the successful CREATE, and the failed CREATE is refunded.
    block_state = create_account_state_gas
    tx_regular = (
        intrinsic_gas
        + factory_code.gas_cost(fork)
        - 2 * create_account_state_gas
    )
    expected = max(tx_regular, block_state)

    tx = Transaction(
        to=factory,
        state_gas_reservoir=2 * create_account_state_gas,
        sender=pre.fund_eoa(),
    )

    blockchain_test(
        pre=pre,
        blocks=[Block(txs=[tx], header_verify=Header(gas_used=expected))],
        post={},
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.