9 "github.com/spf13/cobra"
10 jww "github.com/spf13/jwalterweatherman"
12 chainjson "github.com/bytom/encoding/json"
13 "github.com/bytom/util"
17 getDifficultyCmd.PersistentFlags().StringVar(&blockHash, "hash", "", "hash of block")
18 getDifficultyCmd.PersistentFlags().IntVar(&blockHeight, "height", 0, "height of block")
20 getHashRateCmd.PersistentFlags().StringVar(&blockHash, "hash", "", "hash of block")
21 getHashRateCmd.PersistentFlags().IntVar(&blockHeight, "height", 0, "height of block")
29 var getBlockHashCmd = &cobra.Command{
30 Use: "get-block-hash",
31 Short: "Get the hash of most recent block",
33 Run: func(cmd *cobra.Command, args []string) {
34 data, exitCode := util.ClientCall("get-block-hash")
35 if exitCode != util.Success {
42 var getBlockCountCmd = &cobra.Command{
43 Use: "get-block-count",
44 Short: "Get the number of most recent block",
46 Run: func(cmd *cobra.Command, args []string) {
47 data, exitCode := util.ClientCall("/get-block-count")
48 if exitCode != util.Success {
55 var getBlockCmd = &cobra.Command{
56 Use: "get-block <hash> | <height>",
57 Short: "Get a whole block matching the given hash or height",
58 Args: cobra.ExactArgs(1),
59 Run: func(cmd *cobra.Command, args []string) {
60 var hash chainjson.HexBytes
65 for _, ch := range args[0] {
66 // check whether the char is hex digit
67 if !(unicode.IsNumber(ch) || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
68 jww.ERROR.Printf("Invalid value for hash or height")
69 os.Exit(util.ErrLocalExe)
72 if !unicode.IsNumber(ch) {
78 if len(args[0]) != 64 {
79 jww.ERROR.Printf("Invalid hash length")
80 os.Exit(util.ErrLocalExe)
83 hash, err = hex.DecodeString(args[0])
85 jww.ERROR.Println(err)
86 os.Exit(util.ErrLocalExe)
89 height, err = strconv.ParseUint(args[0], 10, 64)
91 jww.ERROR.Printf("Invalid height value")
92 os.Exit(util.ErrLocalExe)
97 BlockHeight uint64 `json:"block_height"`
98 BlockHash chainjson.HexBytes `json:"block_hash"`
99 }{BlockHeight: height, BlockHash: hash}
101 data, exitCode := util.ClientCall("/get-block", blockReq)
102 if exitCode != util.Success {
109 var getBlockHeaderCmd = &cobra.Command{
110 Use: "get-block-header <hash> | <height>",
111 Short: "Get the header of a block matching the given hash or height",
112 Args: cobra.ExactArgs(1),
113 Run: func(cmd *cobra.Command, args []string) {
114 var hash chainjson.HexBytes
119 for _, ch := range args[0] {
120 // check whether the char is hex digit
121 if !(unicode.IsNumber(ch) || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
122 jww.ERROR.Printf("Invalid value for hash or height")
123 os.Exit(util.ErrLocalExe)
126 if !unicode.IsNumber(ch) {
132 if len(args[0]) != 64 {
133 jww.ERROR.Printf("Invalid hash length")
134 os.Exit(util.ErrLocalExe)
137 hash, err = hex.DecodeString(args[0])
139 jww.ERROR.Println(err)
140 os.Exit(util.ErrLocalExe)
143 height, err = strconv.ParseUint(args[0], 10, 64)
145 jww.ERROR.Printf("Invalid height value")
146 os.Exit(util.ErrLocalExe)
151 BlockHeight uint64 `json:"block_height"`
152 BlockHash chainjson.HexBytes `json:"block_hash"`
153 }{BlockHeight: height, BlockHash: hash}
155 data, exitCode := util.ClientCall("/get-block-header", req)
156 if exitCode != util.Success {
163 var getDifficultyCmd = &cobra.Command{
164 Use: "get-difficulty",
165 Short: "Get the difficulty of most recent block",
167 Run: func(cmd *cobra.Command, args []string) {
168 var hash chainjson.HexBytes
172 hash, err = hex.DecodeString(blockHash)
174 jww.ERROR.Println(err)
175 os.Exit(util.ErrLocalExe)
180 BlockHeight uint64 `json:"block_height"`
181 BlockHash chainjson.HexBytes `json:"block_hash"`
182 }{BlockHeight: uint64(blockHeight), BlockHash: hash}
184 data, exitCode := util.ClientCall("/get-difficulty", req)
185 if exitCode != util.Success {
192 var getHashRateCmd = &cobra.Command{
193 Use: "get-hash-rate",
194 Short: "Get the nonce of most recent block",
196 Run: func(cmd *cobra.Command, args []string) {
197 var hash chainjson.HexBytes
201 hash, err = hex.DecodeString(blockHash)
203 jww.ERROR.Println(err)
204 os.Exit(util.ErrLocalExe)
209 BlockHeight uint64 `json:"block_height"`
210 BlockHash chainjson.HexBytes `json:"block_hash"`
211 }{BlockHeight: uint64(blockHeight), BlockHash: hash}
213 data, exitCode := util.ClientCall("/get-hash-rate", req)
214 if exitCode != util.Success {