Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/constantinople/eip1052_extcodehash/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Cross-client EIP-1052 EXTCODEHASH Tests."""
158 changes: 158 additions & 0 deletions tests/constantinople/eip1052_extcodehash/test_extcodehash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
"""
Test EIP-1052 EXTCODEHASH.
"""

import pytest
from execution_testing import (
Account,
Alloc,
Initcode,
Op,
StateTestFiller,
Storage,
Transaction,
compute_create2_address,
)

from ethereum.crypto.hash import keccak256

REFERENCE_SPEC_GIT_PATH = "EIPS/eip-1052.md"
REFERENCE_SPEC_VERSION = "b0a21c42cb76b9f4f120dd4bd2bd3d83f6682b8a"


@pytest.mark.valid_from("Constantinople")
@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],
)
Comment on lines +30 to +33
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test format looks good, but in the legacy test the target account (created via CREATE2) had pre-existing storage. We’ve removed that setup, do we lose the coverage here?

I now understand why pytest.mark.pre_alloc_modify is required: we want to configure an account storage via pre.

I’m thinking of parameterizing the cases like this:

@pytest.mark.parametrize(
    "exist_balance, exist_storage",
    [
        pytest.param(True, True, id="collision", marks=pytest.mark.pre_alloc_modify),
        pytest.param(True, False, id="balance_only"),
        pytest.param(False, False, id="non_existent"),
    ],
)

And configuring pre like:

if exist_storage:
    pre[target_address] = Account(balance=10, storage={0: 1})
elif exist_balance:
    pre.fund_address(target_address, 1)

However, this setting keeps failing in fill: it looks like t8n doesn’t handle the initial storage + nonce=0 scenario as I expected. Am I missing something in how pre-state is applied here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add multiple markers like this, for the pre-alloc modification, we should also skip the execute-remote mode.

pytest.param(
  True,
  True,
  id="collision",
  marks=[
      pytest.mark.pre_alloc_modify,
      pytest.mark.execute(
          pytest.mark.skip(reason="pre-alloc modified")
      ),
  ],
),

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 non-existent target account.
+ Op.SSTORE(
caller_storage.store_next(0), Op.EXTCODESIZE(Op.CALLDATALOAD(0))
)
# EXTCODECOPY of non-existent target account.
+ Op.EXTCODECOPY(Op.CALLDATALOAD(0), 0, 0, 32)
+ Op.SSTORE(caller_storage.store_next(0), Op.MLOAD(0))
# DELEGATECALL the non-existent 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)

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"),
gas_limit=400_000,
)

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

This file was deleted.

Loading