cosmos-sdk/x/slashing/client/rest/query.go

78 lines
2.0 KiB
Go
Raw Normal View History

package rest
import (
2018-12-14 11:09:39 -08:00
"fmt"
"net/http"
2018-12-10 06:27:25 -08:00
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
2018-10-24 06:37:06 -07:00
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing"
)
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {
r.HandleFunc(
"/slashing/validators/{validatorPubKey}/signing_info",
signingInfoHandlerFn(cliCtx, slashing.StoreKey, cdc),
).Methods("GET")
2018-12-14 11:09:39 -08:00
r.HandleFunc(
"/slashing/parameters",
queryParamsHandlerFn(cdc, cliCtx),
).Methods("GET")
}
// http request handler to query signing info
2018-08-31 15:22:37 -07:00
// nolint: unparam
func signingInfoHandlerFn(cliCtx context.CLIContext, storeName string, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
pk, err := sdk.GetConsPubKeyBech32(vars["validatorPubKey"])
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
key := slashing.GetValidatorSigningInfoKey(sdk.ConsAddress(pk.Address()))
2018-08-06 11:11:30 -07:00
res, err := cliCtx.QueryStore(key, storeName)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
if len(res) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}
var signingInfo slashing.ValidatorSigningInfo
err = cdc.UnmarshalBinaryLengthPrefixed(res, &signingInfo)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, signingInfo, cliCtx.Indent)
}
}
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 {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}