Skip to content

Commit 89d5807

Browse files
new(tests): EIP-7610: init-collision tests (#636)
* new(tests): EIP-7610: init-collision tests * fixup * fix(tests): Remove static variants * Review suggestions Co-authored-by: 蔡佳誠 Louis Tsai <[email protected]> --------- Co-authored-by: 蔡佳誠 Louis Tsai <[email protected]>
1 parent 98be2d6 commit 89d5807

File tree

4 files changed

+157
-330
lines changed

4 files changed

+157
-330
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Cross-client Create Collision Tests."""
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
"""
2+
Test collision in CREATE/CREATE2 account creation, where the existing account
3+
only has a non-zero storage slot set.
4+
"""
5+
6+
import pytest
7+
8+
from ethereum_test_tools import (
9+
Account,
10+
Alloc,
11+
Bytecode,
12+
Initcode,
13+
StateTestFiller,
14+
Transaction,
15+
compute_create_address,
16+
)
17+
from ethereum_test_tools import Opcodes as Op
18+
19+
REFERENCE_SPEC_GIT_PATH = "EIPS/eip-7610.md"
20+
REFERENCE_SPEC_VERSION = "80ef48d0bbb5a4939ade51caaaac57b5df6acd4e"
21+
22+
pytestmark = [
23+
pytest.mark.valid_from("Paris"),
24+
pytest.mark.ported_from(
25+
[
26+
"https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stSStoreTest/InitCollisionFiller.json",
27+
"https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stSStoreTest/InitCollisionNonZeroNonceFiller.json",
28+
"https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stSStoreTest/InitCollisionParisFiller.json",
29+
],
30+
pr=["https://github.com/ethereum/execution-spec-tests/pull/636"],
31+
),
32+
pytest.mark.parametrize(
33+
"collision_nonce,collision_balance,collision_code",
34+
[
35+
pytest.param(0, 0, b"\0", id="non-empty-code"),
36+
pytest.param(0, 1, b"", id="non-empty-balance"),
37+
pytest.param(1, 0, b"", id="non-empty-nonce"),
38+
],
39+
),
40+
pytest.mark.parametrize(
41+
"initcode",
42+
[
43+
pytest.param(
44+
Initcode(
45+
deploy_code=Op.STOP,
46+
initcode_prefix=Op.SSTORE(0, 1) + Op.SSTORE(1, 0),
47+
),
48+
id="correct-initcode",
49+
),
50+
pytest.param(Op.REVERT(0, 0), id="revert-initcode"),
51+
pytest.param(Op.MSTORE(0xFFFFFFFFFFFFFFFFFFFFFFFFFFF, 1), id="oog-initcode"),
52+
],
53+
),
54+
pytest.mark.pre_alloc_modify, # We need to modify the pre-alloc to include the collision
55+
]
56+
57+
58+
@pytest.mark.with_all_contract_creating_tx_types
59+
def test_init_collision_create_tx(
60+
state_test: StateTestFiller,
61+
pre: Alloc,
62+
tx_type: int,
63+
collision_nonce: int,
64+
collision_balance: int,
65+
collision_code: bytes,
66+
initcode: Bytecode,
67+
):
68+
"""
69+
Test that a contract creation transaction exceptionally aborts when
70+
the target address has a non-empty storage, balance, nonce, or code.
71+
"""
72+
tx = Transaction(
73+
sender=pre.fund_eoa(),
74+
type=tx_type,
75+
to=None,
76+
data=initcode,
77+
gas_limit=200_000,
78+
)
79+
80+
created_contract_address = tx.created_contract
81+
82+
# This is the collision
83+
pre[created_contract_address] = Account(
84+
storage={0x01: 0x01},
85+
nonce=collision_nonce,
86+
balance=collision_balance,
87+
code=collision_code,
88+
)
89+
90+
state_test(
91+
pre=pre,
92+
post={
93+
created_contract_address: Account(
94+
storage={0x01: 0x01},
95+
),
96+
},
97+
tx=tx,
98+
)
99+
100+
101+
@pytest.mark.parametrize("opcode", [Op.CREATE, Op.CREATE2])
102+
def test_init_collision_create_opcode(
103+
state_test: StateTestFiller,
104+
pre: Alloc,
105+
opcode: Op,
106+
collision_nonce: int,
107+
collision_balance: int,
108+
collision_code: bytes,
109+
initcode: Bytecode,
110+
):
111+
"""
112+
Test that a contract creation opcode exceptionally aborts when the target
113+
address has a non-empty storage, balance, nonce, or code.
114+
"""
115+
assert len(initcode) <= 32
116+
contract_creator_code = (
117+
Op.MSTORE(0, Op.PUSH32(bytes(initcode).ljust(32, b"\0")))
118+
+ Op.SSTORE(0x01, opcode(value=0, offset=0, size=len(initcode)))
119+
+ Op.STOP
120+
)
121+
contract_creator_address = pre.deploy_contract(
122+
contract_creator_code,
123+
storage={0x01: 0x01},
124+
)
125+
created_contract_address = compute_create_address(
126+
address=contract_creator_address,
127+
nonce=1,
128+
salt=0,
129+
initcode=initcode,
130+
opcode=opcode,
131+
)
132+
133+
tx = Transaction(
134+
sender=pre.fund_eoa(),
135+
to=contract_creator_address,
136+
data=initcode,
137+
gas_limit=2_000_000,
138+
)
139+
140+
pre[created_contract_address] = Account(
141+
storage={0x01: 0x01},
142+
nonce=collision_nonce,
143+
balance=collision_balance,
144+
code=collision_code,
145+
)
146+
147+
state_test(
148+
pre=pre,
149+
post={
150+
created_contract_address: Account(
151+
storage={0x01: 0x01},
152+
),
153+
contract_creator_address: Account(storage={0x01: 0x00}),
154+
},
155+
tx=tx,
156+
)

static/state_tests/stSStoreTest/InitCollisionNonZeroNonceFiller.json

Lines changed: 0 additions & 161 deletions
This file was deleted.

0 commit comments

Comments
 (0)