Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add channels rpc
Browse files Browse the repository at this point in the history
DimitrisJim committed Dec 18, 2024
1 parent 39e6b69 commit be9b002
Showing 6 changed files with 851 additions and 153 deletions.
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
@@ -20,6 +20,7 @@ func GetQueryCmd() *cobra.Command {

queryCmd.AddCommand(
getCmdQueryChannel(),
getCmdQueryChannels(),
getCmdQueryChannelClientState(),
getCmdQueryNextSequenceSend(),
getCmdQueryPacketCommitment(),
39 changes: 38 additions & 1 deletion modules/core/04-channel/v2/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -49,6 +49,44 @@ func getCmdQueryChannel() *cobra.Command {
return cmd
}

// getCmdQueryChannels defines the command to query all the v2 channels that this chain maintains.
func getCmdQueryChannels() *cobra.Command {
cmd := &cobra.Command{
Use: "channels",
Short: "Query all channels",
Long: "Query all channels from a chain",
Example: fmt.Sprintf("%s query %s %s channels", version.AppName, exported.ModuleName, types.SubModuleName),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

req := &types.QueryChannelsRequest{
Pagination: pageReq,
}

res, err := queryClient.Channels(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "channels")
return cmd
}

// getCmdQueryChannelClientState defines the command to query the channel client state for the given channel ID.
func getCmdQueryChannelClientState() *cobra.Command {
cmd := &cobra.Command{
@@ -76,7 +114,6 @@ func getCmdQueryChannelClientState() *cobra.Command {
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

36 changes: 36 additions & 0 deletions modules/core/04-channel/v2/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -52,6 +52,42 @@ func (q *queryServer) Channel(ctx context.Context, req *types.QueryChannelReques
return types.NewQueryChannelResponse(channel), nil
}

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

var channels []*types.IdentifiedChannel
store := prefix.NewStore(runtime.KVStoreAdapter(q.storeService.OpenKVStore(ctx)), []byte(types.ChannelPrefix))

pageRes, err := query.Paginate(store, req.Pagination, func(key, value []byte) error {
var channel types.Channel
if err := q.cdc.Unmarshal(value, &channel); err != nil {
return err
}

_, channelID, err := host.ParseChannelPath(string(key))
if err != nil {
return err
}

identifiedChannel := types.NewIdentifiedChannel(channelID, channel)
channels = append(channels, &identifiedChannel)
return nil
})
if err != nil {
return nil, err
}

selfHeight := clienttypes.GetSelfHeight(ctx)
return &types.QueryChannelsResponse{
Channels: channels,
Pagination: pageRes,
Height: selfHeight,
}, nil
}

// ChannelClientState implements the Query/ChannelClientState gRPC method
func (q *queryServer) ChannelClientState(ctx context.Context, req *types.QueryChannelClientStateRequest) (*types.QueryChannelClientStateResponse, error) {
if req == nil {
Loading

0 comments on commit be9b002

Please sign in to comment.