create EVM utilities in itest framework.

This commit is contained in:
Raúl Kripalani 2022-11-15 13:57:36 +00:00
parent 43e2a2b451
commit f22762d94d
2 changed files with 101 additions and 88 deletions

View File

@ -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")
@ -136,27 +72,7 @@ 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")

97
itests/kit/evm.go Normal file
View File

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