Add --to-code to chain encode params

This commit is contained in:
Łukasz Magiera 2021-08-20 18:00:38 +02:00
parent 12875a9664
commit 1ac87279d7
3 changed files with 42 additions and 154 deletions

View File

@ -10,7 +10,6 @@ import (
"os"
"os/exec"
"path"
"reflect"
"sort"
"strconv"
"strings"
@ -1347,7 +1346,7 @@ var ChainEncodeCmd = &cli.Command{
var chainEncodeParamsCmd = &cli.Command{
Name: "params",
Usage: "Encodes the given JSON params",
ArgsUsage: "[toAddr method params]",
ArgsUsage: "[dest method params]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tipset",
@ -1357,62 +1356,65 @@ var chainEncodeParamsCmd = &cli.Command{
Value: "base64",
Usage: "specify input encoding to parse",
},
&cli.BoolFlag{
Name: "to-code",
Usage: "interpret dest as code CID instead of as address",
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
if cctx.Args().Len() != 3 {
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments"))
}
to, err := address.NewFromString(cctx.Args().First())
if err != nil {
return xerrors.Errorf("parsing toAddr: %w", err)
}
method, err := strconv.ParseInt(cctx.Args().Get(1), 10, 64)
if err != nil {
return xerrors.Errorf("parsing method id: %w", err)
}
ts, err := LoadTipSet(ctx, cctx, api)
if err != nil {
return err
}
ctx := ReqContext(cctx)
act, err := api.StateGetActor(ctx, to, ts.Key())
if err != nil {
return xerrors.Errorf("getting actor: %w", err)
}
var p []byte
if !cctx.Bool("to-code") {
svc, err := GetFullNodeServices(cctx)
if err != nil {
return err
}
defer svc.Close() // nolint
methodMeta, found := stmgr.MethodsMap[act.Code][abi.MethodNum(method)]
if !found {
return fmt.Errorf("method %d not found on actor %s", method, act.Code)
}
to, err := address.NewFromString(cctx.Args().First())
if err != nil {
return xerrors.Errorf("parsing to addr: %w", err)
}
p := reflect.New(methodMeta.Params.Elem()).Interface().(cbg.CBORMarshaler)
p, err = svc.DecodeTypedParamsFromJSON(ctx, to, abi.MethodNum(method), cctx.Args().Get(2))
if err != nil {
return xerrors.Errorf("decoding json params: %w", err)
}
} else {
api, done, err := GetFullNodeAPIV1(cctx)
if err != nil {
return err
}
defer done()
if err := json.Unmarshal([]byte(cctx.Args().Get(2)), p); err != nil {
return fmt.Errorf("unmarshaling input into params type: %w", err)
}
to, err := cid.Parse(cctx.Args().First())
if err != nil {
return xerrors.Errorf("parsing to addr: %w", err)
}
buf := new(bytes.Buffer)
if err := p.MarshalCBOR(buf); err != nil {
return err
p, err = api.StateEncodeParams(ctx, to, abi.MethodNum(method), json.RawMessage(cctx.Args().Get(2)))
if err != nil {
return xerrors.Errorf("decoding json params: %w", err)
}
}
switch cctx.String("encoding") {
case "base64":
fmt.Println(base64.StdEncoding.EncodeToString(buf.Bytes()))
case "base64", "b64":
fmt.Println(base64.StdEncoding.EncodeToString(p))
case "hex":
fmt.Println(hex.EncodeToString(buf.Bytes()))
fmt.Println(hex.EncodeToString(p))
default:
return xerrors.Errorf("unrecognized encoding: %s", cctx.String("encoding"))
return xerrors.Errorf("unknown encoding")
}
return nil

View File

@ -14,7 +14,6 @@ import (
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/lotus/chain/stmgr"
@ -24,11 +23,7 @@ import (
)
var msgCmd = &cli.Command{
Name: "msg",
Subcommands: []*cli.Command{
msgParamsCmd,
msgActorParamsCmd,
},
Name: "msg",
Usage: "Translate message between various formats",
ArgsUsage: "Message in any form",
Action: func(cctx *cli.Context) error {
@ -283,113 +278,3 @@ func messageFromCID(cctx *cli.Context, c cid.Cid) (types.ChainMsg, error) {
return messageFromBytes(cctx, msgb)
}
var msgParamsCmd = &cli.Command{
Name: "encode-params",
Usage: "convert json params to binary (for given dest address)",
ArgsUsage: "[json params]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "to",
Usage: "dest address",
Required: true,
},
&cli.Int64Flag{
Name: "method",
Aliases: []string{"m"},
Usage: "method number",
Required: true,
},
&cli.StringFlag{
Name: "encoding",
Aliases: []string{"e"},
Usage: "out encoding (b64, hex)",
Value: "b64",
},
},
Action: func(cctx *cli.Context) error {
svc, err := lcli.GetFullNodeServices(cctx)
if err != nil {
return err
}
defer svc.Close() // nolint
ctx := lcli.ReqContext(cctx)
to, err := address.NewFromString(cctx.String("to"))
if err != nil {
return xerrors.Errorf("parsing to addr: %w", err)
}
p, err := svc.DecodeTypedParamsFromJSON(ctx, to, abi.MethodNum(cctx.Int64("method")), cctx.Args().First())
if err != nil {
return xerrors.Errorf("decoding json params: %w", err)
}
switch cctx.String("encoding") {
case "b64":
fmt.Println(base64.StdEncoding.EncodeToString(p))
case "hex":
fmt.Println(hex.EncodeToString(p))
default:
return xerrors.Errorf("unknown encoding")
}
return nil
},
}
var msgActorParamsCmd = &cli.Command{
Name: "encode-params-code",
Usage: "convert json params to binary (for given actor code)",
ArgsUsage: "[json params]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "to",
Usage: "dest actor code cid",
Required: true,
},
&cli.Int64Flag{
Name: "method",
Aliases: []string{"m"},
Usage: "method number",
Required: true,
},
&cli.StringFlag{
Name: "encoding",
Aliases: []string{"e"},
Usage: "out encoding (b64, hex)",
Value: "b64",
},
},
Action: func(cctx *cli.Context) error {
api, done, err := lcli.GetFullNodeAPIV1(cctx)
if err != nil {
return err
}
defer done()
ctx := lcli.ReqContext(cctx)
to, err := cid.Parse(cctx.String("to"))
if err != nil {
return xerrors.Errorf("parsing to addr: %w", err)
}
p, err := api.StateEncodeParams(ctx, to, abi.MethodNum(cctx.Int64("method")), json.RawMessage(cctx.Args().First()))
if err != nil {
return xerrors.Errorf("decoding json params: %w", err)
}
switch cctx.String("encoding") {
case "b64":
fmt.Println(base64.StdEncoding.EncodeToString(p))
case "hex":
fmt.Println(hex.EncodeToString(p))
default:
return xerrors.Errorf("unknown encoding")
}
return nil
},
}

View File

@ -2297,11 +2297,12 @@ NAME:
lotus chain encode params - Encodes the given JSON params
USAGE:
lotus chain encode params [command options] [toAddr method params]
lotus chain encode params [command options] [dest method params]
OPTIONS:
--tipset value
--encoding value specify input encoding to parse (default: "base64")
--to-code interpret dest as code CID instead of as address (default: false)
--help, -h show help (default: false)
```