Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: itests: test creating a contract and sending value #10162

Merged
merged 3 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions itests/contracts/DeployValueTest.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
60806040523460405161001190610073565b6040518091039082f090508015801561002e573d6000803e3d6000fd5b506000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555061007f565b60c78061031683390190565b6102888061008e6000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80630e3608df146100465780634313b53114610064578063b0d22c6214610082575b600080fd5b61004e6100a0565b60405161005b919061017d565b60405180910390f35b61006c610137565b60405161007991906101d9565b60405180910390f35b61008a61015b565b604051610097919061017d565b60405180910390f35b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166312065fe06040518163ffffffff1660e01b8152600401602060405180830381865afa15801561010e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906101329190610225565b905090565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60006007905090565b6000819050919050565b61017781610164565b82525050565b6000602082019050610192600083018461016e565b92915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006101c382610198565b9050919050565b6101d3816101b8565b82525050565b60006020820190506101ee60008301846101ca565b92915050565b600080fd5b61020281610164565b811461020d57600080fd5b50565b60008151905061021f816101f9565b92915050565b60006020828403121561023b5761023a6101f4565b5b600061024984828501610210565b9150509291505056fea2646970667358221220c24abd10dbe58d92bfe62cb351771fcdc45d54241a8ce7085f2a75179c67cd8a64736f6c63430008110033608060405260b5806100126000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806312065fe014602d575b600080fd5b60336047565b604051603e91906066565b60405180910390f35b600047905090565b6000819050919050565b606081604f565b82525050565b6000602082019050607960008301846059565b9291505056fea26469706673582212207123972a300833ee01aebf99e4bdf8ecf9f01c0d3dd776048bd41803c6855c0e64736f6c63430008110033
29 changes: 29 additions & 0 deletions itests/contracts/DeployValueTest.sol
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion itests/contracts/compile.sh
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 ; 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
Expand Down
26 changes: 26 additions & 0 deletions itests/fevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 10 additions & 4 deletions itests/kit/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
}
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be named DeployContractFromFileWithValue.

I think all the functions here should end with WithValue instead of just Value. I think the latter makes sense to us, but is probably ambiguous to anyone looking at this code fresh.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of variants here -- looks like an API cartesian product situation. This is where the functional options pattern comes in handy: https://golang.cafe/blog/golang-functional-options-pattern.html.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @raulk @geoff-vball ! This feedback is super helpful! I have updated the PR to use WithValue

contractHex, err := os.ReadFile(binFilename)
require.NoError(e.t, err)

Expand All @@ -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...)
Expand Down