Skip to content

test_create_selfdestruct_sstore_restoration_refund()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py::test_create_selfdestruct_sstore_restoration_refund@5c024cbb.

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

Verify SSTORE restoration still refunds its slot state gas when the surrounding contract SELFDESTRUCTs.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
@pytest.mark.valid_from("EIP8037")
def test_create_selfdestruct_sstore_restoration_refund(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Verify SSTORE restoration still refunds its slot state gas when
    the surrounding contract SELFDESTRUCTs.
    """
    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 = (
        Op.SSTORE.with_metadata(
            key_warm=False,
            original_value=0,
            current_value=0,
            new_value=1,
        )(0, 1)
        + Op.SSTORE.with_metadata(
            key_warm=True,
            original_value=0,
            current_value=1,
            new_value=0,
        )(0, 0)
        + Op.SELFDESTRUCT.with_metadata(address_warm=True)(Op.ADDRESS)
    )
    mstore_value, size = init_code_at_high_bytes(init_code)

    mstore = Op.MSTORE.with_metadata(new_memory_size=32, old_memory_size=0)(
        0, mstore_value
    )
    create_call = Op.CREATE.with_metadata(init_code_size=size)(0, 0, size)
    factory_code = mstore + Op.POP(create_call)
    factory = pre.deploy_contract(code=factory_code)

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

    tx = Transaction(
        to=factory,
        state_gas_reservoir=new_account_state_gas + sstore_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 1 parametrized test case across 1 fork.