Skip to content

Commit

Permalink
update REST
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze committed Jun 24, 2020
1 parent 759ae9c commit 447b0e5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 82 deletions.
41 changes: 13 additions & 28 deletions x/ibc/03-connection/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -39,6 +38,7 @@ func GetCmdQueryConnections(clientCtx client.Context) *cobra.Command {
return err
}

// TODO: return res?
return clientCtx.PrintOutput(res.Connections)
},
}
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
71 changes: 21 additions & 50 deletions x/ibc/03-connection/client/rest/query.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package rest

import (
"context"
"fmt"
"net/http"

"github.com/gorilla/mux"

"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) {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions x/ibc/04-channel/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
}
}
7 changes: 6 additions & 1 deletion x/ibc/04-channel/client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 22 additions & 0 deletions x/ibc/04-channel/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 447b0e5

Please sign in to comment.