Skip to content

test_bal_cross_block_ripemd160_state_leak()

Documentation for tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py::test_bal_cross_block_ripemd160_state_leak@892e6d1e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py::test_bal_cross_block_ripemd160_state_leak --fork Amsterdam

Ensure internal EVM state for RIMPEMD-160 precompile handling does not leak between blocks.

The EVM may track internal state related to the Parity Touch Bug (EIP-161) when calling RIPEMD-160 (0x03) with zero value. If this state is not properly reset between blocks, it can cause incorrect BAL entries in subsequent blocks.

Prerequisites for triggering the bug: 1. RIPEMD-160 (0x03) must already exist in state before the call. 2. Block 1 must call RIPEMD-160 with zero value and complete successfully. 3. Block 2 must have a TX that triggers an exception (not REVERT).

Expected behavior: - Block 1: RIPEMD-160 in BAL (legitimate access) - Block 2: RIPEMD-160 NOT in BAL (never touched in this block)

Bug behavior: - Block 2 incorrectly has RIPEMD-160 in its BAL due to leaked internal state.

Source code in tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
def test_bal_cross_block_ripemd160_state_leak(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
) -> None:
    """
    Ensure internal EVM state for RIMPEMD-160 precompile handling does not
    leak between blocks.

    The EVM may track internal state related to the Parity Touch Bug (EIP-161)
    when calling RIPEMD-160 (0x03) with zero value. If this state is not
    properly reset between blocks, it can cause incorrect BAL entries in
    subsequent blocks.

    Prerequisites for triggering the bug:
    1. RIPEMD-160 (0x03) must already exist in state before the call.
    2. Block 1 must call RIPEMD-160 with zero value and complete successfully.
    3. Block 2 must have a TX that triggers an exception (not REVERT).

    Expected behavior:
    - Block 1: RIPEMD-160 in BAL (legitimate access)
    - Block 2: RIPEMD-160 NOT in BAL (never touched in this block)

    Bug behavior:
    - Block 2 incorrectly has RIPEMD-160 in its BAL due to leaked
      internal state.
    """
    alice = pre.fund_eoa()
    bob = pre.fund_eoa()
    # Pre-fund RIPEMD-160 so it exists before the call.
    # This is required to trigger the internal state tracking.
    ripemd160_addr = Address(0x03)
    pre.fund_address(ripemd160_addr, amount=1)

    # Contract that calls RIPEMD-160 with zero value
    ripemd_caller = pre.deploy_contract(
        code=Op.CALL(50_000, ripemd160_addr, 0, 0, 0, 0, 0) + Op.STOP
    )
    # Contract that triggers an exception
    # (stack underflow from ADD on empty stack)
    exception_contract = pre.deploy_contract(code=Op.ADD)

    # Block 1: Call RIPEMD-160 successfully
    block1 = Block(
        txs=[
            Transaction(
                sender=alice,
                to=ripemd_caller,
                gas_limit=100_000,
            )
        ],
        expected_block_access_list=BlockAccessListExpectation(
            account_expectations={
                alice: BalAccountExpectation(
                    nonce_changes=[
                        BalNonceChange(block_access_index=1, post_nonce=1)
                    ]
                ),
                bob: None,
                ripemd_caller: BalAccountExpectation.empty(),
                ripemd160_addr: BalAccountExpectation.empty(),
            }
        ),
    )

    # Block 2: Exception triggers internal exception handling.
    # If internal state leaked from Block 1, RIPEMD-160 would incorrectly
    # appear in Block 2's BAL.
    block2 = Block(
        txs=[
            Transaction(
                sender=bob,
                to=exception_contract,
                gas_limit=100_000,
            )
        ],
        expected_block_access_list=BlockAccessListExpectation(
            account_expectations={
                alice: None,
                bob: BalAccountExpectation(
                    nonce_changes=[
                        BalNonceChange(block_access_index=1, post_nonce=1)
                    ]
                ),
                # this is the important check
                ripemd160_addr: None,
            }
        ),
    )

    blockchain_test(
        pre=pre,
        blocks=[block1, block2],
        post={
            alice: Account(nonce=1),
            bob: Account(nonce=1),
            ripemd160_addr: Account(balance=1),
        },
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.