From 9700f787e7269a7ecf7d39be1e66d29400c1262d Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Tue, 27 Feb 2024 12:47:48 +0100 Subject: [PATCH] updates --- CHANGELOG.md | 1 + baseapp/msg_service_router.go | 3 +++ core/CHANGELOG.md | 3 ++- runtime/router.go | 26 ++++++++++++++------------ 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e9939c7bd8d..c5d15e6869f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i ### Features +* (runtime) [#19571](https://github.com/cosmos/cosmos-sdk/pull/19571) Implement `core/router.Service` it in runtime. This service is present in all modules (when using depinject). * (types) [#19164](https://github.com/cosmos/cosmos-sdk/pull/19164) Add a ValueCodec for the math.Uint type that can be used in collections maps. * (types) [#19281](https://github.com/cosmos/cosmos-sdk/pull/19281) Added a new method, `IsGT`, for `types.Coin`. This method is used to check if a `types.Coin` is greater than another `types.Coin`. * (client) [#18557](https://github.com/cosmos/cosmos-sdk/pull/18557) Add `--qrcode` flag to `keys show` command to support displaying keys address QR code. diff --git a/baseapp/msg_service_router.go b/baseapp/msg_service_router.go index fe4488d2673c..22f76cbafa3c 100644 --- a/baseapp/msg_service_router.go +++ b/baseapp/msg_service_router.go @@ -24,6 +24,9 @@ import ( type MessageRouter interface { Handler(msg sdk.Msg) MsgServiceHandler HandlerByTypeURL(typeURL string) MsgServiceHandler + + ResponseNameByRequestName(msgName string) string + HybridHandlerByMsgName(msgName string) func(ctx context.Context, req, resp protoiface.MessageV1) error } // MsgServiceRouter routes fully-qualified Msg service methods to their handler. diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 27ee7b8be825..49a3e692f14d 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -42,7 +42,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#18457](https://github.com/cosmos/cosmos-sdk/pull/18457) Add branch.ExecuteWithGasLimit. * [#19041](https://github.com/cosmos/cosmos-sdk/pull/19041) Add `appmodule.Environment` interface to fetch different services * [#19370](https://github.com/cosmos/cosmos-sdk/pull/19370) Add `appmodule.Migrations` interface to handle migrations - +* [#19571](https://github.com/cosmos/cosmos-sdk/pull/19571) Add `router.Service` and add it in `appmodule.Environment` + ### API Breaking Changes * [#18857](https://github.com/cosmos/cosmos-sdk/pull/18857) Moved `FormatCoins` to `x/tx`. diff --git a/runtime/router.go b/runtime/router.go index baebea58ecca..f690e213d54e 100644 --- a/runtime/router.go +++ b/runtime/router.go @@ -3,10 +3,10 @@ package runtime import ( "context" "fmt" + "reflect" "github.com/cosmos/gogoproto/proto" protov2 "google.golang.org/protobuf/proto" - "google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoregistry" "google.golang.org/protobuf/runtime/protoiface" @@ -16,7 +16,7 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" ) -func NewMsgRouterService(storeService store.KVStoreService, router *baseapp.MsgServiceRouter) router.Service { +func NewMsgRouterService(storeService store.KVStoreService, router baseapp.MessageRouter) router.Service { return &msgRouterService{ storeService: storeService, router: router, @@ -26,22 +26,24 @@ func NewMsgRouterService(storeService store.KVStoreService, router *baseapp.MsgS type msgRouterService struct { storeService store.KVStoreService - router *baseapp.MsgServiceRouter + router baseapp.MessageRouter resolver protoregistry.MessageTypeResolver } -// InvokeTyped implements router.Service. -func (m *msgRouterService) InvokeTyped(ctx context.Context, msg, msgResp protoiface.MessageV1) error { +// InvokeTyped execute a message and fill-in a response. +// The response must be known and passed as a parameter. +// Use InvokeUntyped if the response type is not known. +func (m *msgRouterService) InvokeTyped(ctx context.Context, msg, resp protoiface.MessageV1) error { messageName := msgTypeURL(msg) handler := m.router.HybridHandlerByMsgName(messageName) if handler == nil { return fmt.Errorf("unknown message: %s", messageName) } - return handler(ctx, msg, msgResp) + return handler(ctx, msg, resp) } -// InvokeUntyped implements router.Service. +// InvokeUntyped execute a message and returns a response. func (m *msgRouterService) InvokeUntyped(ctx context.Context, msg protoiface.MessageV1) (protoiface.MessageV1, error) { messageName := msgTypeURL(msg) respName := m.router.ResponseNameByRequestName(messageName) @@ -50,18 +52,18 @@ func (m *msgRouterService) InvokeUntyped(ctx context.Context, msg protoiface.Mes } // get response type - resp, err := m.resolver.FindMessageByName(protoreflect.FullName(respName)) - if err != nil { - return nil, err + typ := proto.MessageType(respName) + if typ == nil { + return nil, fmt.Errorf("no message type found for %s", respName) } + msgResp := reflect.New(typ.Elem()).Interface().(protoiface.MessageV1) handler := m.router.HybridHandlerByMsgName(messageName) if handler == nil { return nil, fmt.Errorf("unknown message: %s", messageName) } - msgResp := resp.New().Interface().(protoiface.MessageV1) - err = handler(ctx, msg, msgResp) + err := handler(ctx, msg, msgResp) return msgResp, err }