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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 | @pytest.mark.parametrize(
"sweep_on_redeploy",
[
pytest.param(False, id="selfdestruct_to_self"),
pytest.param(True, id="sweep_to_origin"),
],
)
@EIPChecklist.Opcode.Test.ExecutionContext.Initcode.Reentry()
@EIPChecklist.Opcode.Test.Terminating.Scenarios.Initcode()
def test_create2_redeploy_over_remnant(
blockchain_test: BlockchainTestFiller,
pre: Alloc,
fork: Fork,
sweep_on_redeploy: bool,
) -> None:
"""
Case 17 of the EIP and its third security consideration. Because the
account left behind has nonce zero and no storage, the same factory can
CREATE2 over it again in a later transaction, and that second deployment
can spend the balance the first one left there.
"""
endowment = 5
deployer = pre.fund_eoa()
redeployer = pre.fund_eoa()
if sweep_on_redeploy:
# Only a redeploy sees a balance above the call value, so the same
# initcode bytes sweep to the redeployer the second time.
selfdestruct: Bytecode = Conditional(
condition=Op.GT(Op.BALANCE(Op.ADDRESS), Op.CALLVALUE),
if_true=Op.SELFDESTRUCT(Op.ORIGIN),
if_false=Op.SELFDESTRUCT(Op.ADDRESS),
)
redeploy_value = 0
else:
selfdestruct = Op.SELFDESTRUCT(Op.ADDRESS)
redeploy_value = endowment
initcode = Op.SSTORE(0, 1) + selfdestruct
factory = pre.deploy_contract(
code=Om.MSTORE(initcode, 0)
+ Op.SSTORE(
Op.CALLDATALOAD(0),
Op.CREATE2(
value=Op.CALLVALUE, offset=0, size=len(initcode), salt=0
),
)
+ Op.STOP
)
created = compute_create_address(
address=factory, salt=0, initcode=initcode, opcode=Op.CREATE2
)
tx1 = Transaction(
sender=deployer, to=factory, value=endowment, data=Hash(1)
)
tx2 = Transaction(
sender=redeployer, to=factory, value=redeploy_value, data=Hash(2)
)
created_post: Account | None
if sweep_on_redeploy:
created_post = Account.NONEXISTENT
final_balance = 0
tx2_logs = [transfer_log(created, redeployer, endowment)]
else:
final_balance = 2 * endowment
created_post = Account(
balance=final_balance, nonce=0, code=b"", storage={}
)
tx2_logs = [
transfer_log(redeployer, factory, endowment),
transfer_log(factory, created, endowment),
]
if fork.is_eip_enabled(7708):
tx1.expected_receipt = TransactionReceipt(
logs=[
transfer_log(deployer, factory, endowment),
transfer_log(factory, created, endowment),
]
)
tx2.expected_receipt = TransactionReceipt(logs=tx2_logs)
expected_bal = None
if fork.is_eip_enabled(7928):
expected_bal = BlockAccessListExpectation(
account_expectations={
created: BalAccountExpectation(
nonce_changes=[],
code_changes=[],
storage_changes=[],
storage_reads=[0],
balance_changes=[
BalBalanceChange(
block_access_index=1, post_balance=endowment
),
BalBalanceChange(
block_access_index=2, post_balance=final_balance
),
],
)
}
)
blockchain_test(
pre=pre,
blocks=[
Block(txs=[tx1, tx2], expected_block_access_list=expected_bal)
],
post={
factory: Account(nonce=3, storage={1: created, 2: created}),
created: created_post,
},
)
|