diff --git a/baseapp/grpcrouter.go b/baseapp/grpcrouter.go index 632e34915..45021a56e 100644 --- a/baseapp/grpcrouter.go +++ b/baseapp/grpcrouter.go @@ -9,6 +9,7 @@ import ( "google.golang.org/grpc/encoding" "google.golang.org/grpc/encoding/proto" + "github.com/cosmos/cosmos-sdk/client/grpc/reflection" "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -17,9 +18,9 @@ var protoCodec = encoding.GetCodec(proto.Name) // GRPCQueryRouter routes ABCI Query requests to GRPC handlers type GRPCQueryRouter struct { - routes map[string]GRPCQueryHandler - anyUnpacker types.AnyUnpacker - serviceData []serviceData + routes map[string]GRPCQueryHandler + interfaceRegistry types.InterfaceRegistry + serviceData []serviceData } // serviceData represents a gRPC service, along with its handler. @@ -28,7 +29,7 @@ type serviceData struct { handler interface{} } -var _ gogogrpc.Server +var _ gogogrpc.Server = &GRPCQueryRouter{} // NewGRPCQueryRouter creates a new GRPCQueryRouter func NewGRPCQueryRouter() *GRPCQueryRouter { @@ -67,8 +68,8 @@ func (qrt *GRPCQueryRouter) RegisterService(sd *grpc.ServiceDesc, handler interf if err != nil { return err } - if qrt.anyUnpacker != nil { - return types.UnpackInterfaces(i, qrt.anyUnpacker) + if qrt.interfaceRegistry != nil { + return types.UnpackInterfaces(i, qrt.interfaceRegistry) } return nil }, nil) @@ -96,12 +97,14 @@ func (qrt *GRPCQueryRouter) RegisterService(sd *grpc.ServiceDesc, handler interf }) } -// AnyUnpacker returns the AnyUnpacker for the router -func (qrt *GRPCQueryRouter) AnyUnpacker() types.AnyUnpacker { - return qrt.anyUnpacker -} +// SetInterfaceRegistry sets the interface registry for the router. +func (qrt *GRPCQueryRouter) SetInterfaceRegistry(interfaceRegistry types.InterfaceRegistry) { + qrt.interfaceRegistry = interfaceRegistry -// SetAnyUnpacker sets the AnyUnpacker for the router -func (qrt *GRPCQueryRouter) SetAnyUnpacker(anyUnpacker types.AnyUnpacker) { - qrt.anyUnpacker = anyUnpacker + // Once we have an interface registry, we can register the interface + // registry reflection gRPC service. + reflection.RegisterReflectionServiceServer( + qrt, + reflection.NewReflectionServiceServer(qrt.interfaceRegistry), + ) } diff --git a/baseapp/grpcrouter_helpers.go b/baseapp/grpcrouter_helpers.go index c40f22648..5d4048e0e 100644 --- a/baseapp/grpcrouter_helpers.go +++ b/baseapp/grpcrouter_helpers.go @@ -24,9 +24,9 @@ type QueryServiceTestHelper struct { // NewQueryServerTestHelper creates a new QueryServiceTestHelper that wraps // the provided sdk.Context -func NewQueryServerTestHelper(ctx sdk.Context, anyUnpacker types.AnyUnpacker) *QueryServiceTestHelper { +func NewQueryServerTestHelper(ctx sdk.Context, interfaceRegistry types.InterfaceRegistry) *QueryServiceTestHelper { qrt := NewGRPCQueryRouter() - qrt.SetAnyUnpacker(anyUnpacker) + qrt.SetInterfaceRegistry(interfaceRegistry) return &QueryServiceTestHelper{GRPCQueryRouter: qrt, ctx: ctx} } @@ -51,8 +51,8 @@ func (q *QueryServiceTestHelper) Invoke(_ gocontext.Context, method string, args return err } - if q.anyUnpacker != nil { - return types.UnpackInterfaces(reply, q.anyUnpacker) + if q.interfaceRegistry != nil { + return types.UnpackInterfaces(reply, q.interfaceRegistry) } return nil diff --git a/baseapp/grpcrouter_test.go b/baseapp/grpcrouter_test.go index 13e839a23..2e06ea7ae 100644 --- a/baseapp/grpcrouter_test.go +++ b/baseapp/grpcrouter_test.go @@ -15,7 +15,7 @@ import ( func TestGRPCRouter(t *testing.T) { qr := NewGRPCQueryRouter() interfaceRegistry := testdata.NewTestInterfaceRegistry() - qr.SetAnyUnpacker(interfaceRegistry) + qr.SetInterfaceRegistry(interfaceRegistry) testdata.RegisterTestServiceServer(qr, testdata.TestServiceImpl{}) helper := &QueryServiceTestHelper{ GRPCQueryRouter: qr, diff --git a/client/grpc/reflection/reflection.go b/client/grpc/reflection/reflection.go new file mode 100644 index 000000000..eb07ea86b --- /dev/null +++ b/client/grpc/reflection/reflection.go @@ -0,0 +1,45 @@ +package reflection + +import ( + "context" + + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + + "github.com/cosmos/cosmos-sdk/codec/types" +) + +type reflectionServiceServer struct { + interfaceRegistry types.InterfaceRegistry +} + +// NewReflectionServiceServer creates a new reflectionServiceServer. +func NewReflectionServiceServer(interfaceRegistry types.InterfaceRegistry) ReflectionServiceServer { + return &reflectionServiceServer{interfaceRegistry: interfaceRegistry} +} + +var _ ReflectionServiceServer = (*reflectionServiceServer)(nil) + +// ListAllInterfaces implements the ListAllInterfaces method of the +// ReflectionServiceServer interface. +func (r reflectionServiceServer) ListAllInterfaces(_ context.Context, _ *ListAllInterfacesRequest) (*ListAllInterfacesResponse, error) { + ifaces := r.interfaceRegistry.ListAllInterfaces() + + return &ListAllInterfacesResponse{InterfaceNames: ifaces}, nil +} + +// ListImplementations implements the ListImplementations method of the +// ReflectionServiceServer interface. +func (r reflectionServiceServer) ListImplementations(_ context.Context, req *ListImplementationsRequest) (*ListImplementationsResponse, error) { + if req == nil { + return nil, status.Error(codes.InvalidArgument, "empty request") + } + + if req.InterfaceName == "" { + return nil, status.Error(codes.InvalidArgument, "invalid interface name") + } + + impls := r.interfaceRegistry.ListImplementations(req.InterfaceName) + + return &ListImplementationsResponse{ImplementationMessageNames: impls}, nil +} diff --git a/client/grpc/reflection/reflection.pb.go b/client/grpc/reflection/reflection.pb.go new file mode 100644 index 000000000..8b26ea90e --- /dev/null +++ b/client/grpc/reflection/reflection.pb.go @@ -0,0 +1,942 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: cosmos/base/reflection/v1beta1/reflection.proto + +package reflection + +import ( + context "context" + fmt "fmt" + grpc1 "github.com/gogo/protobuf/grpc" + proto "github.com/gogo/protobuf/proto" + 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 + +// ListAllInterfacesRequest is the request type of the ListAllInterfaces RPC. +type ListAllInterfacesRequest struct { +} + +func (m *ListAllInterfacesRequest) Reset() { *m = ListAllInterfacesRequest{} } +func (m *ListAllInterfacesRequest) String() string { return proto.CompactTextString(m) } +func (*ListAllInterfacesRequest) ProtoMessage() {} +func (*ListAllInterfacesRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_d48c054165687f5c, []int{0} +} +func (m *ListAllInterfacesRequest) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ListAllInterfacesRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ListAllInterfacesRequest.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 *ListAllInterfacesRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_ListAllInterfacesRequest.Merge(m, src) +} +func (m *ListAllInterfacesRequest) XXX_Size() int { + return m.Size() +} +func (m *ListAllInterfacesRequest) XXX_DiscardUnknown() { + xxx_messageInfo_ListAllInterfacesRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_ListAllInterfacesRequest proto.InternalMessageInfo + +// ListAllInterfacesResponse is the response type of the ListAllInterfaces RPC. +type ListAllInterfacesResponse struct { + // interface_names is an array of all the registered interfaces. + InterfaceNames []string `protobuf:"bytes,1,rep,name=interface_names,json=interfaceNames,proto3" json:"interface_names,omitempty"` +} + +func (m *ListAllInterfacesResponse) Reset() { *m = ListAllInterfacesResponse{} } +func (m *ListAllInterfacesResponse) String() string { return proto.CompactTextString(m) } +func (*ListAllInterfacesResponse) ProtoMessage() {} +func (*ListAllInterfacesResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_d48c054165687f5c, []int{1} +} +func (m *ListAllInterfacesResponse) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ListAllInterfacesResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ListAllInterfacesResponse.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 *ListAllInterfacesResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_ListAllInterfacesResponse.Merge(m, src) +} +func (m *ListAllInterfacesResponse) XXX_Size() int { + return m.Size() +} +func (m *ListAllInterfacesResponse) XXX_DiscardUnknown() { + xxx_messageInfo_ListAllInterfacesResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_ListAllInterfacesResponse proto.InternalMessageInfo + +func (m *ListAllInterfacesResponse) GetInterfaceNames() []string { + if m != nil { + return m.InterfaceNames + } + return nil +} + +// ListImplementationsRequest is the request type of the ListImplementations RPC. +type ListImplementationsRequest struct { + // interface_name defines the interface to query the implementations for. + InterfaceName string `protobuf:"bytes,1,opt,name=interface_name,json=interfaceName,proto3" json:"interface_name,omitempty"` +} + +func (m *ListImplementationsRequest) Reset() { *m = ListImplementationsRequest{} } +func (m *ListImplementationsRequest) String() string { return proto.CompactTextString(m) } +func (*ListImplementationsRequest) ProtoMessage() {} +func (*ListImplementationsRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_d48c054165687f5c, []int{2} +} +func (m *ListImplementationsRequest) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ListImplementationsRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ListImplementationsRequest.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 *ListImplementationsRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_ListImplementationsRequest.Merge(m, src) +} +func (m *ListImplementationsRequest) XXX_Size() int { + return m.Size() +} +func (m *ListImplementationsRequest) XXX_DiscardUnknown() { + xxx_messageInfo_ListImplementationsRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_ListImplementationsRequest proto.InternalMessageInfo + +func (m *ListImplementationsRequest) GetInterfaceName() string { + if m != nil { + return m.InterfaceName + } + return "" +} + +// ListImplementationsResponse is the response type of the ListImplementations RPC. +type ListImplementationsResponse struct { + ImplementationMessageNames []string `protobuf:"bytes,1,rep,name=implementation_message_names,json=implementationMessageNames,proto3" json:"implementation_message_names,omitempty"` +} + +func (m *ListImplementationsResponse) Reset() { *m = ListImplementationsResponse{} } +func (m *ListImplementationsResponse) String() string { return proto.CompactTextString(m) } +func (*ListImplementationsResponse) ProtoMessage() {} +func (*ListImplementationsResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_d48c054165687f5c, []int{3} +} +func (m *ListImplementationsResponse) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ListImplementationsResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ListImplementationsResponse.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 *ListImplementationsResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_ListImplementationsResponse.Merge(m, src) +} +func (m *ListImplementationsResponse) XXX_Size() int { + return m.Size() +} +func (m *ListImplementationsResponse) XXX_DiscardUnknown() { + xxx_messageInfo_ListImplementationsResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_ListImplementationsResponse proto.InternalMessageInfo + +func (m *ListImplementationsResponse) GetImplementationMessageNames() []string { + if m != nil { + return m.ImplementationMessageNames + } + return nil +} + +func init() { + proto.RegisterType((*ListAllInterfacesRequest)(nil), "cosmos.base.reflection.v1beta1.ListAllInterfacesRequest") + proto.RegisterType((*ListAllInterfacesResponse)(nil), "cosmos.base.reflection.v1beta1.ListAllInterfacesResponse") + proto.RegisterType((*ListImplementationsRequest)(nil), "cosmos.base.reflection.v1beta1.ListImplementationsRequest") + proto.RegisterType((*ListImplementationsResponse)(nil), "cosmos.base.reflection.v1beta1.ListImplementationsResponse") +} + +func init() { + proto.RegisterFile("cosmos/base/reflection/v1beta1/reflection.proto", fileDescriptor_d48c054165687f5c) +} + +var fileDescriptor_d48c054165687f5c = []byte{ + // 339 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x52, 0x4d, 0x4b, 0xc3, 0x40, + 0x14, 0xcc, 0x2a, 0x08, 0x5d, 0xb0, 0xd2, 0xf5, 0x52, 0xa3, 0x2c, 0x25, 0x20, 0xf6, 0x62, 0x42, + 0xed, 0xc5, 0x8f, 0x8b, 0x5f, 0x97, 0x82, 0xf5, 0x10, 0x6f, 0x5e, 0x4a, 0xb2, 0xbe, 0xd6, 0xc5, + 0x24, 0x1b, 0xf3, 0xb6, 0xfd, 0x11, 0x9e, 0xfa, 0x5f, 0xfc, 0x13, 0x1e, 0x7b, 0xf4, 0x28, 0xed, + 0x1f, 0x91, 0x34, 0x4d, 0x69, 0x30, 0x8a, 0xf4, 0xb4, 0x30, 0xef, 0xcd, 0xbc, 0xd9, 0x61, 0xa8, + 0x23, 0x14, 0x86, 0x0a, 0x1d, 0xdf, 0x43, 0x70, 0x12, 0xe8, 0x07, 0x20, 0xb4, 0x54, 0x91, 0x33, + 0x6a, 0xf9, 0xa0, 0xbd, 0xd6, 0x0a, 0x64, 0xc7, 0x89, 0xd2, 0x8a, 0xf1, 0x8c, 0x60, 0xa7, 0x04, + 0x7b, 0x65, 0xba, 0x20, 0x58, 0x26, 0xad, 0xdf, 0x49, 0xd4, 0x57, 0x41, 0xd0, 0x89, 0x34, 0x24, + 0x7d, 0x4f, 0x00, 0xba, 0xf0, 0x3a, 0x04, 0xd4, 0xd6, 0x2d, 0xdd, 0x2b, 0x99, 0x61, 0xac, 0x22, + 0x04, 0x76, 0x44, 0x77, 0x64, 0x8e, 0xf6, 0x22, 0x2f, 0x04, 0xac, 0x93, 0xc6, 0x66, 0xb3, 0xe2, + 0x56, 0x97, 0xf0, 0x7d, 0x8a, 0x5a, 0x37, 0xd4, 0x4c, 0x55, 0x3a, 0x61, 0x1c, 0x40, 0x08, 0x91, + 0xf6, 0xd2, 0xfb, 0xf9, 0x0d, 0x76, 0x48, 0xab, 0x45, 0x99, 0x3a, 0x69, 0x90, 0x66, 0xc5, 0xdd, + 0x2e, 0xa8, 0x58, 0x3d, 0xba, 0x5f, 0x2a, 0xb2, 0x30, 0x73, 0x49, 0x0f, 0x64, 0x61, 0xd4, 0x0b, + 0x01, 0xd1, 0x1b, 0x14, 0x9d, 0x99, 0xc5, 0x9d, 0x6e, 0xb6, 0x32, 0x77, 0x79, 0xf2, 0xbe, 0x41, + 0x6b, 0xee, 0x32, 0x9e, 0x07, 0x48, 0x46, 0x52, 0x00, 0x7b, 0x23, 0xb4, 0xf6, 0x23, 0x02, 0x76, + 0x6a, 0xff, 0x1d, 0xaa, 0xfd, 0x5b, 0xa2, 0xe6, 0xd9, 0x1a, 0xcc, 0xec, 0x8b, 0x96, 0xc1, 0xc6, + 0x84, 0xee, 0x96, 0x84, 0xc0, 0xce, 0xff, 0x23, 0x5a, 0x1e, 0xbf, 0x79, 0xb1, 0x16, 0x37, 0xb7, + 0x74, 0xdd, 0xfd, 0x98, 0x72, 0x32, 0x99, 0x72, 0xf2, 0x35, 0xe5, 0x64, 0x3c, 0xe3, 0xc6, 0x64, + 0xc6, 0x8d, 0xcf, 0x19, 0x37, 0x1e, 0xdb, 0x03, 0xa9, 0x9f, 0x87, 0xbe, 0x2d, 0x54, 0x98, 0x77, + 0x36, 0x7b, 0x8e, 0xf1, 0xe9, 0xc5, 0x11, 0x81, 0x84, 0x48, 0x3b, 0x83, 0x24, 0x16, 0x2b, 0x95, + 0xf5, 0xb7, 0xe6, 0x9d, 0x6d, 0x7f, 0x07, 0x00, 0x00, 0xff, 0xff, 0x2d, 0x13, 0x9d, 0x10, 0xe6, + 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 + +// ReflectionServiceClient is the client API for ReflectionService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type ReflectionServiceClient interface { + // ListAllInterfaces lists all the interfaces registered in the interface + // registry. + ListAllInterfaces(ctx context.Context, in *ListAllInterfacesRequest, opts ...grpc.CallOption) (*ListAllInterfacesResponse, error) + // ListImplementations list all the concrete types that implement a given + // interface. + ListImplementations(ctx context.Context, in *ListImplementationsRequest, opts ...grpc.CallOption) (*ListImplementationsResponse, error) +} + +type reflectionServiceClient struct { + cc grpc1.ClientConn +} + +func NewReflectionServiceClient(cc grpc1.ClientConn) ReflectionServiceClient { + return &reflectionServiceClient{cc} +} + +func (c *reflectionServiceClient) ListAllInterfaces(ctx context.Context, in *ListAllInterfacesRequest, opts ...grpc.CallOption) (*ListAllInterfacesResponse, error) { + out := new(ListAllInterfacesResponse) + err := c.cc.Invoke(ctx, "/cosmos.base.reflection.v1beta1.ReflectionService/ListAllInterfaces", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *reflectionServiceClient) ListImplementations(ctx context.Context, in *ListImplementationsRequest, opts ...grpc.CallOption) (*ListImplementationsResponse, error) { + out := new(ListImplementationsResponse) + err := c.cc.Invoke(ctx, "/cosmos.base.reflection.v1beta1.ReflectionService/ListImplementations", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// ReflectionServiceServer is the server API for ReflectionService service. +type ReflectionServiceServer interface { + // ListAllInterfaces lists all the interfaces registered in the interface + // registry. + ListAllInterfaces(context.Context, *ListAllInterfacesRequest) (*ListAllInterfacesResponse, error) + // ListImplementations list all the concrete types that implement a given + // interface. + ListImplementations(context.Context, *ListImplementationsRequest) (*ListImplementationsResponse, error) +} + +// UnimplementedReflectionServiceServer can be embedded to have forward compatible implementations. +type UnimplementedReflectionServiceServer struct { +} + +func (*UnimplementedReflectionServiceServer) ListAllInterfaces(ctx context.Context, req *ListAllInterfacesRequest) (*ListAllInterfacesResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method ListAllInterfaces not implemented") +} +func (*UnimplementedReflectionServiceServer) ListImplementations(ctx context.Context, req *ListImplementationsRequest) (*ListImplementationsResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method ListImplementations not implemented") +} + +func RegisterReflectionServiceServer(s grpc1.Server, srv ReflectionServiceServer) { + s.RegisterService(&_ReflectionService_serviceDesc, srv) +} + +func _ReflectionService_ListAllInterfaces_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(ListAllInterfacesRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ReflectionServiceServer).ListAllInterfaces(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/cosmos.base.reflection.v1beta1.ReflectionService/ListAllInterfaces", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ReflectionServiceServer).ListAllInterfaces(ctx, req.(*ListAllInterfacesRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _ReflectionService_ListImplementations_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(ListImplementationsRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ReflectionServiceServer).ListImplementations(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/cosmos.base.reflection.v1beta1.ReflectionService/ListImplementations", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ReflectionServiceServer).ListImplementations(ctx, req.(*ListImplementationsRequest)) + } + return interceptor(ctx, in, info, handler) +} + +var _ReflectionService_serviceDesc = grpc.ServiceDesc{ + ServiceName: "cosmos.base.reflection.v1beta1.ReflectionService", + HandlerType: (*ReflectionServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "ListAllInterfaces", + Handler: _ReflectionService_ListAllInterfaces_Handler, + }, + { + MethodName: "ListImplementations", + Handler: _ReflectionService_ListImplementations_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "cosmos/base/reflection/v1beta1/reflection.proto", +} + +func (m *ListAllInterfacesRequest) 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 *ListAllInterfacesRequest) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ListAllInterfacesRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + return len(dAtA) - i, nil +} + +func (m *ListAllInterfacesResponse) 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 *ListAllInterfacesResponse) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ListAllInterfacesResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.InterfaceNames) > 0 { + for iNdEx := len(m.InterfaceNames) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.InterfaceNames[iNdEx]) + copy(dAtA[i:], m.InterfaceNames[iNdEx]) + i = encodeVarintReflection(dAtA, i, uint64(len(m.InterfaceNames[iNdEx]))) + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + +func (m *ListImplementationsRequest) 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 *ListImplementationsRequest) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ListImplementationsRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.InterfaceName) > 0 { + i -= len(m.InterfaceName) + copy(dAtA[i:], m.InterfaceName) + i = encodeVarintReflection(dAtA, i, uint64(len(m.InterfaceName))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *ListImplementationsResponse) 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 *ListImplementationsResponse) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ListImplementationsResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.ImplementationMessageNames) > 0 { + for iNdEx := len(m.ImplementationMessageNames) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.ImplementationMessageNames[iNdEx]) + copy(dAtA[i:], m.ImplementationMessageNames[iNdEx]) + i = encodeVarintReflection(dAtA, i, uint64(len(m.ImplementationMessageNames[iNdEx]))) + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + +func encodeVarintReflection(dAtA []byte, offset int, v uint64) int { + offset -= sovReflection(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *ListAllInterfacesRequest) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + return n +} + +func (m *ListAllInterfacesResponse) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.InterfaceNames) > 0 { + for _, s := range m.InterfaceNames { + l = len(s) + n += 1 + l + sovReflection(uint64(l)) + } + } + return n +} + +func (m *ListImplementationsRequest) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.InterfaceName) + if l > 0 { + n += 1 + l + sovReflection(uint64(l)) + } + return n +} + +func (m *ListImplementationsResponse) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.ImplementationMessageNames) > 0 { + for _, s := range m.ImplementationMessageNames { + l = len(s) + n += 1 + l + sovReflection(uint64(l)) + } + } + return n +} + +func sovReflection(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozReflection(x uint64) (n int) { + return sovReflection(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *ListAllInterfacesRequest) 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 ErrIntOverflowReflection + } + 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: ListAllInterfacesRequest: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ListAllInterfacesRequest: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + default: + iNdEx = preIndex + skippy, err := skipReflection(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthReflection + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthReflection + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *ListAllInterfacesResponse) 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 ErrIntOverflowReflection + } + 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: ListAllInterfacesResponse: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ListAllInterfacesResponse: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field InterfaceNames", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowReflection + } + 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 ErrInvalidLengthReflection + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthReflection + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.InterfaceNames = append(m.InterfaceNames, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipReflection(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthReflection + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthReflection + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *ListImplementationsRequest) 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 ErrIntOverflowReflection + } + 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: ListImplementationsRequest: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ListImplementationsRequest: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field InterfaceName", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowReflection + } + 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 ErrInvalidLengthReflection + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthReflection + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.InterfaceName = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipReflection(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthReflection + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthReflection + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *ListImplementationsResponse) 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 ErrIntOverflowReflection + } + 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: ListImplementationsResponse: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ListImplementationsResponse: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ImplementationMessageNames", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowReflection + } + 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 ErrInvalidLengthReflection + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthReflection + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.ImplementationMessageNames = append(m.ImplementationMessageNames, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipReflection(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthReflection + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthReflection + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipReflection(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, ErrIntOverflowReflection + } + 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, ErrIntOverflowReflection + } + 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, ErrIntOverflowReflection + } + 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, ErrInvalidLengthReflection + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupReflection + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthReflection + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthReflection = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowReflection = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupReflection = fmt.Errorf("proto: unexpected end of group") +) diff --git a/client/grpc/reflection/reflection_test.go b/client/grpc/reflection/reflection_test.go new file mode 100644 index 000000000..037e7e06a --- /dev/null +++ b/client/grpc/reflection/reflection_test.go @@ -0,0 +1,60 @@ +package reflection_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/suite" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/client/grpc/reflection" + "github.com/cosmos/cosmos-sdk/simapp" +) + +type IntegrationTestSuite struct { + suite.Suite + + queryClient reflection.ReflectionServiceClient +} + +func (s *IntegrationTestSuite) SetupSuite() { + app := simapp.Setup(false) + + srv := reflection.NewReflectionServiceServer(app.InterfaceRegistry()) + + sdkCtx := app.BaseApp.NewContext(false, tmproto.Header{}) + queryHelper := baseapp.NewQueryServerTestHelper(sdkCtx, app.InterfaceRegistry()) + + reflection.RegisterReflectionServiceServer(queryHelper, srv) + queryClient := reflection.NewReflectionServiceClient(queryHelper) + + s.queryClient = queryClient +} + +func (s IntegrationTestSuite) TestSimulateService() { + // We will test the following interface for testing. + var iface = "cosmos.evidence.v1beta1.Evidence" + + // Test that "cosmos.evidence.v1beta1.Evidence" is included in the + // interfaces. + resIface, err := s.queryClient.ListAllInterfaces( + context.Background(), + &reflection.ListAllInterfacesRequest{}, + ) + s.Require().NoError(err) + s.Require().Contains(resIface.GetInterfaceNames(), iface) + + // Test that "cosmos.evidence.v1beta1.Evidence" has at least the + // Equivocation implementations. + resImpl, err := s.queryClient.ListImplementations( + context.Background(), + &reflection.ListImplementationsRequest{InterfaceName: iface}, + ) + s.Require().NoError(err) + s.Require().Contains(resImpl.GetImplementationMessageNames(), "/cosmos.evidence.v1beta1.Equivocation") +} + +func TestSimulateTestSuite(t *testing.T) { + suite.Run(t, new(IntegrationTestSuite)) +} diff --git a/codec/types/interface_registry.go b/codec/types/interface_registry.go index 6b5f47410..0e59ea969 100644 --- a/codec/types/interface_registry.go +++ b/codec/types/interface_registry.go @@ -42,6 +42,13 @@ type InterfaceRegistry interface { // Ex: // registry.RegisterImplementations((*sdk.Msg)(nil), &MsgSend{}, &MsgMultiSend{}) RegisterImplementations(iface interface{}, impls ...proto.Message) + + // ListAllInterfaces list the type URLs of all registered interfaces. + ListAllInterfaces() []string + + // ListImplementations lists the valid type URLs for the given interface name that can be used + // for the provided interface type URL. + ListImplementations(ifaceTypeURL string) []string } // UnpackInterfacesMessage is meant to extend protobuf types (which implement @@ -111,6 +118,33 @@ func (registry *interfaceRegistry) RegisterImplementations(iface interface{}, im registry.interfaceImpls[ityp] = imap } +func (registry *interfaceRegistry) ListAllInterfaces() []string { + interfaceNames := registry.interfaceNames + keys := make([]string, 0, len(interfaceNames)) + for key := range interfaceNames { + keys = append(keys, key) + } + return keys +} + +func (registry *interfaceRegistry) ListImplementations(ifaceName string) []string { + typ, ok := registry.interfaceNames[ifaceName] + if !ok { + return []string{} + } + + impls, ok := registry.interfaceImpls[typ.Elem()] + if !ok { + return []string{} + } + + keys := make([]string, 0, len(impls)) + for key := range impls { + keys = append(keys, key) + } + return keys +} + func (registry *interfaceRegistry) UnpackAny(any *Any, iface interface{}) error { if any.TypeUrl == "" { // if TypeUrl is empty return nil because without it we can't actually unpack anything diff --git a/proto/cosmos/base/reflection/v1beta1/reflection.proto b/proto/cosmos/base/reflection/v1beta1/reflection.proto new file mode 100644 index 000000000..7e7a8d048 --- /dev/null +++ b/proto/cosmos/base/reflection/v1beta1/reflection.proto @@ -0,0 +1,35 @@ +syntax = "proto3"; +package cosmos.base.reflection.v1beta1; + +option go_package = "github.com/cosmos/cosmos-sdk/client/grpc/reflection"; + +// ReflectionService defines a service for interface reflection. +service ReflectionService { + // ListAllInterfaces lists all the interfaces registered in the interface + // registry. + rpc ListAllInterfaces(ListAllInterfacesRequest) returns (ListAllInterfacesResponse) {}; + + // ListImplementations list all the concrete types that implement a given + // interface. + rpc ListImplementations(ListImplementationsRequest) returns (ListImplementationsResponse) {}; +} + +// ListAllInterfacesRequest is the request type of the ListAllInterfaces RPC. +message ListAllInterfacesRequest { } + +// ListAllInterfacesResponse is the response type of the ListAllInterfaces RPC. +message ListAllInterfacesResponse { + // interface_names is an array of all the registered interfaces. + repeated string interface_names = 1; +} + +// ListImplementationsRequest is the request type of the ListImplementations RPC. +message ListImplementationsRequest { + // interface_name defines the interface to query the implementations for. + string interface_name = 1; +} + +// ListImplementationsResponse is the response type of the ListImplementations RPC. +message ListImplementationsResponse { + repeated string implementation_message_names = 1; +} diff --git a/simapp/app.go b/simapp/app.go index 0dee68f02..fc6cda4c7 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -181,7 +181,7 @@ func NewSimApp( bApp := baseapp.NewBaseApp(appName, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...) bApp.SetCommitMultiStoreTracer(traceStore) bApp.SetAppVersion(version.Version) - bApp.GRPCQueryRouter().SetAnyUnpacker(interfaceRegistry) + bApp.GRPCQueryRouter().SetInterfaceRegistry(interfaceRegistry) keys := sdk.NewKVStoreKeys( authtypes.StoreKey, banktypes.StoreKey, stakingtypes.StoreKey, diff --git a/types/module/module.go b/types/module/module.go index 797c29cdb..27401c64a 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -195,6 +195,7 @@ func (GenesisOnlyAppModule) QuerierRoute() string { return "" } // LegacyQuerierHandler returns an empty module querier func (gam GenesisOnlyAppModule) LegacyQuerierHandler(codec.JSONMarshaler) sdk.Querier { return nil } +// RegisterQueryService registers all gRPC query services. func (gam GenesisOnlyAppModule) RegisterQueryService(grpc.Server) {} // BeginBlock returns an empty module begin-block