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 blockHashCmd = &cobra.Command{
18 Short: "Get the hash of most recent block",
20 Run: func(cmd *cobra.Command, args []string) {
21 data, exitCode := util.ClientCall("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 getBlockHeaderByHashCmd = &cobra.Command{
97 Use: "get-block-header-by-hash <hash>",
98 Short: "Get the header of a block matching the given hash",
99 Args: cobra.ExactArgs(1),
100 Run: func(cmd *cobra.Command, args []string) {
101 data, exitCode := util.ClientCall("/get-block-header-by-hash", args[0])
102 if exitCode != util.Success {
109 var getBlockTransactionsCountByHashCmd = &cobra.Command{
110 Use: "get-block-transactions-count-by-hash <hash>",
111 Short: "Get the transactions count of a block matching the given hash",
112 Args: cobra.ExactArgs(1),
113 Run: func(cmd *cobra.Command, args []string) {
114 data, exitCode := util.ClientCall("/get-block-transactions-count-by-hash", args[0])
115 if exitCode != util.Success {
122 var getBlockTransactionsCountByHeightCmd = &cobra.Command{
123 Use: "get-block-transactions-count-by-height <height>",
124 Short: "Get the transactions count of a block matching the given height",
125 Args: cobra.ExactArgs(1),
126 Run: func(cmd *cobra.Command, args []string) {
127 ui64, err := strconv.ParseUint(args[0], 10, 64)
129 jww.ERROR.Printf("Invalid height value")
130 os.Exit(util.ErrLocalExe)
133 data, exitCode := util.ClientCall("/get-block-transactions-count-by-height", ui64)
134 if exitCode != util.Success {