OSDN Git Service

Merge pull request #767 from Bytom/utxo-view-unit-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 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
102                 var height uint64
103                 var err error
104                 isNumber := false
105
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)
111                         }
112
113                         if !unicode.IsNumber(ch) {
114                                 isNumber = true
115                         }
116                 }
117
118                 if isNumber {
119                         if len(args[0]) != 64 {
120                                 jww.ERROR.Printf("Invalid hash length")
121                                 os.Exit(util.ErrLocalExe)
122                         }
123
124                         hash, err = hex.DecodeString(args[0])
125                         if err != nil {
126                                 jww.ERROR.Println(err)
127                                 os.Exit(util.ErrLocalExe)
128                         }
129                 } else {
130                         height, err = strconv.ParseUint(args[0], 10, 64)
131                         if err != nil {
132                                 jww.ERROR.Printf("Invalid height value")
133                                 os.Exit(util.ErrLocalExe)
134                         }
135                 }
136
137                 req := &struct {
138                         BlockHeight uint64             `json:"block_height"`
139                         BlockHash   chainjson.HexBytes `json:"block_hash"`
140                 }{BlockHeight: height, BlockHash: hash}
141
142                 data, exitCode := util.ClientCall("/get-block-header", &req)
143                 if exitCode != util.Success {
144                         os.Exit(exitCode)
145                 }
146                 printJSON(data)
147         },
148 }