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: Implement RegisterServices for CoreAPI in module manager #15133

Merged
merged 26 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
33336d2
feat: Implement RegisterServices for CoreAPI modules
facundomedica Feb 23, 2023
66d05dd
suggestions from Aaron
facundomedica Feb 23, 2023
6a45294
return err
facundomedica Feb 23, 2023
5067402
fix autocli
facundomedica Feb 23, 2023
71963fa
Merge branch 'main' into facu/coreapi-service-2
facundomedica Feb 23, 2023
0b66d23
Merge branch 'main' into facu/coreapi-service-2
facundomedica Feb 23, 2023
66c71ef
Merge branch 'main' into facu/coreapi-service-2
tac0turtle Feb 24, 2023
223cc26
Merge branch 'main' into facu/coreapi-service-2
facundomedica Feb 24, 2023
0fd5a01
Merge branch 'main' into facu/coreapi-service-2
facundomedica Feb 27, 2023
51722d1
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu…
facundomedica Feb 28, 2023
550aed9
update to core v0.6.0
facundomedica Feb 28, 2023
24064ca
Merge branch 'facu/coreapi-service-2' of https://github.com/cosmos/co…
facundomedica Feb 28, 2023
fe96c15
go mod tidy simapp
facundomedica Feb 28, 2023
109bd53
go mod tidy tests
facundomedica Feb 28, 2023
4edb500
update to use appmodule.HasServices when needed
facundomedica Feb 28, 2023
55df21d
update to use appmodule.HasServices when needed
facundomedica Feb 28, 2023
a5dd4c1
Merge branch 'main' into facu/coreapi-service-2
facundomedica Feb 28, 2023
90a6226
Merge branch 'main' into facu/coreapi-service-2
facundomedica Mar 1, 2023
d7e3d9c
Merge branch 'main' into facu/coreapi-service-2
facundomedica Mar 1, 2023
49c3a05
Merge branch 'main' into facu/coreapi-service-2
facundomedica Mar 2, 2023
2275d6c
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu…
facundomedica Mar 2, 2023
4e86967
de-duplicate code in autocli + correct implementation of the configur…
facundomedica Mar 2, 2023
62b2989
Merge branch 'facu/coreapi-service-2' of https://github.com/cosmos/co…
facundomedica Mar 2, 2023
849416b
Merge branch 'main' into facu/coreapi-service-2
facundomedica Mar 2, 2023
eff5ec5
cl++
facundomedica Mar 2, 2023
4a348b2
Merge branch 'facu/coreapi-service-2' of https://github.com/cosmos/co…
facundomedica Mar 2, 2023
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
27 changes: 27 additions & 0 deletions types/module/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@ import (
"fmt"

"github.com/cosmos/gogoproto/grpc"
"github.com/cosmos/gogoproto/proto"
googlegrpc "google.golang.org/grpc"
protobuf "google.golang.org/protobuf/proto"

errorsmod "cosmossdk.io/errors"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

cosmosmsg "cosmossdk.io/api/cosmos/msg/v1"
)

// Configurator provides the hooks to allow modules to configure and register
// their services in the RegisterServices method. It is designed to eventually
// support module object capabilities isolation as described in
// https://github.com/cosmos/cosmos-sdk/issues/7093
type Configurator interface {
grpc.Server

// MsgServer returns a grpc.Server instance which allows registering services
// that will handle TxBody.messages in transactions. These Msg's WILL NOT
// be exposed as gRPC services.
Expand Down Expand Up @@ -47,6 +54,26 @@ type configurator struct {
migrations map[string]map[uint64]MigrationHandler
}

// RegisterService implements Configurator
func (c configurator) RegisterService(sd *googlegrpc.ServiceDesc, ss interface{}) {
fds, err := proto.MergedFileDescriptors()
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic(err)
}

for _, fd := range fds.File {
for _, svc := range fd.GetService() {
if sd.ServiceName == fd.GetPackage()+"."+svc.GetName() {
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
if protobuf.HasExtension(svc.Options, cosmosmsg.E_Service) {
c.msgServer.RegisterService(sd, ss)
} else {
c.queryServer.RegisterService(sd, ss)
}
}
}
}
}

