Merge pull request #10162 from filecoin-project/mikers/deployValueTest2

tests: itests: test creating a contract and sending value
This commit is contained in:
Łukasz Magiera 2023-02-09 18:00:15 +01:00 committed by GitHub
commit d84479bb05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 8 deletions

View File

@ -0,0 +1 @@
60806040523460405161001190610073565b6040518091039082f090508015801561002e573d6000803e3d6000fd5b506000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555061007f565b60c78061031683390190565b6102888061008e6000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80630e3608df146100465780634313b53114610064578063b0d22c6214610082575b600080fd5b61004e6100a0565b60405161005b919061017d565b60405180910390f35b61006c610137565b60405161007991906101d9565b60405180910390f35b61008a61015b565b604051610097919061017d565b60405180910390f35b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166312065fe06040518163ffffffff1660e01b8152600401602060405180830381865afa15801561010e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906101329190610225565b905090565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60006007905090565b6000819050919050565b61017781610164565b82525050565b6000602082019050610192600083018461016e565b92915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006101c382610198565b9050919050565b6101d3816101b8565b82525050565b60006020820190506101ee60008301846101ca565b92915050565b600080fd5b61020281610164565b811461020d57600080fd5b50565b60008151905061021f816101f9565b92915050565b60006020828403121561023b5761023a6101f4565b5b600061024984828501610210565b9150509291505056fea2646970667358221220c24abd10dbe58d92bfe62cb351771fcdc45d54241a8ce7085f2a75179c67cd8a64736f6c63430008110033608060405260b5806100126000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806312065fe014602d575b600080fd5b60336047565b604051603e91906066565b60405180910390f35b600047905090565b6000819050919050565b606081604f565b82525050565b6000602082019050607960008301846059565b9291505056fea26469706673582212207123972a300833ee01aebf99e4bdf8ecf9f01c0d3dd776048bd41803c6855c0e64736f6c63430008110033

View File

@ -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;
}
}

View File

@ -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

View File

@ -704,3 +704,29 @@ func TestFEVMRecursiveActorCallEstimate(t *testing.T) {
t.Run("n=50", testN(50))
t.Run("n=100", testN(100))
}
// TestFEVM deploys a contract while sending value to it
func TestFEVMDeployWithValue(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().DeployContractFromFilenameWithValue(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)
}

View File

@ -43,19 +43,18 @@ func (f *TestFullNode) EVM() *EVM {
return &EVM{f}
}
func (e *EVM) DeployContract(ctx context.Context, sender address.Address, bytecode []byte) eam.CreateReturn {
var err error
func (e *EVM) DeployContractWithValue(ctx context.Context, sender address.Address, bytecode []byte, value big.Int) eam.CreateReturn {
require := require.New(e.t)
method := builtintypes.MethodsEAM.CreateExternal
initcode := abi.CborBytes(bytecode)
params, err := actors.SerializeParams(&initcode)
require.NoError(err)
params, errActors := actors.SerializeParams(&initcode)
require.NoError(errActors)
msg := &types.Message{
To: builtintypes.EthereumAddressManagerActorAddr,
From: sender,
Value: big.Zero(),
Value: value,
Method: method,
Params: params,
}
@ -77,8 +76,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.DeployContractWithValue(ctx, sender, bytecode, big.Zero())
}
func (e *EVM) DeployContractFromFilename(ctx context.Context, binFilename string) (address.Address, address.Address) {
func (e *EVM) DeployContractFromFilenameWithValue(ctx context.Context, binFilename string, value big.Int) (address.Address, address.Address) {
contractHex, err := os.ReadFile(binFilename)
require.NoError(e.t, err)
@ -91,12 +93,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.DeployContractWithValue(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.DeployContractFromFilenameWithValue(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...)