fix send then deploy test

We changed how we compute addresses. It's still broken due to a bug in
the EAM, but the _test_ is now correct(ly failing).
This commit is contained in:
Steven Allen 2023-01-13 23:25:54 -08:00
parent 51d77d408b
commit b4714c6f3e

View File

@ -3,13 +3,14 @@ package itests
import (
"bytes"
"context"
"encoding/binary"
"encoding/hex"
"os"
"testing"
"time"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"golang.org/x/crypto/sha3"
"github.com/stretchr/testify/require"
@ -26,6 +27,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()
@ -45,18 +64,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)
@ -105,11 +117,11 @@ func TestAddressCreationBeforeDeploy(t *testing.T) {
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))
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)