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

update evm actor creation for eam refactor #10005

Merged
merged 7 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Binary file modified build/actors/v10.tar.zst
Binary file not shown.
Binary file modified build/actors/v8.tar.zst
Binary file not shown.
Binary file modified build/actors/v9.tar.zst
Binary file not shown.
715 changes: 335 additions & 380 deletions build/builtin_actors_gen.go

Large diffs are not rendered by default.

23 changes: 7 additions & 16 deletions chain/types/ethtypes/eth_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

"github.com/filecoin-project/go-address"
gocrypto "github.com/filecoin-project/go-crypto"
"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"
typescrypto "github.com/filecoin-project/go-state-types/crypto"

"github.com/filecoin-project/lotus/build"
Expand Down Expand Up @@ -71,16 +71,12 @@ func EthTxArgsFromMessage(msg *types.Message) (EthTxArgs, error) {

if msg.To == builtintypes.EthereumAddressManagerActorAddr {
switch msg.Method {
// TODO: Uncomment
//case builtintypes.MethodsEAM.CreateExternal:
case builtintypes.MethodsEAM.Create:
// TODO: Uncomment
// var create eam.CreateExternalParams
var create eam.CreateParams
case builtintypes.MethodsEAM.CreateExternal:
var create abi.CborBytes
if err := create.UnmarshalCBOR(paramsReader); err != nil {
return EthTxArgs{}, err
}
params = create.Initcode
params = create
default:
return EthTxArgs{}, fmt.Errorf("unsupported EAM method")
}
Expand Down Expand Up @@ -142,17 +138,12 @@ func (tx *EthTxArgs) ToUnsignedMessage(from address.Address) (*types.Message, er
// nil indicates the EAM, only CreateExternal is allowed
if tx.To == nil {
to = builtintypes.EthereumAddressManagerActorAddr
// TODO: Uncomment
//method = builtintypes.MethodsEAM.CreateExternal
method = builtintypes.MethodsEAM.Create
method = builtintypes.MethodsEAM.CreateExternal
if len(tx.Input) == 0 {
return nil, xerrors.New("cannot call CreateExternal without params")
}
// TODO: CreateExternalParams, it doesn't have a nonce
params, err = actors.SerializeParams(&eam.CreateParams{
Initcode: tx.Input,
Nonce: uint64(tx.Nonce),
})
inputParams := abi.CborBytes(tx.Input)
params, err = actors.SerializeParams(&inputParams)
if err != nil {
return nil, fmt.Errorf("failed to serialize Create params: %w", err)
}
Expand Down
18 changes: 9 additions & 9 deletions chain/types/ethtypes/eth_transactions_test.go

Large diffs are not rendered by default.

13 changes: 3 additions & 10 deletions cli/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,8 @@ var EvmDeployCmd = &cli.Command{
return err
}

nonce, err := api.MpoolGetNonce(ctx, fromAddr)
if err != nil {
nonce = 0 // assume a zero nonce on error (e.g. sender doesn't exist).
}

params, err := actors.SerializeParams(&eam.CreateParams{
Initcode: contract,
Nonce: nonce,
})
initcode := abi.CborBytes(contract)
params, err := actors.SerializeParams(&initcode)
if err != nil {
return fmt.Errorf("failed to serialize Create params: %w", err)
}
Expand All @@ -266,7 +259,7 @@ var EvmDeployCmd = &cli.Command{
To: builtintypes.EthereumAddressManagerActorAddr,
From: fromAddr,
Value: big.Zero(),
Method: builtintypes.MethodsEAM.Create,
Method: builtintypes.MethodsEAM.CreateExternal,
Params: params,
}

Expand Down
2 changes: 1 addition & 1 deletion extern/filecoin-ffi
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ require (
github.com/filecoin-project/go-legs v0.4.4
github.com/filecoin-project/go-padreader v0.0.1
github.com/filecoin-project/go-paramfetch v0.0.4
github.com/filecoin-project/go-state-types v0.10.0-alpha-9
github.com/filecoin-project/go-state-types v0.10.0-alpha-10
github.com/filecoin-project/go-statemachine v1.0.2
github.com/filecoin-project/go-statestore v0.2.0
github.com/filecoin-project/go-storedcounter v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ github.com/filecoin-project/go-state-types v0.1.0/go.mod h1:ezYnPf0bNkTsDibL/psS
github.com/filecoin-project/go-state-types v0.1.6/go.mod h1:UwGVoMsULoCK+bWjEdd/xLCvLAQFBC7EDT477SKml+Q=
github.com/filecoin-project/go-state-types v0.1.8/go.mod h1:UwGVoMsULoCK+bWjEdd/xLCvLAQFBC7EDT477SKml+Q=
github.com/filecoin-project/go-state-types v0.1.10/go.mod h1:UwGVoMsULoCK+bWjEdd/xLCvLAQFBC7EDT477SKml+Q=
github.com/filecoin-project/go-state-types v0.10.0-alpha-9 h1:Rriwh/Fs/hV15QqHuL47PkJMz4e8kLGRwgsdh+G+S5I=
github.com/filecoin-project/go-state-types v0.10.0-alpha-9/go.mod h1:FPgQE05BFwZxKw/vCuIaIrzfJKo4RPQQMMPGd43dAFI=
github.com/filecoin-project/go-state-types v0.10.0-alpha-10 h1:QUpSayVFUADlrtzCh7SDNlbuaNSlYPBR46Nt7WpFl9I=
github.com/filecoin-project/go-state-types v0.10.0-alpha-10/go.mod h1:FPgQE05BFwZxKw/vCuIaIrzfJKo4RPQQMMPGd43dAFI=
github.com/filecoin-project/go-statemachine v0.0.0-20200925024713-05bd7c71fbfe/go.mod h1:FGwQgZAt2Gh5mjlwJUlVB62JeYdo+if0xWxSEfBD9ig=
github.com/filecoin-project/go-statemachine v1.0.2 h1:421SSWBk8GIoCoWYYTE/d+qCWccgmRH0uXotXRDjUbc=
github.com/filecoin-project/go-statemachine v1.0.2/go.mod h1:jZdXXiHa61n4NmgWFG4w8tnqgvZVHYbJ3yW7+y8bF54=
Expand Down
55 changes: 33 additions & 22 deletions itests/fevm_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package itests
import (
"bytes"
"context"
"encoding/binary"
"encoding/hex"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
"golang.org/x/crypto/sha3"

"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"
Expand All @@ -24,6 +26,24 @@ import (
"github.com/filecoin-project/lotus/itests/kit"
)

func effectiveEthAddressForCreate(t *testing.T, sender address.Address) ethtypes.EthAddress {
switch sender.Protocol() {
case address.SECP256K1, address.BLS:
hasher := sha3.NewLegacyKeccak256()
hasher.Write(sender.Bytes())
addr, err := ethtypes.CastEthAddress(hasher.Sum(nil)[12:])
require.NoError(t, err)
return addr
case address.Delegated:
addr, err := ethtypes.EthAddressFromFilecoinAddress(sender)
require.NoError(t, err)
return addr
default:
require.FailNow(t, "unsupported protocol %d", sender.Protocol())
}
panic("unreachable")
}

func TestAddressCreationBeforeDeploy(t *testing.T) {
kit.QuietMiningLogs()

Expand All @@ -43,18 +63,11 @@ func TestAddressCreationBeforeDeploy(t *testing.T) {

fromAddr, err := client.WalletDefaultAddress(ctx)
require.NoError(t, err)
fromId, err := client.StateLookupID(ctx, fromAddr, types.EmptyTSK)
require.NoError(t, err)

senderEthAddr, err := ethtypes.EthAddressFromFilecoinAddress(fromId)
require.NoError(t, err)

var salt [32]byte
binary.BigEndian.PutUint64(salt[:], 1)

// Generate contract address before actually deploying contract
ethAddr, err := ethtypes.GetContractEthAddressFromCode(senderEthAddr, salt, contract)
require.NoError(t, err)
// We hash the f1/f3 address into the EVM's address space when deploying contracts from
// accounts.
effectiveEvmAddress := effectiveEthAddressForCreate(t, fromAddr)
ethAddr := client.EVM().ComputeContractAddress(effectiveEvmAddress, 1)

contractFilAddr, err := ethAddr.ToFilecoinAddress()
require.NoError(t, err)
Expand All @@ -76,18 +89,16 @@ func TestAddressCreationBeforeDeploy(t *testing.T) {
require.NoError(t, err)
require.Equal(t, exitcode.Ok, mLookup.Receipt.ExitCode)

// Check if actor at new address is an placeholder actor
// Check if actor at new address is a placeholder actor
actor, err := client.StateGetActor(ctx, contractFilAddr, types.EmptyTSK)
require.NoError(t, err)
require.True(t, builtin.IsPlaceholderActor(actor.Code))

// Create and deploy evm actor

method := builtintypes.MethodsEAM.Create2
params, err := actors.SerializeParams(&eam.Create2Params{
Initcode: contract,
Salt: salt,
})
method := builtintypes.MethodsEAM.CreateExternal
contractParams := abi.CborBytes(contract)
params, err := actors.SerializeParams(&contractParams)
require.NoError(t, err)

createMsg := &types.Message{
Expand All @@ -104,12 +115,12 @@ func TestAddressCreationBeforeDeploy(t *testing.T) {
require.NoError(t, err)
require.Equal(t, exitcode.Ok, wait.Receipt.ExitCode)

// Check if eth address returned from Create2 is the same as eth address predicted at the start
var create2Return eam.Create2Return
err = create2Return.UnmarshalCBOR(bytes.NewReader(wait.Receipt.Return))
// Check if eth address returned from CreateExternal is the same as eth address predicted at the start
var createExternalReturn eam.CreateExternalReturn
err = createExternalReturn.UnmarshalCBOR(bytes.NewReader(wait.Receipt.Return))
require.NoError(t, err)

createdEthAddr, err := ethtypes.CastEthAddress(create2Return.EthAddress[:])
createdEthAddr, err := ethtypes.CastEthAddress(createExternalReturn.EthAddress[:])
require.NoError(t, err)
require.Equal(t, ethAddr, createdEthAddr)

Expand Down
9 changes: 4 additions & 5 deletions itests/kit/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/filecoin-project/go-address"
amt4 "github.com/filecoin-project/go-amt-ipld/v4"
"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"
Expand Down Expand Up @@ -45,11 +46,9 @@ func (e *EVM) DeployContract(ctx context.Context, sender address.Address, byteco
var salt [32]byte
binary.BigEndian.PutUint64(salt[:], nonce)

method := builtintypes.MethodsEAM.Create2
params, err := actors.SerializeParams(&eam.Create2Params{
Initcode: bytecode,
Salt: salt,
})
method := builtintypes.MethodsEAM.CreateExternal
initcode := abi.CborBytes(bytecode)
params, err := actors.SerializeParams(&initcode)
require.NoError(err)

msg := &types.Message{
Expand Down
18 changes: 8 additions & 10 deletions node/impl/full/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,20 +675,13 @@ func (a *EthModule) ethCallToFilecoinMessage(ctx context.Context, tx ethtypes.Et
// this is a contract creation
to = builtintypes.EthereumAddressManagerActorAddr

nonce, err := a.Mpool.GetNonce(ctx, from, types.EmptyTSK)
if err != nil {
nonce = 0 // assume a zero nonce on error (e.g. sender doesn't exist).
}

params2, err := actors.SerializeParams(&eam.CreateParams{
Initcode: tx.Data,
Nonce: nonce,
})
initcode := abi.CborBytes(tx.Data)
params2, err := actors.SerializeParams(&initcode)
if err != nil {
return nil, fmt.Errorf("failed to serialize Create params: %w", err)
}
params = params2
method = builtintypes.MethodsEAM.Create
method = builtintypes.MethodsEAM.CreateExternal
} else {
addr, err := tx.To.ToFilecoinAddress()
if err != nil {
Expand Down Expand Up @@ -1488,6 +1481,11 @@ func newEthTxFromFilecoinMessage(ctx context.Context, smsg *types.SignedMessage,
var params eam.Create2Params
err = params.UnmarshalCBOR(bytes.NewReader(smsg.Message.Params))
input = params.Initcode
case builtintypes.MethodsEAM.CreateExternal:
toAddr = nil
var params abi.CborBytes
err = params.UnmarshalCBOR(bytes.NewReader(smsg.Message.Params))
input = []byte(params)
}
if err != nil {
return ethtypes.EthTx{}, err
Expand Down