Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Staking REST utils refactor and querier tests (pt 1 redelegation PR) #2537

Merged
merged 33 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
84fc6ea
docs: add config.js for ToC sidebar
zramsay Aug 15, 2018
4928c19
Updating config.js for docs website build process
greg-szabo Aug 27, 2018
5cd17d0
Update config.js
faboweb Aug 28, 2018
f15139f
Merge pull request #2185 from cosmos/greg/docs-deployment
ebuchman Aug 29, 2018
3fe942b
double curlies breaks vuepress - single curlies does not
jbibla Aug 24, 2018
416181b
Merge pull request #2203 from cosmos/greg/website-hotfix
ebuchman Aug 31, 2018
5281799
Add redelegation REST endpoint
Sep 13, 2018
b8a547d
Fixed utils and added tests
Sep 13, 2018
80427f0
Redelegation Querier tests
Sep 14, 2018
4f1a4ff
Fix HumanReadableString
Sep 14, 2018
39169ff
Gaia-lite tests
Sep 14, 2018
334f998
fix conflicts
Sep 30, 2018
6dddbb0
add NewQuerier tests
Oct 2, 2018
fcb1044
Changelog, renaming and refactor
Oct 2, 2018
96b997f
Fixed error in rest redelegation endpoint
Oct 2, 2018
8bd3210
Merge branch 'develop' into fedekunze/2182-redelegation-endpoint
cwgoes Oct 3, 2018
e0c6811
Increased coverage
Oct 3, 2018
57d71df
Merge develop changes
Oct 3, 2018
dbfe7ca
Fix conflicts
Oct 9, 2018
35cd97c
Update stake rest
Oct 9, 2018
7c7dc55
Add final unit tests
Oct 9, 2018
a5d84b9
Fix error import
Oct 9, 2018
0129010
Update PENDING
Oct 9, 2018
444c4c7
Merge branch 'develop' of https://github.com/cosmos/cosmos-sdk into f…
Oct 11, 2018
26c4375
RPC: add temporary file for hotfix
zramsay Oct 12, 2018
50675b8
Merge pull request #2486 from cosmos/hotfix-2
ebuchman Oct 12, 2018
a38cc91
Merge branch 'develop' of https://github.com/cosmos/cosmos-sdk into f…
Oct 15, 2018
ab7654d
MErge conflicts
Oct 17, 2018
e8d0892
Merge branch 'fedekunze/2182-redelegation-endpoint' of https://github…
Oct 17, 2018
2dbb61f
Merge develop and conflicts
Oct 19, 2018
c2dfbc5
Unnecessary changes
Oct 19, 2018
364e77c
Delete redelegation reference
Oct 19, 2018
c1d342b
use util post process response
Oct 19, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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