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

refactor(cmd): extract rpc cmds #2706

Merged
merged 10 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions cmd/celestia/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,23 @@ func TestBridge(t *testing.T) {
})
*/
}

func parseSignatureForHelpstring(methodSig reflect.StructField) string {
simplifiedSignature := "("
in, out := methodSig.Type.NumIn(), methodSig.Type.NumOut()
for i := 1; i < in; i++ {
simplifiedSignature += methodSig.Type.In(i).String()
if i != in-1 {
simplifiedSignature += ", "
}
}
simplifiedSignature += ") -> ("
for i := 0; i < out-1; i++ {
simplifiedSignature += methodSig.Type.Out(i).String()
if i != out-2 {
simplifiedSignature += ", "
}
}
simplifiedSignature += ")"
return simplifiedSignature
}
27 changes: 0 additions & 27 deletions cmd/celestia/das.go

This file was deleted.

74 changes: 27 additions & 47 deletions cmd/celestia/admin.go → cmd/celestia/internal/admin/admin.go
Original file line number Diff line number Diff line change
@@ -1,69 +1,59 @@
package main
package admin

import (
"context"
"errors"
"strings"

"github.com/filecoin-project/go-jsonrpc/auth"
"github.com/spf13/cobra"

"github.com/celestiaorg/celestia-node/cmd/celestia/internal"
)

func init() {
nodeCmd.AddCommand(nodeInfoCmd, logCmd, verifyCmd, authCmd)
rootCmd.AddCommand(nodeCmd)
}
NodeCmd.AddCommand(nodeInfoCmd, logCmd, verifyCmd, authCmd)

var nodeCmd = &cobra.Command{
Use: "node [command]",
Short: "Allows administrating running node.",
Args: cobra.NoArgs,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
rpcClient, err := newRPCClient(cmd.Context())
if err != nil {
return err
}
NodeCmd.PersistentFlags().StringVar(
&internal.RequestURL,
"url",
"http://localhost:26658",
"Request URL",
)
}

ctx := context.WithValue(cmd.Context(), rpcClientKey{}, rpcClient)
cmd.SetContext(ctx)
return nil
},
var NodeCmd = &cobra.Command{
Use: "node [command]",
Short: "Allows administrating running node.",
Args: cobra.NoArgs,
PersistentPreRunE: internal.InitClient,
}

var nodeInfoCmd = &cobra.Command{
Use: "info",
Args: cobra.NoArgs,
Short: "Returns administrative information about the node.",
RunE: func(c *cobra.Command, args []string) error {
client, err := rpcClient(c.Context())
if err != nil {
return err
}
info, err := client.Node.Info(c.Context())
return printOutput(info, err, nil)
info, err := internal.RPCClient.Node.Info(c.Context())
return internal.PrintOutput(info, err, nil)
},
}

