Skip to content

Commit 57277d1

Browse files
fedekunzecwgoes
authored andcommitted
Merge PR #2537: Staking REST utils refactor and querier tests (pt 1 redelegation PR)
1 parent b48d0d5 commit 57277d1

File tree

8 files changed

+173
-216
lines changed

8 files changed

+173
-216
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
BUG FIXES
88

99
* Tendermint
10-
- Fix unbounded consensus WAL growth
10+
- Fix unbounded consensus WAL growth
1111

1212
## 0.24.1
1313

PENDING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ IMPROVEMENTS
154154
* [x/stake] [\#2000](https://github.com/cosmos/cosmos-sdk/issues/2000) Added tests for new staking endpoints
155155
* [gaia-lite] [\#2445](https://github.com/cosmos/cosmos-sdk/issues/2445) Standarized REST error responses
156156
* [gaia-lite] Added example to Swagger specification for /keys/seed.
157+
* [x/stake] Refactor REST utils
157158

158159
* Gaia CLI (`gaiacli`)
159160
* [cli] #2060 removed `--select` from `block` command

docs/DOCS_README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,4 @@ much as possible with its [counterpart in the Tendermint Core repo](https://gith
112112
4. Compile gaiacli
113113
```
114114
make install
115-
```
115+
```

x/stake/client/rest/query.go

Lines changed: 21 additions & 210 deletions
Original file line numberDiff line numberDiff line change
@@ -58,74 +58,41 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Co
5858
// Get all validators
5959
r.HandleFunc(
6060
"/stake/validators",
61-
validatorsHandlerFn(cliCtx),
61+
validatorsHandlerFn(cliCtx, cdc),
6262
).Methods("GET")
6363

6464
// Get a single validator info
6565
r.HandleFunc(
66-
"/stake/validators/{addr}",
66+
"/stake/validators/{validatorAddr}",
6767
validatorHandlerFn(cliCtx, cdc),
6868
).Methods("GET")
6969

7070
// Get the current state of the staking pool
7171
r.HandleFunc(
7272
"/stake/pool",
73-
poolHandlerFn(cliCtx),
73+
poolHandlerFn(cliCtx, cdc),
7474
).Methods("GET")
7575

7676
// Get the current staking parameter values
7777
r.HandleFunc(
7878
"/stake/parameters",
79-
paramsHandlerFn(cliCtx),
79+
paramsHandlerFn(cliCtx, cdc),
8080
).Methods("GET")
8181

8282
}
8383

8484
// HTTP request handler to query a delegator delegations
8585
func delegatorHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
86-
return func(w http.ResponseWriter, r *http.Request) {
87-
88-
vars := mux.Vars(r)
89-
bech32delegator := vars["delegatorAddr"]
90-
91-
w.Header().Set("Content-Type", "application/json")
92-
93-
delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator)
94-
if err != nil {
95-
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
96-
return
97-
}
98-
99-
params := stake.QueryDelegatorParams{
100-
DelegatorAddr: delegatorAddr,
101-
}
102-
103-
bz, err := cdc.MarshalJSON(params)
104-
if err != nil {
105-
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
106-
return
107-
}
108-
109-
res, err := cliCtx.QueryWithData("custom/stake/delegator", bz)
110-
if err != nil {
111-
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
112-
return
113-
}
114-
115-
w.Write(res)
116-
}
86+
return queryDelegator(cliCtx, cdc, "custom/stake/delegator")
11787
}
11888

11989
// HTTP request handler to query all staking txs (msgs) from a delegator
12090
func delegatorTxsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
12191
return func(w http.ResponseWriter, r *http.Request) {
122-
var output []byte
12392
var typesQuerySlice []string
12493
vars := mux.Vars(r)
12594
delegatorAddr := vars["delegatorAddr"]
12695

127-
w.Header().Set("Content-Type", "application/json")
128-
12996
_, err := sdk.AccAddressFromBech32(delegatorAddr)
13097
if err != nil {
13198
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
@@ -134,8 +101,7 @@ func delegatorTxsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Han
134101

135102
node, err := cliCtx.GetNode()
136103
if err != nil {
137-
w.WriteHeader(http.StatusInternalServerError)
138-
w.Write([]byte(err.Error()))
104+
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
139105
return
140106
}
141107

@@ -182,198 +148,52 @@ func delegatorTxsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Han
182148
txs = append(txs, foundTxs...)
183149
}
184150

185-
output, err = cdc.MarshalJSON(txs)
151+
res, err := cdc.MarshalJSON(txs)
186152
if err != nil {
187153
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
188154
return
189155
}
190-
w.Write(output)
156+
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
191157
}
192158
}
193159

194160
// HTTP request handler to query an unbonding-delegation
195161
func unbondingDelegationHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
196-
return func(w http.ResponseWriter, r *http.Request) {
197-
vars := mux.Vars(r)
198-
bech32delegator := vars["delegatorAddr"]
199-
bech32validator := vars["validatorAddr"]
200-
201-
w.Header().Set("Content-Type", "application/json")
202-
203-
delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator)
204-
if err != nil {
205-
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
206-
return
207-
}
208-
209-
validatorAddr, err := sdk.ValAddressFromBech32(bech32validator)
210-
if err != nil {
211-
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
212-
return
213-
}
214-
215-
params := stake.QueryBondsParams{
216-
DelegatorAddr: delegatorAddr,
217-
ValidatorAddr: validatorAddr,
218-
}
219-
220-
bz, err := cdc.MarshalJSON(params)
221-
if err != nil {
222-
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
223-
return
224-
}
225-
226-
res, err := cliCtx.QueryWithData("custom/stake/unbondingDelegation", bz)
227-
if err != nil {
228-
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
229-
return
230-
}
231-
232-
w.Write(res)
233-
}
162+
return queryBonds(cliCtx, cdc, "custom/stake/unbondingDelegation")
234163
}
235164

236-
// HTTP request handler to query a bonded validator
165+
// HTTP request handler to query a delegation
237166
func delegationHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
238-
return func(w http.ResponseWriter, r *http.Request) {
239-
// read parameters
240-
vars := mux.Vars(r)
241-
bech32delegator := vars["delegatorAddr"]
242-
bech32validator := vars["validatorAddr"]
243-
244-
w.Header().Set("Content-Type", "application/json")
245-
246-
delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator)
247-
if err != nil {
248-
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
249-
return
250-
}
251-
252-
validatorAddr, err := sdk.ValAddressFromBech32(bech32validator)
253-
if err != nil {
254-
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
255-
return
256-
}
257-
258-
params := stake.QueryBondsParams{
259-
DelegatorAddr: delegatorAddr,
260-
ValidatorAddr: validatorAddr,
261-
}
262-
263-
bz, err := cdc.MarshalJSON(params)
264-
if err != nil {
265-
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
266-
return
267-
}
268-
269-
res, err := cliCtx.QueryWithData("custom/stake/delegation", bz)
270-
if err != nil {
271-
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
272-
return
273-
}
274-
275-
w.Write(res)
276-
}
167+
return queryBonds(cliCtx, cdc, "custom/stake/delegation")
277168
}
278169

279170
// HTTP request handler to query all delegator bonded validators
280171
func delegatorValidatorsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
281-
return func(w http.ResponseWriter, r *http.Request) {
282-
// read parameters
283-
vars := mux.Vars(r)
284-
bech32delegator := vars["delegatorAddr"]
285-
286-
w.Header().Set("Content-Type", "application/json")
287-
288-
delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator)
289-
if err != nil {
290-
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
291-
return
292-
}
293-
294-
params := stake.QueryDelegatorParams{
295-
DelegatorAddr: delegatorAddr,
296-
}
297-
298-
bz, err := cdc.MarshalJSON(params)
299-
if err != nil {
300-
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
301-
return
302-
}
303-
304-
res, err := cliCtx.QueryWithData("custom/stake/delegatorValidators", bz)
305-
if err != nil {
306-
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
307-
return
308-
}
309-
310-
w.Write(res)
311-
}
172+
return queryDelegator(cliCtx, cdc, "custom/stake/delegatorValidators")
312173
}
313174

314175
// HTTP request handler to get information from a currently bonded validator
315176
func delegatorValidatorHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
316-
return func(w http.ResponseWriter, r *http.Request) {
317-
318-
vars := mux.Vars(r)
319-
bech32delegator := vars["delegatorAddr"]
320-
bech32validator := vars["validatorAddr"]
321-
322-
w.Header().Set("Content-Type", "application/json")
323-
324-
delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator)
325-
validatorAddr, err := sdk.ValAddressFromBech32(bech32validator)
326-
if err != nil {
327-
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
328-
return
329-
}
330-
331-
params := stake.QueryBondsParams{
332-
DelegatorAddr: delegatorAddr,
333-
ValidatorAddr: validatorAddr,
334-
}
335-
336-
bz, err := cdc.MarshalJSON(params)
337-
if err != nil {
338-
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
339-
return
340-
}
341-
342-
res, err := cliCtx.QueryWithData("custom/stake/delegatorValidator", bz)
343-
if err != nil {
344-
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
345-
return
346-
}
347-
348-
w.Write(res)
349-
}
177+
return queryBonds(cliCtx, cdc, "custom/stake/delegatorValidator")
350178
}
351179

352180
// HTTP request handler to query list of validators
353-
func validatorsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
181+
func validatorsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
354182
return func(w http.ResponseWriter, r *http.Request) {
355-
356-
w.Header().Set("Content-Type", "application/json")
357-
358183
res, err := cliCtx.QueryWithData("custom/stake/validators", nil)
359184
if err != nil {
360185
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
361186
return
362187
}
363-
364-
w.Header().Set("Content-Type", "application/json")
365-
w.Write(res)
188+
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
366189
}
367190
}
368191

369192
// HTTP request handler to query the validator information from a given validator address
370193
func validatorHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
371194
return func(w http.ResponseWriter, r *http.Request) {
372-
373195
vars := mux.Vars(r)
374-
bech32validatorAddr := vars["addr"]
375-
376-
w.Header().Set("Content-Type", "application/json")
196+
bech32validatorAddr := vars["validatorAddr"]
377197

378198
validatorAddr, err := sdk.ValAddressFromBech32(bech32validatorAddr)
379199
if err != nil {
@@ -396,39 +216,30 @@ func validatorHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle
396216
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
397217
return
398218
}
399-
400-
w.Write(res)
219+
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
401220
}
402221
}
403222

404223
// HTTP request handler to query the pool information
405-
func poolHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
224+
func poolHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
406225
return func(w http.ResponseWriter, r *http.Request) {
407-
408-
w.Header().Set("Content-Type", "application/json")
409-
410226
res, err := cliCtx.QueryWithData("custom/stake/pool", nil)
411227
if err != nil {
412228
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
413229
return
414230
}
415-
416-
w.Write(res)
231+
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
417232
}
418233
}
419234

420235
// HTTP request handler to query the staking params values
421-
func paramsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
236+
func paramsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
422237
return func(w http.ResponseWriter, r *http.Request) {
423-
424-
w.Header().Set("Content-Type", "application/json")
425-
426238
res, err := cliCtx.QueryWithData("custom/stake/parameters", nil)
427239
if err != nil {
428240
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
429241
return
430242
}
431-
432-
w.Write(res)
243+
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
433244
}
434245
}

x/stake/client/rest/tx.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,12 @@ func delegationsRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx conte
262262
results[i] = res
263263
}
264264

265-
output, err := codec.MarshalJSONIndent(cdc, results[:])
265+
res, err := codec.MarshalJSONIndent(cdc, results[:])
266266
if err != nil {
267267
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
268268
return
269269
}
270270

271-
w.Write(output)
271+
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
272272
}
273273
}

0 commit comments

Comments
 (0)