OSDN Git Service

Merge pull request #690 from Bytom/skip-solve-in-tests
[bytom/bytom.git] / cmd / bytomcli / commands / block.go
1 package commands
2
3 import (
4         "encoding/hex"
5         "os"
6         "strconv"
7         "unicode"
8
9         "github.com/spf13/cobra"
10         jww "github.com/spf13/jwalterweatherman"
11
12         chainjson "github.com/bytom/encoding/json"
13         "github.com/bytom/util"
14 )
15
16 var getBlockHashCmd = &cobra.Command{
17         Use:   "get-block-hash",
18         Short: "Get the hash of most recent block",
19         Args:  cobra.NoArgs,
20         Run: func(cmd *cobra.Command, args []string) {
21                 data, exitCode := util.ClientCall("get-block-hash")
22                 if exitCode != util.Success {
23                         os.Exit(exitCode)
24                 }
25                 printJSON(data)
26         },
27 }
28
29 var getBlockCountCmd = &cobra.Command{
30         Use:   "get-block-count",
31         Short: "Get the number of most recent block",
32         Args:  cobra.NoArgs,
33         Run: func(cmd *cobra.Command, args []string) {
34                 data, exitCode := util.ClientCall("/get-block-count")
35                 if exitCode != util.Success {
36                         os.Exit(exitCode)
37                 }
38                 printJSON(data)
39         },
40 }
41
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
48                 var height uint64
49                 var err error
50                 isNumber := false
51
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)
57                         }
58
59                         if !unicode.IsNumber(ch) {
60                                 isNumber = true
61                         }
62                 }
63
64                 if isNumber {
65                         if len(args[0]) != 64 {
66                                 jww.ERROR.Printf("Invalid hash length")
67                                 os.Exit(util.ErrLocalExe)
68                         }
69
70                         hash, err = hex.DecodeString(args[0])
71                         if err != nil {
72                                 jww.ERROR.Println(err)
73                                 os.Exit(util.ErrLocalExe)
74                         }
75                 } else {
76                         height, err = strconv.ParseUint(args[0], 10, 64)
77                         if err != nil {
78                                 jww.ERROR.Printf("Invalid height value")
79                                 os.Exit(util.ErrLocalExe)
80                         }
81                 }
82
83                 blockReq := &struct {
84                         BlockHeight uint64             `json:"block_height"`
85                         BlockHash   chainjson.HexBytes `json:"block_hash"`
86                 }{BlockHeight: height, BlockHash: hash}
87
88                 data, exitCode := util.ClientCall("/get-block", blockReq)
89                 if exitCode != util.Success {
90                         os.Exit(exitCode)
91                 }
92                 printJSON(data)
93         },
94 }
95
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 {
103                         os.Exit(exitCode)
104                 }
105                 printJSON(data)
106         },
107 }
108
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 {
116                         os.Exit(exitCode)
117                 }
118                 printJSON(data)
119         },
120 }
121
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)
128                 if err != nil {
129                         jww.ERROR.Printf("Invalid height value")
130                         os.Exit(util.ErrLocalExe)
131                 }
132
133                 data, exitCode := util.ClientCall("/get-block-transactions-count-by-height", ui64)
134                 if exitCode != util.Success {
135                         os.Exit(exitCode)
136                 }
137
138                 printJSON(data)
139         },
140 }