Skip to content

Commit

Permalink
feat(cmd/rpc): add commands for the p2p module (celestiaorg#2599)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Oct 5, 2023
1 parent 403a39e commit e6e8402
Show file tree
Hide file tree
Showing 3 changed files with 591 additions and 44 deletions.
58 changes: 14 additions & 44 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 @@ -85,8 +83,11 @@ var getCmd = &cobra.Command{

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

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

Expand All @@ -112,8 +113,11 @@ var getAllCmd = &cobra.Command{

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

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

Expand Down Expand Up @@ -150,9 +154,7 @@ var submitCmd = &cobra.Command{
Height: height,
Commitment: parsedBlob.Commitment,
}

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

Expand Down Expand Up @@ -182,35 +184,10 @@ var getProofCmd = &cobra.Command{
}

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

printOutput(proof, err)
return nil
return printOutput(proof, err, 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 @@ -220,11 +197,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 @@ -237,10 +210,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

0 comments on commit e6e8402

Please sign in to comment.