forked from CyberMiles/travis
-
Notifications
You must be signed in to change notification settings - Fork 3
/
query.go
123 lines (103 loc) · 2.76 KB
/
query.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package commands
import (
"fmt"
"github.com/CyberMiles/travis/sdk/client/commands"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
rpcclient "github.com/tendermint/tendermint/rpc/client"
"os"
)
/**
The stake/query/validator is to query the current stake status of the validator. Not signed.
* Validator address
The stake/query/delegator is to query the current stake status of a delegator. Not signed.
* Delegator address
*/
//nolint
const (
FlagHeight = "height"
)
//nolint
var (
CmdQueryValidator = &cobra.Command{
Use: "validator",
RunE: cmdQueryValidator,
Short: "Query the current stake status of a validator",
}
CmdQueryValidators = &cobra.Command{
Use: "validators",
RunE: cmdQueryValidators,
Short: "Query a list of all current validators and validator candidates",
}
CmdQueryDelegator = &cobra.Command{
Use: "delegator",
RunE: cmdQueryDelegator,
Short: "Query the current stake status of a delegator",
}
CmdQueryAwardInfo = &cobra.Command{
Use: "award-info",
RunE: cmdQueryAwardInfo,
Short: "Query the award info of a block",
}
)
func init() {
//Add Flags
fsAddr := flag.NewFlagSet("", flag.ContinueOnError)
fsAddr.String(FlagAddress, "", "account address")
CmdQueryValidator.Flags().AddFlagSet(fsAddr)
CmdQueryDelegator.Flags().AddFlagSet(fsAddr)
}
func cmdQueryValidators(cmd *cobra.Command, args []string) error {
b, err := Get("/validators", []byte{0})
if err != nil {
return err
}
return Foutput(b)
}
func cmdQueryValidator(cmd *cobra.Command, args []string) error {
address := viper.GetString(FlagAddress)
if address == "" {
return fmt.Errorf("please enter validator address using --address")
}
b, err := Get("/validator", []byte(address))
if err != nil {
return err
}
return Foutput(b)
}
func cmdQueryDelegator(cmd *cobra.Command, args []string) error {
address := viper.GetString(FlagAddress)
b, err := Get("/delegator", []byte(address))
if err != nil {
return err
}
return Foutput(b)
}
func cmdQueryAwardInfo(cmd *cobra.Command, args []string) error {
b, err := GetByHeight("/awardInfo", []byte{0x00}, int64(viper.GetInt(FlagHeight)))
if err != nil {
return err
}
return Foutput(b)
}
func Get(path string, params []byte) ([]byte, error) {
node := commands.GetNode()
resp, err := node.ABCIQuery(path, params)
if resp == nil {
return nil, err
}
return resp.Response.Value, err
}
func GetByHeight(path string, params []byte, height int64) ([]byte, error) {
node := commands.GetNode()
resp, err := node.ABCIQueryWithOptions(path, params, rpcclient.ABCIQueryOptions{Trusted: true, Height: int64(height)})
if resp == nil {
return nil, err
}
return resp.Response.Value, err
}
func Foutput(b []byte) error {
_, err := fmt.Fprintf(os.Stdout, "%s\n", b)
return err
}