-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
msg_service_router: reduce code complexity #7570
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would work too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true, though I prefer seeing the exact signature from gogogrpc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exactly, no need to generalize here. |
||||||
return nil, nil | ||||||
} | ||||||
|
||||||
func noopDecoder(_ interface{}) error { return nil } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you planning to use this function elsewhere? What's the benefit of extracting a tiny snippet of code that just return
nil, nil
when it's not yet reused anywhere else?This smells like early refactoring, and as such, pointless (best case scenario) if not potentially harmful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check the PR description.
The function is private
improving readability
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it really improve readability to extract an empty function? I too tend to prefer named functions, though it generally boils down to one question to me: Am I going to use this function elsewhere?
If I am, then it's a no-brainer and I would define it as a separate function right away. If not well, does the benefit overweight the cost of increasing file size and polluting the private namespace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good questions. In fact, sometimes it's opposite - extracting a function it can even reduce the compiled code, because we don't need to carry the context. So depending on the compiler.
Again - function is private, and it's used. Going further with your argument we would prefer to have big functions, which, IMHO is not good.
So, I don't see any benefit. I would see some if the parent function would be smaller and less nested.