Skip to content

Commit

Permalink
feat(rpc): add share cmd (celestiaorg#2664)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs authored Sep 13, 2023
1 parent 74fe698 commit 0794b5d
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/celestia/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func init() {
rpcCmd.AddCommand(p2pCmd)
rpcCmd.AddCommand(dasCmd)
rpcCmd.AddCommand(headerCmd)
rpcCmd.AddCommand(shareCmd)
rootCmd.AddCommand(rpcCmd)
}

Expand Down
196 changes: 196 additions & 0 deletions cmd/celestia/share.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package main

import (
"encoding/hex"
"encoding/json"
"strconv"

"github.com/spf13/cobra"

"github.com/celestiaorg/celestia-app/pkg/da"

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

func init() {
shareCmd.AddCommand(
sharesAvailableCmd,
probabilityOfAvailabilityCmd,
getSharesByNamespaceCmd,
getShare,
getEDS,
)
}

var shareCmd = &cobra.Command{
Use: "share [command]",
Short: "Allows interaction with the Share Module via JSON-RPC",
Args: cobra.NoArgs,
}

var sharesAvailableCmd = &cobra.Command{
Use: "available",
Short: "Subjectively validates if Shares committed to the given Root are available on the Network.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := rpcClient(cmd.Context())
if err != nil {
return err
}

raw, err := parseJSON(args[0])
if err != nil {
return err
}

root := da.MinDataAvailabilityHeader()
err = json.Unmarshal(raw, &root)
if err != nil {
return err
}

err = client.Share.SharesAvailable(cmd.Context(), &root)
formatter := func(data interface{}) interface{} {
err, ok := data.(error)
available := false
if !ok {
available = true
}
return struct {
Available bool `json:"available"`
Hash []byte `json:"dah_hash"`
Reason error `json:"reason,omitempty"`
}{
Available: available,
Hash: []byte(args[0]),
Reason: err,
}
}
return printOutput(err, nil, formatter)
},
}

var probabilityOfAvailabilityCmd = &cobra.Command{
Use: "availability",
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 {
client, err := rpcClient(cmd.Context())
if err != nil {
return err
}

prob := client.Share.ProbabilityOfAvailability(cmd.Context())
return printOutput(prob, nil, nil)
},
}

var getSharesByNamespaceCmd = &cobra.Command{
Use: "get-by-namespace [dah, namespace]",
Short: "Gets all shares from an EDS within the given namespace.",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := rpcClient(cmd.Context())
if err != nil {
return err
}

raw, err := parseJSON(args[0])
if err != nil {
return err
}

root := da.MinDataAvailabilityHeader()
err = json.Unmarshal(raw, &root)
if err != nil {
return err
}

ns, err := parseV0Namespace(args[1])
if err != nil {
return err
}

shares, err := client.Share.GetSharesByNamespace(cmd.Context(), &root, ns)
return printOutput(shares, err, nil)
},
}

var getShare = &cobra.Command{
Use: "get-share [dah, row, col]",
Short: "Gets a Share by coordinates in EDS.",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := rpcClient(cmd.Context())
if err != nil {
return err
}

raw, err := parseJSON(args[0])
if err != nil {
return err
}

root := da.MinDataAvailabilityHeader()
err = json.Unmarshal(raw, &root)
if err != nil {
return err
}

row, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
}

col, err := strconv.ParseInt(args[2], 10, 64)
if err != nil {
return err
}

s, err := client.Share.GetShare(cmd.Context(), &root, int(row), int(col))

formatter := func(data interface{}) interface{} {
sh, ok := data.(share.Share)
if !ok {
return data
}

ns := hex.EncodeToString(share.GetNamespace(sh))

return struct {
Namespace string `json:"namespace"`
Data []byte `json:"data"`
}{
Namespace: ns,
Data: share.GetData(sh),
}
}
return printOutput(s, err, formatter)
},
}

var getEDS = &cobra.Command{
Use: "get-eds [dah]",
Short: "Gets the full EDS identified by the given root",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := rpcClient(cmd.Context())
if err != nil {
return err
}

raw, err := parseJSON(args[0])
if err != nil {
return err
}

root := da.MinDataAvailabilityHeader()
err = json.Unmarshal(raw, &root)
if err != nil {
return err
}

shares, err := client.Share.GetEDS(cmd.Context(), &root)
return printOutput(shares, err, nil)
},
}

0 comments on commit 0794b5d

Please sign in to comment.