Add --to-code to chain encode params
This commit is contained in:
parent
12875a9664
commit
1ac87279d7
76
cli/chain.go
76
cli/chain.go
@ -10,7 +10,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"reflect"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -1347,7 +1346,7 @@ var ChainEncodeCmd = &cli.Command{
|
|||||||
var chainEncodeParamsCmd = &cli.Command{
|
var chainEncodeParamsCmd = &cli.Command{
|
||||||
Name: "params",
|
Name: "params",
|
||||||
Usage: "Encodes the given JSON params",
|
Usage: "Encodes the given JSON params",
|
||||||
ArgsUsage: "[toAddr method params]",
|
ArgsUsage: "[dest method params]",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "tipset",
|
Name: "tipset",
|
||||||
@ -1357,62 +1356,65 @@ var chainEncodeParamsCmd = &cli.Command{
|
|||||||
Value: "base64",
|
Value: "base64",
|
||||||
Usage: "specify input encoding to parse",
|
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 {
|
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 {
|
if cctx.Args().Len() != 3 {
|
||||||
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments"))
|
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)
|
method, err := strconv.ParseInt(cctx.Args().Get(1), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return xerrors.Errorf("parsing method id: %w", err)
|
return xerrors.Errorf("parsing method id: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ts, err := LoadTipSet(ctx, cctx, api)
|
ctx := ReqContext(cctx)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
act, err := api.StateGetActor(ctx, to, ts.Key())
|
var p []byte
|
||||||
if err != nil {
|
if !cctx.Bool("to-code") {
|
||||||
return xerrors.Errorf("getting actor: %w", err)
|
svc, err := GetFullNodeServices(cctx)
|
||||||
}
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer svc.Close() // nolint
|
||||||
|
|
||||||
methodMeta, found := stmgr.MethodsMap[act.Code][abi.MethodNum(method)]
|
to, err := address.NewFromString(cctx.Args().First())
|
||||||
if !found {
|
if err != nil {
|
||||||
return fmt.Errorf("method %d not found on actor %s", method, act.Code)
|
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 {
|
to, err := cid.Parse(cctx.Args().First())
|
||||||
return fmt.Errorf("unmarshaling input into params type: %w", err)
|
if err != nil {
|
||||||
}
|
return xerrors.Errorf("parsing to addr: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
p, err = api.StateEncodeParams(ctx, to, abi.MethodNum(method), json.RawMessage(cctx.Args().Get(2)))
|
||||||
if err := p.MarshalCBOR(buf); err != nil {
|
if err != nil {
|
||||||
return err
|
return xerrors.Errorf("decoding json params: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch cctx.String("encoding") {
|
switch cctx.String("encoding") {
|
||||||
case "base64":
|
case "base64", "b64":
|
||||||
fmt.Println(base64.StdEncoding.EncodeToString(buf.Bytes()))
|
fmt.Println(base64.StdEncoding.EncodeToString(p))
|
||||||
case "hex":
|
case "hex":
|
||||||
fmt.Println(hex.EncodeToString(buf.Bytes()))
|
fmt.Println(hex.EncodeToString(p))
|
||||||
default:
|
default:
|
||||||
return xerrors.Errorf("unrecognized encoding: %s", cctx.String("encoding"))
|
return xerrors.Errorf("unknown encoding")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -14,7 +14,6 @@ import (
|
|||||||
"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/big"
|
"github.com/filecoin-project/go-state-types/big"
|
||||||
|
|
||||||
"github.com/filecoin-project/lotus/chain/stmgr"
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
||||||
@ -24,11 +23,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var msgCmd = &cli.Command{
|
var msgCmd = &cli.Command{
|
||||||
Name: "msg",
|
Name: "msg",
|
||||||
Subcommands: []*cli.Command{
|
|
||||||
msgParamsCmd,
|
|
||||||
msgActorParamsCmd,
|
|
||||||
},
|
|
||||||
Usage: "Translate message between various formats",
|
Usage: "Translate message between various formats",
|
||||||
ArgsUsage: "Message in any form",
|
ArgsUsage: "Message in any form",
|
||||||
Action: func(cctx *cli.Context) error {
|
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)
|
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
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
@ -2297,11 +2297,12 @@ NAME:
|
|||||||
lotus chain encode params - Encodes the given JSON params
|
lotus chain encode params - Encodes the given JSON params
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
lotus chain encode params [command options] [toAddr method params]
|
lotus chain encode params [command options] [dest method params]
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
--tipset value
|
--tipset value
|
||||||
--encoding value specify input encoding to parse (default: "base64")
|
--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)
|
--help, -h show help (default: false)
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user