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

feat(cmd): use rpc client instead of http.Request #2521

Merged
merged 13 commits into from
Aug 7, 2023
200 changes: 200 additions & 0 deletions cmd/celestia/blob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package main

import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"strconv"

"github.com/spf13/cobra"

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

type response struct {
Result interface{} `json:"result"`
}
Wondertan marked this conversation as resolved.
Show resolved Hide resolved

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

var getCmd = &cobra.Command{
Use: "get [params]",
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
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
}

num, err := strconv.ParseUint(args[0], 10, 64)
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("error parsing a height:%v", err)
}

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

commitment, err := base64.StdEncoding.DecodeString(args[2])
if err != nil {
return fmt.Errorf("error parsing a commitment:%v", err)
}

var output []byte
blob, err := client.Blob.Get(cmd.Context(), num, namespace, commitment)
if err != nil {
output, err = prepareOutput(err)
if err != nil {
return err
}
} else {
output, err = prepareOutput(blob)
if err != nil {
return err
}
}

fmt.Fprintln(os.Stdout, string(output))
return nil
},
}

var getAllCmd = &cobra.Command{
Use: "getAll [params]",
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
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])
if err != nil {
return fmt.Errorf("error parsing a namespace:%v", err)
}

var output []byte
blobs, err := client.Blob.GetAll(cmd.Context(), height, []share.Namespace{namespace})
if err != nil {
output, err = prepareOutput(err)
if err != nil {
return err
}
} else {
output, err = prepareOutput(blobs)
if err != nil {
return err
}
}

fmt.Fprintln(os.Stdout, string(output))
return nil
},
}

var submitCmd = &cobra.Command{
Use: "submit [params]",
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])
if err != nil {
return fmt.Errorf("error parsing a namespace:%v", err)
}

parsedBlob, err := blob.NewBlobV0(namespace, []byte(args[1]))
if err != nil {
return fmt.Errorf("error creating a blob:%v", err)
}

var output []byte
height, err := client.Blob.Submit(cmd.Context(), []*blob.Blob{parsedBlob})
if err != nil {
output, err = prepareOutput(err)
if err != nil {
return err
}
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
} else {
output, err = prepareOutput(height)
if err != nil {
return err
}
}

fmt.Fprintln(os.Stdout, string(output))
return nil
},
}

var getProofCmd = &cobra.Command{
Use: "getProof [params]",
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])
if err != nil {
return fmt.Errorf("error parsing a namespace:%v", err)
}

commitment, err := base64.StdEncoding.DecodeString(args[2])
if err != nil {
return fmt.Errorf("error parsing a commitment:%v", err)
}

var output []byte
proof, err := client.Blob.GetProof(cmd.Context(), height, namespace, commitment)
if err != nil {
output, err = prepareOutput(err)
if err != nil {
return err
}
} else {
output, err = prepareOutput(proof)
if err != nil {
return err
}
}

fmt.Fprintln(os.Stdout, string(output))
return nil
},
}

func prepareOutput(data interface{}) ([]byte, error) {
bytes, err := json.MarshalIndent(response{
Result: data,
}, "", " ")
vgonkivs marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
return bytes, nil
}
Loading
Loading