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

Add RegisterQueryService to AppModule #6336

Merged
merged 13 commits into from
Jun 6, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
46 changes: 46 additions & 0 deletions tests/mocks/grpc_server.go

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

13 changes: 13 additions & 0 deletions tests/mocks/types_module_module.go

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

15 changes: 15 additions & 0 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {}

Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions types/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
11 changes: 7 additions & 4 deletions x/capability/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ 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"
"github.com/cosmos/cosmos-sdk/types/module"
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 (
Expand Down Expand Up @@ -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) {}

Expand Down
4 changes: 4 additions & 0 deletions x/crisis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"

"github.com/gogo/protobuf/grpc"

"github.com/gorilla/mux"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions x/evidence/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"math/rand"

"github.com/gogo/protobuf/grpc"

"github.com/gorilla/mux"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -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) {}

Expand Down
4 changes: 4 additions & 0 deletions x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"math/rand"

"github.com/gogo/protobuf/grpc"

"github.com/gorilla/mux"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions x/ibc-transfer/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"math/rand"

"github.com/gogo/protobuf/grpc"

"github.com/gorilla/mux"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions x/ibc/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/rand"

"github.com/gogo/protobuf/grpc"
"github.com/gorilla/mux"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions x/params/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions x/slashing/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"math/rand"

"github.com/gogo/protobuf/grpc"

"github.com/gorilla/mux"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -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 {
Expand Down
Loading