Skip to content

Commit 2140244

Browse files
authored
Merge PR #5508: Fix and Return Height in x/distribution REST Handlers
1 parent bf41dea commit 2140244

File tree

5 files changed

+85
-60
lines changed

5 files changed

+85
-60
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ to detail this new feature and how state transitions occur.
254254

255255
### Bug Fixes
256256

257+
* (rest) [\#5508](https://github.com/cosmos/cosmos-sdk/pull/5508) Fix `x/distribution` endpoints to properly return height in the response.
257258
* (x/genutil) [\#5499](https://github.com/cosmos/cosmos-sdk/pull/) Ensure `DefaultGenesis` returns valid and non-nil default genesis state.
258259
* (client) [\#5303](https://github.com/cosmos/cosmos-sdk/issues/5303) Fix ignored error in tx generate only mode.
259260
* (cli) [\#4763](https://github.com/cosmos/cosmos-sdk/issues/4763) Fix flag `--min-self-delegation` for staking `EditValidator`

x/distribution/client/cli/query.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -217,26 +217,44 @@ $ %s query distribution rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p co
217217
RunE: func(cmd *cobra.Command, args []string) error {
218218
cliCtx := context.NewCLIContext().WithCodec(cdc)
219219

220+
// query for rewards from a particular delegation
220221
if len(args) == 2 {
221-
// query for rewards from a particular delegation
222-
resp, err := common.QueryDelegationRewards(cliCtx, queryRoute, args[0], args[1])
222+
resp, _, err := common.QueryDelegationRewards(cliCtx, queryRoute, args[0], args[1])
223223
if err != nil {
224224
return err
225225
}
226226

227227
var result sdk.DecCoins
228-
cdc.MustUnmarshalJSON(resp, &result)
228+
if err = cdc.UnmarshalJSON(resp, &result); err != nil {
229+
return fmt.Errorf("failed to unmarshal response: %w", err)
230+
}
231+
229232
return cliCtx.PrintOutput(result)
230233
}
231234

235+
delegatorAddr, err := sdk.AccAddressFromBech32(args[0])
236+
if err != nil {
237+
return err
238+
}
239+
240+
params := types.NewQueryDelegatorParams(delegatorAddr)
241+
bz, err := cdc.MarshalJSON(params)
242+
if err != nil {
243+
return fmt.Errorf("failed to marshal params: %w", err)
244+
}
245+
232246
// query for delegator total rewards
233-
resp, err := common.QueryDelegatorTotalRewards(cliCtx, queryRoute, args[0])
247+
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorTotalRewards)
248+
res, _, err := cliCtx.QueryWithData(route, bz)
234249
if err != nil {
235250
return err
236251
}
237252

238253
var result types.QueryDelegatorTotalRewardsResponse
239-
cdc.MustUnmarshalJSON(resp, &result)
254+
if err = cdc.UnmarshalJSON(res, &result); err != nil {
255+
return fmt.Errorf("failed to unmarshal response: %w", err)
256+
}
257+
240258
return cliCtx.PrintOutput(result)
241259
},
242260
}

x/distribution/client/common/common.go

+11-21
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,27 @@ import (
88
"github.com/cosmos/cosmos-sdk/x/distribution/types"
99
)
1010

11-
// QueryDelegatorTotalRewards queries delegator total rewards.
12-
func QueryDelegatorTotalRewards(cliCtx context.CLIContext, queryRoute, delAddr string) ([]byte, error) {
11+
// QueryDelegationRewards queries a delegation rewards between a delegator and a
12+
// validator.
13+
func QueryDelegationRewards(cliCtx context.CLIContext, queryRoute, delAddr, valAddr string) ([]byte, int64, error) {
1314
delegatorAddr, err := sdk.AccAddressFromBech32(delAddr)
1415
if err != nil {
15-
return nil, err
16+
return nil, 0, err
1617
}
1718

18-
res, _, err := cliCtx.QueryWithData(
19-
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorTotalRewards),
20-
cliCtx.Codec.MustMarshalJSON(types.NewQueryDelegatorParams(delegatorAddr)),
21-
)
22-
return res, err
23-
}
24-
25-
// QueryDelegationRewards queries a delegation rewards.
26-
func QueryDelegationRewards(cliCtx context.CLIContext, queryRoute, delAddr, valAddr string) ([]byte, error) {
27-
delegatorAddr, err := sdk.AccAddressFromBech32(delAddr)
19+
validatorAddr, err := sdk.ValAddressFromBech32(valAddr)
2820
if err != nil {
29-
return nil, err
21+
return nil, 0, err
3022
}
3123

32-
validatorAddr, err := sdk.ValAddressFromBech32(valAddr)
24+
params := types.NewQueryDelegationRewardsParams(delegatorAddr, validatorAddr)
25+
bz, err := cliCtx.Codec.MarshalJSON(params)
3326
if err != nil {
34-
return nil, err
27+
return nil, 0, fmt.Errorf("failed to marshal params: %w", err)
3528
}
3629

37-
res, _, err := cliCtx.QueryWithData(
38-
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegationRewards),
39-
cliCtx.Codec.MustMarshalJSON(types.NewQueryDelegationRewardsParams(delegatorAddr, validatorAddr)),
40-
)
41-
return res, err
30+
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegationRewards)
31+
return cliCtx.QueryWithData(route, bz)
4232
}
4333

