Skip to content

Commit

Permalink
chore: add channel consensus state rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitrisJim committed Dec 6, 2024
1 parent 3864793 commit 5d98657
Show file tree
Hide file tree
Showing 6 changed files with 1,003 additions and 168 deletions.
32 changes: 32 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,38 @@ func (q *queryServer) Channel(ctx context.Context, req *types.QueryChannelReques
return types.NewQueryChannelResponse(channel), nil
}

// ChannelConsensusState implements the Query/ChannelConsensusState gRPC method
func (q *queryServer) ChannelConsensusState(ctx context.Context, req *types.QueryChannelConsensusStateRequest) (*types.QueryChannelConsensusStateResponse, 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())
}

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

consHeight := clienttypes.NewHeight(req.RevisionNumber, req.RevisionHeight)
consensusState, found := q.ClientKeeper.GetClientConsensusState(ctx, channel.ClientId, consHeight)
if !found {
return nil, status.Error(
codes.NotFound,
errorsmod.Wrapf(clienttypes.ErrConsensusStateNotFound, "client-id: %s", channel.ClientId).Error(),
)
}

anyConsensusState, err := clienttypes.PackConsensusState(consensusState)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return types.NewQueryChannelConsensusStateResponse(channel.ClientId, anyConsensusState, nil, clienttypes.GetSelfHeight(ctx)), 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
2 changes: 2 additions & 0 deletions modules/core/04-channel/v2/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ type ClientKeeper interface {
// GetClientTimestampAtHeight returns the timestamp for a given height on the client
// given its client ID and height
GetClientTimestampAtHeight(ctx context.Context, clientID string, height exported.Height) (uint64, error)
// GetClientConsensusState gets the stored consensus state from a client at a given height.
GetClientConsensusState(ctx context.Context, clientID string, height exported.Height) (exported.ConsensusState, bool)
}
16 changes: 15 additions & 1 deletion modules/core/04-channel/v2/types/query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package types

import clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"

clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
)

// NewQueryChannelRequest creates and returns a new channel query request.
func NewQueryChannelRequest(channelID string) *QueryChannelRequest {
Expand All @@ -16,6 +20,16 @@ func NewQueryChannelResponse(channel Channel) *QueryChannelResponse {
}
}

// NewQueryChannelConsensusStateResponse creates and returns a new ChannelConsensusState query response.
func NewQueryChannelConsensusStateResponse(clientID string, anyConsensusState *codectypes.Any, proof []byte, height clienttypes.Height) *QueryChannelConsensusStateResponse {
return &QueryChannelConsensusStateResponse{
ConsensusState: anyConsensusState,
ClientId: clientID,
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 5d98657

Please sign in to comment.