fix: cli: better handle sending from EthAccount actors

This will make `lotus send` mostly just "do what the user wants" in this
case:

1. The user may not explicitly specify a method number.
2. Parameters are automatically cbor-encoded where applicable.
3. The method number is automatically selected based on the
   recipient (CreateExternal if sent to the EAM, InvokeEVM otherwise).
This commit is contained in:
Steven Allen
2023-02-24 15:15:41 -08:00
parent 1ec02c5c95
commit 68b401a895
3 changed files with 105 additions and 16 deletions
+49 -3
View File
@@ -7,14 +7,16 @@ import (
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ucli "github.com/urfave/cli/v2"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
)
func mustAddr(a address.Address, err error) address.Address {
@@ -65,7 +67,51 @@ func TestSendCLI(t *testing.T) {
mockSrvcs.EXPECT().Close(),
)
err := app.Run([]string{"lotus", "send", "t01", "1"})
assert.NoError(t, err)
assert.EqualValues(t, sigMsg.Cid().String()+"\n", buf.String())
require.NoError(t, err)
require.EqualValues(t, sigMsg.Cid().String()+"\n", buf.String())
})
}
func TestSendEthereum(t *testing.T) {
oneFil := abi.TokenAmount(types.MustParseFIL("1"))
t.Run("simple", func(t *testing.T) {
app, mockSrvcs, buf, done := newMockApp(t, sendCmd)
defer done()
testEthAddr, err := ethtypes.CastEthAddress(make([]byte, 20))
require.NoError(t, err)
testAddr := mustAddr(testEthAddr.ToFilecoinAddress())
params := abi.CborBytes([]byte{1, 2, 3, 4})
var paramsBuf bytes.Buffer
require.NoError(t, params.MarshalCBOR(&paramsBuf))
arbtProto := &api.MessagePrototype{
Message: types.Message{
From: testAddr,
To: mustAddr(address.NewIDAddress(1)),
Value: oneFil,
Method: builtin.MethodsEVM.InvokeContract,
Params: paramsBuf.Bytes(),
},
}
sigMsg := fakeSign(&arbtProto.Message)
gomock.InOrder(
mockSrvcs.EXPECT().MessageForSend(gomock.Any(), SendParams{
From: testAddr,
To: mustAddr(address.NewIDAddress(1)),
Val: oneFil,
Method: builtin.MethodsEVM.InvokeContract,
Params: paramsBuf.Bytes(),
}).Return(arbtProto, nil),
mockSrvcs.EXPECT().PublishMessage(gomock.Any(), arbtProto, false).
Return(sigMsg, nil, nil),
mockSrvcs.EXPECT().Close(),
)
err = app.Run([]string{"lotus", "send", "--from-eth-addr", testEthAddr.String(), "--params-hex", "01020304", "f01", "1"})
require.NoError(t, err)
require.EqualValues(t, sigMsg.Cid().String()+"\n", buf.String())
})
}