30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 | @pytest.mark.valid_from("Paris")
@pytest.mark.parametrize(
"create2_dest_already_in_state",
(
pytest.param(True, marks=pytest.mark.pre_alloc_mutable),
False,
),
)
@pytest.mark.parametrize(
"call_create2_contract_in_between,call_create2_contract_at_the_end",
[
(True, True),
(True, False),
(False, True),
],
)
def test_dynamic_create2_selfdestruct_collision(
fork: Fork,
create2_dest_already_in_state: bool,
call_create2_contract_in_between: bool,
call_create2_contract_at_the_end: bool,
pre: Alloc,
state_test: StateTestFiller,
) -> None:
"""
Dynamic Create2->Suicide->Create2 collision scenario.
Perform a CREATE2, make sure that the initcode sets at least a couple of
storage keys, then on a different call, in the same tx, perform a
self-destruct.
Then:
a) on the same tx, attempt to recreate the contract
-> Covered in this test
1) and create2 contract already in the state
2) and create2 contract is not in the state
b) on a different tx, attempt to recreate the contract
Perform a CREATE2, make sure that the initcode sets at least a couple
of storage keys, then in a different tx, perform a self-destruct.
Then:
a) on the same tx, attempt to recreate the contract
b) on a different tx, attempt to recreate the contract
Check the test case described in
https://lf-hyperledger.atlassian.net/wiki/spaces/BESU/pages/
22156575/2024-01-06+Mainnet+Halting+Event
"""
assert (
call_create2_contract_in_between or call_create2_contract_at_the_end
), "invalid test"
# Storage locations
create2_constructor_worked = 1
first_create2_result = 2
second_create2_result = 3
code_worked = 4
# Constants
address_zero = Address(0x00)
create2_salt = 1
# Create EOA for sendall destination (receives selfdestruct funds)
sendall_destination = pre.fund_eoa(0) # Will be funded by selfdestruct
# calls
# Create storage contract that will be called during initialization
address_create2_storage = pre.deploy_contract(
code=Op.SSTORE(1, 1),
balance=7000000000000000000,
)
# CREATE2 Initcode
deploy_code = Op.SELFDESTRUCT(sendall_destination)
initcode = Initcode(
deploy_code=deploy_code,
initcode_prefix=Op.SSTORE(create2_constructor_worked, 1)
+ Op.CALL(Op.GAS(), address_create2_storage, 0, 0, 0, 0, 0),
)
# Create the contract that performs CREATE2 operations
address_code = pre.deploy_contract(
code=Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE())
+ Op.MSTORE(
0,
Op.CREATE2(Op.SELFBALANCE(), 0, Op.CALLDATASIZE(), create2_salt),
)
+ Op.RETURN(0, 32),
)
# Created addresses - now we can compute create2_address
create2_address = compute_create2_address(
address_code, create2_salt, initcode
)
call_address_in_between = (
create2_address if call_create2_contract_in_between else address_zero
)
call_address_in_the_end = (
create2_address if call_create2_contract_at_the_end else address_zero
)
# Values
pre_existing_create2_balance = 1
first_create2_value = 10
first_call_value = 100
second_create2_value = 1000
second_call_value = 10000
# Create the main contract that orchestrates the test
address_to = pre.deploy_contract(
code=Op.JUMPDEST()
# Make a subcall that do CREATE2 and returns its the result
+ Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE())
+ Op.CALL(
100000,
address_code,
first_create2_value,
0,
Op.CALLDATASIZE(),
0,
32,
)
+ Op.SSTORE(
first_create2_result,
Op.MLOAD(0),
)
# In case the create2 didn't work, flush account balance
+ Op.CALL(100000, address_code, 0, 0, 0, 0, 0)
# Call to the created account to trigger selfdestruct
+ Op.CALL(
100000, call_address_in_between, first_call_value, 0, 0, 0, 0
)
# Make a subcall that do CREATE2 collision and returns its address as
# the result
+ Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE())
+ Op.CALL(
100000,
address_code,
second_create2_value,
0,
Op.CALLDATASIZE(),
0,
32,
)
+ Op.SSTORE(
second_create2_result,
Op.MLOAD(0),
)
# Call to the created account to trigger selfdestruct
+ Op.CALL(
100000, call_address_in_the_end, second_call_value, 0, 0, 0, 0
)
+ Op.SSTORE(code_worked, 1),
balance=100000000,
storage={first_create2_result: 0xFF, second_create2_result: 0xFF},
)
# Create the sender EOA
sender = pre.fund_eoa(7000000000000000000)
if create2_dest_already_in_state:
# Create2 address already in the state, e.g. deployed in a previous
# block
pre[create2_address] = Account(
balance=pre_existing_create2_balance,
nonce=1,
code=deploy_code,
storage={},
)
post: Dict[Address, Union[Account, object]] = {}
# Create2 address only exists if it was pre-existing and after cancun
post[create2_address] = (
Account(
balance=0,
nonce=1,
code=deploy_code,
storage={create2_constructor_worked: 0x00},
)
if create2_dest_already_in_state and fork >= Cancun
else Account.NONEXISTENT
)
# Create2 initcode is only executed if the contract did not already exist
post[address_create2_storage] = Account(
storage={
create2_constructor_worked: int(not create2_dest_already_in_state)
}
)
# Entry code that makes the calls to the create2 contract creator
post[address_to] = Account(
storage={
code_worked: 0x01,
# First create2 only works if the contract was not preexisting
first_create2_result: 0x00
if create2_dest_already_in_state
else create2_address,
# Second create2 must never work
second_create2_result: 0x00,
}
)
# Calculate the destination account expected balance for the
# selfdestruct/sendall calls
sendall_destination_balance = (
pre_existing_create2_balance
if create2_dest_already_in_state
else first_create2_value
)
if call_create2_contract_in_between:
sendall_destination_balance += first_call_value
if call_create2_contract_at_the_end:
sendall_destination_balance += second_call_value
post[sendall_destination] = Account(balance=sendall_destination_balance)
tx = Transaction(
to=address_to,
data=initcode,
gas_limit=5_000_000,
sender=sender,
)
state_test(pre=pre, post=post, tx=tx)
|