Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Feb 27, 2024
1 parent 3c550b2 commit 9700f78
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions baseapp/msg_service_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
26 changes: 14 additions & 12 deletions runtime/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package runtime
import (
"context"
"fmt"
"reflect"

Check notice

Code scanning / CodeQL

Sensitive package import Note

Certain system packages contain functions which may be a possible source of non-determinism

"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"

Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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
}

Expand Down

0 comments on commit 9700f78

Please sign in to comment.