Hi all, does someone know why I cant recreate the same contract using create2 after calling selfdestruct?

The below is a Foundry test:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";

contract Factory {
event WalletCreated(address wallet);

function deployWallet() external returns (address) {
uint256 salt = 0x123456789; // Specify a unique salt value for create2

address walletAddress;
bytes memory bytecode = type(Wallet).creationCode;
assembly {
// Bytecode of the Wallet contract


// Calculate the address using the create2 opcode
walletAddress := create2(0, add(bytecode, 0x20), mload(bytecode), salt)
if iszero(extcodesize(walletAddress)) {
revert(0, 0)
}
}

emit WalletCreated(walletAddress);
return walletAddress;
}
}

contract Wallet {
constructor() {
}

function destroyMe() public {
selfdestruct(payable(address(msg.sender)));
}
}

contract MetamorphicTest is Test {
Factory public factory;
address public wallet1;
address public wallet2;


function _getCodeHash(address _account) private view returns (bytes32 codeHash){
assembly {
codeHash := extcodehash(_account)
}
}

function setUp() public {
factory = new Factory();
}

function testMeta() public {
wallet1 = factory.deployWallet();
console.log(address(wallet1));
Wallet(wallet1).destroyMe();

wallet2 = factory.deployWallet();
console.log(address(wallet2));
assertEq(wallet1, wallet2);
}
}

May 25, 2023, 8:43 AM
Foundry doesn't selfdestruct the contracts
It's a known issue
May 25, 2023, 8:46 AM
I think that's the problem, I just moved until destroyMe() to the setUp(), and then it works
like basically until the Test TX is not fully finished, the contract is not really destroyed
May 25, 2023, 8:47 AM
Yes
May 25, 2023, 8:47 AM
that's a pain! I wrote a little challenge that needs to mutate the code of an address (like the tornadocash exploit), I guess I'll be able to go around in my tests with the setUp
thank you Niccoló
May 25, 2023, 8:50 AM
Yw, you could try to recreate the real selfdestruct behavior using vm.etch
May 25, 2023, 8:56 AM
thanks, I just tried but it still reverts in the second create2
May 25, 2023, 8:58 AM
You should put that after destroyMe
Abd use it to empty the code
You can do it here too
May 25, 2023, 9:01 AM
yes:

wallet1 = factory.deployWallet();
console.log(address(wallet1));
console.log("hash wallet1: %s", uint256(_getCodeHash(wallet1)));
Wallet(wallet1).destroyMe();

vm.etch(wallet1, "");
console.log("hash wallet1 destroyed: %s", uint256(_getCodeHash(wallet1)));

wallet2 = factory.deployWallet();
console.log(address(wallet2));
assertEq(wallet1, wallet2);

the console.log works as expected, but the next deployWallet fails
May 25, 2023, 9:01 AM
Mmhh
May 25, 2023, 9:03 AM
I just ended doing until the selfdestruct in the setUp, dirty but it works for the test
May 26, 2023, 6:27 AM

© 2024 Draquery.com All rights reserved.