2023-01-13 19:11:13 +00:00
|
|
|
package itests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-01-14 16:56:21 +00:00
|
|
|
"golang.org/x/crypto/sha3"
|
2023-01-13 19:11:13 +00:00
|
|
|
|
2023-01-14 16:56:21 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2023-01-13 19:11:13 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
|
|
|
builtintypes "github.com/filecoin-project/go-state-types/builtin"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types/ethtypes"
|
|
|
|
"github.com/filecoin-project/lotus/itests/kit"
|
|
|
|
)
|
|
|
|
|
2023-01-14 07:25:54 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-11-17 19:46:49 +00:00
|
|
|
func createAndDeploy(ctx context.Context, t *testing.T, client *kit.TestFullNode, fromAddr address.Address, contract []byte) *api.EthTxReceipt {
|
2023-02-01 05:13:13 +00:00
|
|
|
// Create and deploy evm actor
|
|
|
|
|
|
|
|
method := builtintypes.MethodsEAM.CreateExternal
|
|
|
|
contractParams := abi.CborBytes(contract)
|
|
|
|
params, actorsErr := actors.SerializeParams(&contractParams)
|
|
|
|
require.NoError(t, actorsErr)
|
|
|
|
|
|
|
|
createMsg := &types.Message{
|
|
|
|
To: builtintypes.EthereumAddressManagerActorAddr,
|
|
|
|
From: fromAddr,
|
|
|
|
Value: big.Zero(),
|
|
|
|
Method: method,
|
|
|
|
Params: params,
|
|
|
|
}
|
|
|
|
smsg, err := client.MpoolPushMessage(ctx, createMsg, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-11-17 19:46:49 +00:00
|
|
|
txHash, err := client.EthGetTransactionHashByCid(ctx, smsg.Cid())
|
2023-02-01 05:13:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-01-13 19:11:13 +00:00
|
|
|
|
2023-11-17 19:46:49 +00:00
|
|
|
receipt, err := client.EVM().WaitTransaction(ctx, *txHash)
|
2023-02-01 05:13:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-11-17 19:46:49 +00:00
|
|
|
require.EqualValues(t, ethtypes.EthUint64(0x1), receipt.Status)
|
|
|
|
return receipt
|
2023-02-01 05:13:13 +00:00
|
|
|
}
|
2023-01-13 19:11:13 +00:00
|
|
|
|
2023-02-01 05:13:13 +00:00
|
|
|
func TestAddressCreationBeforeDeploy(t *testing.T) {
|
|
|
|
ctx, cancel, client := kit.SetupFEVMTest(t)
|
2023-01-13 19:11:13 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// install contract
|
|
|
|
contractHex, err := os.ReadFile("contracts/SimpleCoin.hex")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
contract, err := hex.DecodeString(string(contractHex))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fromAddr, err := client.WalletDefaultAddress(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-01-14 07:25:54 +00:00
|
|
|
// 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)
|
2023-01-13 19:11:13 +00:00
|
|
|
|
|
|
|
contractFilAddr, err := ethAddr.ToFilecoinAddress()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-01 05:13:13 +00:00
|
|
|
//transfer half the wallet balance
|
2023-01-13 19:11:13 +00:00
|
|
|
bal, err := client.WalletBalance(ctx, client.DefaultKey.Address)
|
|
|
|
require.NoError(t, err)
|
|
|
|
sendAmount := big.Div(bal, big.NewInt(2))
|
2023-02-01 05:13:13 +00:00
|
|
|
client.EVM().TransferValueOrFail(ctx, fromAddr, contractFilAddr, sendAmount)
|
2023-01-13 19:11:13 +00:00
|
|
|
|
2023-01-14 16:20:24 +00:00
|
|
|
// Check if actor at new address is a placeholder actor
|
2023-01-13 19:11:13 +00:00
|
|
|
actor, err := client.StateGetActor(ctx, contractFilAddr, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, builtin.IsPlaceholderActor(actor.Code))
|
|
|
|
|
|
|
|
// Create and deploy evm actor
|
2023-11-17 19:46:49 +00:00
|
|
|
receipt := createAndDeploy(ctx, t, client, fromAddr, contract)
|
2023-01-13 19:11:13 +00:00
|
|
|
|
2023-11-17 19:46:49 +00:00
|
|
|
// Check if eth address returned from CreateExternal is the same as eth address predicted at
|
|
|
|
// the start
|
|
|
|
require.Equal(t, ðAddr, receipt.ContractAddress)
|
2023-02-01 05:13:13 +00:00
|
|
|
|
|
|
|
// Check if newly deployed actor still has funds
|
|
|
|
actorPostCreate, err := client.StateGetActor(ctx, contractFilAddr, types.EmptyTSK)
|
2023-01-13 19:11:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-02-01 05:13:13 +00:00
|
|
|
require.Equal(t, actorPostCreate.Balance, sendAmount)
|
|
|
|
require.True(t, builtin.IsEvmActor(actorPostCreate.Code))
|
2023-01-13 19:11:13 +00:00
|
|
|
|
2023-02-01 05:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeployAddressMultipleTimes(t *testing.T) {
|
|
|
|
ctx, cancel, client := kit.SetupFEVMTest(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// install contract
|
|
|
|
contractHex, err := os.ReadFile("contracts/SimpleCoin.hex")
|
2023-01-13 19:11:13 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-01 05:13:13 +00:00
|
|
|
contract, err := hex.DecodeString(string(contractHex))
|
2023-01-13 19:11:13 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-01 05:13:13 +00:00
|
|
|
fromAddr, err := client.WalletDefaultAddress(ctx)
|
2023-01-13 19:11:13 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-01 05:13:13 +00:00
|
|
|
// 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)
|
|
|
|
|
|
|
|
// Send contract address small funds to init
|
|
|
|
sendAmount := big.NewInt(2)
|
|
|
|
client.EVM().TransferValueOrFail(ctx, fromAddr, contractFilAddr, sendAmount)
|
|
|
|
|
|
|
|
// Check if actor at new address is a placeholder actor
|
|
|
|
actor, err := client.StateGetActor(ctx, contractFilAddr, types.EmptyTSK)
|
2023-01-13 19:11:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-02-01 05:13:13 +00:00
|
|
|
require.True(t, builtin.IsPlaceholderActor(actor.Code))
|
|
|
|
|
|
|
|
// Create and deploy evm actor
|
2023-11-17 19:46:49 +00:00
|
|
|
receipt := createAndDeploy(ctx, t, client, fromAddr, contract)
|
2023-02-01 05:13:13 +00:00
|
|
|
|
2023-11-17 19:46:49 +00:00
|
|
|
// Check if eth address returned from CreateExternal is the same as eth address predicted at
|
|
|
|
// the start
|
|
|
|
require.Equal(t, ðAddr, receipt.ContractAddress)
|
2023-01-13 19:11:13 +00:00
|
|
|
|
|
|
|
// Check if newly deployed actor still has funds
|
|
|
|
actorPostCreate, err := client.StateGetActor(ctx, contractFilAddr, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, actorPostCreate.Balance, sendAmount)
|
|
|
|
require.True(t, builtin.IsEvmActor(actorPostCreate.Code))
|
|
|
|
|
2023-02-01 05:13:13 +00:00
|
|
|
// Create and deploy evm actor
|
2023-11-17 19:46:49 +00:00
|
|
|
receipt = createAndDeploy(ctx, t, client, fromAddr, contract)
|
2023-02-01 05:13:13 +00:00
|
|
|
|
|
|
|
// Check that this time eth address returned from CreateExternal is not the same as eth address predicted at the start
|
2023-11-17 19:46:49 +00:00
|
|
|
require.NotEqual(t, ðAddr, receipt.ContractAddress)
|
2023-02-01 05:13:13 +00:00
|
|
|
|
2023-01-13 19:11:13 +00:00
|
|
|
}
|