Skip to content

test_sstore_dirty_slot_refund()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py::test_sstore_dirty_slot_refund@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py::test_sstore_dirty_slot_refund --fork Amsterdam

Test the refund for a second write to a slot already changed in the same transaction.

The write cost was paid by the first change, so the second write is charged the warm access alone. Clearing a dirty slot still earns the storage-clear refund; clearing and then writing the original value back reverses that refund and returns the write cost instead.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
@pytest.mark.parametrize(
    "initial_value,post_value",
    [
        pytest.param(2, 0, id="dirty_clear"),
        pytest.param(0, 1, id="clear_then_restore_original"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_sstore_dirty_slot_refund(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    initial_value: int,
    post_value: int,
) -> None:
    """
    Test the refund for a second write to a slot already changed in the
    same transaction.

    The write cost was paid by the first change, so the second write is
    charged the warm access alone. Clearing a dirty slot still earns the
    storage-clear refund; clearing and then writing the original value
    back reverses that refund and returns the write cost instead.
    """
    original = 1
    code = Op.SSTORE(
        0,
        initial_value,
        # gas accounting
        original_value=original,
        current_value=original,
        new_value=initial_value,
    ) + Op.SSTORE(
        0,
        post_value,
        # gas accounting
        key_warm=True,
        original_value=original,
        current_value=initial_value,
        new_value=post_value,
    )
    assert code.state_cost(fork) == 0, "a nonzero slot grows no state"
    assert code.state_refund(fork) == 0, "no state charge, no state refund"
    assert code.refund(fork) > 0, "the dirty second write earns a refund"

    pre_refund_gas = (
        fork.transaction_intrinsic_cost_calculator()()
        + code.execution_cost(fork)
    )

    contract = pre.deploy_contract(code=code, storage={0: original})

    tx = Transaction(
        to=contract,
        state_gas_reservoir=0,
        sender=pre.fund_eoa(),
        expected_receipt=TransactionReceipt(
            cumulative_gas_used=sender_gas_used(fork, pre_refund_gas, code)
        ),
    )

    state_test(
        pre=pre,
        post={contract: Account(storage={0: post_value})},
        tx=tx,
        blockchain_test_header_verify=Header(gas_used=pre_refund_gas),
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.