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 cf6ff8a commit 3c550b2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core/router/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (
// Service is the interface that wraps the basic methods for a router service.
type Service interface {
InvokeTyped(ctx context.Context, req, res protoiface.MessageV1) error
// InvokeUntyped(ctx context.Context, req protoiface.MessageV1) (res protoiface.MessageV1, err error)
InvokeUntyped(ctx context.Context, req protoiface.MessageV1) (res protoiface.MessageV1, err error)
}
40 changes: 37 additions & 3 deletions runtime/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package runtime

import (
"context"
"fmt"

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

"cosmossdk.io/core/router"
Expand All @@ -17,18 +20,49 @@ func NewMsgRouterService(storeService store.KVStoreService, router *baseapp.MsgS
return &msgRouterService{
storeService: storeService,
router: router,
resolver: protoregistry.GlobalTypes,
}
}

type msgRouterService struct {
storeService store.KVStoreService
router *baseapp.MsgServiceRouter
resolver protoregistry.MessageTypeResolver
}

// InvokeTyped implements router.Service.
func (m *msgRouterService) InvokeTyped(ctx context.Context, req, res protoiface.MessageV1) error {
messageName := msgTypeURL(req)
return m.router.HybridHandlerByMsgName(messageName)(ctx, req, res)
func (m *msgRouterService) InvokeTyped(ctx context.Context, msg, msgResp 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)
}

// InvokeUntyped implements router.Service.
func (m *msgRouterService) InvokeUntyped(ctx context.Context, msg protoiface.MessageV1) (protoiface.MessageV1, error) {
messageName := msgTypeURL(msg)
respName := m.router.ResponseNameByRequestName(messageName)
if respName == "" {
return nil, fmt.Errorf("could not find response type for message %T", msg)
}

// get response type
resp, err := m.resolver.FindMessageByName(protoreflect.FullName(respName))
if err != nil {
return nil, err
}

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)
return msgResp, err
}

// msgTypeURL returns the TypeURL of a `sdk.Msg`.
Expand Down

0 comments on commit 3c550b2

Please sign in to comment.