-
Notifications
You must be signed in to change notification settings - Fork 47
/
cmd_query.go
309 lines (282 loc) · 8.36 KB
/
cmd_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import (
"bytes"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"strconv"
"strings"
"github.com/iov-one/weave"
bnsd "github.com/iov-one/weave/cmd/bnsd/app"
"github.com/iov-one/weave/cmd/bnsd/client"
"github.com/iov-one/weave/cmd/bnsd/x/account"
"github.com/iov-one/weave/cmd/bnsd/x/username"
"github.com/iov-one/weave/orm"
"github.com/iov-one/weave/x/cash"
"github.com/iov-one/weave/x/distribution"
"github.com/iov-one/weave/x/escrow"
"github.com/iov-one/weave/x/gov"
"github.com/iov-one/weave/x/multisig"
)
func cmdQuery(input io.Reader, output io.Writer, args []string) error {
fl := flag.NewFlagSet("", flag.ExitOnError)
fl.Usage = func() {
fmt.Fprint(flag.CommandLine.Output(), `
Execute a ABCI query and print JSON encoded result.
`)
fl.PrintDefaults()
}
var (
tmAddrFl = fl.String("tm", env("BNSCLI_TM_ADDR", "https://bns.NETWORK.iov.one:443"),
"Tendermint node address. Use proper NETWORK name. You can use BNSCLI_TM_ADDR environment variable to set it.")
pathFl = fl.String("path", "", "Path to be queried. Must be one of the supported.")
dataFl = fl.String("data", "", "individual query data. Format depends on the queried entity. Use 'id/version' for electoraterules, electorates")
prefixQueryFl = fl.Bool("prefix", false, "If true, use prefix queries instead of the exact match with provided data.")
)
fl.Parse(args)
conf, ok := queries[*pathFl]
if !ok {
var paths []string
for p := range queries {
paths = append(paths, p)
}
return fmt.Errorf("available query paths:\n\t- %s", strings.Join(paths, "\n\t- "))
}
var data []byte
if len(*dataFl) != 0 {
var err error
if data, err = conf.encID(*dataFl); err != nil {
return fmt.Errorf("can not encode data: %s", err)
}
}
queryPath := *pathFl
if *prefixQueryFl || *dataFl == "" {
queryPath += "?" + weave.PrefixQueryMod
}
bnsClient := client.NewClient(client.NewHTTPConnection(*tmAddrFl))
resp, err := bnsClient.AbciQuery(queryPath, data)
if err != nil {
return fmt.Errorf("failed to run query: %s", err)
}
result := make([]keyval, 0, len(resp.Models))
for i, m := range resp.Models {
obj := conf.newObj()
if err := obj.Unmarshal(m.Value); err != nil {
return fmt.Errorf("failed to unmarshal model %d: %s", i, err)
}
key, err := conf.decKey(m.Key)
if err != nil {
return fmt.Errorf("cannot decode %x key: %s", m.Key, err)
}
result = append(result, keyval{Key: key, Value: obj})
}
pretty, err := json.MarshalIndent(result, "", "\t")
if err != nil {
return fmt.Errorf("cannot JSON serialize: %s", err)
}
_, err = output.Write(pretty)
return err
}
type keyval struct {
Key string
Value model
}
// queries contains a mapping of query path to that query specifics. Each query
// returns a custom model type and may use different ID encoding pattern.
var queries = map[string]struct {
// newObj returns a new instance of the model that the result of the
// ABCI query should be extracted into.
newObj func() model
// decKey is used to decode key value returned by the ABCI query and
// transform it into human readable form.
decKey func([]byte) (string, error)
// encID is used to parse input format of the ID and encode it into
// form that will be passed to the ABCI query. The format can differ
// from decKey if we use secondary index for matching.
encID func(string) ([]byte, error)
}{
"/proposals": {
newObj: func() model { return &extendedProposal{} },
decKey: sequenceKey,
encID: numericID,
},
"/proposals/author": {
newObj: func() model { return &extendedProposal{} },
decKey: sequenceKey,
encID: addressID,
},
"/proposals/electorate": {
newObj: func() model { return &extendedProposal{} },
decKey: sequenceKey,
encID: numericID,
},
"/electionrules": {
newObj: func() model { return &gov.ElectionRule{} },
decKey: refKey,
encID: refID,
},
"/electorates": {
newObj: func() model { return &gov.Electorate{} },
decKey: refKey,
encID: refID,
},
"/electorates/elector": {
newObj: func() model { return &gov.Electorate{} },
decKey: refKey,
encID: addressID,
},
"/votes": {
newObj: func() model { return &gov.Vote{} },
decKey: rawKey,
encID: addressID,
},
"/votes/proposals": {
newObj: func() model { return &gov.Vote{} },
decKey: rawKey,
encID: numericID,
},
"/votes/electors": {
newObj: func() model { return &gov.Vote{} },
decKey: rawKey,
encID: addressID,
},
"/usernames": {
newObj: func() model { return &username.Token{} },
decKey: rawKey,
encID: addressID,
},
"/usernames/owner": {
newObj: func() model { return &username.Token{} },
decKey: rawKey,
encID: addressID,
},
"/wallets": {
newObj: func() model { return &cash.Set{} },
decKey: rawKey,
encID: addressID,
},
"/escrows": {
newObj: func() model { return &escrow.Escrow{} },
decKey: sequenceKey,
encID: numericID,
},
"/revenues": {
newObj: func() model { return &distribution.Revenue{} },
decKey: sequenceKey,
encID: numericID,
},
"/contracts": {
newObj: func() model { return &multisig.Contract{} },
decKey: sequenceKey,
encID: numericID,
},
"/accounts": {
newObj: func() model { return &account.Account{} },
decKey: strKey,
encID: strID,
},
"/domains": {
newObj: func() model { return &account.Domain{} },
decKey: strKey,
encID: strID,
},
}
// model is an entity used by weave to store data. This interface is
// implemented by any protobuf message.
type model interface {
Unmarshal([]byte) error
}
// refID expects `id/version` pair with integers.
func refID(s string) ([]byte, error) {
tokens := strings.Split(s, "/")
var version uint32
switch len(tokens) {
case 1:
// Allow providing just the ID value to enable prefix queries.
// This is a special case.
case 2:
if n, err := strconv.ParseUint(tokens[1], 10, 32); err != nil {
return nil, fmt.Errorf("cannot decode version: %s", err)
} else {
version = uint32(n)
}
default:
return nil, errors.New("invalid ID format, use 'id/version'")
}
encID := make([]byte, 8)
if n, err := strconv.ParseUint(tokens[0], 10, 64); err != nil {
return nil, fmt.Errorf("cannot decode ID: %s", err)
} else {
binary.BigEndian.PutUint64(encID, n)
}
ref := orm.VersionedIDRef{ID: encID, Version: version}
if ref.Version == 0 {
return ref.ID, nil
}
return orm.MarshalVersionedID(ref), nil
}
func addressID(s string) ([]byte, error) {
return weave.ParseAddress(s)
}
func strID(s string) ([]byte, error) {
return []byte(s), nil
}
func refKey(raw []byte) (string, error) {
// Skip the prefix, being the characters before : (including separator)
val := raw[bytes.Index(raw, []byte(":"))+1:]
ref, err := orm.UnmarshalVersionedID(val)
if err != nil {
return "", fmt.Errorf("cannot unmarshal versioned key: %s", err)
}
id := binary.BigEndian.Uint64(ref.ID)
return fmt.Sprintf("%d/%d", id, ref.Version), nil
}
func numericID(s string) ([]byte, error) {
n, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return nil, fmt.Errorf("cannot parse number: %s", err)
}
encID := make([]byte, 8)
binary.BigEndian.PutUint64(encID, n)
return encID, nil
}
func sequenceKey(raw []byte) (string, error) {
// Skip the prefix, being the characters before : (including separator)
seq := raw[bytes.Index(raw, []byte(":"))+1:]
if len(seq) != 8 {
return "", fmt.Errorf("invalid sequence length: %d", len(seq))
}
n := binary.BigEndian.Uint64(seq)
return fmt.Sprint(int64(n)), nil
}
func rawKey(raw []byte) (string, error) {
return hex.EncodeToString(raw), nil
}
func strKey(raw []byte) (string, error) {
return string(raw), nil
}
// extendedProposal is the gov.Proposal with an additional field to extract
// RawOption. When serialized using JSON, this structure produce the same
// result as the gov.Proposal with an addition of an attribute representing
// deserialized (human readable) form of the message that is proposed.
type extendedProposal struct {
gov.Proposal
// Option contains a deserialized value of the RawOption
Option interface{} `json:"executed_when_accepted"`
}
// Unmarshal implements protobuf unmarshaler interface.
func (p *extendedProposal) Unmarshal(raw []byte) error {
if err := p.Proposal.Unmarshal(raw); err != nil {
return fmt.Errorf("cannot unmarshal proposal: %s", err)
}
var opts bnsd.ProposalOptions
if err := opts.Unmarshal(p.Proposal.RawOption); err != nil {
return fmt.Errorf("cannot unmarshal proposal option: %s", err)
}
p.Option = opts.GetOption()
return nil
}