Skip to content

Commit

Permalink
Merge pull request #10163 from filecoin-project/mikers/create2test2
Browse files Browse the repository at this point in the history
tests: itests: test create2 and destroy
  • Loading branch information
magik6k authored Feb 9, 2023
2 parents 83a7d6e + c98093b commit 4f5fea8
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
1 change: 1 addition & 0 deletions itests/contracts/Create2Factory.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
608060405234801561001057600080fd5b506109fb806100206000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c80632b85ba3814610051578063732768dd146100815780637c3e4053146100b2578063bb29998e146100e2575b600080fd5b61006b60048036038101906100669190610473565b610112565b60405161007891906104e1565b60405180910390f35b61009b60048036038101906100969190610473565b61013f565b6040516100a9929190610517565b60405180910390f35b6100cc60048036038101906100c79190610473565b61027e565b6040516100d991906104e1565b60405180910390f35b6100fc60048036038101906100f7919061056c565b6102c0565b60405161010991906104e1565b60405180910390f35b6000816000819055506000806101278461013f565b915091508161013557600080fd5b8092505050919050565b60008060008360405160240161015591906105a8565b6040516020818303038152906040527f7c3e4053000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff838183161783525050505090506000803073ffffffffffffffffffffffffffffffffffffffff16836040516101fc9190610634565b600060405180830381855af49150503d8060008114610237576040519150601f19603f3d011682016040523d82523d6000602084013e61023c565b606091505b5091509150811561026e5760008180602001905181019061025d9190610689565b905082819550955050505050610279565b816000945094505050505b915091565b6000818260405161028e9061042b565b61029891906105a8565b8190604051809103906000f59050801580156102b8573d6000803e3d6000fd5b509050919050565b6000808290508073ffffffffffffffffffffffffffffffffffffffff166383197ef06040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561030e57600080fd5b505af1158015610322573d6000803e3d6000fd5b5050505060008173ffffffffffffffffffffffffffffffffffffffff166367e404ce6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610373573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061039791906106cb565b905060006104128373ffffffffffffffffffffffffffffffffffffffff1663bfa0b1336040518163ffffffff1660e01b8152600401602060405180830381865afa1580156103e9573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061040d919061070d565b61013f565b509050801561042057600080fd5b819350505050919050565b61028b8061073b83390190565b600080fd5b6000819050919050565b6104508161043d565b811461045b57600080fd5b50565b60008135905061046d81610447565b92915050565b60006020828403121561048957610488610438565b5b60006104978482850161045e565b91505092915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006104cb826104a0565b9050919050565b6104db816104c0565b82525050565b60006020820190506104f660008301846104d2565b92915050565b60008115159050919050565b610511816104fc565b82525050565b600060408201905061052c6000830185610508565b61053960208301846104d2565b9392505050565b610549816104c0565b811461055457600080fd5b50565b60008135905061056681610540565b92915050565b60006020828403121561058257610581610438565b5b600061059084828501610557565b91505092915050565b6105a28161043d565b82525050565b60006020820190506105bd6000830184610599565b92915050565b600081519050919050565b600081905092915050565b60005b838110156105f75780820151818401526020810190506105dc565b60008484015250505050565b600061060e826105c3565b61061881856105ce565b93506106288185602086016105d9565b80840191505092915050565b60006106408284610603565b915081905092915050565b6000610656826104a0565b9050919050565b6106668161064b565b811461067157600080fd5b50565b6000815190506106838161065d565b92915050565b60006020828403121561069f5761069e610438565b5b60006106ad84828501610674565b91505092915050565b6000815190506106c581610540565b92915050565b6000602082840312156106e1576106e0610438565b5b60006106ef848285016106b6565b91505092915050565b60008151905061070781610447565b92915050565b60006020828403121561072357610722610438565b5b6000610731848285016106f8565b9150509291505056fe608060405234801561001057600080fd5b5060405161028b38038061028b833981810160405281019061003291906100ba565b326000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555080600181905550506100e7565b600080fd5b6000819050919050565b61009781610084565b81146100a257600080fd5b50565b6000815190506100b48161008e565b92915050565b6000602082840312156100d0576100cf61007f565b5b60006100de848285016100a5565b91505092915050565b610195806100f66000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c806367e404ce1461004657806383197ef014610064578063bfa0b1331461006e575b600080fd5b61004e61008c565b60405161005b9190610110565b60405180910390f35b61006c6100b0565b005b6100766100c9565b6040516100839190610144565b60405180910390f35b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b3373ffffffffffffffffffffffffffffffffffffffff16ff5b60015481565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006100fa826100cf565b9050919050565b61010a816100ef565b82525050565b60006020820190506101256000830184610101565b92915050565b6000819050919050565b61013e8161012b565b82525050565b60006020820190506101596000830184610135565b9291505056fea26469706673582212208252a57fdfc00b722b7063f2d28dd8c5a36b469462c89da52bcf17ccdda2de6764736f6c63430008110033a26469706673582212200edf974d1735ee81f671df7e60300ab28168b8cb22cc3e59180ef743c4a526c964736f6c63430008110033
67 changes: 67 additions & 0 deletions itests/contracts/Create2Factory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.17;
contract Create2Factory {

bytes32 savedSalt;

// Returns the address of the newly deployed contract
function deploy(
bytes32 _salt
) public returns (address) {
// This syntax is a newer way to invoke create2 without assembly, you just need to pass salt
// https://docs.soliditylang.org/en/latest/control-structures.html#salted-contract-creations-create2
savedSalt = _salt;
(bool success, address ret) = deployDelegateCall(_salt);
require(success);
return ret;
}

function deployDelegateCall(
bytes32 _salt
) public returns (bool, address) {
bytes memory data = abi.encodeWithSignature("_deploy(bytes32)", _salt);
(bool success, bytes memory returnedData) = address(this).delegatecall(data);
if(success){
(address ret) = abi.decode(returnedData, (address));
return (success, ret);
}else{
return (success, address(0));
}
}

function _deploy(bytes32 _salt) public returns (address) {
// https://solidity-by-example.org/app/create1/
// This syntax is a newer way to invoke create2 without assembly, you just need to pass salt
// https://docs.soliditylang.org/en/latest/control-structures.html#salted-contract-creations-create2
return address(new SelfDestruct{salt: _salt}(_salt));
}

function test(address _address) public returns (address){

// run destroy() on _address
SelfDestruct selfDestruct = SelfDestruct(_address);
selfDestruct.destroy();

//verify data can still be accessed
address ret = selfDestruct.sender();

// attempt and fail to deploy contract using salt
(bool success, ) = deployDelegateCall(selfDestruct.salt());
require(!success);

return ret;
}
}

