lotus/itests/eth_account_abstraction_test.go

132 lines
4.4 KiB
Go
Raw Normal View History

package itests
import (
"context"
2022-12-15 21:38:48 +00:00
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"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"
"github.com/filecoin-project/lotus/itests/kit"
)
// TestEthAccountAbstraction goes over the account abstraction workflow:
2023-01-10 03:23:55 +00:00
// - an placeholder is created when it receives a message
// - the placeholder 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)
require.NoError(t, err)
2023-01-10 03:23:55 +00:00
placeholderAddress, err := client.WalletImport(ctx, &secpKey.KeyInfo)
2022-12-15 21:38:48 +00:00
require.NoError(t, err)
2023-01-10 03:23:55 +00:00
fmt.Println(placeholderAddress)
2022-12-15 21:38:48 +00:00
2023-01-10 03:23:55 +00:00
// create an placeholder actor at the target address
msgCreatePlaceholder := &types.Message{
From: client.DefaultKey.Address,
2023-01-10 03:23:55 +00:00
To: placeholderAddress,
Value: abi.TokenAmount(types.MustParseFIL("100")),
}
2023-01-10 03:23:55 +00:00
smCreatePlaceholder, err := client.MpoolPushMessage(ctx, msgCreatePlaceholder, nil)
require.NoError(t, err)
2023-01-10 03:23:55 +00:00
mLookup, err := client.StateWaitMsg(ctx, smCreatePlaceholder.Cid(), 3, api.LookbackNoLimit, true)
require.NoError(t, err)
require.Equal(t, exitcode.Ok, mLookup.Receipt.ExitCode)
2023-01-10 03:23:55 +00:00
// confirm the placeholder is an placeholder
placeholderActor, err := client.StateGetActor(ctx, placeholderAddress, types.EmptyTSK)
require.NoError(t, err)
2023-01-10 03:23:55 +00:00
require.Equal(t, uint64(0), placeholderActor.Nonce)
require.True(t, builtin.IsPlaceholderActor(placeholderActor.Code))
2023-01-10 03:23:55 +00:00
// send a message from the placeholder address
msgFromPlaceholder := &types.Message{
From: placeholderAddress,
// self-send because an "eth tx payload" can't be to a filecoin address?
2023-01-10 03:23:55 +00:00
To: placeholderAddress,
}
2023-01-10 03:23:55 +00:00
msgFromPlaceholder, err = client.GasEstimateMessageGas(ctx, msgFromPlaceholder, nil, types.EmptyTSK)
require.NoError(t, err)
2023-01-10 03:23:55 +00:00
txArgs, err := ethtypes.NewEthTxArgsFromMessage(msgFromPlaceholder)
2022-12-15 21:38:48 +00:00
require.NoError(t, err)
digest, err := txArgs.ToRlpUnsignedMsg()
2022-12-15 21:38:48 +00:00
require.NoError(t, err)
2023-01-10 03:23:55 +00:00
siggy, err := client.WalletSign(ctx, placeholderAddress, digest)
2022-12-15 21:38:48 +00:00
require.NoError(t, err)
2023-01-10 03:23:55 +00:00
smFromPlaceholderCid, err := client.MpoolPush(ctx, &types.SignedMessage{Message: *msgFromPlaceholder, Signature: *siggy})
require.NoError(t, err)
2023-01-10 03:23:55 +00:00
mLookup, err = client.StateWaitMsg(ctx, smFromPlaceholderCid, 3, api.LookbackNoLimit, true)
require.NoError(t, err)
require.Equal(t, exitcode.Ok, mLookup.Receipt.ExitCode)
2023-01-10 03:23:55 +00:00
// confirm ugly Placeholder duckling has turned into a beautiful EthAccount swan
2023-01-10 03:23:55 +00:00
eoaActor, err := client.StateGetActor(ctx, placeholderAddress, types.EmptyTSK)
require.NoError(t, err)
2023-01-10 03:23:55 +00:00
require.False(t, builtin.IsPlaceholderActor(eoaActor.Code))
require.True(t, builtin.IsEthAccountActor(eoaActor.Code))
require.Equal(t, uint64(1), eoaActor.Nonce)
// Send another message, it should succeed without any code CID changes
2023-01-10 03:23:55 +00:00
msgFromPlaceholder = &types.Message{
From: placeholderAddress,
To: placeholderAddress,
Nonce: 1,
}
2023-01-10 03:23:55 +00:00
msgFromPlaceholder, err = client.GasEstimateMessageGas(ctx, msgFromPlaceholder, nil, types.EmptyTSK)
require.NoError(t, err)
2023-01-10 03:23:55 +00:00
txArgs, err = ethtypes.NewEthTxArgsFromMessage(msgFromPlaceholder)
2022-12-15 21:38:48 +00:00
require.NoError(t, err)
digest, err = txArgs.ToRlpUnsignedMsg()
2022-12-15 21:38:48 +00:00
require.NoError(t, err)
2023-01-10 03:23:55 +00:00
siggy, err = client.WalletSign(ctx, placeholderAddress, digest)
2022-12-15 21:38:48 +00:00
require.NoError(t, err)
2023-01-10 03:23:55 +00:00
smFromPlaceholderCid, err = client.MpoolPush(ctx, &types.SignedMessage{Message: *msgFromPlaceholder, Signature: *siggy})
require.NoError(t, err)
2023-01-10 03:23:55 +00:00
mLookup, err = client.StateWaitMsg(ctx, smFromPlaceholderCid, 3, api.LookbackNoLimit, true)
require.NoError(t, err)
require.Equal(t, exitcode.Ok, mLookup.Receipt.ExitCode)
// confirm no changes in code CID
2023-01-10 03:23:55 +00:00
eoaActor, err = client.StateGetActor(ctx, placeholderAddress, types.EmptyTSK)
require.NoError(t, err)
require.Equal(t, uint64(2), eoaActor.Nonce)
2023-01-10 03:23:55 +00:00
require.False(t, builtin.IsPlaceholderActor(eoaActor.Code))
require.True(t, builtin.IsEthAccountActor(eoaActor.Code))
}