Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Nov 17, 2023
1 parent e22ba5f commit 3720746
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 1 deletion.
20 changes: 19 additions & 1 deletion itests/eth_hash_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
109 changes: 109 additions & 0 deletions itests/eth_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

0 comments on commit 3720746

Please sign in to comment.