// NewConfigurator returns a new Configurator instance
func NewConfigurator(cdc codec.Codec, msgServer grpc.Server, queryServer grpc.Server) Configurator {
return configurator{
Expand Down
8 changes: 8 additions & 0 deletions types/module/core_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
var (
_ AppModuleBasic = coreAppModuleBasicAdapator{}
_ HasGenesis = coreAppModuleBasicAdapator{}
_ HasServices = coreAppModuleBasicAdapator{}
)

// CoreAppModuleBasicAdaptor wraps the core API module as an AppModule that this version
Expand Down Expand Up @@ -161,3 +162,10 @@ func (c coreAppModuleBasicAdapator) RegisterLegacyAminoCodec(amino *codec.Legacy
mod.RegisterLegacyAminoCodec(amino)
}
}

// RegisterServices implements HasServices
func (c coreAppModuleBasicAdapator) RegisterServices(cfg Configurator) {
if module, ok := c.module.(appmodule.HasServices); ok {
module.RegisterServices(cfg)
}
}
2 changes: 2 additions & 0 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ func (m *Manager) RegisterServices(cfg Configurator) {
for _, module := range m.Modules {
if module, ok := module.(HasServices); ok {
module.RegisterServices(cfg)
} else if module, ok := module.(appmodule.HasServices); ok {
module.RegisterServices(cfg)
}
}
}
Expand Down
21 changes: 17 additions & 4 deletions types/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import (
"github.com/golang/mock/gomock"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil/mock"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

var errFoo = errors.New("dummy")
Expand Down Expand Up @@ -155,7 +157,7 @@ func TestManager_RegisterQueryServices(t *testing.T) {

mockAppModule1 := mock.NewMockAppModuleWithAllExtensions(mockCtrl)
mockAppModule2 := mock.NewMockAppModuleWithAllExtensions(mockCtrl)
mockAppModule3 := mock.NewMockCoreAppModule(mockCtrl)
mockAppModule3 := MockCoreAppModule{}
mockAppModule1.EXPECT().Name().Times(2).Return("module1")
mockAppModule2.EXPECT().Name().Times(2).Return("module2")
// TODO: This is not working for Core API modules yet
Expand All @@ -164,14 +166,17 @@ func TestManager_RegisterQueryServices(t *testing.T) {
require.Equal(t, 3, len(mm.Modules))

msgRouter := mock.NewMockServer(mockCtrl)
msgRouter.EXPECT().RegisterService(gomock.Any(), gomock.Any()).Times(1)
queryRouter := mock.NewMockServer(mockCtrl)
queryRouter.EXPECT().RegisterService(gomock.Any(), gomock.Any()).Times(1)

interfaceRegistry := types.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
cfg := module.NewConfigurator(cdc, msgRouter, queryRouter)
mockAppModule1.EXPECT().RegisterServices(cfg).Times(1)
mockAppModule2.EXPECT().RegisterServices(cfg).Times(1)

mm.RegisterServices(cfg)
require.NotPanics(t, func() { mm.RegisterServices(cfg) })
}

func TestManager_InitGenesis(t *testing.T) {
Expand Down Expand Up @@ -484,6 +489,13 @@ func TestCoreAPIManager_EndBlock(t *testing.T) {
// MockCoreAppModule allows us to test functions like DefaultGenesis
type MockCoreAppModule struct{}

// RegisterServices implements appmodule.HasServices
func (MockCoreAppModule) RegisterServices(reg grpc.ServiceRegistrar) {
// Use Auth's service definitions as a placeholder
authtypes.RegisterQueryServer(reg, &authtypes.UnimplementedQueryServer{})
authtypes.RegisterMsgServer(reg, &authtypes.UnimplementedMsgServer{})
}

func (MockCoreAppModule) IsOnePerModuleType() {}
func (MockCoreAppModule) IsAppModule() {}
func (MockCoreAppModule) DefaultGenesis(target appmodule.GenesisTarget) error {
Expand Down Expand Up @@ -523,6 +535,7 @@ func (MockCoreAppModule) ExportGenesis(ctx context.Context, target appmodule.Gen
}

var (
_ appmodule.AppModule = MockCoreAppModule{}
_ appmodule.HasGenesis = MockCoreAppModule{}
_ appmodule.AppModule = MockCoreAppModule{}
_ appmodule.HasGenesis = MockCoreAppModule{}
_ appmodule.HasServices = MockCoreAppModule{}
)