From 447b0e5e246ecd0a97ec7dfc7ade46412175ea00 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Wed, 24 Jun 2020 17:20:20 +0200 Subject: [PATCH] update REST --- x/ibc/03-connection/client/cli/query.go | 41 +++++--------- x/ibc/03-connection/client/rest/query.go | 71 +++++++----------------- x/ibc/04-channel/client/cli/query.go | 6 +- x/ibc/04-channel/client/utils/utils.go | 7 ++- x/ibc/04-channel/keeper/grpc_query.go | 22 ++++++++ 5 files changed, 65 insertions(+), 82 deletions(-) diff --git a/x/ibc/03-connection/client/cli/query.go b/x/ibc/03-connection/client/cli/query.go index d30a045190ee..fe28f2c6e06a 100644 --- a/x/ibc/03-connection/client/cli/query.go +++ b/x/ibc/03-connection/client/cli/query.go @@ -3,7 +3,6 @@ package cli import ( "context" "fmt" - "strings" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -39,6 +38,7 @@ func GetCmdQueryConnections(clientCtx client.Context) *cobra.Command { return err } + // TODO: return res? return clientCtx.PrintOutput(res.Connections) }, } @@ -47,15 +47,10 @@ func GetCmdQueryConnections(clientCtx client.Context) *cobra.Command { // GetCmdQueryConnection defines the command to query a connection end func GetCmdQueryConnection(clientCtx client.Context) *cobra.Command { cmd := &cobra.Command{ - Use: "end [connection-id]", - Short: "Query stored connection end", - Long: strings.TrimSpace(fmt.Sprintf(`Query stored connection end - -Example: -$ %s query ibc connection end [connection-id] - `, version.ClientName), - ), - Example: fmt.Sprintf("%s query ibc connection end [connection-id]", version.ClientName), + Use: "end [connection-id]", + Short: "Query stored connection end", + Long: "Query stored connection end", + Example: fmt.Sprintf("%s query %s %s end [connection-id]", version.ClientName, host.ModuleName, types.SubModuleName), Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { clientCtx = clientCtx.Init() @@ -80,15 +75,10 @@ $ %s query ibc connection end [connection-id] // GetCmdQueryAllClientConnections defines the command to query a all the client connection paths. func GetCmdQueryAllClientConnections(clientCtx client.Context) *cobra.Command { cmd := &cobra.Command{ - Use: "paths", - Short: "Query all stored client connection paths", - Long: strings.TrimSpace(fmt.Sprintf(`Query all stored client connection paths - -Example: -$ %s query ibc connection paths - `, version.ClientName), - ), - Example: fmt.Sprintf("%s query ibc connection paths", version.ClientName), + Use: "paths", + Short: "Query all stored client connection paths", + Long: "Query all stored client connection paths", + Example: fmt.Sprintf("%s query %s %s paths", version.ClientName, host.ModuleName, types.SubModuleName), Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { clientCtx = clientCtx.Init() @@ -114,15 +104,10 @@ $ %s query ibc connection paths // GetCmdQueryClientConnections defines the command to query a client connections func GetCmdQueryClientConnections(clientCtx client.Context) *cobra.Command { return &cobra.Command{ - Use: "path [client-id]", - Short: "Query stored client connection paths", - Long: strings.TrimSpace(fmt.Sprintf(`Query stored client connection paths - -Example: -$ %s query ibc connection path [client-id] - `, version.ClientName), - ), - Example: fmt.Sprintf("%s query ibc connection path [client-id]", version.ClientName), + Use: "path [client-id]", + Short: "Query stored client connection paths", + Long: "Query stored client connection paths", + Example: fmt.Sprintf("%s query %s %s path [client-id]", version.ClientName, host.ModuleName, types.SubModuleName), Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { clientCtx = clientCtx.Init() diff --git a/x/ibc/03-connection/client/rest/query.go b/x/ibc/03-connection/client/rest/query.go index 20cdac2432ab..3133d3c81efe 100644 --- a/x/ibc/03-connection/client/rest/query.go +++ b/x/ibc/03-connection/client/rest/query.go @@ -1,6 +1,7 @@ package rest import ( + "context" "fmt" "net/http" @@ -8,8 +9,10 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/types/query" "github.com/cosmos/cosmos-sdk/types/rest" "github.com/cosmos/cosmos-sdk/x/ibc/03-connection/client/utils" + "github.com/cosmos/cosmos-sdk/x/ibc/03-connection/types" ) func registerQueryRoutes(clientCtx client.Context, r *mux.Router) { @@ -19,17 +22,6 @@ func registerQueryRoutes(clientCtx client.Context, r *mux.Router) { r.HandleFunc(fmt.Sprintf("/ibc/clients/{%s}/connections", RestClientID), queryClientConnectionsHandlerFn(clientCtx)).Methods("GET") } -// queryConnectionsHandlerFn implements connections querying route -// -// @Summary Query a client connection paths -// @Tags IBC -// @Produce json -// @Param page query int false "The page number to query" default(1) -// @Param limit query int false "The number of results per page" default(100) -// @Success 200 {object} QueryConnection "OK" -// @Failure 400 {object} rest.ErrorResponse "Bad Request" -// @Failure 500 {object} rest.ErrorResponse "Internal Server Error" -// @Router /ibc/connections [get] func queryClientsConnectionsHandlerFn(clientCtx client.Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { _, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0) @@ -42,28 +34,23 @@ func queryClientsConnectionsHandlerFn(clientCtx client.Context) http.HandlerFunc return } - connections, height, err := utils.QueryAllConnections(clientCtx, page, limit) + queryClient := types.NewQueryClient(clientCtx) + + req := &types.QueryConnectionsRequest{ + Req: &query.PageRequest{}, + } + + res, err := queryClient.Connections(context.Background(), req) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } - clientCtx = clientCtx.WithHeight(height) - rest.PostProcessResponse(w, clientCtx, connections) + clientCtx = clientCtx.WithHeight(res.Height) + rest.PostProcessResponse(w, clientCtx, res) } } -// queryConnectionHandlerFn implements a connection querying route -// -// @Summary Query connection -// @Tags IBC -// @Produce json -// @Param connection-id path string true "Client ID" -// @Param prove query boolean false "Proof of result" -// @Success 200 {object} QueryConnection "OK" -// @Failure 400 {object} rest.ErrorResponse "Invalid connection id" -// @Failure 500 {object} rest.ErrorResponse "Internal Server Error" -// @Router /ibc/connections/{connection-id} [get] func queryConnectionHandlerFn(clientCtx client.Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) @@ -86,17 +73,6 @@ func queryConnectionHandlerFn(clientCtx client.Context) http.HandlerFunc { } } -// queryConnectionsHandlerFn implements a client connections paths querying route -// -// @Summary Query all client connection paths -// @Tags IBC -// @Produce json -// @Param page query int false "The page number to query" default(1) -// @Param limit query int false "The number of results per page" default(100) -// @Success 200 {object} QueryClientsConnections "OK" -// @Failure 400 {object} rest.ErrorResponse "Bad Request" -// @Failure 500 {object} rest.ErrorResponse "Internal Server Error" -// @Router /ibc/clients/connections [get] func queryConnectionsHandlerFn(clientCtx client.Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { _, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0) @@ -109,28 +85,23 @@ func queryConnectionsHandlerFn(clientCtx client.Context) http.HandlerFunc { return } - connectionsPaths, height, err := utils.QueryAllClientConnectionPaths(clientCtx, page, limit) + queryClient := types.NewQueryClient(clientCtx) + + req := &types.QueryClientsConnectionsRequest{ + Req: &query.PageRequest{}, + } + + res, err := queryClient.ClientsConnections(context.Background(), req) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } - clientCtx = clientCtx.WithHeight(height) - rest.PostProcessResponse(w, clientCtx, connectionsPaths) + clientCtx = clientCtx.WithHeight(res.Height) + rest.PostProcessResponse(w, clientCtx, res) } } -// queryClientConnectionsHandlerFn implements a client connections querying route -// -// @Summary Query connections of a client -// @Tags IBC -// @Produce json -// @Param client-id path string true "Client ID" -// @Param prove query boolean false "Proof of result" -// @Success 200 {object} QueryClientConnections "OK" -// @Failure 400 {object} rest.ErrorResponse "Invalid client id" -// @Failure 500 {object} rest.ErrorResponse "Internal Server Error" -// @Router /ibc/clients/{client-id}/connections [get] func queryClientConnectionsHandlerFn(clientCtx client.Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) diff --git a/x/ibc/04-channel/client/cli/query.go b/x/ibc/04-channel/client/cli/query.go index 9ff99f1f4b23..b32c9a99df0b 100644 --- a/x/ibc/04-channel/client/cli/query.go +++ b/x/ibc/04-channel/client/cli/query.go @@ -75,11 +75,11 @@ func GetCmdQueryNextSequence(clientCtx client.Context) *cobra.Command { cmd := &cobra.Command{ Use: "next-sequence-receive [port-id] [channel-id]", Short: "Query a next receive sequence", - Long: "Query the next receive sequence for a given channel" + Long: "Query the next receive sequence for a given channel", Example: fmt.Sprintf( "%s query %s %s next-sequence-receive [port-id] [channel-id]", version.ClientName, host.ModuleName, types.SubModuleName, ), - Args: cobra.ExactArgs(2), + Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { clientCtx = clientCtx.Init() @@ -98,4 +98,4 @@ func GetCmdQueryNextSequence(clientCtx client.Context) *cobra.Command { } cmd.Flags().Bool(flags.FlagProve, true, "show proofs for the query results") return cmd -} \ No newline at end of file +} diff --git a/x/ibc/04-channel/client/utils/utils.go b/x/ibc/04-channel/client/utils/utils.go index 566134bae512..32eb0499c7e7 100644 --- a/x/ibc/04-channel/client/utils/utils.go +++ b/x/ibc/04-channel/client/utils/utils.go @@ -104,6 +104,11 @@ func QueryNextSequenceReceive( return nil, err } + proofBz, err := clientCtx.Codec.MarshalBinaryBare(res.Proof) + if err != nil { + return nil, err + } + sequence := binary.BigEndian.Uint64(res.Value) - return types.NewQueryNextSequenceReceiveResponse(portID, channelID, sequence, res.Proof, res.Height), nil + return types.NewQueryNextSequenceReceiveResponse(portID, channelID, sequence, proofBz, res.Height), nil } diff --git a/x/ibc/04-channel/keeper/grpc_query.go b/x/ibc/04-channel/keeper/grpc_query.go index 913a71f00be1..200200879a1b 100644 --- a/x/ibc/04-channel/keeper/grpc_query.go +++ b/x/ibc/04-channel/keeper/grpc_query.go @@ -220,6 +220,28 @@ func (q Keeper) UnrelayedPackets(c context.Context, req *types.QueryUnrelayedPac }, nil } +// NextSequenceReceive implements the Query/NextSequenceReceive gRPC method +func (q Keeper) NextSequenceReceive(c context.Context, req *types.QueryNextSequenceReceiveRequest) (*types.QueryNextSequenceReceiveResponse, error) { + if req == nil { + return nil, status.Errorf(codes.InvalidArgument, "empty request") + } + + if err := validategRPCRequest(req.PortID, req.ChannelID); err != nil { + return nil, err + } + + ctx := sdk.UnwrapSDKContext(c) + sequence, found := q.GetNextSequenceRecv(ctx, req.PortID, req.ChannelID) + if !found { + return nil, status.Error( + codes.NotFound, + sdkerrors.Wrapf(types.ErrSequenceReceiveNotFound, "port-id: , channel-id %s", req.PortID, req.ChannelID).Error(), + ) + } + + return types.NewQueryNextSequenceReceiveResponse(req.PortID, req.ChannelID, sequence, nil, ctx.BlockHeight()), nil +} + func validategRPCRequest(portID, channelID string) error { if err := host.PortIdentifierValidator(portID); err != nil { return status.Error(codes.InvalidArgument, err.Error())