Skip to content

Commit

Permalink
Add --to-code to chain encode params
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Aug 20, 2021
1 parent 12875a9 commit 95bc189
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 153 deletions.
76 changes: 39 additions & 37 deletions cli/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"os"
"os/exec"
"path"
"reflect"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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",
Expand All @@ -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
Expand Down
116 changes: 1 addition & 115 deletions cmd/lotus-shed/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,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 {
Expand Down Expand Up @@ -283,113 +279,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
},
}
3 changes: 2 additions & 1 deletion documentation/en/cli-lotus.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
Expand Down

0 comments on commit 95bc189

Please sign in to comment.