From 7588f1e9ca00b043643ac68802c046e22664823a Mon Sep 17 00:00:00 2001 From: lyh169 Date: Wed, 30 Aug 2023 18:42:23 +0800 Subject: [PATCH 1/3] add rest query the block config --- cmd/exchaind/rest.go | 2 ++ x/params/client/rest/rest.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 x/params/client/rest/rest.go diff --git a/cmd/exchaind/rest.go b/cmd/exchaind/rest.go index 2643047542..c1d143cbc8 100644 --- a/cmd/exchaind/rest.go +++ b/cmd/exchaind/rest.go @@ -35,6 +35,7 @@ import ( govrest "github.com/okex/exchain/x/gov/client/rest" orderrest "github.com/okex/exchain/x/order/client/rest" paramsclient "github.com/okex/exchain/x/params/client" + paramsrest "github.com/okex/exchain/x/params/client/rest" slashingrest "github.com/okex/exchain/x/slashing/client/rest" stakingrest "github.com/okex/exchain/x/staking/client/rest" "github.com/okex/exchain/x/token" @@ -102,6 +103,7 @@ func registerRoutesV1(rs *lcd.RestServer, pathPrefix string) { ) mintrest.RegisterRoutes(rs.CliCtx, v1Router) ibctransferrest.RegisterOriginRPCRoutersForGRPC(rs.CliCtx, v1Router) + paramsrest.RegisterRoutes(rs.CliCtx, v1Router) } func registerRoutesV2(rs *lcd.RestServer, pathPrefix string) { diff --git a/x/params/client/rest/rest.go b/x/params/client/rest/rest.go new file mode 100644 index 0000000000..6f1a334d26 --- /dev/null +++ b/x/params/client/rest/rest.go @@ -0,0 +1,32 @@ +package rest + +import ( + "fmt" + "github.com/okex/exchain/x/params/types" + "net/http" + + "github.com/gorilla/mux" + "github.com/okex/exchain/libs/cosmos-sdk/client/context" + "github.com/okex/exchain/libs/cosmos-sdk/types/rest" + "github.com/okex/exchain/x/common" + "github.com/okex/exchain/x/params" +) + +// RegisterRoutes, a central function to define routes +// which is called by the rest module in main application +func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { + r.HandleFunc(fmt.Sprintf("/params/blockconfig"), QueryBlockConfigFn(cliCtx)).Methods("GET") +} + +func QueryBlockConfigFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + route := fmt.Sprintf("custom/%s/%s", params.RouterKey, types.QueryBlockConfig) + bz, _, err := cliCtx.QueryWithData(route, nil) + if err != nil { + sdkErr := common.ParseSDKError(err.Error()) + common.HandleErrorMsg(w, cliCtx, sdkErr.Code, err.Error()) + return + } + rest.PostProcessResponseBare(w, cliCtx, bz) + } +} From 7d0d0cda82e4e898649abbcd91abade10aad47c4 Mon Sep 17 00:00:00 2001 From: lyh169 Date: Fri, 1 Sep 2023 15:03:29 +0800 Subject: [PATCH 2/3] add rest query the block config and add height --- x/params/client/cli/query.go | 16 ++++++++++++---- x/params/client/rest/rest.go | 10 +++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/x/params/client/cli/query.go b/x/params/client/cli/query.go index af69818a63..285e31eb65 100644 --- a/x/params/client/cli/query.go +++ b/x/params/client/cli/query.go @@ -3,6 +3,7 @@ package cli import ( "fmt" "github.com/okex/exchain/libs/cosmos-sdk/client/flags" + "strconv" "strings" "github.com/okex/exchain/x/params/types" @@ -41,7 +42,6 @@ $ exchaincli query params params Args: cobra.NoArgs, RunE: func(_ *cobra.Command, _ []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) - route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryParams) bz, _, err := cliCtx.QueryWithData(route, nil) if err != nil { @@ -90,9 +90,17 @@ func GetCmdQueryBlockConfig(queryRoute string, cdc *codec.Codec) *cobra.Command $ exchaincli query params blockconfig `), - Args: cobra.NoArgs, - RunE: func(_ *cobra.Command, _ []string) error { - cliCtx := context.NewCLIContext().WithCodec(cdc) + Args: cobra.MinimumNArgs(0), + RunE: func(_ *cobra.Command, args []string) error { + height := int64(0) + if len(args) > 0 { + var err error + height, err = strconv.ParseInt(args[0], 10, 64) + if err != nil { + return err + } + } + cliCtx := context.NewCLIContext().WithCodec(cdc).WithHeight(height) route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryBlockConfig) bz, _, err := cliCtx.QueryWithData(route, nil) diff --git a/x/params/client/rest/rest.go b/x/params/client/rest/rest.go index 6f1a334d26..af54f12697 100644 --- a/x/params/client/rest/rest.go +++ b/x/params/client/rest/rest.go @@ -2,14 +2,13 @@ package rest import ( "fmt" - "github.com/okex/exchain/x/params/types" - "net/http" - "github.com/gorilla/mux" "github.com/okex/exchain/libs/cosmos-sdk/client/context" "github.com/okex/exchain/libs/cosmos-sdk/types/rest" "github.com/okex/exchain/x/common" "github.com/okex/exchain/x/params" + "github.com/okex/exchain/x/params/types" + "net/http" ) // RegisterRoutes, a central function to define routes @@ -20,6 +19,11 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { func QueryBlockConfigFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + route := fmt.Sprintf("custom/%s/%s", params.RouterKey, types.QueryBlockConfig) bz, _, err := cliCtx.QueryWithData(route, nil) if err != nil { From faa0e4d1c06d700b5a439afdd3f9cfe40c0e1e0e Mon Sep 17 00:00:00 2001 From: lyh169 Date: Fri, 1 Sep 2023 16:24:56 +0800 Subject: [PATCH 3/3] add rest query the block config and add height --- x/params/client/cli/query.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/params/client/cli/query.go b/x/params/client/cli/query.go index 285e31eb65..5f048a4c55 100644 --- a/x/params/client/cli/query.go +++ b/x/params/client/cli/query.go @@ -42,6 +42,7 @@ $ exchaincli query params params Args: cobra.NoArgs, RunE: func(_ *cobra.Command, _ []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) + route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryParams) bz, _, err := cliCtx.QueryWithData(route, nil) if err != nil {