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(gov): app-wiring for x/gov #12369

Merged
merged 19 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
560 changes: 560 additions & 0 deletions api/cosmos/gov/module/v1/module.pulsar.go

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions proto/cosmos/gov/module/v1/module.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";

package cosmos.gov.module.v1;

import "cosmos/app/v1alpha1/module.proto";

// Module is the config object of the gov module.
message Module {
option (cosmos.app.v1alpha1.module) = {
go_import: "github.com/cosmos/cosmos-sdk/x/gov"
};

uint64 max_metadata_len = 1;
}
10 changes: 5 additions & 5 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,15 @@ func NewSimApp(
AddRoute(paramproposal.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper)).
AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)).
AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper))
govConfig := govtypes.DefaultConfig()
//govConfig := govtypes.DefaultConfig()
/*
Example of setting gov params:
govConfig.MaxMetadataLen = 10000
*/
govKeeper := govkeeper.NewKeeper(
app.appCodec, app.keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
app.StakingKeeper, govRouter, app.MsgServiceRouter(), govConfig,
)
//govKeeper := govkeeper.NewKeeper(
// app.appCodec, app.keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
// app.StakingKeeper, govRouter, app.MsgServiceRouter(), govConfig,
//)

app.GovKeeper = *govKeeper.SetHooks(
govtypes.NewMultiGovHooks(
Expand Down
3 changes: 3 additions & 0 deletions x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"math/rand"

gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
Expand Down Expand Up @@ -240,6 +241,7 @@ type distrOutputs struct {
DistrKeeper keeper.Keeper
Module runtime.AppModuleWrapper
Hooks staking.StakingHooksWrapper
GovHandler govv1beta1.RoutedHandler
}

func provideModule(in distrInputs) distrOutputs {
Expand All @@ -250,5 +252,6 @@ func provideModule(in distrInputs) distrOutputs {
DistrKeeper: k,
Module: runtime.WrapAppModule(m),
Hooks: staking.StakingHooksWrapper{StakingHooks: k.Hooks()},
GovHandler: govv1beta1.RoutedHandler{Handler: NewCommunityPoolSpendProposalHandler(k), RouteKey: types.RouterKey},
}
}
33 changes: 17 additions & 16 deletions x/gov/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,27 @@ type Keeper struct {
func NewKeeper(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this requires a changelog

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100% this is api breaking

cdc codec.BinaryCodec, key storetypes.StoreKey, paramSpace types.ParamSubspace,
authKeeper types.AccountKeeper, bankKeeper types.BankKeeper, sk types.StakingKeeper,
legacyRouter v1beta1.Router, router *baseapp.MsgServiceRouter,
config types.Config,
router *baseapp.MsgServiceRouter, config types.Config,
) Keeper {
// ensure governance module account is set
if addr := authKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}

// It is vital to seal the governance proposal router here as to not allow
// further handlers to be registered after the keeper is created since this
// could create invalid or non-deterministic behavior.
legacyRouter.Seal()

// If MaxMetadataLen not set by app developer, set to default value.
if config.MaxMetadataLen == 0 {
config.MaxMetadataLen = types.DefaultConfig().MaxMetadataLen
}

return Keeper{
storeKey: key,
paramSpace: paramSpace,
authKeeper: authKeeper,
bankKeeper: bankKeeper,
sk: sk,
cdc: cdc,
legacyRouter: legacyRouter,
router: router,
config: config,
storeKey: key,
paramSpace: paramSpace,
authKeeper: authKeeper,
bankKeeper: bankKeeper,
sk: sk,
cdc: cdc,
router: router,
config: config,
}
}

Expand All @@ -97,6 +90,14 @@ func (keeper *Keeper) SetHooks(gh types.GovHooks) *Keeper {
return keeper
}

func (keeper *Keeper) SetLegacyRouter(router v1beta1.Router) {
// It is vital to seal the governance proposal router here as to not allow
// further handlers to be registered after the keeper is created since this
// could create invalid or non-deterministic behavior.
router.Seal()
keeper.legacyRouter = router
}

// Logger returns a module-specific logger.
func (keeper Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
Expand Down
50 changes: 48 additions & 2 deletions x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ package gov

import (
"context"
modulev1 "cosmossdk.io/api/cosmos/gov/module/v1"
"cosmossdk.io/core/appmodule"
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/runtime"
store "github.com/cosmos/cosmos-sdk/store/types"
"math/rand"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"

Expand Down Expand Up @@ -74,7 +79,7 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncod
}

// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the gov module.
func (a AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
func (a AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *gwruntime.ServeMux) {
if err := v1.RegisterQueryHandlerClient(context.Background(), mux, v1.NewQueryClient(clientCtx)); err != nil {
panic(err)
}
Expand Down Expand Up @@ -128,6 +133,47 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper,
}
}

func init() {
appmodule.Register(
&modulev1.Module{},
appmodule.Provide(provideModuleBasic, provideModule),
appmodule.Invoke(invokeAddRoutes))
}

func provideModuleBasic() runtime.AppModuleBasicWrapper {
return runtime.WrapAppModuleBasic(AppModuleBasic{})
}

func provideModule(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency I'd think we should use a struct named govInputs and govOutputs.

config *modulev1.Module,
cdc codec.Codec,
key *store.KVStoreKey,
subSpace types.ParamSubspace,
msgServiceRouter *baseapp.MsgServiceRouter,
ak types.AccountKeeper,
bk types.BankKeeper,
sk types.StakingKeeper) (runtime.AppModuleWrapper, v1beta1.RoutedHandler) {

kConfig := types.DefaultConfig()
if config.MaxMetadataLen != 0 {
kConfig.MaxMetadataLen = config.MaxMetadataLen
}

k := keeper.NewKeeper(cdc, key, subSpace, ak, bk, sk, msgServiceRouter, kConfig)
m := NewAppModule(cdc, k, ak, bk)
return runtime.WrapAppModule(m), v1beta1.RoutedHandler{Handler: v1beta1.ProposalHandler, RouteKey: types.RouterKey}
}

func invokeAddRoutes(
keeper keeper.Keeper,
routes []v1beta1.RoutedHandler) {
router := v1beta1.NewRouter()
for _, r := range routes {
router.AddRoute(r.RouteKey, r.Handler)
}
keeper.SetLegacyRouter(router)
}

// Name returns the gov module's name.
func (AppModule) Name() string {
return types.ModuleName
Expand Down
8 changes: 8 additions & 0 deletions x/gov/types/v1beta1/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ type Content interface {
// Handler defines a function that handles a proposal after it has passed the
// governance process.
type Handler func(ctx sdk.Context, content Content) error

type RoutedHandler struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type RoutedHandler struct {
type HandlerRoute struct {

Handler Handler
RouteKey string
}

// ManyPerContainer implements the depinject.ManyPerContainer interface.
func (RoutedHandler) ManyPerContainer() {}
5 changes: 4 additions & 1 deletion x/params/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package params
import (
"context"
"encoding/json"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"math/rand"

gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
Expand Down Expand Up @@ -187,6 +188,7 @@ type paramsOutputs struct {
ParamsKeeper keeper.Keeper
BaseAppOption runtime.BaseAppOption
Module runtime.AppModuleWrapper
GovHandler govv1beta1.RoutedHandler
}

func provideModule(in paramsInputs) paramsOutputs {
Expand All @@ -195,8 +197,9 @@ func provideModule(in paramsInputs) paramsOutputs {
app.SetParamStore(k.Subspace(baseapp.Paramspace).WithKeyTable(types.ConsensusParamsKeyTable()))
}
m := runtime.WrapAppModule(NewAppModule(k))
govHandler := govv1beta1.RoutedHandler{RouteKey: proposal.RouterKey, Handler: NewParamChangeProposalHandler(k)}

return paramsOutputs{ParamsKeeper: k, BaseAppOption: baseappOpt, Module: m}
return paramsOutputs{ParamsKeeper: k, BaseAppOption: baseappOpt, Module: m, GovHandler: govHandler}
}

func provideSubSpace(key depinject.ModuleKey, k keeper.Keeper) types.Subspace {
Expand Down