Skip to content

test_nested_create_failure_refunds_state_gas_before_parent_exit()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_nested_create_failure_refunds_state_gas_before_parent_exit@8acae1b0.

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

Verify failed inner CREATE state gas is refunded before its parent exits.

The child fails by REVERT or exceptional halt, and the factory then either returns or reverts. In every case the failed creation leaves no state gas charged, so the exact receipt and header contain execution gas only. The factory nonce additionally proves that a parent revert rolls back the failed CREATE's nonce bump while a successful parent preserves it.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
@pytest.mark.parametrize(
    "parent_reverts",
    [
        pytest.param(True, id="parent_reverts"),
        pytest.param(False, id="parent_succeeds"),
    ],
)
@pytest.mark.parametrize(
    "child_failure",
    [
        pytest.param("revert", id="child_revert"),
        pytest.param("halt", id="child_halt"),
    ],
)
@pytest.mark.with_all_create_opcodes()
@pytest.mark.valid_from("EIP8037")
def test_nested_create_failure_refunds_state_gas_before_parent_exit(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    parent_reverts: bool,
    child_failure: str,
    create_opcode: Op,
) -> None:
    """
    Verify failed inner CREATE state gas is refunded before its parent exits.

    The child fails by REVERT or exceptional halt, and the factory then either
    returns or reverts. In every case the failed creation leaves no state gas
    charged, so the exact receipt and header contain execution gas only. The
    factory nonce additionally proves that a parent revert rolls back the
    failed CREATE's nonce bump while a successful parent preserves it.
    """
    if child_failure == "revert":
        init_code = Op.REVERT(0, 0)
    else:
        init_code = Op.INVALID

    setup = Op.MSTORE(
        0,
        int.from_bytes(bytes(init_code), "big") << (256 - 8 * len(init_code)),
        new_memory_size=32,
    )

    create_call = (
        create_opcode(
            value=0,
            offset=0,
            size=len(init_code),
            salt=0,
            # gas accounting
            init_code_size=len(init_code),
        )
        if create_opcode == Op.CREATE2
        else create_opcode(
            value=0,
            offset=0,
            size=len(init_code),
            # gas accounting
            init_code_size=len(init_code),
        )
    )
    create_state_gas = create_call.state_cost(fork)

    factory_before_child = setup + create_call
    factory_after_child = Op.REVERT(0, 0) if parent_reverts else Op.STOP
    factory_code = factory_before_child + factory_after_child
    factory = pre.deploy_contract(code=factory_code)

    # Nested CALL required so the child-error path has a parent
    # frame to receive the restored state gas.
    factory_gas = 500_000
    caller_code = Op.POP(Op.CALL(gas=factory_gas, address=factory))
    caller = pre.deploy_contract(code=caller_code)

    intrinsic_execution = fork.transaction_intrinsic_cost_calculator()()
    if child_failure == "revert":
        expected_execution = (
            intrinsic_execution
            + caller_code.execution_cost(fork)
            + factory_code.execution_cost(fork)
            + init_code.execution_cost(fork)
        )
    else:
        gas_before_child = factory_gas - factory_before_child.execution_cost(
            fork
        )
        child_gas = gas_before_child - gas_before_child // 64
        expected_execution = (
            intrinsic_execution
            + caller_code.execution_cost(fork)
            + factory_before_child.execution_cost(fork)
            + child_gas
            + factory_after_child.execution_cost(fork)
        )

    tx = Transaction(
        to=caller,
        state_gas_reservoir=create_state_gas,
        sender=pre.fund_eoa(),
        expected_receipt=TransactionReceipt(
            status=1,
            cumulative_gas_used=expected_execution,
        ),
    )

    inner_address = compute_create_address(
        address=factory,
        nonce=1,
        salt=0,
        initcode=bytes(init_code),
        opcode=create_opcode,
    )

    if parent_reverts:
        post = {
            factory: Account(nonce=1),
            inner_address: Account.NONEXISTENT,
        }
    else:
        post = {
            factory: Account(nonce=2),
            inner_address: Account.NONEXISTENT,
        }

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

Parametrized Test Cases

This test generates 8 parametrized test cases across 1 fork.