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/rpc): add commands for the p2p module #2599

54 changes: 16 additions & 38 deletions cmd/celestia/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

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

Expand Down Expand Up @@ -66,7 +64,12 @@ var getCmd = &cobra.Command{

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

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

printOutput(blob, err, formatter)
return nil
},
}
Expand All @@ -93,7 +96,12 @@ var getAllCmd = &cobra.Command{

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

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

printOutput(blobs, err, formatter)
return nil
},
}
Expand Down Expand Up @@ -128,7 +136,7 @@ var submitCmd = &cobra.Command{
Commitment: parsedBlob.Commitment,
}

printOutput(response, err)
printOutput(response, err, nil)
return nil
},
}
Expand Down Expand Up @@ -160,34 +168,11 @@ var getProofCmd = &cobra.Command{

proof, err := client.Blob.GetProof(cmd.Context(), height, namespace, commitment)

printOutput(proof, err)
printOutput(proof, err, nil)
return nil
},
}

func printOutput(data interface{}, err error) {
if err != nil {
data = err
}

if !base64Flag && err == nil {
data = formatData(data)
}

resp := struct {
Result interface{} `json:"result"`
}{
Result: data,
}

bytes, err := json.MarshalIndent(resp, "", " ")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Fprintln(os.Stdout, string(bytes))
}

func formatData(data interface{}) interface{} {
type tempBlob struct {
Namespace []byte `json:"namespace"`
Expand All @@ -197,11 +182,7 @@ func formatData(data interface{}) interface{} {
}

if reflect.TypeOf(data).Kind() == reflect.Slice {
blobs, ok := data.([]*blob.Blob)
if !ok {
return data
}

blobs := data.([]*blob.Blob)
result := make([]tempBlob, len(blobs))
for i, b := range blobs {
result[i] = tempBlob{
Expand All @@ -214,10 +195,7 @@ func formatData(data interface{}) interface{} {
return result
}

b, ok := data.(*blob.Blob)
if !ok {
return data
}
b := data.(*blob.Blob)
return tempBlob{
Namespace: b.Namespace(),
Data: string(b.Data),
Expand Down
Loading
Loading