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@622620d8.

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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
@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."""
    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(
        Op.ADDRESS, account_new=False, address_warm=True
    )
    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 = factory_code.state_cost(fork) + init_code.state_cost(
        fork
    )
    execution_used = (
        intrinsic_gas
        + factory_code.execution_cost(fork)
        + init_code.execution_cost(fork)
    )

    assert total_state_gas > execution_used, (
        f"test requires state gas > execution gas, got "
        f"state={total_state_gas} execution={execution_used}"
    )
    expected_gas_used = total_state_gas

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

    created = compute_create_address(
        address=factory, nonce=1, opcode=create_opcode
    )

    blockchain_test(
        pre=pre,
        blocks=[
            Block(txs=[tx], header_verify=Header(gas_used=expected_gas_used)),
        ],
        post={created: Account.NONEXISTENT},
    )

Parametrized Test Cases

This test generates 6 parametrized test cases across 1 fork.