From 75f7379a044e4d4223446d930141986b76a89440 Mon Sep 17 00:00:00 2001 From: DimitrisJim Date: Wed, 11 Sep 2024 00:34:55 +0300 Subject: [PATCH 1/2] chore(packet-server): add queryServer to packet-server --- modules/core/module.go | 6 + .../core/packet-server/keeper/grpc_query.go | 49 ++ .../packet-server/keeper/grpc_query_test.go | 109 +++ .../packet-server/types/expected_keepers.go | 3 + modules/core/packet-server/types/query.pb.go | 643 ++++++++++++++++++ .../core/packet-server/types/query.pb.gw.go | 189 +++++ proto/ibc/core/packetserver/v1/query.proto | 28 + 7 files changed, 1027 insertions(+) create mode 100644 modules/core/packet-server/keeper/grpc_query.go create mode 100644 modules/core/packet-server/keeper/grpc_query_test.go create mode 100644 modules/core/packet-server/types/query.pb.go create mode 100644 modules/core/packet-server/types/query.pb.gw.go create mode 100644 proto/ibc/core/packetserver/v1/query.proto diff --git a/modules/core/module.go b/modules/core/module.go index f81c9f54bda..e0b7baf4a67 100644 --- a/modules/core/module.go +++ b/modules/core/module.go @@ -27,6 +27,7 @@ import ( "github.com/cosmos/ibc-go/v9/modules/core/client/cli" "github.com/cosmos/ibc-go/v9/modules/core/exported" "github.com/cosmos/ibc-go/v9/modules/core/keeper" + packetserverkeeper "github.com/cosmos/ibc-go/v9/modules/core/packet-server/keeper" packetservertypes "github.com/cosmos/ibc-go/v9/modules/core/packet-server/types" "github.com/cosmos/ibc-go/v9/modules/core/simulation" "github.com/cosmos/ibc-go/v9/modules/core/types" @@ -92,6 +93,10 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *r if err != nil { panic(err) } + err = packetservertypes.RegisterQueryHandlerClient(context.Background(), mux, packetservertypes.NewQueryClient(clientCtx)) + if err != nil { + panic(err) + } } // GetTxCmd returns the root tx command for the ibc module. @@ -138,6 +143,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { clienttypes.RegisterQueryServer(cfg.QueryServer(), clientkeeper.NewQueryServer(am.keeper.ClientKeeper)) connectiontypes.RegisterQueryServer(cfg.QueryServer(), connectionkeeper.NewQueryServer(am.keeper.ConnectionKeeper)) channeltypes.RegisterQueryServer(cfg.QueryServer(), channelkeeper.NewQueryServer(am.keeper.ChannelKeeper)) + packetservertypes.RegisterQueryServer(cfg.QueryServer(), packetserverkeeper.NewQueryServer(am.keeper.PacketServerKeeper)) clientMigrator := clientkeeper.NewMigrator(am.keeper.ClientKeeper) if err := cfg.RegisterMigration(exported.ModuleName, 2, clientMigrator.Migrate2to3); err != nil { diff --git a/modules/core/packet-server/keeper/grpc_query.go b/modules/core/packet-server/keeper/grpc_query.go new file mode 100644 index 00000000000..fe74ee55f64 --- /dev/null +++ b/modules/core/packet-server/keeper/grpc_query.go @@ -0,0 +1,49 @@ +package keeper + +import ( + "context" + + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + + sdk "github.com/cosmos/cosmos-sdk/types" + + host "github.com/cosmos/ibc-go/v9/modules/core/24-host" + "github.com/cosmos/ibc-go/v9/modules/core/packet-server/types" +) + +var _ types.QueryServer = (*queryServer)(nil) + +// queryServer implements the packet-server types.QueryServer interface. +type queryServer struct { + *Keeper +} + +// NewQueryServer returns a new types.QueryServer implementation. +func NewQueryServer(k *Keeper) types.QueryServer { + return &queryServer{ + Keeper: k, + } +} + +// Client implements the Query/Client gRPC method +func (q *queryServer) Client(ctx context.Context, req *types.QueryClientRequest) (*types.QueryClientResponse, error) { + if req == nil { + return nil, status.Error(codes.InvalidArgument, "empty request") + } + + if err := host.ClientIdentifierValidator(req.ClientId); err != nil { + return nil, status.Error(codes.InvalidArgument, err.Error()) + } + + res := types.QueryClientResponse{} + + sdkCtx := sdk.UnwrapSDKContext(ctx) + creator, _ := q.ClientKeeper.GetCreator(sdkCtx, req.ClientId) + res.Creator = creator + + counterparty, _ := q.GetCounterparty(sdkCtx, req.ClientId) + res.Counterparty = counterparty + + return &res, nil +} diff --git a/modules/core/packet-server/keeper/grpc_query_test.go b/modules/core/packet-server/keeper/grpc_query_test.go new file mode 100644 index 00000000000..1fba64e7e96 --- /dev/null +++ b/modules/core/packet-server/keeper/grpc_query_test.go @@ -0,0 +1,109 @@ +package keeper_test + +import ( + "fmt" + + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + + commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types" + "github.com/cosmos/ibc-go/v9/modules/core/packet-server/keeper" + "github.com/cosmos/ibc-go/v9/modules/core/packet-server/types" + ibctesting "github.com/cosmos/ibc-go/v9/testing" +) + +func (suite *KeeperTestSuite) TestQueryClient() { + var ( + req *types.QueryClientRequest + expCreator string + expCounterparty types.Counterparty + ) + + testCases := []struct { + msg string + malleate func() + expError error + }{ + { + "success", + func() { + ctx := suite.chainA.GetContext() + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetCreator(ctx, ibctesting.FirstClientID, expCreator) + suite.chainA.App.GetIBCKeeper().PacketServerKeeper.SetCounterparty(ctx, ibctesting.FirstClientID, expCounterparty) + + req = &types.QueryClientRequest{ + ClientId: ibctesting.FirstClientID, + } + }, + nil, + }, + { + "success: no creator", + func() { + expCreator = "" + + suite.chainA.App.GetIBCKeeper().PacketServerKeeper.SetCounterparty(suite.chainA.GetContext(), ibctesting.FirstClientID, expCounterparty) + + req = &types.QueryClientRequest{ + ClientId: ibctesting.FirstClientID, + } + }, + nil, + }, + { + "success: no counterparty", + func() { + expCounterparty = types.Counterparty{} + + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetCreator(suite.chainA.GetContext(), ibctesting.FirstClientID, expCreator) + + req = &types.QueryClientRequest{ + ClientId: ibctesting.FirstClientID, + } + }, + nil, + }, + { + "req is nil", + func() { + req = nil + }, + status.Error(codes.InvalidArgument, "empty request"), + }, + { + "invalid clientID", + func() { + req = &types.QueryClientRequest{} + }, + status.Error(codes.InvalidArgument, "identifier cannot be blank: invalid identifier"), + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { + suite.SetupTest() // reset + + expCreator = ibctesting.TestAccAddress + merklePathPrefix := commitmenttypes.NewMerklePath([]byte("prefix")) + expCounterparty = types.Counterparty{ClientId: ibctesting.SecondClientID, MerklePathPrefix: merklePathPrefix} + + tc.malleate() + + queryServer := keeper.NewQueryServer(suite.chainA.GetSimApp().IBCKeeper.PacketServerKeeper) + res, err := queryServer.Client(suite.chainA.GetContext(), req) + + expPass := tc.expError == nil + if expPass { + suite.Require().NoError(err) + suite.Require().NotNil(res) + suite.Require().Equal(expCreator, res.Creator) + suite.Require().Equal(expCounterparty, res.Counterparty) + } else { + suite.Require().ErrorIs(err, tc.expError) + suite.Require().Nil(res) + } + }) + } +} diff --git a/modules/core/packet-server/types/expected_keepers.go b/modules/core/packet-server/types/expected_keepers.go index f74b6df031e..7134ab5495f 100644 --- a/modules/core/packet-server/types/expected_keepers.go +++ b/modules/core/packet-server/types/expected_keepers.go @@ -52,4 +52,7 @@ type ClientKeeper interface { // GetClientTimestampAtHeight returns the timestamp for a given height on the client // given its client ID and height GetClientTimestampAtHeight(ctx sdk.Context, clientID string, height exported.Height) (uint64, error) + + // GetCreator returns the creator of the client denoted by the clientID. + GetCreator(ctx sdk.Context, clientID string) (string, bool) } diff --git a/modules/core/packet-server/types/query.pb.go b/modules/core/packet-server/types/query.pb.go new file mode 100644 index 00000000000..02d3e24447a --- /dev/null +++ b/modules/core/packet-server/types/query.pb.go @@ -0,0 +1,643 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: ibc/core/packetserver/v1/query.proto + +package types + +import ( + context "context" + fmt "fmt" + _ "github.com/cosmos/gogoproto/gogoproto" + grpc1 "github.com/cosmos/gogoproto/grpc" + proto "github.com/cosmos/gogoproto/proto" + _ "google.golang.org/genproto/googleapis/api/annotations" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// QueryClientRequest is the request type for the Query/Client RPC method +type QueryClientRequest struct { + ClientId string `protobuf:"bytes,1,opt,name=client_id,json=clientId,proto3" json:"client_id,omitempty"` +} + +func (m *QueryClientRequest) Reset() { *m = QueryClientRequest{} } +func (m *QueryClientRequest) String() string { return proto.CompactTextString(m) } +func (*QueryClientRequest) ProtoMessage() {} +func (*QueryClientRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_7ac0ae50eee8e6db, []int{0} +} +func (m *QueryClientRequest) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *QueryClientRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_QueryClientRequest.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *QueryClientRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_QueryClientRequest.Merge(m, src) +} +func (m *QueryClientRequest) XXX_Size() int { + return m.Size() +} +func (m *QueryClientRequest) XXX_DiscardUnknown() { + xxx_messageInfo_QueryClientRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_QueryClientRequest proto.InternalMessageInfo + +func (m *QueryClientRequest) GetClientId() string { + if m != nil { + return m.ClientId + } + return "" +} + +// QueryClientRequest is the response type for the Query/Client RPC method +type QueryClientResponse struct { + Creator string `protobuf:"bytes,1,opt,name=creator,proto3" json:"creator,omitempty"` + Counterparty Counterparty `protobuf:"bytes,2,opt,name=counterparty,proto3" json:"counterparty"` +} + +func (m *QueryClientResponse) Reset() { *m = QueryClientResponse{} } +func (m *QueryClientResponse) String() string { return proto.CompactTextString(m) } +func (*QueryClientResponse) ProtoMessage() {} +func (*QueryClientResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_7ac0ae50eee8e6db, []int{1} +} +func (m *QueryClientResponse) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *QueryClientResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_QueryClientResponse.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *QueryClientResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_QueryClientResponse.Merge(m, src) +} +func (m *QueryClientResponse) XXX_Size() int { + return m.Size() +} +func (m *QueryClientResponse) XXX_DiscardUnknown() { + xxx_messageInfo_QueryClientResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_QueryClientResponse proto.InternalMessageInfo + +func (m *QueryClientResponse) GetCreator() string { + if m != nil { + return m.Creator + } + return "" +} + +func (m *QueryClientResponse) GetCounterparty() Counterparty { + if m != nil { + return m.Counterparty + } + return Counterparty{} +} + +func init() { + proto.RegisterType((*QueryClientRequest)(nil), "ibc.core.packetserver.v1.QueryClientRequest") + proto.RegisterType((*QueryClientResponse)(nil), "ibc.core.packetserver.v1.QueryClientResponse") +} + +func init() { + proto.RegisterFile("ibc/core/packetserver/v1/query.proto", fileDescriptor_7ac0ae50eee8e6db) +} + +var fileDescriptor_7ac0ae50eee8e6db = []byte{ + // 362 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x51, 0x3d, 0x4b, 0x2b, 0x41, + 0x14, 0xdd, 0x09, 0xef, 0xe5, 0xbd, 0xcc, 0x7b, 0xd5, 0x68, 0xb1, 0x44, 0x59, 0x43, 0x10, 0x8d, + 0x68, 0x76, 0x48, 0xac, 0x04, 0xab, 0xa4, 0xb2, 0xd3, 0x14, 0x16, 0x36, 0xb2, 0x3b, 0xb9, 0xac, + 0x8b, 0xc9, 0xde, 0xcd, 0xcc, 0xec, 0x42, 0x10, 0x0b, 0xfd, 0x05, 0x82, 0x60, 0xed, 0xcf, 0x49, + 0x19, 0xb0, 0xb1, 0x12, 0x49, 0xfc, 0x21, 0xb2, 0x1f, 0x86, 0x0d, 0x12, 0xb0, 0xbb, 0x73, 0xe7, + 0x9c, 0xc3, 0x39, 0xf7, 0xd0, 0x6d, 0xdf, 0x15, 0x5c, 0xa0, 0x04, 0x1e, 0x3a, 0xe2, 0x1a, 0xb4, + 0x02, 0x19, 0x83, 0xe4, 0x71, 0x8b, 0x8f, 0x22, 0x90, 0x63, 0x3b, 0x94, 0xa8, 0x91, 0x99, 0xbe, + 0x2b, 0xec, 0x04, 0x65, 0x17, 0x51, 0x76, 0xdc, 0xaa, 0xee, 0xaf, 0xe4, 0x0b, 0x8c, 0x02, 0x0d, + 0x32, 0x74, 0xa4, 0xce, 0x65, 0xaa, 0x9b, 0x1e, 0xa2, 0x37, 0x00, 0xee, 0x84, 0x3e, 0x77, 0x82, + 0x00, 0xb5, 0xa3, 0x7d, 0x0c, 0x54, 0xfe, 0xbb, 0xee, 0xa1, 0x87, 0xe9, 0xc8, 0x93, 0x29, 0xdb, + 0xd6, 0x5b, 0x94, 0x9d, 0x25, 0x4e, 0xba, 0x03, 0x1f, 0x02, 0xdd, 0x83, 0x51, 0x04, 0x4a, 0xb3, + 0x0d, 0x5a, 0x11, 0xe9, 0xe2, 0xd2, 0xef, 0x9b, 0xa4, 0x46, 0x1a, 0x95, 0xde, 0xdf, 0x6c, 0x71, + 0xd2, 0xaf, 0xdf, 0x11, 0xba, 0xb6, 0xc4, 0x51, 0x21, 0x06, 0x0a, 0x98, 0x49, 0xff, 0x08, 0x09, + 0x8e, 0x46, 0x99, 0x53, 0xbe, 0x9e, 0xec, 0x94, 0xfe, 0x2f, 0xda, 0x35, 0x4b, 0x35, 0xd2, 0xf8, + 0xd7, 0xde, 0xb1, 0x57, 0xc5, 0xb6, 0xbb, 0x05, 0x74, 0xe7, 0xd7, 0xe4, 0x6d, 0xcb, 0xe8, 0x2d, + 0x29, 0xb4, 0x9f, 0x09, 0xfd, 0x9d, 0x7a, 0x60, 0x4f, 0x84, 0x96, 0x33, 0x23, 0xec, 0x60, 0xb5, + 0xe0, 0xf7, 0x8c, 0xd5, 0xe6, 0x0f, 0xd1, 0x59, 0xba, 0x3a, 0xbf, 0x7f, 0xf9, 0x78, 0x2c, 0xed, + 0xb1, 0x5d, 0xbe, 0xa8, 0x24, 0xbb, 0x48, 0x5a, 0x46, 0x3a, 0x29, 0x7e, 0xb3, 0xb8, 0xda, 0x6d, + 0xe7, 0x7c, 0x32, 0xb3, 0xc8, 0x74, 0x66, 0x91, 0xf7, 0x99, 0x45, 0x1e, 0xe6, 0x96, 0x31, 0x9d, + 0x5b, 0xc6, 0xeb, 0xdc, 0x32, 0x2e, 0x8e, 0x3d, 0x5f, 0x5f, 0x45, 0xae, 0x2d, 0x70, 0xc8, 0x05, + 0xaa, 0x21, 0xaa, 0x44, 0xb3, 0xe9, 0x21, 0x8f, 0x8f, 0xf8, 0x10, 0xfb, 0xd1, 0x00, 0x54, 0xb1, + 0xf4, 0x66, 0xde, 0xba, 0x1e, 0x87, 0xa0, 0xdc, 0x72, 0x5a, 0xdc, 0xe1, 0x67, 0x00, 0x00, 0x00, + 0xff, 0xff, 0x90, 0xb9, 0x8b, 0xca, 0x5b, 0x02, 0x00, 0x00, +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConn + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion4 + +// QueryClient is the client API for Query service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type QueryClient interface { + // Client queries the counterparty of an IBC client. + Client(ctx context.Context, in *QueryClientRequest, opts ...grpc.CallOption) (*QueryClientResponse, error) +} + +type queryClient struct { + cc grpc1.ClientConn +} + +func NewQueryClient(cc grpc1.ClientConn) QueryClient { + return &queryClient{cc} +} + +func (c *queryClient) Client(ctx context.Context, in *QueryClientRequest, opts ...grpc.CallOption) (*QueryClientResponse, error) { + out := new(QueryClientResponse) + err := c.cc.Invoke(ctx, "/ibc.core.packetserver.v1.Query/Client", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// QueryServer is the server API for Query service. +type QueryServer interface { + // Client queries the counterparty of an IBC client. + Client(context.Context, *QueryClientRequest) (*QueryClientResponse, error) +} + +// UnimplementedQueryServer can be embedded to have forward compatible implementations. +type UnimplementedQueryServer struct { +} + +func (*UnimplementedQueryServer) Client(ctx context.Context, req *QueryClientRequest) (*QueryClientResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Client not implemented") +} + +func RegisterQueryServer(s grpc1.Server, srv QueryServer) { + s.RegisterService(&_Query_serviceDesc, srv) +} + +func _Query_Client_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(QueryClientRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(QueryServer).Client(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/ibc.core.packetserver.v1.Query/Client", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(QueryServer).Client(ctx, req.(*QueryClientRequest)) + } + return interceptor(ctx, in, info, handler) +} + +var _Query_serviceDesc = grpc.ServiceDesc{ + ServiceName: "ibc.core.packetserver.v1.Query", + HandlerType: (*QueryServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Client", + Handler: _Query_Client_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "ibc/core/packetserver/v1/query.proto", +} + +func (m *QueryClientRequest) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *QueryClientRequest) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *QueryClientRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.ClientId) > 0 { + i -= len(m.ClientId) + copy(dAtA[i:], m.ClientId) + i = encodeVarintQuery(dAtA, i, uint64(len(m.ClientId))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *QueryClientResponse) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *QueryClientResponse) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *QueryClientResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + { + size, err := m.Counterparty.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintQuery(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + if len(m.Creator) > 0 { + i -= len(m.Creator) + copy(dAtA[i:], m.Creator) + i = encodeVarintQuery(dAtA, i, uint64(len(m.Creator))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintQuery(dAtA []byte, offset int, v uint64) int { + offset -= sovQuery(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *QueryClientRequest) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.ClientId) + if l > 0 { + n += 1 + l + sovQuery(uint64(l)) + } + return n +} + +func (m *QueryClientResponse) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Creator) + if l > 0 { + n += 1 + l + sovQuery(uint64(l)) + } + l = m.Counterparty.Size() + n += 1 + l + sovQuery(uint64(l)) + return n +} + +func sovQuery(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozQuery(x uint64) (n int) { + return sovQuery(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *QueryClientRequest) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: QueryClientRequest: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: QueryClientRequest: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ClientId", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthQuery + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.ClientId = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipQuery(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthQuery + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *QueryClientResponse) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: QueryClientResponse: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: QueryClientResponse: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Creator", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthQuery + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Creator = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Counterparty", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthQuery + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.Counterparty.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipQuery(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthQuery + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipQuery(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowQuery + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowQuery + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowQuery + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthQuery + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupQuery + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthQuery + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthQuery = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowQuery = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupQuery = fmt.Errorf("proto: unexpected end of group") +) diff --git a/modules/core/packet-server/types/query.pb.gw.go b/modules/core/packet-server/types/query.pb.gw.go new file mode 100644 index 00000000000..dbe2224a7b8 --- /dev/null +++ b/modules/core/packet-server/types/query.pb.gw.go @@ -0,0 +1,189 @@ +// Code generated by protoc-gen-grpc-gateway. DO NOT EDIT. +// source: ibc/core/packetserver/v1/query.proto + +/* +Package types is a reverse proxy. + +It translates gRPC into RESTful JSON APIs. +*/ +package types + +import ( + "context" + "io" + "net/http" + + "github.com/golang/protobuf/descriptor" + "github.com/golang/protobuf/proto" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/grpc-ecosystem/grpc-gateway/utilities" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/grpclog" + "google.golang.org/grpc/metadata" + "google.golang.org/grpc/status" +) + +// Suppress "imported and not used" errors +var _ codes.Code +var _ io.Reader +var _ status.Status +var _ = runtime.String +var _ = utilities.NewDoubleArray +var _ = descriptor.ForMessage +var _ = metadata.Join + +func request_Query_Client_0(ctx context.Context, marshaler runtime.Marshaler, client QueryClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq QueryClientRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["client_id"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "client_id") + } + + protoReq.ClientId, err = runtime.String(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "client_id", err) + } + + msg, err := client.Client(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_Query_Client_0(ctx context.Context, marshaler runtime.Marshaler, server QueryServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq QueryClientRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["client_id"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "client_id") + } + + protoReq.ClientId, err = runtime.String(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "client_id", err) + } + + msg, err := server.Client(ctx, &protoReq) + return msg, metadata, err + +} + +// RegisterQueryHandlerServer registers the http handlers for service Query to "mux". +// UnaryRPC :call QueryServer directly. +// StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906. +// Note that using this registration option will cause many gRPC library features to stop working. Consider using RegisterQueryHandlerFromEndpoint instead. +func RegisterQueryHandlerServer(ctx context.Context, mux *runtime.ServeMux, server QueryServer) error { + + mux.Handle("GET", pattern_Query_Client_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + var stream runtime.ServerTransportStream + ctx = grpc.NewContextWithServerTransportStream(ctx, &stream) + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_Query_Client_0(rctx, inboundMarshaler, server, req, pathParams) + md.HeaderMD, md.TrailerMD = metadata.Join(md.HeaderMD, stream.Header()), metadata.Join(md.TrailerMD, stream.Trailer()) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Query_Client_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +// RegisterQueryHandlerFromEndpoint is same as RegisterQueryHandler but +// automatically dials to "endpoint" and closes the connection when "ctx" gets done. +func RegisterQueryHandlerFromEndpoint(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error) { + conn, err := grpc.Dial(endpoint, opts...) + if err != nil { + return err + } + defer func() { + if err != nil { + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + return + } + go func() { + <-ctx.Done() + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + }() + }() + + return RegisterQueryHandler(ctx, mux, conn) +} + +// RegisterQueryHandler registers the http handlers for service Query to "mux". +// The handlers forward requests to the grpc endpoint over "conn". +func RegisterQueryHandler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error { + return RegisterQueryHandlerClient(ctx, mux, NewQueryClient(conn)) +} + +// RegisterQueryHandlerClient registers the http handlers for service Query +// to "mux". The handlers forward requests to the grpc endpoint over the given implementation of "QueryClient". +// Note: the gRPC framework executes interceptors within the gRPC handler. If the passed in "QueryClient" +// doesn't go through the normal gRPC flow (creating a gRPC client etc.) then it will be up to the passed in +// "QueryClient" to call the correct interceptors. +func RegisterQueryHandlerClient(ctx context.Context, mux *runtime.ServeMux, client QueryClient) error { + + mux.Handle("GET", pattern_Query_Client_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Query_Client_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Query_Client_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +var ( + pattern_Query_Client_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3, 2, 4, 1, 0, 4, 1, 5, 5}, []string{"ibc", "core", "client", "v1", "clients", "client_id"}, "", runtime.AssumeColonVerbOpt(false))) +) + +var ( + forward_Query_Client_0 = runtime.ForwardResponseMessage +) diff --git a/proto/ibc/core/packetserver/v1/query.proto b/proto/ibc/core/packetserver/v1/query.proto new file mode 100644 index 00000000000..f056da89c32 --- /dev/null +++ b/proto/ibc/core/packetserver/v1/query.proto @@ -0,0 +1,28 @@ +syntax = "proto3"; + +package ibc.core.packetserver.v1; + +option go_package = "github.com/cosmos/ibc-go/v9/modules/core/packet-server/types"; + +import "ibc/core/packetserver/v1/counterparty.proto"; +import "google/api/annotations.proto"; +import "gogoproto/gogo.proto"; + +// Query provides defines the gRPC querier service +service Query { + // Client queries the counterparty of an IBC client. + rpc Client(QueryClientRequest) returns (QueryClientResponse) { + option (google.api.http).get = "/ibc/core/client/v1/clients/{client_id}"; + } +} + +// QueryClientRequest is the request type for the Query/Client RPC method +message QueryClientRequest { + string client_id = 1; +} + +// QueryClientRequest is the response type for the Query/Client RPC method +message QueryClientResponse { + string creator = 1; + Counterparty counterparty = 2 [(gogoproto.nullable) = false]; +} From 4eb7e580949b024cc3d4722fa0f7d09e1c96ebf7 Mon Sep 17 00:00:00 2001 From: DimitrisJim Date: Thu, 12 Sep 2024 11:14:07 +0300 Subject: [PATCH 2/2] chore(packet-server): return error if non of creator/counterparty is stored. --- modules/core/packet-server/keeper/grpc_query.go | 16 +++++++++++++--- .../core/packet-server/keeper/grpc_query_test.go | 9 +++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/modules/core/packet-server/keeper/grpc_query.go b/modules/core/packet-server/keeper/grpc_query.go index fe74ee55f64..2632ea1dc4b 100644 --- a/modules/core/packet-server/keeper/grpc_query.go +++ b/modules/core/packet-server/keeper/grpc_query.go @@ -6,6 +6,8 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + errorsmod "cosmossdk.io/errors" + sdk "github.com/cosmos/cosmos-sdk/types" host "github.com/cosmos/ibc-go/v9/modules/core/24-host" @@ -39,11 +41,19 @@ func (q *queryServer) Client(ctx context.Context, req *types.QueryClientRequest) res := types.QueryClientResponse{} sdkCtx := sdk.UnwrapSDKContext(ctx) - creator, _ := q.ClientKeeper.GetCreator(sdkCtx, req.ClientId) - res.Creator = creator - counterparty, _ := q.GetCounterparty(sdkCtx, req.ClientId) + creator, foundCreator := q.ClientKeeper.GetCreator(sdkCtx, req.ClientId) + counterparty, foundCounterparty := q.GetCounterparty(sdkCtx, req.ClientId) + + if !foundCreator && !foundCounterparty { + return nil, status.Error( + codes.NotFound, + errorsmod.Wrapf(types.ErrCounterpartyNotFound, "client-id: %s", req.ClientId).Error(), + ) + } + res.Counterparty = counterparty + res.Creator = creator return &res, nil } diff --git a/modules/core/packet-server/keeper/grpc_query_test.go b/modules/core/packet-server/keeper/grpc_query_test.go index 1fba64e7e96..014efa8cf59 100644 --- a/modules/core/packet-server/keeper/grpc_query_test.go +++ b/modules/core/packet-server/keeper/grpc_query_test.go @@ -70,6 +70,15 @@ func (suite *KeeperTestSuite) TestQueryClient() { }, status.Error(codes.InvalidArgument, "empty request"), }, + { + "no creator and no counterparty", + func() { + req = &types.QueryClientRequest{ + ClientId: ibctesting.FirstClientID, + } + }, + status.Error(codes.NotFound, fmt.Sprintf("client-id: %s: counterparty not found", ibctesting.FirstClientID)), + }, { "invalid clientID", func() {