Skip to content

Commit

Permalink
x/ibc: gRPC query service
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze committed Jun 18, 2020
1 parent 257354d commit e82f809
Show file tree
Hide file tree
Showing 7 changed files with 1,439 additions and 136 deletions.
7 changes: 3 additions & 4 deletions x/bank/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions x/ibc/03-connection/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package keeper

import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/ibc/03-connection/types"
host "github.com/cosmos/cosmos-sdk/x/ibc/24-host"
)

var _ types.QueryServer = Keeper{}

// Connection implements the Query/Connection gRPC method
func (q Keeper) Connection(c context.Context, req *types.QueryConnectionRequest) (*types.QueryConnectionResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

if err := host.ConnectionIdentifierValidator(req.ConnectionID); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

ctx := sdk.UnwrapSDKContext(c)
connection, found := q.GetConnection(ctx, req.Address, req.ConnectionID)
if !found {
return nil, status.Error(
codes.NotFound,
sdkerrors.Wrap(types.ErrConnectionNotFound, req.ConnectionID).Error(),
)
}

return &types.QueryConnectionResponse{Connection: &balance}, nil
}

// ClientConnections implements the Query/ClientConnections gRPC method
func (q Keeper) ClientConnections(c context.Context, req *types.QueryClientConnectionsRequest) (*types.QueryClientConnectionsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

if err := host.ClientIdentifierValidator(req.ClientID); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

ctx := sdk.UnwrapSDKContext(c)
clientConnectionPaths, found := q.GetClientConnectionPaths(ctx, req.ClientID)
if !found {
return nil, status.Error(
codes.NotFound,
sdkerrors.Wrap(types.ErrClientConnectionPathsNotFound, req.ClientID).Error(),
)
}

return &types.QueryConnectionResponse{Connection: &balance}, nil
}

// // AllBalances implements the Query/AllBalances gRPC method
// func (q Keeper) AllBalances(c context.Context, req *types.QueryAllBalancesRequest) (*types.QueryAllBalancesResponse, error) {
// if req == nil {
// return nil, status.Errorf(codes.InvalidArgument, "empty request")
// }

// if len(req.Address) == 0 {
// return nil, status.Errorf(codes.InvalidArgument, "invalid address")
// }

// ctx := sdk.UnwrapSDKContext(c)
// balances := q.GetAllBalances(ctx, req.Address)

// return &types.QueryAllBalancesResponse{Balances: balances}, nil
// }

// // TotalSupply implements the Query/TotalSupply gRPC method
// func (q Keeper) TotalSupply(c context.Context, _ *types.QueryTotalSupplyRequest) (*types.QueryTotalSupplyResponse, error) {
// ctx := sdk.UnwrapSDKContext(c)
// totalSupply := q.GetSupply(ctx).GetTotal()

// return &types.QueryTotalSupplyResponse{Supply: totalSupply}, nil
// }

// // SupplyOf implements the Query/SupplyOf gRPC method
// func (q Keeper) SupplyOf(c context.Context, req *types.QuerySupplyOfRequest) (*types.QuerySupplyOfResponse, error) {
// if req == nil {
// return nil, status.Errorf(codes.InvalidArgument, "empty request")
// }

// if req.Denom == "" {
// return nil, status.Errorf(codes.InvalidArgument, "invalid denom")
// }

// ctx := sdk.UnwrapSDKContext(c)
// supply := q.GetSupply(ctx).GetTotal().AmountOf(req.Denom)

// return &types.QuerySupplyOfResponse{Amount: supply}, nil
// }
8 changes: 8 additions & 0 deletions x/ibc/03-connection/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package connection
import (
"github.com/gorilla/mux"
"github.com/spf13/cobra"
"google.golang.org/grpc"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/x/ibc/03-connection/client/cli"
"github.com/cosmos/cosmos-sdk/x/ibc/03-connection/client/rest"
"github.com/cosmos/cosmos-sdk/x/ibc/03-connection/keeper"
"github.com/cosmos/cosmos-sdk/x/ibc/03-connection/types"
)