4434
// QueryDelegatorValidators returns delegator's list of validators

x/distribution/client/common/common_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestQueryDelegationRewardsAddrValidation(t *testing.T) {
3030
for _, tt := range tests {
3131
tt := tt
3232
t.Run(tt.name, func(t *testing.T) {
33-
_, err := QueryDelegationRewards(ctx, "", tt.args.delAddr, tt.args.valAddr)
33+
_, _, err := QueryDelegationRewards(ctx, "", tt.args.delAddr, tt.args.valAddr)
3434
require.True(t, err != nil, tt.wantErr)
3535
})
3636
}

x/distribution/client/rest/query.go

+49-33
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,26 @@ func delegatorRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) htt
7373
return
7474
}
7575

76-
// query for rewards from a particular delegator
77-
res, ok := checkResponseQueryDelegatorTotalRewards(w, cliCtx, queryRoute, mux.Vars(r)["delegatorAddr"])
76+
delegatorAddr, ok := checkDelegatorAddressVar(w, r)
7877
if !ok {
7978
return
8079
}
8180

81+
params := types.NewQueryDelegatorParams(delegatorAddr)
82+
bz, err := cliCtx.Codec.MarshalJSON(params)
83+
if err != nil {
84+
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal params: %s", err))
85+
return
86+
}
87+
88+
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorTotalRewards)
89+
res, height, err := cliCtx.QueryWithData(route, bz)
90+
if err != nil {
91+
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
92+
return
93+
}
94+
95+
cliCtx = cliCtx.WithHeight(height)
8296
rest.PostProcessResponse(w, cliCtx, res)
8397
}
8498
}
@@ -91,12 +105,16 @@ func delegationRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) ht
91105
return
92106
}
93107

108+
delAddr := mux.Vars(r)["delegatorAddr"]
109+
valAddr := mux.Vars(r)["validatorAddr"]
110+
94111
// query for rewards from a particular delegation
95-
res, ok := checkResponseQueryDelegationRewards(w, cliCtx, queryRoute, mux.Vars(r)["delegatorAddr"], mux.Vars(r)["validatorAddr"])
112+
res, height, ok := checkResponseQueryDelegationRewards(w, cliCtx, queryRoute, delAddr, valAddr)
96113
if !ok {
97114
return
98115
}
99116

