From d32597be51318c455b1b14e035691c3202fa85ff Mon Sep 17 00:00:00 2001 From: Viacheslav Gonkivskyi Date: Mon, 18 Sep 2023 17:40:38 +0300 Subject: [PATCH] refactoring --- api/rpc/client/client.go | 16 ++- cmd/celestia/bridge.go | 3 +- cmd/celestia/full.go | 3 +- cmd/celestia/internal/init.go | 33 ----- cmd/celestia/internal/util.go | 64 ---------- cmd/celestia/light.go | 3 +- cmd/celestia/rpc.go | 38 ++++-- cmd/celestia/util.go | 68 ---------- cmd/celestia/util/util.go | 128 +++++++++++++++++++ cmd/celestia/{internal => util}/util_test.go | 2 +- nodebuilder/blob/cmds/blob.go | 53 ++++---- nodebuilder/das/cmds/das.go | 34 ++--- nodebuilder/header/cmds/header.go | 50 ++++---- nodebuilder/node/cmds/{admin.go => node.go} | 43 ++++--- nodebuilder/p2p/cmds/p2p.go | 98 +++++++------- nodebuilder/share/cmds/share.go | 52 ++++---- nodebuilder/state/cmds/state.go | 84 ++++++------ 17 files changed, 394 insertions(+), 378 deletions(-) delete mode 100644 cmd/celestia/internal/init.go delete mode 100644 cmd/celestia/internal/util.go delete mode 100644 cmd/celestia/util.go create mode 100644 cmd/celestia/util/util.go rename cmd/celestia/{internal => util}/util_test.go (99%) rename nodebuilder/node/cmds/{admin.go => node.go} (64%) diff --git a/api/rpc/client/client.go b/api/rpc/client/client.go index 7ac8a55b3d..bc2e72278b 100644 --- a/api/rpc/client/client.go +++ b/api/rpc/client/client.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "os" "github.com/filecoin-project/go-jsonrpc" @@ -22,6 +23,15 @@ var ( // staticClient is used for generating the OpenRPC spec. staticClient Client Modules = moduleMap(&staticClient) + + RequestURL string + RPCClient *Client +) + +const ( + authEnvKey = "CELESTIA_NODE_AUTH_TOKEN" //nolint:gosec + + DefaultRPCAddress = "http://localhost:26658" ) type Client struct { @@ -65,8 +75,12 @@ func NewPublicClient(ctx context.Context, addr string) (*Client, error) { } // NewClient creates a new Client with one connection per namespace with the -// given token as the authorization token. +// 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) } diff --git a/cmd/celestia/bridge.go b/cmd/celestia/bridge.go index fb5066e5d4..d51bae6d3b 100644 --- a/cmd/celestia/bridge.go +++ b/cmd/celestia/bridge.go @@ -5,6 +5,7 @@ import ( "github.com/spf13/pflag" cmdnode "github.com/celestiaorg/celestia-node/cmd" + "github.com/celestiaorg/celestia-node/cmd/celestia/util" "github.com/celestiaorg/celestia-node/nodebuilder/core" "github.com/celestiaorg/celestia-node/nodebuilder/gateway" "github.com/celestiaorg/celestia-node/nodebuilder/node" @@ -42,6 +43,6 @@ var bridgeCmd = &cobra.Command{ Args: cobra.NoArgs, Short: "Manage your Bridge node", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - return persistentPreRunEnv(cmd, node.Bridge, args) + return util.PersistentPreRunEnv(cmd, node.Bridge, args) }, } diff --git a/cmd/celestia/full.go b/cmd/celestia/full.go index 912de0bca8..fbb20d8619 100644 --- a/cmd/celestia/full.go +++ b/cmd/celestia/full.go @@ -5,6 +5,7 @@ import ( "github.com/spf13/pflag" cmdnode "github.com/celestiaorg/celestia-node/cmd" + "github.com/celestiaorg/celestia-node/cmd/celestia/util" "github.com/celestiaorg/celestia-node/nodebuilder/core" "github.com/celestiaorg/celestia-node/nodebuilder/gateway" "github.com/celestiaorg/celestia-node/nodebuilder/header" @@ -46,6 +47,6 @@ var fullCmd = &cobra.Command{ Args: cobra.NoArgs, Short: "Manage your Full node", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - return persistentPreRunEnv(cmd, node.Full, args) + return util.PersistentPreRunEnv(cmd, node.Full, args) }, } diff --git a/cmd/celestia/internal/init.go b/cmd/celestia/internal/init.go deleted file mode 100644 index 430eac0f06..0000000000 --- a/cmd/celestia/internal/init.go +++ /dev/null @@ -1,33 +0,0 @@ -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() - } -} diff --git a/cmd/celestia/internal/util.go b/cmd/celestia/internal/util.go deleted file mode 100644 index 99c1a4415a..0000000000 --- a/cmd/celestia/internal/util.go +++ /dev/null @@ -1,64 +0,0 @@ -package internal - -import ( - "encoding/base64" - "encoding/hex" - "encoding/json" - "fmt" - "os" - "strings" - - "github.com/celestiaorg/celestia-node/share" -) - -func PrintOutput(data interface{}, err error, formatData func(interface{}) interface{}) error { - switch { - case err != nil: - data = err - case formatData != nil: - data = formatData(data) - } - - resp := struct { - Result interface{} `json:"result"` - }{ - Result: data, - } - - bytes, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return err - } - fmt.Fprintln(os.Stdout, string(bytes)) - return nil -} - -// ParseV0Namespace parses a namespace from a base64 or hex string. The param -// is expected to be the user-specified portion of a v0 namespace ID (i.e. the -// last 10 bytes). -func ParseV0Namespace(param string) (share.Namespace, error) { - userBytes, err := DecodeToBytes(param) - if err != nil { - return nil, err - } - - // if the namespace ID is <= 10 bytes, left pad it with 0s - return share.NewBlobNamespaceV0(userBytes) -} - -// DecodeToBytes decodes a Base64 or hex input string into a byte slice. -func DecodeToBytes(param string) ([]byte, error) { - if strings.HasPrefix(param, "0x") { - decoded, err := hex.DecodeString(param[2:]) - if err != nil { - return nil, fmt.Errorf("error decoding namespace ID: %w", err) - } - return decoded, nil - } - // otherwise, it's just a base64 string - decoded, err := base64.StdEncoding.DecodeString(param) - if err != nil { - return nil, fmt.Errorf("error decoding namespace ID: %w", err) - } - return decoded, nil -} diff --git a/cmd/celestia/light.go b/cmd/celestia/light.go index 9c63945445..b25a1ceecb 100644 --- a/cmd/celestia/light.go +++ b/cmd/celestia/light.go @@ -5,6 +5,7 @@ import ( "github.com/spf13/pflag" cmdnode "github.com/celestiaorg/celestia-node/cmd" + "github.com/celestiaorg/celestia-node/cmd/celestia/util" "github.com/celestiaorg/celestia-node/nodebuilder/core" "github.com/celestiaorg/celestia-node/nodebuilder/gateway" "github.com/celestiaorg/celestia-node/nodebuilder/header" @@ -46,6 +47,6 @@ var lightCmd = &cobra.Command{ Args: cobra.NoArgs, Short: "Manage your Light node", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - return persistentPreRunEnv(cmd, node.Light, args) + return util.PersistentPreRunEnv(cmd, node.Light, args) }, } diff --git a/cmd/celestia/rpc.go b/cmd/celestia/rpc.go index 1b1c269975..4e53dd70a1 100644 --- a/cmd/celestia/rpc.go +++ b/cmd/celestia/rpc.go @@ -1,19 +1,39 @@ package main import ( - "github.com/celestiaorg/celestia-node/cmd/celestia/internal/admin" - "github.com/celestiaorg/celestia-node/cmd/celestia/internal/rpc" + "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" ) +const authEnvKey = "CELESTIA_NODE_AUTH_TOKEN" //nolint:gosec + func init() { - rootCmd.AddCommand(admin.NodeCmd) + blob.BlobCmd.PersistentFlags().StringVar(initUrlFlag()) + das.DASCmd.PersistentFlags().StringVar(initUrlFlag()) + header.HeaderCmd.PersistentFlags().StringVar(initUrlFlag()) + p2p.P2PCmd.PersistentFlags().StringVar(initUrlFlag()) + share.ShareCmd.PersistentFlags().StringVar(initUrlFlag()) + state.StateCmd.PersistentFlags().StringVar(initUrlFlag()) + node.NodeCmd.PersistentFlags().StringVar(initUrlFlag()) rootCmd.AddCommand( - rpc.BlobCmd, - rpc.DASCmd, - rpc.HeaderCmd, - rpc.P2PCmd, - rpc.ShareCmd, - rpc.StateCmd, + blob.BlobCmd, + das.DASCmd, + header.HeaderCmd, + p2p.P2PCmd, + share.ShareCmd, + state.StateCmd, + node.NodeCmd, ) } + +func initUrlFlag() (*string, string, string, string) { + return &client.RequestURL, "url", client.DefaultRPCAddress, "Request URL" +} diff --git a/cmd/celestia/util.go b/cmd/celestia/util.go deleted file mode 100644 index a38860d1f7..0000000000 --- a/cmd/celestia/util.go +++ /dev/null @@ -1,68 +0,0 @@ -package main - -import ( - "github.com/spf13/cobra" - - cmdnode "github.com/celestiaorg/celestia-node/cmd" - "github.com/celestiaorg/celestia-node/nodebuilder/core" - "github.com/celestiaorg/celestia-node/nodebuilder/gateway" - "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" - "github.com/celestiaorg/celestia-node/nodebuilder/state" -) - -func persistentPreRunEnv(cmd *cobra.Command, nodeType node.Type, _ []string) error { - var ( - ctx = cmd.Context() - err error - ) - - ctx = cmdnode.WithNodeType(ctx, nodeType) - - parsedNetwork, err := p2p.ParseNetwork(cmd) - if err != nil { - return err - } - ctx = cmdnode.WithNetwork(ctx, parsedNetwork) - - // loads existing config into the environment - ctx, err = cmdnode.ParseNodeFlags(ctx, cmd, cmdnode.Network(ctx)) - if err != nil { - return err - } - - cfg := cmdnode.NodeConfig(ctx) - - err = p2p.ParseFlags(cmd, &cfg.P2P) - if err != nil { - return err - } - - err = core.ParseFlags(cmd, &cfg.Core) - if err != nil { - return err - } - - if nodeType != node.Bridge { - err = header.ParseFlags(cmd, &cfg.Header) - if err != nil { - return err - } - } - - ctx, err = cmdnode.ParseMiscFlags(ctx, cmd) - if err != nil { - return err - } - - rpc.ParseFlags(cmd, &cfg.RPC) - gateway.ParseFlags(cmd, &cfg.Gateway) - state.ParseFlags(cmd, &cfg.State) - - // set config - ctx = cmdnode.WithNodeConfig(ctx, &cfg) - cmd.SetContext(ctx) - return nil -} diff --git a/cmd/celestia/util/util.go b/cmd/celestia/util/util.go new file mode 100644 index 0000000000..8b8320d549 --- /dev/null +++ b/cmd/celestia/util/util.go @@ -0,0 +1,128 @@ +package util + +import ( + "encoding/base64" + "encoding/hex" + "encoding/json" + "fmt" + "os" + "strings" + + "github.com/spf13/cobra" + + cmdnode "github.com/celestiaorg/celestia-node/cmd" + "github.com/celestiaorg/celestia-node/nodebuilder/core" + "github.com/celestiaorg/celestia-node/nodebuilder/gateway" + "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" + "github.com/celestiaorg/celestia-node/nodebuilder/state" + "github.com/celestiaorg/celestia-node/share" +) + +func PrintOutput(data interface{}, err error, formatData func(interface{}) interface{}) error { + switch { + case err != nil: + data = err + case formatData != nil: + data = formatData(data) + } + + resp := struct { + Result interface{} `json:"result"` + }{ + Result: data, + } + + bytes, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return err + } + fmt.Fprintln(os.Stdout, string(bytes)) + return nil +} + +// ParseV0Namespace parses a namespace from a base64 or hex string. The param +// is expected to be the user-specified portion of a v0 namespace ID (i.e. the +// last 10 bytes). +func ParseV0Namespace(param string) (share.Namespace, error) { + userBytes, err := DecodeToBytes(param) + if err != nil { + return nil, err + } + + // if the namespace ID is <= 10 bytes, left pad it with 0s + return share.NewBlobNamespaceV0(userBytes) +} + +// DecodeToBytes decodes a Base64 or hex input string into a byte slice. +func DecodeToBytes(param string) ([]byte, error) { + if strings.HasPrefix(param, "0x") { + decoded, err := hex.DecodeString(param[2:]) + if err != nil { + return nil, fmt.Errorf("error decoding namespace ID: %w", err) + } + return decoded, nil + } + // otherwise, it's just a base64 string + decoded, err := base64.StdEncoding.DecodeString(param) + if err != nil { + return nil, fmt.Errorf("error decoding namespace ID: %w", err) + } + return decoded, nil +} + +func PersistentPreRunEnv(cmd *cobra.Command, nodeType node.Type, _ []string) error { + var ( + ctx = cmd.Context() + err error + ) + + ctx = cmdnode.WithNodeType(ctx, nodeType) + + parsedNetwork, err := p2p.ParseNetwork(cmd) + if err != nil { + return err + } + ctx = cmdnode.WithNetwork(ctx, parsedNetwork) + + // loads existing config into the environment + ctx, err = cmdnode.ParseNodeFlags(ctx, cmd, cmdnode.Network(ctx)) + if err != nil { + return err + } + + cfg := cmdnode.NodeConfig(ctx) + + err = p2p.ParseFlags(cmd, &cfg.P2P) + if err != nil { + return err + } + + err = core.ParseFlags(cmd, &cfg.Core) + if err != nil { + return err + } + + if nodeType != node.Bridge { + err = header.ParseFlags(cmd, &cfg.Header) + if err != nil { + return err + } + } + + ctx, err = cmdnode.ParseMiscFlags(ctx, cmd) + if err != nil { + return err + } + + rpc.ParseFlags(cmd, &cfg.RPC) + gateway.ParseFlags(cmd, &cfg.Gateway) + state.ParseFlags(cmd, &cfg.State) + + // set config + ctx = cmdnode.WithNodeConfig(ctx, &cfg) + cmd.SetContext(ctx) + return nil +} diff --git a/cmd/celestia/internal/util_test.go b/cmd/celestia/util/util_test.go similarity index 99% rename from cmd/celestia/internal/util_test.go rename to cmd/celestia/util/util_test.go index c67efe506d..3bfa51396f 100644 --- a/cmd/celestia/internal/util_test.go +++ b/cmd/celestia/util/util_test.go @@ -1,4 +1,4 @@ -package internal +package util import ( "testing" diff --git a/nodebuilder/blob/cmds/blob.go b/nodebuilder/blob/cmds/blob.go index 2c98113150..38e37b6cb3 100644 --- a/nodebuilder/blob/cmds/blob.go +++ b/nodebuilder/blob/cmds/blob.go @@ -1,4 +1,4 @@ -package rpc +package cmd import ( "encoding/base64" @@ -9,11 +9,14 @@ 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/internal" + "github.com/celestiaorg/celestia-node/cmd/celestia/util" "github.com/celestiaorg/celestia-node/share" ) +var rpcClient *client.Client + var ( base64Flag bool @@ -22,12 +25,6 @@ var ( ) func init() { - BlobCmd.PersistentFlags().StringVar( - &internal.RequestURL, - "url", - "http://localhost:26658", - "Request URL", - ) BlobCmd.AddCommand(getCmd, getAllCmd, submitCmd, getProofCmd) getCmd.PersistentFlags().BoolVar( @@ -60,11 +57,17 @@ func init() { } 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, + 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() + }, } var getCmd = &cobra.Command{ @@ -77,7 +80,7 @@ var getCmd = &cobra.Command{ return fmt.Errorf("error parsing a height:%v", err) } - namespace, err := internal.ParseV0Namespace(args[1]) + namespace, err := util.ParseV0Namespace(args[1]) if err != nil { return fmt.Errorf("error parsing a namespace:%v", err) } @@ -87,13 +90,13 @@ var getCmd = &cobra.Command{ return fmt.Errorf("error parsing a commitment:%v", err) } - blob, err := internal.RPCClient.Blob.Get(cmd.Context(), height, namespace, commitment) + blob, err := rpcClient.Blob.Get(cmd.Context(), height, namespace, commitment) formatter := formatData if base64Flag || err != nil { formatter = nil } - return internal.PrintOutput(blob, err, formatter) + return util.PrintOutput(blob, err, formatter) }, } @@ -107,12 +110,12 @@ var getAllCmd = &cobra.Command{ return fmt.Errorf("error parsing a height:%v", err) } - namespace, err := internal.ParseV0Namespace(args[1]) + namespace, err := util.ParseV0Namespace(args[1]) if err != nil { return fmt.Errorf("error parsing a namespace:%v", err) } - blobs, err := internal.RPCClient.Blob.GetAll(cmd.Context(), height, []share.Namespace{namespace}) + blobs, err := rpcClient.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()) @@ -120,7 +123,7 @@ var getAllCmd = &cobra.Command{ if base64Flag || err != nil { formatter = nil } - return internal.PrintOutput(blobs, err, formatter) + return util.PrintOutput(blobs, err, formatter) }, } @@ -129,7 +132,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 { - namespace, err := internal.ParseV0Namespace(args[0]) + namespace, err := util.ParseV0Namespace(args[0]) if err != nil { return fmt.Errorf("error parsing a namespace:%v", err) } @@ -139,7 +142,7 @@ var submitCmd = &cobra.Command{ return fmt.Errorf("error creating a blob:%v", err) } - height, err := internal.RPCClient.Blob.Submit( + height, err := rpcClient.Blob.Submit( cmd.Context(), []*blob.Blob{parsedBlob}, &blob.SubmitOptions{Fee: fee, GasLimit: gasLimit}, @@ -152,7 +155,7 @@ var submitCmd = &cobra.Command{ Height: height, Commitment: parsedBlob.Commitment, } - return internal.PrintOutput(response, err, nil) + return util.PrintOutput(response, err, nil) }, } @@ -166,7 +169,7 @@ var getProofCmd = &cobra.Command{ return fmt.Errorf("error parsing a height:%v", err) } - namespace, err := internal.ParseV0Namespace(args[1]) + namespace, err := util.ParseV0Namespace(args[1]) if err != nil { return fmt.Errorf("error parsing a namespace:%v", err) } @@ -176,8 +179,8 @@ var getProofCmd = &cobra.Command{ return fmt.Errorf("error parsing a commitment:%v", err) } - proof, err := internal.RPCClient.Blob.GetProof(cmd.Context(), height, namespace, commitment) - return internal.PrintOutput(proof, err, nil) + proof, err := rpcClient.Blob.GetProof(cmd.Context(), height, namespace, commitment) + return util.PrintOutput(proof, err, nil) }, } diff --git a/nodebuilder/das/cmds/das.go b/nodebuilder/das/cmds/das.go index f0f67a6bce..2671929522 100644 --- a/nodebuilder/das/cmds/das.go +++ b/nodebuilder/das/cmds/das.go @@ -1,28 +1,30 @@ -package rpc +package cmd import ( "github.com/spf13/cobra" - "github.com/celestiaorg/celestia-node/cmd/celestia/internal" + "github.com/celestiaorg/celestia-node/api/rpc/client" + "github.com/celestiaorg/celestia-node/cmd/celestia/util" ) +var rpcClient *client.Client + func init() { DASCmd.AddCommand(samplingStatsCmd) - - DASCmd.PersistentFlags().StringVar( - &internal.RequestURL, - "url", - "http://localhost:26658", - "Request URL", - ) } var DASCmd = &cobra.Command{ - Use: "das [command]", - Short: "Allows to interact with the Daser via JSON-RPC", - Args: cobra.NoArgs, - PersistentPreRunE: internal.InitClient, - PersistentPostRun: internal.CloseClient, + Use: "das [command]", + Short: "Allows to interact with the Daser 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() + }, } var samplingStatsCmd = &cobra.Command{ @@ -30,7 +32,7 @@ var samplingStatsCmd = &cobra.Command{ Short: "Returns the current statistics over the DA sampling process", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - stats, err := internal.RPCClient.DAS.SamplingStats(cmd.Context()) - return internal.PrintOutput(stats, err, nil) + stats, err := rpcClient.DAS.SamplingStats(cmd.Context()) + return util.PrintOutput(stats, err, nil) }, } diff --git a/nodebuilder/header/cmds/header.go b/nodebuilder/header/cmds/header.go index 3253fabecd..e93086f0b0 100644 --- a/nodebuilder/header/cmds/header.go +++ b/nodebuilder/header/cmds/header.go @@ -1,4 +1,4 @@ -package rpc +package cmd import ( "encoding/hex" @@ -7,9 +7,12 @@ import ( "github.com/spf13/cobra" - "github.com/celestiaorg/celestia-node/cmd/celestia/internal" + "github.com/celestiaorg/celestia-node/api/rpc/client" + "github.com/celestiaorg/celestia-node/cmd/celestia/util" ) +var rpcClient *client.Client + func init() { HeaderCmd.AddCommand( localHeadCmd, @@ -18,21 +21,20 @@ func init() { getByHeightCmd, syncStateCmd, ) - - HeaderCmd.PersistentFlags().StringVar( - &internal.RequestURL, - "url", - "http://localhost:26658", - "Request URL", - ) } var HeaderCmd = &cobra.Command{ - Use: "header [command]", - Short: "Allows interaction with the Header Module via JSON-RPC", - Args: cobra.NoArgs, - PersistentPreRunE: internal.InitClient, - PersistentPostRun: internal.CloseClient, + Use: "header [command]", + Short: "Allows interaction with the Header Module 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() + }, } var localHeadCmd = &cobra.Command{ @@ -40,8 +42,8 @@ var localHeadCmd = &cobra.Command{ Short: "Returns the ExtendedHeader from the chain head.", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - header, err := internal.RPCClient.Header.LocalHead(cmd.Context()) - return internal.PrintOutput(header, err, nil) + header, err := rpcClient.Header.LocalHead(cmd.Context()) + return util.PrintOutput(header, err, nil) }, } @@ -50,8 +52,8 @@ var networkHeadCmd = &cobra.Command{ Short: "Provides the Syncer's view of the current network head.", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - header, err := internal.RPCClient.Header.NetworkHead(cmd.Context()) - return internal.PrintOutput(header, err, nil) + header, err := rpcClient.Header.NetworkHead(cmd.Context()) + return util.PrintOutput(header, err, nil) }, } @@ -64,8 +66,8 @@ var getByHashCmd = &cobra.Command{ if err != nil { return fmt.Errorf("error decoding a hash: expected a hex encoded string:%v", err) } - header, err := internal.RPCClient.Header.GetByHash(cmd.Context(), hash) - return internal.PrintOutput(header, err, nil) + header, err := rpcClient.Header.GetByHash(cmd.Context(), hash) + return util.PrintOutput(header, err, nil) }, } @@ -79,8 +81,8 @@ var getByHeightCmd = &cobra.Command{ return fmt.Errorf("error parsing a height:%v", err) } - header, err := internal.RPCClient.Header.GetByHeight(cmd.Context(), height) - return internal.PrintOutput(header, err, nil) + header, err := rpcClient.Header.GetByHeight(cmd.Context(), height) + return util.PrintOutput(header, err, nil) }, } @@ -89,7 +91,7 @@ var syncStateCmd = &cobra.Command{ Short: "Returns the current state of the header Syncer.", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - header, err := internal.RPCClient.Header.SyncState(cmd.Context()) - return internal.PrintOutput(header, err, nil) + header, err := rpcClient.Header.SyncState(cmd.Context()) + return util.PrintOutput(header, err, nil) }, } diff --git a/nodebuilder/node/cmds/admin.go b/nodebuilder/node/cmds/node.go similarity index 64% rename from nodebuilder/node/cmds/admin.go rename to nodebuilder/node/cmds/node.go index dfb12e6f66..cf3555dcb8 100644 --- a/nodebuilder/node/cmds/admin.go +++ b/nodebuilder/node/cmds/node.go @@ -1,4 +1,4 @@ -package admin +package cmd import ( "errors" @@ -7,25 +7,28 @@ import ( "github.com/filecoin-project/go-jsonrpc/auth" "github.com/spf13/cobra" - "github.com/celestiaorg/celestia-node/cmd/celestia/internal" + "github.com/celestiaorg/celestia-node/api/rpc/client" + "github.com/celestiaorg/celestia-node/cmd/celestia/util" ) +var rpcClient *client.Client + func init() { NodeCmd.AddCommand(nodeInfoCmd, logCmd, verifyCmd, authCmd) - - NodeCmd.PersistentFlags().StringVar( - &internal.RequestURL, - "url", - "http://localhost:26658", - "Request URL", - ) } var NodeCmd = &cobra.Command{ - Use: "node [command]", - Short: "Allows administrating running node.", - Args: cobra.NoArgs, - PersistentPreRunE: internal.InitClient, + Use: "node [command]", + Short: "Allows administrating running node.", + 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() + }, } var nodeInfoCmd = &cobra.Command{ @@ -33,8 +36,8 @@ var nodeInfoCmd = &cobra.Command{ Args: cobra.NoArgs, Short: "Returns administrative information about the node.", RunE: func(c *cobra.Command, args []string) error { - info, err := internal.RPCClient.Node.Info(c.Context()) - return internal.PrintOutput(info, err, nil) + info, err := rpcClient.Node.Info(c.Context()) + return util.PrintOutput(info, err, nil) }, } @@ -53,7 +56,7 @@ var logCmd = &cobra.Command{ "e.g. pubsub:debug") } - if err := internal.RPCClient.Node.LogLevelSet(c.Context(), params[0], params[1]); err != nil { + if err := rpcClient.Node.LogLevelSet(c.Context(), params[0], params[1]); err != nil { return err } } @@ -67,8 +70,8 @@ var verifyCmd = &cobra.Command{ Short: "Returns the permissions assigned to the given token.", RunE: func(c *cobra.Command, args []string) error { - perms, err := internal.RPCClient.Node.AuthVerify(c.Context(), args[0]) - return internal.PrintOutput(perms, err, nil) + perms, err := rpcClient.Node.AuthVerify(c.Context(), args[0]) + return util.PrintOutput(perms, err, nil) }, } @@ -82,7 +85,7 @@ var authCmd = &cobra.Command{ perms[i] = (auth.Permission)(p) } - result, err := internal.RPCClient.Node.AuthNew(c.Context(), perms) - return internal.PrintOutput(result, err, nil) + result, err := rpcClient.Node.AuthNew(c.Context(), perms) + return util.PrintOutput(result, err, nil) }, } diff --git a/nodebuilder/p2p/cmds/p2p.go b/nodebuilder/p2p/cmds/p2p.go index 41cf34f1a4..eaca23fe3c 100644 --- a/nodebuilder/p2p/cmds/p2p.go +++ b/nodebuilder/p2p/cmds/p2p.go @@ -1,4 +1,4 @@ -package rpc +package cmd import ( "github.com/libp2p/go-libp2p/core/metrics" @@ -8,9 +8,12 @@ import ( ma2 "github.com/multiformats/go-multiaddr" "github.com/spf13/cobra" - "github.com/celestiaorg/celestia-node/cmd/celestia/internal" + "github.com/celestiaorg/celestia-node/api/rpc/client" + "github.com/celestiaorg/celestia-node/cmd/celestia/util" ) +var rpcClient *client.Client + type peerInfo struct { ID string `json:"id"` PeerAddr []string `json:"peer_addr"` @@ -35,21 +38,20 @@ func init() { bandwidthForProtocolCmd, pubsubPeersCmd, ) - - P2PCmd.PersistentFlags().StringVar( - &internal.RequestURL, - "url", - "http://localhost:26658", - "Request URL", - ) } var P2PCmd = &cobra.Command{ - Use: "p2p [command]", - Short: "Allows interaction with the P2P Module via JSON-RPC", - Args: cobra.NoArgs, - PersistentPreRunE: internal.InitClient, - PersistentPostRun: internal.CloseClient, + Use: "p2p [command]", + Short: "Allows interaction with the P2P Module 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() + }, } var infoCmd = &cobra.Command{ @@ -57,7 +59,7 @@ var infoCmd = &cobra.Command{ Short: "Gets the node's peer info (peer id and multiaddresses)", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - info, err := internal.RPCClient.P2P.Info(cmd.Context()) + info, err := rpcClient.P2P.Info(cmd.Context()) formatter := func(data interface{}) interface{} { peerAdd := data.(peer.AddrInfo) @@ -71,7 +73,7 @@ var infoCmd = &cobra.Command{ PeerAddr: ma, } } - return internal.PrintOutput(info, err, formatter) + return util.PrintOutput(info, err, formatter) }, } @@ -80,7 +82,7 @@ var peersCmd = &cobra.Command{ Short: "Lists the peers we are connected to", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - result, err := internal.RPCClient.P2P.Peers(cmd.Context()) + result, err := rpcClient.P2P.Peers(cmd.Context()) peers := make([]string, len(result)) for i, peer := range result { peers[i] = peer.String() @@ -94,7 +96,7 @@ var peersCmd = &cobra.Command{ Peers: conPeers, } } - return internal.PrintOutput(peers, err, formatter) + return util.PrintOutput(peers, err, formatter) }, } @@ -107,7 +109,7 @@ var peerInfoCmd = &cobra.Command{ if err != nil { return err } - info, err := internal.RPCClient.P2P.PeerInfo(cmd.Context(), pid) + info, err := rpcClient.P2P.PeerInfo(cmd.Context(), pid) formatter := func(data interface{}) interface{} { peerAdd := data.(peer.AddrInfo) ma := make([]string, len(info.Addrs)) @@ -120,7 +122,7 @@ var peerInfoCmd = &cobra.Command{ PeerAddr: ma, } } - return internal.PrintOutput(info, err, formatter) + return util.PrintOutput(info, err, formatter) }, } @@ -144,9 +146,9 @@ var connectCmd = &cobra.Command{ Addrs: []ma2.Multiaddr{ma}, } - err = internal.RPCClient.P2P.Connect(cmd.Context(), peerInfo) + err = rpcClient.P2P.Connect(cmd.Context(), peerInfo) if err != nil { - return internal.PrintOutput(nil, err, nil) + return util.PrintOutput(nil, err, nil) } return connectednessCmd.RunE(cmd, args) }, @@ -162,9 +164,9 @@ var closePeerCmd = &cobra.Command{ return err } - err = internal.RPCClient.P2P.ClosePeer(cmd.Context(), pid) + err = rpcClient.P2P.ClosePeer(cmd.Context(), pid) if err != nil { - return internal.PrintOutput(nil, err, nil) + return util.PrintOutput(nil, err, nil) } return connectednessCmd.RunE(cmd, args) }, @@ -180,7 +182,7 @@ var connectednessCmd = &cobra.Command{ return err } - con, err := internal.RPCClient.P2P.Connectedness(cmd.Context(), pid) + con, err := rpcClient.P2P.Connectedness(cmd.Context(), pid) formatter := func(data interface{}) interface{} { conn := data.(network.Connectedness) @@ -190,7 +192,7 @@ var connectednessCmd = &cobra.Command{ ConnectionState: conn.String(), } } - return internal.PrintOutput(con, err, formatter) + return util.PrintOutput(con, err, formatter) }, } @@ -199,7 +201,7 @@ var natStatusCmd = &cobra.Command{ Short: "Gets the currrent NAT status", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - r, err := internal.RPCClient.P2P.NATStatus(cmd.Context()) + r, err := rpcClient.P2P.NATStatus(cmd.Context()) formatter := func(data interface{}) interface{} { rr := data.(network.Reachability) @@ -209,7 +211,7 @@ var natStatusCmd = &cobra.Command{ Reachability: rr.String(), } } - return internal.PrintOutput(r, err, formatter) + return util.PrintOutput(r, err, formatter) }, } @@ -223,7 +225,7 @@ var blockPeerCmd = &cobra.Command{ return err } - err = internal.RPCClient.P2P.BlockPeer(cmd.Context(), pid) + err = rpcClient.P2P.BlockPeer(cmd.Context(), pid) formatter := func(data interface{}) interface{} { err, ok := data.(error) @@ -241,7 +243,7 @@ var blockPeerCmd = &cobra.Command{ Reason: err, } } - return internal.PrintOutput(err, nil, formatter) + return util.PrintOutput(err, nil, formatter) }, } @@ -255,7 +257,7 @@ var unblockPeerCmd = &cobra.Command{ return err } - err = internal.RPCClient.P2P.UnblockPeer(cmd.Context(), pid) + err = rpcClient.P2P.UnblockPeer(cmd.Context(), pid) formatter := func(data interface{}) interface{} { err, ok := data.(error) @@ -274,7 +276,7 @@ var unblockPeerCmd = &cobra.Command{ Reason: err, } } - return internal.PrintOutput(err, nil, formatter) + return util.PrintOutput(err, nil, formatter) }, } @@ -283,7 +285,7 @@ var blockedPeersCmd = &cobra.Command{ Short: "Lists the node's blocked peers", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - list, err := internal.RPCClient.P2P.ListBlockedPeers(cmd.Context()) + list, err := rpcClient.P2P.ListBlockedPeers(cmd.Context()) pids := make([]string, len(list)) for i, peer := range list { @@ -298,7 +300,7 @@ var blockedPeersCmd = &cobra.Command{ Peers: peers, } } - return internal.PrintOutput(pids, err, formatter) + return util.PrintOutput(pids, err, formatter) }, } @@ -312,7 +314,7 @@ var protectCmd = &cobra.Command{ return err } - err = internal.RPCClient.P2P.Protect(cmd.Context(), pid, args[1]) + err = rpcClient.P2P.Protect(cmd.Context(), pid, args[1]) formatter := func(data interface{}) interface{} { err, ok := data.(error) @@ -330,7 +332,7 @@ var protectCmd = &cobra.Command{ Reason: err, } } - return internal.PrintOutput(err, nil, formatter) + return util.PrintOutput(err, nil, formatter) }, } @@ -346,7 +348,7 @@ var unprotectCmd = &cobra.Command{ return err } - _, err = internal.RPCClient.P2P.Unprotect(cmd.Context(), pid, args[1]) + _, err = rpcClient.P2P.Unprotect(cmd.Context(), pid, args[1]) formatter := func(data interface{}) interface{} { err, ok := data.(error) @@ -364,7 +366,7 @@ var unprotectCmd = &cobra.Command{ Reason: err, } } - return internal.PrintOutput(err, nil, formatter) + return util.PrintOutput(err, nil, formatter) }, } @@ -378,8 +380,8 @@ var protectedCmd = &cobra.Command{ return err } - result, err := internal.RPCClient.P2P.IsProtected(cmd.Context(), pid, args[1]) - return internal.PrintOutput(result, err, nil) + result, err := rpcClient.P2P.IsProtected(cmd.Context(), pid, args[1]) + return util.PrintOutput(result, err, nil) }, } @@ -397,7 +399,7 @@ var bandwidthStatsCmd = &cobra.Command{ "received by the local peer, regardless of protocol or remote peer IDs", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - result, err := internal.RPCClient.P2P.BandwidthStats(cmd.Context()) + result, err := rpcClient.P2P.BandwidthStats(cmd.Context()) formatter := func(data interface{}) interface{} { stats := data.(metrics.Stats) @@ -408,7 +410,7 @@ var bandwidthStatsCmd = &cobra.Command{ RateOut: stats.RateOut, } } - return internal.PrintOutput(result, err, formatter) + return util.PrintOutput(result, err, formatter) }, } @@ -422,7 +424,7 @@ var peerBandwidthCmd = &cobra.Command{ return err } - result, err := internal.RPCClient.P2P.BandwidthForPeer(cmd.Context(), pid) + result, err := rpcClient.P2P.BandwidthForPeer(cmd.Context(), pid) formatter := func(data interface{}) interface{} { stats := data.(metrics.Stats) @@ -433,7 +435,7 @@ var peerBandwidthCmd = &cobra.Command{ RateOut: stats.RateOut, } } - return internal.PrintOutput(result, err, formatter) + return util.PrintOutput(result, err, formatter) }, } @@ -442,7 +444,7 @@ var bandwidthForProtocolCmd = &cobra.Command{ Short: "Gets stats struct with bandwidth metrics associated with the given protocol.ID", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - result, err := internal.RPCClient.P2P.BandwidthForProtocol(cmd.Context(), protocol.ID(args[0])) + result, err := rpcClient.P2P.BandwidthForProtocol(cmd.Context(), protocol.ID(args[0])) formatter := func(data interface{}) interface{} { stats := data.(metrics.Stats) @@ -453,7 +455,7 @@ var bandwidthForProtocolCmd = &cobra.Command{ RateOut: stats.RateOut, } } - return internal.PrintOutput(result, err, formatter) + return util.PrintOutput(result, err, formatter) }, } @@ -462,7 +464,7 @@ var pubsubPeersCmd = &cobra.Command{ Short: "Lists the peers we are connected to in the given topic", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - result, err := internal.RPCClient.P2P.PubSubPeers(cmd.Context(), args[0]) + result, err := rpcClient.P2P.PubSubPeers(cmd.Context(), args[0]) peers := make([]string, len(result)) for i, peer := range result { @@ -477,6 +479,6 @@ var pubsubPeersCmd = &cobra.Command{ Peers: conPeers, } } - return internal.PrintOutput(peers, err, formatter) + return util.PrintOutput(peers, err, formatter) }, } diff --git a/nodebuilder/share/cmds/share.go b/nodebuilder/share/cmds/share.go index fa6d748c78..c303ca4124 100644 --- a/nodebuilder/share/cmds/share.go +++ b/nodebuilder/share/cmds/share.go @@ -1,4 +1,4 @@ -package rpc +package cmd import ( "encoding/hex" @@ -9,18 +9,14 @@ import ( "github.com/celestiaorg/celestia-app/pkg/da" - "github.com/celestiaorg/celestia-node/cmd/celestia/internal" + "github.com/celestiaorg/celestia-node/api/rpc/client" + "github.com/celestiaorg/celestia-node/cmd/celestia/util" "github.com/celestiaorg/celestia-node/share" ) -func init() { - ShareCmd.PersistentFlags().StringVar( - &internal.RequestURL, - "url", - "http://localhost:26658", - "Request URL", - ) +var rpcClient *client.Client +func init() { ShareCmd.AddCommand( sharesAvailableCmd, probabilityOfAvailabilityCmd, @@ -31,11 +27,17 @@ func init() { } var ShareCmd = &cobra.Command{ - Use: "share [command]", - Short: "Allows interaction with the Share Module via JSON-RPC", - Args: cobra.NoArgs, - PersistentPreRunE: internal.InitClient, - PersistentPostRun: internal.CloseClient, + Use: "share [command]", + Short: "Allows interaction with the Share Module 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() + }, } var sharesAvailableCmd = &cobra.Command{ @@ -54,7 +56,7 @@ var sharesAvailableCmd = &cobra.Command{ return err } - err = internal.RPCClient.Share.SharesAvailable(cmd.Context(), &root) + err = rpcClient.Share.SharesAvailable(cmd.Context(), &root) formatter := func(data interface{}) interface{} { err, ok := data.(error) available := false @@ -71,7 +73,7 @@ var sharesAvailableCmd = &cobra.Command{ Reason: err, } } - return internal.PrintOutput(err, nil, formatter) + return util.PrintOutput(err, nil, formatter) }, } @@ -80,8 +82,8 @@ var probabilityOfAvailabilityCmd = &cobra.Command{ Short: "Calculates the probability of the data square being available based on the number of samples collected.", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - prob := internal.RPCClient.Share.ProbabilityOfAvailability(cmd.Context()) - return internal.PrintOutput(prob, nil, nil) + prob := rpcClient.Share.ProbabilityOfAvailability(cmd.Context()) + return util.PrintOutput(prob, nil, nil) }, } @@ -101,13 +103,13 @@ var getSharesByNamespaceCmd = &cobra.Command{ return err } - ns, err := internal.ParseV0Namespace(args[1]) + ns, err := util.ParseV0Namespace(args[1]) if err != nil { return err } - shares, err := internal.RPCClient.Share.GetSharesByNamespace(cmd.Context(), &root, ns) - return internal.PrintOutput(shares, err, nil) + shares, err := rpcClient.Share.GetSharesByNamespace(cmd.Context(), &root, ns) + return util.PrintOutput(shares, err, nil) }, } @@ -137,7 +139,7 @@ var getShare = &cobra.Command{ return err } - s, err := internal.RPCClient.Share.GetShare(cmd.Context(), &root, int(row), int(col)) + s, err := rpcClient.Share.GetShare(cmd.Context(), &root, int(row), int(col)) formatter := func(data interface{}) interface{} { sh, ok := data.(share.Share) @@ -155,7 +157,7 @@ var getShare = &cobra.Command{ Data: share.GetData(sh), } } - return internal.PrintOutput(s, err, formatter) + return util.PrintOutput(s, err, formatter) }, } @@ -175,8 +177,8 @@ var getEDS = &cobra.Command{ return err } - shares, err := internal.RPCClient.Share.GetEDS(cmd.Context(), &root) - return internal.PrintOutput(shares, err, nil) + shares, err := rpcClient.Share.GetEDS(cmd.Context(), &root) + return util.PrintOutput(shares, err, nil) }, } diff --git a/nodebuilder/state/cmds/state.go b/nodebuilder/state/cmds/state.go index eff2f487c3..00a427c92b 100644 --- a/nodebuilder/state/cmds/state.go +++ b/nodebuilder/state/cmds/state.go @@ -1,4 +1,4 @@ -package rpc +package cmd import ( "encoding/hex" @@ -9,11 +9,14 @@ import ( "github.com/cosmos/cosmos-sdk/types" "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/internal" + "github.com/celestiaorg/celestia-node/cmd/celestia/util" "github.com/celestiaorg/celestia-node/state" ) +var rpcClient *client.Client + func init() { StateCmd.AddCommand( accountAddressCmd, @@ -30,21 +33,20 @@ func init() { queryUnbondingCmd, queryRedelegationCmd, ) - - StateCmd.PersistentFlags().StringVar( - &internal.RequestURL, - "url", - "http://localhost:26658", - "Request URL", - ) } var StateCmd = &cobra.Command{ - Use: "state [command]", - Short: "Allows interaction with the State Module via JSON-RPC", - Args: cobra.NoArgs, - PersistentPreRunE: internal.InitClient, - PersistentPostRun: internal.CloseClient, + Use: "state [command]", + Short: "Allows interaction with the State Module 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() + }, } var accountAddressCmd = &cobra.Command{ @@ -52,8 +54,8 @@ var accountAddressCmd = &cobra.Command{ Short: "Retrieves the address of the node's account/signer.", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - address, err := internal.RPCClient.State.AccountAddress(cmd.Context()) - return internal.PrintOutput(address, err, nil) + address, err := rpcClient.State.AccountAddress(cmd.Context()) + return util.PrintOutput(address, err, nil) }, } @@ -63,8 +65,8 @@ var balanceCmd = &cobra.Command{ "the corresponding block's AppHash.", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - balance, err := internal.RPCClient.State.Balance(cmd.Context()) - return internal.PrintOutput(balance, err, nil) + balance, err := rpcClient.State.Balance(cmd.Context()) + return util.PrintOutput(balance, err, nil) }, } @@ -79,8 +81,8 @@ var balanceForAddressCmd = &cobra.Command{ return fmt.Errorf("error parsing an address:%v", err) } - balance, err := internal.RPCClient.State.BalanceForAddress(cmd.Context(), addr) - return internal.PrintOutput(balance, err, nil) + balance, err := rpcClient.State.BalanceForAddress(cmd.Context(), addr) + return util.PrintOutput(balance, err, nil) }, } @@ -107,13 +109,13 @@ var transferCmd = &cobra.Command{ return fmt.Errorf("error parsing a gas limit:%v", err) } - txResponse, err := internal.RPCClient.State.Transfer( + txResponse, err := rpcClient.State.Transfer( cmd.Context(), addr.Address.(state.AccAddress), math.NewInt(amount), math.NewInt(fee), gasLimit, ) - return internal.PrintOutput(txResponse, err, nil) + return util.PrintOutput(txResponse, err, nil) }, } @@ -128,11 +130,11 @@ var submitTxCmd = &cobra.Command{ } fmt.Println(args[0]) - txResponse, err := internal.RPCClient.State.SubmitTx( + txResponse, err := rpcClient.State.SubmitTx( cmd.Context(), decoded, ) - return internal.PrintOutput(txResponse, err, nil) + return util.PrintOutput(txResponse, err, nil) }, } @@ -141,7 +143,7 @@ var submitPFBCmd = &cobra.Command{ Short: "Allows to submit PFBs", Args: cobra.ExactArgs(4), RunE: func(cmd *cobra.Command, args []string) error { - namespace, err := internal.ParseV0Namespace(args[0]) + namespace, err := util.ParseV0Namespace(args[0]) if err != nil { return fmt.Errorf("error parsing a namespace:%v", err) } @@ -161,13 +163,13 @@ var submitPFBCmd = &cobra.Command{ return fmt.Errorf("error creating a blob:%v", err) } - txResp, err := internal.RPCClient.State.SubmitPayForBlob( + txResp, err := rpcClient.State.SubmitPayForBlob( cmd.Context(), types.NewInt(fee), gasLimit, []*blob.Blob{parsedBlob}, ) - return internal.PrintOutput(txResp, err, nil) + return util.PrintOutput(txResp, err, nil) }, } @@ -201,7 +203,7 @@ var cancelUnbondingDelegationCmd = &cobra.Command{ return fmt.Errorf("error parsing a gas limit:%v", err) } - txResponse, err := internal.RPCClient.State.CancelUnbondingDelegation( + txResponse, err := rpcClient.State.CancelUnbondingDelegation( cmd.Context(), addr.Address.(state.ValAddress), math.NewInt(amount), @@ -209,7 +211,7 @@ var cancelUnbondingDelegationCmd = &cobra.Command{ math.NewInt(fee), gasLimit, ) - return internal.PrintOutput(txResponse, err, nil) + return util.PrintOutput(txResponse, err, nil) }, } @@ -242,7 +244,7 @@ var beginRedelegateCmd = &cobra.Command{ return fmt.Errorf("error parsing a gas limit:%v", err) } - txResponse, err := internal.RPCClient.State.BeginRedelegate( + txResponse, err := rpcClient.State.BeginRedelegate( cmd.Context(), srcAddr.Address.(state.ValAddress), dstAddr.Address.(state.ValAddress), @@ -250,7 +252,7 @@ var beginRedelegateCmd = &cobra.Command{ math.NewInt(fee), gasLimit, ) - return internal.PrintOutput(txResponse, err, nil) + return util.PrintOutput(txResponse, err, nil) }, } @@ -277,14 +279,14 @@ var undelegateCmd = &cobra.Command{ return fmt.Errorf("error parsing a gas limit:%v", err) } - txResponse, err := internal.RPCClient.State.Undelegate( + txResponse, err := rpcClient.State.Undelegate( cmd.Context(), addr.Address.(state.ValAddress), math.NewInt(amount), math.NewInt(fee), gasLimit, ) - return internal.PrintOutput(txResponse, err, nil) + return util.PrintOutput(txResponse, err, nil) }, } @@ -313,14 +315,14 @@ var delegateCmd = &cobra.Command{ return fmt.Errorf("error parsing a gas limit:%v", err) } - txResponse, err := internal.RPCClient.State.Delegate( + txResponse, err := rpcClient.State.Delegate( cmd.Context(), addr.Address.(state.ValAddress), math.NewInt(amount), math.NewInt(fee), gasLimit, ) - return internal.PrintOutput(txResponse, err, nil) + return util.PrintOutput(txResponse, err, nil) }, } @@ -334,10 +336,10 @@ var queryDelegationCmd = &cobra.Command{ return fmt.Errorf("error parsing an address:%v", err) } - balance, err := internal.RPCClient.State.QueryDelegation(cmd.Context(), addr.Address.(state.ValAddress)) + balance, err := rpcClient.State.QueryDelegation(cmd.Context(), addr.Address.(state.ValAddress)) fmt.Println(balance) fmt.Println(err) - return internal.PrintOutput(balance, err, nil) + return util.PrintOutput(balance, err, nil) }, } @@ -351,8 +353,8 @@ var queryUnbondingCmd = &cobra.Command{ return fmt.Errorf("error parsing an address:%v", err) } - response, err := internal.RPCClient.State.QueryUnbonding(cmd.Context(), addr.Address.(state.ValAddress)) - return internal.PrintOutput(response, err, nil) + response, err := rpcClient.State.QueryUnbonding(cmd.Context(), addr.Address.(state.ValAddress)) + return util.PrintOutput(response, err, nil) }, } @@ -371,12 +373,12 @@ var queryRedelegationCmd = &cobra.Command{ return fmt.Errorf("error parsing a dst address:%v", err) } - response, err := internal.RPCClient.State.QueryRedelegations( + response, err := rpcClient.State.QueryRedelegations( cmd.Context(), srcAddr.Address.(state.ValAddress), dstAddr.Address.(state.ValAddress), ) - return internal.PrintOutput(response, err, nil) + return util.PrintOutput(response, err, nil) }, }