// Name returns the IBC connection ICS name
Expand All @@ -28,3 +31,8 @@ func GetQueryCmd(clientCtx client.Context) *cobra.Command {
func RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Router) {
rest.RegisterRoutes(clientCtx, rtr)
}

// RegisterQueryService registers the gRPC query service for IBC connections.
func RegisterQueryService(server grpc.Server, k keeper.Keeper) {
types.RegisterQueryServer(server, k)
}
47 changes: 11 additions & 36 deletions x/ibc/03-connection/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,11 @@ const (
QueryAllClientConnections = "all_client_connections"
)

// ConnectionResponse defines the client query response for a connection which
// also includes a proof and the height from which the proof was retrieved.
type ConnectionResponse struct {
Connection ConnectionEnd `json:"connection" yaml:"connection"`
Proof commitmenttypes.MerkleProof `json:"proof,omitempty" yaml:"proof,omitempty"`
ProofPath commitmenttypes.MerklePath `json:"proof_path,omitempty" yaml:"proof_path,omitempty"`
ProofHeight uint64 `json:"proof_height,omitempty" yaml:"proof_height,omitempty"`
}

// NewConnectionResponse creates a new ConnectionResponse instance
func NewConnectionResponse(
// NewQueryConnectionResponse creates a new QueryConnectionResponse instance
func NewQueryConnectionResponse(
connectionID string, connection ConnectionEnd, proof *merkle.Proof, height int64,
) ConnectionResponse {
return ConnectionResponse{
) *QueryConnectionResponse {
return &QueryConnectionResponse{
Connection: connection,
Proof: commitmenttypes.MerkleProof{Proof: proof},
ProofPath: commitmenttypes.NewMerklePath(strings.Split(host.ConnectionPath(connectionID), "/")),
Expand All @@ -52,37 +43,21 @@ func NewQueryAllConnectionsParams(page, limit int) QueryAllConnectionsParams {
}
}

// ClientConnectionsResponse defines the client query response for a client
// connection paths which also includes a proof and the height from which the
// proof was retrieved.
type ClientConnectionsResponse struct {
ConnectionPaths []string `json:"connection_paths" yaml:"connection_paths"`
Proof commitmenttypes.MerkleProof `json:"proof,omitempty" yaml:"proof,omitempty"`
ProofPath commitmenttypes.MerklePath `json:"proof_path,omitempty" yaml:"proof_path,omitempty"`
ProofHeight uint64 `json:"proof_height,omitempty" yaml:"proof_height,omitempty"`
}

// NewClientConnectionsResponse creates a new ConnectionPaths instance
func NewClientConnectionsResponse(
// NewQueryClientConnectionsResponse creates a new ConnectionPaths instance
func NewQueryClientConnectionsResponse(
clientID string, connectionPaths []string, proof *merkle.Proof, height int64,
) ClientConnectionsResponse {
return ClientConnectionsResponse{
) *QueryClientConnectionsResponse {
return &QueryClientConnectionsResponse{
ConnectionPaths: connectionPaths,
Proof: commitmenttypes.MerkleProof{Proof: proof},
ProofPath: commitmenttypes.NewMerklePath(strings.Split(host.ClientConnectionsPath(clientID), "/")),
ProofHeight: uint64(height),
}
}

// QueryClientConnectionsParams defines the params for the following queries:
// - 'custom/ibc/client/<clientID>/connections'
type QueryClientConnectionsParams struct {
ClientID string
}

// NewQueryClientConnectionsParams creates a new QueryClientConnectionsParams instance
func NewQueryClientConnectionsParams(clientID string) QueryClientConnectionsParams {
return QueryClientConnectionsParams{
// NewQueryClientConnectionsRequest creates a new QueryClientConnectionsRequest instance
func NewQueryClientConnectionsRequest(clientID string) QueryClientConnectionsRequest {
return QueryClientConnectionsRequest{
ClientID: clientID,
}
}
Loading

0 comments on commit e82f809

Please sign in to comment.