var logCmd = &cobra.Command{
Use: "log-level",
Args: cobra.MinimumNArgs(1),
Short: "Allows to set log level for module to in format <module>:<level>" +
Use: "log-level",
Args: cobra.MinimumNArgs(1),
Short: "Sets log level for module.",
Long: "Allows to set log level for module to in format <module>:<level>" +
"`DEBUG, INFO, WARN, ERROR, DPANIC, PANIC, FATAL and their lower-case forms`.\n" +
"To set all modules to a particular level `*:<log.level>` should be passed",
RunE: func(c *cobra.Command, args []string) error {
client, err := rpcClient(c.Context())
if err != nil {
return err
}

for _, ll := range args {
params := strings.Split(ll, ":")
if len(params) != 2 {
return errors.New("cmd: log-level arg must be in form <module>:<level>," +
"e.g. pubsub:debug")
}

if err = client.Node.LogLevelSet(c.Context(), params[0], params[1]); err != nil {
if err := internal.RPCClient.Node.LogLevelSet(c.Context(), params[0], params[1]); err != nil {
return err
}
}
Expand All @@ -77,13 +67,8 @@ var verifyCmd = &cobra.Command{
Short: "Returns the permissions assigned to the given token.",

RunE: func(c *cobra.Command, args []string) error {
client, err := rpcClient(c.Context())
if err != nil {
return err
}

perms, err := client.Node.AuthVerify(c.Context(), args[0])
return printOutput(perms, err, nil)
perms, err := internal.RPCClient.Node.AuthVerify(c.Context(), args[0])
return internal.PrintOutput(perms, err, nil)
},
}

Expand All @@ -92,17 +77,12 @@ var authCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
Short: "Signs and returns a new token with the given permissions.",
RunE: func(c *cobra.Command, args []string) error {
client, err := rpcClient(c.Context())
if err != nil {
return err
}

perms := make([]auth.Permission, len(args))
for i, p := range args {
perms[i] = (auth.Permission)(p)
}

result, err := client.Node.AuthNew(c.Context(), perms)
return printOutput(result, err, nil)
result, err := internal.RPCClient.Node.AuthNew(c.Context(), perms)
return internal.PrintOutput(result, err, nil)
},
}
33 changes: 33 additions & 0 deletions cmd/celestia/internal/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package internal

import (
"os"

"github.com/spf13/cobra"

"github.com/celestiaorg/celestia-node/api/rpc/client"
)

const authEnvKey = "CELESTIA_NODE_AUTH_TOKEN" //nolint:gosec

var (
RPCClient *client.Client
RequestURL string
)

// InitClient creates the rpc client under the given at the *RequestURL* address
func InitClient(cmd *cobra.Command, _ []string) error {
var err error
RPCClient, err = client.NewClient(cmd.Context(), RequestURL, os.Getenv(authEnvKey))
if err != nil {
return err
}
return nil
}

// CloseClient closes the connection with the rpc client.
func CloseClient(_ *cobra.Command, _ []string) {
if RPCClient != nil {
RPCClient.Close()
}
}
66 changes: 28 additions & 38 deletions cmd/celestia/blob.go → cmd/celestia/internal/rpc/blob.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package rpc

import (
"encoding/base64"
Expand All @@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"

"github.com/celestiaorg/celestia-node/blob"
"github.com/celestiaorg/celestia-node/cmd/celestia/internal"
"github.com/celestiaorg/celestia-node/share"
)

Expand All @@ -20,14 +21,21 @@ var (
)

func init() {
blobCmd.AddCommand(getCmd, getAllCmd, submitCmd, getProofCmd)
BlobCmd.PersistentFlags().StringVar(
&internal.RequestURL,
"url",
"http://localhost:26658",
"Request URL",
)
BlobCmd.AddCommand(getCmd, getAllCmd, submitCmd, getProofCmd)

getCmd.PersistentFlags().BoolVar(
&base64Flag,
"base64",
false,
"printed blob's data a base64 string",
)

getAllCmd.PersistentFlags().BoolVar(
&base64Flag,
"base64",
Expand All @@ -50,28 +58,25 @@ func init() {
)
}

var blobCmd = &cobra.Command{
Use: "blob [command]",
Short: "Allows to interact with the Blob Service via JSON-RPC",
Args: cobra.NoArgs,
var BlobCmd = &cobra.Command{
Use: "blob [command]",
Short: "Allows to interact with the Blob Service via JSON-RPC",
Args: cobra.NoArgs,
PersistentPreRunE: internal.InitClient,
PersistentPostRun: internal.CloseClient,
}

var getCmd = &cobra.Command{
Use: "get [height, namespace, commitment]",
Args: cobra.ExactArgs(3),
Short: "Returns the blob for the given namespace by commitment at a particular height.",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := rpcClient(cmd.Context())
if err != nil {
return err
}

height, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("error parsing a height:%v", err)
}

namespace, err := parseV0Namespace(args[1])
namespace, err := internal.ParseV0Namespace(args[1])
if err != nil {
return fmt.Errorf("error parsing a namespace:%v", err)
}
Expand All @@ -81,13 +86,13 @@ var getCmd = &cobra.Command{
return fmt.Errorf("error parsing a commitment:%v", err)
}

blob, err := client.Blob.Get(cmd.Context(), height, namespace, commitment)
blob, err := internal.RPCClient.Blob.Get(cmd.Context(), height, namespace, commitment)

formatter := formatData
if base64Flag || err != nil {
formatter = nil
}
return printOutput(blob, err, formatter)
return internal.PrintOutput(blob, err, formatter)
},
}

Expand All @@ -96,28 +101,23 @@ var getAllCmd = &cobra.Command{
Args: cobra.ExactArgs(2),
Short: "Returns all blobs for the given namespace at a particular height.",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := rpcClient(cmd.Context())
if err != nil {
return err
}

height, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("error parsing a height:%v", err)
}

namespace, err := parseV0Namespace(args[1])
namespace, err := internal.ParseV0Namespace(args[1])
if err != nil {
return fmt.Errorf("error parsing a namespace:%v", err)
}

blobs, err := client.Blob.GetAll(cmd.Context(), height, []share.Namespace{namespace})
blobs, err := internal.RPCClient.Blob.GetAll(cmd.Context(), height, []share.Namespace{namespace})

formatter := formatData
if base64Flag || err != nil {
formatter = nil
}
return printOutput(blobs, err, formatter)
return internal.PrintOutput(blobs, err, formatter)
},
}

Expand All @@ -126,12 +126,7 @@ var submitCmd = &cobra.Command{
Args: cobra.ExactArgs(2),
Short: "Submit the blob at the given namespace. Note: only one blob is allowed to submit through the RPC.",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := rpcClient(cmd.Context())
if err != nil {
return err
}

namespace, err := parseV0Namespace(args[0])
namespace, err := internal.ParseV0Namespace(args[0])
if err != nil {
return fmt.Errorf("error parsing a namespace:%v", err)
}
Expand All @@ -141,7 +136,7 @@ var submitCmd = &cobra.Command{
return fmt.Errorf("error creating a blob:%v", err)
}

height, err := client.Blob.Submit(
height, err := internal.RPCClient.Blob.Submit(
cmd.Context(),
[]*blob.Blob{parsedBlob},
&blob.SubmitOptions{Fee: fee, GasLimit: gasLimit},
Expand All @@ -154,7 +149,7 @@ var submitCmd = &cobra.Command{
Height: height,
Commitment: parsedBlob.Commitment,
}
return printOutput(response, err, nil)
return internal.PrintOutput(response, err, nil)
},
}

Expand All @@ -163,17 +158,12 @@ var getProofCmd = &cobra.Command{
Args: cobra.ExactArgs(3),
Short: "Retrieves the blob in the given namespaces at the given height by commitment and returns its Proof.",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := rpcClient(cmd.Context())
if err != nil {
return err
}

height, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("error parsing a height:%v", err)
}

namespace, err := parseV0Namespace(args[1])
namespace, err := internal.ParseV0Namespace(args[1])
if err != nil {
return fmt.Errorf("error parsing a namespace:%v", err)
}
Expand All @@ -183,8 +173,8 @@ var getProofCmd = &cobra.Command{
return fmt.Errorf("error parsing a commitment:%v", err)
}

proof, err := client.Blob.GetProof(cmd.Context(), height, namespace, commitment)
return printOutput(proof, err, nil)
proof, err := internal.RPCClient.Blob.GetProof(cmd.Context(), height, namespace, commitment)
return internal.PrintOutput(proof, err, nil)
},
}

Expand Down
Loading