Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api/command for encoding actor params #7150

Merged
merged 2 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/api_full.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ type FullNode interface {
StateListMessages(ctx context.Context, match *MessageMatch, tsk types.TipSetKey, toht abi.ChainEpoch) ([]cid.Cid, error) //perm:read
// StateDecodeParams attempts to decode the provided params, based on the recipient actor address and method number.
StateDecodeParams(ctx context.Context, toAddr address.Address, method abi.MethodNum, params []byte, tsk types.TipSetKey) (interface{}, error) //perm:read
// StateEncodeParams attempts to encode the provided json params to the binary from
StateEncodeParams(ctx context.Context, toActCode cid.Cid, method abi.MethodNum, params json.RawMessage) ([]byte, error) //perm:read

// StateNetworkName returns the name of the network the node is synced to
StateNetworkName(context.Context) (dtypes.NetworkName, error) //perm:read
Expand Down
16 changes: 16 additions & 0 deletions api/mocks/mock_full.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions api/proxy_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified build/openrpc/full.json.gz
Binary file not shown.
Binary file modified build/openrpc/miner.json.gz
Binary file not shown.
Binary file modified build/openrpc/worker.json.gz
Binary file not shown.
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
20 changes: 20 additions & 0 deletions documentation/en/api-v1-unstable-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
* [StateCompute](#StateCompute)
* [StateDealProviderCollateralBounds](#StateDealProviderCollateralBounds)
* [StateDecodeParams](#StateDecodeParams)
* [StateEncodeParams](#StateEncodeParams)
* [StateGetActor](#StateGetActor)
* [StateListActors](#StateListActors)
* [StateListMessages](#StateListMessages)
Expand Down Expand Up @@ -4184,6 +4185,25 @@ Inputs:

Response: `{}`

### StateEncodeParams
StateEncodeParams attempts to encode the provided json params to the binary from


Perms: read

Inputs:
```json
[
{
"/": "bafy2bzacea3wsdh6y3a36tb3skempjoxqpuyompjbmfeyf34fi3uy6uue42v4"
},
1,
null
]
```

Response: `"Ynl0ZSBhcnJheQ=="`

### StateGetActor
StateGetActor returns the indicated actor's nonce and balance.

Expand Down
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
20 changes: 20 additions & 0 deletions node/impl/full/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package full
import (
"bytes"
"context"
"encoding/json"
"strconv"

"github.com/filecoin-project/go-state-types/cbor"
cid "github.com/ipfs/go-cid"
"go.uber.org/fx"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -497,6 +499,24 @@ func (a *StateAPI) StateDecodeParams(ctx context.Context, toAddr address.Address
return paramType, nil
}

func (a *StateAPI) StateEncodeParams(ctx context.Context, toActCode cid.Cid, method abi.MethodNum, params json.RawMessage) ([]byte, error) {
paramType, err := stmgr.GetParamType(toActCode, method)
if err != nil {
return nil, xerrors.Errorf("getting params type: %w", err)
}

if err := json.Unmarshal(params, &paramType); err != nil {
return nil, xerrors.Errorf("json unmarshal: %w", err)
}

var cbb bytes.Buffer
if err := paramType.(cbor.Marshaler).MarshalCBOR(&cbb); err != nil {
return nil, xerrors.Errorf("cbor marshal: %w", err)
}

return cbb.Bytes(), nil
}

// This is on StateAPI because miner.Miner requires this, and MinerAPI requires miner.Miner
func (a *StateAPI) MinerGetBaseInfo(ctx context.Context, maddr address.Address, epoch abi.ChainEpoch, tsk types.TipSetKey) (*api.MiningBaseInfo, error) {
// XXX: Gets the state by computing the tipset state, instead of looking at the parent.
Expand Down