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

Fix Signing Infos Endpoint #4292

Merged
merged 9 commits into from
May 9, 2019
Prev Previous commit
Next Next commit
Update signingInfoHandlerListFn
  • Loading branch information
alexanderbez committed May 7, 2019
commit 039bc31bf4538eeaf57188a49386e6bc397e5f39
73 changes: 15 additions & 58 deletions x/slashing/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
Expand All @@ -22,8 +21,8 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Co

r.HandleFunc(
"/slashing/signing_infos",
signingInfoHandlerListFn(cliCtx, slashing.StoreKey, cdc),
).Methods("GET").Queries("page", "{page}", "limit", "{limit}")
signingInfoHandlerListFn(cliCtx, cdc),
).Methods("GET")

r.HandleFunc(
"/slashing/parameters",
Expand Down Expand Up @@ -52,55 +51,35 @@ func signingInfoHandlerFn(cliCtx context.CLIContext, storeName string, cdc *code
}

// http request handler to query signing info
func signingInfoHandlerListFn(cliCtx context.CLIContext, storeName string, cdc *codec.Codec) http.HandlerFunc {
func signingInfoHandlerListFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var signingInfoList []slashing.ValidatorSigningInfo

_, page, limit, err := rest.ParseHTTPArgs(r)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

height, err := rpc.GetChainHeight(cliCtx)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
// Override default limit if it wasn't provided as the querier will set
// it to the correct default limit.
if l := r.FormValue("limit"); l == "" {
limit = 0
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
}

valResult, err := rpc.GetValidators(cliCtx, &height)
params := slashing.NewQuerySigningInfosParams(page, limit)
bz, err := cdc.MarshalJSON(params)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

if len(valResult.Validators) == 0 {
rest.PostProcessResponse(w, cdc, []slashing.ValidatorSigningInfo{}, cliCtx.Indent)
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
alessio marked this conversation as resolved.
Show resolved Hide resolved
return
}

fmt.Println("NUM VALIDATORS IN RESULT:", len(valResult.Validators))

// TODO: this should happen when querying Validators from RPC,
// as soon as it's available this is not needed anymore
// parameter page is (page-1) because ParseHTTPArgs starts with page 1, where our array start with 0
start, end := adjustPagination(uint(len(valResult.Validators)), uint(page)-1, uint(limit))
for _, validator := range valResult.Validators[start:end] {
consAddr := validator.Address
signingInfo, code, err := getSigningInfo(cliCtx, storeName, cdc, consAddr)
if err != nil {
rest.WriteErrorResponse(w, code, err.Error())
return
}
signingInfoList = append(signingInfoList, signingInfo)
}

if len(signingInfoList) == 0 {
rest.PostProcessResponse(w, cdc, signingInfoList, cliCtx.Indent)
route := fmt.Sprintf("custom/%s/%s", slashing.QuerierRoute, slashing.QuerySigningInfos)
res, err := cliCtx.QueryWithData(route, bz)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

rest.PostProcessResponse(w, cdc, signingInfoList, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}

Expand Down Expand Up @@ -142,25 +121,3 @@ func getSigningInfo(

return signingInfo, code, nil
}

// Adjust pagination with page starting from 0
func adjustPagination(size, page, limit uint) (start uint, end uint) {
// If someone asks for pages bigger than our dataset, just return everything
if limit > size {
return 0, size
}

// Do pagination when healthy, fallback to 0
start = 0
if page*limit < size {
start = page * limit
}

// Do pagination only when healthy, fallback to len(dataset)
end = size
if start+limit <= size {
end = start + limit
}

return start, end
}