From 21d0953a4bcfcbc944f37a7317056a50b0b84562 Mon Sep 17 00:00:00 2001 From: Sorawit Suriyakarn Date: Fri, 5 Jun 2020 10:06:06 +0700 Subject: [PATCH 1/4] chain: Add more functionality to querier --- chain/x/oracle/client/cli/query.go | 63 +++++++++++++++++++++++------ chain/x/oracle/client/rest/query.go | 38 ++++++++++++++++- chain/x/oracle/client/rest/rest.go | 11 ++--- chain/x/oracle/keeper/querier.go | 30 ++++++++++++++ chain/x/oracle/types/querier.go | 2 + 5 files changed, 124 insertions(+), 20 deletions(-) diff --git a/chain/x/oracle/client/cli/query.go b/chain/x/oracle/client/cli/query.go index eb904076e8..91c11b6a87 100644 --- a/chain/x/oracle/client/cli/query.go +++ b/chain/x/oracle/client/cli/query.go @@ -23,17 +23,18 @@ func GetQueryCmd(storeKey string, cdc *codec.Codec) *cobra.Command { RunE: client.ValidateCmd, } oracleCmd.AddCommand(flags.GetCommands( - GetCmdParams(storeKey, cdc), - GetCmdCounts(storeKey, cdc), - GetCmdDataSource(storeKey, cdc), - GetCmdOracleScript(storeKey, cdc), + GetQueryCmdParams(storeKey, cdc), + GetQueryCmdCounts(storeKey, cdc), + GetQueryCmdDataSource(storeKey, cdc), + GetQueryCmdOracleScript(storeKey, cdc), + GetQueryCmdRequest(storeKey, cdc), + GetQueryCmdReports(storeKey, cdc), )...) - return oracleCmd } -// GetCmdParams implements the query parameters command. -func GetCmdParams(route string, cdc *codec.Codec) *cobra.Command { +// GetQueryCmdParams implements the query parameters command. +func GetQueryCmdParams(route string, cdc *codec.Codec) *cobra.Command { return &cobra.Command{ Use: "params", Args: cobra.NoArgs, @@ -50,8 +51,8 @@ func GetCmdParams(route string, cdc *codec.Codec) *cobra.Command { } } -// GetCmdCounts implements the query counts command. -func GetCmdCounts(route string, cdc *codec.Codec) *cobra.Command { +// GetQueryCmdCounts implements the query counts command. +func GetQueryCmdCounts(route string, cdc *codec.Codec) *cobra.Command { return &cobra.Command{ Use: "counts", Args: cobra.NoArgs, @@ -68,8 +69,8 @@ func GetCmdCounts(route string, cdc *codec.Codec) *cobra.Command { } } -// GetCmdDataSource implements the query data source command. -func GetCmdDataSource(route string, cdc *codec.Codec) *cobra.Command { +// GetQueryCmdDataSource implements the query data source command. +func GetQueryCmdDataSource(route string, cdc *codec.Codec) *cobra.Command { return &cobra.Command{ Use: "data-source [id]", Args: cobra.ExactArgs(1), @@ -86,8 +87,8 @@ func GetCmdDataSource(route string, cdc *codec.Codec) *cobra.Command { } } -// GetCmdOracleScript implements the query oracle script command. -func GetCmdOracleScript(route string, cdc *codec.Codec) *cobra.Command { +// GetQueryCmdOracleScript implements the query oracle script command. +func GetQueryCmdOracleScript(route string, cdc *codec.Codec) *cobra.Command { return &cobra.Command{ Use: "oracle-script [id]", Args: cobra.ExactArgs(1), @@ -103,3 +104,39 @@ func GetCmdOracleScript(route string, cdc *codec.Codec) *cobra.Command { }, } } + +// GetQueryCmdRequest implements the query request command. +func GetQueryCmdRequest(route string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "request [id]", + 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", route, types.QueryRequests, args[0]), nil) + if err != nil { + return err + } + var out types.Request + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} + +// GetQueryCmdRequest implements the query reports command. +func GetQueryCmdReports(route string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "reports [req-id]", + 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", route, types.QueryReports, args[0]), nil) + if err != nil { + return err + } + var out []types.Report + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} diff --git a/chain/x/oracle/client/rest/query.go b/chain/x/oracle/client/rest/query.go index 11bba7a713..20f905ca2a 100644 --- a/chain/x/oracle/client/rest/query.go +++ b/chain/x/oracle/client/rest/query.go @@ -68,7 +68,7 @@ func getDataSourceByIDHandler(cliCtx context.CLIContext, route string) http.Hand return } vars := mux.Vars(r) - res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryDataSources, vars[dataSourceIDTag]), nil) + res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryDataSources, vars[idTag]), nil) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return @@ -85,7 +85,41 @@ func getOracleScriptByIDHandler(cliCtx context.CLIContext, route string) http.Ha return } vars := mux.Vars(r) - res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryOracleScripts, vars[oracleScriptIDTag]), nil) + res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryOracleScripts, vars[idTag]), nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func getRequestByIDHandler(cliCtx context.CLIContext, route string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + vars := mux.Vars(r) + res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryRequests, vars[idTag]), nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func getReportsByIDHandler(cliCtx context.CLIContext, route string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + vars := mux.Vars(r) + res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryReports, vars[idTag]), nil) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return diff --git a/chain/x/oracle/client/rest/rest.go b/chain/x/oracle/client/rest/rest.go index d168df8e6e..8a3f523c9f 100644 --- a/chain/x/oracle/client/rest/rest.go +++ b/chain/x/oracle/client/rest/rest.go @@ -9,15 +9,16 @@ import ( ) const ( - dataSourceIDTag = "dataSourceIDTag" - oracleScriptIDTag = "oracleScriptIDTag" - dataHashTag = "dataHashTag" + idTag = "idTag" + dataHashTag = "dataHashTag" ) func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string) { r.HandleFunc(fmt.Sprintf("/%s/params", storeName), getParamsHandler(cliCtx, storeName)).Methods("GET") r.HandleFunc(fmt.Sprintf("/%s/counts", storeName), getCountsHandler(cliCtx, storeName)).Methods("GET") r.HandleFunc(fmt.Sprintf("/%s/data/{%s}", storeName, dataHashTag), getDataByHashHandler(cliCtx, storeName)).Methods("GET") - r.HandleFunc(fmt.Sprintf("/%s/data_sources/{%s}", storeName, dataSourceIDTag), getDataSourceByIDHandler(cliCtx, storeName)).Methods("GET") - r.HandleFunc(fmt.Sprintf("/%s/oracle_scripts/{%s}", storeName, oracleScriptIDTag), getOracleScriptByIDHandler(cliCtx, storeName)).Methods("GET") + r.HandleFunc(fmt.Sprintf("/%s/data_sources/{%s}", storeName, idTag), getDataSourceByIDHandler(cliCtx, storeName)).Methods("GET") + r.HandleFunc(fmt.Sprintf("/%s/oracle_scripts/{%s}", storeName, idTag), getOracleScriptByIDHandler(cliCtx, storeName)).Methods("GET") + r.HandleFunc(fmt.Sprintf("/%s/requests/{%s}", storeName, idTag), getRequestByIDHandler(cliCtx, storeName)).Methods("GET") + r.HandleFunc(fmt.Sprintf("/%s/reports/{%s}", storeName, idTag), getReportsByIDHandler(cliCtx, storeName)).Methods("GET") } diff --git a/chain/x/oracle/keeper/querier.go b/chain/x/oracle/keeper/querier.go index 0ffc6bbb27..554e43c400 100644 --- a/chain/x/oracle/keeper/querier.go +++ b/chain/x/oracle/keeper/querier.go @@ -25,6 +25,10 @@ func NewQuerier(keeper Keeper) sdk.Querier { return queryDataSourceByID(ctx, path[1:], keeper) case types.QueryOracleScripts: return queryOracleScriptByID(ctx, path[1:], keeper) + case types.QueryRequests: + return queryRequestByID(ctx, path[1:], keeper) + case types.QueryReports: + return queryReportsByID(ctx, path[1:], keeper) default: return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown oracle query endpoint") } @@ -79,3 +83,29 @@ func queryOracleScriptByID(ctx sdk.Context, path []string, k Keeper) ([]byte, er } return codec.MarshalJSONIndent(types.ModuleCdc, oracleScript) } + +func queryRequestByID(ctx sdk.Context, path []string, k Keeper) ([]byte, error) { + if len(path) != 1 { + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "request not specified") + } + id, err := strconv.ParseInt(path[0], 10, 64) + if err != nil { + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, err.Error()) + } + request, err := k.GetRequest(ctx, types.RequestID(id)) + if err != nil { + return nil, err + } + return codec.MarshalJSONIndent(types.ModuleCdc, request) +} + +func queryReportsByID(ctx sdk.Context, path []string, k Keeper) ([]byte, error) { + if len(path) != 1 { + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "request id not specified") + } + id, err := strconv.ParseInt(path[0], 10, 64) + if err != nil { + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, err.Error()) + } + return codec.MarshalJSONIndent(types.ModuleCdc, k.GetReports(ctx, types.RequestID(id))) +} diff --git a/chain/x/oracle/types/querier.go b/chain/x/oracle/types/querier.go index 6406f6cd50..e987e998f4 100644 --- a/chain/x/oracle/types/querier.go +++ b/chain/x/oracle/types/querier.go @@ -7,6 +7,8 @@ const ( QueryData = "data" QueryDataSources = "data_sources" QueryOracleScripts = "oracle_scripts" + QueryRequests = "requests" + QueryReports = "reports" ) // QueryCountsResult is the struct for the result of query counts. From 52fac43d40a94fc5be26ffa1a69ef564e8b5f680 Mon Sep 17 00:00:00 2001 From: Sorawit Suriyakarn Date: Fri, 5 Jun 2020 10:26:57 +0700 Subject: [PATCH 2/4] chain: Remove unused import from x/oracle/handler --- chain/x/oracle/handler.go | 1 - 1 file changed, 1 deletion(-) diff --git a/chain/x/oracle/handler.go b/chain/x/oracle/handler.go index 8f4d79d14e..87f457b444 100644 --- a/chain/x/oracle/handler.go +++ b/chain/x/oracle/handler.go @@ -7,7 +7,6 @@ import ( "github.com/bandprotocol/bandchain/chain/x/oracle/types" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - _ "github.com/cosmos/cosmos-sdk/x/ibc/04-channel" ) // NewHandler creates the msg handler of this module, as required by Cosmos-SDK standard. From c59e521ad9f9f90b22133fd66b19c4b27c934fac Mon Sep 17 00:00:00 2001 From: Sorawit Suriyakarn Date: Fri, 5 Jun 2020 11:22:10 +0700 Subject: [PATCH 3/4] chain: Add query interface for request --- chain/x/oracle/client/cli/query.go | 21 +-------------------- chain/x/oracle/client/rest/query.go | 17 ----------------- chain/x/oracle/client/rest/rest.go | 1 - chain/x/oracle/handler.go | 1 - chain/x/oracle/keeper/querier.go | 19 +++++-------------- chain/x/oracle/types/querier.go | 7 ++++++- 6 files changed, 12 insertions(+), 54 deletions(-) diff --git a/chain/x/oracle/client/cli/query.go b/chain/x/oracle/client/cli/query.go index 91c11b6a87..62d083ea8f 100644 --- a/chain/x/oracle/client/cli/query.go +++ b/chain/x/oracle/client/cli/query.go @@ -28,7 +28,6 @@ func GetQueryCmd(storeKey string, cdc *codec.Codec) *cobra.Command { GetQueryCmdDataSource(storeKey, cdc), GetQueryCmdOracleScript(storeKey, cdc), GetQueryCmdRequest(storeKey, cdc), - GetQueryCmdReports(storeKey, cdc), )...) return oracleCmd } @@ -116,25 +115,7 @@ func GetQueryCmdRequest(route string, cdc *codec.Codec) *cobra.Command { if err != nil { return err } - var out types.Request - cdc.MustUnmarshalJSON(res, &out) - return cliCtx.PrintOutput(out) - }, - } -} - -// GetQueryCmdRequest implements the query reports command. -func GetQueryCmdReports(route string, cdc *codec.Codec) *cobra.Command { - return &cobra.Command{ - Use: "reports [req-id]", - 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", route, types.QueryReports, args[0]), nil) - if err != nil { - return err - } - var out []types.Report + var out types.QueryRequestResult cdc.MustUnmarshalJSON(res, &out) return cliCtx.PrintOutput(out) }, diff --git a/chain/x/oracle/client/rest/query.go b/chain/x/oracle/client/rest/query.go index 20f905ca2a..77b438ced4 100644 --- a/chain/x/oracle/client/rest/query.go +++ b/chain/x/oracle/client/rest/query.go @@ -111,20 +111,3 @@ func getRequestByIDHandler(cliCtx context.CLIContext, route string) http.Handler rest.PostProcessResponse(w, cliCtx, res) } } - -func getReportsByIDHandler(cliCtx context.CLIContext, route string) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) - if !ok { - return - } - vars := mux.Vars(r) - res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryReports, vars[idTag]), nil) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) - return - } - cliCtx = cliCtx.WithHeight(height) - rest.PostProcessResponse(w, cliCtx, res) - } -} diff --git a/chain/x/oracle/client/rest/rest.go b/chain/x/oracle/client/rest/rest.go index 8a3f523c9f..2431d89c39 100644 --- a/chain/x/oracle/client/rest/rest.go +++ b/chain/x/oracle/client/rest/rest.go @@ -20,5 +20,4 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string) r.HandleFunc(fmt.Sprintf("/%s/data_sources/{%s}", storeName, idTag), getDataSourceByIDHandler(cliCtx, storeName)).Methods("GET") r.HandleFunc(fmt.Sprintf("/%s/oracle_scripts/{%s}", storeName, idTag), getOracleScriptByIDHandler(cliCtx, storeName)).Methods("GET") r.HandleFunc(fmt.Sprintf("/%s/requests/{%s}", storeName, idTag), getRequestByIDHandler(cliCtx, storeName)).Methods("GET") - r.HandleFunc(fmt.Sprintf("/%s/reports/{%s}", storeName, idTag), getReportsByIDHandler(cliCtx, storeName)).Methods("GET") } diff --git a/chain/x/oracle/handler.go b/chain/x/oracle/handler.go index 8f4d79d14e..87f457b444 100644 --- a/chain/x/oracle/handler.go +++ b/chain/x/oracle/handler.go @@ -7,7 +7,6 @@ import ( "github.com/bandprotocol/bandchain/chain/x/oracle/types" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - _ "github.com/cosmos/cosmos-sdk/x/ibc/04-channel" ) // NewHandler creates the msg handler of this module, as required by Cosmos-SDK standard. diff --git a/chain/x/oracle/keeper/querier.go b/chain/x/oracle/keeper/querier.go index 554e43c400..cdcdd3de74 100644 --- a/chain/x/oracle/keeper/querier.go +++ b/chain/x/oracle/keeper/querier.go @@ -27,8 +27,6 @@ func NewQuerier(keeper Keeper) sdk.Querier { return queryOracleScriptByID(ctx, path[1:], keeper) case types.QueryRequests: return queryRequestByID(ctx, path[1:], keeper) - case types.QueryReports: - return queryReportsByID(ctx, path[1:], keeper) default: return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown oracle query endpoint") } @@ -93,19 +91,12 @@ func queryRequestByID(ctx sdk.Context, path []string, k Keeper) ([]byte, error) return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, err.Error()) } request, err := k.GetRequest(ctx, types.RequestID(id)) + reports := k.GetReports(ctx, types.RequestID(id)) if err != nil { return nil, err } - return codec.MarshalJSONIndent(types.ModuleCdc, request) -} - -func queryReportsByID(ctx sdk.Context, path []string, k Keeper) ([]byte, error) { - if len(path) != 1 { - return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "request id not specified") - } - id, err := strconv.ParseInt(path[0], 10, 64) - if err != nil { - return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, err.Error()) - } - return codec.MarshalJSONIndent(types.ModuleCdc, k.GetReports(ctx, types.RequestID(id))) + return codec.MarshalJSONIndent(types.ModuleCdc, types.QueryRequestResult{ + Request: request, + Reports: reports, + }) } diff --git a/chain/x/oracle/types/querier.go b/chain/x/oracle/types/querier.go index e987e998f4..7bce90c92e 100644 --- a/chain/x/oracle/types/querier.go +++ b/chain/x/oracle/types/querier.go @@ -8,7 +8,6 @@ const ( QueryDataSources = "data_sources" QueryOracleScripts = "oracle_scripts" QueryRequests = "requests" - QueryReports = "reports" ) // QueryCountsResult is the struct for the result of query counts. @@ -17,3 +16,9 @@ type QueryCountsResult struct { OracleScriptCount int64 `json:"oracle_script_count"` RequestCount int64 `json:"request_count"` } + +// QueryRequestResult is the struct for the result of request query. +type QueryRequestResult struct { + Request Request `json:"request"` + Reports []Report `json:"reports"` +} From 17da05caab4710a2f250dee510193507bcf8930a Mon Sep 17 00:00:00 2001 From: Sorawit Suriyakarn Date: Fri, 5 Jun 2020 11:24:08 +0700 Subject: [PATCH 4/4] Add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3abef2bd81..c47306782f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ ### Chain +- (feat) [\#1875](https://github.com/bandprotocol/bandchain/pull/1875) Add CLI and REST query interface for request. - (chore) [\#1869](https://github.com/bandprotocol/bandchain/pull/1869) Update new schema and source code url for all oracle scripts - (chore) [\#1864](https://github.com/bandprotocol/bandchain/pull/1864) Remove unused query types. - (impv) [\#1792](https://github.com/bandprotocol/bandchain/pull/1792) Request data message handler test