@pytest.mark.parametrize("recreate_on_separate_block", [True, False])
@pytest.mark.valid_from("Constantinople")
@pytest.mark.valid_before("EIP6780")
def test_recreate(
blockchain_test: BlockchainTestFiller,
pre: Alloc,
fork: Fork,
recreate_on_separate_block: bool,
) -> None:
"""
Test that the storage is cleared when a contract is first destructed then
re-created using CREATE2.
"""
creator_contract_code = Op.CALLDATACOPY(
0, 0, Op.CALLDATASIZE
) + Op.CREATE2(0, 0, Op.CALLDATASIZE, 0)
creator_address = pre.deploy_contract(creator_contract_code)
sender = pre.fund_eoa()
deploy_code = (
Op.EQ(0, Op.CALLVALUE)
+ Op.PUSH1(0xC)
+ Op.JUMPI
+ Op.SSTORE(0, Op.CALLVALUE)
+ Op.STOP
+ Op.JUMPDEST
+ Op.PUSH1(0x0)
+ Op.SELFDESTRUCT
)
initcode = Initcode(deploy_code=deploy_code)
create_tx = Transaction(
gas_limit=100_000,
to=creator_address,
data=initcode,
sender=sender,
)
created_contract_address = compute_create2_address(
address=creator_address, salt=0, initcode=initcode
)
set_storage_tx = Transaction(
gas_limit=100_000,
to=created_contract_address,
value=1,
sender=sender,
)
blocks = [Block(txs=[create_tx, set_storage_tx])]
destruct_tx = Transaction(
gas_limit=100_000,
to=created_contract_address,
value=0,
sender=sender,
)
balance = 1
send_funds_tx = Transaction(
gas_limit=100_000,
to=created_contract_address,
value=balance,
sender=sender,
)
re_create_tx = Transaction(
gas_limit=100_000,
to=creator_address,
data=initcode,
sender=sender,
)
if recreate_on_separate_block:
blocks.append(Block(txs=[destruct_tx, send_funds_tx]))
blocks.append(Block(txs=[re_create_tx]))
else:
blocks.append(Block(txs=[destruct_tx, send_funds_tx, re_create_tx]))
post = {
created_contract_address: Account(
nonce=1,
balance=balance,
code=deploy_code,
storage={},
),
}
blockchain_test(pre=pre, post=post, blocks=blocks)