-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryable.go
520 lines (452 loc) · 16.5 KB
/
queryable.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package querier
import (
"encoding/json"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
keep "github.com/cosmos/cosmos-sdk/x/stake/keeper"
"github.com/cosmos/cosmos-sdk/x/stake/types"
abci "github.com/tendermint/tendermint/abci/types"
)
// query endpoints supported by the staking Querier
const (
QueryValidators = "validators"
QueryValidator = "validator"
QueryDelegatorDelegations = "delegatorDelegations"
QueryDelegatorUnbondingDelegations = "delegatorUnbondingDelegations"
QueryDelegatorRedelegations = "delegatorRedelegations"
QueryValidatorUnbondingDelegations = "validatorUnbondingDelegations"
QueryValidatorRedelegations = "validatorRedelegations"
QueryDelegator = "delegator"
QueryDelegation = "delegation"
QueryRedelegation = "redelegation"
QueryUnbondingDelegation = "unbondingDelegation"
QueryDelegatorValidators = "delegatorValidators"
QueryDelegatorValidator = "delegatorValidator"
QueryPool = "pool"
QueryParameters = "parameters"
QueryTopValidators = "topValidators"
QueryAllValidatorsCount = "allValidatorsCount"
QueryAllUnJailValidatorsCount = "allUnJailValidatorsCount"
QueryCrossStakeInfoByAxcAddress = "crossStakeInfoByAxcAddress"
)
// creates a querier for staking REST endpoints
func NewQuerier(k keep.Keeper, cdc *codec.Codec) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
switch path[0] {
case QueryValidators:
p := new(BaseParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryValidators(ctx, cdc, k)
case QueryValidator:
p := new(QueryValidatorParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryValidator(ctx, cdc, p, k)
case QueryValidatorUnbondingDelegations:
p := new(QueryValidatorParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryValidatorUnbondingDelegations(ctx, cdc, p, k)
case QueryValidatorRedelegations:
p := new(QueryValidatorParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryValidatorRedelegations(ctx, cdc, p, k)
case QueryDelegation:
p := new(QueryBondsParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryDelegation(ctx, cdc, p, k)
case QueryRedelegation:
p := new(QueryRedelegationParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryRedelegation(ctx, cdc, p, k)
case QueryUnbondingDelegation:
p := new(QueryBondsParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryUnbondingDelegation(ctx, cdc, p, k)
case QueryDelegatorDelegations:
p := new(QueryDelegatorParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryDelegatorDelegations(ctx, cdc, p, k)
case QueryDelegatorUnbondingDelegations:
p := new(QueryDelegatorParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryDelegatorUnbondingDelegations(ctx, cdc, p, k)
case QueryDelegatorRedelegations:
p := new(QueryDelegatorParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryDelegatorRedelegations(ctx, cdc, p, k)
case QueryDelegatorValidators:
p := new(QueryDelegatorParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryDelegatorValidators(ctx, cdc, p, k)
case QueryDelegatorValidator:
p := new(QueryBondsParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryDelegatorValidator(ctx, cdc, p, k)
case QueryPool:
p := new(BaseParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryPool(ctx, cdc, k)
case QueryParameters:
p := new(BaseParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryParameters(ctx, cdc, k)
case QueryTopValidators:
p := new(QueryTopValidatorsParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryTopValidators(ctx, cdc, p, k)
case QueryAllValidatorsCount:
p := new(BaseParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryAllValidatorsCount(ctx, cdc, k)
case QueryAllUnJailValidatorsCount:
p := new(BaseParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryAllUnJailValidatorsCount(ctx, cdc, k)
case QueryCrossStakeInfoByAxcAddress:
p := new(QueryCrossStakeInfoParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryCrossStakeInfoByAxcAddress(ctx, cdc, p, k)
default:
return nil, sdk.ErrUnknownRequest("unknown stake query endpoint")
}
}
}
func RequestPrepare(ctx sdk.Context, k keep.Keeper, req abci.RequestQuery, p types.SideChainIder) (newCtx sdk.Context, err sdk.Error) {
if req.Data == nil || len(req.Data) == 0 {
return ctx, nil
}
newCtx = ctx
errRes := json.Unmarshal(req.Data, p)
if errRes != nil {
return newCtx, sdk.ErrInternal("can not unmarshal request")
}
if len(p.GetSideChainId()) != 0 {
newCtx, err = prepareSideChainCtx(ctx, k, p.GetSideChainId())
if err != nil {
return newCtx, err
}
}
return newCtx, nil
}
// BaseParams
type BaseParams struct {
SideChainId string
}
func NewBaseParams(sideChainId string) BaseParams {
return BaseParams{
SideChainId: sideChainId,
}
}
func (p BaseParams) GetSideChainId() string {
return p.SideChainId
}
// defines the params for the following queries:
// - 'custom/stake/delegatorDelegations'
// - 'custom/stake/delegatorUnbondingDelegations'
// - 'custom/stake/delegatorRedelegations'
// - 'custom/stake/delegatorValidators'
type QueryDelegatorParams struct {
BaseParams
DelegatorAddr sdk.AccAddress
}
// defines the params for the following queries:
// - 'custom/stake/validator'
// - 'custom/stake/validatorUnbondingDelegations'
// - 'custom/stake/validatorRedelegations'
type QueryValidatorParams struct {
BaseParams
ValidatorAddr sdk.ValAddress
}
// defines the params for the following queries:
// - 'custom/stake/delegation'
// - 'custom/stake/unbondingDelegation'
// - 'custom/stake/delegatorValidator'
type QueryBondsParams struct {
BaseParams
DelegatorAddr sdk.AccAddress
ValidatorAddr sdk.ValAddress
}
// defines the params for 'custom/stake/topValidators'
type QueryTopValidatorsParams struct {
BaseParams
Top int
}
type QueryRedelegationParams struct {
BaseParams
DelegatorAddr sdk.AccAddress
ValSrcAddr sdk.ValAddress
ValDstAddr sdk.ValAddress
}
// defines the params for 'custom/stake/crossStakeInfo'
type QueryCrossStakeInfoParams struct {
BaseParams
AxcAddress sdk.SmartChainAddress
}
func queryValidators(ctx sdk.Context, cdc *codec.Codec, k keep.Keeper) (res []byte, err sdk.Error) {
stakeParams := k.GetParams(ctx)
validators := k.GetValidators(ctx, stakeParams.MaxValidators)
res, errRes := codec.MarshalJSONIndent(cdc, validators)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryValidator(ctx sdk.Context, cdc *codec.Codec, params *QueryValidatorParams, k keep.Keeper) (res []byte, err sdk.Error) {
validator, found := k.GetValidator(ctx, params.ValidatorAddr)
if !found {
return []byte{}, types.ErrNoValidatorFound(types.DefaultCodespace)
}
res, errRes := codec.MarshalJSONIndent(cdc, validator)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryValidatorUnbondingDelegations(ctx sdk.Context, cdc *codec.Codec, params *QueryValidatorParams, k keep.Keeper) (res []byte, err sdk.Error) {
unbonds := k.GetUnbondingDelegationsFromValidator(ctx, params.ValidatorAddr)
res, errRes := codec.MarshalJSONIndent(cdc, unbonds)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryValidatorRedelegations(ctx sdk.Context, cdc *codec.Codec, params *QueryValidatorParams, k keep.Keeper) (res []byte, err sdk.Error) {
redelegations := k.GetRedelegationsFromValidator(ctx, params.ValidatorAddr)
res, errRes := codec.MarshalJSONIndent(cdc, redelegations)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryDelegatorDelegations(ctx sdk.Context, cdc *codec.Codec, params *QueryDelegatorParams, k keep.Keeper) (res []byte, err sdk.Error) {
delegations := k.GetAllDelegatorDelegations(ctx, params.DelegatorAddr)
delResponses, err := delegationsToDelegationResponses(ctx, k, delegations)
if err != nil {
return res, err
}
res, errRes := codec.MarshalJSONIndent(cdc, delResponses)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryDelegatorUnbondingDelegations(ctx sdk.Context, cdc *codec.Codec, params *QueryDelegatorParams, k keep.Keeper) (res []byte, err sdk.Error) {
unbondingDelegations := k.GetAllUnbondingDelegations(ctx, params.DelegatorAddr)
res, errRes := codec.MarshalJSONIndent(cdc, unbondingDelegations)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryDelegatorRedelegations(ctx sdk.Context, cdc *codec.Codec, params *QueryDelegatorParams, k keep.Keeper) (res []byte, err sdk.Error) {
redelegations := k.GetAllRedelegations(ctx, params.DelegatorAddr)
res, errRes := codec.MarshalJSONIndent(cdc, redelegations)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryDelegatorValidators(ctx sdk.Context, cdc *codec.Codec, params *QueryDelegatorParams, k keep.Keeper) (res []byte, err sdk.Error) {
stakeParams := k.GetParams(ctx)
validators := k.GetDelegatorValidators(ctx, params.DelegatorAddr, stakeParams.MaxValidators)
res, errRes := codec.MarshalJSONIndent(cdc, validators)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryDelegatorValidator(ctx sdk.Context, cdc *codec.Codec, params *QueryBondsParams, k keep.Keeper) (res []byte, err sdk.Error) {
validator, err := k.GetDelegatorValidator(ctx, params.DelegatorAddr, params.ValidatorAddr)
if err != nil {
return
}
res, errRes := codec.MarshalJSONIndent(cdc, validator)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryDelegation(ctx sdk.Context, cdc *codec.Codec, params *QueryBondsParams, k keep.Keeper) (res []byte, err sdk.Error) {
delegation, found := k.GetDelegation(ctx, params.DelegatorAddr, params.ValidatorAddr)
if !found {
return []byte{}, types.ErrNoDelegation(types.DefaultCodespace)
}
delResponse, err := delegationToDelegationResponse(ctx, k, delegation)
if err != nil {
return res, err
}
res, errRes := codec.MarshalJSONIndent(cdc, delResponse)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryRedelegation(ctx sdk.Context, cdc *codec.Codec, params *QueryRedelegationParams, k keep.Keeper) (res []byte, err sdk.Error) {
redelegation, found := k.GetRedelegation(ctx, params.DelegatorAddr, params.ValSrcAddr, params.ValDstAddr)
if !found {
return nil, types.ErrNoRedelegation(types.DefaultCodespace)
}
res, errRes := codec.MarshalJSONIndent(cdc, redelegation)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryUnbondingDelegation(ctx sdk.Context, cdc *codec.Codec, params *QueryBondsParams, k keep.Keeper) (res []byte, err sdk.Error) {
unbond, found := k.GetUnbondingDelegation(ctx, params.DelegatorAddr, params.ValidatorAddr)
if !found {
return []byte{}, types.ErrNoUnbondingDelegation(types.DefaultCodespace)
}
res, errRes := codec.MarshalJSONIndent(cdc, unbond)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryPool(ctx sdk.Context, cdc *codec.Codec, k keep.Keeper) (res []byte, err sdk.Error) {
pool := k.GetPool(ctx)
res, errRes := codec.MarshalJSONIndent(cdc, pool)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryParameters(ctx sdk.Context, cdc *codec.Codec, k keep.Keeper) (res []byte, err sdk.Error) {
params := k.GetParams(ctx)
res, errRes := codec.MarshalJSONIndent(cdc, params)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryTopValidators(ctx sdk.Context, cdc *codec.Codec, params *QueryTopValidatorsParams, k keep.Keeper) (res []byte, err sdk.Error) {
if params.Top == 0 {
params.Top = int(k.MaxValidators(ctx))
}
if params.Top > 50 || params.Top < 1 {
return []byte{}, sdk.ErrInternal("top must be between 1 and 50")
}
validators := k.GetTopValidatorsByPower(ctx, params.Top)
res, errRes := codec.MarshalJSONIndent(cdc, validators)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryAllValidatorsCount(ctx sdk.Context, cdc *codec.Codec, k keep.Keeper) ([]byte, sdk.Error) {
count := k.GetAllValidatorsCount(ctx)
res, errRes := codec.MarshalJSONIndent(cdc, count)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryAllUnJailValidatorsCount(ctx sdk.Context, cdc *codec.Codec, k keep.Keeper) ([]byte, sdk.Error) {
count := k.GetAllUnJailValidatorsCount(ctx)
res, errRes := codec.MarshalJSONIndent(cdc, count)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func queryCrossStakeInfoByAxcAddress(ctx sdk.Context, cdc *codec.Codec, params *QueryCrossStakeInfoParams, k keep.Keeper) ([]byte, sdk.Error) {
if params.AxcAddress.IsEmpty() {
return []byte{}, sdk.ErrInternal("invalid side chain address")
}
delCAoB := types.GetStakeCAoB(params.AxcAddress[:], types.DelegateCAoBSalt)
rewardCAoB := types.GetStakeCAoB(delCAoB.Bytes(), types.RewardCAoBSalt)
reward := k.BankKeeper.GetCoins(ctx, rewardCAoB).AmountOf(k.BondDenom(ctx))
csResponse := types.NewCrossStakeInfoResponse(delCAoB, rewardCAoB, reward)
res, errRes := codec.MarshalJSONIndent(cdc, csResponse)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}
func prepareSideChainCtx(ctx sdk.Context, k keep.Keeper, sideChainId string) (sdk.Context, sdk.Error) {
scCtx, err := k.ScKeeper.PrepareCtxForSideChain(ctx, sideChainId)
if err != nil {
return sdk.Context{}, types.ErrInvalidSideChainId(k.Codespace())
}
return scCtx, nil
}
//______________________________________________________
// util
func delegationToDelegationResponse(ctx sdk.Context, k keep.Keeper, del types.Delegation) (types.DelegationResponse, sdk.Error) {
val, found := k.GetValidator(ctx, del.ValidatorAddr)
if !found {
return types.DelegationResponse{}, types.ErrNoValidatorFound(k.Codespace())
}
return types.NewDelegationResp(
del.DelegatorAddr,
del.ValidatorAddr,
del.Shares,
sdk.NewCoin(k.BondDenom(ctx), val.TokensFromShares(del.Shares).RawInt()),
), nil
}
func delegationsToDelegationResponses(
ctx sdk.Context, k keep.Keeper, delegations []types.Delegation,
) ([]types.DelegationResponse, sdk.Error) {
resp := make([]types.DelegationResponse, len(delegations))
for i, del := range delegations {
delResp, err := delegationToDelegationResponse(ctx, k, del)
if err != nil {
return nil, err
}
resp[i] = delResp
}
return resp, nil
}