Skip to content
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

feat: Add server implementation of Group module #10570

Merged
merged 70 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
32cb63a
remove Request suffix from Msg types
likhita-809 Oct 29, 2021
fe6a8d5
wip
likhita-809 Nov 8, 2021
246484e
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Nov 8, 2021
e274069
wip
likhita-809 Nov 9, 2021
4203f24
wip
likhita-809 Nov 9, 2021
743520c
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Nov 9, 2021
3f4179e
wip
likhita-809 Nov 9, 2021
acad315
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Nov 17, 2021
be27cba
wip
likhita-809 Nov 18, 2021
71c2dee
run go mod tidy
likhita-809 Nov 18, 2021
a864359
small change
likhita-809 Nov 18, 2021
18794db
wip
likhita-809 Nov 22, 2021
3851900
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Nov 22, 2021
36b07b3
wip
likhita-809 Nov 22, 2021
efb22ee
fix something
likhita-809 Nov 22, 2021
1edaf8c
wip
likhita-809 Nov 23, 2021
3d5c37d
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Nov 23, 2021
87054b8
wip
likhita-809 Nov 23, 2021
164a153
wip
likhita-809 Nov 23, 2021
0f6028b
run go mod tidy
likhita-809 Nov 23, 2021
d4156a2
wip
likhita-809 Nov 23, 2021
c473327
wip
likhita-809 Nov 23, 2021
491ff91
fix tests
likhita-809 Nov 23, 2021
8bcbbf7
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Nov 24, 2021
ec93341
add keeper tests
likhita-809 Nov 25, 2021
624726b
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Nov 25, 2021
ac4e247
address comments
likhita-809 Nov 25, 2021
f8a74b6
use single MsgServiceRouter
likhita-809 Nov 25, 2021
308852e
address review comments
likhita-809 Nov 25, 2021
f5fdbea
fix something
likhita-809 Nov 25, 2021
2c405d3
Update import
blushi Nov 25, 2021
100c4f4
delete conditions.go file
likhita-809 Nov 25, 2021
977c94e
Merge branch 'likhita/group-server-implementation' of https://github.…
likhita-809 Nov 25, 2021
ca772c6
fix something
likhita-809 Nov 25, 2021
020e58f
add func to run TestSuite
likhita-809 Nov 25, 2021
140fbb8
Set accKeeper in group keeper
blushi Nov 25, 2021
abbe3ba
update keeper_test
likhita-809 Nov 25, 2021
8ed012a
Merge branch 'likhita/group-server-implementation' of https://github.…
likhita-809 Nov 25, 2021
bf9a57f
Fix timeout duration in test
blushi Nov 25, 2021
d11be84
Fix get group account
blushi Nov 25, 2021
edaa1c7
fix failed test case in keeper
likhita-809 Nov 26, 2021
177ac08
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into li…
likhita-809 Nov 26, 2021
50f3241
Fix some tests
blushi Nov 26, 2021
2c3a4c0
Merge branch 'likhita/group-server-implementation' of github.com:cosm…
blushi Nov 26, 2021
66db140
Rm unneeded set policy
blushi Nov 26, 2021
410e96d
Fix more tests related to ctx not being cached
blushi Nov 26, 2021
d76908e
Fix keeper_tests
blushi Nov 29, 2021
8c1f888
Rm derivation key
blushi Nov 29, 2021
334c667
Merge branch 'master' into likhita/group-server-implementation
blushi Nov 29, 2021
2f7f72c
proto-gen
blushi Nov 29, 2021
00b8cdb
go mod tidy
blushi Nov 29, 2021
10cefb7
Update proto wip
blushi Dec 1, 2021
1501297
Merge branch 'master' into likhita/group-server-implementation
blushi Dec 1, 2021
8a0f415
Fix tests
blushi Dec 1, 2021
52fad21
proto lint
blushi Dec 1, 2021
318d527
go mod tidy
blushi Dec 1, 2021
e2e26d0
Use existing TestMsg
blushi Dec 2, 2021
bdfa1f8
Merge branch 'master' into likhita/group-server-implementation
blushi Dec 2, 2021
12a2f32
go mod
blushi Dec 2, 2021
5422f59
Update x/group/keeper/msg_server.go
blushi Dec 2, 2021
6c8886d
Address review comments
blushi Dec 2, 2021
de9584d
Merge branch 'likhita/group-server-implementation' of github.com:cosm…
blushi Dec 2, 2021
b6442dc
tidy
blushi Dec 2, 2021
48c10a1
Merge branch 'master' into likhita/group-server-implementation
likhita-809 Dec 6, 2021
7cdeccc
Use BigEndian
blushi Dec 8, 2021
9bbcaba
Merge branch 'master' into likhita/group-server-implementation
blushi Dec 8, 2021
f911eda
Merge branch 'master' into likhita/group-server-implementation
blushi Dec 9, 2021
81dd664
Use derivation for group acc addr
blushi Dec 9, 2021
9f0b1e9
Merge branch 'master' into likhita/group-server-implementation
blushi Dec 10, 2021
f03ac5a
Rm sim
blushi Dec 10, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ type BaseApp struct { // nolint: maligned
db dbm.DB // common DB backend
cms sdk.CommitMultiStore // Main (uncached) state
storeLoader StoreLoader // function to handle store loading, may be overridden with SetStoreLoader()
router sdk.Router // handle any kind of message
queryRouter sdk.QueryRouter // router for redirecting query calls
grpcQueryRouter *GRPCQueryRouter // router for redirecting gRPC query calls
msgServiceRouter *MsgServiceRouter // router for redirecting Msg service messages
interfaceRegistry types.InterfaceRegistry
txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx

Expand Down Expand Up @@ -145,6 +147,7 @@ func NewBaseApp(
db: db,
cms: store.NewCommitMultiStore(db),
storeLoader: DefaultStoreLoader,
router: NewRouter(),
queryRouter: NewQueryRouter(),
grpcQueryRouter: NewGRPCQueryRouter(),
txDecoder: txDecoder,
Expand Down Expand Up @@ -187,6 +190,9 @@ func (app *BaseApp) Trace() bool {
return app.trace
}

// MsgServiceRouter returns the MsgServiceRouter of a BaseApp.
func (app *BaseApp) MsgServiceRouter() *MsgServiceRouter { return app.msgServiceRouter }

// MountStores mounts all IAVL or DB stores to the provided keys in the BaseApp
// multistore.
func (app *BaseApp) MountStores(keys ...storetypes.StoreKey) {
Expand Down Expand Up @@ -341,6 +347,17 @@ func (app *BaseApp) setIndexEvents(ie []string) {
}
}

// Router returns the router of the BaseApp.
func (app *BaseApp) Router() sdk.Router {
if app.sealed {
// We cannot return a Router when the app is sealed because we can't have
// any routes modified which would cause unexpected routing behavior.
panic("Router() on sealed BaseApp")
}

return app.router
}

// QueryRouter returns the QueryRouter of a BaseApp.
func (app *BaseApp) QueryRouter() sdk.QueryRouter { return app.queryRouter }

Expand Down
140 changes: 140 additions & 0 deletions baseapp/msg_service_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
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("can't register request type %T for service method %s", i, fqMethod))
}

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
}
41 changes: 41 additions & 0 deletions baseapp/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package baseapp

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

type Router struct {
routes map[string]sdk.Handler
}

var _ sdk.Router = NewRouter()

// NewRouter returns a reference to a new router.
func NewRouter() *Router {
return &Router{
routes: make(map[string]sdk.Handler),
}
}

// AddRoute adds a route path to the router with a given handler. The route must
// be alphanumeric.
func (rtr *Router) AddRoute(route sdk.Route) sdk.Router {
if !sdk.IsAlphaNumeric(route.Path()) {
panic("route expressions can only contain alphanumeric characters")
}
if rtr.routes[route.Path()] != nil {
panic(fmt.Sprintf("route %s has already been initialized", route.Path()))
}

rtr.routes[route.Path()] = route.Handler()
return rtr
}

// Route returns a handler for a given route path.
//
// TODO: Handle expressive matches.
func (rtr *Router) Route(_ sdk.Context, path string) sdk.Handler {
return rtr.routes[path]
}
Loading