-
Notifications
You must be signed in to change notification settings - Fork 894
/
header.go
117 lines (101 loc) · 2.89 KB
/
header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cmd
import (
"encoding/hex"
"fmt"
"strconv"
"github.com/spf13/cobra"
cmdnode "github.com/celestiaorg/celestia-node/cmd"
)
func init() {
Cmd.AddCommand(
localHeadCmd,
networkHeadCmd,
getByHashCmd,
getByHeightCmd,
syncStateCmd,
)
}
var Cmd = &cobra.Command{
Use: "header [command]",
Short: "Allows interaction with the Header Module via JSON-RPC",
Args: cobra.NoArgs,
PersistentPreRunE: cmdnode.InitClient,
}
var localHeadCmd = &cobra.Command{
Use: "local-head",
Short: "Returns the ExtendedHeader from the chain head.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
client, err := cmdnode.ParseClientFromCtx(cmd.Context())
if err != nil {
return err
}
defer client.Close()
header, err := client.Header.LocalHead(cmd.Context())
return cmdnode.PrintOutput(header, err, nil)
},
}
var networkHeadCmd = &cobra.Command{
Use: "network-head",
Short: "Provides the Syncer's view of the current network head.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
client, err := cmdnode.ParseClientFromCtx(cmd.Context())
if err != nil {
return err
}
defer client.Close()
header, err := client.Header.NetworkHead(cmd.Context())
return cmdnode.PrintOutput(header, err, nil)
},
}
var getByHashCmd = &cobra.Command{
Use: "get-by-hash",
Short: "Returns the header of the given hash from the node's header store.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := cmdnode.ParseClientFromCtx(cmd.Context())
if err != nil {
return err
}
defer client.Close()
hash, err := hex.DecodeString(args[0])
if err != nil {
return fmt.Errorf("error decoding a hash: expected a hex encoded string: %w", err)
}
header, err := client.Header.GetByHash(cmd.Context(), hash)
return cmdnode.PrintOutput(header, err, nil)
},
}
var getByHeightCmd = &cobra.Command{
Use: "get-by-height",
Short: "Returns the ExtendedHeader at the given height if it is currently available.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := cmdnode.ParseClientFromCtx(cmd.Context())
if err != nil {
return err
}
defer client.Close()
height, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("error parsing a height: %w", err)
}
header, err := client.Header.GetByHeight(cmd.Context(), height)
return cmdnode.PrintOutput(header, err, nil)
},
}
var syncStateCmd = &cobra.Command{
Use: "sync-state",
Short: "Returns the current state of the header Syncer.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
client, err := cmdnode.ParseClientFromCtx(cmd.Context())
if err != nil {
return err
}
defer client.Close()
header, err := client.Header.SyncState(cmd.Context())
return cmdnode.PrintOutput(header, err, nil)
},
}