2022-08-29 14:25:30 +00:00
|
|
|
// stm: ignore
|
|
|
|
// stm: #unit
|
2021-02-23 14:50:47 +00:00
|
|
|
package cli
|
|
|
|
|
2021-03-25 14:24:09 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2022-06-15 10:06:22 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2023-02-24 18:04:57 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-14 15:00:51 +00:00
|
|
|
ucli "github.com/urfave/cli/v2"
|
|
|
|
|
2021-03-25 14:24:09 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2023-02-24 18:04:57 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/builtin"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
2021-03-25 14:24:09 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2022-06-15 10:06:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2023-02-24 18:04:57 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types/ethtypes"
|
2021-03-25 14:24:09 +00:00
|
|
|
)
|
2021-02-23 14:50:47 +00:00
|
|
|
|
|
|
|
func mustAddr(a address.Address, err error) address.Address {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockApp(t *testing.T, cmd *ucli.Command) (*ucli.App, *MockServicesAPI, *bytes.Buffer, func()) {
|
|
|
|
app := ucli.NewApp()
|
|
|
|
app.Commands = ucli.Commands{cmd}
|
|
|
|
app.Setup()
|
|
|
|
|
|
|
|
mockCtrl := gomock.NewController(t)
|
|
|
|
mockSrvcs := NewMockServicesAPI(mockCtrl)
|
|
|
|
app.Metadata["test-services"] = mockSrvcs
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
app.Writer = buf
|
|
|
|
|
|
|
|
return app, mockSrvcs, buf, mockCtrl.Finish
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendCLI(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()
|
|
|
|
|
2021-03-25 14:24:09 +00:00
|
|
|
arbtProto := &api.MessagePrototype{
|
|
|
|
Message: types.Message{
|
|
|
|
From: mustAddr(address.NewIDAddress(1)),
|
|
|
|
To: mustAddr(address.NewIDAddress(1)),
|
|
|
|
Value: oneFil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sigMsg := fakeSign(&arbtProto.Message)
|
2021-02-23 14:50:47 +00:00
|
|
|
|
|
|
|
gomock.InOrder(
|
2021-03-25 14:24:09 +00:00
|
|
|
mockSrvcs.EXPECT().MessageForSend(gomock.Any(), SendParams{
|
2021-02-23 14:50:47 +00:00
|
|
|
To: mustAddr(address.NewIDAddress(1)),
|
|
|
|
Val: oneFil,
|
2021-03-25 14:24:09 +00:00
|
|
|
}).Return(arbtProto, nil),
|
|
|
|
mockSrvcs.EXPECT().PublishMessage(gomock.Any(), arbtProto, false).
|
|
|
|
Return(sigMsg, nil, nil),
|
2021-02-23 14:50:47 +00:00
|
|
|
mockSrvcs.EXPECT().Close(),
|
|
|
|
)
|
|
|
|
err := app.Run([]string{"lotus", "send", "t01", "1"})
|
2023-02-24 18:04:57 +00:00
|
|
|
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(¶msBuf))
|
|
|
|
|
|
|
|
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())
|
2021-02-23 14:50:47 +00:00
|
|
|
})
|
|
|
|
}
|