Merge pull request #10344 from filecoin-project/steb/invoke-on-send

fix: cli: send with InvokeEVM when sending from an eth account
This commit is contained in:
Aayush Rajasekaran 2023-02-27 15:17:32 -05:00 committed by GitHub
commit 7422deaa0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 105 additions and 16 deletions

View File

@ -295,6 +295,19 @@ func EthAddressFromPubKey(pubk []byte) ([]byte, error) {
return ethAddr, nil return ethAddr, nil
} }
func IsEthAddress(addr address.Address) bool {
if addr.Protocol() != address.Delegated {
return false
}
payload := addr.Payload()
namespace, _, err := varint.FromUvarint(payload)
if err != nil {
return false
}
return namespace == builtintypes.EthereumAddressManagerActorID
}
func EthAddressFromFilecoinAddress(addr address.Address) (EthAddress, error) { func EthAddressFromFilecoinAddress(addr address.Address) (EthAddress, error) {
switch addr.Protocol() { switch addr.Protocol() {
case address.ID: case address.ID:

View File

@ -1,15 +1,18 @@
package cli package cli
import ( import (
"bytes"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"strings" "strings"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/filecoin-project/go-address" "github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/abi"
builtintypes "github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/lotus/chain/actors/builtin" "github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
@ -117,15 +120,51 @@ var sendCmd = &cli.Command{
params.From = faddr params.From = faddr
} }
if params.From.Protocol() == address.Delegated { if cctx.IsSet("params-hex") {
decparams, err := hex.DecodeString(cctx.String("params-hex"))
if err != nil {
return fmt.Errorf("failed to decode hex params: %w", err)
}
params.Params = decparams
}
if ethtypes.IsEthAddress(params.From) {
// Method numbers don't make sense from eth accounts.
if cctx.IsSet("method") {
return xerrors.Errorf("messages from f410f addresses may not specify a method number")
}
// Now, figure out the correct method number from the recipient.
if params.To == builtintypes.EthereumAddressManagerActorAddr {
params.Method = builtintypes.MethodsEAM.CreateExternal
} else {
params.Method = builtintypes.MethodsEVM.InvokeContract
}
if cctx.IsSet("params-json") {
return xerrors.Errorf("may not call with json parameters from an eth account")
}
// And format the parameters, if present.
if len(params.Params) > 0 {
var buf bytes.Buffer
if err := cbg.WriteByteArray(&buf, params.Params); err != nil {
return xerrors.Errorf("failed to marshal EVM parameters")
}
params.Params = buf.Bytes()
}
// We can only send to an f410f or f0 address.
if !(params.To.Protocol() == address.ID || params.To.Protocol() == address.Delegated) { if !(params.To.Protocol() == address.ID || params.To.Protocol() == address.Delegated) {
api := srv.FullNodeAPI() api := srv.FullNodeAPI()
// Resolve id addr if possible. // Resolve id addr if possible.
params.To, err = api.StateLookupID(ctx, params.To, types.EmptyTSK) params.To, err = api.StateLookupID(ctx, params.To, types.EmptyTSK)
if err != nil { if err != nil {
return xerrors.Errorf("f4 addresses can only send to other f4 or id addresses. could not find id address for %s", params.To.String()) return xerrors.Errorf("addresses starting with f410f can only send to other addresses starting with f410f, or id addresses. could not find id address for %s", params.To.String())
} }
} }
} else {
params.Method = abi.MethodNum(cctx.Uint64("method"))
} }
if cctx.IsSet("gas-premium") { if cctx.IsSet("gas-premium") {
@ -149,22 +188,13 @@ var sendCmd = &cli.Command{
params.GasLimit = &limit params.GasLimit = &limit
} }
params.Method = abi.MethodNum(cctx.Uint64("method"))
if cctx.IsSet("params-json") { if cctx.IsSet("params-json") {
decparams, err := srv.DecodeTypedParamsFromJSON(ctx, params.To, params.Method, cctx.String("params-json"))
if err != nil {
return fmt.Errorf("failed to decode json params: %w", err)
}
params.Params = decparams
}
if cctx.IsSet("params-hex") {
if params.Params != nil { if params.Params != nil {
return fmt.Errorf("can only specify one of 'params-json' and 'params-hex'") return fmt.Errorf("can only specify one of 'params-json' and 'params-hex'")
} }
decparams, err := hex.DecodeString(cctx.String("params-hex")) decparams, err := srv.DecodeTypedParamsFromJSON(ctx, params.To, params.Method, cctx.String("params-json"))
if err != nil { if err != nil {
return fmt.Errorf("failed to decode hex params: %w", err) return fmt.Errorf("failed to decode json params: %w", err)
} }
params.Params = decparams params.Params = decparams
} }

View File

@ -7,14 +7,16 @@ import (
"testing" "testing"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
ucli "github.com/urfave/cli/v2" ucli "github.com/urfave/cli/v2"
"github.com/filecoin-project/go-address" "github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi" "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/api"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
) )
func mustAddr(a address.Address, err error) address.Address { func mustAddr(a address.Address, err error) address.Address {
@ -65,7 +67,51 @@ func TestSendCLI(t *testing.T) {
mockSrvcs.EXPECT().Close(), mockSrvcs.EXPECT().Close(),
) )
err := app.Run([]string{"lotus", "send", "t01", "1"}) err := app.Run([]string{"lotus", "send", "t01", "1"})
assert.NoError(t, err) require.NoError(t, err)
assert.EqualValues(t, sigMsg.Cid().String()+"\n", buf.String()) 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())
}) })
} }