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@c74f1a67.

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
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
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)],
        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)],
        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.