Skip to content

Commit

Permalink
Merge PR #2537: Staking REST utils refactor and querier tests (pt 1 r…
Browse files Browse the repository at this point in the history
…edelegation PR)
  • Loading branch information
fedekunze authored and cwgoes committed Oct 19, 2018
1 parent b48d0d5 commit 57277d1
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 216 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
BUG FIXES

* Tendermint
- Fix unbounded consensus WAL growth
- Fix unbounded consensus WAL growth

## 0.24.1

Expand Down
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ IMPROVEMENTS
* [x/stake] [\#2000](https://github.com/cosmos/cosmos-sdk/issues/2000) Added tests for new staking endpoints
* [gaia-lite] [\#2445](https://github.com/cosmos/cosmos-sdk/issues/2445) Standarized REST error responses
* [gaia-lite] Added example to Swagger specification for /keys/seed.
* [x/stake] Refactor REST utils

* Gaia CLI (`gaiacli`)
* [cli] #2060 removed `--select` from `block` command
Expand Down
2 changes: 1 addition & 1 deletion docs/DOCS_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ much as possible with its [counterpart in the Tendermint Core repo](https://gith
4. Compile gaiacli
```
make install
```
```
231 changes: 21 additions & 210 deletions x/stake/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,74 +58,41 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Co
// Get all validators
r.HandleFunc(
"/stake/validators",
validatorsHandlerFn(cliCtx),
validatorsHandlerFn(cliCtx, cdc),
).Methods("GET")

// Get a single validator info
r.HandleFunc(
"/stake/validators/{addr}",
"/stake/validators/{validatorAddr}",
validatorHandlerFn(cliCtx, cdc),
).Methods("GET")

// Get the current state of the staking pool
r.HandleFunc(
"/stake/pool",
poolHandlerFn(cliCtx),
poolHandlerFn(cliCtx, cdc),
).Methods("GET")

// Get the current staking parameter values
r.HandleFunc(
"/stake/parameters",
paramsHandlerFn(cliCtx),
paramsHandlerFn(cliCtx, cdc),
).Methods("GET")

}

// HTTP request handler to query a delegator delegations
func delegatorHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

vars := mux.Vars(r)
bech32delegator := vars["delegatorAddr"]

w.Header().Set("Content-Type", "application/json")

delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

params := stake.QueryDelegatorParams{
DelegatorAddr: delegatorAddr,
}

bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

res, err := cliCtx.QueryWithData("custom/stake/delegator", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

w.Write(res)
}
return queryDelegator(cliCtx, cdc, "custom/stake/delegator")
}

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

w.Header().Set("Content-Type", "application/json")

_, err := sdk.AccAddressFromBech32(delegatorAddr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
Expand All @@ -134,8 +101,7 @@ func delegatorTxsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Han

node, err := cliCtx.GetNode()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

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

output, err = cdc.MarshalJSON(txs)
res, err := cdc.MarshalJSON(txs)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
w.Write(output)
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}

// HTTP request handler to query an unbonding-delegation
func unbondingDelegationHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bech32delegator := vars["delegatorAddr"]
bech32validator := vars["validatorAddr"]

w.Header().Set("Content-Type", "application/json")

delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

validatorAddr, err := sdk.ValAddressFromBech32(bech32validator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

params := stake.QueryBondsParams{
DelegatorAddr: delegatorAddr,
ValidatorAddr: validatorAddr,
}

bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

res, err := cliCtx.QueryWithData("custom/stake/unbondingDelegation", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

w.Write(res)
}
return queryBonds(cliCtx, cdc, "custom/stake/unbondingDelegation")
}

// HTTP request handler to query a bonded validator
// HTTP request handler to query a delegation
func delegationHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// read parameters
vars := mux.Vars(r)
bech32delegator := vars["delegatorAddr"]
bech32validator := vars["validatorAddr"]

w.Header().Set("Content-Type", "application/json")

delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

validatorAddr, err := sdk.ValAddressFromBech32(bech32validator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

params := stake.QueryBondsParams{
DelegatorAddr: delegatorAddr,
ValidatorAddr: validatorAddr,
}

bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

res, err := cliCtx.QueryWithData("custom/stake/delegation", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

w.Write(res)
}
return queryBonds(cliCtx, cdc, "custom/stake/delegation")
}

// HTTP request handler to query all delegator bonded validators
func delegatorValidatorsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// read parameters
vars := mux.Vars(r)
bech32delegator := vars["delegatorAddr"]

w.Header().Set("Content-Type", "application/json")

delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

params := stake.QueryDelegatorParams{
DelegatorAddr: delegatorAddr,
}

bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

res, err := cliCtx.QueryWithData("custom/stake/delegatorValidators", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

w.Write(res)
}
return queryDelegator(cliCtx, cdc, "custom/stake/delegatorValidators")
}

// HTTP request handler to get information from a currently bonded validator
func delegatorValidatorHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

vars := mux.Vars(r)
bech32delegator := vars["delegatorAddr"]
bech32validator := vars["validatorAddr"]

w.Header().Set("Content-Type", "application/json")

delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator)
validatorAddr, err := sdk.ValAddressFromBech32(bech32validator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

params := stake.QueryBondsParams{
DelegatorAddr: delegatorAddr,
ValidatorAddr: validatorAddr,
}

bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

res, err := cliCtx.QueryWithData("custom/stake/delegatorValidator", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

w.Write(res)
}
return queryBonds(cliCtx, cdc, "custom/stake/delegatorValidator")
}

// HTTP request handler to query list of validators
func validatorsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
func validatorsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")

res, err := cliCtx.QueryWithData("custom/stake/validators", nil)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

w.Header().Set("Content-Type", "application/json")
w.Write(res)
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}

// HTTP request handler to query the validator information from a given validator address
func validatorHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

vars := mux.Vars(r)
bech32validatorAddr := vars["addr"]

w.Header().Set("Content-Type", "application/json")
bech32validatorAddr := vars["validatorAddr"]

validatorAddr, err := sdk.ValAddressFromBech32(bech32validatorAddr)
if err != nil {
Expand All @@ -396,39 +216,30 @@ func validatorHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

w.Write(res)
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}

// HTTP request handler to query the pool information
func poolHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
func poolHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")

res, err := cliCtx.QueryWithData("custom/stake/pool", nil)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

w.Write(res)
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}

// HTTP request handler to query the staking params values
func paramsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
func paramsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")

res, err := cliCtx.QueryWithData("custom/stake/parameters", nil)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

w.Write(res)
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
4 changes: 2 additions & 2 deletions x/stake/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,12 @@ func delegationsRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx conte
results[i] = res
}

output, err := codec.MarshalJSONIndent(cdc, results[:])
res, err := codec.MarshalJSONIndent(cdc, results[:])
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

w.Write(output)
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
Loading

0 comments on commit 57277d1

Please sign in to comment.