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@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_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
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
@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_execution = intrinsic_gas + factory_code.execution_cost(fork)
    assert block_state > tx_execution, "state gas must dominate"
    expected = max(tx_execution, 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.