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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323 | @pytest.mark.parametrize(
"to_self",
[
pytest.param(True, id="to_self"),
pytest.param(False, id="to_other"),
],
)
@pytest.mark.parametrize(
"call_twice,second_call_value",
[
pytest.param(True, 1, id="call_twice_with_value"),
pytest.param(True, 0, id="call_twice"),
pytest.param(False, 0, id="call_once"),
],
)
@pytest.mark.parametrize(
"transfer_during_create",
[
pytest.param(True, id="transfer_during_create"),
pytest.param(False, id="transfer_during_call"),
],
)
def test_selfdestruct_same_tx_via_call(
state_test: StateTestFiller,
env: Environment,
pre: Alloc,
sender: EOA,
to_self: bool,
call_twice: bool,
second_call_value: int,
transfer_during_create: bool,
) -> None:
"""
Test selfdestruct via CREATE-then-CALL (not initcode selfdestruct).
Factory CREATEs contract with runtime code, then CALLs the contract that
was just created to trigger SELFDESTRUCT (depending on
`transfer_during_create`, the value of the contract is transferred during
the CREATE or CALL opcodes). Contract is still in created_accounts.
Depending on `call_twice`, the contract can be called twice during the
same call frame where it was created.
"""
contract_balance = 2000
beneficiary = pre.deploy_contract(Op.STOP)
if to_self:
runtime_code = Op.SELFDESTRUCT(Op.ADDRESS)
else:
runtime_code = Op.SELFDESTRUCT(beneficiary)
initcode = Initcode(deploy_code=runtime_code)
initcode_len = len(initcode)
if transfer_during_create:
create_value = contract_balance
first_call_value = 0
else:
create_value = 0
first_call_value = contract_balance
factory_code = (
Om.MSTORE(initcode, 0)
+ Op.SSTORE(
0, Op.CREATE(value=create_value, offset=0, size=initcode_len)
)
+ Op.SSTORE(
1,
Op.CALL(gas=100_000, address=Op.SLOAD(0), value=first_call_value),
)
)
if call_twice:
factory_code += Op.SSTORE(
2,
Op.CALL(gas=100_000, address=Op.SLOAD(0), value=second_call_value),
)
factory = pre.deploy_contract(
factory_code, balance=contract_balance + second_call_value
)
created_address = compute_create_address(address=factory, nonce=1)
factory_storage = {
0: created_address,
1: 1,
}
if call_twice:
factory_storage[2] = 1
if to_self:
expected_logs = [
transfer_log(factory, created_address, contract_balance),
burn_log(created_address, contract_balance),
]
if call_twice and second_call_value > 0:
expected_logs += [
transfer_log(factory, created_address, second_call_value),
burn_log(created_address, second_call_value),
]
post = {factory: Account(storage=factory_storage)}
else:
expected_logs = [
transfer_log(factory, created_address, contract_balance),
transfer_log(created_address, beneficiary, contract_balance),
]
if call_twice and second_call_value > 0:
expected_logs += [
transfer_log(factory, created_address, second_call_value),
transfer_log(created_address, beneficiary, second_call_value),
]
post = {
beneficiary: Account(balance=contract_balance + second_call_value),
factory: Account(storage=factory_storage),
}
tx = Transaction(
sender=sender,
to=factory,
value=0,
gas_limit=300_000,
expected_receipt=TransactionReceipt(logs=expected_logs),
)
state_test(env=env, pre=pre, post=post, tx=tx)
|