From f22762d94dab9840a8cdb4fea1c5b7449dedf8fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Kripalani?= Date: Tue, 15 Nov 2022 13:57:36 +0000 Subject: [PATCH] create EVM utilities in itest framework. --- itests/fevm_test.go | 92 ++---------------------------------------- itests/kit/evm.go | 97 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 88 deletions(-) create mode 100644 itests/kit/evm.go diff --git a/itests/fevm_test.go b/itests/fevm_test.go index 662b89763..ae816e8b3 100644 --- a/itests/fevm_test.go +++ b/itests/fevm_test.go @@ -3,7 +3,6 @@ package itests import ( "bytes" "context" - "encoding/binary" "encoding/hex" "os" "testing" @@ -13,13 +12,6 @@ import ( cbg "github.com/whyrusleeping/cbor-gen" "github.com/filecoin-project/go-address" - "github.com/filecoin-project/go-state-types/abi" - "github.com/filecoin-project/go-state-types/big" - builtintypes "github.com/filecoin-project/go-state-types/builtin" - "github.com/filecoin-project/go-state-types/builtin/v10/eam" - - "github.com/filecoin-project/lotus/chain/actors" - "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/itests/kit" ) @@ -46,43 +38,7 @@ func TestFEVMBasic(t *testing.T) { fromAddr, err := client.WalletDefaultAddress(ctx) require.NoError(t, err) - nonce, err := client.MpoolGetNonce(ctx, fromAddr) - if err != nil { - nonce = 0 // assume a zero nonce on error (e.g. sender doesn't exist). - } - - var salt [32]byte - binary.BigEndian.PutUint64(salt[:], nonce) - - method := builtintypes.MethodsEAM.Create2 - params, err := actors.SerializeParams(&eam.Create2Params{ - Initcode: contract, - Salt: salt, - }) - require.NoError(t, err) - - msg := &types.Message{ - To: builtintypes.EthereumAddressManagerActorAddr, - From: fromAddr, - Value: big.Zero(), - Method: method, - Params: params, - } - - t.Log("sending create message") - smsg, err := client.MpoolPushMessage(ctx, msg, nil) - require.NoError(t, err) - - t.Log("waiting for message to execute") - wait, err := client.StateWaitMsg(ctx, smsg.Cid(), 0, 0, false) - require.NoError(t, err) - - require.True(t, wait.Receipt.ExitCode.IsSuccess(), "contract installation failed") - - var result eam.CreateReturn - r := bytes.NewReader(wait.Receipt.Return) - err = result.UnmarshalCBOR(r) - require.NoError(t, err) + result := client.EVM().DeployContract(ctx, fromAddr, contract) idAddr, err := address.NewIDAddress(result.ActorID) require.NoError(t, err) @@ -96,27 +52,7 @@ func TestFEVMBasic(t *testing.T) { inputData, err := hex.DecodeString("000000000000000000000000ff00000000000000000000000000000000000064") require.NoError(t, err) - params := append(entryPoint, inputData...) - var buffer bytes.Buffer - err = cbg.WriteByteArray(&buffer, params) - require.NoError(t, err) - params = buffer.Bytes() - - msg := &types.Message{ - To: idAddr, - From: fromAddr, - Value: big.Zero(), - Method: abi.MethodNum(2), - Params: params, - } - - t.Log("sending invoke message") - smsg, err := client.MpoolPushMessage(ctx, msg, nil) - require.NoError(t, err) - - t.Log("waiting for message to execute") - wait, err := client.StateWaitMsg(ctx, smsg.Cid(), 0, 0, false) - require.NoError(t, err) + wait := client.EVM().InvokeSolidity(ctx, fromAddr, idAddr, entryPoint, inputData) require.True(t, wait.Receipt.ExitCode.IsSuccess(), "contract execution failed") @@ -135,28 +71,8 @@ func TestFEVMBasic(t *testing.T) { inputData, err := hex.DecodeString("000000000000000000000000ff00000000000000000000000000000000000065") require.NoError(t, err) - - params := append(entryPoint, inputData...) - var buffer bytes.Buffer - err = cbg.WriteByteArray(&buffer, params) - require.NoError(t, err) - params = buffer.Bytes() - - msg := &types.Message{ - To: idAddr, - From: fromAddr, - Value: big.Zero(), - Method: abi.MethodNum(2), - Params: params, - } - - t.Log("sending invoke message") - smsg, err := client.MpoolPushMessage(ctx, msg, nil) - require.NoError(t, err) - - t.Log("waiting for message to execute") - wait, err := client.StateWaitMsg(ctx, smsg.Cid(), 0, 0, false) - require.NoError(t, err) + + wait := client.EVM().InvokeSolidity(ctx, fromAddr, idAddr, entryPoint, inputData) require.True(t, wait.Receipt.ExitCode.IsSuccess(), "contract execution failed") diff --git a/itests/kit/evm.go b/itests/kit/evm.go new file mode 100644 index 000000000..ca5130593 --- /dev/null +++ b/itests/kit/evm.go @@ -0,0 +1,97 @@ +package kit + +import ( + "bytes" + "context" + "encoding/binary" + + "github.com/filecoin-project/go-address" + "github.com/filecoin-project/go-state-types/abi" + "github.com/filecoin-project/go-state-types/big" + builtintypes "github.com/filecoin-project/go-state-types/builtin" + "github.com/filecoin-project/go-state-types/builtin/v10/eam" + "github.com/filecoin-project/lotus/api" + "github.com/filecoin-project/lotus/chain/actors" + "github.com/filecoin-project/lotus/chain/types" + "github.com/stretchr/testify/require" + cbg "github.com/whyrusleeping/cbor-gen" +) + +// EVM groups EVM-related actions. +type EVM struct{ *TestFullNode } + +func (f *TestFullNode) EVM() *EVM { + return &EVM{f} +} + +func (e *EVM) DeployContract(ctx context.Context, sender address.Address, bytecode []byte) eam.CreateReturn { + require := require.New(e.t) + + nonce, err := e.MpoolGetNonce(ctx, sender) + if err != nil { + nonce = 0 // assume a zero nonce on error (e.g. sender doesn't exist). + } + + var salt [32]byte + binary.BigEndian.PutUint64(salt[:], nonce) + + method := builtintypes.MethodsEAM.Create2 + params, err := actors.SerializeParams(&eam.Create2Params{ + Initcode: bytecode, + Salt: salt, + }) + require.NoError(err) + + msg := &types.Message{ + To: builtintypes.EthereumAddressManagerActorAddr, + From: sender, + Value: big.Zero(), + Method: method, + Params: params, + } + + e.t.Log("sending create message") + smsg, err := e.MpoolPushMessage(ctx, msg, nil) + require.NoError(err) + + e.t.Log("waiting for message to execute") + wait, err := e.StateWaitMsg(ctx, smsg.Cid(), 0, 0, false) + require.NoError(err) + + require.True(wait.Receipt.ExitCode.IsSuccess(), "contract installation failed") + + var result eam.CreateReturn + r := bytes.NewReader(wait.Receipt.Return) + err = result.UnmarshalCBOR(r) + require.NoError(err) + + return result +} + +func (e *EVM) InvokeSolidity(ctx context.Context, sender address.Address, target address.Address, selector []byte, inputData []byte) *api.MsgLookup { + require := require.New(e.t) + + params := append(selector, inputData...) + var buffer bytes.Buffer + err := cbg.WriteByteArray(&buffer, params) + require.NoError(err) + params = buffer.Bytes() + + msg := &types.Message{ + To: target, + From: sender, + Value: big.Zero(), + Method: abi.MethodNum(2), + Params: params, + } + + e.t.Log("sending invoke message") + smsg, err := e.MpoolPushMessage(ctx, msg, nil) + require.NoError(err) + + e.t.Log("waiting for message to execute") + wait, err := e.StateWaitMsg(ctx, smsg.Cid(), 0, 0, false) + require.NoError(err) + + return wait +}