Skip to content

test_revert_discards_descendant_storage_clear_credit_through_depth()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_reservoir.py::test_revert_discards_descendant_storage_clear_credit_through_depth@c74f1a67.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_reservoir.py::test_revert_discards_descendant_storage_clear_credit_through_depth --fork Amsterdam

A reverted ancestor must discard a clear-credit regardless of how many successful frames sit between the X→0 source and the revert.

top → reverter (REVERT) → pass_1 → … → pass_k → inner (X→0)

Each pass frame returns successfully, so the inner credit walks up through incorporate_child_on_success at every layer before landing in the reverter, where it must be dropped on incorporate_child_on_error. The receipt invariant holds for every k.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_reservoir.py
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
@pytest.mark.parametrize(
    "intermediate_depth",
    [
        pytest.param(0, id="direct"),
        pytest.param(1, id="depth_1"),
        pytest.param(3, id="depth_3"),
        pytest.param(10, id="depth_10"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_revert_discards_descendant_storage_clear_credit_through_depth(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    intermediate_depth: int,
) -> None:
    """
    A reverted ancestor must discard a clear-credit regardless of
    how many successful frames sit between the X→0 source and the
    revert.

    top → reverter (REVERT)
            → pass_1 → … → pass_k → inner (X→0)

    Each pass frame returns successfully, so the inner credit walks
    up through `incorporate_child_on_success` at every layer before
    landing in the reverter, where it must be dropped on
    `incorporate_child_on_error`. The receipt invariant holds for
    every `k`.
    """
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)
    intrinsic_cost = fork.transaction_intrinsic_cost_calculator()()

    num_slots = 5
    phantom_base = 10

    # Slots are warm at inner: top's setup populates the access list
    # and DELEGATECALL preserves it down the chain.
    inner_code = Bytecode()
    for i in range(num_slots):
        inner_code += Op.SSTORE.with_metadata(
            key_warm=True,
            original_value=0,
            current_value=1,
            new_value=0,
        )(i, 0)
    inner = pre.deploy_contract(code=inner_code)

    # Build the pass-through chain bottom-up so each frame can encode
    # the next address. Each pass_i DELEGATECALLs into the next frame
    # and STOPs successfully, propagating inner's credit upward.
    pass_codes = []
    next_addr = inner
    for _ in range(intermediate_depth):
        pass_code = (
            Op.POP(Op.DELEGATECALL(gas=Op.GAS, address=next_addr)) + Op.STOP
        )
        pass_codes.append(pass_code)
        next_addr = pre.deploy_contract(code=pass_code)

    # Reverter sits between top and the chain: enters, then REVERTs.
    reverter_code = Op.POP(Op.DELEGATECALL(gas=Op.GAS, address=next_addr)) + (
        Op.REVERT(0, 0)
    )
    reverter = pre.deploy_contract(code=reverter_code)

    setup_code = Bytecode()
    for i in range(num_slots):
        setup_code += Op.SSTORE(i, 1)
    delegatecall_step = Op.POP(Op.DELEGATECALL(gas=Op.GAS, address=reverter))
    phantom_code = Bytecode()
    for i in range(num_slots):
        phantom_code += Op.SSTORE(phantom_base + i, 1)
    top_code = setup_code + delegatecall_step + phantom_code
    top = pre.deploy_contract(code=top_code)

    legit_state_cost = 2 * num_slots * sstore_state_gas

    expected_cumulative = (
        intrinsic_cost
        + top_code.gas_cost(fork)
        + reverter_code.gas_cost(fork)
        + sum(c.gas_cost(fork) for c in pass_codes)
        + inner_code.gas_cost(fork)
    )

    tx = Transaction(
        to=top,
        state_gas_reservoir=legit_state_cost,
        sender=pre.fund_eoa(),
        expected_receipt=TransactionReceipt(
            cumulative_gas_used=expected_cumulative,
        ),
    )
    expected_storage = dict.fromkeys(range(num_slots), 1) | {
        phantom_base + i: 1 for i in range(num_slots)
    }

    state_test(
        pre=pre,
        post={top: Account(storage=expected_storage)},
        tx=tx,
    )

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.