Skip to content

test_create_selfdestruct_no_refund_account_and_storage()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py::test_create_selfdestruct_no_refund_account_and_storage@87aba1a3.

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

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

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
@pytest.mark.parametrize(
    "num_slots",
    [
        pytest.param(0, id="no_storage"),
        pytest.param(1, id="one_slot"),
        pytest.param(5, id="five_slots"),
    ],
)
@pytest.mark.with_all_create_opcodes()
@pytest.mark.valid_from("EIP8037")
def test_create_selfdestruct_no_refund_account_and_storage(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
    num_slots: int,
) -> None:
    """Verify same tx CREATE+SELFDESTRUCT does not refund state gas."""
    new_account_state_gas = fork.gas_costs().NEW_ACCOUNT
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)
    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()()

    init_code = Bytecode()
    for i in range(num_slots):
        init_code += Op.SSTORE.with_metadata(
            key_warm=False,
            original_value=0,
            current_value=0,
            new_value=1,
        )(i, 1)
    init_code += Op.SELFDESTRUCT.with_metadata(address_warm=True)(Op.ADDRESS)
    mstore_value, size = init_code_at_high_bytes(init_code)

    # Metadata so `.gas_cost(fork)` matches runtime charges.
    mstore = Op.MSTORE.with_metadata(new_memory_size=32, old_memory_size=0)(
        0, mstore_value
    )
    create_metadata = create_opcode.with_metadata(init_code_size=size)
    create_call = (
        create_metadata(value=0, offset=0, size=size, salt=0)
        if create_opcode == Op.CREATE2
        else create_metadata(value=0, offset=0, size=size)
    )
    factory_code = mstore + Op.POP(create_call)
    factory = pre.deploy_contract(code=factory_code)

    total_state_gas = new_account_state_gas + num_slots * sstore_state_gas
    regular_used = (
        intrinsic_gas
        + factory_code.gas_cost(fork)
        + init_code.gas_cost(fork)
        - total_state_gas
    )
    expected_gas_used = max(regular_used, total_state_gas)

    tx = Transaction(
        to=factory,
        state_gas_reservoir=total_state_gas,
        sender=pre.fund_eoa(),
    )

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

Parametrized Test Cases

This test generates 6 parametrized test cases across 1 fork.