Skip to content

test_create_selfdestruct_no_refund_code_deposit_state_gas()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py::test_create_selfdestruct_no_refund_code_deposit_state_gas@c74f1a67.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py::test_create_selfdestruct_no_refund_code_deposit_state_gas --fork Amsterdam

Verify same tx CREATE+SELFDESTRUCT does not refund code deposit state gas.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
@pytest.mark.parametrize(
    "beneficiary_type,code_size",
    [
        pytest.param("self", 2, id="self_tiny"),
        pytest.param("self", 100, id="self_medium"),
        pytest.param("external", 100, id="external_medium"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_create_selfdestruct_no_refund_code_deposit_state_gas(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    code_size: int,
    beneficiary_type: str,
) -> None:
    """
    Verify same tx CREATE+SELFDESTRUCT does not refund code deposit
    state gas.
    """
    assert code_size >= 2
    new_account_state_gas = fork.gas_costs().NEW_ACCOUNT
    code_deposit_state_gas = fork.code_deposit_state_gas(code_size=code_size)

    if beneficiary_type == "self":
        selfdestruct = Op.SELFDESTRUCT(Op.ADDRESS)
    else:
        beneficiary = pre.deploy_contract(code=Op.STOP)
        selfdestruct = Op.SELFDESTRUCT(beneficiary)
    sd_len = len(bytes(selfdestruct))
    assert code_size >= sd_len
    deployed = bytes(selfdestruct) + b"\x00" * (code_size - sd_len)
    initcode = Initcode(deploy_code=deployed)
    initcode_len = len(initcode)

    # Nest CREATE directly as the address argument to CALL so the
    # deployed contract's address flows via the stack, avoiding a
    # magic memory slot for address storage and an arbitrary gas
    # budget.
    factory_code = Op.CALLDATACOPY(
        0,
        0,
        Op.CALLDATASIZE,
        data_size=initcode_len,
        new_memory_size=initcode_len,
    ) + Op.POP(
        Op.CALL(
            gas=Op.GAS,
            address=Op.CREATE(
                value=0,
                offset=0,
                size=Op.CALLDATASIZE,
                init_code_size=initcode_len,
            ),
        )
    )
    factory = pre.deploy_contract(code=factory_code)
    created_address = compute_create_address(address=factory, nonce=1)

    total_state_gas = new_account_state_gas + code_deposit_state_gas
    tx = Transaction(
        to=factory,
        data=bytes(initcode),
        state_gas_reservoir=total_state_gas,
        sender=pre.fund_eoa(),
    )

    blockchain_test(
        pre=pre,
        blocks=[Block(txs=[tx])],
        post={created_address: Account.NONEXISTENT},
    )

Parametrized Test Cases

This test generates 3 parametrized test cases across 1 fork.