-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathquerier.go
255 lines (199 loc) · 7.53 KB
/
querier.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
package keeper
import (
"encoding/json"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/staking/exported"
)
func NewQuerier(k Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case types.QueryParams:
return queryParams(ctx, path[1:], req, k)
case types.QueryValidatorOutstandingRewards:
return queryValidatorOutstandingRewards(ctx, path[1:], req, k)
case types.QueryValidatorCommission:
return queryValidatorCommission(ctx, path[1:], req, k)
case types.QueryValidatorSlashes:
return queryValidatorSlashes(ctx, path[1:], req, k)
case types.QueryDelegationRewards:
return queryDelegationRewards(ctx, path[1:], req, k)
case types.QueryDelegatorTotalRewards:
return queryDelegatorTotalRewards(ctx, path[1:], req, k)
case types.QueryDelegatorValidators:
return queryDelegatorValidators(ctx, path[1:], req, k)
case types.QueryWithdrawAddr:
return queryDelegatorWithdrawAddress(ctx, path[1:], req, k)
case types.QueryCommunityPool:
return queryCommunityPool(ctx, path[1:], req, k)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query path: %s", path[0])
}
}
}
func queryParams(ctx sdk.Context, path []string, req abci.RequestQuery, k Keeper) ([]byte, error) {
params := k.GetParams(ctx)
res, err := codec.MarshalJSONIndent(k.cdc, params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
func queryValidatorOutstandingRewards(ctx sdk.Context, path []string, req abci.RequestQuery, k Keeper) ([]byte, error) {
var params types.QueryValidatorOutstandingRewardsParams
err := k.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
rewards := k.GetValidatorOutstandingRewards(ctx, params.ValidatorAddress)
if rewards == nil {
rewards = sdk.DecCoins{}
}
bz, err := codec.MarshalJSONIndent(k.cdc, rewards)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryValidatorCommission(ctx sdk.Context, path []string, req abci.RequestQuery, k Keeper) ([]byte, error) {
var params types.QueryValidatorCommissionParams
err := k.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
commission := k.GetValidatorAccumulatedCommission(ctx, params.ValidatorAddress)
if commission == nil {
commission = sdk.DecCoins{}
}
bz, err := codec.MarshalJSONIndent(k.cdc, commission)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryValidatorSlashes(ctx sdk.Context, path []string, req abci.RequestQuery, k Keeper) ([]byte, error) {
var params types.QueryValidatorSlashesParams
err := k.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
events := make([]types.ValidatorSlashEvent, 0)
k.IterateValidatorSlashEventsBetween(ctx, params.ValidatorAddress, params.StartingHeight, params.EndingHeight,
func(height uint64, event types.ValidatorSlashEvent) (stop bool) {
events = append(events, event)
return false
},
)
bz, err := codec.MarshalJSONIndent(k.cdc, events)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryDelegationRewards(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper) ([]byte, error) {
var params types.QueryDelegationRewardsParams
err := k.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
// cache-wrap context as to not persist state changes during querying
ctx, _ = ctx.CacheContext()
val := k.stakingKeeper.Validator(ctx, params.ValidatorAddress)
if val == nil {
return nil, sdkerrors.Wrap(types.ErrNoValidatorExists, params.ValidatorAddress.String())
}
del := k.stakingKeeper.Delegation(ctx, params.DelegatorAddress, params.ValidatorAddress)
if del == nil {
return nil, types.ErrNoDelegationExists
}
endingPeriod := k.incrementValidatorPeriod(ctx, val)
rewards := k.calculateDelegationRewards(ctx, val, del, endingPeriod)
if rewards == nil {
rewards = sdk.DecCoins{}
}
bz, err := codec.MarshalJSONIndent(k.cdc, rewards)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryDelegatorTotalRewards(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper) ([]byte, error) {
var params types.QueryDelegatorParams
err := k.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
// cache-wrap context as to not persist state changes during querying
ctx, _ = ctx.CacheContext()
total := sdk.DecCoins{}
var delRewards []types.DelegationDelegatorReward
k.stakingKeeper.IterateDelegations(
ctx, params.DelegatorAddress,
func(_ int64, del exported.DelegationI) (stop bool) {
valAddr := del.GetValidatorAddr()
val := k.stakingKeeper.Validator(ctx, valAddr)
endingPeriod := k.incrementValidatorPeriod(ctx, val)
delReward := k.calculateDelegationRewards(ctx, val, del, endingPeriod)
delRewards = append(delRewards, types.NewDelegationDelegatorReward(valAddr, delReward))
total = total.Add(delReward...)
return false
},
)
totalRewards := types.NewQueryDelegatorTotalRewardsResponse(delRewards, total)
bz, err := json.Marshal(totalRewards)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryDelegatorValidators(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper) ([]byte, error) {
var params types.QueryDelegatorParams
err := k.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
// cache-wrap context as to not persist state changes during querying
ctx, _ = ctx.CacheContext()
var validators []sdk.ValAddress
k.stakingKeeper.IterateDelegations(
ctx, params.DelegatorAddress,
func(_ int64, del exported.DelegationI) (stop bool) {
validators = append(validators, del.GetValidatorAddr())
return false
},
)
bz, err := codec.MarshalJSONIndent(k.cdc, validators)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryDelegatorWithdrawAddress(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper) ([]byte, error) {
var params types.QueryDelegatorWithdrawAddrParams
err := k.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
// cache-wrap context as to not persist state changes during querying
ctx, _ = ctx.CacheContext()
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, params.DelegatorAddress)
bz, err := codec.MarshalJSONIndent(k.cdc, withdrawAddr)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryCommunityPool(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper) ([]byte, error) {
pool := k.GetFeePoolCommunityCoins(ctx)
if pool == nil {
pool = sdk.DecCoins{}
}
bz, err := k.cdc.MarshalJSON(pool)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}