2022-12-09 17:57:53 +00:00
|
|
|
package itests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-12-15 21:38:48 +00:00
|
|
|
"fmt"
|
2022-12-09 17:57:53 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-12-11 00:27:00 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-12-09 17:57:53 +00:00
|
|
|
|
2022-12-11 00:27:00 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-12-09 17:57:53 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/exitcode"
|
2022-12-11 00:27:00 +00:00
|
|
|
|
2022-12-09 17:57:53 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2022-12-11 00:27:00 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin"
|
2022-12-09 17:57:53 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2022-12-16 18:43:55 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types/ethtypes"
|
2022-12-15 21:38:48 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/wallet/key"
|
2022-12-09 17:57:53 +00:00
|
|
|
"github.com/filecoin-project/lotus/itests/kit"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestEthAccountAbstraction goes over the account abstraction workflow:
|
|
|
|
// - an embryo is created when it receives a message
|
|
|
|
// - the embryo turns into an EOA when it sends a message
|
|
|
|
func TestEthAccountAbstraction(t *testing.T) {
|
|
|
|
kit.QuietMiningLogs()
|
|
|
|
|
|
|
|
blockTime := 100 * 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()
|
|
|
|
|
2022-12-15 21:38:48 +00:00
|
|
|
secpKey, err := key.GenerateKey(types.KTDelegated)
|
2022-12-09 17:57:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-15 21:38:48 +00:00
|
|
|
embryoAddress, err := client.WalletImport(ctx, &secpKey.KeyInfo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fmt.Println(embryoAddress)
|
|
|
|
|
2022-12-09 17:57:53 +00:00
|
|
|
// create an embryo actor at the target address
|
|
|
|
msgCreateEmbryo := &types.Message{
|
|
|
|
From: client.DefaultKey.Address,
|
|
|
|
To: embryoAddress,
|
|
|
|
Value: abi.TokenAmount(types.MustParseFIL("100")),
|
|
|
|
}
|
|
|
|
smCreateEmbryo, err := client.MpoolPushMessage(ctx, msgCreateEmbryo, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
mLookup, err := client.StateWaitMsg(ctx, smCreateEmbryo.Cid(), 3, api.LookbackNoLimit, true)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, exitcode.Ok, mLookup.Receipt.ExitCode)
|
|
|
|
|
|
|
|
// confirm the embryo is an embryo
|
|
|
|
embryoActor, err := client.StateGetActor(ctx, embryoAddress, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-19 18:50:21 +00:00
|
|
|
require.Equal(t, uint64(0), embryoActor.Nonce)
|
2022-12-11 00:27:00 +00:00
|
|
|
require.True(t, builtin.IsEmbryoActor(embryoActor.Code))
|
2022-12-09 17:57:53 +00:00
|
|
|
|
|
|
|
// send a message from the embryo address
|
|
|
|
msgFromEmbryo := &types.Message{
|
|
|
|
From: embryoAddress,
|
|
|
|
// self-send because an "eth tx payload" can't be to a filecoin address?
|
|
|
|
To: embryoAddress,
|
|
|
|
}
|
|
|
|
msgFromEmbryo, err = client.GasEstimateMessageGas(ctx, msgFromEmbryo, nil, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-16 18:43:55 +00:00
|
|
|
txArgs, err := ethtypes.NewEthTxArgsFromMessage(msgFromEmbryo)
|
2022-12-15 21:38:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
digest, err := txArgs.OriginalRlpMsg()
|
|
|
|
require.NoError(t, err)
|
2022-12-09 17:57:53 +00:00
|
|
|
|
2022-12-15 21:38:48 +00:00
|
|
|
siggy, err := client.WalletSign(ctx, embryoAddress, digest)
|
|
|
|
require.NoError(t, err)
|
2022-12-09 17:57:53 +00:00
|
|
|
|
2022-12-15 21:38:48 +00:00
|
|
|
smFromEmbryoCid, err := client.MpoolPush(ctx, &types.SignedMessage{Message: *msgFromEmbryo, Signature: *siggy})
|
2022-12-09 17:57:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-15 21:38:48 +00:00
|
|
|
mLookup, err = client.StateWaitMsg(ctx, smFromEmbryoCid, 3, api.LookbackNoLimit, true)
|
2022-12-09 17:57:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, exitcode.Ok, mLookup.Receipt.ExitCode)
|
|
|
|
|
2022-12-11 00:27:00 +00:00
|
|
|
// confirm ugly Embryo duckling has turned into a beautiful EthAccount swan
|
2022-12-09 17:57:53 +00:00
|
|
|
|
|
|
|
eoaActor, err := client.StateGetActor(ctx, embryoAddress, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-11 00:27:00 +00:00
|
|
|
require.False(t, builtin.IsEmbryoActor(eoaActor.Code))
|
|
|
|
require.True(t, builtin.IsEthAccountActor(eoaActor.Code))
|
2022-12-19 18:50:21 +00:00
|
|
|
require.Equal(t, uint64(1), eoaActor.Nonce)
|
2022-12-11 00:27:00 +00:00
|
|
|
|
|
|
|
// Send another message, it should succeed without any code CID changes
|
|
|
|
|
|
|
|
msgFromEmbryo = &types.Message{
|
2022-12-19 18:50:21 +00:00
|
|
|
From: embryoAddress,
|
|
|
|
To: embryoAddress,
|
|
|
|
Nonce: 1,
|
2022-12-11 00:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msgFromEmbryo, err = client.GasEstimateMessageGas(ctx, msgFromEmbryo, nil, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-16 18:43:55 +00:00
|
|
|
txArgs, err = ethtypes.NewEthTxArgsFromMessage(msgFromEmbryo)
|
2022-12-15 21:38:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
digest, err = txArgs.OriginalRlpMsg()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
siggy, err = client.WalletSign(ctx, embryoAddress, digest)
|
|
|
|
require.NoError(t, err)
|
2022-12-11 00:27:00 +00:00
|
|
|
|
2022-12-15 21:38:48 +00:00
|
|
|
smFromEmbryoCid, err = client.MpoolPush(ctx, &types.SignedMessage{Message: *msgFromEmbryo, Signature: *siggy})
|
2022-12-11 00:27:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-15 21:38:48 +00:00
|
|
|
mLookup, err = client.StateWaitMsg(ctx, smFromEmbryoCid, 3, api.LookbackNoLimit, true)
|
2022-12-11 00:27:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, exitcode.Ok, mLookup.Receipt.ExitCode)
|
|
|
|
|
|
|
|
// confirm no changes in code CID
|
|
|
|
|
|
|
|
eoaActor, err = client.StateGetActor(ctx, embryoAddress, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
2022-12-19 18:50:21 +00:00
|
|
|
require.Equal(t, uint64(2), eoaActor.Nonce)
|
2022-12-11 00:27:00 +00:00
|
|
|
|
|
|
|
require.False(t, builtin.IsEmbryoActor(eoaActor.Code))
|
|
|
|
require.True(t, builtin.IsEthAccountActor(eoaActor.Code))
|
2022-12-09 17:57:53 +00:00
|
|
|
}
|