Skip to content

Commit

Permalink
Refactor RegisterQueryServices -> RegisterServices (#7518)
Browse files Browse the repository at this point in the history
* Refactor RegisterQueryServices -> RegisterServices

* Fix tests
  • Loading branch information
aaronc committed Oct 12, 2020
1 parent 647ad0d commit 228728c
Show file tree
Hide file tree
Showing 21 changed files with 84 additions and 71 deletions.
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func NewSimApp(

app.mm.RegisterInvariants(&app.CrisisKeeper)
app.mm.RegisterRoutes(app.Router(), app.QueryRouter(), encodingConfig.Amino)
app.mm.RegisterQueryServices(app.GRPCQueryRouter())
app.mm.RegisterServices(module.NewConfigurator(app.GRPCQueryRouter()))

// add test gRPC service for testing gRPC queries in isolation
testdata.RegisterTestServiceServer(app.GRPCQueryRouter(), testdata.TestServiceImpl{})
Expand Down
26 changes: 13 additions & 13 deletions tests/mocks/types_module_module.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions types/module/configurator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package module

import "github.com/gogo/protobuf/grpc"

// 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 {
QueryServer() grpc.Server
}

type configurator struct {
queryServer grpc.Server
}

// NewConfigurator returns a new Configurator instance
func NewConfigurator(queryServer grpc.Server) Configurator {
return configurator{queryServer: queryServer}
}

var _ Configurator = configurator{}

// QueryServer implements the Configurator.QueryServer method
func (c configurator) QueryServer() grpc.Server {
return c.queryServer
}
15 changes: 7 additions & 8 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ package module
import (
"encoding/json"

"github.com/gogo/protobuf/grpc"
"github.com/grpc-ecosystem/grpc-gateway/runtime"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -173,8 +172,8 @@ type AppModule interface {
// Deprecated: use RegisterQueryService
LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier

// RegisterQueryService allows a module to register a gRPC query service
RegisterQueryService(grpc.Server)
// RegisterServices allows a module to register services
RegisterServices(Configurator)

// ABCI
BeginBlock(sdk.Context, abci.RequestBeginBlock)
Expand Down Expand Up @@ -207,8 +206,8 @@ func (GenesisOnlyAppModule) QuerierRoute() string { return "" }
// LegacyQuerierHandler returns an empty module querier
func (gam GenesisOnlyAppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier { return nil }

// RegisterQueryService registers all gRPC query services.
func (gam GenesisOnlyAppModule) RegisterQueryService(grpc.Server) {}
// RegisterServices registers all services.
func (gam GenesisOnlyAppModule) RegisterServices(Configurator) {}

// BeginBlock returns an empty module begin-block
func (gam GenesisOnlyAppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {}
Expand Down Expand Up @@ -288,10 +287,10 @@ func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter,
}
}

// RegisterQueryServices registers all module query services
func (m *Manager) RegisterQueryServices(grpcRouter grpc.Server) {
// RegisterServices registers all module services
func (m *Manager) RegisterServices(cfg Configurator) {
for _, module := range m.Modules {
module.RegisterQueryService(grpcRouter)
module.RegisterServices(cfg)
}
}

Expand Down
11 changes: 6 additions & 5 deletions types/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ func TestManager_RegisterRoutes(t *testing.T) {
mockAppModule1.EXPECT().QuerierRoute().Times(2).Return("querierRoute1")
mockAppModule2.EXPECT().QuerierRoute().Times(1).Return("")
handler3 := sdk.Querier(nil)
mockAppModule1.EXPECT().NewQuerierHandler().Times(1).Return(handler3)
amino := codec.NewLegacyAmino()
mockAppModule1.EXPECT().LegacyQuerierHandler(amino).Times(1).Return(handler3)
queryRouter.EXPECT().AddRoute(gomock.Eq("querierRoute1"), gomock.Eq(handler3)).Times(1)

amino := codec.NewLegacyAmino()
mm.RegisterRoutes(router, queryRouter, amino)
}

Expand All @@ -182,10 +182,11 @@ func TestManager_RegisterQueryServices(t *testing.T) {
require.Equal(t, 2, len(mm.Modules))

queryRouter := mocks.NewMockServer(mockCtrl)
mockAppModule1.EXPECT().RegisterQueryService(queryRouter).Times(1)
mockAppModule2.EXPECT().RegisterQueryService(queryRouter).Times(1)
cfg := module.NewConfigurator(queryRouter)
mockAppModule1.EXPECT().RegisterServices(cfg).Times(1)
mockAppModule2.EXPECT().RegisterServices(cfg).Times(1)

mm.RegisterQueryServices(queryRouter)
mm.RegisterServices(cfg)
}

func TestManager_InitGenesis(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"math/rand"

"github.com/gogo/protobuf/grpc"
"github.com/grpc-ecosystem/grpc-gateway/runtime"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -128,8 +127,8 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryServer(server, am.accountKeeper)
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryServer(cfg.QueryServer(), am.accountKeeper)
}

// InitGenesis performs genesis initialization for the auth module. It returns
Expand Down
3 changes: 1 addition & 2 deletions x/auth/vesting/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vesting
import (
"encoding/json"

"github.com/gogo/protobuf/grpc"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -102,7 +101,7 @@ func (am AppModule) Route() sdk.Route {
func (AppModule) QuerierRoute() string { return "" }

// RegisterQueryService performs a no-op.
func (am AppModule) RegisterQueryService(_ grpc.Server) {}
func (am AppModule) RegisterServices(_ module.Configurator) {}

// LegacyQuerierHandler performs a no-op.
func (am AppModule) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier {
Expand Down
5 changes: 2 additions & 3 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"math/rand"

"github.com/gogo/protobuf/grpc"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -97,8 +96,8 @@ type AppModule struct {

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryServer(server, am.keeper)
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}

// NewAppModule creates a new AppModule object
Expand Down
3 changes: 1 addition & 2 deletions x/capability/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"math/rand"

"github.com/gogo/protobuf/grpc"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -114,7 +113,7 @@ func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier { retur

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(grpc.Server) {}
func (am AppModule) RegisterServices(module.Configurator) {}

// RegisterInvariants registers the capability module's invariants.
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
Expand Down
3 changes: 1 addition & 2 deletions x/crisis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"

"github.com/gogo/protobuf/grpc"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -115,7 +114,7 @@ func (AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier { return n

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(grpc.Server) {}
func (am AppModule) RegisterServices(module.Configurator) {}

// InitGenesis performs genesis initialization for the crisis module. It returns
// no validator updates.
Expand Down
5 changes: 2 additions & 3 deletions x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"math/rand"

"github.com/gogo/protobuf/grpc"
"github.com/grpc-ecosystem/grpc-gateway/runtime"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -142,8 +141,8 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryServer(server, am.keeper)
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}

// InitGenesis performs genesis initialization for the distribution module. It returns
Expand Down
5 changes: 2 additions & 3 deletions x/evidence/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"math/rand"

"github.com/gogo/protobuf/grpc"
"github.com/grpc-ecosystem/grpc-gateway/runtime"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -151,8 +150,8 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryServer(server, am.keeper)
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}

// RegisterInvariants registers the evidence module's invariants.
Expand Down
5 changes: 2 additions & 3 deletions x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"math/rand"

"github.com/gogo/protobuf/grpc"
"github.com/grpc-ecosystem/grpc-gateway/runtime"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -158,8 +157,8 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryServer(server, am.keeper)
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}

// InitGenesis performs genesis initialization for the gov module. It returns
Expand Down
5 changes: 2 additions & 3 deletions x/ibc/applications/transfer/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"math/rand"

"github.com/gogo/protobuf/grpc"
"github.com/grpc-ecosystem/grpc-gateway/runtime"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -123,8 +122,8 @@ func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier {

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryServer(server, am.keeper)
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}

// InitGenesis performs genesis initialization for the ibc-transfer module. It returns
Expand Down
5 changes: 2 additions & 3 deletions x/ibc/core/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"math/rand"

"github.com/gogo/protobuf/grpc"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -132,8 +131,8 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd
}

// RegisterQueryService registers the gRPC query service for the ibc module.
func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryService(server, am.keeper)
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryService(cfg.QueryServer(), am.keeper)
}

// InitGenesis performs genesis initialization for the ibc module. It returns
Expand Down
5 changes: 3 additions & 2 deletions x/ibc/testing/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package mock
import (
"encoding/json"

"github.com/gogo/protobuf/grpc"
"github.com/cosmos/cosmos-sdk/types/module"

"github.com/grpc-ecosystem/grpc-gateway/runtime"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -102,7 +103,7 @@ func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier {
}

// RegisterQueryService implements the AppModule interface.
func (am AppModule) RegisterQueryService(server grpc.Server) {}
func (am AppModule) RegisterServices(module.Configurator) {}

// InitGenesis implements the AppModule interface.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate {
Expand Down
5 changes: 2 additions & 3 deletions x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"math/rand"

"github.com/gogo/protobuf/grpc"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -126,8 +125,8 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd

// RegisterQueryService registers a gRPC query service to respond to the
// module-specific gRPC queries.
func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryServer(server, am.keeper)
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}

// InitGenesis performs genesis initialization for the mint module. It returns
Expand Down
Loading

0 comments on commit 228728c

Please sign in to comment.