From 132e5ebe03e280a1fb22d828a0bf21b9ab52543f Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Mon, 26 Oct 2020 12:37:50 +0100 Subject: [PATCH] Add RegisterMsgServiceDesc --- baseapp/msg_service_router.go | 21 +---------------- types/msg_service.go | 44 +++++++++++++++++++++++++++++++++++ x/auth/types/codec.go | 3 +++ x/bank/types/codec.go | 2 ++ x/crisis/types/codec.go | 2 ++ 5 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 types/msg_service.go diff --git a/baseapp/msg_service_router.go b/baseapp/msg_service_router.go index b02fbd10c0d4..eb2d0cf9d7aa 100644 --- a/baseapp/msg_service_router.go +++ b/baseapp/msg_service_router.go @@ -45,21 +45,7 @@ func (msr *MsgServiceRouter) RegisterService(sd *grpc.ServiceDesc, handler inter fqMethod := fmt.Sprintf("/%s/%s", sd.ServiceName, method.MethodName) methodHandler := method.Handler - // NOTE: This is how we pull the concrete request type for each handler for registering in the InterfaceRegistry. - // This approach is maybe a bit hacky, but less hacky than reflecting on the handler object itself. - // We use a no-op interceptor to avoid actually calling into the handler itself. - _, _ = methodHandler(nil, context.Background(), func(i interface{}) error { - msg, ok := i.(proto.Message) - if !ok { - // We panic here because there is no other alternative and the app cannot be initialized correctly - // this should only happen if there is a problem with code generation in which case the app won't - // work correctly anyway. - panic(fmt.Errorf("can't register request type %T for service method %s", i, fqMethod)) - } - - msr.interfaceRegistry.RegisterCustomTypeURL((*sdk.MsgRequest)(nil), fqMethod, msg) - return nil - }, noopInterceptor) + sdk.RegisterMsgServiceDesc(msr.interfaceRegistry, sd) msr.routes[fqMethod] = func(ctx sdk.Context, req sdk.MsgRequest) (*sdk.Result, error) { ctx = ctx.WithEventManager(sdk.NewEventManager()) @@ -89,9 +75,4 @@ func (msr *MsgServiceRouter) SetInterfaceRegistry(interfaceRegistry codectypes.I msr.interfaceRegistry = interfaceRegistry } -// gRPC NOOP interceptor -func noopInterceptor(_ context.Context, _ interface{}, _ *grpc.UnaryServerInfo, _ grpc.UnaryHandler) (interface{}, error) { - return nil, nil -} - func noopDecoder(_ interface{}) error { return nil } diff --git a/types/msg_service.go b/types/msg_service.go new file mode 100644 index 000000000000..319721a045ea --- /dev/null +++ b/types/msg_service.go @@ -0,0 +1,44 @@ +package types + +import ( + context "context" + fmt "fmt" + + proto "github.com/gogo/protobuf/proto" + grpc "google.golang.org/grpc" + + "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// RegisterMsgServiceDesc registers all type_urls from Msg services described +// in `sd` inside the registry. +func RegisterMsgServiceDesc(registry types.InterfaceRegistry, sd *grpc.ServiceDesc) { + // Adds a top-level type_url based on the Msg service name. + for _, method := range sd.Methods { + fqMethod := fmt.Sprintf("/%s/%s", sd.ServiceName, method.MethodName) + methodHandler := method.Handler + + // NOTE: This is how we pull the concrete request type for each handler for registering in the InterfaceRegistry. + // This approach is maybe a bit hacky, but less hacky than reflecting on the handler object itself. + // We use a no-op interceptor to avoid actually calling into the handler itself. + _, _ = methodHandler(nil, context.Background(), func(i interface{}) error { + msg, ok := i.(proto.Message) + if !ok { + // We panic here because there is no other alternative and the app cannot be initialized correctly + // this should only happen if there is a problem with code generation in which case the app won't + // work correctly anyway. + panic(fmt.Errorf("can't register request type %T for service method %s", i, fqMethod)) + } + + registry.RegisterCustomTypeURL((*sdk.MsgRequest)(nil), fqMethod, msg) + return nil + }, noopInterceptor) + + } +} + +// gRPC NOOP interceptor +func noopInterceptor(_ context.Context, _ interface{}, _ *grpc.UnaryServerInfo, _ grpc.UnaryHandler) (interface{}, error) { + return nil, nil +} diff --git a/x/auth/types/codec.go b/x/auth/types/codec.go index 629e2919d24d..fe6de5025ced 100644 --- a/x/auth/types/codec.go +++ b/x/auth/types/codec.go @@ -4,6 +4,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" ) @@ -35,6 +36,8 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { &BaseAccount{}, &ModuleAccount{}, ) + + sdk.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } var ( diff --git a/x/bank/types/codec.go b/x/bank/types/codec.go index 42c117e24a9d..eab963b1f7af 100644 --- a/x/bank/types/codec.go +++ b/x/bank/types/codec.go @@ -28,6 +28,8 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { (*exported.SupplyI)(nil), &Supply{}, ) + + sdk.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } var ( diff --git a/x/crisis/types/codec.go b/x/crisis/types/codec.go index 3b5dd5eb5a26..efe8214cfc22 100644 --- a/x/crisis/types/codec.go +++ b/x/crisis/types/codec.go @@ -17,6 +17,8 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { registry.RegisterImplementations((*sdk.Msg)(nil), &MsgVerifyInvariant{}, ) + + sdk.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } var (