Skip to content

Commit c44c988

Browse files
vgonkivsramin
andauthored
misc(cel-shed): add a data hash exists | get helper to cel-shed (#4736)
Co-authored-by: ramin <[email protected]>
1 parent f109913 commit c44c988

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

cmd/cel-shed/hash_presence.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/hex"
6+
"encoding/json"
7+
"fmt"
8+
"io"
9+
10+
"github.com/spf13/cobra"
11+
12+
"github.com/celestiaorg/celestia-node/share"
13+
"github.com/celestiaorg/celestia-node/store"
14+
)
15+
16+
const (
17+
nodeStorePathFlag = "node.store"
18+
)
19+
20+
func init() {
21+
hashCmd.PersistentFlags().String("node.store", "", "Path to the node store")
22+
23+
hashCmd.AddCommand(hasHashCmd, getByHashCmd)
24+
}
25+
26+
var hashCmd = &cobra.Command{
27+
Use: "hash [subcommand] [flags]",
28+
Short: "Commands to interact with EDS hashes",
29+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
30+
path, err := cmd.Flags().GetString(nodeStorePathFlag)
31+
if err != nil {
32+
return err
33+
}
34+
35+
rootHash, err := parseHash(args[0])
36+
if err != nil {
37+
return err
38+
}
39+
st, err := store.NewStore(store.DefaultParameters(), path)
40+
if err != nil {
41+
return fmt.Errorf("creating a store: %w", err)
42+
}
43+
44+
ctx := cmd.Context()
45+
46+
ctx = context.WithValue(ctx, hashKey{}, rootHash)
47+
ctx = context.WithValue(ctx, edsStoreKey{}, st)
48+
cmd.SetContext(ctx)
49+
return nil
50+
},
51+
}
52+
53+
var hasHashCmd = &cobra.Command{
54+
Use: "has [root hash] [flags]",
55+
Aliases: []string{"exists"},
56+
Short: "Command to check whether a hash is present in the eds store",
57+
Args: cobra.ExactArgs(1),
58+
RunE: func(cmd *cobra.Command, _ []string) error {
59+
rootHash, store, err := parseDataFromContext(cmd.Context())
60+
if err != nil {
61+
return err
62+
}
63+
64+
exists, err := store.HasByHash(context.Background(), rootHash)
65+
if err != nil {
66+
return fmt.Errorf("checking for root hash: %w", err)
67+
}
68+
69+
if exists {
70+
fmt.Printf("Root hash %s exists.\n", rootHash)
71+
} else {
72+
fmt.Printf("Root hash %s does not exist.\n", rootHash)
73+
}
74+
return nil
75+
},
76+
}
77+
78+
var getByHashCmd = &cobra.Command{
79+
Use: "get [root hash] [flag]",
80+
Short: "Command to get the eds from the eds store by its hash",
81+
Args: cobra.ExactArgs(1),
82+
RunE: func(cmd *cobra.Command, _ []string) error {
83+
rootHash, store, err := parseDataFromContext(cmd.Context())
84+
if err != nil {
85+
return err
86+
}
87+
88+
eds, err := store.GetByHash(context.Background(), rootHash)
89+
if err != nil {
90+
return err
91+
}
92+
93+
fmt.Printf("Retrieved EDS for root hash: %s\n\n", rootHash)
94+
95+
reader, err := eds.Reader()
96+
if err != nil {
97+
return err
98+
}
99+
data, err := io.ReadAll(reader)
100+
if err != nil {
101+
return err
102+
}
103+
output, err := json.MarshalIndent(data, "", " ")
104+
if err != nil {
105+
return err
106+
}
107+
108+
fmt.Println(string(output))
109+
return nil
110+
},
111+
}
112+
113+
func parseHash(str string) (root share.DataHash, err error) {
114+
hexData, err := hex.DecodeString(str)
115+
if err != nil {
116+
return nil, fmt.Errorf("failed to decode hex string: %w", err)
117+
}
118+
return hexData, nil
119+
}
120+
121+
type (
122+
hashKey struct{}
123+
edsStoreKey struct{}
124+
)
125+
126+
func parseDataFromContext(ctx context.Context) (share.DataHash, *store.Store, error) {
127+
hash, ok := ctx.Value(hashKey{}).(share.DataHash)
128+
if !ok {
129+
return nil, nil, fmt.Errorf("root hash not found in context")
130+
}
131+
132+
st, ok := ctx.Value(edsStoreKey{}).(*store.Store)
133+
if !ok {
134+
return nil, nil, fmt.Errorf("eds store not found in context")
135+
}
136+
return hash, st, nil
137+
}

cmd/cel-shed/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func init() {
13-
rootCmd.AddCommand(p2pCmd, headerCmd, edsStoreCmd, shwapCmd, squareCmd)
13+
rootCmd.AddCommand(p2pCmd, headerCmd, edsStoreCmd, shwapCmd, squareCmd, hashCmd)
1414
}
1515

1616
var rootCmd = &cobra.Command{

0 commit comments

Comments
 (0)