Skip to content

test_bal_extcodecopy_and_oog()

Documentation for tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_opcodes.py::test_bal_extcodecopy_and_oog@c74f1a67.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_opcodes.py::test_bal_extcodecopy_and_oog --fork Amsterdam

Ensure BAL handles EXTCODECOPY and OOG during EXTCODECOPY appropriately.

Tests various OOG scenarios: - success: EXTCODECOPY completes, target appears in BAL - oog_at_cold_access: OOG before cold access, target NOT in BAL - oog_at_memory_large_offset: OOG at memory expansion (large offset), target NOT in BAL - oog_at_memory_boundary: OOG at memory expansion (boundary case), target NOT in BAL

Gas for all components (cold access + copy + memory expansion) must be checked BEFORE recording account access.

Source code in tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_opcodes.py
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
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
@pytest.mark.parametrize(
    "oog_scenario,memory_offset,copy_size",
    [
        pytest.param("success", 0, 0, id="successful_extcodecopy"),
        pytest.param("oog_at_cold_access", 0, 0, id="oog_at_cold_access"),
        pytest.param(
            "oog_at_memory_large_offset",
            0x10000,
            32,
            id="oog_at_memory_large_offset",
        ),
        pytest.param(
            "oog_at_memory_boundary",
            256,
            32,
            id="oog_at_memory_boundary",
        ),
    ],
)
def test_bal_extcodecopy_and_oog(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
    fork: Fork,
    oog_scenario: str,
    memory_offset: int,
    copy_size: int,
) -> None:
    """
    Ensure BAL handles EXTCODECOPY and OOG during EXTCODECOPY appropriately.

    Tests various OOG scenarios:
    - success: EXTCODECOPY completes, target appears in BAL
    - oog_at_cold_access: OOG before cold access, target NOT in BAL
    - oog_at_memory_large_offset: OOG at memory expansion (large offset),
      target NOT in BAL
    - oog_at_memory_boundary: OOG at memory expansion (boundary case),
      target NOT in BAL

    Gas for all components (cold access + copy + memory expansion) must be
    checked BEFORE recording account access.
    """
    alice = pre.fund_eoa()

    # Create target contract with some code
    target_contract = pre.deploy_contract(code=Op.PUSH1(0x42) + Op.STOP)

    # Full EXTCODECOPY: access + copy + memory expansion
    extcodecopy_code = Op.EXTCODECOPY(
        address=target_contract,
        dest_offset=memory_offset,
        offset=0,
        size=copy_size,
        address_warm=False,
        data_size=copy_size,
        new_memory_size=memory_offset + copy_size,
    )

    extcodecopy_contract = pre.deploy_contract(code=extcodecopy_code + Op.STOP)

    intrinsic_gas_cost = fork.transaction_intrinsic_cost_calculator()()

    if oog_scenario == "success":
        # Provide enough gas for everything including memory expansion
        tx_gas_limit = intrinsic_gas_cost + extcodecopy_code.gas_cost(fork)
        target_in_bal = True
    elif oog_scenario == "oog_at_cold_access":
        # Provide gas for pushes but 1 less than cold access
        extcodecopy_access_only = Op.EXTCODECOPY(
            address=target_contract,
            dest_offset=memory_offset,
            offset=0,
            size=copy_size,
            address_warm=False,
            data_size=0,
            new_memory_size=0,
        )
        tx_gas_limit = (
            intrinsic_gas_cost + extcodecopy_access_only.gas_cost(fork) - 1
        )
        target_in_bal = False
    elif oog_scenario == "oog_at_memory_large_offset":
        # Provide gas for push + cold access + copy, but NOT memory expansion
        extcodecopy_no_mem = Op.EXTCODECOPY(
            address=target_contract,
            dest_offset=memory_offset,
            offset=0,
            size=copy_size,
            address_warm=False,
            data_size=copy_size,
            new_memory_size=0,
        )
        tx_gas_limit = intrinsic_gas_cost + extcodecopy_no_mem.gas_cost(fork)
        target_in_bal = False
    elif oog_scenario == "oog_at_memory_boundary":
        # Calculate full cost and provide exactly 1 less than needed
        tx_gas_limit = intrinsic_gas_cost + extcodecopy_code.gas_cost(fork) - 1
        target_in_bal = False
    else:
        raise ValueError(f"Invariant: unknown oog_scenario {oog_scenario}")

    tx = Transaction(
        sender=alice,
        to=extcodecopy_contract,
        gas_limit=tx_gas_limit,
    )

    block = Block(
        txs=[tx],
        expected_block_access_list=BlockAccessListExpectation(
            account_expectations={
                extcodecopy_contract: BalAccountExpectation.empty(),
                **(
                    {target_contract: BalAccountExpectation.empty()}
                    if target_in_bal
                    else {target_contract: None}
                ),
            }
        ),
    )

    blockchain_test(
        pre=pre,
        blocks=[block],
        post={
            alice: Account(nonce=1),
            extcodecopy_contract: Account(),
            target_contract: Account(),
        },
    )

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.