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

Add lcd endpoints and commands exists to runner and process #1768

Merged
merged 1 commit into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions x/process/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ var (

ModuleCdc = types.ModuleCdc

QueryGet = types.QueryGet
QueryList = types.QueryList
QueryGet = types.QueryGet
QueryList = types.QueryList
QueryExist = types.QueryExist
)

// module types
Expand Down
21 changes: 21 additions & 0 deletions x/process/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
flags.GetCommands(
GetCmdGet(queryRoute, cdc),
GetCmdList(queryRoute, cdc),
GetCmdExist(queryRoute, cdc),
)...,
)

Expand Down Expand Up @@ -71,3 +72,23 @@ func GetCmdList(queryRoute string, cdc *codec.Codec) *cobra.Command {
},
}
}

func GetCmdExist(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "exist [hash]",
Short: "exist",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, types.QueryExist, args[0]), nil)
if err != nil {
fmt.Printf("could not check process\n%s\n", err.Error())
return nil
}

var out bool
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
}
27 changes: 27 additions & 0 deletions x/process/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
"/process/parameters",
queryParamsHandlerFn(cliCtx),
).Methods(http.MethodGet)

r.HandleFunc(
"/process/exist/{hash}",
queryExistHandlerFn(cliCtx),
).Methods(http.MethodGet)
}

func queryGetHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
Expand Down Expand Up @@ -131,3 +136,25 @@ func queryHashHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
rest.PostProcessResponse(w, cliCtx, proc.Hash.String())
}
}

func queryExistHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryExist, vars["hash"])

res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
5 changes: 5 additions & 0 deletions x/process/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ func (k Keeper) Get(ctx sdk.Context, hash hash.Hash) (*processpb.Process, error)
return p, nil
}

// Exists returns true if a specific set of data exists in the database, false otherwise
func (k Keeper) Exists(ctx sdk.Context, hash hash.Hash) (bool, error) {
return ctx.KVStore(k.storeKey).Has(hash), nil
}

// List returns all processes.
func (k Keeper) List(ctx sdk.Context) ([]*processpb.Process, error) {
var (
Expand Down
23 changes: 23 additions & 0 deletions x/process/internal/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func NewQuerier(k Keeper) sdk.Querier {
return get(ctx, path[1:], k)
case types.QueryList:
return list(ctx, k)
case types.QueryExist:
return exist(ctx, k, path[1:])
default:
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown process query endpoint")
}
Expand Down Expand Up @@ -55,3 +57,24 @@ func list(ctx sdk.Context, k Keeper) ([]byte, error) {
}
return res, nil
}

func exist(ctx sdk.Context, k Keeper, path []string) ([]byte, error) {
if len(path) == 0 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "missing hash")
}
hash, err := hash.Decode(path[0])
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}

exists, err := k.Exists(ctx, hash)
if err != nil {
return nil, err
}

res, err := types.ModuleCdc.MarshalJSON(exists)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
5 changes: 3 additions & 2 deletions x/process/internal/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

// Query endpoints supported by the process querier
const (
QueryGet = "get"
QueryList = "list"
QueryGet = "get"
QueryList = "list"
QueryExist = "exist"
)
5 changes: 3 additions & 2 deletions x/runner/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ var (

ModuleCdc = types.ModuleCdc

QueryGet = types.QueryGet
QueryList = types.QueryList
QueryGet = types.QueryGet
QueryList = types.QueryList
QueryExist = types.QueryExist
)

// module types
Expand Down
21 changes: 21 additions & 0 deletions x/runner/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
flags.GetCommands(
GetCmdGet(queryRoute, cdc),
GetCmdList(queryRoute, cdc),
GetCmdExist(queryRoute, cdc),
)...,
)

Expand Down Expand Up @@ -71,3 +72,23 @@ func GetCmdList(queryRoute string, cdc *codec.Codec) *cobra.Command {
},
}
}

func GetCmdExist(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "exist [hash]",
Short: "exist",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, types.QueryExist, args[0]), nil)
if err != nil {
fmt.Printf("could not check runner\n%s\n", err.Error())
return nil
}

var out bool
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
}
27 changes: 27 additions & 0 deletions x/runner/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
"/runner/parameters",
queryParamsHandlerFn(cliCtx),
).Methods(http.MethodGet)

r.HandleFunc(
"/runner/exist/{hash}",
queryExistHandlerFn(cliCtx),
).Methods(http.MethodGet)
}

func queryGetHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
Expand Down Expand Up @@ -166,3 +171,25 @@ func queryHashHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
rest.PostProcessResponse(w, cliCtx, res)
}
}

func queryExistHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryExist, vars["hash"])

res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
5 changes: 5 additions & 0 deletions x/runner/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ func (k Keeper) Get(ctx sdk.Context, hash hash.Hash) (*runner.Runner, error) {
return r, nil
}

// Exists returns true if a specific set of data exists in the database, false otherwise
func (k Keeper) Exists(ctx sdk.Context, hash hash.Hash) (bool, error) {
return ctx.KVStore(k.storeKey).Has(hash), nil
}

// List returns all runners.
func (k Keeper) List(ctx sdk.Context) ([]*runner.Runner, error) {
var (
Expand Down
23 changes: 23 additions & 0 deletions x/runner/internal/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func NewQuerier(k Keeper) sdk.Querier {
return get(ctx, path[1:], k)
case types.QueryList:
return list(ctx, k)
case types.QueryExist:
return exist(ctx, k, path[1:])
default:
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown runner query endpoint")
}
Expand Down Expand Up @@ -55,3 +57,24 @@ func list(ctx sdk.Context, k Keeper) ([]byte, error) {
}
return res, nil
}

func exist(ctx sdk.Context, k Keeper, path []string) ([]byte, error) {
if len(path) == 0 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "missing hash")
}
hash, err := hash.Decode(path[0])
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}

exists, err := k.Exists(ctx, hash)
if err != nil {
return nil, err
}

res, err := types.ModuleCdc.MarshalJSON(exists)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
5 changes: 3 additions & 2 deletions x/runner/internal/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

// Query endpoints supported by the runner querier
const (
QueryGet = "get"
QueryList = "list"
QueryGet = "get"
QueryList = "list"
QueryExist = "exist"
)
4 changes: 2 additions & 2 deletions x/service/internal/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ func exist(ctx sdk.Context, k Keeper, path []string) ([]byte, error) {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}

srv, err := k.Exists(ctx, hash)
exists, err := k.Exists(ctx, hash)
if err != nil {
return nil, err
}

res, err := types.ModuleCdc.MarshalJSON(srv)
res, err := types.ModuleCdc.MarshalJSON(exists)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
Expand Down