forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
208 lines (170 loc) · 4.96 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package commands
import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
crypto "github.com/tendermint/go-crypto"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" // XXX fix
"github.com/cosmos/cosmos-sdk/x/stake"
)
// XXX remove dependancy
func PrefixedKey(app string, key []byte) []byte {
prefix := append([]byte(app), byte(0))
return append(prefix, key...)
}
//nolint
var (
fsValAddr = flag.NewFlagSet("", flag.ContinueOnError)
fsDelAddr = flag.NewFlagSet("", flag.ContinueOnError)
FlagValidatorAddr = "address"
FlagDelegatorAddr = "delegator-address"
)
func init() {
//Add Flags
fsValAddr.String(FlagValidatorAddr, "", "Address of the validator/candidate")
fsDelAddr.String(FlagDelegatorAddr, "", "Delegator hex address")
}
// create command to query for all candidates
func GetCmdQueryCandidates(cdc *wire.Codec, storeName string) *cobra.Command {
cmd := &cobra.Command{
Use: "candidates",
Short: "Query for the set of validator-candidates pubkeys",
RunE: func(cmd *cobra.Command, args []string) error {
key := PrefixedKey(stake.MsgType, stake.CandidatesKey)
ctx := context.NewCoreContextFromViper()
res, err := ctx.Query(key, storeName)
if err != nil {
return err
}
// parse out the candidates
candidates := new(stake.Candidates)
err = cdc.UnmarshalJSON(res, candidates)
if err != nil {
return err
}
output, err := json.MarshalIndent(candidates, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))
return nil
// TODO output with proofs / machine parseable etc.
},
}
cmd.Flags().AddFlagSet(fsDelAddr)
return cmd
}
// get the command to query a candidate
func GetCmdQueryCandidate(cdc *wire.Codec, storeName string) *cobra.Command {
cmd := &cobra.Command{
Use: "candidate",
Short: "Query a validator-candidate account",
RunE: func(cmd *cobra.Command, args []string) error {
addr, err := sdk.GetAddress(viper.GetString(FlagValidatorAddr))
if err != nil {
return err
}
key := PrefixedKey(stake.MsgType, stake.GetCandidateKey(addr))
ctx := context.NewCoreContextFromViper()
res, err := ctx.Query(key, storeName)
if err != nil {
return err
}
// parse out the candidate
candidate := new(stake.Candidate)
err = cdc.UnmarshalBinary(res, candidate)
if err != nil {
return err
}
output, err := json.MarshalIndent(candidate, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))
return nil
// TODO output with proofs / machine parseable etc.
},
}
cmd.Flags().AddFlagSet(fsValAddr)
return cmd
}
// get the command to query a single delegator bond
func GetCmdQueryDelegatorBond(cdc *wire.Codec, storeName string) *cobra.Command {
cmd := &cobra.Command{
Use: "delegator-bond",
Short: "Query a delegators bond based on address and candidate pubkey",
RunE: func(cmd *cobra.Command, args []string) error {
addr, err := sdk.GetAddress(viper.GetString(FlagValidatorAddr))
if err != nil {
return err
}
bz, err := hex.DecodeString(viper.GetString(FlagDelegatorAddr))
if err != nil {
return err
}
delegator := crypto.Address(bz)
key := PrefixedKey(stake.MsgType, stake.GetDelegatorBondKey(delegator, addr, cdc))
ctx := context.NewCoreContextFromViper()
res, err := ctx.Query(key, storeName)
if err != nil {
return err
}
// parse out the bond
var bond stake.DelegatorBond
err = cdc.UnmarshalBinary(res, bond)
if err != nil {
return err
}
output, err := json.MarshalIndent(bond, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))
return nil
// TODO output with proofs / machine parseable etc.
},
}
cmd.Flags().AddFlagSet(fsValAddr)
cmd.Flags().AddFlagSet(fsDelAddr)
return cmd
}
// get the command to query all the candidates bonded to a delegator
func GetCmdQueryDelegatorBonds(cdc *wire.Codec, storeName string) *cobra.Command {
cmd := &cobra.Command{
Use: "delegator-candidates",
Short: "Query all delegators candidates' pubkeys based on address",
RunE: func(cmd *cobra.Command, args []string) error {
bz, err := hex.DecodeString(viper.GetString(FlagDelegatorAddr))
if err != nil {
return err
}
delegator := crypto.Address(bz)
key := PrefixedKey(stake.MsgType, stake.GetDelegatorBondsKey(delegator, cdc))
ctx := context.NewCoreContextFromViper()
res, err := ctx.Query(key, storeName)
if err != nil {
return err
}
// parse out the candidates list
var candidates []crypto.PubKey
err = cdc.UnmarshalBinary(res, candidates)
if err != nil {
return err
}
output, err := json.MarshalIndent(candidates, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))
return nil
// TODO output with proofs / machine parseable etc.
},
}
cmd.Flags().AddFlagSet(fsDelAddr)
return cmd
}