forked from iost-official/go-iost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.go
61 lines (54 loc) · 1.65 KB
/
block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package iwallet
import (
"fmt"
"strconv"
"github.com/iost-official/go-iost/sdk"
"github.com/iost-official/go-iost/rpc/pb"
"github.com/spf13/cobra"
)
var method string
var complete bool
var methodMap = map[string]func(string) (*rpcpb.BlockResponse, error){
"num": func(arg string) (*rpcpb.BlockResponse, error) {
num, err := strconv.ParseInt(arg, 10, 64)
if err != nil {
err = fmt.Errorf("invalid block number: %v", err)
return nil, err
}
return iwalletSDK.GetBlockByNum(num, complete)
},
"hash": func(arg string) (*rpcpb.BlockResponse, error) {
return iwalletSDK.GetBlockByHash(arg, complete)
},
}
// blockCmd represents the block command.
var blockCmd = &cobra.Command{
Use: "block blockNum|blockHash",
Short: "Print block info",
Long: `Print block info by block number or hash`,
Example: ` iwallet block 0
iwallet block 5dEgmyMURGfe7GxvTLajmaLXTkcqs5JwiJ2C2DE5VvVX -m hash`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errorWithHelp(cmd, "please enter the block number or hash")
}
_, ok := methodMap[method]
if !ok {
return errorWithHelp(cmd, "wrong method: %v", method)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
blockInfo, err := methodMap[method](args[0])
if err != nil {
return err
}
fmt.Println(sdk.MarshalTextString(blockInfo))
return nil
},
}
func init() {
rootCmd.AddCommand(blockCmd)
blockCmd.Flags().StringVarP(&method, "method", "m", "num", `find by block num (set as "num") or hash (set as "hash")`)
blockCmd.Flags().BoolVarP(&complete, "complete", "c", false, "indicate whether to fetch all the transactions in the block or not")
}