From 3adafea574d127618833d9e15d5ca7859d135240 Mon Sep 17 00:00:00 2001 From: Mike Seiler Date: Wed, 1 Feb 2023 06:51:32 +0000 Subject: [PATCH] merge --- itests/contracts/DeployValueTest.hex | 1 + itests/contracts/DeployValueTest.sol | 29 ++++++++++++++++++++++++++++ itests/contracts/compile.sh | 2 +- itests/fevm_test.go | 26 +++++++++++++++++++++++++ itests/kit/evm.go | 14 ++++++++++---- 5 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 itests/contracts/DeployValueTest.hex create mode 100644 itests/contracts/DeployValueTest.sol diff --git a/itests/contracts/DeployValueTest.hex b/itests/contracts/DeployValueTest.hex new file mode 100644 index 000000000..535704abf --- /dev/null +++ b/itests/contracts/DeployValueTest.hex @@ -0,0 +1 @@ +60806040523460405161001190610073565b6040518091039082f090508015801561002e573d6000803e3d6000fd5b506000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555061007f565b60c78061031683390190565b6102888061008e6000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80630e3608df146100465780634313b53114610064578063b0d22c6214610082575b600080fd5b61004e6100a0565b60405161005b919061017d565b60405180910390f35b61006c610137565b60405161007991906101d9565b60405180910390f35b61008a61015b565b604051610097919061017d565b60405180910390f35b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166312065fe06040518163ffffffff1660e01b8152600401602060405180830381865afa15801561010e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906101329190610225565b905090565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60006007905090565b6000819050919050565b61017781610164565b82525050565b6000602082019050610192600083018461016e565b92915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006101c382610198565b9050919050565b6101d3816101b8565b82525050565b60006020820190506101ee60008301846101ca565b92915050565b600080fd5b61020281610164565b811461020d57600080fd5b50565b60008151905061021f816101f9565b92915050565b60006020828403121561023b5761023a6101f4565b5b600061024984828501610210565b9150509291505056fea2646970667358221220c24abd10dbe58d92bfe62cb351771fcdc45d54241a8ce7085f2a75179c67cd8a64736f6c63430008110033608060405260b5806100126000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806312065fe014602d575b600080fd5b60336047565b604051603e91906066565b60405180910390f35b600047905090565b6000819050919050565b606081604f565b82525050565b6000602082019050607960008301846059565b9291505056fea26469706673582212207123972a300833ee01aebf99e4bdf8ecf9f01c0d3dd776048bd41803c6855c0e64736f6c63430008110033 \ No newline at end of file diff --git a/itests/contracts/DeployValueTest.sol b/itests/contracts/DeployValueTest.sol new file mode 100644 index 000000000..fe261b014 --- /dev/null +++ b/itests/contracts/DeployValueTest.sol @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.17; + + + +contract DeployValueTest { + address public newContract; + + constructor() payable { + newContract = address(new NewContract{value: msg.value}()); + } + + function getConst() public view returns (uint) { + return 7; + } + + function getNewContractBalance() public view returns (uint) { + return NewContract(newContract).getBalance(); + } +} + +contract NewContract { + constructor() payable { + } + + function getBalance() public view returns (uint) { + return address(this).balance; + } +} diff --git a/itests/contracts/compile.sh b/itests/contracts/compile.sh index 4395fdb53..b87c830d7 100755 --- a/itests/contracts/compile.sh +++ b/itests/contracts/compile.sh @@ -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 ; do +for filename in Constructor TestApp ValueSender DeployValueTest; do echo $filename solc --bin $filename.sol | tail -n5|head -n1 | tr -d "\n" > $filename.hex done diff --git a/itests/fevm_test.go b/itests/fevm_test.go index de11fd618..863b030ad 100644 --- a/itests/fevm_test.go +++ b/itests/fevm_test.go @@ -606,3 +606,29 @@ func TestFEVMRecursiveActorCall(t *testing.T) { t.Run("n=0,r=255-fails", testN(0, 255, exitcode.ExitCode(33))) // 33 means transaction reverted t.Run("n=251,r=171-fails", testN(251, 171, exitcode.ExitCode(33))) } + +// TestFEVM deploys a contract while sending value to it +func TestFEVMDeployValue(t *testing.T) { + ctx, cancel, client := kit.SetupFEVMTest(t) + defer cancel() + + //testValue is the amount sent when the contract is created + //at the end we check that the new contract has a balance of testValue + testValue := big.NewInt(20) + + // deploy DeployValueTest which creates NewContract + // testValue is sent to DeployValueTest and that amount is + // also sent to NewContract + filenameActor := "contracts/DeployValueTest.hex" + fromAddr, idAddr := client.EVM().DeployContractFromFilenameValue(ctx, filenameActor, testValue) + + //call getNewContractBalance to find the value of NewContract + ret, _, err := client.EVM().InvokeContractByFuncName(ctx, fromAddr, idAddr, "getNewContractBalance()", []byte{}) + require.NoError(t, err) + + contractBalance, err := decodeOutputToUint64(ret) + require.NoError(t, err) + + //require balance of NewContract is testValue + require.Equal(t, testValue.Uint64(), contractBalance) +} diff --git a/itests/kit/evm.go b/itests/kit/evm.go index 6c27da557..c24197a67 100644 --- a/itests/kit/evm.go +++ b/itests/kit/evm.go @@ -42,7 +42,7 @@ func (f *TestFullNode) EVM() *EVM { return &EVM{f} } -func (e *EVM) DeployContract(ctx context.Context, sender address.Address, bytecode []byte) eam.CreateReturn { +func (e *EVM) DeployContractValue(ctx context.Context, sender address.Address, bytecode []byte, value big.Int) eam.CreateReturn { require := require.New(e.t) nonce, err := e.MpoolGetNonce(ctx, sender) @@ -61,7 +61,7 @@ func (e *EVM) DeployContract(ctx context.Context, sender address.Address, byteco msg := &types.Message{ To: builtintypes.EthereumAddressManagerActorAddr, From: sender, - Value: big.Zero(), + Value: value, Method: method, Params: params, } @@ -83,8 +83,11 @@ func (e *EVM) DeployContract(ctx context.Context, sender address.Address, byteco return result } +func (e *EVM) DeployContract(ctx context.Context, sender address.Address, bytecode []byte) eam.CreateReturn { + return e.DeployContractValue(ctx, sender, bytecode, big.Zero()) +} -func (e *EVM) DeployContractFromFilename(ctx context.Context, binFilename string) (address.Address, address.Address) { +func (e *EVM) DeployContractFromFilenameValue(ctx context.Context, binFilename string, value big.Int) (address.Address, address.Address) { contractHex, err := os.ReadFile(binFilename) require.NoError(e.t, err) @@ -97,12 +100,15 @@ func (e *EVM) DeployContractFromFilename(ctx context.Context, binFilename string fromAddr, err := e.WalletDefaultAddress(ctx) require.NoError(e.t, err) - result := e.DeployContract(ctx, fromAddr, contract) + result := e.DeployContractValue(ctx, fromAddr, contract, value) idAddr, err := address.NewIDAddress(result.ActorID) require.NoError(e.t, err) return fromAddr, idAddr } +func (e *EVM) DeployContractFromFilename(ctx context.Context, binFilename string) (address.Address, address.Address) { + return e.DeployContractFromFilenameValue(ctx, binFilename, big.Zero()) +} func (e *EVM) InvokeSolidity(ctx context.Context, sender address.Address, target address.Address, selector []byte, inputData []byte) (*api.MsgLookup, error) { params := append(selector, inputData...)