## Description We decided to remove middlewares, and revert to antehandlers (exactly like in v045) for this release. A better middleware solution will be implemented after v046. ref: #11955 Because this refactor is big, so I decided to cut it into two. This PR is part 1 of 2: - part 1: Revert baseapp and middlewares to v0.45.4 - part 2: Add posthandler, tips, priority --- Suggestion for reviewers: This PR might still be hard to review though. I think it's easier to actually review the diff between v0.45.4 and this PR: - `git difftool -d v0.45.4..am/revert-045-baseapp baseapp` - most important parts to review: runTx, runMsgs - `git difftool -d v0.45.4..am/revert-045-baseapp x/auth/ante` - only cosmetic changes - `git difftool -d v0.45.4..am/revert-045-baseapp simapp` --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
141 lines
5.4 KiB
Go
141 lines
5.4 KiB
Go
package baseapp
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
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.
|
|
type MsgServiceRouter struct {
|
|
interfaceRegistry codectypes.InterfaceRegistry
|
|
routes map[string]MsgServiceHandler
|
|
}
|
|
|
|
var _ gogogrpc.Server = &MsgServiceRouter{}
|
|
|
|
// NewMsgServiceRouter creates a new MsgServiceRouter.
|
|
func NewMsgServiceRouter() *MsgServiceRouter {
|
|
return &MsgServiceRouter{
|
|
routes: map[string]MsgServiceHandler{},
|
|
}
|
|
}
|
|
|
|
// MsgServiceHandler defines a function type which handles Msg service message.
|
|
type MsgServiceHandler = func(ctx sdk.Context, req sdk.Msg) (*sdk.Result, error)
|
|
|
|
// Handler returns the MsgServiceHandler for a given msg or nil if not found.
|
|
func (msr *MsgServiceRouter) Handler(msg sdk.Msg) MsgServiceHandler {
|
|
return msr.routes[sdk.MsgTypeURL(msg)]
|
|
}
|
|
|
|
// HandlerByTypeURL returns the MsgServiceHandler for a given query route path or nil
|
|
// if not found.
|
|
func (msr *MsgServiceRouter) HandlerByTypeURL(typeURL string) MsgServiceHandler {
|
|
return msr.routes[typeURL]
|
|
}
|
|
|
|
// RegisterService implements the gRPC Server.RegisterService method. sd is a gRPC
|
|
// service description, handler is an object which implements that gRPC service.
|
|
//
|
|
// This function PANICs:
|
|
// - if it is called before the service `Msg`s have been registered using
|
|
// RegisterInterfaces,
|
|
// - or if a service is being registered twice.
|
|
func (msr *MsgServiceRouter) RegisterService(sd *grpc.ServiceDesc, handler interface{}) {
|
|
// Adds a top-level query handler based on the gRPC service name.
|
|
for _, method := range sd.Methods {
|
|
fqMethod := fmt.Sprintf("/%s/%s", sd.ServiceName, method.MethodName)
|
|
methodHandler := method.Handler
|
|
|
|
var requestTypeName string
|
|
|
|
// 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.(sdk.Msg)
|
|
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("unable to register service method %s: %T does not implement sdk.Msg", fqMethod, i))
|
|
}
|
|
|
|
requestTypeName = sdk.MsgTypeURL(msg)
|
|
return nil
|
|
}, noopInterceptor)
|
|
|
|
// Check that the service Msg fully-qualified method name has already
|
|
// been registered (via RegisterInterfaces). If the user registers a
|
|
// service without registering according service Msg type, there might be
|
|
// some unexpected behavior down the road. Since we can't return an error
|
|
// (`Server.RegisterService` interface restriction) we panic (at startup).
|
|
reqType, err := msr.interfaceRegistry.Resolve(requestTypeName)
|
|
if err != nil || reqType == nil {
|
|
panic(
|
|
fmt.Errorf(
|
|
"type_url %s has not been registered yet. "+
|
|
"Before calling RegisterService, you must register all interfaces by calling the `RegisterInterfaces` "+
|
|
"method on module.BasicManager. Each module should call `msgservice.RegisterMsgServiceDesc` inside its "+
|
|
"`RegisterInterfaces` method with the `_Msg_serviceDesc` generated by proto-gen",
|
|
requestTypeName,
|
|
),
|
|
)
|
|
}
|
|
|
|
// Check that each service is only registered once. If a service is
|
|
// registered more than once, then we should error. Since we can't
|
|
// return an error (`Server.RegisterService` interface restriction) we
|
|
// panic (at startup).
|
|
_, found := msr.routes[requestTypeName]
|
|
if found {
|
|
panic(
|
|
fmt.Errorf(
|
|
"msg service %s has already been registered. Please make sure to only register each service once. "+
|
|
"This usually means that there are conflicting modules registering the same msg service",
|
|
fqMethod,
|
|
),
|
|
)
|
|
}
|
|
|
|
msr.routes[requestTypeName] = func(ctx sdk.Context, req sdk.Msg) (*sdk.Result, error) {
|
|
ctx = ctx.WithEventManager(sdk.NewEventManager())
|
|
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, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting proto.Message, got %T", resMsg)
|
|
}
|
|
|
|
return sdk.WrapServiceResult(ctx, resMsg, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// SetInterfaceRegistry sets the interface registry for the router.
|
|
func (msr *MsgServiceRouter) SetInterfaceRegistry(interfaceRegistry codectypes.InterfaceRegistry) {
|
|
msr.interfaceRegistry = interfaceRegistry
|
|
}
|
|
|
|
func noopDecoder(_ interface{}) error { return nil }
|
|
func noopInterceptor(_ context.Context, _ interface{}, _ *grpc.UnaryServerInfo, _ grpc.UnaryHandler) (interface{}, error) {
|
|
return nil, nil
|
|
}
|