-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat(runtime): message router service #19571
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
Changes from 5 commits
1f28529
c8622b3
cf6ff8a
3c550b2
9700f78
c7b8a8a
37c08d2
ad4ec72
3a986fa
e8f8d24
64a92c4
092f902
dee1a22
d84802d
59f1727
c58a930
e38add9
1caec81
b3a330a
ca231ae
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,6 +4,7 @@ go 1.20 | |
|
|
||
| require ( | ||
| cosmossdk.io/log v1.3.1 | ||
| github.com/cosmos/gogoproto v1.4.11 | ||
|
Contributor
Author
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. this dep will be gone in server_modular as I can make use of this: https://github.com/cosmos/cosmos-sdk/pull/19499/files#diff-c1fc6f9f872ad64b8a1e6dee2e5e8bb2d675b920a403e621b8cf1f4ac5d6938cR12-R21 |
||
| github.com/stretchr/testify v1.8.4 | ||
| google.golang.org/grpc v1.62.0 | ||
| google.golang.org/protobuf v1.32.0 | ||
|
|
@@ -12,13 +13,15 @@ require ( | |
| require ( | ||
| github.com/davecgh/go-spew v1.1.1 // indirect | ||
| github.com/golang/protobuf v1.5.3 // indirect | ||
| github.com/google/go-cmp v0.6.0 // indirect | ||
| github.com/kr/pretty v0.3.1 // indirect | ||
| github.com/mattn/go-colorable v0.1.13 // indirect | ||
| github.com/mattn/go-isatty v0.0.20 // indirect | ||
| github.com/pkg/errors v0.9.1 // indirect | ||
| github.com/pmezard/go-difflib v1.0.0 // indirect | ||
| github.com/rogpeppe/go-internal v1.11.0 // indirect | ||
| github.com/rs/zerolog v1.32.0 // indirect | ||
| golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb // indirect | ||
| golang.org/x/net v0.21.0 // indirect | ||
| golang.org/x/sys v0.17.0 // indirect | ||
| golang.org/x/text v0.14.0 // indirect | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package router | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "google.golang.org/protobuf/runtime/protoiface" | ||
| ) | ||
|
|
||
| // 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 | ||
|
julienrbrt marked this conversation as resolved.
|
||
| InvokeUntyped(ctx context.Context, req protoiface.MessageV1) (res protoiface.MessageV1, err error) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package runtime | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "reflect" | ||
Check noticeCode scanning / CodeQL Sensitive package import
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/protoregistry" | ||
| "google.golang.org/protobuf/runtime/protoiface" | ||
|
|
||
| "cosmossdk.io/core/router" | ||
| "cosmossdk.io/core/store" | ||
|
|
||
| "github.com/cosmos/cosmos-sdk/baseapp" | ||
| ) | ||
|
|
||
| func NewMsgRouterService(storeService store.KVStoreService, router baseapp.MessageRouter) router.Service { | ||
|
julienrbrt marked this conversation as resolved.
Outdated
|
||
| return &msgRouterService{ | ||
| storeService: storeService, | ||
| router: router, | ||
|
julienrbrt marked this conversation as resolved.
Outdated
|
||
| resolver: protoregistry.GlobalTypes, | ||
| } | ||
| } | ||
|
|
||
| type msgRouterService struct { | ||
| storeService store.KVStoreService | ||
| router baseapp.MessageRouter | ||
| resolver protoregistry.MessageTypeResolver | ||
| } | ||
|
|
||
| // 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, resp) | ||
| } | ||
|
|
||
| // 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) | ||
| if respName == "" { | ||
| return nil, fmt.Errorf("could not find response type for message %T", msg) | ||
| } | ||
|
|
||
| // get response type | ||
| 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) | ||
|
julienrbrt marked this conversation as resolved.
Outdated
|
||
|
|
||
| handler := m.router.HybridHandlerByMsgName(messageName) | ||
| if handler == nil { | ||
| return nil, fmt.Errorf("unknown message: %s", messageName) | ||
| } | ||
|
|
||
| err := handler(ctx, msg, msgResp) | ||
| return msgResp, err | ||
| } | ||
|
|
||
| // msgTypeURL returns the TypeURL of a `sdk.Msg`. | ||
| func msgTypeURL(msg proto.Message) string { | ||
| if m, ok := msg.(protov2.Message); ok { | ||
| return "/" + string(m.ProtoReflect().Descriptor().FullName()) | ||
| } | ||
|
|
||
| return "/" + proto.MessageName(msg) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.