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
12 changes: 0 additions & 12 deletions api/rpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"os"

"github.com/filecoin-project/go-jsonrpc"

Expand All @@ -23,14 +22,6 @@ var (
// staticClient is used for generating the OpenRPC spec.
staticClient Client
Modules = moduleMap(&staticClient)

RequestURL string
)

const (
authEnvKey = "CELESTIA_NODE_AUTH_TOKEN"

DefaultRPCAddress = "http://localhost:26658"
)

type Client struct {
Expand Down Expand Up @@ -77,9 +68,6 @@ func NewPublicClient(ctx context.Context, addr string) (*Client, error) {
// given token as the authorization token. In case if token will be empty, then
// `CELESTIA_NODE_AUTH_TOKEN` will be used in order to get the token.
func NewClient(ctx context.Context, addr string, token string) (*Client, error) {
if token == "" {
token = os.Getenv(authEnvKey)
}
authHeader := http.Header{perms.AuthKey: []string{fmt.Sprintf("Bearer %s", token)}}
return newClient(ctx, addr, authHeader)
}
Expand Down
42 changes: 23 additions & 19 deletions cmd/celestia/rpc.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package main

import (
"github.com/celestiaorg/celestia-node/api/rpc/client"
blob "github.com/celestiaorg/celestia-node/nodebuilder/blob/cmds"
das "github.com/celestiaorg/celestia-node/nodebuilder/das/cmds"
header "github.com/celestiaorg/celestia-node/nodebuilder/header/cmds"
node "github.com/celestiaorg/celestia-node/nodebuilder/node/cmds"
p2p "github.com/celestiaorg/celestia-node/nodebuilder/p2p/cmds"
share "github.com/celestiaorg/celestia-node/nodebuilder/share/cmds"
state "github.com/celestiaorg/celestia-node/nodebuilder/state/cmds"
"github.com/celestiaorg/celestia-node/cmd/celestia/util"
blob "github.com/celestiaorg/celestia-node/nodebuilder/blob/cmd"
das "github.com/celestiaorg/celestia-node/nodebuilder/das/cmd"
header "github.com/celestiaorg/celestia-node/nodebuilder/header/cmd"
node "github.com/celestiaorg/celestia-node/nodebuilder/node/cmd"
p2p "github.com/celestiaorg/celestia-node/nodebuilder/p2p/cmd"
share "github.com/celestiaorg/celestia-node/nodebuilder/share/cmd"
state "github.com/celestiaorg/celestia-node/nodebuilder/state/cmd"
)

func init() {
blob.Cmd.PersistentFlags().StringVar(initURLFlag())
das.Cmd.PersistentFlags().StringVar(initURLFlag())
header.Cmd.PersistentFlags().StringVar(initURLFlag())
p2p.Cmd.PersistentFlags().StringVar(initURLFlag())
share.Cmd.PersistentFlags().StringVar(initURLFlag())
state.Cmd.PersistentFlags().StringVar(initURLFlag())
node.Cmd.PersistentFlags().StringVar(initURLFlag())
blob.Cmd.PersistentFlags().StringVar(util.InitURLFlag())
das.Cmd.PersistentFlags().StringVar(util.InitURLFlag())
header.Cmd.PersistentFlags().StringVar(util.InitURLFlag())
p2p.Cmd.PersistentFlags().StringVar(util.InitURLFlag())
share.Cmd.PersistentFlags().StringVar(util.InitURLFlag())
state.Cmd.PersistentFlags().StringVar(util.InitURLFlag())
node.Cmd.PersistentFlags().StringVar(util.InitURLFlag())

blob.Cmd.PersistentFlags().StringVar(util.InitAuthTokenFlag())
das.Cmd.PersistentFlags().StringVar(util.InitAuthTokenFlag())
header.Cmd.PersistentFlags().StringVar(util.InitAuthTokenFlag())
p2p.Cmd.PersistentFlags().StringVar(util.InitAuthTokenFlag())
share.Cmd.PersistentFlags().StringVar(util.InitAuthTokenFlag())
state.Cmd.PersistentFlags().StringVar(util.InitAuthTokenFlag())
node.Cmd.PersistentFlags().StringVar(util.InitAuthTokenFlag())

rootCmd.AddCommand(
blob.Cmd,
Expand All @@ -30,7 +38,3 @@ func init() {
node.Cmd,
)
}

func initURLFlag() (*string, string, string, string) {
return &client.RequestURL, "url", client.DefaultRPCAddress, "Request URL"
}
58 changes: 58 additions & 0 deletions cmd/celestia/util/rpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package util

import (
"context"
"errors"
"os"

"github.com/spf13/cobra"

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

const (
// defaultRPCAddress is a default address to dial to
defaultRPCAddress = "http://localhost:26658"
authEnvKey = "CELESTIA_NODE_AUTH_TOKEN" //nolint:gosec
)

var (
requestURL string
authTokenFlag string
)

func InitURLFlag() (*string, string, string, string) {
return &requestURL, "url", defaultRPCAddress, "Request URL"
}

func InitAuthTokenFlag() (*string, string, string, string) {
return &authTokenFlag,
"token",
"",
"Authorization token (if not provided, the " + authEnvKey + " environment variable will be used)"
}

func InitClient(cmd *cobra.Command, _ []string) error {
if authTokenFlag == "" {
authTokenFlag = os.Getenv(authEnvKey)
}

client, err := rpc.NewClient(cmd.Context(), requestURL, authTokenFlag)
if err != nil {
return err
}

ctx := context.WithValue(cmd.Context(), rpcClientKey{}, client)
cmd.SetContext(ctx)
return nil
}

type rpcClientKey struct{}

func ParseClientFromCtx(ctx context.Context) (*rpc.Client, error) {
client, ok := ctx.Value(rpcClientKey{}).(*rpc.Client)
if !ok {
return nil, errors.New("rpc client was not set")
}
return client, nil
}
4 changes: 2 additions & 2 deletions cmd/celestia/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/celestiaorg/celestia-node/nodebuilder/header"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
"github.com/celestiaorg/celestia-node/nodebuilder/rpc"
rpc_cfg "github.com/celestiaorg/celestia-node/nodebuilder/rpc"
"github.com/celestiaorg/celestia-node/nodebuilder/state"
"github.com/celestiaorg/celestia-node/share"
)
Expand Down Expand Up @@ -117,7 +117,7 @@ func PersistentPreRunEnv(cmd *cobra.Command, nodeType node.Type, _ []string) err
return err
}

rpc.ParseFlags(cmd, &cfg.RPC)
rpc_cfg.ParseFlags(cmd, &cfg.RPC)
gateway.ParseFlags(cmd, &cfg.Gateway)
state.ParseFlags(cmd, &cfg.State)

Expand Down
50 changes: 32 additions & 18 deletions nodebuilder/blob/cmds/blob.go → nodebuilder/blob/cmd/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ import (

"github.com/spf13/cobra"

"github.com/celestiaorg/celestia-node/api/rpc/client"
"github.com/celestiaorg/celestia-node/blob"
"github.com/celestiaorg/celestia-node/cmd/celestia/util"
"github.com/celestiaorg/celestia-node/share"
)

var rpcClient *client.Client

var (
base64Flag bool

Expand Down Expand Up @@ -57,24 +54,23 @@ func init() {
}

var Cmd = &cobra.Command{
Use: "blob [command]",
Short: "Allows to interact with the Blob Service via JSON-RPC",
Args: cobra.NoArgs,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var err error
rpcClient, err = client.NewClient(cmd.Context(), client.RequestURL, "")
return err
},
PersistentPostRun: func(_ *cobra.Command, _ []string) {
rpcClient.Close()
},
Use: "blob [command]",
Short: "Allows to interact with the Blob Service via JSON-RPC",
Args: cobra.NoArgs,
PersistentPreRunE: util.InitClient,
}

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 := util.ParseClientFromCtx(cmd.Context())
if err != nil {
return err
}
defer client.Close()

height, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("error parsing a height:%v", err)
Expand All @@ -90,7 +86,7 @@ var getCmd = &cobra.Command{
return fmt.Errorf("error parsing a commitment:%v", err)
}

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

formatter := formatData
if base64Flag || err != nil {
Expand All @@ -105,6 +101,12 @@ 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 := util.ParseClientFromCtx(cmd.Context())
if err != nil {
return err
}
defer client.Close()

height, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("error parsing a height:%v", err)
Expand All @@ -115,7 +117,7 @@ var getAllCmd = &cobra.Command{
return fmt.Errorf("error parsing a namespace:%v", err)
}

blobs, err := rpcClient.Blob.GetAll(cmd.Context(), height, []share.Namespace{namespace})
blobs, err := client.Blob.GetAll(cmd.Context(), height, []share.Namespace{namespace})
fmt.Println(hex.EncodeToString(blobs[0].Namespace().ID()))
fmt.Println(blobs[0].Namespace().ID())
fmt.Println(blobs[0].Namespace())
Expand All @@ -132,6 +134,12 @@ 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 := util.ParseClientFromCtx(cmd.Context())
if err != nil {
return err
}
defer client.Close()

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

height, err := rpcClient.Blob.Submit(
height, err := client.Blob.Submit(
cmd.Context(),
[]*blob.Blob{parsedBlob},
&blob.SubmitOptions{Fee: fee, GasLimit: gasLimit},
Expand All @@ -164,6 +172,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 := util.ParseClientFromCtx(cmd.Context())
if err != nil {
return err
}
defer client.Close()

height, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("error parsing a height:%v", err)
Expand All @@ -179,7 +193,7 @@ var getProofCmd = &cobra.Command{
return fmt.Errorf("error parsing a commitment:%v", err)
}

proof, err := rpcClient.Blob.GetProof(cmd.Context(), height, namespace, commitment)
proof, err := client.Blob.GetProof(cmd.Context(), height, namespace, commitment)
return util.PrintOutput(proof, err, nil)
},
}
Expand Down
34 changes: 34 additions & 0 deletions nodebuilder/das/cmd/das.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"github.com/spf13/cobra"

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

func init() {
Cmd.AddCommand(samplingStatsCmd)
}

var Cmd = &cobra.Command{
Use: "das [command]",
Short: "Allows to interact with the Daser via JSON-RPC",
Args: cobra.NoArgs,
PersistentPreRunE: util.InitClient,
}

var samplingStatsCmd = &cobra.Command{
Use: "sampling-stats",
Short: "Returns the current statistics over the DA sampling process",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
client, err := util.ParseClientFromCtx(cmd.Context())
if err != nil {
return err
}
defer client.Close()

stats, err := client.DAS.SamplingStats(cmd.Context())
return util.PrintOutput(stats, err, nil)
},
}
38 changes: 0 additions & 38 deletions nodebuilder/das/cmds/das.go

This file was deleted.

Loading