chain cli: decode params command

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Łukasz Magiera 2020-10-10 14:17:44 +02:00 committed by Jakub Sztandera
parent 8ae66d6c6a
commit bb6a354b62
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA

View File

@ -3,6 +3,7 @@ package cli
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"os"
@ -53,6 +54,7 @@ var chainCmd = &cli.Command{
slashConsensusFault,
chainGasPriceCmd,
chainInspectUsage,
chainDecodeCmd,
},
}
@ -1233,3 +1235,68 @@ var chainGasPriceCmd = &cli.Command{
return nil
},
}
var chainDecodeCmd = &cli.Command{
Name: "decode",
Usage: "decode various types",
Subcommands: []*cli.Command{
chainDecodeParamsCmd,
},
}
var chainDecodeParamsCmd = &cli.Command{
Name: "params",
Usage: "Decode message params",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tipset",
},
},
ArgsUsage: "[toAddr method hexParams]",
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)
}
params, err := hex.DecodeString(cctx.Args().Get(2))
if err != nil {
return xerrors.Errorf("parsing hex params: %w", err)
}
ts, err := LoadTipSet(ctx, cctx, api)
if err != nil {
return err
}
act, err := api.StateGetActor(ctx, to, ts.Key())
if err != nil {
return xerrors.Errorf("getting actor: %w", err)
}
pstr, err := jsonParams(act.Code, abi.MethodNum(method), params)
if err != nil {
return err
}
fmt.Println(pstr)
return nil
},
}