use lotus CLI package; document API endpoint setting in usage.

This commit is contained in:
Raúl Kripalani 2020-09-28 12:27:42 +01:00
parent 9a355c4bc5
commit f05a40feed
2 changed files with 23 additions and 63 deletions

View File

@ -15,6 +15,7 @@ import (
"github.com/filecoin-project/lotus/chain/actors/builtin/reward"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/vm"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/conformance"
"github.com/filecoin-project/go-address"
@ -38,7 +39,6 @@ var extractCmd = &cli.Command{
Description: "generate a message-class test vector by extracting it from a live chain",
Action: runExtract,
Flags: []cli.Flag{
&apiFlag,
&cli.StringFlag{
Name: "class",
Usage: "class of vector to extract; other required flags depend on the; values: 'message'",
@ -72,7 +72,7 @@ var extractCmd = &cli.Command{
},
}
func runExtract(_ *cli.Context) error {
func runExtract(c *cli.Context) error {
// LOTUS_DISABLE_VM_BUF disables what's called "VM state tree buffering",
// which stashes write operations in a BufferedBlockstore
// (https://github.com/filecoin-project/lotus/blob/b7a4dbb07fd8332b4492313a617e3458f8003b2a/lib/bufbstore/buf_bstore.go#L21)
@ -91,7 +91,7 @@ func runExtract(_ *cli.Context) error {
}
// Make the API client.
api, closer, err := makeAPIClient()
api, closer, err := lcli.GetFullNodeAPI(c)
if err != nil {
return err
}

View File

@ -1,49 +1,41 @@
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"sort"
"strings"
"github.com/filecoin-project/go-jsonrpc"
"github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/urfave/cli/v2"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/client"
)
var apiEndpoint string
var apiFlag = cli.StringFlag{
Name: "api",
Usage: "json-rpc api endpoint, formatted as [token]:multiaddr;" +
"tvx uses unpriviliged operations, so the token may be omitted," +
"but permissions may change in the future",
EnvVars: []string{"FULLNODE_API_INFO"},
DefaultText: "",
Destination: &apiEndpoint,
}
func main() {
app := &cli.App{
Name: "tvx",
Description: `tvx is a tool for extracting and executing test vectors. It has two subcommands.
tvx extract extracts a test vector from a live network. It requires access to
a Filecoin client that exposes the standard JSON-RPC API endpoint. Set the API
endpoint on the FULLNODE_API_INFO env variable, or through the --api flag. The
format is token:multiaddr. Only message class test vectors are supported
for now.
a Filecoin client that exposes the standard JSON-RPC API endpoint. Only
message class test vectors are supported at this time.
tvx exec executes test vectors against Lotus. Either you can supply one in a
file, or many as an ndjson stdin stream.`,
file, or many as an ndjson stdin stream.
SETTING THE JSON-RPC API ENDPOINT
You can set the JSON-RPC API endpoint through one of the following approaches.
1. Directly set the API endpoint on the FULLNODE_API_INFO env variable.
The format is [token]:multiaddr, where token is optional for commands not
accessing privileged operations.
2. If you're running tvx against a local Lotus client, you can set the REPO
env variable to have the API endpoint and token extracted from the repo.
3. Rely on the default fallback, which inspects ~/.lotus and extracts the
API endpoint string if the location is a Lotus repo.
tvx will apply these approaches in the same order of precedence they're listed.
`,
Usage: "tvx is a tool for extracting and executing test vectors",
Commands: []*cli.Command{
extractCmd,
@ -60,35 +52,3 @@ func main() {
log.Fatal(err)
}
}
func makeAPIClient() (api.FullNode, jsonrpc.ClientCloser, error) {
sp := strings.SplitN(apiEndpoint, ":", 2)
if len(sp) != 2 {
return nil, nil, fmt.Errorf("invalid api value, missing token or address: %s", apiEndpoint)
}
token := sp[0]
ma, err := multiaddr.NewMultiaddr(sp[1])
if err != nil {
return nil, nil, fmt.Errorf("could not parse provided multiaddr: %w", err)
}
_, dialAddr, err := manet.DialArgs(ma)
if err != nil {
return nil, nil, fmt.Errorf("invalid api multiAddr: %w", err)
}
var (
addr = "ws://" + dialAddr + "/rpc/v0"
headers = make(http.Header, 1)
)
if len(token) != 0 {
headers.Add("Authorization", "Bearer "+token)
}
node, closer, err := client.NewFullNodeRPC(context.Background(), addr, headers)
if err != nil {
return nil, nil, fmt.Errorf("could not connect to api: %w", err)
}
return node, closer, nil
}