Skip to content

test_extcodehash_dynamic_account_overwrite()

Documentation for tests/constantinople/eip1052_extcodehash/test_extcodehash.py::test_extcodehash_dynamic_account_overwrite@b314d18e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/constantinople/eip1052_extcodehash/test_extcodehash.py::test_extcodehash_dynamic_account_overwrite --fork Amsterdam

Test EXTCODEHASH of non-existent/no-code account, then with code deployed at the address via CREATE2.

This verifies that the code hash cache is correctly updated during the transaction when an account is overwritten by CREATE2.

The target address is computed after the caller contract code is deployed, and passed as calldata to the caller contract.

The target account code sets a fixed storage slot. This code is executed at the caller account via DELEGATECALL and at the target account via CALL.

Modified from original test: target account has no storage to avoid EIP-7610 collision behavior.

Source code in tests/constantinople/eip1052_extcodehash/test_extcodehash.py
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
@pytest.mark.ported_from(
    [
        "https://github.com/ethereum/tests/tree/v13.3/src/GeneralStateTestsFiller/stExtCodeHash/dynamicAccountOverwriteEmpty_ParisFiller.yml",  # noqa: E501
    ],
    pr=["https://github.com/ethereum/execution-specs/pull/2032"],
)
@pytest.mark.parametrize(
    "target_exists",
    [True, False],
)
def test_extcodehash_dynamic_account_overwrite(
    state_test: StateTestFiller,
    pre: Alloc,
    target_exists: bool,
) -> None:
    """
    Test EXTCODEHASH of non-existent/no-code account,
    then with code deployed at the address via CREATE2.

    This verifies that the code hash cache is correctly updated during the
    transaction when an account is overwritten by CREATE2.

    The target address is computed after the caller contract code is deployed,
    and passed as calldata to the caller contract.

    The target account code sets a fixed storage slot. This code is executed
    at the caller account via DELEGATECALL and at the target account via CALL.

    Modified from original test: target account has no storage to avoid
    EIP-7610 collision behavior.
    """
    target_storage_slot = 0x4A
    caller_storage = Storage()
    target_storage = Storage()

    deploy_code = Op.SSTORE(target_storage_slot, 1)
    create2_initcode = Initcode(deploy_code=deploy_code)

    caller_code = (
        # EXTCODEHASH of the pre-CREATE2 target account.
        Op.SSTORE(
            caller_storage.store_next(keccak256(b"") if target_exists else 0),
            Op.EXTCODEHASH(Op.CALLDATALOAD(0)),
        )
        # EXTCODESIZE of target account.
        + Op.SSTORE(
            caller_storage.store_next(0), Op.EXTCODESIZE(Op.CALLDATALOAD(0))
        )
        # EXTCODECOPY of target account.
        + Op.EXTCODECOPY(Op.CALLDATALOAD(0), 0, 0, 32)
        + Op.SSTORE(caller_storage.store_next(0), Op.MLOAD(0))
        # DELEGATECALL the target account.
        + Op.SSTORE(
            caller_storage.store_next(1),
            Op.DELEGATECALL(
                address=Op.CALLDATALOAD(0),
                gas=0,  # Pass zero gas to ensure no execution.
            ),
        )
    )
    # Target address to be set later.
    target_address_slot = caller_storage.store_next(0, "target_address")
    caller_code += (
        # CREATE2 to overwrite the account
        Op.MSTORE(0, Op.PUSH32(bytes(create2_initcode).ljust(32, b"\0")))
        + Op.SSTORE(
            target_address_slot,
            Op.CREATE2(value=0, offset=0, size=len(create2_initcode), salt=0),
        )
        # EXTCODEHASH of the target account.
        + Op.SSTORE(
            caller_storage.store_next(deploy_code.keccak256()),
            Op.EXTCODEHASH(Op.CALLDATALOAD(0)),
        )
        # EXTCODESIZE of the target account.
        + Op.SSTORE(
            caller_storage.store_next(len(deploy_code)),
            Op.EXTCODESIZE(Op.CALLDATALOAD(0)),
        )
        # EXTCODECOPY of the target account.
        + Op.EXTCODECOPY(Op.CALLDATALOAD(0), 0, 0, 32)
        + Op.SSTORE(
            caller_storage.store_next(bytes(deploy_code).ljust(32, b"\0")),
            Op.MLOAD(0),
        )
        # DELEGATECALL the target account.
        + Op.SSTORE(
            caller_storage.store_next(1),
            Op.DELEGATECALL(
                address=Op.CALLDATALOAD(0),
                gas=Op.GAS,
            ),
        )
        # Call the deployed contract to execute its "deploy_code".
        + Op.SSTORE(
            caller_storage.store_next(1),
            Op.CALL(address=Op.CALLDATALOAD(0), gas=Op.GAS),
        )
    )

    caller_address = pre.deploy_contract(
        caller_code, balance=1, storage=caller_storage.canary()
    )

    target_address = compute_create2_address(
        address=caller_address,
        salt=0,
        initcode=create2_initcode,
    )

    if target_exists:
        pre.fund_address(target_address, 1)

    caller_storage[target_address_slot] = target_address
    caller_storage[target_storage_slot] = 1
    target_storage[target_storage_slot] = 1

    sender = pre.fund_eoa()
    tx = Transaction(
        sender=sender,
        to=caller_address,
        data=bytes(target_address).rjust(32, b"\0"),
    )

    state_test(
        pre=pre,
        post={
            caller_address: Account(storage=caller_storage),
            target_address: Account(
                nonce=1,
                code=deploy_code,
                storage=target_storage,
            ),
        },
        tx=tx,
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 10 forks.