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

chore: replace embryo with placeholder in test #9989

Closed
wants to merge 10 commits into from
Closed
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
Prev Previous commit
Next Next commit
Add contract invocation test
Shrenuj Bansal committed Jan 6, 2023
commit 0d675d19ba17541b4e14f400969b1cdd6224a3bc
151 changes: 125 additions & 26 deletions itests/eth_transactions_test.go
Original file line number Diff line number Diff line change
@@ -73,18 +73,8 @@ func TestValueTransferValidSignature(t *testing.T) {
client.EVM().SignTransaction(&tx, key.PrivateKey)

hash := client.EVM().SubmitTransaction(ctx, &tx)
fmt.Println(hash)

var receipt *api.EthTxReceipt
for i := 0; i < 10000000000; i++ {
receipt, err = client.EthGetTransactionReceipt(ctx, hash)
fmt.Println(receipt, err)
if err != nil || receipt == nil {
time.Sleep(500 * time.Millisecond)
continue
}
break
}
receipt, err := waitForEthTxReceipt(ctx, client, hash)
require.NoError(t, err)
require.NotNil(t, receipt)

@@ -101,6 +91,8 @@ func TestLegacyTransaction(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

// This is a legacy style transaction obtained from etherscan
// Tx details: https://etherscan.io/getRawTx?tx=0x0763262208d89efeeb50c8bb05b50c537903fe9d7bdef3b223fd1f5f69f69b32
txBytes, err := hex.DecodeString("f86f830131cf8504a817c800825208942cf1e5a8250ded8835694ebeb90cfa0237fcb9b1882ec4a5251d1100008026a0f5f8d2244d619e211eeb634acd1bea0762b7b4c97bba9f01287c82bfab73f911a015be7982898aa7cc6c6f27ff33e999e4119d6cd51330353474b98067ff56d930")
require.NoError(t, err)
_, err = client.EVM().EthSendRawTransaction(ctx, txBytes)
@@ -130,10 +122,6 @@ func TestContractDeploymentValidSignature(t *testing.T) {
// send some funds to the f410 address
kit.SendFunds(ctx, t, client, deployer, types.FromFil(10))

// verify balances.
//bal := client.EVM().AssertAddressBalanceConsistent(ctx, deployer)
//require.Equal(t, types.FromFil(10), bal)

// verify the deployer address is an embryo.
client.AssertActorType(ctx, deployer, manifest.EmbryoKey)

@@ -169,18 +157,8 @@ func TestContractDeploymentValidSignature(t *testing.T) {
client.EVM().SignTransaction(&tx, key.PrivateKey)

hash := client.EVM().SubmitTransaction(ctx, &tx)
fmt.Println(hash)

var receipt *api.EthTxReceipt
for i := 0; i < 10000000000; i++ {
receipt, err = client.EthGetTransactionReceipt(ctx, hash)
fmt.Println(receipt, err)
if err != nil || receipt == nil {
time.Sleep(500 * time.Millisecond)
continue
}
break
}
receipt, err := waitForEthTxReceipt(ctx, client, hash)
require.NoError(t, err)
require.NotNil(t, receipt)

@@ -198,3 +176,124 @@ func TestContractDeploymentValidSignature(t *testing.T) {
// Verify that the deployer is now an account.
client.AssertActorType(ctx, deployer, manifest.EthAccountKey)
}

func TestContractInvocation(t *testing.T) {
blockTime := 100 * time.Millisecond
client, _, ens := kit.EnsembleMinimal(t, kit.MockProofs(), kit.ThroughRPC())

ens.InterconnectAll().BeginMining(blockTime)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

// install contract
contractHex, err := os.ReadFile("./contracts/SimpleCoin.bin")
require.NoError(t, err)

contract, err := hex.DecodeString(string(contractHex))
require.NoError(t, err)

// create a new Ethereum account
key, ethAddr, deployer := client.EVM().NewAccount()
// send some funds to the f410 address
kit.SendFunds(ctx, t, client, deployer, types.FromFil(10))

// DEPLOY CONTRACT

gaslimit, err := client.EthEstimateGas(ctx, ethtypes.EthCall{
From: &ethAddr,
Data: contract,
})
require.NoError(t, err)

maxPriorityFeePerGas, err := client.EthMaxPriorityFeePerGas(ctx)
require.NoError(t, err)

// now deploy a contract from the embryo, and validate it went well
tx := ethtypes.EthTxArgs{
ChainID: build.Eip155ChainId,
Value: big.Zero(),
Nonce: 0,
MaxFeePerGas: types.NanoFil,
MaxPriorityFeePerGas: big.Int(maxPriorityFeePerGas),
GasLimit: int(gaslimit),
Input: contract,
V: big.Zero(),
R: big.Zero(),
S: big.Zero(),
}

client.EVM().SignTransaction(&tx, key.PrivateKey)

hash := client.EVM().SubmitTransaction(ctx, &tx)
fmt.Println(hash)

receipt, err := waitForEthTxReceipt(ctx, client, hash)
require.NoError(t, err)
require.NotNil(t, receipt)
require.EqualValues(t, ethtypes.EthUint64(0x1), receipt.Status)

// Get contract address.
contractAddr := client.EVM().ComputeContractAddress(ethAddr, 0)

// INVOKE CONTRACT

params, err := hex.DecodeString("f8b2cb4f000000000000000000000000ff00000000000000000000000000000000000064")
require.NoError(t, err)

gaslimit, err = client.EthEstimateGas(ctx, ethtypes.EthCall{
From: &ethAddr,
To: &contractAddr,
Data: params,
})
require.NoError(t, err)

maxPriorityFeePerGas, err = client.EthMaxPriorityFeePerGas(ctx)
require.NoError(t, err)

invokeTx := ethtypes.EthTxArgs{
ChainID: build.Eip155ChainId,
To: &contractAddr,
Value: big.Zero(),
Nonce: 1,
MaxFeePerGas: types.NanoFil,
MaxPriorityFeePerGas: big.Int(maxPriorityFeePerGas),
GasLimit: int(gaslimit),
Input: params,
V: big.Zero(),
R: big.Zero(),
S: big.Zero(),
}

unsigned, err := tx.ToRlpUnsignedMsg()
require.NoError(t, err)
// Submit transaction without signing message
_, err = client.EVM().EthSendRawTransaction(ctx, unsigned)
require.Error(t, err)

client.EVM().SignTransaction(&invokeTx, key.PrivateKey)

hash = client.EVM().SubmitTransaction(ctx, &invokeTx)

receipt, err = waitForEthTxReceipt(ctx, client, hash)
require.NoError(t, err)
require.NotNil(t, receipt)

// Success.
require.EqualValues(t, ethtypes.EthUint64(0x1), receipt.Status)

}

func waitForEthTxReceipt(ctx context.Context, client *kit.TestFullNode, hash ethtypes.EthHash) (*api.EthTxReceipt, error) {
var receipt *api.EthTxReceipt
var err error
for i := 0; i < 10000000000; i++ {
receipt, err = client.EthGetTransactionReceipt(ctx, hash)
if err != nil || receipt == nil {
time.Sleep(500 * time.Millisecond)
continue
}
break
}
return receipt, err
}