OSDN Git Service

a9aefb08f87be8acae31a55b2b6c87c0021f8589
[bytom/bytom.git] / cmd / bytomcli / commands / block.go
1 package commands
2
3 import (
4         "os"
5         "strconv"
6
7         "github.com/spf13/cobra"
8         jww "github.com/spf13/jwalterweatherman"
9
10         "github.com/bytom/blockchain"
11         "github.com/bytom/env"
12 )
13
14 var (
15         home    = blockchain.HomeDirFromEnvironment()
16         coreURL = env.String("BYTOM_URL", "http://localhost:9888")
17 )
18
19 var blockHashCmd = &cobra.Command{
20         Use:   "block-hash",
21         Short: "Get the hash of most recent block",
22         Args:  cobra.NoArgs,
23         Run: func(cmd *cobra.Command, args []string) {
24                 data, exitCode := clientCall("block-hash")
25                 if exitCode != Success {
26                         os.Exit(exitCode)
27                 }
28                 printJSON(data)
29         },
30 }
31
32 var blockHeightCmd = &cobra.Command{
33         Use:   "block-height",
34         Short: "Get the number of most recent block",
35         Args:  cobra.NoArgs,
36         Run: func(cmd *cobra.Command, args []string) {
37                 data, exitCode := clientCall("/block-height")
38                 if exitCode != Success {
39                         os.Exit(exitCode)
40                 }
41                 printJSON(data)
42         },
43 }
44
45 var getBlockByHashCmd = &cobra.Command{
46         Use:   "get-block-by-hash <hash>",
47         Short: "Get a whole block matching the given hash",
48         Args:  cobra.ExactArgs(1),
49         Run: func(cmd *cobra.Command, args []string) {
50                 data, exitCode := clientCall("/get-block-by-hash", args[0])
51                 if exitCode != Success {
52                         os.Exit(exitCode)
53                 }
54                 printJSON(data)
55         },
56 }
57
58 var getBlockHeaderByHashCmd = &cobra.Command{
59         Use:   "get-block-header-by-hash",
60         Short: "Get the header of a block matching the given hash",
61         Args:  cobra.ExactArgs(1),
62         Run: func(cmd *cobra.Command, args []string) {
63                 data, exitCode := clientCall("/get-block-header-by-hash", args[0])
64                 if exitCode != Success {
65                         os.Exit(exitCode)
66                 }
67                 printJSON(data)
68         },
69 }
70
71 var getBlockTransactionsCountByHashCmd = &cobra.Command{
72         Use:   "get-block-transactions-count-by-hash",
73         Short: "Get the transactions count of a block matching the given hash",
74         Args:  cobra.ExactArgs(1),
75         Run: func(cmd *cobra.Command, args []string) {
76                 data, exitCode := clientCall("/get-block-transactions-count-by-hash", args[0])
77                 if exitCode != Success {
78                         os.Exit(exitCode)
79                 }
80                 printJSON(data)
81         },
82 }
83
84 var getBlockByHeightCmd = &cobra.Command{
85         Use:   "get-block-by-height <height>",
86         Short: "Get a whole block matching the given height",
87         Args:  cobra.ExactArgs(1),
88         Run: func(cmd *cobra.Command, args []string) {
89                 height, err := strconv.ParseUint(args[0], 10, 64)
90                 if err != nil {
91                         jww.ERROR.Printf("Invalid height value")
92                         os.Exit(ErrLocalExe)
93                 }
94
95                 data, exitCode := clientCall("/get-block-by-height", height)
96                 if exitCode != Success {
97                         os.Exit(exitCode)
98                 }
99
100                 printJSON(data)
101         },
102 }
103
104 var getBlockTransactionsCountByHeightCmd = &cobra.Command{
105         Use:   "get-block-transactions-count-by-height",
106         Short: "Get the transactions count of a block matching the given height",
107         Args:  cobra.ExactArgs(1),
108         Run: func(cmd *cobra.Command, args []string) {
109                 ui64, err := strconv.ParseUint(args[0], 10, 64)
110                 if err != nil {
111                         jww.ERROR.Printf("Invalid height value")
112                         os.Exit(ErrLocalExe)
113                 }
114
115                 data, exitCode := clientCall("/get-block-transactions-count-by-height", ui64)
116                 if exitCode != Success {
117                         os.Exit(exitCode)
118                 }
119
120                 printJSON(data)
121         },
122 }