Skip to content

Commit

Permalink
chore: add channel client state rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitrisJim committed Dec 4, 2024
1 parent 3864793 commit 2b5f40b
Show file tree
Hide file tree
Showing 8 changed files with 849 additions and 132 deletions.
4 changes: 4 additions & 0 deletions modules/core/04-channel/v2/client/cli/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
ibcclient "github.com/cosmos/ibc-go/v9/modules/core/client"
)

func queryChannelClientStateABCI(clientCtx client.Context, channelID string) (*types.QueryChannelClientStateResponse, error) {
return &types.QueryChannelClientStateResponse{}, nil
}

func queryNextSequenceSendABCI(clientCtx client.Context, channelID string) (*types.QueryNextSequenceSendResponse, error) {
key := hostv2.NextSequenceSendKey(channelID)
value, proofBz, proofHeight, err := ibcclient.QueryTendermintProof(clientCtx, key)
Expand Down
1 change: 1 addition & 0 deletions modules/core/04-channel/v2/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func GetQueryCmd() *cobra.Command {

queryCmd.AddCommand(
getCmdQueryChannel(),
getCmdQueryChannelClientState(),
getCmdQueryNextSequenceSend(),
getCmdQueryPacketCommitment(),
getCmdQueryPacketCommitments(),
Expand Down
45 changes: 45 additions & 0 deletions modules/core/04-channel/v2/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,51 @@ func getCmdQueryChannel() *cobra.Command {
return cmd
}

// getCmdQueryChannelClientState defines the command to query the channel client state for the given channel ID.
func getCmdQueryChannelClientState() *cobra.Command {
cmd := &cobra.Command{
Use: "client-state [channel-id]",
Short: "Query the client state associated with a channel.",
Long: "Query the client state associated with a channel for the provided channel ID.",
Example: fmt.Sprintf("%s query %s %s client-state [channel-id]", version.AppName, exported.ModuleName, types.SubModuleName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

channelID := args[0]
prove, err := cmd.Flags().GetBool(flags.FlagProve)
if err != nil {
return err
}

if prove {
res, err := queryChannelClientStateABCI(clientCtx, channelID)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
}

queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.ChannelClientState(cmd.Context(), types.NewQueryChannelClientStateRequest(channelID))
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

cmd.Flags().Bool(flags.FlagProve, true, "show proofs for the query results")
flags.AddQueryFlagsToCmd(cmd)

return cmd
}

// getCmdQueryNextSequenceSend defines the command to query a next send sequence for a given channel
func getCmdQueryNextSequenceSend() *cobra.Command {
cmd := &cobra.Command{
Expand Down
18 changes: 18 additions & 0 deletions modules/core/04-channel/v2/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ func (q *queryServer) Channel(ctx context.Context, req *types.QueryChannelReques
return types.NewQueryChannelResponse(channel), nil
}

// ChannelClientState implements the Query/ChannelClientState gRPC method
func (q *queryServer) ChannelClientState(ctx context.Context, req *types.QueryChannelClientStateRequest) (*types.QueryChannelClientStateResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

_, found := q.GetChannel(ctx, req.ChannelId)
if !found {
return nil, status.Error(codes.NotFound, errorsmod.Wrapf(types.ErrChannelNotFound, "channel-id: %s", req.ChannelId).Error())
}

return nil, nil
}

// NextSequenceSend implements the Query/NextSequenceSend gRPC method
func (q *queryServer) NextSequenceSend(ctx context.Context, req *types.QueryNextSequenceSendRequest) (*types.QueryNextSequenceSendResponse, error) {
if req == nil {
Expand Down
16 changes: 16 additions & 0 deletions modules/core/04-channel/v2/types/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ func NewQueryChannelResponse(channel Channel) *QueryChannelResponse {
}
}

// NewQueryChannelClientStateRequest creates and returns a new ChannelClientState query request.
func NewQueryChannelClientStateRequest(channelID string) *QueryChannelClientStateRequest {
return &QueryChannelClientStateRequest{
ChannelId: channelID,
}
}

// NewQueryChannelClientStateResponse creates and returns a new ChannelClientState query response.
func NewQueryChannelClientStateResponse(identifiedClientState clienttypes.IdentifiedClientState, proof []byte, height clienttypes.Height) *QueryChannelClientStateResponse {
return &QueryChannelClientStateResponse{
IdentifiedClientState: &identifiedClientState,
Proof: proof,
ProofHeight: height,
}
}

// NewQueryNextSequenceSendRequest creates a new next sequence send query.
func NewQueryNextSequenceSendRequest(channelID string) *QueryNextSequenceSendRequest {
return &QueryNextSequenceSendRequest{
Expand Down
Loading

0 comments on commit 2b5f40b

Please sign in to comment.