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"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2020-02-19 16:04:13 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
2018-06-29 20:34:55 -07:00
|
|
|
)
|
|
|
|
|
2019-06-06 08:43:15 -07:00
|
|
|
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
2018-06-29 20:34:55 -07:00
|
|
|
r.HandleFunc(
|
2018-10-24 06:19:48 -07:00
|
|
|
"/slashing/validators/{validatorPubKey}/signing_info",
|
2019-06-06 08:43:15 -07:00
|
|
|
signingInfoHandlerFn(cliCtx),
|
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-06-06 08:43:15 -07:00
|
|
|
signingInfoHandlerListFn(cliCtx),
|
2019-05-09 10:38:15 -07:00
|
|
|
).Methods("GET")
|
2019-03-28 11:43:33 -07:00
|
|
|
|
2018-12-14 11:09:39 -08:00
|
|
|
r.HandleFunc(
|
|
|
|
"/slashing/parameters",
|
2019-06-06 08:43:15 -07:00
|
|
|
queryParamsHandlerFn(cliCtx),
|
2018-12-14 11:09:39 -08:00
|
|
|
).Methods("GET")
|
2018-06-29 20:34:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// http request handler to query signing info
|
2019-06-06 08:43:15 -07:00
|
|
|
func signingInfoHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
2018-06-29 20:34:55 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2020-01-09 06:04:28 -08:00
|
|
|
pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, 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-06-07 06:02:07 -07:00
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
params := types.NewQuerySigningInfoParams(sdk.ConsAddress(pk.Address()))
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2019-06-06 08:43:15 -07:00
|
|
|
bz, err := cliCtx.Codec.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-06-05 16:26:17 -07:00
|
|
|
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySigningInfo)
|
2019-07-01 09:48:13 -07:00
|
|
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
2019-05-09 10:38:15 -07:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-01 09:48:13 -07:00
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
2019-06-06 08:43:15 -07:00
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
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-06-06 08:43:15 -07:00
|
|
|
func signingInfoHandlerListFn(cliCtx context.CLIContext) 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-06-07 06:02:07 -07:00
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
params := types.NewQuerySigningInfosParams(page, limit)
|
2019-06-06 08:43:15 -07:00
|
|
|
bz, err := cliCtx.Codec.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-06-05 16:26:17 -07:00
|
|
|
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySigningInfos)
|
2019-07-01 09:48:13 -07:00
|
|
|
res, height, 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-07-01 09:48:13 -07:00
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
2019-06-06 08:43:15 -07:00
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
2018-06-29 20:34:55 -07:00
|
|
|
}
|
|
|
|
}
|
2018-12-14 11:09:39 -08:00
|
|
|
|
2019-06-06 08:43:15 -07:00
|
|
|
func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
2018-12-14 11:09:39 -08:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2019-06-07 06:02:07 -07:00
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
route := fmt.Sprintf("custom/%s/parameters", types.QuerierRoute)
|
2018-12-14 11:09:39 -08:00
|
|
|
|
2019-07-01 09:48:13 -07:00
|
|
|
res, height, err := cliCtx.QueryWithData(route, nil)
|
2018-12-14 11:09:39 -08:00
|
|
|
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-07-01 09:48:13 -07:00
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
2019-06-06 08:43:15 -07:00
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
2018-12-14 11:09:39 -08:00
|
|
|
}
|
|
|
|
}
|