9 "github.com/spf13/cobra"
10 jww "github.com/spf13/jwalterweatherman"
12 chainjson "github.com/bytom/encoding/json"
13 "github.com/bytom/util"
16 var getBlockHashCmd = &cobra.Command{
17 Use: "get-block-hash",
18 Short: "Get the hash of most recent block",
20 Run: func(cmd *cobra.Command, args []string) {
21 data, exitCode := util.ClientCall("get-block-hash")
22 if exitCode != util.Success {
29 var getBlockCountCmd = &cobra.Command{
30 Use: "get-block-count",
31 Short: "Get the number of most recent block",
33 Run: func(cmd *cobra.Command, args []string) {
34 data, exitCode := util.ClientCall("/get-block-count")
35 if exitCode != util.Success {
42 var getBlockCmd = &cobra.Command{
43 Use: "get-block <hash> | <height>",
44 Short: "Get a whole block matching the given hash or height",
45 Args: cobra.ExactArgs(1),
46 Run: func(cmd *cobra.Command, args []string) {
47 var hash chainjson.HexBytes
52 for _, ch := range args[0] {
53 // check whether the char is hex digit
54 if !(unicode.IsNumber(ch) || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
55 jww.ERROR.Printf("Invalid value for hash or height")
56 os.Exit(util.ErrLocalExe)
59 if !unicode.IsNumber(ch) {
65 if len(args[0]) != 64 {
66 jww.ERROR.Printf("Invalid hash length")
67 os.Exit(util.ErrLocalExe)
70 hash, err = hex.DecodeString(args[0])
72 jww.ERROR.Println(err)
73 os.Exit(util.ErrLocalExe)
76 height, err = strconv.ParseUint(args[0], 10, 64)
78 jww.ERROR.Printf("Invalid height value")
79 os.Exit(util.ErrLocalExe)
84 BlockHeight uint64 `json:"block_height"`
85 BlockHash chainjson.HexBytes `json:"block_hash"`
86 }{BlockHeight: height, BlockHash: hash}
88 data, exitCode := util.ClientCall("/get-block", blockReq)
89 if exitCode != util.Success {
96 var getBlockHeaderCmd = &cobra.Command{
97 Use: "get-block-header <hash> | <height>",
98 Short: "Get the header of a block matching the given hash or height",
99 Args: cobra.ExactArgs(1),
100 Run: func(cmd *cobra.Command, args []string) {
101 var hash chainjson.HexBytes
106 for _, ch := range args[0] {
107 // check whether the char is hex digit
108 if !(unicode.IsNumber(ch) || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
109 jww.ERROR.Printf("Invalid value for hash or height")
110 os.Exit(util.ErrLocalExe)
113 if !unicode.IsNumber(ch) {
119 if len(args[0]) != 64 {
120 jww.ERROR.Printf("Invalid hash length")
121 os.Exit(util.ErrLocalExe)
124 hash, err = hex.DecodeString(args[0])
126 jww.ERROR.Println(err)
127 os.Exit(util.ErrLocalExe)
130 height, err = strconv.ParseUint(args[0], 10, 64)
132 jww.ERROR.Printf("Invalid height value")
133 os.Exit(util.ErrLocalExe)
138 BlockHeight uint64 `json:"block_height"`
139 BlockHash chainjson.HexBytes `json:"block_hash"`
140 }{BlockHeight: height, BlockHash: hash}
142 data, exitCode := util.ClientCall("/get-block-header", &req)
143 if exitCode != util.Success {