117+
cliCtx = cliCtx.WithHeight(height)
100118
rest.PostProcessResponse(w, cliCtx, res)
101119
}
102120
}
@@ -147,8 +165,7 @@ func NewValidatorDistInfo(operatorAddr sdk.AccAddress, rewards sdk.DecCoins,
147165
// HTTP request handler to query validator's distribution information
148166
func validatorInfoHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
149167
return func(w http.ResponseWriter, r *http.Request) {
150-
valAddr := mux.Vars(r)["validatorAddr"]
151-
validatorAddr, ok := checkValidatorAddressVar(w, r)
168+
valAddr, ok := checkValidatorAddressVar(w, r)
152169
if !ok {
153170
return
154171
}
@@ -159,28 +176,39 @@ func validatorInfoHandlerFn(cliCtx context.CLIContext, queryRoute string) http.H
159176
}
160177

161178
// query commission
162-
commissionRes, err := common.QueryValidatorCommission(cliCtx, queryRoute, validatorAddr)
179+
bz, err := common.QueryValidatorCommission(cliCtx, queryRoute, valAddr)
163180
if err != nil {
164181
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
165182
return
166183
}
167184

168-
var valCom types.ValidatorAccumulatedCommission
169-
cliCtx.Codec.MustUnmarshalJSON(commissionRes, &valCom)
185+
var commission types.ValidatorAccumulatedCommission
186+
if err := cliCtx.Codec.UnmarshalJSON(bz, &commission); err != nil {
187+
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
188+
return
189+
}
170190

171191
// self bond rewards
172-
delAddr := sdk.AccAddress(validatorAddr)
173-
rewardsRes, ok := checkResponseQueryDelegationRewards(w, cliCtx, queryRoute, delAddr.String(), valAddr)
192+
delAddr := sdk.AccAddress(valAddr)
193+
bz, height, ok := checkResponseQueryDelegationRewards(w, cliCtx, queryRoute, delAddr.String(), valAddr.String())
174194
if !ok {
175195
return
176196
}
177197

178198
var rewards sdk.DecCoins
179-
cliCtx.Codec.MustUnmarshalJSON(rewardsRes, &rewards)
199+
if err := cliCtx.Codec.UnmarshalJSON(bz, &rewards); err != nil {
200+
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
201+
return
202+
}
180203

181-
// Prepare response
182-
res := cliCtx.Codec.MustMarshalJSON(NewValidatorDistInfo(delAddr, rewards, valCom))
183-
rest.PostProcessResponse(w, cliCtx, res)
204+
bz, err = cliCtx.Codec.MarshalJSON(NewValidatorDistInfo(delAddr, rewards, commission))
205+
if err != nil {
206+
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
207+
return
208+
}
209+
210+
cliCtx = cliCtx.WithHeight(height)
211+
rest.PostProcessResponse(w, cliCtx, bz)
184212
}
185213
}
186214

@@ -199,12 +227,13 @@ func validatorRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) htt
199227
}
200228

201229
delAddr := sdk.AccAddress(validatorAddr).String()
202-
res, ok := checkResponseQueryDelegationRewards(w, cliCtx, queryRoute, delAddr, valAddr)
230+
bz, height, ok := checkResponseQueryDelegationRewards(w, cliCtx, queryRoute, delAddr, valAddr)
203231
if !ok {
204232
return
205233
}
206234

207-
rest.PostProcessResponse(w, cliCtx, res)
235+
cliCtx = cliCtx.WithHeight(height)
236+
rest.PostProcessResponse(w, cliCtx, bz)
208237
}
209238
}
210239

@@ -277,28 +306,15 @@ func outstandingRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) h
277306
}
278307
}
279308

280-
func checkResponseQueryDelegatorTotalRewards(
281-
w http.ResponseWriter, cliCtx context.CLIContext, queryRoute, delAddr string,
282-
) (res []byte, ok bool) {
283-
284-
res, err := common.QueryDelegatorTotalRewards(cliCtx, queryRoute, delAddr)
285-
if err != nil {
286-
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
287-
return nil, false
288-
}
289-
290-
return res, true
291-
}
292-
293309
func checkResponseQueryDelegationRewards(
294310
w http.ResponseWriter, cliCtx context.CLIContext, queryRoute, delAddr, valAddr string,
295-
) (res []byte, ok bool) {
311+
) (res []byte, height int64, ok bool) {
296312

297-
res, err := common.QueryDelegationRewards(cliCtx, queryRoute, delAddr, valAddr)
313+
res, height, err := common.QueryDelegationRewards(cliCtx, queryRoute, delAddr, valAddr)
298314
if err != nil {
299315
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
300-
return nil, false
316+
return nil, 0, false
301317
}
302318

303-
return res, true
319+
return res, height, true
304320
}

0 commit comments

Comments
 (0)