2018-06-29 20:34:55 -07:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2018-12-14 11:09:39 -08:00
|
|
|
"fmt"
|
2019-04-04 07:36:39 -07:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2018-06-29 20:34:55 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-06-29 20:34:55 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2018-06-29 20:34:55 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing"
|
|
|
|
)
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {
|
2018-06-29 20:34:55 -07:00
|
|
|
r.HandleFunc(
|
2018-10-24 06:19:48 -07:00
|
|
|
"/slashing/validators/{validatorPubKey}/signing_info",
|
2019-05-09 10:38:15 -07:00
|
|
|
signingInfoHandlerFn(cliCtx, cdc),
|
2018-06-29 20:34:55 -07:00
|
|
|
).Methods("GET")
|
2018-12-14 11:09:39 -08:00
|
|
|
|
2019-03-28 11:43:33 -07:00
|
|
|
r.HandleFunc(
|
|
|
|
"/slashing/signing_infos",
|
2019-05-09 10:38:15 -07:00
|
|
|
signingInfoHandlerListFn(cliCtx, cdc),
|
|
|
|
).Methods("GET")
|
2019-03-28 11:43:33 -07:00
|
|
|
|
2018-12-14 11:09:39 -08:00
|
|
|
r.HandleFunc(
|
|
|
|
"/slashing/parameters",
|
|
|
|
queryParamsHandlerFn(cdc, cliCtx),
|
|
|
|
).Methods("GET")
|
2018-06-29 20:34:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// http request handler to query signing info
|
2019-05-09 10:38:15 -07:00
|
|
|
func signingInfoHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
2018-06-29 20:34:55 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2018-10-24 06:19:48 -07:00
|
|
|
pk, err := sdk.GetConsPubKeyBech32(vars["validatorPubKey"])
|
2018-06-29 20:34:55 -07:00
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-06-29 20:34:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-09 10:38:15 -07:00
|
|
|
params := slashing.NewQuerySigningInfoParams(sdk.ConsAddress(pk.Address()))
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2019-05-09 10:38:15 -07:00
|
|
|
bz, err := cdc.MarshalJSON(params)
|
2018-06-29 20:34:55 -07:00
|
|
|
if err != nil {
|
2019-05-09 10:38:15 -07:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-06-29 20:34:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-09 10:38:15 -07:00
|
|
|
route := fmt.Sprintf("custom/%s/%s", slashing.QuerierRoute, slashing.QuerySigningInfo)
|
|
|
|
res, err := cliCtx.QueryWithData(route, bz)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
|
2019-03-28 11:43:33 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-24 06:19:48 -07:00
|
|
|
|
2019-03-28 11:43:33 -07:00
|
|
|
// http request handler to query signing info
|
2019-05-09 10:38:15 -07:00
|
|
|
func signingInfoHandlerListFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
2019-03-28 11:43:33 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2019-05-09 10:38:15 -07:00
|
|
|
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
2019-03-28 11:43:33 -07:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-09 10:38:15 -07:00
|
|
|
params := slashing.NewQuerySigningInfosParams(page, limit)
|
|
|
|
bz, err := cdc.MarshalJSON(params)
|
2018-06-29 20:34:55 -07:00
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-06-29 20:34:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-09 10:38:15 -07:00
|
|
|
route := fmt.Sprintf("custom/%s/%s", slashing.QuerierRoute, slashing.QuerySigningInfos)
|
|
|
|
res, err := cliCtx.QueryWithData(route, bz)
|
2019-03-28 11:43:33 -07:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-09 10:38:15 -07:00
|
|
|
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
|
2018-06-29 20:34:55 -07:00
|
|
|
}
|
|
|
|
}
|
2018-12-14 11:09:39 -08:00
|
|
|
|
|
|
|
func queryParamsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
route := fmt.Sprintf("custom/%s/parameters", slashing.QuerierRoute)
|
|
|
|
|
|
|
|
res, err := cliCtx.QueryWithData(route, nil)
|
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-12-14 11:09:39 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
|
2018-12-14 11:09:39 -08:00
|
|
|
}
|
|
|
|
}
|