From 3e49249adb0db84c4ef054b099ae2e62342082bb Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 16 Oct 2020 11:43:28 +0200 Subject: [PATCH] msg_service_router: reduce code complexity (#7570) * msg_service_router: reduce code complexity * order imports Co-authored-by: Alessio Treglia Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- baseapp/msg_service_router.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/baseapp/msg_service_router.go b/baseapp/msg_service_router.go index efc108881bda..b02fbd10c0d4 100644 --- a/baseapp/msg_service_router.go +++ b/baseapp/msg_service_router.go @@ -4,13 +4,13 @@ import ( "context" "fmt" - "github.com/gogo/protobuf/proto" - gogogrpc "github.com/gogo/protobuf/grpc" + "github.com/gogo/protobuf/proto" "google.golang.org/grpc" codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) // MsgServiceRouter routes fully-qualified Msg service methods to their handler. @@ -59,28 +59,24 @@ func (msr *MsgServiceRouter) RegisterService(sd *grpc.ServiceDesc, handler inter msr.interfaceRegistry.RegisterCustomTypeURL((*sdk.MsgRequest)(nil), fqMethod, msg) return nil - }, func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { - return nil, nil - }) + }, noopInterceptor) msr.routes[fqMethod] = func(ctx sdk.Context, req sdk.MsgRequest) (*sdk.Result, error) { ctx = ctx.WithEventManager(sdk.NewEventManager()) - - // Call the method handler from the service description with the handler object. - res, err := methodHandler(handler, sdk.WrapSDKContext(ctx), func(_ interface{}) error { - // We don't do any decoding here because the decoding was already done. - return nil - }, func(goCtx context.Context, _ interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + interceptor := func(goCtx context.Context, _ interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { goCtx = context.WithValue(goCtx, sdk.SdkContextKey, ctx) return handler(goCtx, req) - }) + } + // Call the method handler from the service description with the handler object. + // We don't do any decoding here because the decoding was already done. + res, err := methodHandler(handler, sdk.WrapSDKContext(ctx), noopDecoder, interceptor) if err != nil { return nil, err } resMsg, ok := res.(proto.Message) if !ok { - return nil, fmt.Errorf("can't proto encode %T", resMsg) + return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting proto.Message, got %T", resMsg) } return sdk.WrapServiceResult(ctx, resMsg, err) @@ -92,3 +88,10 @@ func (msr *MsgServiceRouter) RegisterService(sd *grpc.ServiceDesc, handler inter func (msr *MsgServiceRouter) SetInterfaceRegistry(interfaceRegistry codectypes.InterfaceRegistry) { 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 }