From 3720746df0576170c9a7a114cba36af08521198b Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 17 Nov 2023 13:03:29 +0100 Subject: [PATCH] more tests --- itests/eth_hash_lookup_test.go | 20 +++++- itests/eth_transactions_test.go | 109 ++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 1 deletion(-) diff --git a/itests/eth_hash_lookup_test.go b/itests/eth_hash_lookup_test.go index 7699d688523..e1474a90d10 100644 --- a/itests/eth_hash_lookup_test.go +++ b/itests/eth_hash_lookup_test.go @@ -187,6 +187,18 @@ func TestTransactionHashLookupBlsFilecoinMessage(t *testing.T) { toEth, err := client.FilecoinAddressToEthAddress(ctx, toId) require.NoError(t, err) require.Equal(t, &toEth, chainTx.To) + + const expectedHex = "868e10c4" + + "0000000000000000000000000000000000000000000000000000000000000000" + + "0000000000000000000000000000000000000000000000000000000000000000" + + "0000000000000000000000000000000000000000000000000000000000000060" + + "0000000000000000000000000000000000000000000000000000000000000000" + + // verify that the params are correctly encoded. + expected, err := hex.DecodeString(expectedHex) + require.NoError(t, err) + + require.Equal(t, ethtypes.EthBytes(expected), chainTx.Input) } // TestTransactionHashLookupSecpFilecoinMessage tests to see if lotus can find a Secp Filecoin Message using the transaction hash @@ -267,8 +279,14 @@ func TestTransactionHashLookupSecpFilecoinMessage(t *testing.T) { require.NoError(t, err) require.Equal(t, &toEth, chainTx.To) + const expectedHex = "868e10c4" + + "0000000000000000000000000000000000000000000000000000000000000000" + + "0000000000000000000000000000000000000000000000000000000000000000" + + "0000000000000000000000000000000000000000000000000000000000000060" + + "0000000000000000000000000000000000000000000000000000000000000000" + // verify that the params are correctly encoded. - expected, err := hex.DecodeString("868e10c40000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000") + expected, err := hex.DecodeString(expectedHex) require.NoError(t, err) require.Equal(t, ethtypes.EthBytes(expected), chainTx.Input) diff --git a/itests/eth_transactions_test.go b/itests/eth_transactions_test.go index 916e3ef829c..488691f704b 100644 --- a/itests/eth_transactions_test.go +++ b/itests/eth_transactions_test.go @@ -9,10 +9,13 @@ import ( "github.com/stretchr/testify/require" + "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/big" + builtin2 "github.com/filecoin-project/go-state-types/builtin" "github.com/filecoin-project/go-state-types/manifest" "github.com/filecoin-project/lotus/build" + "github.com/filecoin-project/lotus/chain/actors" "github.com/filecoin-project/lotus/chain/store" "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types/ethtypes" @@ -374,3 +377,109 @@ func deployContractTx(ctx context.Context, client *kit.TestFullNode, ethAddr eth S: big.Zero(), }, nil } + +func TestEthTxFromNativeAccount(t *testing.T) { + blockTime := 10 * time.Millisecond + client, _, ens := kit.EnsembleMinimal(t, kit.MockProofs(), kit.ThroughRPC()) + + ens.InterconnectAll().BeginMining(blockTime) + + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + + msg := &types.Message{ + From: client.DefaultKey.Address, + To: client.DefaultKey.Address, + Value: abi.TokenAmount(types.MustParseFIL("100")), + Method: builtin2.MethodsEVM.InvokeContract, + } + + // Send a message with no input. + + sMsg, err := client.MpoolPushMessage(ctx, msg, nil) + require.NoError(t, err) + client.WaitMsg(ctx, sMsg.Cid()) + + hash, err := client.EthGetTransactionHashByCid(ctx, sMsg.Cid()) + require.NoError(t, err) + tx, err := client.EthGetTransactionByHash(ctx, hash) + require.NoError(t, err) + + // Expect empty input params given that we "invoked" the contract (well, invoked ourselves). + require.Equal(t, ethtypes.EthBytes{}, tx.Input) + + // Send a message with some input. + + input := abi.CborBytes([]byte{0x1, 0x2, 0x3, 0x4}) + msg.Params, err = actors.SerializeParams(&input) + require.NoError(t, err) + + sMsg, err = client.MpoolPushMessage(ctx, msg, nil) + require.NoError(t, err) + client.WaitMsg(ctx, sMsg.Cid()) + hash, err = client.EthGetTransactionHashByCid(ctx, sMsg.Cid()) + require.NoError(t, err) + tx, err = client.EthGetTransactionByHash(ctx, hash) + require.NoError(t, err) + + // Expect the decoded input. + require.EqualValues(t, input, tx.Input) + + // Invoke the contract, but with incorrectly encoded input. We expect this to be abi-encoded + // as if it were any other method call. + + msg.Params = input + require.NoError(t, err) + + sMsg, err = client.MpoolPushMessage(ctx, msg, nil) + require.NoError(t, err) + client.WaitMsg(ctx, sMsg.Cid()) + hash, err = client.EthGetTransactionHashByCid(ctx, sMsg.Cid()) + require.NoError(t, err) + tx, err = client.EthGetTransactionByHash(ctx, hash) + require.NoError(t, err) + + const expectedHex1 = "868e10c4" + // "handle filecoin method" function selector + // InvokeEVM method number + "00000000000000000000000000000000000000000000000000000000e525aa15" + + // CBOR multicodec (0x51) + "0000000000000000000000000000000000000000000000000000000000000051" + + // Offset + "0000000000000000000000000000000000000000000000000000000000000060" + + // Number of bytes in the input (4) + "0000000000000000000000000000000000000000000000000000000000000004" + + // Input: 1, 2, 3, 4 + "0102030400000000000000000000000000000000000000000000000000000000" + + input, err = hex.DecodeString(expectedHex1) + require.NoError(t, err) + require.EqualValues(t, input, tx.Input) + + // Invoke a random method with the same input. We expect the same result as above, but with + // a different method number. + + msg.Method += 1 + + sMsg, err = client.MpoolPushMessage(ctx, msg, nil) + require.NoError(t, err) + client.WaitMsg(ctx, sMsg.Cid()) + hash, err = client.EthGetTransactionHashByCid(ctx, sMsg.Cid()) + require.NoError(t, err) + tx, err = client.EthGetTransactionByHash(ctx, hash) + require.NoError(t, err) + + const expectedHex2 = "868e10c4" + // "handle filecoin method" function selector + // InvokeEVM+1 + "00000000000000000000000000000000000000000000000000000000e525aa16" + + // CBOR multicodec (0x51) + "0000000000000000000000000000000000000000000000000000000000000051" + + // Offset + "0000000000000000000000000000000000000000000000000000000000000060" + + // Number of bytes in the input (4) + "0000000000000000000000000000000000000000000000000000000000000004" + + // Input: 1, 2, 3, 4 + "0102030400000000000000000000000000000000000000000000000000000000" + input, err = hex.DecodeString(expectedHex2) + require.NoError(t, err) + require.EqualValues(t, input, tx.Input) +}