diff --git a/x/process/alias.go b/x/process/alias.go index 46fcf753a..4ec889f80 100644 --- a/x/process/alias.go +++ b/x/process/alias.go @@ -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 diff --git a/x/process/client/cli/query.go b/x/process/client/cli/query.go index 63b9ec92b..8a658b46e 100644 --- a/x/process/client/cli/query.go +++ b/x/process/client/cli/query.go @@ -27,6 +27,7 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { flags.GetCommands( GetCmdGet(queryRoute, cdc), GetCmdList(queryRoute, cdc), + GetCmdExist(queryRoute, cdc), )..., ) @@ -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) + }, + } +} diff --git a/x/process/client/rest/query.go b/x/process/client/rest/query.go index db78695aa..2a9f1f648 100644 --- a/x/process/client/rest/query.go +++ b/x/process/client/rest/query.go @@ -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 { @@ -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) + } +} diff --git a/x/process/internal/keeper/keeper.go b/x/process/internal/keeper/keeper.go index b8cb903bf..ece5e1d69 100644 --- a/x/process/internal/keeper/keeper.go +++ b/x/process/internal/keeper/keeper.go @@ -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 ( diff --git a/x/process/internal/keeper/querier.go b/x/process/internal/keeper/querier.go index a40cd97d7..6adaf5aca 100644 --- a/x/process/internal/keeper/querier.go +++ b/x/process/internal/keeper/querier.go @@ -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") } @@ -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 +} diff --git a/x/process/internal/types/querier.go b/x/process/internal/types/querier.go index 58d30f86b..b8981af87 100644 --- a/x/process/internal/types/querier.go +++ b/x/process/internal/types/querier.go @@ -2,6 +2,7 @@ package types // Query endpoints supported by the process querier const ( - QueryGet = "get" - QueryList = "list" + QueryGet = "get" + QueryList = "list" + QueryExist = "exist" ) diff --git a/x/runner/alias.go b/x/runner/alias.go index 4805e6867..86729a5cd 100644 --- a/x/runner/alias.go +++ b/x/runner/alias.go @@ -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 diff --git a/x/runner/client/cli/query.go b/x/runner/client/cli/query.go index 2bd0383ab..4f96d95a8 100644 --- a/x/runner/client/cli/query.go +++ b/x/runner/client/cli/query.go @@ -27,6 +27,7 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { flags.GetCommands( GetCmdGet(queryRoute, cdc), GetCmdList(queryRoute, cdc), + GetCmdExist(queryRoute, cdc), )..., ) @@ -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) + }, + } +} diff --git a/x/runner/client/rest/query.go b/x/runner/client/rest/query.go index 2b832d49d..fe427548c 100644 --- a/x/runner/client/rest/query.go +++ b/x/runner/client/rest/query.go @@ -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 { @@ -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) + } +} diff --git a/x/runner/internal/keeper/keeper.go b/x/runner/internal/keeper/keeper.go index 9db72eb8b..b3f1921a2 100644 --- a/x/runner/internal/keeper/keeper.go +++ b/x/runner/internal/keeper/keeper.go @@ -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 ( diff --git a/x/runner/internal/keeper/querier.go b/x/runner/internal/keeper/querier.go index 583dae2c6..83a6dff1a 100644 --- a/x/runner/internal/keeper/querier.go +++ b/x/runner/internal/keeper/querier.go @@ -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") } @@ -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 +} diff --git a/x/runner/internal/types/querier.go b/x/runner/internal/types/querier.go index 1bac1b5bb..2d24c7032 100644 --- a/x/runner/internal/types/querier.go +++ b/x/runner/internal/types/querier.go @@ -2,6 +2,7 @@ package types // Query endpoints supported by the runner querier const ( - QueryGet = "get" - QueryList = "list" + QueryGet = "get" + QueryList = "list" + QueryExist = "exist" ) diff --git a/x/service/internal/keeper/querier.go b/x/service/internal/keeper/querier.go index 0ec872df1..42e67fe53 100644 --- a/x/service/internal/keeper/querier.go +++ b/x/service/internal/keeper/querier.go @@ -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()) }