contract SelfDestruct {
address public sender;
bytes32 public salt;
constructor(bytes32 _salt) {
sender = tx.origin;
salt=_salt;
}
function destroy() public {
selfdestruct(payable(msg.sender));
}
}

2 changes: 1 addition & 1 deletion itests/contracts/compile.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ find . -name \*.sol -print0 |

#for these contracts we have 2 contracts in the same solidity file
#this command grabs the correct bytecode for us
for filename in Constructor TestApp ValueSender DeployValueTest; do
for filename in Constructor TestApp ValueSender Create2Factory DeployValueTest; do
echo $filename
solc --bin $filename.sol | tail -n5|head -n1 | tr -d "\n" > $filename.hex
done
Expand Down
56 changes: 56 additions & 0 deletions itests/fevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package itests
import (
"bytes"
"context"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"fmt"
Expand Down Expand Up @@ -730,3 +731,58 @@ func TestFEVMDeployWithValue(t *testing.T) {
//require balance of NewContract is testValue
require.Equal(t, testValue.Uint64(), contractBalance)
}

func TestFEVMDestroyCreate2(t *testing.T) {
ctx, cancel, client := kit.SetupFEVMTest(t)
defer cancel()

//deploy create2 factory contract
filename := "contracts/Create2Factory.hex"
fromAddr, idAddr := client.EVM().DeployContractFromFilename(ctx, filename)

//construct salt for create2
salt := make([]byte, 32)
_, err := rand.Read(salt)
require.NoError(t, err)

//deploy contract using create2 factory
selfDestructAddress, _, err := client.EVM().InvokeContractByFuncName(ctx, fromAddr, idAddr, "deploy(bytes32)", salt)
require.NoError(t, err)

//convert to filecoin actor address so we can call InvokeContractByFuncName
ea, err := ethtypes.CastEthAddress(selfDestructAddress[12:])
require.NoError(t, err)
selfDestructAddressActor, err := ea.ToFilecoinAddress()
require.NoError(t, err)

//read sender property from contract
ret, _, err := client.EVM().InvokeContractByFuncName(ctx, fromAddr, selfDestructAddressActor, "sender()", []byte{})
require.NoError(t, err)

//assert contract has correct data
ethFromAddr := inputDataFromFrom(ctx, t, client, fromAddr)
require.Equal(t, ethFromAddr, ret)

//run test() which 1.calls sefldestruct 2. verifies sender() is the correct value 3. attempts and fails to deploy via create2
testSenderAddress, _, err := client.EVM().InvokeContractByFuncName(ctx, fromAddr, idAddr, "test(address)", selfDestructAddress)
require.NoError(t, err)
require.Equal(t, testSenderAddress, ethFromAddr)

//read sender() but get response of 0x0 because of self destruct
senderAfterDestroy, _, err := client.EVM().InvokeContractByFuncName(ctx, fromAddr, selfDestructAddressActor, "sender()", []byte{})
require.NoError(t, err)
require.Equal(t, []byte{}, senderAfterDestroy)

// deploy new contract at same address usign same salt
newAddressSelfDestruct, _, err := client.EVM().InvokeContractByFuncName(ctx, fromAddr, idAddr, "deploy(bytes32)", salt)
require.NoError(t, err)
require.Equal(t, newAddressSelfDestruct, selfDestructAddress)

//verify sender() property is correct
senderSecondCall, _, err := client.EVM().InvokeContractByFuncName(ctx, fromAddr, selfDestructAddressActor, "sender()", []byte{})
require.NoError(t, err)

//assert contract has correct data
require.Equal(t, ethFromAddr, senderSecondCall)

}

0 comments on commit 4f5fea8

Please sign in to comment.