diff --git a/CHANGELOG.md b/CHANGELOG.md index bbf05de9a23b..5e952b10bf2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -114,6 +114,7 @@ be used to retrieve the actual proposal `Content`. Also the `NewMsgSubmitProposa - TxBuilder.SignStdTx * (client) [\#6290](https://github.com/cosmos/cosmos-sdk/pull/6290) `CLIContext` is renamed to `Context`. `Context` and all related methods have been moved from package context to client. * (modules) [\#6326](https://github.com/cosmos/cosmos-sdk/pull/6326) `AppModuleBasic.GetQueryCmd` now takes a single `CLIContext` parameter. +* (modules) [\#6336](https://github.com/cosmos/cosmos-sdk/pull/6336) `AppModuleBasic.RegisterQueryService` method was added to support gRPC queries, and `QuerierRoute` and `NewQuerierHandler` were deprecated. Migration guide: diff --git a/Makefile b/Makefile index 659de6a51cc4..5841acd8cbaa 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,7 @@ mocks: $(MOCKS_DIR) mockgen -source=types/invariant.go -package mocks -destination tests/mocks/types_invariant.go mockgen -source=types/router.go -package mocks -destination tests/mocks/types_router.go mockgen -source=types/handler.go -package mocks -destination tests/mocks/types_handler.go + mockgen -package mocks -destination tests/mocks/grpc_server.go github.com/gogo/protobuf/grpc Server .PHONY: mocks $(MOCKS_DIR): diff --git a/simapp/app.go b/simapp/app.go index e13a361b8af4..b7bf89cb7a73 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -304,6 +304,7 @@ func NewSimApp( app.mm.RegisterInvariants(&app.CrisisKeeper) app.mm.RegisterRoutes(app.Router(), app.QueryRouter()) + app.mm.RegisterQueryServices(app.GRPCQueryRouter()) // create the simulation manager and define the order of the modules for deterministic simulations // diff --git a/tests/mocks/grpc_server.go b/tests/mocks/grpc_server.go new file mode 100644 index 000000000000..91fedb7ae560 --- /dev/null +++ b/tests/mocks/grpc_server.go @@ -0,0 +1,46 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/gogo/protobuf/grpc (interfaces: Server) + +// Package mocks is a generated GoMock package. +package mocks + +import ( + gomock "github.com/golang/mock/gomock" + grpc "google.golang.org/grpc" + reflect "reflect" +) + +// MockServer is a mock of Server interface +type MockServer struct { + ctrl *gomock.Controller + recorder *MockServerMockRecorder +} + +// MockServerMockRecorder is the mock recorder for MockServer +type MockServerMockRecorder struct { + mock *MockServer +} + +// NewMockServer creates a new mock instance +func NewMockServer(ctrl *gomock.Controller) *MockServer { + mock := &MockServer{ctrl: ctrl} + mock.recorder = &MockServerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockServer) EXPECT() *MockServerMockRecorder { + return m.recorder +} + +// RegisterService mocks base method +func (m *MockServer) RegisterService(arg0 *grpc.ServiceDesc, arg1 interface{}) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "RegisterService", arg0, arg1) +} + +// RegisterService indicates an expected call of RegisterService +func (mr *MockServerMockRecorder) RegisterService(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterService", reflect.TypeOf((*MockServer)(nil).RegisterService), arg0, arg1) +} diff --git a/tests/mocks/types_module_module.go b/tests/mocks/types_module_module.go index a5f7efdd4941..2db37ef602aa 100644 --- a/tests/mocks/types_module_module.go +++ b/tests/mocks/types_module_module.go @@ -9,6 +9,7 @@ import ( client "github.com/cosmos/cosmos-sdk/client" codec "github.com/cosmos/cosmos-sdk/codec" types "github.com/cosmos/cosmos-sdk/types" + grpc "github.com/gogo/protobuf/grpc" gomock "github.com/golang/mock/gomock" mux "github.com/gorilla/mux" cobra "github.com/spf13/cobra" @@ -491,6 +492,18 @@ func (mr *MockAppModuleMockRecorder) NewQuerierHandler() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewQuerierHandler", reflect.TypeOf((*MockAppModule)(nil).NewQuerierHandler)) } +// RegisterQueryService mocks base method +func (m *MockAppModule) RegisterQueryService(arg0 grpc.Server) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "RegisterQueryService", arg0) +} + +// RegisterQueryService indicates an expected call of RegisterQueryService +func (mr *MockAppModuleMockRecorder) RegisterQueryService(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterQueryService", reflect.TypeOf((*MockAppModule)(nil).RegisterQueryService), arg0) +} + // BeginBlock mocks base method func (m *MockAppModule) BeginBlock(arg0 types.Context, arg1 types0.RequestBeginBlock) { m.ctrl.T.Helper() diff --git a/types/module/module.go b/types/module/module.go index 3723e4e58a9c..92ec90b3a491 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -31,6 +31,8 @@ package module import ( "encoding/json" + "github.com/gogo/protobuf/grpc" + "github.com/gorilla/mux" "github.com/spf13/cobra" abci "github.com/tendermint/tendermint/abci/types" @@ -141,8 +143,12 @@ type AppModule interface { // routes Route() string NewHandler() sdk.Handler + // Deprecated: use RegisterQueryService QuerierRoute() string + // Deprecated: use RegisterQueryService NewQuerierHandler() sdk.Querier + // RegisterQueryService allows a module to register a gRPC query service + RegisterQueryService(grpc.Server) // ABCI BeginBlock(sdk.Context, abci.RequestBeginBlock) @@ -178,6 +184,8 @@ func (GenesisOnlyAppModule) QuerierRoute() string { return "" } // NewQuerierHandler returns an empty module querier func (gam GenesisOnlyAppModule) NewQuerierHandler() sdk.Querier { return nil } +func (gam GenesisOnlyAppModule) RegisterQueryService(grpc.Server) {} + // BeginBlock returns an empty module begin-block func (gam GenesisOnlyAppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {} @@ -256,6 +264,13 @@ func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter) } } +// RegisterQueryServices registers all module query services +func (m *Manager) RegisterQueryServices(grpcRouter grpc.Server) { + for _, module := range m.Modules { + module.RegisterQueryService(grpcRouter) + } +} + // InitGenesis performs init genesis functionality for modules func (m *Manager) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, genesisData map[string]json.RawMessage) abci.ResponseInitChain { var validatorUpdates []abci.ValidatorUpdate diff --git a/types/module/module_test.go b/types/module/module_test.go index 6913c389ffcc..e3a63fe2a601 100644 --- a/types/module/module_test.go +++ b/types/module/module_test.go @@ -159,6 +159,25 @@ func TestManager_RegisterRoutes(t *testing.T) { mm.RegisterRoutes(router, queryRouter) } +func TestManager_RegisterQueryServices(t *testing.T) { + mockCtrl := gomock.NewController(t) + t.Cleanup(mockCtrl.Finish) + + mockAppModule1 := mocks.NewMockAppModule(mockCtrl) + mockAppModule2 := mocks.NewMockAppModule(mockCtrl) + mockAppModule1.EXPECT().Name().Times(2).Return("module1") + mockAppModule2.EXPECT().Name().Times(2).Return("module2") + mm := module.NewManager(mockAppModule1, mockAppModule2) + require.NotNil(t, mm) + require.Equal(t, 2, len(mm.Modules)) + + queryRouter := mocks.NewMockServer(mockCtrl) + mockAppModule1.EXPECT().RegisterQueryService(queryRouter).Times(1) + mockAppModule2.EXPECT().RegisterQueryService(queryRouter).Times(1) + + mm.RegisterQueryServices(queryRouter) +} + func TestManager_InitGenesis(t *testing.T) { mockCtrl := gomock.NewController(t) t.Cleanup(mockCtrl.Finish) diff --git a/x/auth/module.go b/x/auth/module.go index e0da77eec899..291654a114a3 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -5,6 +5,8 @@ import ( "fmt" "math/rand" + "github.com/gogo/protobuf/grpc" + "github.com/gorilla/mux" "github.com/spf13/cobra" abci "github.com/tendermint/tendermint/abci/types" @@ -118,6 +120,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.accountKeeper) } +func (am AppModule) RegisterQueryService(grpc.Server) {} + // InitGenesis performs genesis initialization for the auth module. It returns // no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate { diff --git a/x/bank/module.go b/x/bank/module.go index 00f89117a6d1..2461450f72bd 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -5,6 +5,8 @@ import ( "fmt" "math/rand" + "github.com/gogo/protobuf/grpc" + "github.com/gorilla/mux" "github.com/spf13/cobra" abci "github.com/tendermint/tendermint/abci/types" @@ -86,6 +88,8 @@ type AppModule struct { accountKeeper types.AccountKeeper } +func (am AppModule) RegisterQueryService(grpc.Server) {} + // NewAppModule creates a new AppModule object func NewAppModule(cdc codec.Marshaler, keeper Keeper, accountKeeper types.AccountKeeper) AppModule { return AppModule{ diff --git a/x/capability/module.go b/x/capability/module.go index 5c44837193c1..0ebd9aa2c27c 100644 --- a/x/capability/module.go +++ b/x/capability/module.go @@ -5,6 +5,11 @@ import ( "fmt" "math/rand" + "github.com/gogo/protobuf/grpc" + "github.com/gorilla/mux" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -12,10 +17,6 @@ import ( simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/capability/simulation" "github.com/cosmos/cosmos-sdk/x/capability/types" - - "github.com/gorilla/mux" - "github.com/spf13/cobra" - abci "github.com/tendermint/tendermint/abci/types" ) var ( @@ -105,6 +106,8 @@ func (am AppModule) NewHandler() sdk.Handler { return nil } // NewQuerierHandler returns the capability module's Querier. func (am AppModule) NewQuerierHandler() sdk.Querier { return nil } +func (am AppModule) RegisterQueryService(grpc.Server) {} + // RegisterInvariants registers the capability module's invariants. func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} diff --git a/x/crisis/module.go b/x/crisis/module.go index 524809a2d7b6..6111a289c9c4 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -4,6 +4,8 @@ import ( "encoding/json" "fmt" + "github.com/gogo/protobuf/grpc" + "github.com/gorilla/mux" "github.com/spf13/cobra" @@ -107,6 +109,8 @@ func (AppModule) QuerierRoute() string { return "" } // NewQuerierHandler returns no sdk.Querier. func (AppModule) NewQuerierHandler() sdk.Querier { return nil } +func (am AppModule) RegisterQueryService(grpc.Server) {} + // InitGenesis performs genesis initialization for the crisis module. It returns // no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate { diff --git a/x/distribution/module.go b/x/distribution/module.go index 246a75194d14..6848c012500a 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -5,6 +5,8 @@ import ( "fmt" "math/rand" + "github.com/gogo/protobuf/grpc" + "github.com/gorilla/mux" "github.com/spf13/cobra" abci "github.com/tendermint/tendermint/abci/types" @@ -136,6 +138,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) } +func (am AppModule) RegisterQueryService(grpc.Server) {} + // InitGenesis performs genesis initialization for the distribution module. It returns // no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate { diff --git a/x/evidence/module.go b/x/evidence/module.go index f2004447f5d5..2990d52555d1 100644 --- a/x/evidence/module.go +++ b/x/evidence/module.go @@ -5,6 +5,8 @@ import ( "fmt" "math/rand" + "github.com/gogo/protobuf/grpc" + "github.com/gorilla/mux" "github.com/spf13/cobra" @@ -144,6 +146,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) } +func (am AppModule) RegisterQueryService(grpc.Server) {} + // RegisterInvariants registers the evidence module's invariants. func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {} diff --git a/x/gov/module.go b/x/gov/module.go index dc54f18363f9..48ba7bddc928 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -7,6 +7,8 @@ import ( "fmt" "math/rand" + "github.com/gogo/protobuf/grpc" + "github.com/gorilla/mux" "github.com/spf13/cobra" @@ -152,6 +154,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) } +func (am AppModule) RegisterQueryService(grpc.Server) {} + // InitGenesis performs genesis initialization for the gov module. It returns // no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate { diff --git a/x/ibc-transfer/module.go b/x/ibc-transfer/module.go index 31eac4313588..45eea63a3758 100644 --- a/x/ibc-transfer/module.go +++ b/x/ibc-transfer/module.go @@ -5,6 +5,8 @@ import ( "fmt" "math/rand" + "github.com/gogo/protobuf/grpc" + "github.com/gorilla/mux" "github.com/spf13/cobra" @@ -122,6 +124,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return nil } +func (am AppModule) RegisterQueryService(grpc.Server) {} + // InitGenesis performs genesis initialization for the ibc transfer module. It returns // no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate { diff --git a/x/ibc/module.go b/x/ibc/module.go index fa6725048478..8c1e51bab85f 100644 --- a/x/ibc/module.go +++ b/x/ibc/module.go @@ -5,6 +5,7 @@ import ( "fmt" "math/rand" + "github.com/gogo/protobuf/grpc" "github.com/gorilla/mux" "github.com/spf13/cobra" @@ -126,6 +127,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(*am.keeper) } +func (am AppModule) RegisterQueryService(grpc.Server) {} + // InitGenesis performs genesis initialization for the ibc module. It returns // no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, bz json.RawMessage) []abci.ValidatorUpdate { diff --git a/x/mint/module.go b/x/mint/module.go index 53d51f1397ae..5c03561b7646 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -5,6 +5,8 @@ import ( "fmt" "math/rand" + "github.com/gogo/protobuf/grpc" + "github.com/gorilla/mux" "github.com/spf13/cobra" abci "github.com/tendermint/tendermint/abci/types" @@ -113,6 +115,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) } +func (am AppModule) RegisterQueryService(grpc.Server) {} + // InitGenesis performs genesis initialization for the mint module. It returns // no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate { diff --git a/x/params/module.go b/x/params/module.go index 3f2f797f93bf..8811752cebbe 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -4,6 +4,8 @@ import ( "encoding/json" "math/rand" + "github.com/gogo/protobuf/grpc" + "github.com/gorilla/mux" "github.com/spf13/cobra" abci "github.com/tendermint/tendermint/abci/types" @@ -97,6 +99,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) } +func (am AppModule) RegisterQueryService(grpc.Server) {} + // ProposalContents returns all the params content functions used to // simulate governance proposals. func (am AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent { diff --git a/x/slashing/module.go b/x/slashing/module.go index c7304e625689..d8f1627311af 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -5,6 +5,8 @@ import ( "fmt" "math/rand" + "github.com/gogo/protobuf/grpc" + "github.com/gorilla/mux" "github.com/spf13/cobra" @@ -127,6 +129,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) } +func (am AppModule) RegisterQueryService(grpc.Server) {} + // InitGenesis performs genesis initialization for the slashing module. It returns // no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate { diff --git a/x/staking/module.go b/x/staking/module.go index 56cee71aaf4b..693496db1bb2 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -5,6 +5,8 @@ import ( "fmt" "math/rand" + "github.com/gogo/protobuf/grpc" + "github.com/gorilla/mux" "github.com/spf13/cobra" flag "github.com/spf13/pflag" @@ -151,6 +153,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) } +func (am AppModule) RegisterQueryService(grpc.Server) {} + // InitGenesis performs genesis initialization for the staking module. It returns // no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate { diff --git a/x/upgrade/module.go b/x/upgrade/module.go index 223b1eb31e6f..2bf29a698687 100644 --- a/x/upgrade/module.go +++ b/x/upgrade/module.go @@ -3,6 +3,8 @@ package upgrade import ( "encoding/json" + "github.com/gogo/protobuf/grpc" + "github.com/gorilla/mux" "github.com/spf13/cobra" @@ -109,6 +111,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) } +func (am AppModule) RegisterQueryService(grpc.Server) {} + // InitGenesis is ignored, no sense in serializing future upgrades func (am AppModule) InitGenesis(_ sdk.Context, _ codec.JSONMarshaler, _ json.RawMessage) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{}