|
| 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 | +} |
0 commit comments