OSDN Git Service

Change Makefile and directory for vapor
[bytom/vapor.git] / cmd / vaporcli / 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/vapor/encoding/json"
13         "github.com/vapor/util"
14 )
15
16 func init() {
17         getDifficultyCmd.PersistentFlags().StringVar(&blockHash, "hash", "", "hash of block")
18         getDifficultyCmd.PersistentFlags().IntVar(&blockHeight, "height", 0, "height of block")
19
20         getHashRateCmd.PersistentFlags().StringVar(&blockHash, "hash", "", "hash of block")
21         getHashRateCmd.PersistentFlags().IntVar(&blockHeight, "height", 0, "height of block")
22 }
23
24 var (
25         blockHash   = ""
26         blockHeight = 0
27 )
28
29 var getBlockHashCmd = &cobra.Command{
30         Use:   "get-block-hash",
31         Short: "Get the hash of most recent block",
32         Args:  cobra.NoArgs,
33         Run: func(cmd *cobra.Command, args []string) {
34                 data, exitCode := util.ClientCall("get-block-hash")
35                 if exitCode != util.Success {
36                         os.Exit(exitCode)
37                 }
38                 printJSON(data)
39         },
40 }
41
42 var getBlockCountCmd = &cobra.Command{
43         Use:   "get-block-count",
44         Short: "Get the number of most recent block",
45         Args:  cobra.NoArgs,
46         Run: func(cmd *cobra.Command, args []string) {
47                 data, exitCode := util.ClientCall("/get-block-count")
48                 if exitCode != util.Success {
49                         os.Exit(exitCode)
50                 }
51                 printJSON(data)
52         },
53 }
54
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
61                 var height uint64
62                 var err error
63                 isNumber := false
64
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)
70                         }
71
72                         if !unicode.IsNumber(ch) {
73                                 isNumber = true
74                         }
75                 }
76
77                 if isNumber {
78                         if len(args[0]) != 64 {
79                                 jww.ERROR.Printf("Invalid hash length")
80                                 os.Exit(util.ErrLocalExe)
81                         }
82
83                         hash, err = hex.DecodeString(args[0])
84                         if err != nil {
85                                 jww.ERROR.Println(err)
86                                 os.Exit(util.ErrLocalExe)
87                         }
88                 } else {
89                         height, err = strconv.ParseUint(args[0], 10, 64)
90                         if err != nil {
91                                 jww.ERROR.Printf("Invalid height value")
92                                 os.Exit(util.ErrLocalExe)
93                         }
94                 }
95
96                 blockReq := &struct {
97                         BlockHeight uint64             `json:"block_height"`
98                         BlockHash   chainjson.HexBytes `json:"block_hash"`
99                 }{BlockHeight: height, BlockHash: hash}
100
101                 data, exitCode := util.ClientCall("/get-block", blockReq)
102                 if exitCode != util.Success {
103                         os.Exit(exitCode)
104                 }
105                 printJSON(data)
106         },
107 }
108
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
115                 var height uint64
116                 var err error
117                 isNumber := false
118
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)
124                         }
125
126                         if !unicode.IsNumber(ch) {
127                                 isNumber = true
128                         }
129                 }
130
131                 if isNumber {
132                         if len(args[0]) != 64 {
133                                 jww.ERROR.Printf("Invalid hash length")
134                                 os.Exit(util.ErrLocalExe)
135                         }
136
137                         hash, err = hex.DecodeString(args[0])
138                         if err != nil {
139                                 jww.ERROR.Println(err)
140                                 os.Exit(util.ErrLocalExe)
141                         }
142                 } else {
143                         height, err = strconv.ParseUint(args[0], 10, 64)
144                         if err != nil {
145                                 jww.ERROR.Printf("Invalid height value")
146                                 os.Exit(util.ErrLocalExe)
147                         }
148                 }
149
150                 req := &struct {
151                         BlockHeight uint64             `json:"block_height"`
152                         BlockHash   chainjson.HexBytes `json:"block_hash"`
153                 }{BlockHeight: height, BlockHash: hash}
154
155                 data, exitCode := util.ClientCall("/get-block-header", req)
156                 if exitCode != util.Success {
157                         os.Exit(exitCode)
158                 }
159                 printJSON(data)
160         },
161 }
162
163 var getDifficultyCmd = &cobra.Command{
164         Use:   "get-difficulty",
165         Short: "Get the difficulty of most recent block",
166         Args:  cobra.NoArgs,
167         Run: func(cmd *cobra.Command, args []string) {
168                 var hash chainjson.HexBytes
169                 var err error
170
171                 if blockHash != "" {
172                         hash, err = hex.DecodeString(blockHash)
173                         if err != nil {
174                                 jww.ERROR.Println(err)
175                                 os.Exit(util.ErrLocalExe)
176                         }
177                 }
178
179                 req := &struct {
180                         BlockHeight uint64             `json:"block_height"`
181                         BlockHash   chainjson.HexBytes `json:"block_hash"`
182                 }{BlockHeight: uint64(blockHeight), BlockHash: hash}
183
184                 data, exitCode := util.ClientCall("/get-difficulty", req)
185                 if exitCode != util.Success {
186                         os.Exit(exitCode)
187                 }
188                 printJSON(data)
189         },
190 }
191
192 var getHashRateCmd = &cobra.Command{
193         Use:   "get-hash-rate",
194         Short: "Get the nonce of most recent block",
195         Args:  cobra.NoArgs,
196         Run: func(cmd *cobra.Command, args []string) {
197                 var hash chainjson.HexBytes
198                 var err error
199
200                 if blockHash != "" {
201                         hash, err = hex.DecodeString(blockHash)
202                         if err != nil {
203                                 jww.ERROR.Println(err)
204                                 os.Exit(util.ErrLocalExe)
205                         }
206                 }
207
208                 req := &struct {
209                         BlockHeight uint64             `json:"block_height"`
210                         BlockHash   chainjson.HexBytes `json:"block_hash"`
211                 }{BlockHeight: uint64(blockHeight), BlockHash: hash}
212
213                 data, exitCode := util.ClientCall("/get-hash-rate", req)
214                 if exitCode != util.Success {
215                         os.Exit(exitCode)
216                 }
217                 printJSON(data)
218         },
219 }